2020-06-25 04:21:12 +02:00
|
|
|
/* global window, textsecure, libsession */
|
2020-05-15 04:53:49 +02:00
|
|
|
/* eslint-disable no-bitwise */
|
2019-01-09 23:59:45 +01:00
|
|
|
|
|
|
|
// eslint-disable-next-line func-names
|
2019-01-16 05:44:13 +01:00
|
|
|
(function() {
|
2019-01-09 23:59:45 +01:00
|
|
|
window.libloki = window.libloki || {};
|
|
|
|
|
2020-05-15 04:53:49 +02:00
|
|
|
const DebugFlagsEnum = {
|
|
|
|
GROUP_SYNC_MESSAGES: 1,
|
|
|
|
CONTACT_SYNC_MESSAGES: 2,
|
|
|
|
SESSION_REQUEST_MESSAGES: 8,
|
|
|
|
SESSION_MESSAGE_SENDING: 16,
|
|
|
|
SESSION_BACKGROUND_MESSAGE: 32,
|
|
|
|
GROUP_REQUEST_INFO: 64,
|
|
|
|
// If you add any new flag, be sure it is bitwise safe! (unique and 2 multiples)
|
|
|
|
ALL: 65535,
|
|
|
|
};
|
2020-05-19 03:08:38 +02:00
|
|
|
|
2020-05-15 04:53:49 +02:00
|
|
|
const debugFlags = DebugFlagsEnum.ALL;
|
|
|
|
|
|
|
|
const debugLogFn = (...args) => {
|
2020-06-03 07:35:11 +02:00
|
|
|
if (window.lokiFeatureFlags.debugMessageLogs) {
|
2020-05-15 04:53:49 +02:00
|
|
|
window.console.warn(...args);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
function logSessionMessageSending(...args) {
|
|
|
|
if (debugFlags & DebugFlagsEnum.SESSION_MESSAGE_SENDING) {
|
|
|
|
debugLogFn(...args);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
function logGroupSync(...args) {
|
|
|
|
if (debugFlags & DebugFlagsEnum.GROUP_SYNC_MESSAGES) {
|
|
|
|
debugLogFn(...args);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
function logGroupRequestInfo(...args) {
|
|
|
|
if (debugFlags & DebugFlagsEnum.GROUP_REQUEST_INFO) {
|
|
|
|
debugLogFn(...args);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
function logContactSync(...args) {
|
2020-06-03 02:01:42 +02:00
|
|
|
if (debugFlags & DebugFlagsEnum.CONTACT_SYNC_MESSAGES) {
|
2020-05-15 04:53:49 +02:00
|
|
|
debugLogFn(...args);
|
|
|
|
}
|
2020-05-19 03:08:38 +02:00
|
|
|
}
|
|
|
|
|
2020-05-15 04:53:49 +02:00
|
|
|
function logBackgroundMessage(...args) {
|
|
|
|
if (debugFlags & DebugFlagsEnum.SESSION_BACKGROUND_MESSAGE) {
|
|
|
|
debugLogFn(...args);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-06-24 09:54:56 +02:00
|
|
|
async function createContactSyncMessage(sessionContacts) {
|
2020-02-19 02:31:01 +01:00
|
|
|
if (sessionContacts.length === 0) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
2019-11-11 01:36:23 +01:00
|
|
|
const rawContacts = await Promise.all(
|
2020-02-10 05:20:34 +01:00
|
|
|
sessionContacts.map(async conversation => {
|
2019-11-11 01:36:23 +01:00
|
|
|
const profile = conversation.getLokiProfile();
|
|
|
|
const name = profile
|
|
|
|
? profile.displayName
|
|
|
|
: conversation.getProfileName();
|
|
|
|
const status = await conversation.safeGetVerified();
|
2020-06-25 04:21:12 +02:00
|
|
|
|
2019-11-11 01:36:23 +01:00
|
|
|
return {
|
|
|
|
name,
|
2020-06-25 04:21:12 +02:00
|
|
|
number: conversation.getNumber(),
|
2019-11-11 01:36:23 +01:00
|
|
|
nickname: conversation.getNickname(),
|
|
|
|
blocked: conversation.isBlocked(),
|
|
|
|
expireTimer: conversation.get('expireTimer'),
|
2020-06-25 04:21:12 +02:00
|
|
|
verifiedStatus: status,
|
2019-11-11 01:36:23 +01:00
|
|
|
};
|
|
|
|
})
|
|
|
|
);
|
2020-06-25 04:21:12 +02:00
|
|
|
|
2020-06-24 09:54:56 +02:00
|
|
|
return new libsession.Messages.Outgoing.ContactSyncMessage({
|
|
|
|
timestamp: Date.now(),
|
2020-06-25 04:21:12 +02:00
|
|
|
rawContacts,
|
2019-09-05 09:41:12 +02:00
|
|
|
});
|
|
|
|
}
|
2020-05-18 07:12:22 +02:00
|
|
|
|
2020-06-24 09:54:56 +02:00
|
|
|
function createGroupSyncMessage(sessionGroup) {
|
2020-05-15 04:53:49 +02:00
|
|
|
// We are getting a single open group here
|
2020-02-19 02:31:01 +01:00
|
|
|
|
2020-05-15 04:53:49 +02:00
|
|
|
const rawGroup = {
|
2020-06-25 04:21:12 +02:00
|
|
|
id: sessionGroup.id,
|
2020-05-15 04:53:49 +02:00
|
|
|
name: sessionGroup.get('name'),
|
|
|
|
members: sessionGroup.get('members') || [],
|
|
|
|
blocked: sessionGroup.isBlocked(),
|
|
|
|
expireTimer: sessionGroup.get('expireTimer'),
|
|
|
|
admins: sessionGroup.get('groupAdmins') || [],
|
|
|
|
};
|
2020-02-19 02:31:01 +01:00
|
|
|
|
2020-06-24 09:54:56 +02:00
|
|
|
return new libsession.Messages.Outgoing.ClosedGroupSyncMessage({
|
|
|
|
timestamp: Date.now(),
|
2020-06-25 04:21:12 +02:00
|
|
|
rawGroup,
|
2020-02-19 00:32:30 +01:00
|
|
|
});
|
|
|
|
}
|
2020-02-21 05:09:16 +01:00
|
|
|
|
2020-06-19 07:11:18 +02:00
|
|
|
async function sendSessionRequestsToMembers(members = []) {
|
|
|
|
// For every member, trigger a session request if needed
|
|
|
|
members.forEach(async memberStr => {
|
2020-05-15 04:53:49 +02:00
|
|
|
const ourPubKey = textsecure.storage.user.getNumber();
|
2020-06-19 07:11:18 +02:00
|
|
|
if (memberStr !== ourPubKey) {
|
|
|
|
const memberPubkey = new libsession.Types.PubKey(memberStr);
|
|
|
|
await libsession.Protocols.SessionProtocol.sendSessionRequestIfNeeded(
|
|
|
|
memberPubkey
|
2020-05-15 04:53:49 +02:00
|
|
|
);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
const debug = {
|
|
|
|
logContactSync,
|
|
|
|
logGroupSync,
|
|
|
|
logSessionMessageSending,
|
|
|
|
logBackgroundMessage,
|
|
|
|
logGroupRequestInfo,
|
|
|
|
};
|
|
|
|
|
2019-01-09 23:59:45 +01:00
|
|
|
window.libloki.api = {
|
2020-05-15 04:53:49 +02:00
|
|
|
sendSessionRequestsToMembers,
|
2020-06-24 09:54:56 +02:00
|
|
|
createContactSyncMessage,
|
|
|
|
createGroupSyncMessage,
|
2020-05-15 04:53:49 +02:00
|
|
|
debug,
|
2019-01-09 23:59:45 +01:00
|
|
|
};
|
|
|
|
})();
|