Skip to content

Commit

Permalink
feat: queries, storage and mapping for receiving location messages (W…
Browse files Browse the repository at this point in the history
…PB-5170) (#2285)

* feat: parse location messages persistence, queries and mappers

* feat: location migration added

* feat: location migration added, query insertion

* feat: map message preview and notification

* feat: map message preview and notification, map temp sqm

* feat: map notifications and second line

* feat: fix tests
  • Loading branch information
yamilmedina authored Dec 8, 2023
1 parent 030a4bc commit 3c8ceb0
Show file tree
Hide file tree
Showing 19 changed files with 317 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,10 @@ sealed interface Message {
is MessageContent.Composite -> mutableMapOf(
typeKey to "composite"
)

is MessageContent.Location -> mutableMapOf(
typeKey to "location",
)
}

val standardProperties = mapOf(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -288,6 +288,13 @@ sealed class MessageContent {
val clientId: ClientId? = null
) : Regular()

data class Location(
val latitude: Float,
val longitude: Float,
val name: String? = null,
val zoom: Int? = null,
) : Regular()

data object MLSWrongEpochWarning : System()

data object ClientAction : Signaling()
Expand Down Expand Up @@ -359,6 +366,7 @@ fun MessageContent?.getType() = when (this) {
MessageContent.ConversationVerifiedMLS -> "ConversationVerification.Verified.MLS"
MessageContent.ConversationVerifiedProteus -> "ConversationVerification.Verified.Proteus"
is MessageContent.ConversationStartedUnverifiedWarning -> "ConversationStartedUnverifiedWarning"
is MessageContent.Location -> "Location"
null -> "null"
}

Expand All @@ -378,6 +386,7 @@ sealed interface MessagePreviewContent {
data class QuotedSelf(override val username: String?) : WithUser

data class Knock(override val username: String?) : WithUser
data class Location(override val username: String?) : WithUser

data class MemberLeft(override val username: String?) : WithUser

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -264,6 +264,12 @@ class MessageMapperImpl(
)
}

MessageEntity.ContentType.LOCATION -> LocalNotificationMessage.Comment(
message.id,
sender,
message.date,
LocalNotificationCommentType.LOCATION
)
MessageEntity.ContentType.MEMBER_CHANGE -> null
MessageEntity.ContentType.RESTRICTED_ASSET -> null
MessageEntity.ContentType.CONVERSATION_RENAMED -> null
Expand Down Expand Up @@ -358,6 +364,13 @@ class MessageMapperImpl(
)
},
)

is MessageContent.Location -> MessageEntityContent.Location(
latitude = regularMessage.latitude,
longitude = regularMessage.longitude,
name = regularMessage.name,
zoom = regularMessage.zoom
)
}

private fun toTextEntity(textContent: MessageContent.Text): MessageEntityContent.Text = MessageEntityContent.Text(
Expand Down Expand Up @@ -466,6 +479,7 @@ private fun MessagePreviewEntityContent.toMessageContent(): MessagePreviewConten
is MessagePreviewEntityContent.ConversationVerifiedProteus -> MessagePreviewContent.VerificationChanged.VerifiedProteus
is MessagePreviewEntityContent.ConversationVerificationDegradedMls -> MessagePreviewContent.VerificationChanged.DegradedMls
is MessagePreviewEntityContent.ConversationVerificationDegradedProteus -> MessagePreviewContent.VerificationChanged.DegradedProteus
is MessagePreviewEntityContent.Location -> MessagePreviewContent.WithUser.Location(username = senderName)
}

fun AssetTypeEntity.toModel(): AssetType = when (this) {
Expand Down Expand Up @@ -551,6 +565,13 @@ fun MessageEntityContent.Regular.toMessageContent(hidden: Boolean, selfUserId: U
)
}
)

is MessageEntityContent.Location -> MessageContent.Location(
latitude = this.latitude,
longitude = this.longitude,
name = this.name,
zoom = this.zoom
)
}

private fun quotedContentFromEntity(it: MessageEntityContent.Text.QuotedMessage) = when {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -110,5 +110,6 @@ internal class PersistMessageUseCaseImpl(
is MessageContent.FederationStopped.Removed -> false
is MessageContent.ConversationProtocolChanged -> false
is MessageContent.ConversationStartedUnverifiedWarning -> false
is MessageContent.Location -> true
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ import com.wire.kalium.protobuf.messages.GenericMessage
import com.wire.kalium.protobuf.messages.Knock
import com.wire.kalium.protobuf.messages.LastRead
import com.wire.kalium.protobuf.messages.LegalHoldStatus
import com.wire.kalium.protobuf.messages.Location
import com.wire.kalium.protobuf.messages.MessageDelete
import com.wire.kalium.protobuf.messages.MessageEdit
import com.wire.kalium.protobuf.messages.MessageHide
Expand Down Expand Up @@ -132,6 +133,7 @@ class ProtoContentMapperImpl(
is MessageContent.ButtonAction -> packButtonAction(readableContent)

is MessageContent.ButtonActionConfirmation -> TODO()
is MessageContent.Location -> TODO("todo, when implementing send location")
}
}

Expand Down Expand Up @@ -200,6 +202,10 @@ class ProtoContentMapperImpl(
)
}

is MessageContent.Location -> {
TODO("todo, when implementing send location")
}

is MessageContent.FailedDecryption,
is MessageContent.RestrictedAsset,
is MessageContent.Unknown,
Expand Down Expand Up @@ -319,7 +325,7 @@ class ProtoContentMapperImpl(
is GenericMessage.Content.Hidden -> unpackHidden(genericMessage, protoContent)
is GenericMessage.Content.Knock -> MessageContent.Knock(protoContent.value.hotKnock)
is GenericMessage.Content.LastRead -> unpackLastRead(genericMessage, protoContent)
is GenericMessage.Content.Location -> MessageContent.Unknown(typeName, encodedContent.data)
is GenericMessage.Content.Location -> unpackLocation(protoContent)
is GenericMessage.Content.Reaction -> unpackReaction(protoContent)

is GenericMessage.Content.External -> {
Expand All @@ -335,6 +341,15 @@ class ProtoContentMapperImpl(
return readableContent
}

private fun unpackLocation(
protoContent: GenericMessage.Content.Location
): MessageContent.FromProto = MessageContent.Location(
latitude = protoContent.value.latitude,
longitude = protoContent.value.longitude,
name = protoContent.value.name,
zoom = protoContent.value.zoom
)

private fun packReceipt(
receiptContent: MessageContent.Receipt
): GenericMessage.Content.Confirmation {
Expand Down Expand Up @@ -621,9 +636,20 @@ class ProtoContentMapperImpl(
)
}

is Ephemeral.Content.Location -> {
val location = GenericMessage.Content.Location(
Location(
latitude = ephemeralContent.value.latitude,
longitude = ephemeralContent.value.longitude,
name = ephemeralContent.value.name,
zoom = ephemeralContent.value.zoom
)
)
unpackLocation(location)
}

// Handle self-deleting Location messages when they are implemented
is Ephemeral.Content.Image,
is Ephemeral.Content.Location,
null -> {
MessageContent.Ignored
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -102,5 +102,5 @@ sealed class LocalNotificationMessage(
data class LocalNotificationMessageAuthor(val name: String, val imageUri: UserAssetId?)

enum class LocalNotificationCommentType {
PICTURE, FILE, REACTION, MISSED_CALL, NOT_SUPPORTED_YET
PICTURE, FILE, REACTION, MISSED_CALL, LOCATION, NOT_SUPPORTED_YET
}
Original file line number Diff line number Diff line change
Expand Up @@ -198,5 +198,6 @@ internal class PersistMigratedMessagesUseCaseImpl(
is MessageContent.Composite -> MessageEntity.Visibility.VISIBLE
is MessageContent.ButtonAction -> MessageEntity.Visibility.HIDDEN
is MessageContent.ButtonActionConfirmation -> MessageEntity.Visibility.HIDDEN
is MessageContent.Location -> MessageEntity.Visibility.VISIBLE
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,7 @@ internal class ApplicationMessageHandlerImpl(
is MessageContent.LastRead -> Message.Visibility.HIDDEN
is MessageContent.Cleared -> Message.Visibility.HIDDEN
is MessageContent.Composite -> Message.Visibility.VISIBLE
is MessageContent.Location -> Message.Visibility.VISIBLE
}
val message = Message.Regular(
id = content.messageUid,
Expand Down Expand Up @@ -233,6 +234,7 @@ internal class ApplicationMessageHandlerImpl(
}

is MessageContent.Composite -> persistMessage(message)
is MessageContent.Location -> persistMessage(message)
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,11 @@ IFNULL(
) AS buttonsJson,
FederationTerminatedContent.domain_list AS federationDomainList,
FederationTerminatedContent.federation_type AS federationType,
ConversationProtocolChangedContent.protocol AS conversationProtocolChanged
ConversationProtocolChangedContent.protocol AS conversationProtocolChanged,
ConversationLocationContent.latitude AS latitude,
ConversationLocationContent.longitude AS longitude,
ConversationLocationContent.name AS locationName,
ConversationLocationContent.zoom AS locationZoom

FROM Message
JOIN User ON Message.sender_user_id = User.qualified_id
Expand All @@ -155,5 +159,6 @@ LEFT JOIN MessageConversationReceiptModeChangedContent AS ConversationReceiptMod
LEFT JOIN MessageConversationTimerChangedContent AS ConversationTimerChangedContent ON Message.id = ConversationTimerChangedContent.message_id AND Message.conversation_id = ConversationTimerChangedContent.conversation_id
LEFT JOIN MessageFederationTerminatedContent AS FederationTerminatedContent ON Message.id = FederationTerminatedContent.message_id AND Message.conversation_id = FederationTerminatedContent.conversation_id
LEFT JOIN MessageConversationProtocolChangedContent AS ConversationProtocolChangedContent ON Message.id = ConversationProtocolChangedContent.message_id AND Message.conversation_id = ConversationProtocolChangedContent.conversation_id
LEFT JOIN MessageConversationLocationContent AS ConversationLocationContent ON Message.id = ConversationLocationContent.message_id AND Message.conversation_id = ConversationLocationContent.conversation_id
LEFT JOIN SelfUser;
-- TODO: Remove IFNULL functions above if we can force SQLDelight to not unpack as notnull
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ WHERE id IN (
SELECT id FROM Message
WHERE
Message.visibility = 'VISIBLE' AND
Message.content_type IN ('TEXT', 'ASSET', 'KNOCK', 'MISSED_CALL', 'CONVERSATION_RENAMED', 'MEMBER_CHANGE', 'COMPOSITE', 'CONVERSATION_DEGRADED_MLS', 'CONVERSATION_DEGRADED_PROTEUS', 'CONVERSATION_VERIFIED_MLS', 'CONVERSATION_VERIFIED_PROTEUS')
Message.content_type IN ('TEXT', 'ASSET', 'KNOCK', 'MISSED_CALL', 'CONVERSATION_RENAMED', 'MEMBER_CHANGE', 'COMPOSITE', 'CONVERSATION_DEGRADED_MLS', 'CONVERSATION_DEGRADED_PROTEUS', 'CONVERSATION_VERIFIED_MLS', 'CONVERSATION_VERIFIED_PROTEUS', 'LOCATION')
GROUP BY Message.conversation_id
HAVING Message.creation_date = MAX(Message.creation_date)
);
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
import com.wire.kalium.persistence.dao.conversation.ConversationEntity;
import com.wire.kalium.persistence.dao.QualifiedIDEntity;
import com.wire.kalium.persistence.dao.conversation.ConversationEntity;
import com.wire.kalium.persistence.dao.message.MessageEntity.ContentType;
import com.wire.kalium.persistence.dao.message.MessageEntity.FederationType;
import com.wire.kalium.persistence.dao.message.MessageEntity.MemberChangeType;
import com.wire.kalium.persistence.dao.message.MessageEntity;
import com.wire.kalium.persistence.dao.message.RecipientFailureTypeEntity;
import kotlin.Boolean;
import kotlin.Float;
import kotlin.Int;
import kotlin.String;
import kotlin.collections.List;
Expand Down Expand Up @@ -221,6 +222,18 @@ CREATE TABLE MessageConversationProtocolChangedContent (
PRIMARY KEY (message_id, conversation_id)
);

CREATE TABLE MessageConversationLocationContent (
message_id TEXT NOT NULL,
conversation_id TEXT AS QualifiedIDEntity NOT NULL,
latitude REAL AS Float NOT NULL,
longitude REAL AS Float NOT NULL,
name TEXT,
zoom INTEGER AS Int,

FOREIGN KEY (message_id, conversation_id) REFERENCES Message(id, conversation_id) ON DELETE CASCADE ON UPDATE CASCADE,
PRIMARY KEY (message_id, conversation_id)
);

needsToBeNotified:
WITH targetMessage(isSelfMessage, isMentioningSelfUser, isQuotingSelfUser, mutedStatus) AS (
SELECT isSelfMessage,
Expand Down Expand Up @@ -383,6 +396,10 @@ insertConversationProtocolChanged:
INSERT OR IGNORE INTO MessageConversationProtocolChangedContent(message_id, conversation_id, protocol)
VALUES(?, ?, ?);

insertLocationMessageContent:
INSERT OR IGNORE INTO MessageConversationLocationContent(message_id, conversation_id, latitude, longitude, name, zoom)
VALUES(?, ?, ?, ?, ?, ?);

updateMessageStatus:
UPDATE Message
SET status = ?
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ SELECT
Conversation.type AS conversationType
FROM Message
LEFT JOIN SelfUser
JOIN User ON Message.sender_user_id = User.qualified_id AND Message.content_type IN ('TEXT', 'RESTRICTED_ASSET', 'ASSET', 'KNOCK', 'MISSED_CALL')
JOIN User ON Message.sender_user_id = User.qualified_id AND Message.content_type IN ('TEXT', 'RESTRICTED_ASSET', 'ASSET', 'KNOCK', 'MISSED_CALL', 'LOCATION')
JOIN Conversation AS Conversation ON Message.conversation_id == Conversation.qualified_id AND (Message.creation_date > IFNULL(Conversation.last_notified_date, 0))
LEFT JOIN MessageAssetContent AS AssetContent ON Message.id = AssetContent.message_id AND Message.conversation_id = AssetContent.conversation_id
LEFT JOIN MessageTextContent AS TextContent ON Message.id = TextContent.message_id AND Message.conversation_id = TextContent.conversation_id
Expand Down
Loading

0 comments on commit 3c8ceb0

Please sign in to comment.