11package org.thoughtcrime.securesms.dependencies
22
3+ import androidx.collection.arrayMapOf
34import androidx.collection.arraySetOf
45import dagger.Lazy
56import kotlinx.coroutines.CoroutineScope
@@ -141,6 +142,7 @@ class ConfigFactory @Inject constructor(
141142 lock.writeLock().lock()
142143 return configs to {
143144 val changed = arraySetOf<UserConfigType >()
145+ var dumped: MutableMap <UserConfigType , ByteArray >? = null
144146
145147 for (type in UserConfigType .entries) {
146148 val config = configs.getConfig(type)
@@ -149,17 +151,33 @@ class ConfigFactory @Inject constructor(
149151 }
150152
151153 if (config.needsDump()) {
152- configDatabase.storeConfig(
153- variant = type.configVariant,
154- publicKey = requiresCurrentUserAccountId().hexString,
155- data = config.dump(),
156- timestamp = clock.currentTimeMills()
157- )
154+ if (dumped == null ) {
155+ dumped = arrayMapOf()
156+ }
157+
158+ dumped[type] = config.dump()
158159 }
159160 }
160161
161162 lock.writeLock().unlock()
162163
164+ // Persist dumped configs
165+ if (dumped != null ) {
166+ coroutineScope.launch {
167+ val userAccountId = requiresCurrentUserAccountId()
168+ val currentTimeMs = clock.currentTimeMills()
169+
170+ for ((type, data) in dumped) {
171+ configDatabase.storeConfig(
172+ variant = type.configVariant,
173+ publicKey = userAccountId.hexString,
174+ data = data,
175+ timestamp = currentTimeMs
176+ )
177+ }
178+ }
179+ }
180+
163181 // Notify changes on a coroutine
164182 if (changed.isNotEmpty()) {
165183 coroutineScope.launch {
@@ -180,19 +198,31 @@ class ConfigFactory @Inject constructor(
180198 configs.groupKeys.needsDump() ||
181199 configs.groupKeys.needsRekey()
182200
183- if (configs.groupInfo.needsDump() || configs.groupMembers.needsDump() ||
201+ val dumped = if (configs.groupInfo.needsDump() || configs.groupMembers.needsDump() ||
184202 configs.groupKeys.needsDump()) {
185- configDatabase.storeGroupConfigs(
186- publicKey = groupId.hexString,
187- keysConfig = configs.groupKeys.dump(),
188- infoConfig = configs.groupInfo.dump(),
189- memberConfig = configs.groupMembers.dump(),
190- timestamp = clock.currentTimeMills()
203+ Triple (
204+ configs.groupKeys.dump(),
205+ configs.groupInfo.dump(),
206+ configs.groupMembers.dump()
191207 )
208+ } else {
209+ null
192210 }
193211
194212 lock.writeLock().unlock()
195213
214+ if (dumped != null ) {
215+ coroutineScope.launch {
216+ configDatabase.storeGroupConfigs(
217+ publicKey = groupId.hexString,
218+ keysConfig = dumped.first,
219+ infoConfig = dumped.second,
220+ memberConfig = dumped.third,
221+ timestamp = clock.currentTimeMills()
222+ )
223+ }
224+ }
225+
196226 // Notify changes on a coroutine
197227 if (changed) {
198228 coroutineScope.launch {
0 commit comments