Made changes for new protocol

pull/1135/head
Mikunj 5 years ago
parent d2aeb545c9
commit a497871ea3

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

@ -2045,23 +2045,8 @@
}
if (message.isFriendRequest() && isSessionRequest) {
// Check if the contact is a member in one of our private groups:
const groupMember = window
.getConversations()
.models.filter(c => c.get('members'))
.reduce((acc, x) => window.Lodash.concat(acc, x.get('members')), [])
.includes(primarySource);
if (groupMember) {
window.log.info(
`Auto accepting a 'group' friend request for a known group member: ${primarySource}`
);
window.libloki.api.sendBackgroundMessage(message.get('source'));
confirm();
}
window.libloki.api.sendSessionEstablishedMessage(message.get('source'));
confirm();
// Wether or not we accepted the FR, we exit early so background friend requests
// cannot be used for establishing regular private conversations
return null;

@ -4,6 +4,26 @@
(function() {
window.libloki = window.libloki || {};
async function sendSessionEstablishedMessage(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' };
const outgoingMessage = new textsecure.OutgoingMessage(
null, // server
Date.now(), // timestamp,
[pubKey], // numbers
content, // message
true, // silent
() => null, // callback
options
);
await outgoingMessage.sendToNumber(pubKey);
}
async function sendBackgroundMessage(pubKey) {
return sendOnlineBroadcastMessage(pubKey);
}
@ -273,6 +293,7 @@
}
window.libloki.api = {
sendSessionEstablishedMessage,
sendBackgroundMessage,
sendOnlineBroadcastMessage,
sendPairingAuthorisation,

@ -644,29 +644,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:',
@ -1313,13 +1290,11 @@ MessageReceiver.prototype.extend({
message.group &&
message.group.type === textsecure.protobuf.GroupContext.Type.QUIT
);
const friendRequest =
envelope.type === textsecure.protobuf.Envelope.Type.FRIEND_REQUEST;
const { UNPAIRING_REQUEST } = textsecure.protobuf.DataMessage.Flags;
// eslint-disable-next-line no-bitwise
const isUnpairingRequest = Boolean(message.flags & UNPAIRING_REQUEST);
if (!friendRequest && isUnpairingRequest) {
if (isUnpairingRequest) {
// TODO: move high-level pairing logic to libloki.multidevice.xx
const unpairingRequestIsLegit = async () => {
@ -1394,7 +1369,7 @@ MessageReceiver.prototype.extend({
// if we're not friends with the current user that sent this private message
// Check to see if we need to auto accept their friend request
const isGroupMessage = !!groupId;
if (friendRequest || (!isGroupMessage && !conversation.isFriend())) {
if (!isGroupMessage && !conversation.isFriend()) {
if (isMe) {
window.log.info('refusing to add a friend request to ourselves');
throw new Error('Cannot add a friend request for ourselves!');
@ -1429,16 +1404,27 @@ MessageReceiver.prototype.extend({
);
return this.removeFromCache(envelope);
}
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
const friendRequestStatus = conversation.get('friendRequestStatus');
const FriendRequestStatusEnum = window.friends.friendRequestStatusEnum;
const isFriendRequestStatusNone =
friendRequestStatus === FriendRequestStatusEnum.none ||
friendRequestStatus === FriendRequestStatusEnum.requestExpired;
const isFriendRequest =
!isGroupMessage && isFriendRequestStatusNone && !_.isEmpty(message.body);
const ev = new Event('message');
ev.confirm = this.removeFromCache.bind(this, envelope);
ev.data = {
friendRequest,
friendRequest: isFriendRequest,
source: envelope.source,
sourceDevice: envelope.sourceDevice,
timestamp: envelope.timestamp.toNumber(),
@ -1493,6 +1479,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);
@ -1503,6 +1514,8 @@ MessageReceiver.prototype.extend({
);
}
this.handleFriendRequestAcceptIfNeeded(envelope, content);
if (content.lokiAddressMessage) {
return this.handleLokiAddressMessage(
envelope,
@ -1606,6 +1619,13 @@ 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);
window.libloki.api.sendSessionEstablishedMessage(envelope.source);
}
window.log.info('null message from', this.getEnvelopeId(envelope));
this.removeFromCache(envelope);
},

@ -1096,6 +1096,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.
@ -1104,7 +1112,7 @@ MessageSender.prototype = {
proto,
timestamp,
silent,
options
msgOptions
).catch(logError('resetSession/sendToContact error:'));
},

Loading…
Cancel
Save