Skip to content

Commit

Permalink
change primary/foreign keys to use values from accountId/token/messageId
Browse files Browse the repository at this point in the history
remove conversationId (should not be used anymore, use roomToken instead)

Signed-off-by: Marcel Hibbe <dev@mhibbe.de>
  • Loading branch information
mahibi committed Jul 4, 2024
1 parent 340f411 commit 61549ea
Show file tree
Hide file tree
Showing 24 changed files with 218 additions and 140 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"formatVersion": 1,
"database": {
"version": 10,
"identityHash": "482937f14afa95a62c2dea77613938e7",
"identityHash": "d86bdb57171c8cedf1bd8d499bb9da19",
"entities": [
{
"tableName": "User",
Expand Down Expand Up @@ -138,17 +138,17 @@
},
{
"tableName": "Conversations",
"createSql": "CREATE TABLE IF NOT EXISTS `${TABLE_NAME}` (`id` INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL, `account_id` INTEGER, `token` TEXT, `name` TEXT, `displayName` TEXT, `description` TEXT, `type` TEXT, `lastPing` INTEGER NOT NULL, `participantType` TEXT, `hasPassword` INTEGER NOT NULL, `sessionId` TEXT, `actorId` TEXT, `actorType` TEXT, `isFavorite` INTEGER NOT NULL, `lastActivity` INTEGER NOT NULL, `unreadMessages` INTEGER NOT NULL, `unreadMention` INTEGER NOT NULL, `lastMessage` INTEGER, `objectType` TEXT, `notificationLevel` TEXT, `readOnly` TEXT, `lobbyState` TEXT, `lobbyTimer` INTEGER, `lastReadMessage` INTEGER NOT NULL, `hasCall` INTEGER NOT NULL, `callFlag` INTEGER NOT NULL, `canStartCall` INTEGER NOT NULL, `canLeaveConversation` INTEGER, `canDeleteConversation` INTEGER, `unreadMentionDirect` INTEGER, `notificationCalls` INTEGER, `permissions` INTEGER NOT NULL, `messageExpiration` INTEGER NOT NULL, `status` TEXT, `statusIcon` TEXT, `statusMessage` TEXT, `statusClearAt` INTEGER, `callRecording` INTEGER NOT NULL, `avatarVersion` TEXT, `isCustomAvatar` INTEGER, `callStartTime` INTEGER, `recordingConsent` INTEGER NOT NULL, `remoteServer` TEXT, `remoteToken` TEXT, FOREIGN KEY(`account_id`) REFERENCES `User`(`id`) ON UPDATE CASCADE ON DELETE CASCADE )",
"createSql": "CREATE TABLE IF NOT EXISTS `${TABLE_NAME}` (`internalId` TEXT NOT NULL, `accountId` INTEGER, `token` TEXT, `name` TEXT, `displayName` TEXT, `description` TEXT, `type` TEXT, `lastPing` INTEGER NOT NULL, `participantType` TEXT, `hasPassword` INTEGER NOT NULL, `sessionId` TEXT, `actorId` TEXT, `actorType` TEXT, `isFavorite` INTEGER NOT NULL, `lastActivity` INTEGER NOT NULL, `unreadMessages` INTEGER NOT NULL, `unreadMention` INTEGER NOT NULL, `lastMessage` INTEGER, `objectType` TEXT, `notificationLevel` TEXT, `readOnly` TEXT, `lobbyState` TEXT, `lobbyTimer` INTEGER, `lastReadMessage` INTEGER NOT NULL, `hasCall` INTEGER NOT NULL, `callFlag` INTEGER NOT NULL, `canStartCall` INTEGER NOT NULL, `canLeaveConversation` INTEGER, `canDeleteConversation` INTEGER, `unreadMentionDirect` INTEGER, `notificationCalls` INTEGER, `permissions` INTEGER NOT NULL, `messageExpiration` INTEGER NOT NULL, `status` TEXT, `statusIcon` TEXT, `statusMessage` TEXT, `statusClearAt` INTEGER, `callRecording` INTEGER NOT NULL, `avatarVersion` TEXT, `isCustomAvatar` INTEGER, `callStartTime` INTEGER, `recordingConsent` INTEGER NOT NULL, `remoteServer` TEXT, `remoteToken` TEXT, PRIMARY KEY(`internalId`), FOREIGN KEY(`accountId`) REFERENCES `User`(`id`) ON UPDATE CASCADE ON DELETE CASCADE )",
"fields": [
{
"fieldPath": "id",
"columnName": "id",
"affinity": "INTEGER",
"fieldPath": "internalId",
"columnName": "internalId",
"affinity": "TEXT",
"notNull": true
},
{
"fieldPath": "accountId",
"columnName": "account_id",
"columnName": "accountId",
"affinity": "INTEGER",
"notNull": false
},
Expand Down Expand Up @@ -406,29 +406,29 @@
}
],
"primaryKey": {
"autoGenerate": true,
"autoGenerate": false,
"columnNames": [
"id"
"internalId"
]
},
"indices": [
{
"name": "index_Conversations_account_id",
"name": "index_Conversations_accountId",
"unique": false,
"columnNames": [
"account_id"
"accountId"
],
"orders": [],
"createSql": "CREATE INDEX IF NOT EXISTS `index_Conversations_account_id` ON `${TABLE_NAME}` (`account_id`)"
"createSql": "CREATE INDEX IF NOT EXISTS `index_Conversations_accountId` ON `${TABLE_NAME}` (`accountId`)"
}
],
"foreignKeys": [
{
"table": "User",
"onDelete": "CASCADE",
"onUpdate": "CASCADE",
"onUpdate": "CASCADE",
"columns": [
"account_id"
"accountId"
],
"referencedColumns": [
"id"
Expand All @@ -438,20 +438,32 @@
},
{
"tableName": "ChatMessages",
"createSql": "CREATE TABLE IF NOT EXISTS `${TABLE_NAME}` (`id` INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL, `internal_conversation_id` INTEGER, `message` TEXT, `token` TEXT, `actorType` TEXT, `actorId` TEXT, `actorDisplayName` TEXT, `timestamp` INTEGER NOT NULL, `messageParameters` TEXT, `systemMessage` TEXT, `isReplyable` INTEGER NOT NULL, `parent` INTEGER, `messageType` TEXT, `reactions` TEXT, `reactionsSelf` TEXT, `expirationTimestamp` INTEGER NOT NULL, `markdown` INTEGER, `lastEditActorDisplayName` TEXT, `lastEditActorId` TEXT, `lastEditActorType` TEXT, `lastEditTimestamp` INTEGER NOT NULL)",
"createSql": "CREATE TABLE IF NOT EXISTS `${TABLE_NAME}` (`internalId` TEXT NOT NULL, `internalConversationId` TEXT, `accountId` INTEGER, `id` INTEGER NOT NULL, `message` TEXT, `token` TEXT, `actorType` TEXT, `actorId` TEXT, `actorDisplayName` TEXT, `timestamp` INTEGER NOT NULL, `messageParameters` TEXT, `systemMessage` TEXT, `isReplyable` INTEGER NOT NULL, `parent` INTEGER, `messageType` TEXT, `reactions` TEXT, `reactionsSelf` TEXT, `expirationTimestamp` INTEGER NOT NULL, `markdown` INTEGER, `lastEditActorDisplayName` TEXT, `lastEditActorId` TEXT, `lastEditActorType` TEXT, `lastEditTimestamp` INTEGER NOT NULL, PRIMARY KEY(`internalId`), FOREIGN KEY(`internalConversationId`) REFERENCES `Conversations`(`internalId`) ON UPDATE CASCADE ON DELETE CASCADE )",
"fields": [
{
"fieldPath": "id",
"columnName": "id",
"affinity": "INTEGER",
"fieldPath": "internalId",
"columnName": "internalId",
"affinity": "TEXT",
"notNull": true
},
{
"fieldPath": "internalConversationId",
"columnName": "internal_conversation_id",
"columnName": "internalConversationId",
"affinity": "TEXT",
"notNull": false
},
{
"fieldPath": "accountId",
"columnName": "accountId",
"affinity": "INTEGER",
"notNull": false
},
{
"fieldPath": "id",
"columnName": "id",
"affinity": "INTEGER",
"notNull": true
},
{
"fieldPath": "message",
"columnName": "message",
Expand Down Expand Up @@ -518,11 +530,11 @@
"affinity": "TEXT",
"notNull": false
},
{
"fieldPath": "reactions",
"columnName": "reactions",
"affinity": "TEXT",
"notNull": false
{
"fieldPath": "reactions",
"columnName": "reactions",
"affinity": "TEXT",
"notNull": false
},
{
"fieldPath": "reactionsSelf",
Expand Down Expand Up @@ -568,19 +580,50 @@
}
],
"primaryKey": {
"autoGenerate": true,
"autoGenerate": false,
"columnNames": [
"id"
"internalId"
]
},
"indices": [],
"foreignKeys": []
"indices": [
{
"name": "index_ChatMessages_internalId",
"unique": true,
"columnNames": [
"internalId"
],
"orders": [],
"createSql": "CREATE UNIQUE INDEX IF NOT EXISTS `index_ChatMessages_internalId` ON `${TABLE_NAME}` (`internalId`)"
},
{
"name": "index_ChatMessages_internalConversationId",
"unique": false,
"columnNames": [
"internalConversationId"
],
"orders": [],
"createSql": "CREATE INDEX IF NOT EXISTS `index_ChatMessages_internalConversationId` ON `${TABLE_NAME}` (`internalConversationId`)"
}
],
"foreignKeys": [
{
"table": "Conversations",
"onDelete": "CASCADE",
"onUpdate": "CASCADE",
"columns": [
"internalConversationId"
],
"referencedColumns": [
"internalId"
]
}
]
}
],
"views": [],
"setupQueries": [
"CREATE TABLE IF NOT EXISTS room_master_table (id INTEGER PRIMARY KEY,identity_hash TEXT)",
"INSERT OR REPLACE INTO room_master_table (id,identity_hash) VALUES(42, '482937f14afa95a62c2dea77613938e7')"
"INSERT OR REPLACE INTO room_master_table (id,identity_hash) VALUES(42, 'd86bdb57171c8cedf1bd8d499bb9da19')"
]
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,8 @@ class ChatMessagesDaoTest {
private lateinit var db: TalkDatabase
private val tag = ChatMessagesDaoTest::class.java.simpleName

var chatMessageCounter: Long = 1

@Before
fun createDb() {
val context = ApplicationProvider.getApplicationContext<Context>()
Expand Down Expand Up @@ -83,7 +85,7 @@ class ChatMessagesDaoTest {
// Lets imagine we are on conversations screen...
conversationsDao.getConversationsForUser(account1.id).first().forEach {
Log.d(tag, "- next Conversation for account1 -")
Log.d(tag, "id (PK): " + it.id)
Log.d(tag, "internalId (PK): " + it.internalId)
Log.d(tag, "accountId: " + it.accountId)
Log.d(tag, "name: " + it.name)
Log.d(tag, "token: " + it.token)
Expand All @@ -104,45 +106,45 @@ class ChatMessagesDaoTest {
// Lets insert some messages to the conversations
chatMessagesDao.upsertChatMessages(
listOf(
createChatMessageEntity(conversation1.id, "hello"),
createChatMessageEntity(conversation1.id, "here"),
createChatMessageEntity(conversation1.id, "are"),
createChatMessageEntity(conversation1.id, "some"),
createChatMessageEntity(conversation1.id, "messages")
createChatMessageEntity(conversation1.internalId, "hello"),
createChatMessageEntity(conversation1.internalId, "here"),
createChatMessageEntity(conversation1.internalId, "are"),
createChatMessageEntity(conversation1.internalId, "some"),
createChatMessageEntity(conversation1.internalId, "messages")
)
)
chatMessagesDao.upsertChatMessages(
listOf(
createChatMessageEntity(conversation2.id, "first message in conversation 2")
createChatMessageEntity(conversation2.internalId, "first message in conversation 2")
)
)

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

val chatMessagesConv1 = chatMessagesDao.getMessagesForConversation(conversation1.id)
val chatMessagesConv1 = chatMessagesDao.getMessagesForConversation(conversation1.internalId)
assertEquals(5, chatMessagesConv1.first().size)

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

assertEquals("here", chatMessagesConv1.first()[1].message)

val conv1chatMessage3 = chatMessagesDao.getChatMessageForConversation(conversation1.id, 3).first()
val conv1chatMessage3 = chatMessagesDao.getChatMessageForConversation(conversation1.internalId, 3).first()
assertEquals("are", conv1chatMessage3.message)

val chatMessagesConv1Since =
chatMessagesDao.getMessagesForConversationSince(conversation1.id, conv1chatMessage3.id)
chatMessagesDao.getMessagesForConversationSince(conversation1.internalId, conv1chatMessage3.id)
assertEquals(3, chatMessagesConv1Since.first().size)
assertEquals("are", chatMessagesConv1Since.first()[0].message)
assertEquals("some", chatMessagesConv1Since.first()[1].message)
assertEquals("messages", chatMessagesConv1Since.first()[2].message)

val chatMessagesConv1To =
chatMessagesDao.getMessagesForConversationBefore(conversation1.id, conv1chatMessage3.id, 3)
chatMessagesDao.getMessagesForConversationBefore(conversation1.internalId, conv1chatMessage3.id, 3)
assertEquals(2, chatMessagesConv1To.first().size)
assertEquals("hello", chatMessagesConv1To.first()[1].message)
assertEquals("here", chatMessagesConv1To.first()[0].message)
Expand All @@ -168,13 +170,16 @@ class ChatMessagesDaoTest {
val token = (0..10000000).random().toString()

return ConversationEntity(
internalId = "$accountId@$token",
accountId = accountId,
token = token,
name = roomName
)
}

private fun createChatMessageEntity(internalConversationId: Long, message: String): ChatMessageEntity {
private fun createChatMessageEntity(internalConversationId: String, message: String): ChatMessageEntity {
val id = chatMessageCounter++

val emoji1 = "\uD83D\uDE00" // 😀
val emoji2 = "\uD83D\uDE1C" // 😜
val reactions = LinkedHashMap<String, Int>()
Expand All @@ -185,7 +190,9 @@ class ChatMessagesDaoTest {
reactionsSelf.add(emoji1)

val entity = ChatMessageEntity(
internalId = "$internalConversationId@$id",
internalConversationId = internalConversationId,
id = id,
message = message,
reactions = reactions,
reactionsSelf = reactionsSelf
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ class ConversationItem(
ISectionable<ConversationItemViewHolder, GenericTextHeaderItem?>,
IFilterable<String?> {
private var header: GenericTextHeaderItem? = null
private val chatMessage = model.lastMessage?.asEntity()?.asModel() // TODO fix this work around
private val chatMessage = model.lastMessage?.asEntity(user.id!!)?.asModel() // TODO fix this work around

constructor(
conversation: Conversation,
Expand Down
10 changes: 7 additions & 3 deletions app/src/main/java/com/nextcloud/talk/chat/ChatActivity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -583,17 +583,19 @@ class ChatActivity :
updateRoomTimerHandler()

val urlForChatting = ApiUtils.getUrlForChat(chatApiVersion, conversationUser?.baseUrl, roomToken)

// TODO: pass lookIntoFuture true
chatViewModel.loadMoreMessages(
beforeMessageId = -1, // gets history of conversation
withCredentials = credentials!!,
withUrl = urlForChatting,
withConversationId = currentConversation!!.roomId!!.toLong(),
roomToken = currentConversation!!.token!!,
withMessageLimit = MESSAGE_PULL_LIMIT
)
chatViewModel.initMessagePolling(
withCredentials = credentials!!,
withUrl = urlForChatting,
withConversationId = currentConversation!!.roomId!!.toLong()
roomToken = currentConversation!!.token!!
)

}
Expand Down Expand Up @@ -2728,12 +2730,14 @@ class ChatActivity :
}?.item as ChatMessage).jsonMessageId

val urlForChatting = ApiUtils.getUrlForChat(chatApiVersion, conversationUser?.baseUrl, roomToken)

// TODO: pass lookIntoFuture false
chatViewModel.loadMoreMessages(
beforeMessageId = id.toLong(),
withUrl = urlForChatting,
withCredentials = credentials!!,
withMessageLimit = MESSAGE_PULL_LIMIT,
withConversationId = currentConversation!!.roomId!!.toLong()
roomToken = currentConversation!!.token!!
)
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ interface ChatMessageRepository : Syncable, LifecycleAwareManager {
*/
fun loadMoreMessages(
beforeMessageId: Long,
withConversationId: Long,
roomToken: String,
withMessageLimit: Int,
withNetworkParams: Bundle
): Job
Expand All @@ -51,7 +51,7 @@ interface ChatMessageRepository : Syncable, LifecycleAwareManager {
*
* [withNetworkParams] credentials and url.
*/
fun initMessagePolling(withConversationId: Long, withNetworkParams: Bundle): Job
fun initMessagePolling(roomToken: String, withNetworkParams: Bundle): Job

/**
* Gets a individual message.
Expand Down
Loading

0 comments on commit 61549ea

Please sign in to comment.