Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -338,7 +338,7 @@ class SharedCallingViewModel @AssistedInject constructor(
private fun recentInCallReactionMap(): MutableMap<UserId, String> =
ExpiringMap<UserId, String>(
scope = viewModelScope,
expiration = InCallReactions.recentReactionShowDurationMs,
expirationMs = InCallReactions.recentReactionShowDurationMs,
delegate = mutableStateMapOf<UserId, String>()
)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ data class MultipartAttachmentUi(
val localPath: String?,
val contentHash: String? = null,
val contentUrl: String? = null,
val contentUrlExpiresAt: Long? = null,
val previewUrl: String? = null,
val mimeType: String,
val assetType: AttachmentFileType,
Expand All @@ -55,6 +56,7 @@ fun CellAssetContent.toUiModel(progress: Float?) = MultipartAttachmentUi(
fileName = this.assetPath?.substringAfterLast("/"),
localPath = this.localPath,
contentUrl = this.contentUrl,
contentUrlExpiresAt = this.contentUrlExpiresAt,
previewUrl = this.previewUrl,
mimeType = this.mimeType,
assetType = AttachmentFileType.fromMimeType(mimeType),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,13 +21,14 @@
import androidx.lifecycle.ViewModel
import androidx.lifecycle.viewModelScope
import com.wire.android.appLogger
import com.wire.android.feature.cells.domain.model.AttachmentFileType
import com.wire.android.feature.cells.domain.model.AttachmentFileType.IMAGE
import com.wire.android.feature.cells.domain.model.AttachmentFileType.PDF
import com.wire.android.feature.cells.domain.model.AttachmentFileType.VIDEO
import com.wire.android.feature.cells.domain.model.AttachmentFileType
import com.wire.android.ui.common.multipart.AssetSource
import com.wire.android.ui.common.multipart.MultipartAttachmentUi
import com.wire.android.ui.common.multipart.toUiModel
import com.wire.android.util.ExpiringMap
import com.wire.android.util.FileManager
import com.wire.kalium.cells.domain.usecase.DownloadCellFileUseCase
import com.wire.kalium.cells.domain.usecase.RefreshCellAssetStateUseCase
Expand All @@ -41,6 +42,7 @@
import kotlinx.coroutines.launch
import okio.Path.Companion.toPath
import javax.inject.Inject
import kotlin.time.Duration.Companion.hours

@HiltViewModel
class MultipartAttachmentsViewModel @Inject constructor(
Expand All @@ -50,7 +52,18 @@
private val kaliumFileSystem: KaliumFileSystem,
) : ViewModel() {

private val refreshed = mutableListOf<String>()
private companion object {
val DEFAULT_CONTENT_URL_EXPIRY_MS = 1.hours.inWholeMilliseconds

Check warning on line 56 in app/src/main/kotlin/com/wire/android/ui/home/conversations/model/messagetypes/multipart/MultipartAttachmentsViewModel.kt

View check run for this annotation

Codecov / codecov/patch

app/src/main/kotlin/com/wire/android/ui/home/conversations/model/messagetypes/multipart/MultipartAttachmentsViewModel.kt#L56

Added line #L56 was not covered by tests
}

private val refreshed = ExpiringMap<String, Unit>(
scope = viewModelScope,
expirationMs = DEFAULT_CONTENT_URL_EXPIRY_MS,
delegate = mutableMapOf(),
onEntryExpired = { key, _ ->
viewModelScope.launch { refreshAsset(key) }

Check warning on line 64 in app/src/main/kotlin/com/wire/android/ui/home/conversations/model/messagetypes/multipart/MultipartAttachmentsViewModel.kt

View check run for this annotation

Codecov / codecov/patch

app/src/main/kotlin/com/wire/android/ui/home/conversations/model/messagetypes/multipart/MultipartAttachmentsViewModel.kt#L59-L64

Added lines #L59 - L64 were not covered by tests
}
)

private val uploadProgress = mutableStateMapOf<String, Float>()

Expand Down Expand Up @@ -105,12 +118,17 @@
}

fun refreshAssetState(attachment: MultipartAttachmentUi) {
if (refreshed.contains(attachment.uuid).not()) {
refreshed.add(attachment.uuid)
if (attachment.source == AssetSource.CELL) {
viewModelScope.launch { refreshAsset(attachment.uuid) }
}

if (attachment.source != AssetSource.CELL) return
if (refreshed.contains(attachment.uuid)) return

if (attachment.contentUrlExpiresAt != null) {
refreshed.putWithExpireAt(attachment.uuid, Unit, attachment.contentUrlExpiresAt)

Check warning on line 126 in app/src/main/kotlin/com/wire/android/ui/home/conversations/model/messagetypes/multipart/MultipartAttachmentsViewModel.kt

View check run for this annotation

Codecov / codecov/patch

app/src/main/kotlin/com/wire/android/ui/home/conversations/model/messagetypes/multipart/MultipartAttachmentsViewModel.kt#L126

Added line #L126 was not covered by tests
} else {
refreshed.put(attachment.uuid, Unit)

Check warning on line 128 in app/src/main/kotlin/com/wire/android/ui/home/conversations/model/messagetypes/multipart/MultipartAttachmentsViewModel.kt

View check run for this annotation

Codecov / codecov/patch

app/src/main/kotlin/com/wire/android/ui/home/conversations/model/messagetypes/multipart/MultipartAttachmentsViewModel.kt#L128

Added line #L128 was not covered by tests
}

viewModelScope.launch { refreshAsset(attachment.uuid) }

Check warning on line 131 in app/src/main/kotlin/com/wire/android/ui/home/conversations/model/messagetypes/multipart/MultipartAttachmentsViewModel.kt

View check run for this annotation

Codecov / codecov/patch

app/src/main/kotlin/com/wire/android/ui/home/conversations/model/messagetypes/multipart/MultipartAttachmentsViewModel.kt#L131

Added line #L131 was not covered by tests
}

private fun openLocalFile(attachment: MultipartAttachmentUi) {
Expand Down
15 changes: 12 additions & 3 deletions app/src/main/kotlin/com/wire/android/util/ExpiringMap.kt
Original file line number Diff line number Diff line change
Expand Up @@ -28,17 +28,25 @@ import java.util.concurrent.ConcurrentHashMap
*/
class ExpiringMap<K, V>(
private val scope: CoroutineScope,
private val expiration: Long,
private val expirationMs: Long,
private val delegate: MutableMap<K, V>,
private val onEntryExpired: ((key: K, value: V?) -> Unit)? = null,
private val currentTime: () -> Long = { System.currentTimeMillis() },
) : MutableMap<K, V> by delegate {

private val timestamps: MutableMap<K, Long> = ConcurrentHashMap<K, Long>()
private var cleanupJob: Job? = null

fun putWithExpireAt(key: K, value: V, expireAt: Long): V? {
return delegate.put(key, value).also {
timestamps.put(key, expireAt)
scheduleCleanup()
}
}

override fun put(key: K, value: V): V? {
return delegate.put(key, value).also {
timestamps.put(key, currentTime() + expiration)
timestamps.put(key, currentTime() + expirationMs)
scheduleCleanup()
}
}
Expand All @@ -65,7 +73,8 @@ class ExpiringMap<K, V>(
val now = currentTime()
timestamps.entries.onEach { (key, expiration) ->
if (expiration <= now) {
delegate.remove(key)
val value = delegate.remove(key)
onEntryExpired?.invoke(key, value)
}
}
timestamps.entries.removeAll { it.value <= now }
Expand Down
19 changes: 16 additions & 3 deletions app/src/test/kotlin/com/wire/android/util/ExpiringMapTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ class ExpiringMapTest {
}

@Test
fun `check item can not be obtained before expiration`() = runTest {
fun `check item can be obtained before expiration`() = runTest {
// given
val map = withTestExpiringMap()

Expand Down Expand Up @@ -107,9 +107,22 @@ class ExpiringMapTest {
assertEquals(null, map["testKey"])
}

private fun TestScope.withTestExpiringMap(): MutableMap<String, String> = ExpiringMap<String, String>(
@Test
fun `check item can be put with custom expiration`() = runTest {
// given
val map = withTestExpiringMap()

// when
map.putWithExpireAt("testKey", "testValue", currentTime + 500)
advanceTimeBy(301) // advance past default expiration

// then
assertEquals("testValue", map["testKey"])
}

private fun TestScope.withTestExpiringMap(): ExpiringMap<String, String> = ExpiringMap<String, String>(
scope = this.backgroundScope,
expiration = 300,
expirationMs = 300,
delegate = mutableMapOf(),
currentTime = { currentTime }
)
Expand Down
2 changes: 1 addition & 1 deletion kalium
Submodule kalium updated 17 files
+3 −1 cells/src/commonMain/kotlin/com/wire/kalium/cells/data/CellAttachmentsDataSource.kt
+5 −0 cells/src/commonMain/kotlin/com/wire/kalium/cells/data/model/CellNodeDTO.kt
+7 −1 cells/src/commonMain/kotlin/com/wire/kalium/cells/domain/CellAttachmentsRepository.kt
+1 −0 cells/src/commonMain/kotlin/com/wire/kalium/cells/domain/model/CellNode.kt
+7 −1 cells/src/commonMain/kotlin/com/wire/kalium/cells/domain/usecase/RefreshCellAssetStateUseCase.kt
+2 −0 cells/src/commonMain/kotlin/com/wire/kalium/cells/domain/usecase/publiclink/CreatePublicLinkUseCase.kt
+9 −2 cells/src/commonTest/kotlin/com/wire/kalium/cells/domain/usecase/RefreshNodeAssetStateUseCaseTest.kt
+1 −0 data/src/commonMain/kotlin/com/wire/kalium/logic/data/message/AssetContent.kt
+1 −0 logic/src/commonMain/kotlin/com/wire/kalium/logic/data/message/attachment/MessageAttachmentMapper.kt
+3 −1 persistence/src/commonMain/db_user/com/wire/kalium/persistence/MessageAttachments.sq
+2 −1 persistence/src/commonMain/db_user/com/wire/kalium/persistence/MessageDetailsView.sq
+1 −1 persistence/src/commonMain/db_user/com/wire/kalium/persistence/Reactions.sq
+2 −0 persistence/src/commonMain/db_user/migrations/119.sqm
+197 −0 persistence/src/commonMain/db_user/migrations/120.sqm
+1 −0 ...istence/src/commonMain/kotlin/com/wire/kalium/persistence/dao/message/attachment/MessageAttachmentEntity.kt
+2 −0 ...istence/src/commonMain/kotlin/com/wire/kalium/persistence/dao/message/attachment/MessageAttachmentMapper.kt
+3 −3 persistence/src/commonMain/kotlin/com/wire/kalium/persistence/dao/message/attachment/MessageAttachmentsDao.kt