Skip to content

Commit

Permalink
Add validation for empty ids before api calling
Browse files Browse the repository at this point in the history
  • Loading branch information
cp-sneh-s committed Jan 10, 2025
1 parent 6391d5e commit 909e7f8
Show file tree
Hide file tree
Showing 6 changed files with 64 additions and 37 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -37,18 +37,24 @@ class ApiJourneyService @Inject constructor(
private val currentSpaceId: String
get() = userPreferences.currentSpace ?: ""

private fun spaceMemberRef(spaceId: String) =
spaceRef.document(spaceId.takeIf { it.isNotBlank() } ?: "null")
.collection(FIRESTORE_COLLECTION_SPACE_MEMBERS)
private fun spaceMemberRef(spaceId: String) = if (spaceId.isBlank()) {
throw IllegalArgumentException("Space ID is empty")
} else {
spaceRef.document(spaceId).collection(FIRESTORE_COLLECTION_SPACE_MEMBERS)
}

private fun spaceMemberJourneyRef(spaceId: String, userId: String) =
spaceMemberRef(spaceId).document(userId.takeIf { it.isNotBlank() } ?: "null")
.collection(Config.FIRESTORE_COLLECTION_USER_JOURNEYS)
private fun spaceMemberJourneyRef(spaceId: String, userId: String) = if (userId.isBlank()) {
throw IllegalArgumentException("User ID is empty")
} else {
spaceMemberRef(spaceId).document(userId).collection(Config.FIRESTORE_COLLECTION_USER_JOURNEYS)
}

private fun spaceGroupKeysRef(spaceId: String) =
spaceRef.document(spaceId.takeIf { it.isNotBlank() } ?: "null")
.collection(FIRESTORE_COLLECTION_SPACE_GROUP_KEYS)
private fun spaceGroupKeysRef(spaceId: String) = if (spaceId.isBlank()) {
throw IllegalArgumentException("Space ID is empty")
} else {
spaceRef.document(spaceId).collection(FIRESTORE_COLLECTION_SPACE_GROUP_KEYS)
.document(FIRESTORE_COLLECTION_SPACE_GROUP_KEYS)
}

private suspend fun getGroupKeyDoc(spaceId: String): GroupKeysDoc? {
return try {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,18 +51,25 @@ class ApiLocationService @Inject constructor(

private val spaceRef by lazy { db.collection(FIRESTORE_COLLECTION_SPACES) }

private fun spaceMemberRef(spaceId: String) =
spaceRef.document(spaceId.takeIf { it.isNotBlank() } ?: "null")
.collection(FIRESTORE_COLLECTION_SPACE_MEMBERS)
private fun spaceMemberRef(spaceId: String) = if (spaceId.isBlank()) {
throw IllegalArgumentException("Space ID cannot be blank")
} else {
spaceRef.document(spaceId).collection(FIRESTORE_COLLECTION_SPACE_MEMBERS)
}

private fun spaceMemberLocationRef(spaceId: String, userId: String) =
spaceMemberRef(spaceId.takeIf { it.isNotBlank() } ?: "null").document(userId)
private fun spaceMemberLocationRef(spaceId: String, userId: String) = if (spaceId.isBlank()) {
throw IllegalArgumentException("Space ID cannot be blank")
} else {
spaceMemberRef(spaceId).document(userId)
.collection(Config.FIRESTORE_COLLECTION_USER_LOCATIONS)
}

private fun spaceGroupKeysRef(spaceId: String) =
spaceRef.document(spaceId.takeIf { it.isNotBlank() } ?: "null")
.collection(FIRESTORE_COLLECTION_SPACE_GROUP_KEYS)
private fun spaceGroupKeysRef(spaceId: String) = if (spaceId.isBlank()) {
throw IllegalArgumentException("Space ID cannot be blank")
} else {
spaceRef.document(spaceId).collection(FIRESTORE_COLLECTION_SPACE_GROUP_KEYS)
.document(FIRESTORE_COLLECTION_SPACE_GROUP_KEYS)
}

suspend fun saveLastKnownLocation(userId: String) {
val lastLocation = locationManager.getLastLocation() ?: return
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,12 +28,11 @@ class ApiMessagesService @Inject constructor(

private val threadRef = db.collection(FIRESTORE_COLLECTION_SPACE_THREADS)

private fun threadMessagesRef(threadId: String) =
threadRef.document(
threadId.takeIf {
it.isNotBlank()
} ?: "null"
).collection(Config.FIRESTORE_COLLECTION_THREAD_MESSAGES)
private fun threadMessagesRef(threadId: String) = if (threadId.isBlank()) {
throw IllegalArgumentException("Thread ID cannot be empty")
} else {
threadRef.document(threadId).collection(Config.FIRESTORE_COLLECTION_THREAD_MESSAGES)
}

suspend fun createThread(spaceId: String, adminId: String): String {
val docRef = threadRef.document()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,14 +21,21 @@ class ApiPlaceService @Inject constructor(

private val spaceRef = db.collection(Config.FIRESTORE_COLLECTION_SPACES)

private fun spacePlacesRef(spaceId: String) =
spaceRef.document(spaceId.takeIf { it.isNotBlank() } ?: "null").collection(Config.FIRESTORE_COLLECTION_SPACE_PLACES)
private fun spacePlacesRef(spaceId: String) = if (spaceId.isEmpty()) {
throw IllegalStateException("Space ID is empty")
} else {
spaceRef.document(spaceId).collection(Config.FIRESTORE_COLLECTION_SPACE_PLACES)
}

private fun spacePlacesSettingsRef(spaceId: String, placeId: String) =
spaceRef.document(spaceId.takeIf { it.isNotBlank() } ?: "null").collection(Config.FIRESTORE_COLLECTION_SPACE_PLACES)
.document(placeId.takeIf { it.isNotBlank() } ?: "null").collection(
Config.FIRESTORE_COLLECTION_SPACE_PLACES_MEMBER_SETTINGS
)
if (spaceId.isEmpty() || placeId.isEmpty()) {
throw IllegalStateException("Space ID or Place ID is empty")
} else {
spaceRef.document(spaceId).collection(Config.FIRESTORE_COLLECTION_SPACE_PLACES)
.document(placeId).collection(
Config.FIRESTORE_COLLECTION_SPACE_PLACES_MEMBER_SETTINGS
)
}

suspend fun addPlace(
spaceId: String,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,14 +37,19 @@ class ApiSpaceService @Inject constructor(
private val bufferedSenderKeyStore: BufferedSenderKeyStore
) {
private val spaceRef = db.collection(FIRESTORE_COLLECTION_SPACES)
private fun spaceMemberRef(spaceId: String) =
spaceRef.document(spaceId.takeIf { it.isNotBlank() } ?: "null")
.collection(FIRESTORE_COLLECTION_SPACE_MEMBERS)
private fun spaceMemberRef(spaceId: String) = if (spaceId.isBlank()) {
throw IllegalArgumentException("Space ID cannot be empty")
} else {
spaceRef.document(spaceId).collection(FIRESTORE_COLLECTION_SPACE_MEMBERS)
}

private fun spaceGroupKeysDoc(spaceId: String) =
spaceRef.document(spaceId.takeIf { it.isNotBlank() } ?: "null")
private fun spaceGroupKeysDoc(spaceId: String) = if (spaceId.isBlank()) {
throw IllegalArgumentException("Space ID cannot be empty")
} else {
spaceRef.document(spaceId)
.collection(FIRESTORE_COLLECTION_SPACE_GROUP_KEYS)
.document(FIRESTORE_COLLECTION_SPACE_GROUP_KEYS)
}

suspend fun createSpace(spaceName: String): String {
val spaceId = UUID.randomUUID().toString()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,12 +43,15 @@ class ApiUserService @Inject constructor(
) {
private val currentUser = userPreferences.currentUser
private val userRef = db.collection(FIRESTORE_COLLECTION_USERS)
private fun sessionRef(userId: String) =
userRef.document(userId.takeIf { it.isNotBlank() } ?: "null")
.collection(Config.FIRESTORE_COLLECTION_USER_SESSIONS)
private fun sessionRef(userId: String) = if (userId.isBlank()) {
throw IllegalArgumentException("User ID cannot be empty")
} else {
userRef.document(userId).collection(Config.FIRESTORE_COLLECTION_USER_SESSIONS)
}

suspend fun getUser(userId: String): ApiUser? {
return try {
if (userId.isBlank()) return null
val user = userRef.document(userId).get().await().toObject(ApiUser::class.java)
when {
user == null -> null
Expand Down

0 comments on commit 909e7f8

Please sign in to comment.