Messaging.kt (6793B) - raw


      1 package me.rhunk.snapenhance.storage
      2 
      3 import me.rhunk.snapenhance.common.data.FriendStreaks
      4 import me.rhunk.snapenhance.common.data.MessagingFriendInfo
      5 import me.rhunk.snapenhance.common.data.MessagingGroupInfo
      6 import me.rhunk.snapenhance.common.data.MessagingRuleType
      7 import me.rhunk.snapenhance.common.util.ktx.getInteger
      8 import me.rhunk.snapenhance.common.util.ktx.getLongOrNull
      9 import me.rhunk.snapenhance.common.util.ktx.getStringOrNull
     10 
     11 
     12 fun AppDatabase.getGroups(): List<MessagingGroupInfo> {
     13     return database.rawQuery("SELECT * FROM groups", null).use { cursor ->
     14         val groups = mutableListOf<MessagingGroupInfo>()
     15         while (cursor.moveToNext()) {
     16             groups.add(MessagingGroupInfo.fromCursor(cursor))
     17         }
     18         groups
     19     }
     20 }
     21 
     22 fun AppDatabase.getFriends(descOrder: Boolean = false): List<MessagingFriendInfo> {
     23     return database.rawQuery("SELECT * FROM friends LEFT OUTER JOIN streaks ON friends.userId = streaks.id ORDER BY id ${if (descOrder) "DESC" else "ASC"}", null).use { cursor ->
     24         val friends = mutableListOf<MessagingFriendInfo>()
     25         while (cursor.moveToNext()) {
     26             runCatching {
     27                 friends.add(MessagingFriendInfo.fromCursor(cursor))
     28             }.onFailure {
     29                 context.log.error("Failed to parse friend", it)
     30             }
     31         }
     32         friends
     33     }
     34 }
     35 
     36 
     37 fun AppDatabase.syncGroupInfo(conversationInfo: MessagingGroupInfo) {
     38     executeAsync {
     39         try {
     40             database.execSQL("INSERT OR REPLACE INTO groups (conversationId, name, participantsCount) VALUES (?, ?, ?)", arrayOf(
     41                 conversationInfo.conversationId,
     42                 conversationInfo.name,
     43                 conversationInfo.participantsCount
     44             ))
     45         } catch (e: Exception) {
     46             throw e
     47         }
     48     }
     49 }
     50 
     51 fun AppDatabase.syncFriend(friend: MessagingFriendInfo) {
     52     executeAsync {
     53         try {
     54             database.execSQL(
     55                 "INSERT OR REPLACE INTO friends (userId, dmConversationId, displayName, mutableUsername, bitmojiId, selfieId) VALUES (?, ?, ?, ?, ?, ?)",
     56                 arrayOf(
     57                     friend.userId,
     58                     friend.dmConversationId,
     59                     friend.displayName,
     60                     friend.mutableUsername,
     61                     friend.bitmojiId,
     62                     friend.selfieId
     63                 )
     64             )
     65             //sync streaks
     66             friend.streaks?.takeIf { it.length > 0 }?.also {
     67                 val streaks = getFriendStreaks(friend.userId)
     68 
     69                 database.execSQL("INSERT OR REPLACE INTO streaks (id, notify, expirationTimestamp, length) VALUES (?, ?, ?, ?)", arrayOf(
     70                     friend.userId,
     71                     streaks?.notify != false,
     72                     it.expirationTimestamp,
     73                     it.length
     74                 ))
     75             } ?: database.execSQL("DELETE FROM streaks WHERE id = ?", arrayOf(friend.userId))
     76         } catch (e: Exception) {
     77             throw e
     78         }
     79     }
     80 }
     81 
     82 
     83 
     84 fun AppDatabase.getRules(targetUuid: String): List<MessagingRuleType> {
     85     return database.rawQuery("SELECT type FROM rules WHERE targetUuid = ?", arrayOf(
     86         targetUuid
     87     )).use { cursor ->
     88         val rules = mutableListOf<MessagingRuleType>()
     89         while (cursor.moveToNext()) {
     90             runCatching {
     91                 rules.add(MessagingRuleType.getByName(cursor.getStringOrNull("type")!!) ?: return@runCatching)
     92             }.onFailure {
     93                 context.log.error("Failed to parse rule", it)
     94             }
     95         }
     96         rules
     97     }
     98 }
     99 
    100 fun AppDatabase.setRule(targetUuid: String, type: String, enabled: Boolean) {
    101     executeAsync {
    102         if (enabled) {
    103             database.execSQL("INSERT OR REPLACE INTO rules (targetUuid, type) VALUES (?, ?)", arrayOf(
    104                 targetUuid,
    105                 type
    106             ))
    107         } else {
    108             database.execSQL("DELETE FROM rules WHERE targetUuid = ? AND type = ?", arrayOf(
    109                 targetUuid,
    110                 type
    111             ))
    112         }
    113     }
    114 }
    115 
    116 fun AppDatabase.getFriendInfo(userId: String): MessagingFriendInfo? {
    117     return database.rawQuery("SELECT * FROM friends LEFT OUTER JOIN streaks ON friends.userId = streaks.id WHERE userId = ?", arrayOf(userId)).use { cursor ->
    118         if (!cursor.moveToFirst()) return@use null
    119         MessagingFriendInfo.fromCursor(cursor)
    120     }
    121 }
    122 
    123 fun AppDatabase.findFriend(conversationId: String): MessagingFriendInfo? {
    124     return database.rawQuery("SELECT * FROM friends WHERE dmConversationId = ?", arrayOf(conversationId)).use { cursor ->
    125         if (!cursor.moveToFirst()) return@use null
    126         MessagingFriendInfo.fromCursor(cursor)
    127     }
    128 }
    129 
    130 fun AppDatabase.deleteFriend(userId: String) {
    131     executeAsync {
    132         database.execSQL("DELETE FROM friends WHERE userId = ?", arrayOf(userId))
    133         database.execSQL("DELETE FROM streaks WHERE id = ?", arrayOf(userId))
    134         database.execSQL("DELETE FROM rules WHERE targetUuid = ?", arrayOf(userId))
    135     }
    136 }
    137 
    138 fun AppDatabase.deleteGroup(conversationId: String) {
    139     executeAsync {
    140         database.execSQL("DELETE FROM groups WHERE conversationId = ?", arrayOf(conversationId))
    141         database.execSQL("DELETE FROM rules WHERE targetUuid = ?", arrayOf(conversationId))
    142     }
    143 }
    144 
    145 fun AppDatabase.getGroupInfo(conversationId: String): MessagingGroupInfo? {
    146     return database.rawQuery("SELECT * FROM groups WHERE conversationId = ?", arrayOf(conversationId)).use { cursor ->
    147         if (!cursor.moveToFirst()) return@use null
    148         MessagingGroupInfo.fromCursor(cursor)
    149     }
    150 }
    151 
    152 fun AppDatabase.getFriendStreaks(userId: String): FriendStreaks? {
    153     return database.rawQuery("SELECT * FROM streaks WHERE id = ?", arrayOf(userId)).use { cursor ->
    154         if (!cursor.moveToFirst()) return@use null
    155         FriendStreaks(
    156             notify = cursor.getInteger("notify") == 1,
    157             expirationTimestamp = cursor.getLongOrNull("expirationTimestamp") ?: 0L,
    158             length = cursor.getInteger("length")
    159         )
    160     }
    161 }
    162 
    163 fun AppDatabase.setFriendStreaksNotify(userId: String, notify: Boolean) {
    164     executeAsync {
    165         database.execSQL("UPDATE streaks SET notify = ? WHERE id = ?", arrayOf(
    166             if (notify) 1 else 0,
    167             userId
    168         ))
    169     }
    170 }
    171 
    172 fun AppDatabase.getRuleIds(type: String): MutableList<String> {
    173     return database.rawQuery("SELECT targetUuid FROM rules WHERE type = ?", arrayOf(type)).use { cursor ->
    174         val ruleIds = mutableListOf<String>()
    175         while (cursor.moveToNext()) {
    176             ruleIds.add(cursor.getStringOrNull("targetUuid")!!)
    177         }
    178         ruleIds
    179     }
    180 }
    181 
    182 fun AppDatabase.clearRuleIds(type: String) {
    183     executeAsync {
    184         database.execSQL("DELETE FROM rules WHERE type = ?", arrayOf(type))
    185     }
    186 }
    187