@@ -9,9 +9,11 @@ import dagger.hilt.android.qualifiers.ApplicationContext
9
9
import kotlinx.coroutines.flow.MutableSharedFlow
10
10
import kotlinx.coroutines.flow.SharedFlow
11
11
import kotlinx.serialization.json.Json
12
+ import network.loki.messenger.libsession_util.util.Bytes
12
13
import network.loki.messenger.libsession_util.util.UserPic
13
14
import org.session.libsession.utilities.Address
14
15
import org.session.libsession.utilities.recipients.ProStatus
16
+ import org.session.libsession.utilities.recipients.RemoteFile
15
17
import org.session.libsignal.utilities.Base64
16
18
import org.session.libsignal.utilities.Log
17
19
import org.thoughtcrime.securesms.database.helpers.SQLCipherOpenHelper
@@ -41,7 +43,10 @@ class RecipientSettingsDatabase @Inject constructor(
41
43
42
44
// If nothing is updated, return early
43
45
if (oldSettings == newSettings) {
44
- Log .d(TAG , " No changes to settings for ${address.debugString} , old: $oldSettings , new: $newSettings " )
46
+ Log .d(
47
+ TAG ,
48
+ " No changes to settings for ${address.debugString} , old: $oldSettings , new: $newSettings "
49
+ )
45
50
return
46
51
}
47
52
@@ -78,10 +83,11 @@ class RecipientSettingsDatabase @Inject constructor(
78
83
fun delete (address : Address ) {
79
84
cache.remove(address)
80
85
if (writableDatabase.delete(
81
- TABLE_NAME ,
82
- " $COL_ADDRESS = ?" ,
83
- arrayOf(address.toString())
84
- ) > 0 ) {
86
+ TABLE_NAME ,
87
+ " $COL_ADDRESS = ?" ,
88
+ arrayOf(address.toString())
89
+ ) > 0
90
+ ) {
85
91
mutableChangeNotification.tryEmit(address)
86
92
}
87
93
}
@@ -92,7 +98,10 @@ class RecipientSettingsDatabase @Inject constructor(
92
98
return existing
93
99
}
94
100
95
- return readableDatabase.rawQuery(" SELECT * FROM $TABLE_NAME WHERE $COL_ADDRESS = ?" , address.address)
101
+ return readableDatabase.rawQuery(
102
+ " SELECT * FROM $TABLE_NAME WHERE $COL_ADDRESS = ?" ,
103
+ address.address
104
+ )
96
105
.use { cursor ->
97
106
// If no settings are saved in the database, return the empty settings, and cache
98
107
// that as well so that we don't have to query the database again.
@@ -116,7 +125,11 @@ class RecipientSettingsDatabase @Inject constructor(
116
125
keyB64 = getString(getColumnIndexOrThrow(COL_PROFILE_PIC_KEY )),
117
126
url = getString(getColumnIndexOrThrow(COL_PROFILE_PIC_URL ))
118
127
),
119
- blocksCommunityMessagesRequests = getInt(getColumnIndexOrThrow(COL_BLOCKS_COMMUNITY_MESSAGES_REQUESTS )) == 1 ,
128
+ blocksCommunityMessagesRequests = getInt(
129
+ getColumnIndexOrThrow(
130
+ COL_BLOCKS_COMMUNITY_MESSAGES_REQUESTS
131
+ )
132
+ ) == 1 ,
120
133
name = getString(getColumnIndexOrThrow(COL_NAME )),
121
134
proStatus = getString(getColumnIndexOrThrow(COL_PRO_STATUS ))
122
135
?.let {
@@ -143,6 +156,34 @@ class RecipientSettingsDatabase @Inject constructor(
143
156
}
144
157
}
145
158
159
+ /* *
160
+ * This method returns all referenced profile picture URL.
161
+ * This will be used to identify which avatars are still being used to exclude
162
+ * them from the cleanup.
163
+ */
164
+ fun getAllReferencedAvatarFiles (): Map <Address , RemoteFile .Encrypted > {
165
+ LinkedHashSet <RemoteFile .Encrypted >()
166
+
167
+ val sql =
168
+ """
169
+ SELECT $COL_PROFILE_PIC_URL , $COL_PROFILE_PIC_KEY
170
+ FROM $TABLE_NAME
171
+ WHERE $COL_PROFILE_PIC_URL IS NOT NULL AND $COL_PROFILE_PIC_URL != ''
172
+ AND $COL_PROFILE_PIC_KEY IS NOT NULL AND $COL_PROFILE_PIC_KEY != ''
173
+ """ .trimIndent()
174
+
175
+ return readableDatabase.rawQuery(sql, emptyArray()).use { cursor ->
176
+ buildMap {
177
+ while (cursor.moveToNext()) {
178
+ val address = Address .fromSerialized(cursor.getString(0 ))
179
+ val url = cursor.getString(0 )
180
+ val key = Base64 .decode(cursor.getString(1 ))
181
+ put(address, RemoteFile .Encrypted (url = url, key = Bytes (key)))
182
+ }
183
+ }
184
+ }
185
+ }
186
+
146
187
companion object {
147
188
private const val TAG = " RecipientSettingsDatabase"
148
189
@@ -155,13 +196,15 @@ class RecipientSettingsDatabase @Inject constructor(
155
196
private const val COL_PROFILE_PIC_KEY = " profile_pic_key_b64"
156
197
private const val COL_PROFILE_PIC_URL = " profile_pic_url"
157
198
private const val COL_NAME = " name"
158
- private const val COL_BLOCKS_COMMUNITY_MESSAGES_REQUESTS = " blocks_community_messages_requests"
199
+ private const val COL_BLOCKS_COMMUNITY_MESSAGES_REQUESTS =
200
+ " blocks_community_messages_requests"
159
201
private const val COL_PRO_STATUS = " pro_status"
160
202
161
203
// The time when the profile pic/name/is_pro was last updated, in epoch seconds.
162
204
private const val COL_PROFILE_UPDATE_TIME = " profile_update_time"
163
205
164
- val MIGRATION_CREATE_TABLE = arrayOf("""
206
+ val MIGRATION_CREATE_TABLE = arrayOf(
207
+ """
165
208
CREATE TABLE recipient_settings (
166
209
$COL_ADDRESS TEXT NOT NULL PRIMARY KEY COLLATE NOCASE,
167
210
$COL_MUTE_UNTIL INTEGER NOT NULL DEFAULT 0,
0 commit comments