Add channel id to message table and provide helper functions

pull/390/head
Beaudan 6 years ago
parent f857744e88
commit 899bfc3b2b

@ -118,6 +118,7 @@ module.exports = {
removeMessage, removeMessage,
getUnreadByConversation, getUnreadByConversation,
getMessageBySender, getMessageBySender,
getMessageByServerId,
getMessageById, getMessageById,
getAllMessages, getAllMessages,
getAllMessageIds, getAllMessageIds,
@ -801,6 +802,11 @@ async function updateToLokiSchemaVersion1(currentVersion, instance) {
const { id, type, name, friendRequestStatus } = publicChatData; const { id, type, name, friendRequestStatus } = publicChatData;
await instance.run(
`ALTER TABLE messages
ADD COLUMN serverId STRING;`
);
await instance.run( await instance.run(
`INSERT INTO conversations ( `INSERT INTO conversations (
id, id,
@ -1718,6 +1724,7 @@ async function saveMessage(data, { forceSave } = {}) {
hasFileAttachments, hasFileAttachments,
hasVisualMediaAttachments, hasVisualMediaAttachments,
id, id,
serverId,
// eslint-disable-next-line camelcase // eslint-disable-next-line camelcase
received_at, received_at,
schemaVersion, schemaVersion,
@ -1736,6 +1743,7 @@ async function saveMessage(data, { forceSave } = {}) {
$id: id, $id: id,
$json: objectToJSON(data), $json: objectToJSON(data),
$serverId: serverId,
$body: body, $body: body,
$conversationId: conversationId, $conversationId: conversationId,
$expirationStartTimestamp: expirationStartTimestamp, $expirationStartTimestamp: expirationStartTimestamp,
@ -1758,6 +1766,7 @@ async function saveMessage(data, { forceSave } = {}) {
await db.run( await db.run(
`UPDATE messages SET `UPDATE messages SET
json = $json, json = $json,
serverId = $serverId,
body = $body, body = $body,
conversationId = $conversationId, conversationId = $conversationId,
expirationStartTimestamp = $expirationStartTimestamp, expirationStartTimestamp = $expirationStartTimestamp,
@ -1792,6 +1801,7 @@ async function saveMessage(data, { forceSave } = {}) {
id, id,
json, json,
serverId,
body, body,
conversationId, conversationId,
expirationStartTimestamp, expirationStartTimestamp,
@ -1812,6 +1822,7 @@ async function saveMessage(data, { forceSave } = {}) {
$id, $id,
$json, $json,
$serverId,
$body, $body,
$conversationId, $conversationId,
$expirationStartTimestamp, $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) { async function getMessageById(id) {
const row = await db.get('SELECT * FROM messages WHERE id = $id;', { const row = await db.get('SELECT * FROM messages WHERE id = $id;', {
$id: id, $id: id,

@ -1242,6 +1242,17 @@
Message: Whisper.Message, 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) { async setIsPublic(isPublic) {
if (_.isEqual(this.get('isPublic'), isPublic)) return; if (_.isEqual(this.get('isPublic'), isPublic)) return;

@ -141,6 +141,7 @@ module.exports = {
removeAllMessagesInConversation, removeAllMessagesInConversation,
getMessageBySender, getMessageBySender,
getMessageByServerId,
getMessageById, getMessageById,
getAllMessages, getAllMessages,
getAllUnsentMessages, getAllUnsentMessages,
@ -875,6 +876,15 @@ async function _removeMessages(ids) {
await channels.removeMessage(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 }) { async function getMessageById(id, { Message }) {
const message = await channels.getMessageById(id); const message = await channels.getMessageById(id);
if (!message) { if (!message) {

Loading…
Cancel
Save