diff --git a/app/sql.js b/app/sql.js index f1f6ad229..d64428152 100644 --- a/app/sql.js +++ b/app/sql.js @@ -118,6 +118,7 @@ module.exports = { removeMessage, getUnreadByConversation, getMessageBySender, + getMessageByServerId, getMessageById, getAllMessages, getAllMessageIds, @@ -801,6 +802,11 @@ async function updateToLokiSchemaVersion1(currentVersion, instance) { const { id, type, name, friendRequestStatus } = publicChatData; + await instance.run( + `ALTER TABLE messages + ADD COLUMN serverId STRING;` + ); + await instance.run( `INSERT INTO conversations ( id, @@ -1718,6 +1724,7 @@ async function saveMessage(data, { forceSave } = {}) { hasFileAttachments, hasVisualMediaAttachments, id, + serverId, // eslint-disable-next-line camelcase received_at, schemaVersion, @@ -1736,6 +1743,7 @@ async function saveMessage(data, { forceSave } = {}) { $id: id, $json: objectToJSON(data), + $serverId: serverId, $body: body, $conversationId: conversationId, $expirationStartTimestamp: expirationStartTimestamp, @@ -1758,6 +1766,7 @@ async function saveMessage(data, { forceSave } = {}) { await db.run( `UPDATE messages SET json = $json, + serverId = $serverId, body = $body, conversationId = $conversationId, expirationStartTimestamp = $expirationStartTimestamp, @@ -1792,6 +1801,7 @@ async function saveMessage(data, { forceSave } = {}) { id, json, + serverId, body, conversationId, expirationStartTimestamp, @@ -1812,6 +1822,7 @@ async function saveMessage(data, { forceSave } = {}) { $id, $json, + $serverId, $body, $conversationId, $expirationStartTimestamp, @@ -1934,6 +1945,18 @@ async function removeMessage(id) { ); } +async function getMessageByServerId(id) { + const row = await db.get('SELECT * FROM messages WHERE ServerId = $ServerId;', { + $ServerId: ServerId, + }); + + if (!row) { + return null; + } + + return jsonToObject(row.json); +} + async function getMessageById(id) { const row = await db.get('SELECT * FROM messages WHERE id = $id;', { $id: id, diff --git a/js/models/messages.js b/js/models/messages.js index d1f77a3a5..6aee70a89 100644 --- a/js/models/messages.js +++ b/js/models/messages.js @@ -1242,6 +1242,17 @@ Message: Whisper.Message, }); }, + async setServerId(serverId) { + if (_.isEqual(this.get('serverId'), serverId)) return; + + this.set({ + serverId, + }); + + await window.Signal.Data.saveMessage(this.attributes, { + Message: Whisper.Message, + }); + }, async setIsPublic(isPublic) { if (_.isEqual(this.get('isPublic'), isPublic)) return; diff --git a/js/modules/data.js b/js/modules/data.js index 347fc36fc..cebc102cb 100644 --- a/js/modules/data.js +++ b/js/modules/data.js @@ -141,6 +141,7 @@ module.exports = { removeAllMessagesInConversation, getMessageBySender, + getMessageByServerId, getMessageById, getAllMessages, getAllUnsentMessages, @@ -875,6 +876,15 @@ async function _removeMessages(ids) { await channels.removeMessage(ids); } +async function getMessageByServerId(id, { Message }) { + const message = await channels.getMessageByServerId(id); + if (!message) { + return null; + } + + return new Message(message); +} + async function getMessageById(id, { Message }) { const message = await channels.getMessageById(id); if (!message) {