Skip to content

Commit

Permalink
WIP add tests getMessagesForConversationSince / getMessagesForConvers…
Browse files Browse the repository at this point in the history
…ationTo

TODO: check "order by"...

Signed-off-by: Marcel Hibbe <dev@mhibbe.de>
  • Loading branch information
mahibi committed Jun 20, 2024
1 parent 989d475 commit 959f26c
Show file tree
Hide file tree
Showing 9 changed files with 379 additions and 365 deletions.
672 changes: 327 additions & 345 deletions app/schemas/com.nextcloud.talk.data.source.local.TalkDatabase/10.json

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,10 @@ class ChatMessagesDaoTest {
val conversation2 = conversationsDao.getConversationsForUser(account1.id).first()[1]

// Having a conversation token, we can also get a conversation directly
val conversation1GotByToken = conversationsDao.getConversationForUser(account1.id, conversation1.token!!)
val conversation1GotByToken = conversationsDao.getConversationForUser(
account1.id,
conversation1.token!!
).first()

assertEquals(conversation1, conversation1GotByToken)

Expand All @@ -112,14 +115,30 @@ class ChatMessagesDaoTest {
)
)

chatMessagesDao.getMessagesForConversation(conversation1.id).first().forEach {
Log.d(tag, "- next Message for conversation1 (account1)-")
Log.d(tag, "id (PK): " + it.id)
Log.d(tag, "message: " + it.message)
}

val conv1chatMessage3 = chatMessagesDao.getChatMessageForConversation(conversation1.id, 3).first()

// by having a conversation, we can query it's messages
val chatMessagesConv1 = chatMessagesDao.getMessagesForConversation(conversation1.id)
assertEquals(5, chatMessagesConv1.first().size)

val chatMessagesConv2 = chatMessagesDao.getMessagesForConversation(conversation2.id)
assertEquals(1, chatMessagesConv2.first().size)

assertEquals(chatMessagesConv1.first()[3].message, "some")
assertEquals("some", chatMessagesConv1.first()[3].message)

val chatMessagesConv1Since =
chatMessagesDao.getMessagesForConversationSince(conversation1.id, conv1chatMessage3.id)
assertEquals(3, chatMessagesConv1Since.first().size)

val chatMessagesConv1To =
chatMessagesDao.getMessagesForConversationTo(conversation1.id, conv1chatMessage3.id)
assertEquals(3, chatMessagesConv1To.first().size)
}

private fun createUserEntity(userId: String, userName: String) =
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,10 @@ interface ChatMessageRepository : Syncable {
/**
* Gets available messages as a stream
*/
fun getMessages(id: Long): Flow<List<ChatMessageModel>>
fun getMessages(conversationId: Long): Flow<List<ChatMessageModel>>

/**
* Gets a individual message
*/
fun getMessage(id: Long): Flow<ChatMessageModel>
fun getMessage(conversationId: Long, messageId: Long): Flow<ChatMessageModel>
}
Original file line number Diff line number Diff line change
Expand Up @@ -33,13 +33,13 @@ class OfflineFirstChatRepository @Inject constructor(
private val datastore: AppPreferences
) : ChatMessageRepository {

override fun getMessages(id: Long): Flow<List<ChatMessageModel>> =
chatDao.getMessagesForConversation(id).map {
override fun getMessages(conversationId: Long): Flow<List<ChatMessageModel>> =
chatDao.getMessagesForConversation(conversationId).map {
it.map(ChatMessageEntity::asModel)
}

override fun getMessage(id: Long): Flow<ChatMessageModel> =
chatDao.getChatMessage(id).map(ChatMessageEntity::asModel)
override fun getMessage(conversationId: Long, messageId: Long): Flow<ChatMessageModel> =
chatDao.getChatMessageForConversation(conversationId, messageId).map(ChatMessageEntity::asModel)

@Suppress("UNCHECKED_CAST")
private fun getMessagesFromServer(bundle: Bundle): List<ChatMessage> {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -369,6 +369,8 @@ class ChatViewModel @Inject constructor(
}

fun pullChatMessages(credentials: String, url: String) {
// TODO: this would be the palce to call chatRepository.sync() ??

chatRepository.pullChatMessages(credentials, url, _getFieldMapForChat.value!!)
.subscribeOn(Schedulers.io())
.takeUntil { (currentLifeCycleFlag == LifeCycleFlag.PAUSED) }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,8 @@ interface ChatMessagesDao {
@Upsert
fun upsertChatMessages(chatMessages: List<ChatMessageEntity>)

@Query("SELECT * FROM ChatMessages where id = :messageId")
fun getChatMessage(messageId: Long): Flow<ChatMessageEntity>
@Query("SELECT * FROM ChatMessages where internal_conversation_id = :conversationId AND id = :messageId")
fun getChatMessageForConversation(conversationId: Long, messageId: Long): Flow<ChatMessageEntity>

/**
* Deletes rows in the db matching the specified [messageIds]
Expand All @@ -38,4 +38,14 @@ interface ChatMessagesDao {

@Update
fun updateChatMessage(message: ChatMessageEntity)

@Query(
"SELECT * FROM ChatMessages WHERE internal_conversation_id = :conversationId AND id >= :messageId ORDER BY timestamp ASC, id ASC"
)
fun getMessagesForConversationSince(conversationId: Long, messageId: Long): Flow<List<ChatMessageEntity>>

@Query(
"SELECT * FROM ChatMessages WHERE internal_conversation_id = :conversationId AND id <= :messageId ORDER BY timestamp DESC, id DESC"
)
fun getMessagesForConversationTo(conversationId: Long, messageId: Long): Flow<List<ChatMessageEntity>>
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,13 +20,13 @@ fun ChatMessage.asEntity() =
actorId = actorId,
actorDisplayName = actorDisplayName,
timestamp = timestamp,
messageParameters = messageParameters,
// messageParameters = messageParameters,
systemMessageType = systemMessageType,
replyable = replyable,
parentMessageId = parentMessage?.id?.toLong(),
messageType = messageType,
reactions = reactions,
reactionsSelf = reactionsSelf,
// reactions = reactions,
// reactionsSelf = reactionsSelf,
expirationTimestamp = expirationTimestamp,
renderMarkdown = renderMarkdown,
lastEditActorDisplayName = lastEditActorDisplayName,
Expand All @@ -44,13 +44,13 @@ fun ChatMessageEntity.asModel() =
actorId = actorId,
actorDisplayName = actorDisplayName,
timestamp = timestamp,
messageParameters = messageParameters,
// messageParameters = messageParameters,
systemMessageType = systemMessageType,
replyable = replyable,
parentMessageId = parentMessageId,
messageType = messageType,
reactions = reactions,
reactionsSelf = reactionsSelf,
// reactions = reactions,
// reactionsSelf = reactionsSelf,
expirationTimestamp = expirationTimestamp,
renderMarkdown = renderMarkdown,
lastEditActorDisplayName = lastEditActorDisplayName,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,13 +40,13 @@ data class ChatMessageEntity(
@ColumnInfo(name = "actorId") var actorId: String? = null,
@ColumnInfo(name = "actorDisplayName") var actorDisplayName: String? = null,
@ColumnInfo(name = "timestamp") var timestamp: Long = 0,
@ColumnInfo(name = "messageParameters") var messageParameters: HashMap<String?, HashMap<String?, String?>>? = null,
// @ColumnInfo(name = "messageParameters") var messageParameters: HashMap<String?, HashMap<String?, String?>>? = null,
@ColumnInfo(name = "systemMessage") var systemMessageType: ChatMessage.SystemMessageType? = null,
@ColumnInfo(name = "isReplyable") var replyable: Boolean = false,
@ColumnInfo(name = "parent") var parentMessageId: Long? = null, // TODO refactor code to deal with this
@ColumnInfo(name = "messageType") var messageType: String? = null,
@ColumnInfo(name = "reactions") var reactions: LinkedHashMap<String, Int>? = null,
@ColumnInfo(name = "reactionsSelf") var reactionsSelf: ArrayList<String>? = null,
// @ColumnInfo(name = "reactions") var reactions: LinkedHashMap<String, Int>? = null,
// @ColumnInfo(name = "reactionsSelf") var reactionsSelf: ArrayList<String>? = null,
@ColumnInfo(name = "expirationTimestamp") var expirationTimestamp: Int = 0,
@ColumnInfo(name = "markdown") var renderMarkdown: Boolean? = null,
@ColumnInfo(name = "lastEditActorDisplayName") var lastEditActorDisplayName: String? = null,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -148,7 +148,8 @@ data class ChatMessage(

override var changedId: Int = jsonMessageId,

override var markedForDeletion: Boolean = "comment_deleted" == messageType
// override var markedForDeletion: Boolean = "comment_deleted" == messageType
override var markedForDeletion: Boolean = false

) : Parcelable, MessageContentType, MessageContentType.Image, SyncableModel {

Expand Down

0 comments on commit 959f26c

Please sign in to comment.