Skip to content

Instantly share code, notes, and snippets.

@a914-gowtham
Last active September 26, 2021 17:30
Show Gist options
  • Save a914-gowtham/fe0a75f6ce85e972524567b5ae63e98b to your computer and use it in GitHub Desktop.
Save a914-gowtham/fe0a75f6ce85e972524567b5ae63e98b to your computer and use it in GitHub Desktop.
class GroupMsgStatusUpdater {
fun updateToDeliveryOrSeen(myUserId: String, messageList: List<GroupMessage>,isSeen: Boolean, vararg groupId: String){
try {
val batch= firestore.batch()
val db = FirebaseFirestore.getInstance()
val groupCollection= db.collection("Groups")
for (id in groupId){
val msgSubCollection=groupCollection.document(id).collection("group_messages")
val filterList= messageList
.filter {
val indexOfMyStatus=it.to.indexOf(myUserId)
it.from!=myUserId && it.status[indexOfMyStatus]==0 && it.groupId==id }
.map {
val myIndex=it.to.indexOf(myUserId)
it.status[myIndex]=if(isSeen) 3 else 2
if(isSeen)
it.seenTime[myIndex]= Date()
it.deliveryTime[myIndex]=if(it.deliveryTime.length<myIndex)
it.deliveryTime[myIndex] else Date()
it
}
for (msg in filterList){
batch.update(msgSubCollection
.document(msg.id),msg.asMap())
}
}
batch.commit().addOnSuccessListener {
LogMessage.v("Batch update success from group")
}.addOnFailureListener {
LogMessage.v("Batch update failure ${it.message} from group")
}
} catch (e: Exception) {
e.printStackTrace()
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment