mirror of
https://github.com/oxen-io/session-android.git
synced 2025-04-21 22:41:29 +00:00
Integrate group chat moderation API
This commit is contained in:
parent
749a9a93e9
commit
f4bcb285af
@ -128,6 +128,7 @@ public class SQLCipherOpenHelper extends SQLiteOpenHelper {
|
|||||||
db.execSQL(LokiAPIDatabase.getCreateGroupChatAuthTokenTableCommand());
|
db.execSQL(LokiAPIDatabase.getCreateGroupChatAuthTokenTableCommand());
|
||||||
db.execSQL(LokiAPIDatabase.getCreateLastMessageServerIDTableCommand());
|
db.execSQL(LokiAPIDatabase.getCreateLastMessageServerIDTableCommand());
|
||||||
db.execSQL(LokiAPIDatabase.getCreateLastDeletionServerIDTableCommand());
|
db.execSQL(LokiAPIDatabase.getCreateLastDeletionServerIDTableCommand());
|
||||||
|
db.execSQL(LokiAPIDatabase.getCreateModerationPermissionTableCommand());
|
||||||
db.execSQL(LokiPreKeyBundleDatabase.getCreateTableCommand());
|
db.execSQL(LokiPreKeyBundleDatabase.getCreateTableCommand());
|
||||||
db.execSQL(LokiPreKeyRecordDatabase.getCreateTableCommand());
|
db.execSQL(LokiPreKeyRecordDatabase.getCreateTableCommand());
|
||||||
db.execSQL(LokiMessageDatabase.getCreateTableCommand());
|
db.execSQL(LokiMessageDatabase.getCreateTableCommand());
|
||||||
|
@ -43,6 +43,11 @@ class LokiAPIDatabase(context: Context, helper: SQLCipherOpenHelper) : Database(
|
|||||||
private val lastDeletionServerIDCacheIndex = "loki_api_last_deletion_server_id_cache_index"
|
private val lastDeletionServerIDCacheIndex = "loki_api_last_deletion_server_id_cache_index"
|
||||||
private val lastDeletionServerID = "last_deletion_server_id"
|
private val lastDeletionServerID = "last_deletion_server_id"
|
||||||
@JvmStatic val createLastDeletionServerIDTableCommand = "CREATE TABLE $lastDeletionServerIDCache ($lastDeletionServerIDCacheIndex STRING PRIMARY KEY, $lastDeletionServerID INTEGER DEFAULT 0);"
|
@JvmStatic val createLastDeletionServerIDTableCommand = "CREATE TABLE $lastDeletionServerIDCache ($lastDeletionServerIDCacheIndex STRING PRIMARY KEY, $lastDeletionServerID INTEGER DEFAULT 0);"
|
||||||
|
// Moderation permission cache
|
||||||
|
private val moderationPermissionCache = "loki_api_moderation_permission_cache"
|
||||||
|
private val moderationPermissionCacheIndex = "loki_api_moderation_permission_cache_index"
|
||||||
|
private val isModerator = "is_moderator"
|
||||||
|
@JvmStatic val createModerationPermissionTableCommand = "CREATE TABLE $moderationPermissionCache ($moderationPermissionCacheIndex STRING PRIMARY KEY, $isModerator INTEGER DEFAULT 0);"
|
||||||
}
|
}
|
||||||
|
|
||||||
override fun getSwarmCache(hexEncodedPublicKey: String): Set<LokiAPITarget>? {
|
override fun getSwarmCache(hexEncodedPublicKey: String): Set<LokiAPITarget>? {
|
||||||
@ -139,6 +144,21 @@ class LokiAPIDatabase(context: Context, helper: SQLCipherOpenHelper) : Database(
|
|||||||
val row = wrap(mapOf( lastDeletionServerIDCacheIndex to index, lastDeletionServerID to newValue.toString() ))
|
val row = wrap(mapOf( lastDeletionServerIDCacheIndex to index, lastDeletionServerID to newValue.toString() ))
|
||||||
database.insertOrUpdate(lastDeletionServerIDCache, row, "$lastDeletionServerIDCacheIndex = ?", wrap(index))
|
database.insertOrUpdate(lastDeletionServerIDCache, row, "$lastDeletionServerIDCacheIndex = ?", wrap(index))
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fun isModerator(group: Long, server: String): Boolean {
|
||||||
|
val database = databaseHelper.readableDatabase
|
||||||
|
val index = "$server.$group"
|
||||||
|
return database.get(moderationPermissionCache, "$moderationPermissionCacheIndex = ?", wrap(index)) { cursor ->
|
||||||
|
cursor.getInt(isModerator)
|
||||||
|
} == 1
|
||||||
|
}
|
||||||
|
|
||||||
|
fun setIsModerator(group: Long, server: String, newValue: Boolean) {
|
||||||
|
val database = databaseHelper.writableDatabase
|
||||||
|
val index = "$server.$group"
|
||||||
|
val row = wrap(mapOf( moderationPermissionCacheIndex to index, isModerator to (if (newValue) 1 else 0).toString() ))
|
||||||
|
database.insertOrUpdate(moderationPermissionCache, row, "$moderationPermissionCacheIndex = ?", wrap(index))
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// region Convenience
|
// region Convenience
|
||||||
|
@ -44,21 +44,32 @@ class LokiGroupChatPoller(private val context: Context, private val group: LokiG
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private val pollForModerationPermissionTask = object : Runnable {
|
||||||
|
|
||||||
|
override fun run() {
|
||||||
|
pollForModerationPermission()
|
||||||
|
handler.postDelayed(this, pollForModerationPermissionInterval)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
companion object {
|
companion object {
|
||||||
private val pollForNewMessagesInterval: Long = 4 * 1000
|
private val pollForNewMessagesInterval: Long = 4 * 1000
|
||||||
private val pollForDeletedMessagesInterval: Long = 20 * 1000
|
private val pollForDeletedMessagesInterval: Long = 20 * 1000
|
||||||
|
private val pollForModerationPermissionInterval: Long = 10 * 60 * 1000
|
||||||
}
|
}
|
||||||
|
|
||||||
fun startIfNeeded() {
|
fun startIfNeeded() {
|
||||||
if (hasStarted) return
|
if (hasStarted) return
|
||||||
pollForNewMessagesTask.run()
|
pollForNewMessagesTask.run()
|
||||||
pollForDeletedMessagesTask.run()
|
pollForDeletedMessagesTask.run()
|
||||||
|
pollForModerationPermissionTask.run()
|
||||||
hasStarted = true
|
hasStarted = true
|
||||||
}
|
}
|
||||||
|
|
||||||
fun stop() {
|
fun stop() {
|
||||||
handler.removeCallbacks(pollForNewMessagesTask)
|
handler.removeCallbacks(pollForNewMessagesTask)
|
||||||
handler.removeCallbacks(pollForDeletedMessagesTask)
|
handler.removeCallbacks(pollForDeletedMessagesTask)
|
||||||
|
handler.removeCallbacks(pollForModerationPermissionTask)
|
||||||
hasStarted = false
|
hasStarted = false
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -91,4 +102,11 @@ class LokiGroupChatPoller(private val context: Context, private val group: LokiG
|
|||||||
Log.d("Loki", "Failed to get deleted messages for group chat with ID: ${group.serverID} on server: ${group.server}.")
|
Log.d("Loki", "Failed to get deleted messages for group chat with ID: ${group.serverID} on server: ${group.server}.")
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private fun pollForModerationPermission() {
|
||||||
|
api.userHasModerationPermission(group.serverID, group.server).success { isModerator ->
|
||||||
|
val lokiAPIDatabase = DatabaseFactory.getLokiAPIDatabase(context)
|
||||||
|
lokiAPIDatabase.setIsModerator(group.serverID, group.server, isModerator)
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
Loading…
x
Reference in New Issue
Block a user