|
| 1 | +package com.flipcash.app.messenger.internal.link |
| 2 | + |
| 3 | +import com.flipcash.services.controllers.ChatController |
| 4 | +import com.flipcash.services.controllers.ChatMessagingController |
| 5 | +import com.flipcash.services.controllers.ProfileController |
| 6 | +import com.flipcash.services.models.GetChatError |
| 7 | +import com.flipcash.services.models.GetMessageError |
| 8 | +import com.flipcash.services.models.UserProfile |
| 9 | +import com.flipcash.services.models.chat.BlobAccessContext |
| 10 | +import com.flipcash.services.models.chat.ChatId |
| 11 | +import com.flipcash.services.models.chat.ChatMessage |
| 12 | +import com.flipcash.services.models.chat.ChatMetadata |
| 13 | +import com.flipcash.services.models.chat.ChatType |
| 14 | +import com.flipcash.services.models.chat.MessageContent |
| 15 | +import com.flipcash.services.models.chat.ViewMode |
| 16 | +import com.flipcash.services.models.handle |
| 17 | +import com.flipcash.services.models.nameOrHandle |
| 18 | +import com.flipcash.services.user.UserManager |
| 19 | +import com.flipcash.shared.chat.ChatCoordinator |
| 20 | +import com.flipcash.shared.chat.models.LinkCard |
| 21 | +import com.getcode.opencode.model.core.ID |
| 22 | +import kotlinx.coroutines.flow.first |
| 23 | +import javax.inject.Inject |
| 24 | + |
| 25 | +/** |
| 26 | + * Reads what a message link's card may show, which depends on the source chat and the viewer: |
| 27 | + * |
| 28 | + * - The viewer is in the chat: the message itself, opened on this device if it is end-to-end |
| 29 | + * encrypted, with its reaction count. ([LinkCard.Message.State.Resolved]) |
| 30 | + * - A public group they aren't in: `GetMessage` in `FULL_OR_REDACTED`, which answers with the text |
| 31 | + * when the group's rules let them read and a placeholder when they don't. |
| 32 | + * ([LinkCard.Message.State.Gated]) |
| 33 | + * - A private group they aren't in: the group's public record only, never the message. |
| 34 | + * ([LinkCard.Message.State.LockedGroup]) |
| 35 | + * - A DM they aren't in: that it is one. ([LinkCard.Message.State.LockedDirect]) |
| 36 | + * |
| 37 | + * A deleted message, a message or chat the server doesn't have, or one this device can't open |
| 38 | + * answers [LinkCard.Message.State.Unresolved]; a card never draws resolution failing as an error. |
| 39 | + * A request that didn't complete fails, so the resolver asks again on the next draw. |
| 40 | + * |
| 41 | + * Membership is read from the stored chat. A chat with no stored row, or one whose membership is |
| 42 | + * unknown, is treated as one the viewer is not in: what the card shows is then what the server |
| 43 | + * hands a stranger, so an unknown can never show more than it should. |
| 44 | + */ |
| 45 | +internal class MessageLinkLookup @Inject constructor( |
| 46 | + private val chats: ChatCoordinator, |
| 47 | + private val chatController: ChatController, |
| 48 | + private val messaging: ChatMessagingController, |
| 49 | + private val profiles: ProfileController, |
| 50 | + private val userManager: UserManager, |
| 51 | + private val groups: GroupLinkLookup, |
| 52 | +) { |
| 53 | + suspend operator fun invoke(card: LinkCard.Message): Result<LinkCard.Message.State> = runCatching { |
| 54 | + val stored = chats.observeMetadata(card.chatId).first() |
| 55 | + if (stored?.isMember == true) { |
| 56 | + member(stored.metadata, card.messageId) |
| 57 | + } else if (card.isGroup) { |
| 58 | + nonMemberGroup(card.chatId, card.messageId) |
| 59 | + } else { |
| 60 | + LinkCard.Message.State.LockedDirect |
| 61 | + } |
| 62 | + }.recoverCatching { error -> |
| 63 | + if (error.meansNothingThere()) LinkCard.Message.State.Unresolved else throw error |
| 64 | + } |
| 65 | + |
| 66 | + private suspend fun member(chat: ChatMetadata, messageId: Long): LinkCard.Message.State { |
| 67 | + val message = chats.readMessage(chat.chatId, messageId).getOrThrow() |
| 68 | + val body = message.body() ?: return LinkCard.Message.State.Unresolved |
| 69 | + return LinkCard.Message.State.Resolved( |
| 70 | + source = source(chat), |
| 71 | + message = excerpt(message, body, chat), |
| 72 | + reactionCount = message.reactions?.reactions?.sumOf { it.count } ?: 0, |
| 73 | + ) |
| 74 | + } |
| 75 | + |
| 76 | + private suspend fun nonMemberGroup(chatId: ChatId, messageId: Long): LinkCard.Message.State { |
| 77 | + val chat = chatController.getChat(chatId, ViewMode.REDACTED).getOrThrow() |
| 78 | + if (chat.type != ChatType.GROUP) return LinkCard.Message.State.Unresolved |
| 79 | + if (chat.isPrivate) { |
| 80 | + return LinkCard.Message.State.LockedGroup( |
| 81 | + source = source(chat), |
| 82 | + memberCount = chat.rosterSummary.memberCount, |
| 83 | + host = chat.creator?.let { name(it) }, |
| 84 | + ) |
| 85 | + } |
| 86 | + val message = messaging.getMessage(chatId, messageId, ViewMode.FULL_OR_REDACTED).getOrThrow() |
| 87 | + val body = message.body() ?: return LinkCard.Message.State.Unresolved |
| 88 | + return LinkCard.Message.State.Gated( |
| 89 | + source = source(chat), |
| 90 | + message = excerpt(message, body, chat = null), |
| 91 | + requirement = groups.requirement(chat.rules), |
| 92 | + ) |
| 93 | + } |
| 94 | + |
| 95 | + private fun source(chat: ChatMetadata): LinkCard.Message.Source = when (chat.type) { |
| 96 | + ChatType.GROUP -> LinkCard.Message.Source( |
| 97 | + title = chat.title?.takeIf { it.isNotBlank() }, |
| 98 | + kind = if (chat.isPrivate) LinkCard.Message.Kind.PrivateGroup else LinkCard.Message.Kind.PublicGroup, |
| 99 | + picture = chat.picture, |
| 100 | + pictureAccess = BlobAccessContext.ChatProfile(chat.chatId), |
| 101 | + ) |
| 102 | + else -> { |
| 103 | + val peer = chat.members.firstOrNull { it.userId != userManager.accountId } |
| 104 | + LinkCard.Message.Source( |
| 105 | + title = peer?.userProfile?.displayLabel(), |
| 106 | + kind = LinkCard.Message.Kind.DirectMessage, |
| 107 | + picture = peer?.userProfile?.profilePicture, |
| 108 | + pictureAccess = BlobAccessContext.profile(peer?.userId), |
| 109 | + ) |
| 110 | + } |
| 111 | + } |
| 112 | + |
| 113 | + /** |
| 114 | + * The sender, named from the chat's stored roster when [chat] has one and from their public |
| 115 | + * profile otherwise. An unreadable name is left off rather than failing the card. |
| 116 | + */ |
| 117 | + private suspend fun excerpt( |
| 118 | + message: ChatMessage, |
| 119 | + body: LinkCard.Message.Body, |
| 120 | + chat: ChatMetadata?, |
| 121 | + ): LinkCard.Message.Excerpt { |
| 122 | + val sender = message.senderId |
| 123 | + val isFromSelf = message.isFromSelf || (sender != null && sender == userManager.accountId) |
| 124 | + val author = sender?.let { id -> |
| 125 | + chat?.members?.firstOrNull { it.userId == id }?.userProfile?.displayLabel() ?: name(id) |
| 126 | + } |
| 127 | + return LinkCard.Message.Excerpt( |
| 128 | + author = author, |
| 129 | + isFromSelf = isFromSelf, |
| 130 | + sentAt = message.timestamp, |
| 131 | + body = if (message.redacted && body is LinkCard.Message.Body.Text) body.copy(redacted = true) else body, |
| 132 | + ) |
| 133 | + } |
| 134 | + |
| 135 | + private suspend fun name(userId: ID): String? = |
| 136 | + profiles.getProfileForUser(userId).getOrNull()?.displayLabel() |
| 137 | + |
| 138 | + private fun UserProfile.displayLabel(): String? = nameOrHandle(displayName, handle) |
| 139 | + |
| 140 | + /** The card's body for this message, or null when there is nothing to show: deleted or sealed. */ |
| 141 | + private fun ChatMessage.body(): LinkCard.Message.Body? = content.singleOrNull()?.body() |
| 142 | + |
| 143 | + private fun MessageContent.body(): LinkCard.Message.Body? = when (this) { |
| 144 | + is MessageContent.Text -> LinkCard.Message.Body.Text(text) |
| 145 | + is MessageContent.Reply -> content.singleOrNull()?.body() |
| 146 | + is MessageContent.Media -> caption?.let { LinkCard.Message.Body.Text(it.text) } ?: LinkCard.Message.Body.Other |
| 147 | + is MessageContent.Cash -> LinkCard.Message.Body.Cash( |
| 148 | + amount = amount.formatted(), |
| 149 | + tipped = action == MessageContent.Cash.Action.TIPPED, |
| 150 | + ) |
| 151 | + is MessageContent.Widget, is MessageContent.System -> LinkCard.Message.Body.Other |
| 152 | + is MessageContent.Deleted, is MessageContent.Encrypted -> null |
| 153 | + } |
| 154 | + |
| 155 | + private fun Throwable.meansNothingThere(): Boolean = when (this) { |
| 156 | + is GetMessageError.NotFound, |
| 157 | + is GetMessageError.Denied, |
| 158 | + is GetChatError.NotFound, |
| 159 | + is GetChatError.Denied -> true |
| 160 | + else -> false |
| 161 | + } |
| 162 | +} |
0 commit comments