session-desktop/libloki/api.js

133 lines
3.4 KiB
JavaScript
Raw Normal View History

/* global window, textsecure, libsession */
2020-05-15 04:53:49 +02:00
/* eslint-disable no-bitwise */
// eslint-disable-next-line func-names
2019-01-16 05:44:13 +01:00
(function() {
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) => {
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) {
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);
}
}
async function createContactSyncMessage(sessionContacts) {
if (sessionContacts.length === 0) {
return null;
}
const rawContacts = await Promise.all(
sessionContacts.map(async conversation => {
const profile = conversation.getLokiProfile();
const name = profile
? profile.displayName
: conversation.getProfileName();
const status = await conversation.safeGetVerified();
return {
name,
number: conversation.getNumber(),
nickname: conversation.getNickname(),
blocked: conversation.isBlocked(),
expireTimer: conversation.get('expireTimer'),
verifiedStatus: status,
};
})
);
return new libsession.Messages.Outgoing.ContactSyncMessage({
timestamp: Date.now(),
rawContacts,
});
}
2020-05-18 07:12:22 +02:00
function createGroupSyncMessage(sessionGroup) {
2020-05-15 04:53:49 +02:00
// We are getting a single open group here
2020-05-15 04:53:49 +02:00
const rawGroup = {
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') || [],
};
return new libsession.Messages.Outgoing.ClosedGroupSyncMessage({
timestamp: Date.now(),
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,
};
window.libloki.api = {
2020-05-15 04:53:49 +02:00
sendSessionRequestsToMembers,
createContactSyncMessage,
createGroupSyncMessage,
2020-05-15 04:53:49 +02:00
debug,
};
})();