Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add validation to check document ids before api calling #169

Closed
Closed
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package com.canopas.yourspace.data.service.messages
import com.canopas.yourspace.data.models.messages.ApiThread
import com.canopas.yourspace.data.models.messages.ApiThreadMessage
import com.canopas.yourspace.data.models.messages.ThreadInfo
import com.canopas.yourspace.data.service.user.ApiUserService
import com.canopas.yourspace.data.utils.Config
import com.canopas.yourspace.data.utils.Config.FIRESTORE_COLLECTION_SPACE_THREADS
import com.canopas.yourspace.data.utils.snapshotFlow
Expand All @@ -22,18 +21,13 @@ import java.util.Date
import javax.inject.Inject

class ApiMessagesService @Inject constructor(
private val db: FirebaseFirestore,
private val userService: ApiUserService
private val db: FirebaseFirestore
) {

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)
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 @@ -22,13 +22,11 @@ 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)
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
)
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 @@ -39,25 +39,27 @@ class ApiSpaceService @Inject constructor(
) {
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)
spaceRef.document(spaceId).collection(FIRESTORE_COLLECTION_SPACE_MEMBERS)

private fun spaceGroupKeysDoc(spaceId: String) =
spaceRef.document(spaceId.takeIf { it.isNotBlank() } ?: "null")
.collection(FIRESTORE_COLLECTION_SPACE_GROUP_KEYS)
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()
val docRef = spaceRef.document(spaceId)
val userId = authService.currentUser?.id ?: ""

val space = ApiSpace(
id = spaceId,
name = spaceName,
admin_id = userId
)
docRef.set(space).await()
val userId = authService.currentUser?.id

val space = userId?.let {
ApiSpace(
id = spaceId,
name = spaceName,
admin_id = it
)
}
if (space != null) {
docRef.set(space).await()
}

// Initialize the single group_keys doc to a default structure:
val emptyGroupKeys = GroupKeysDoc()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,12 +45,11 @@ 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)
userRef.document(userId).collection(Config.FIRESTORE_COLLECTION_USER_SESSIONS)

suspend fun getUser(userId: String): ApiUser? {
return try {
val user = userRef.document(userId.takeIf { it.isNotBlank() } ?: "null").get().await()
val user = userRef.document(userId).get().await()
.toObject(ApiUser::class.java)
when {
user == null -> null
Expand Down
Loading