You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
session-desktop/libloki/api.js

98 lines
2.9 KiB
JavaScript

/* global window, textsecure, log */
// eslint-disable-next-line func-names
6 years ago
(function() {
window.libloki = window.libloki || {};
async function sendFriendRequestAccepted(pubKey) {
return sendEmptyMessage(pubKey, true);
}
async function broadcastOnlineStatus() {
const friendKeys = await window.Signal.Data.getAllFriendIds();
friendKeys.forEach(pubKey => {
sendOnlineBroadcastMessage(pubKey)
});
}
async function sendOnlineBroadcastMessage(pubKey) {
const onlineBroadcastMessage = new textsecure.protobuf.OnlineBroadcastMessage({
snappAddress: 'testAddress',
port: parseInt(window.localServerPort, 10),
timestamp: Date.now(),
});
const content = new textsecure.protobuf.Content({
onlineBroadcastMessage,
});
// 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('Online broadcast message sent successfully');
}
};
const options = { messageType: 'onlineBroadcast' };
// 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
callback, // callback
options
);
await outgoingMessage.sendToNumber(pubKey);
}
async function sendEmptyMessage(pubKey, sendContentMessage = false) {
const options = {};
// send an empty message.
if (sendContentMessage) {
// The logic downstream will attach the prekeys and our profile.
await textsecure.messaging.sendMessageToNumber(
pubKey, // number
null, // messageText
[], // attachments
null, // quote
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,
};
})();