mirror of
https://github.com/oxen-io/session-android.git
synced 2024-12-25 09:17:44 +00:00
Poll for deleted group chat messages
This commit is contained in:
parent
42dec85884
commit
3f24f8753a
@ -18,35 +18,51 @@ class LokiGroupChatPoller(private val context: Context, private val groupID: Lon
|
|||||||
private val handler = Handler()
|
private val handler = Handler()
|
||||||
private var hasStarted = false
|
private var hasStarted = false
|
||||||
|
|
||||||
private val task = object : Runnable {
|
private val api: LokiGroupChatAPI
|
||||||
|
get() = {
|
||||||
|
val userHexEncodedPublicKey = TextSecurePreferences.getLocalNumber(context)
|
||||||
|
val lokiAPIDatabase = DatabaseFactory.getLokiAPIDatabase(context)
|
||||||
|
val lokiUserDatabase = DatabaseFactory.getLokiUserDatabase(context)
|
||||||
|
val userPrivateKey = IdentityKeyUtil.getIdentityKeyPair(context).privateKey.serialize()
|
||||||
|
LokiGroupChatAPI(userHexEncodedPublicKey, userPrivateKey, lokiAPIDatabase, lokiUserDatabase)
|
||||||
|
}()
|
||||||
|
|
||||||
|
private val pollForNewMessagesTask = object : Runnable {
|
||||||
|
|
||||||
override fun run() {
|
override fun run() {
|
||||||
poll()
|
pollForNewMessages()
|
||||||
handler.postDelayed(this, pollInterval)
|
handler.postDelayed(this, pollForNewMessagesInterval)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private val pollForDeletedMessagesTask = object : Runnable {
|
||||||
|
|
||||||
|
override fun run() {
|
||||||
|
pollForDeletedMessages()
|
||||||
|
handler.postDelayed(this, pollForDeletedMessagesInterval)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
companion object {
|
companion object {
|
||||||
private val pollInterval: Long = 4 * 1000
|
private val pollForNewMessagesInterval: Long = 4 * 1000
|
||||||
|
private val pollForDeletedMessagesInterval: Long = 120 * 1000
|
||||||
}
|
}
|
||||||
|
|
||||||
fun startIfNeeded() {
|
fun startIfNeeded() {
|
||||||
if (hasStarted) return
|
if (hasStarted) return
|
||||||
task.run()
|
pollForNewMessagesTask.run()
|
||||||
|
pollForDeletedMessagesTask.run()
|
||||||
hasStarted = true
|
hasStarted = true
|
||||||
}
|
}
|
||||||
|
|
||||||
fun stop() {
|
fun stop() {
|
||||||
handler.removeCallbacks(task)
|
handler.removeCallbacks(pollForNewMessagesTask)
|
||||||
|
handler.removeCallbacks(pollForDeletedMessagesTask)
|
||||||
hasStarted = false
|
hasStarted = false
|
||||||
}
|
}
|
||||||
|
|
||||||
private fun poll() {
|
private fun pollForNewMessages() {
|
||||||
val userHexEncodedPublicKey = TextSecurePreferences.getLocalNumber(context)
|
api.getMessages(groupID).success { messages ->
|
||||||
val lokiAPIDatabase = DatabaseFactory.getLokiAPIDatabase(context)
|
|
||||||
val lokiUserDatabase = DatabaseFactory.getLokiUserDatabase(context)
|
|
||||||
val userPrivateKey = IdentityKeyUtil.getIdentityKeyPair(context).privateKey.serialize()
|
|
||||||
LokiGroupChatAPI(userHexEncodedPublicKey, userPrivateKey, lokiAPIDatabase, lokiUserDatabase).getMessages(groupID).success { messages ->
|
|
||||||
messages.reversed().map { message ->
|
messages.reversed().map { message ->
|
||||||
val id = "${LokiGroupChatAPI.serverURL}.$groupID".toByteArray()
|
val id = "${LokiGroupChatAPI.serverURL}.$groupID".toByteArray()
|
||||||
val x1 = SignalServiceGroup(SignalServiceGroup.Type.UPDATE, id, null, null, null)
|
val x1 = SignalServiceGroup(SignalServiceGroup.Type.UPDATE, id, null, null, null)
|
||||||
@ -59,4 +75,19 @@ class LokiGroupChatPoller(private val context: Context, private val groupID: Lon
|
|||||||
Log.d("Loki", "Failed to get messages for group chat with ID: $groupID.")
|
Log.d("Loki", "Failed to get messages for group chat with ID: $groupID.")
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private fun pollForDeletedMessages() {
|
||||||
|
api.getMessages(groupID, 0).success { messages ->
|
||||||
|
val lokiMessageDatabase = DatabaseFactory.getLokiMessageDatabase(context)
|
||||||
|
val deletedMessageIDs = messages.filter { it.isDeleted }.mapNotNull { it.serverID }.mapNotNull { lokiMessageDatabase.getMessageID(it) }
|
||||||
|
val smsMessageDatabase = DatabaseFactory.getSmsDatabase(context)
|
||||||
|
val mmsMessageDatabase = DatabaseFactory.getMmsDatabase(context)
|
||||||
|
deletedMessageIDs.forEach {
|
||||||
|
smsMessageDatabase.deleteMessage(it)
|
||||||
|
mmsMessageDatabase.delete(it)
|
||||||
|
}
|
||||||
|
}.fail {
|
||||||
|
Log.d("Loki", "Failed to get deleted messages for group chat with ID: $groupID.")
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
@ -18,6 +18,13 @@ class LokiMessageDatabase(context: Context, helper: SQLCipherOpenHelper) : Datab
|
|||||||
@JvmStatic val createTableCommand = "CREATE TABLE $tableName ($messageID INTEGER PRIMARY KEY, $serverID INTEGER DEFAULT 0, $friendRequestStatus INTEGER DEFAULT 0);"
|
@JvmStatic val createTableCommand = "CREATE TABLE $tableName ($messageID INTEGER PRIMARY KEY, $serverID INTEGER DEFAULT 0, $friendRequestStatus INTEGER DEFAULT 0);"
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fun getMessageID(serverID: Long): Long? {
|
||||||
|
val database = databaseHelper.readableDatabase
|
||||||
|
return database.get(tableName, "${Companion.serverID} = ?", arrayOf( serverID.toString() )) { cursor ->
|
||||||
|
cursor.getInt(messageID)
|
||||||
|
}?.toLong()
|
||||||
|
}
|
||||||
|
|
||||||
override fun setServerID(messageID: Long, serverID: Long) {
|
override fun setServerID(messageID: Long, serverID: Long) {
|
||||||
val database = databaseHelper.writableDatabase
|
val database = databaseHelper.writableDatabase
|
||||||
val contentValues = ContentValues(2)
|
val contentValues = ContentValues(2)
|
||||||
|
Loading…
x
Reference in New Issue
Block a user