session-desktop/libloki/api.js

107 lines
3.2 KiB
JavaScript
Raw Normal View History

2019-01-23 05:57:56 +01:00
/* global window, textsecure, log */
// eslint-disable-next-line func-names
2019-01-16 05:44:13 +01:00
(function() {
window.libloki = window.libloki || {};
async function sendFriendRequestAccepted(pubKey) {
2019-01-23 05:57:56 +01:00
return sendEmptyMessage(pubKey, true);
}
async function broadcastOnlineStatus() {
const friendKeys = await window.Signal.Data.getPubKeysWithFriendStatus(
window.friends.friendRequestStatusEnum.friends
);
await Promise.all(
friendKeys.map(async pubKey => {
if (pubKey === textsecure.storage.user.getNumber())
return
try {
await sendOnlineBroadcastMessage(pubKey);
} catch (e) {
log.warn(`Failed to send online broadcast message to ${pubKey}`);
}
})
);
}
async function sendOnlineBroadcastMessage(pubKey, isPing = false) {
if (!window.localLokiServer.isListening())
return;
// clearnet change: getMyLokiAddress -> getMyClearIP
// const myLokiAddress = await window.lokiSnodeAPI.getMyLokiAddress();
const myLokiAddress = await window.lokiSnodeAPI.getMyClearIp();
const lokiAddressMessage = new textsecure.protobuf.LokiAddressMessage({
// clearnet change: http -> https
p2pAddress: `https://${myLokiAddress}`,
p2pPort: window.localLokiServer.getPublicPort(),
});
const content = new textsecure.protobuf.Content({
lokiAddressMessage,
});
const options = { messageType: 'onlineBroadcast', isPing };
// Send a empty message with information about how to contact us directly
const outgoingMessage = new textsecure.OutgoingMessage(
null, // server
Date.now(), // timestamp,
[pubKey], // numbers
content, // message
true, // silent
() => null, // callback
options
);
await outgoingMessage.sendToNumber(pubKey);
}
2019-01-23 05:57:56 +01:00
async function sendEmptyMessage(pubKey, sendContentMessage = false) {
const options = {};
// send an empty message.
2019-01-23 05:57:56 +01:00
if (sendContentMessage) {
// The logic downstream will attach the prekeys and our profile.
await textsecure.messaging.sendMessageToNumber(
pubKey, // number
null, // messageText
[], // attachments
null, // quote
[], // preview
2019-01-23 05:57:56 +01:00
Date.now(), // timestamp
null, // expireTimer
null, // profileKey
options
);
} else {
// empty content message
const content = new textsecure.protobuf.Content();
// will be called once the transmission succeeded or failed
const callback = res => {
if (res.errors.length > 0) {
res.errors.forEach(error => log.error(error));
} else {
log.info('empty message sent successfully');
}
};
// send an empty message. The logic in ougoing_message will attach the prekeys.
const outgoingMessage = new textsecure.OutgoingMessage(
null, // server
Date.now(), // timestamp,
[pubKey], // numbers
content, // message
true, // silent
callback, // callback
options
);
await outgoingMessage.sendToNumber(pubKey);
}
}
window.libloki.api = {
sendFriendRequestAccepted,
sendEmptyMessage,
sendOnlineBroadcastMessage,
broadcastOnlineStatus,
};
})();