Merge pull request #1 from BeaudanBrown/public-incoming

Prepare for public sync
pull/539/head
Ryan Tharp 6 years ago committed by GitHub
commit a7b1540fb1
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

@ -836,225 +836,225 @@ class LokiPublicChannelAPI {
params, params,
}); });
if (!res.err && res.response) {
let receivedAt = new Date().getTime();
const pubKeys = [];
let pendingMessages = [];
pendingMessages = await Promise.all(
res.response.data.reverse().map(async adnMessage => {
// still update our last received if deleted, not signed or not valid
this.lastGot = !this.lastGot
? adnMessage.id
: Math.max(this.lastGot, adnMessage.id);
if (
!adnMessage.id ||
!adnMessage.user ||
!adnMessage.user.username || // pubKey lives in the username field
!adnMessage.text ||
adnMessage.is_deleted
) {
return false; // Invalid or delete message
}
const messengerData = await this.getMessengerData(adnMessage);
if (messengerData === false) {
return false;
}
const { timestamp, quote, attachments, preview } = messengerData; if (res.err || !res.response) {
if (!timestamp) { return;
return false; // Invalid message }
}
// Duplicate check let receivedAt = new Date().getTime();
const isDuplicate = message => { const pubKeys = [];
// The username in this case is the users pubKey let pendingMessages = [];
const sameUsername = message.username === adnMessage.user.username; pendingMessages = await Promise.all(
const sameText = message.text === adnMessage.text; res.response.data.reverse().map(async adnMessage => {
// Don't filter out messages that are too far apart from each other // still update our last received if deleted, not signed or not valid
const timestampsSimilar = this.lastGot = !this.lastGot
Math.abs(message.timestamp - timestamp) <= ? adnMessage.id
PUBLICCHAT_MIN_TIME_BETWEEN_DUPLICATE_MESSAGES; : Math.max(this.lastGot, adnMessage.id);
return sameUsername && sameText && timestampsSimilar; if (
}; !adnMessage.id ||
!adnMessage.user ||
!adnMessage.user.username || // pubKey lives in the username field
!adnMessage.text ||
adnMessage.is_deleted
) {
return false; // Invalid or delete message
}
// Filter out any messages that we got previously const messengerData = await this.getMessengerData(adnMessage);
if (this.lastMessagesCache.some(isDuplicate)) { if (messengerData === false) {
return false; // Duplicate message return false;
} }
// FIXME: maybe move after the de-multidev-decode const { timestamp, quote, attachments, preview } = messengerData;
// Add the message to the lastMessage cache and keep the last 5 recent messages if (!timestamp) {
this.lastMessagesCache = [ return false; // Invalid message
...this.lastMessagesCache, }
{
username: adnMessage.user.username,
text: adnMessage.text,
timestamp,
},
].splice(-5);
const from = adnMessage.user.name || 'Anonymous'; // profileName // Duplicate check
if (pubKeys.indexOf(`@${adnMessage.user.username}`) === -1) { const isDuplicate = message => {
pubKeys.push(`@${adnMessage.user.username}`); // The username in this case is the users pubKey
} const sameUsername = message.username === adnMessage.user.username;
const sameText = message.text === adnMessage.text;
// Don't filter out messages that are too far apart from each other
const timestampsSimilar =
Math.abs(message.timestamp - timestamp) <=
PUBLICCHAT_MIN_TIME_BETWEEN_DUPLICATE_MESSAGES;
return sameUsername && sameText && timestampsSimilar;
};
// Filter out any messages that we got previously
if (this.lastMessagesCache.some(isDuplicate)) {
return false; // Duplicate message
}
const messageData = { // FIXME: maybe move after the de-multidev-decode
serverId: adnMessage.id, // Add the message to the lastMessage cache and keep the last 5 recent messages
clientVerified: true, this.lastMessagesCache = [
friendRequest: false, ...this.lastMessagesCache,
source: adnMessage.user.username, {
sourceDevice: 1, username: adnMessage.user.username,
text: adnMessage.text,
timestamp, timestamp,
},
].splice(-5);
serverTimestamp: timestamp, const from = adnMessage.user.name || 'Anonymous'; // profileName
receivedAt, if (pubKeys.indexOf(`@${adnMessage.user.username}`) === -1) {
isPublic: true, pubKeys.push(`@${adnMessage.user.username}`);
message: { }
body:
adnMessage.text === timestamp.toString() ? '' : adnMessage.text, const messageData = {
attachments, serverId: adnMessage.id,
group: { clientVerified: true,
id: this.conversationId, friendRequest: false,
type: textsecure.protobuf.GroupContext.Type.DELIVER, source: adnMessage.user.username,
}, sourceDevice: 1,
flags: 0, timestamp,
expireTimer: 0,
profileKey: null, serverTimestamp: timestamp,
timestamp, receivedAt,
received_at: receivedAt, isPublic: true,
sent_at: timestamp, message: {
quote, body:
contact: [], adnMessage.text === timestamp.toString() ? '' : adnMessage.text,
preview, attachments,
profile: { group: {
displayName: from, id: this.conversationId,
}, type: textsecure.protobuf.GroupContext.Type.DELIVER,
}, },
}; flags: 0,
receivedAt += 1; // Ensure different arrival times expireTimer: 0,
profileKey: null,
timestamp,
received_at: receivedAt,
sent_at: timestamp,
quote,
contact: [],
preview,
profile: {
displayName: from,
},
},
};
receivedAt += 1; // Ensure different arrival times
// now process any user meta data updates
// - update their conversation with a potentially new avatar
return messageData;
})
);
this.conversation.setLastRetrievedMessage(this.lastGot);
// now process any user meta data updates if (!pendingMessages.length) {
// - update their conversation with a potentially new avatar return;
return messageData; }
}) if (pubKeys.length) {
// this will set slavePrimaryMap
const verifiedPrimaryPKs = await lokiFileServerAPI.verifyPrimaryPubKeys(
pubKeys
); );
this.conversation.setLastRetrievedMessage(this.lastGot); const { slavePrimaryMap } = this.serverAPI.chatAPI;
if (pendingMessages.length) { const slaveMessages = {};
// console.log('premultiDeviceResults', pubKeys); // sort pending messages
if (pubKeys.length) { pendingMessages.forEach(messageData => {
// this will set slavePrimaryMap // why am I getting these?
const verifiedPrimaryPKs = await lokiFileServerAPI.verifyPrimaryPubKeys( if (messageData === undefined) {
pubKeys log.warn('invalid pendingMessages');
); return;
const { slavePrimaryMap } = this.serverAPI.chatAPI; }
// if a known slave, queue
const slaveMessages = {}; if (slavePrimaryMap[messageData.source]) {
// sort pending messages // delay sending the message
pendingMessages.forEach(messageData => { if (slaveMessages[messageData.source] === undefined) {
// why am I getting these? slaveMessages[messageData.source] = [messageData];
if (messageData === undefined) { } else {
log.warn('invalid pendingMessages'); slaveMessages[messageData.source].push(messageData);
return;
}
// if a known slave, queue
if (slavePrimaryMap[messageData.source]) {
// delay sending the message
if (slaveMessages[messageData.source] === undefined) {
slaveMessages[messageData.source] = [messageData];
} else {
slaveMessages[messageData.source].push(messageData);
}
} else {
// no user or isPrimary means not multidevice, send event now
this.serverAPI.chatAPI.emit('publicMessage', {
message: messageData,
});
}
});
pendingMessages = []; // free memory
// build map of userProfileName to primaryKeys
// if we have verified primaryKeys/the claimed relation
if (verifiedPrimaryPKs.length) {
// get final list of verified chat server profile names
const verifiedDeviceResults = await this.serverAPI.getUsers(
verifiedPrimaryPKs
);
// console.log('verifiedDeviceResults', verifiedDeviceResults)
// go through verifiedDeviceResults
const newPrimaryUserProfileName = {};
verifiedDeviceResults.forEach(user => {
newPrimaryUserProfileName[user.username] = user.name;
});
// replace whole
this.primaryUserProfileName = newPrimaryUserProfileName;
} }
} else {
// process remaining messages // no user or isPrimary means not multidevice, send event now
const ourNumber = textsecure.storage.user.getNumber(); this.serverAPI.chatAPI.emit('publicMessage', {
Object.keys(slaveMessages).forEach(slaveKey => { message: messageData,
// prevent our own sent messages from coming back in
if (slaveKey === ourNumber) {
// we originally sent these
return;
}
const primaryPubKey = slavePrimaryMap[slaveKey];
slaveMessages[slaveKey].forEach(messageDataP => {
const messageData = messageDataP; // for linter
if (slavePrimaryMap[messageData.source]) {
// rewrite source, profile
messageData.source = primaryPubKey;
messageData.message.profile.displayName = this.primaryUserProfileName[
primaryPubKey
];
}
this.serverAPI.chatAPI.emit('publicMessage', {
message: messageData,
});
});
});
} // end if there are pending pubkeys to look up
// console.log('pendingMessages len', pendingMessages.length);
// console.log('pendingMessages', pendingMessages);
// find messages for original slave key using slavePrimaryMap
if (pendingMessages.length) {
const { slavePrimaryMap } = this.serverAPI.chatAPI;
const ourNumber = textsecure.storage.user.getNumber();
pendingMessages.forEach(messageDataP => {
const messageData = messageDataP; // for linter
// why am I getting these?
if (messageData === undefined) {
log.warn(`invalid pendingMessages ${pendingMessages}`);
return;
}
// prevent our own sent messages from coming back in
if (messageData.source === ourNumber) {
// we originally sent this
return;
}
if (slavePrimaryMap[messageData.source]) {
// rewrite source, profile
const primaryPubKey = slavePrimaryMap[messageData.source];
log.info(`Rewriting ${messageData.source} to ${primaryPubKey}`);
messageData.source = primaryPubKey;
messageData.message.profile.displayName = this.primaryUserProfileName[
primaryPubKey
];
}
this.serverAPI.chatAPI.emit('publicMessage', {
message: messageData,
});
}); });
} }
pendingMessages = []; });
pendingMessages = []; // free memory
// build map of userProfileName to primaryKeys
// if we have verified primaryKeys/the claimed relation
if (verifiedPrimaryPKs.length) {
// get final list of verified chat server profile names
const verifiedDeviceResults = await this.serverAPI.getUsers(
verifiedPrimaryPKs
);
// go through verifiedDeviceResults
const newPrimaryUserProfileName = {};
verifiedDeviceResults.forEach(user => {
newPrimaryUserProfileName[user.username] = user.name;
});
// replace whole
this.primaryUserProfileName = newPrimaryUserProfileName;
} }
// process remaining messages
const ourNumber = textsecure.storage.user.getNumber();
Object.keys(slaveMessages).forEach(slaveKey => {
// prevent our own sent messages from coming back in
if (slaveKey === ourNumber) {
// we originally sent these
return;
}
const primaryPubKey = slavePrimaryMap[slaveKey];
slaveMessages[slaveKey].forEach(messageDataP => {
const messageData = messageDataP; // for linter
if (slavePrimaryMap[messageData.source]) {
// rewrite source, profile
messageData.source = primaryPubKey;
messageData.message.profile.displayName = this.primaryUserProfileName[
primaryPubKey
];
}
this.serverAPI.chatAPI.emit('publicMessage', {
message: messageData,
});
});
});
} // end if there are pending pubkeys to look up
// find messages for original slave key using slavePrimaryMap
if (pendingMessages.length) {
const { slavePrimaryMap } = this.serverAPI.chatAPI;
const ourNumber = textsecure.storage.user.getNumber();
pendingMessages.forEach(messageDataP => {
const messageData = messageDataP; // for linter
// why am I getting these?
if (messageData === undefined) {
log.warn(`invalid pendingMessages ${pendingMessages}`);
return;
}
// prevent our own sent messages from coming back in
if (messageData.source === ourNumber) {
// we originally sent this
return;
}
if (slavePrimaryMap[messageData.source]) {
// rewrite source, profile
const primaryPubKey = slavePrimaryMap[messageData.source];
log.info(`Rewriting ${messageData.source} to ${primaryPubKey}`);
messageData.source = primaryPubKey;
messageData.message.profile.displayName = this.primaryUserProfileName[
primaryPubKey
];
}
this.serverAPI.chatAPI.emit('publicMessage', {
message: messageData,
});
});
} }
pendingMessages = [];
} }
static getPreviewFromAnnotation(annotation) { static getPreviewFromAnnotation(annotation) {

@ -64,7 +64,7 @@ class LokiFileServerAPI {
// go through each user and find deviceMap annotations // go through each user and find deviceMap annotations
const notFoundUsers = []; const notFoundUsers = [];
users.forEach(user => { await Promise.all(users.map(async user => {
let found = false; let found = false;
if (!user.annotations || !user.annotations.length) { if (!user.annotations || !user.annotations.length) {
log.info( log.info(
@ -72,67 +72,31 @@ class LokiFileServerAPI {
); );
return; return;
} }
user.annotations.forEach(note => { const mappingNote = user.annotations.find(note => note.type === DEVICE_MAPPING_ANNOTATION_KEY);
if (note.type !== 'network.loki.messenger.devicemapping') { const { authorisations } = mappingNote.value;
return; if (!Array.isArray(authorisations)) {
} return;
// isn't desired type }
// request is slave => primary type... await Promise.all(authorisations.map(async auth => {
if ( // only skip, if in secondary search mode
(isRequest && note.value.isPrimary !== '0') || if (isRequest && auth.secondaryDevicePubKey !== user.username) {
(!isRequest && note.value.isPrimary === '0') // this is not the authorization we're looking for
) { log.info(
/* log.info(`verifyUserObjectDeviceMap found wrong type of` + `Request and ${auth.secondaryDevicePubKey} != ${user.username}`
`relationship ${user.username}`); */ );
// console.log(`https://file.lokinet.org/users/@${user.username}?prettyPrint=1&include_annotations=1`);
return; return;
} }
const { authorisations } = note.value; const valid = await libloki.crypto.validateAuthorisation(auth);
if (!Array.isArray(authorisations)) { // log.info('auth is valid for', user.username)
return; if (iterator(user.username, auth)) {
found = true;
} }
authorisations.forEach(auth => { })); // end map authorisations
// log.info('devmap auth', auth);
// only skip, if in secondary search mode
if (isRequest && auth.secondaryDevicePubKey !== user.username) {
// this is not the authorization we're looking for
log.info(
`Request and ${auth.secondaryDevicePubKey} != ${user.username}`
);
return;
}
// log.info('auth', auth);
try {
// request (secondary wants to be paired with this primary)
// grant (primary approves this secondary)
window.libloki.crypto.verifyPairingSignature(
auth.primaryDevicePubKey,
auth.secondaryDevicePubKey,
dcodeIO.ByteBuffer.wrap(
isRequest ? auth.requestSignature : auth.grantSignature,
'base64'
).toArrayBuffer(),
isRequest
? textsecure.protobuf.PairingAuthorisationMessage.Type.REQUEST
: textsecure.protobuf.PairingAuthorisationMessage.Type.GRANT
);
// log.info('auth is valid for', user.username)
if (iterator(user.username, auth)) {
found = true;
}
} catch (e) {
log.warn(
`Invalid signature on pubkey ${user.username} authorization ${
auth.secondaryDevicePubKey
} isRequest ${isRequest}`
);
}
}); // end forEach authorisations
}); // end forEach annotations
if (!found) { if (!found) {
notFoundUsers.push(user.username); notFoundUsers.push(user.username);
} }
}); // end forEach users })); // end map users
// log.info('done with users', users.length); // log.info('done with users', users.length);
return notFoundUsers; return notFoundUsers;
} }

Loading…
Cancel
Save