Merge pull request #934 from neuroscr/opengroupordering

Fix message order when mix multidevice messages come in AND default avatars
pull/950/head
Ryan Tharp 5 years ago committed by GitHub
commit 91ad732549
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

@ -5,6 +5,7 @@ const nodeFetch = require('node-fetch');
const { URL, URLSearchParams } = require('url'); const { URL, URLSearchParams } = require('url');
const FormData = require('form-data'); const FormData = require('form-data');
const https = require('https'); const https = require('https');
const path = require('path');
// Can't be less than 1200 if we have unauth'd requests // Can't be less than 1200 if we have unauth'd requests
const PUBLICCHAT_MSG_POLL_EVERY = 1.5 * 1000; // 1.5s const PUBLICCHAT_MSG_POLL_EVERY = 1.5 * 1000; // 1.5s
@ -627,6 +628,12 @@ class LokiAppDotNetServerAPI {
url url
); );
} }
if (mode === '_sendToProxy') {
// if we can detect, certain types of failures, we can retry...
if (e.code === 'ECONNRESET') {
// retry with counter?
}
}
return { return {
err: e, err: e,
}; };
@ -1248,6 +1255,17 @@ class LokiPublicChannelAPI {
this.conversation.setGroupName(note.value.name); this.conversation.setGroupName(note.value.name);
} }
if (note.value && note.value.avatar) { if (note.value && note.value.avatar) {
if (note.value.avatar.match(/^images\//)) {
// local file avatar
const resolvedAvatar = path.normalize(note.value.avatar);
const base = path.normalize('images/');
const re = new RegExp(`^${base}`);
// do we at least ends up inside images/ somewhere?
if (re.test(resolvedAvatar)) {
this.conversation.set('avatar', resolvedAvatar);
}
} else {
// relative URL avatar
const avatarAbsUrl = this.serverAPI.baseServerUrl + note.value.avatar; const avatarAbsUrl = this.serverAPI.baseServerUrl + note.value.avatar;
const { const {
writeNewAttachmentData, writeNewAttachmentData,
@ -1281,6 +1299,7 @@ class LokiPublicChannelAPI {
// update group // update group
this.conversation.set('avatar', newAttributes.avatar); this.conversation.set('avatar', newAttributes.avatar);
} }
}
// is it mutable? // is it mutable?
// who are the moderators? // who are the moderators?
// else could set a default in case of server problems... // else could set a default in case of server problems...
@ -1414,7 +1433,7 @@ class LokiPublicChannelAPI {
} }
if (quote) { if (quote) {
// TODO: Enable quote attachments again using proper ADN style // Disable quote attachments
quote.attachments = []; quote.attachments = [];
} }
@ -1518,6 +1537,14 @@ class LokiPublicChannelAPI {
}); });
if (res.err || !res.response) { if (res.err || !res.response) {
log.error(
'Could not get messages from',
this.serverAPI.baseServerUrl,
this.baseChannelUrl
);
if (res.err) {
log.error('pollOnceForMessages receive error', res.err);
}
return; return;
} }
@ -1705,18 +1732,31 @@ class LokiPublicChannelAPI {
// filter out invalid messages // filter out invalid messages
pendingMessages = pendingMessages.filter(messageData => !!messageData); pendingMessages = pendingMessages.filter(messageData => !!messageData);
// separate messages coming from primary and secondary devices // separate messages coming from primary and secondary devices
const [primaryMessages, slaveMessages] = _.partition( let [primaryMessages, slaveMessages] = _.partition(
pendingMessages, pendingMessages,
message => !(message.source in slavePrimaryMap) message => !(message.source in slavePrimaryMap)
); );
// process primary devices' message directly // get minimum ID for primaryMessages and slaveMessages
primaryMessages.forEach(message => const firstPrimaryId = _.min(primaryMessages.map(msg => msg.serverId));
const firstSlaveId = _.min(slaveMessages.map(msg => msg.serverId));
if (firstPrimaryId < firstSlaveId) {
// early send
// split off count from pendingMessages
let sendNow = [];
[sendNow, pendingMessages] = _.partition(
pendingMessages,
message => message.serverId < firstSlaveId
);
sendNow.forEach(message => {
// send them out now
log.info('emitting primary message', message.serverId);
this.chatAPI.emit('publicMessage', { this.chatAPI.emit('publicMessage', {
message, message,
}) });
); });
sendNow = false;
pendingMessages = []; // allow memory to be freed }
primaryMessages = false; // free memory
// get actual chat server data (mainly the name rn) of primary device // get actual chat server data (mainly the name rn) of primary device
const verifiedDeviceResults = await this.serverAPI.getUsers( const verifiedDeviceResults = await this.serverAPI.getUsers(
@ -1773,11 +1813,25 @@ class LokiPublicChannelAPI {
messageData.message.profileKey = profileKey; messageData.message.profileKey = profileKey;
} }
} }
/* eslint-enable no-param-reassign */ });
slaveMessages = false; // free memory
// process all messages in the order received
pendingMessages.forEach(message => {
// if slave device
if (message.source in slavePrimaryMap) {
// prevent our own device sent messages from coming back in
if (message.source === ourNumberDevice) {
// we originally sent these
return;
}
}
log.info('emitting pending message', message.serverId);
this.chatAPI.emit('publicMessage', { this.chatAPI.emit('publicMessage', {
message: messageData, message,
}); });
}); });
/* eslint-enable no-param-reassign */ /* eslint-enable no-param-reassign */
// if we received one of our own messages // if we received one of our own messages

Loading…
Cancel
Save