Merge pull request #1135 from Mikunj/protocol-bridge

Made changes for new protocol backward compatibility
pull/1146/head
Mikunj Varsani 5 years ago committed by GitHub
commit c7b76dfebb
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

@ -802,8 +802,7 @@
const status = this.get('friendRequestStatus');
return (
status === FriendRequestStatusEnum.pendingSend ||
status === FriendRequestStatusEnum.requestSent ||
status === FriendRequestStatusEnum.requestExpired
status === FriendRequestStatusEnum.requestSent
);
},
hasReceivedFriendRequest() {

@ -1941,7 +1941,7 @@
const { primaryDevicePubKey } = authorisation;
// ensure the primary device is a friend
const c = window.ConversationController.get(primaryDevicePubKey);
if (!c || !c.isFriendWithAnyDevice()) {
if (!c || !await c.isFriendWithAnyDevice()) {
return false;
}
await libloki.storage.savePairingAuthorisation(authorisation);
@ -2048,31 +2048,9 @@
}
return false;
},
async handleSessionRequest(source, primarySource, confirm) {
// Check if the contact is a member in one of our private groups:
const isKnownClosedGroupMember = window
.getConversations()
.models.filter(c => c.get('members'))
.reduce((acc, x) => window.Lodash.concat(acc, x.get('members')), [])
.includes(primarySource);
libloki.api.debug.logSessionRequest(
`Received SESSION_REQUEST from source: ${source}, primarySource: ${primarySource}, is one of our private groups: ${isKnownClosedGroupMember}`
);
if (isKnownClosedGroupMember) {
window.log.info(
`Auto accepting a 'group' session request for a known group member: ${primarySource}`
);
window.libloki.api.sendBackgroundMessage(
source,
window.textsecure.OutgoingMessage.DebugMessageType
.SESSION_REQUEST_ACCEPT
);
confirm();
}
async handleSessionRequest(source, confirm) {
window.libloki.api.sendSessionEstablishedMessage(source);
confirm();
},
isGroupBlocked(groupId) {
return textsecure.storage.get('blocked-groups', []).indexOf(groupId) >= 0;
@ -2196,7 +2174,7 @@
message.isFriendRequest() &&
!!(initialMessage.flags & sessionRequestFlag)
) {
await this.handleSessionRequest(source, primarySource, confirm);
await this.handleSessionRequest(source, confirm);
// Wether or not we accepted the FR, we exit early so session requests
// cannot be used for establishing regular private conversations

@ -84,6 +84,15 @@
return authorisation ? authorisation.primaryDevicePubKey : pubKey;
}
async function sendSessionEstablishedMessage(pubKey) {
// This message shouldn't be routed through multi-device.
// It needs to go directly to the pubKey specified.
const message = textsecure.OutgoingMessage.buildSessionEstablishedMessage(
pubKey
);
await message.sendToNumber(pubKey);
}
async function sendBackgroundMessage(pubKey, debugMessageType) {
const primaryPubKey = await getPrimaryDevicePubkey(pubKey);
if (primaryPubKey !== pubKey) {
@ -327,6 +336,7 @@
};
window.libloki.api = {
sendSessionEstablishedMessage,
sendBackgroundMessage,
sendAutoFriendRequestMessage,
sendSessionRequestsToMembers,

@ -660,29 +660,6 @@ MessageReceiver.prototype.extend({
return null;
}
let conversation;
try {
conversation = await window.ConversationController.getOrCreateAndWait(
envelope.source,
'private'
);
} catch (e) {
window.log.info('Error getting conversation: ', envelope.source);
}
// Type here can actually be UNIDENTIFIED_SENDER even if
// the underlying message is FRIEND_REQUEST
if (envelope.type !== textsecure.protobuf.Envelope.Type.FRIEND_REQUEST) {
// If we got here there is a valid session, which meants friend request
// is complete (if it wasn't already)
if (conversation) {
const isFriendRequestAccept = await conversation.onFriendRequestAccepted();
if (isFriendRequestAccept) {
await conversation.notifyFriendRequest(envelope.source, 'accepted');
}
}
}
this.updateCache(envelope, plaintext).catch(error => {
window.log.error(
'decrypt failed to save decrypted message contents to cache:',
@ -1298,8 +1275,6 @@ MessageReceiver.prototype.extend({
const { UNPAIRING_REQUEST } = textsecure.protobuf.DataMessage.Flags;
const friendRequest =
envelope.type === textsecure.protobuf.Envelope.Type.FRIEND_REQUEST;
// eslint-disable-next-line no-bitwise
const isUnpairingRequest = Boolean(message.flags & UNPAIRING_REQUEST);
@ -1315,17 +1290,29 @@ MessageReceiver.prototype.extend({
message.profileKey
);
}
if (!friendRequest && this.isMessageEmpty(message)) {
if (this.isMessageEmpty(message)) {
window.log.warn(
`Message ${this.getEnvelopeId(envelope)} ignored; it was empty`
);
return this.removeFromCache(envelope);
}
// Loki - Temp hack until new protocol
// A friend request is a non-group text message which we haven't processed yet
const isGroupMessage = Boolean(message.group || message.mediumGroupUpdate);
const friendRequestStatusNoneOrExpired = conversation
? conversation.isFriendRequestStatusNoneOrExpired()
: true;
const isFriendRequest =
!isGroupMessage &&
!_.isEmpty(message.body) &&
friendRequestStatusNoneOrExpired;
// Build a 'message' event i.e. a received message event
const ev = new Event('message');
ev.confirm = this.removeFromCache.bind(this, envelope);
ev.data = {
friendRequest,
friendRequest: isFriendRequest,
source: senderPubKey,
sourceDevice: envelope.sourceDevice,
timestamp: envelope.timestamp.toNumber(),
@ -1380,6 +1367,31 @@ MessageReceiver.prototype.extend({
}
return this.innerHandleContentMessage(envelope, plaintext);
},
async handleFriendRequestAcceptIfNeeded(envelope, content) {
const isGroupMessage =
content &&
content.dataMessage &&
(content.dataMessage.group || content.dataMessage.mediumGroupUpdate);
const isReceiptMessage = content && content.receiptMessage;
const isTypingMessage = content && content.typingMessage;
if (isGroupMessage || isReceiptMessage || isTypingMessage) {
return;
}
// If we sent a friend request and got another message back then we should become friends
try {
const conversation = await window.ConversationController.getOrCreateAndWait(
envelope.source,
'private'
);
const isFriendRequestAccept = await conversation.onFriendRequestAccepted();
if (isFriendRequestAccept) {
await conversation.notifyFriendRequest(envelope.source, 'accepted');
}
} catch (e) {
window.log.info('Error getting conversation: ', envelope.source);
}
},
async innerHandleContentMessage(envelope, plaintext) {
const content = textsecure.protobuf.Content.decode(plaintext);
@ -1390,6 +1402,8 @@ MessageReceiver.prototype.extend({
);
}
this.handleFriendRequestAcceptIfNeeded(envelope, content);
if (content.lokiAddressMessage) {
return this.handleLokiAddressMessage(
envelope,
@ -1493,6 +1507,14 @@ MessageReceiver.prototype.extend({
return this.dispatchEvent(ev);
},
handleNullMessage(envelope) {
// Loki - Temp hack for new protocl backward compatibility
// This should be removed once we add the new protocol
if (envelope.type === textsecure.protobuf.Envelope.Type.FRIEND_REQUEST) {
window.log.info('sent session established to', envelope.source);
// We don't need to await the call below because we just want to send it off
window.libloki.api.sendSessionEstablishedMessage(envelope.source);
}
window.log.info('null message from', this.getEnvelopeId(envelope));
this.removeFromCache(envelope);
},

@ -791,6 +791,25 @@ OutgoingMessage.buildSessionRequestMessage = function buildSessionRequestMessage
);
};
OutgoingMessage.buildSessionEstablishedMessage = pubKey => {
const nullMessage = new textsecure.protobuf.NullMessage();
const content = new textsecure.protobuf.Content({
nullMessage,
});
// The below message type will ignore auto FR
const options = { messageType: 'onlineBroadcast' };
return new textsecure.OutgoingMessage(
null, // server
Date.now(), // timestamp,
[pubKey], // numbers
content, // message
true, // silent
() => null, // callback
options
);
};
OutgoingMessage.buildBackgroundMessage = function buildBackgroundMessage(
pubKey,
debugMessageType

@ -1122,6 +1122,14 @@ MessageSender.prototype = {
window.log.error(prefix, error && error.stack ? error.stack : error);
throw error;
};
// Loki - Temp hack for new protocol
// A session reset should be a `FRIEND_REQUEST`
const msgOptions = {
messageType: 'friend-request',
...options,
};
// The actual deletion of the session now happens later
// as we need to ensure the other contact has successfully
// switch to a new session first.
@ -1130,7 +1138,7 @@ MessageSender.prototype = {
proto,
timestamp,
silent,
options
msgOptions
).catch(logError('resetSession/sendToContact error:'));
},

Loading…
Cancel
Save