Merge pull request #390 from BeaudanBrown/use-server-id

Use server id
pull/446/head
Beaudan Campbell-Brown 6 years ago committed by GitHub
commit b006e87941
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

@ -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,21 @@ async function removeMessage(id) {
); );
} }
async function getMessageByServerId(serverId) {
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,

@ -750,14 +750,17 @@
} }
}); });
Whisper.events.on('publicMessageSent', ({ pubKey, timestamp }) => { Whisper.events.on(
try { 'publicMessageSent',
const conversation = ConversationController.get(pubKey); ({ pubKey, timestamp, serverId }) => {
conversation.onPublicMessageSent(pubKey, timestamp); try {
} catch (e) { const conversation = ConversationController.get(pubKey);
window.log.error('Error setting public on message'); conversation.onPublicMessageSent(pubKey, timestamp, serverId);
} catch (e) {
window.log.error('Error setting public on message');
}
} }
}); );
Whisper.events.on('password-updated', () => { Whisper.events.on('password-updated', () => {
if (appView && appView.inboxView) { if (appView && appView.inboxView) {
@ -1411,7 +1414,6 @@
const { isError } = options; const { isError } = options;
let messageData = { let messageData = {
id: data.id,
source: data.source, source: data.source,
sourceDevice: data.sourceDevice, sourceDevice: data.sourceDevice,
sent_at: data.timestamp, sent_at: data.timestamp,

@ -368,9 +368,14 @@
await Promise.all(messages.map(m => m.setIsP2p(true))); await Promise.all(messages.map(m => m.setIsP2p(true)));
}, },
async onPublicMessageSent(pubKey, timestamp) { async onPublicMessageSent(pubKey, timestamp, serverId) {
const messages = this._getMessagesWithTimestamp(pubKey, timestamp); const messages = this._getMessagesWithTimestamp(pubKey, timestamp);
await Promise.all(messages.map(m => m.setIsPublic(true))); await Promise.all(
messages.map(message => [
message.setIsPublic(true),
message.setServerId(serverId),
])
);
}, },
async onNewMessage(message) { async onNewMessage(message) {
@ -1357,7 +1362,6 @@
options.messageType = message.get('type'); options.messageType = message.get('type');
if (this.isPublic()) { if (this.isPublic()) {
options.publicEndpoint = this.getEndpoint(); options.publicEndpoint = this.getEndpoint();
options.messageId = id;
} }
const groupNumbers = this.getRecipients(); const groupNumbers = this.getRecipients();

@ -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;
@ -2014,9 +2025,7 @@
} else { } else {
await conversation.onFriendRequestAccepted(); await conversation.onFriendRequestAccepted();
} }
// Force save if the message already has an id, used for public channels
const id = await window.Signal.Data.saveMessage(message.attributes, { const id = await window.Signal.Data.saveMessage(message.attributes, {
forceSave: !!message.id,
Message: Whisper.Message, Message: Whisper.Message,
}); });
message.set({ id }); message.set({ id });

@ -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) {

@ -80,7 +80,6 @@ class LokiMessageAPI {
isPing = false, isPing = false,
numConnections = DEFAULT_CONNECTIONS, numConnections = DEFAULT_CONNECTIONS,
publicEndpoint = null, publicEndpoint = null,
messageId = null,
} = options; } = options;
// Data required to identify a message in a conversation // Data required to identify a message in a conversation
const messageEventData = { const messageEventData = {
@ -106,13 +105,12 @@ class LokiMessageAPI {
timestamp: messageTimeStamp, timestamp: messageTimeStamp,
from: displayName, from: displayName,
source: this.ourKey, source: this.ourKey,
id: messageId,
}, },
}, },
], ],
}; };
try { try {
await nodeFetch(publicEndpoint, { const result = await nodeFetch(publicEndpoint, {
method: 'post', method: 'post',
headers: { headers: {
'Content-Type': 'application/json', 'Content-Type': 'application/json',
@ -120,6 +118,8 @@ class LokiMessageAPI {
}, },
body: JSON.stringify(payload), body: JSON.stringify(payload),
}); });
const body = await result.json();
messageEventData.serverId = body.data.id;
window.Whisper.events.trigger('publicMessageSent', messageEventData); window.Whisper.events.trigger('publicMessageSent', messageEventData);
return; return;
} catch (e) { } catch (e) {

@ -161,14 +161,13 @@ class LokiPublicChannelAPI {
let timestamp = new Date(adnMessage.created_at).getTime(); let timestamp = new Date(adnMessage.created_at).getTime();
let from = adnMessage.user.username; let from = adnMessage.user.username;
let source; let source;
let id;
if (adnMessage.annotations.length) { if (adnMessage.annotations.length) {
const noteValue = adnMessage.annotations[0].value; const noteValue = adnMessage.annotations[0].value;
({ from, timestamp, source, id } = noteValue); ({ from, timestamp, source } = noteValue);
} }
const messageData = { const messageData = {
id, serverId: adnMessage.id,
friendRequest: false, friendRequest: false,
source, source,
sourceDevice: 1, sourceDevice: 1,

@ -50,12 +50,10 @@ function OutgoingMessage(
messageType, messageType,
isPing, isPing,
publicEndpoint, publicEndpoint,
messageId,
} = } =
options || {}; options || {};
this.numberInfo = numberInfo; this.numberInfo = numberInfo;
this.publicEndpoint = publicEndpoint; this.publicEndpoint = publicEndpoint;
this.messageId = messageId;
this.senderCertificate = senderCertificate; this.senderCertificate = senderCertificate;
this.online = online; this.online = online;
this.messageType = messageType || 'outgoing'; this.messageType = messageType || 'outgoing';
@ -205,7 +203,6 @@ OutgoingMessage.prototype = {
}; };
if (this.publicEndpoint) { if (this.publicEndpoint) {
options.publicEndpoint = this.publicEndpoint; options.publicEndpoint = this.publicEndpoint;
options.messageId = this.messageId;
} }
await lokiMessageAPI.sendMessage(pubKey, data, timestamp, ttl, options); await lokiMessageAPI.sendMessage(pubKey, data, timestamp, ttl, options);
} catch (e) { } catch (e) {

Loading…
Cancel
Save