2019-01-23 05:57:56 +01:00
|
|
|
/* global window, textsecure, log */
|
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 || {};
|
|
|
|
|
|
|
|
async function sendFriendRequestAccepted(pubKey) {
|
2019-01-23 05:57:56 +01:00
|
|
|
return sendEmptyMessage(pubKey, true);
|
2019-01-09 23:59:45 +01:00
|
|
|
}
|
|
|
|
|
2019-01-23 06:44:03 +01:00
|
|
|
async function broadcastOnlineStatus() {
|
2019-01-25 04:56:54 +01:00
|
|
|
const friendKeys = await window.Signal.Data.getPubKeysWithFriendStatus(
|
|
|
|
friendRequestStatusEnum.friends
|
|
|
|
);
|
2019-01-23 06:44:03 +01:00
|
|
|
friendKeys.forEach(pubKey => {
|
2019-01-25 04:56:54 +01:00
|
|
|
sendOnlineBroadcastMessage(pubKey);
|
2019-01-23 06:44:03 +01:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
async function sendOnlineBroadcastMessage(pubKey) {
|
2019-01-30 04:23:36 +01:00
|
|
|
// TODO: Make this actually get a loki address rather than junk string
|
2019-01-25 05:13:41 +01:00
|
|
|
const lokiAddressMessage = new textsecure.protobuf.LokiAddressMessage({
|
|
|
|
p2pAddress: 'testAddress',
|
|
|
|
p2pPort: parseInt(window.localServerPort, 10),
|
|
|
|
});
|
2019-01-23 06:44:03 +01:00
|
|
|
const content = new textsecure.protobuf.Content({
|
2019-01-25 05:13:41 +01:00
|
|
|
lokiAddressMessage,
|
2019-01-23 06:44:03 +01:00
|
|
|
});
|
|
|
|
|
|
|
|
// 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);
|
|
|
|
}
|
|
|
|
|
2019-01-23 05:57:56 +01:00
|
|
|
async function sendEmptyMessage(pubKey, sendContentMessage = false) {
|
2019-01-09 23:59:45 +01:00
|
|
|
const options = {};
|
2019-01-23 04:58:28 +01:00
|
|
|
// 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
|
|
|
|
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);
|
|
|
|
}
|
2019-01-09 23:59:45 +01:00
|
|
|
}
|
|
|
|
|
2019-01-25 04:56:54 +01:00
|
|
|
// Possible conversation friend states
|
|
|
|
const friendRequestStatusEnum = Object.freeze({
|
|
|
|
// New conversation, no messages sent or received
|
|
|
|
none: 0,
|
|
|
|
// This state is used to lock the input early while sending
|
|
|
|
pendingSend: 1,
|
|
|
|
// Friend request sent, awaiting response
|
|
|
|
requestSent: 2,
|
|
|
|
// Friend request received, awaiting user input
|
|
|
|
requestReceived: 3,
|
|
|
|
// We did it!
|
|
|
|
friends: 4,
|
|
|
|
});
|
|
|
|
|
2019-01-09 23:59:45 +01:00
|
|
|
window.libloki.api = {
|
|
|
|
sendFriendRequestAccepted,
|
|
|
|
sendEmptyMessage,
|
2019-01-23 06:44:03 +01:00
|
|
|
sendOnlineBroadcastMessage,
|
|
|
|
broadcastOnlineStatus,
|
2019-01-09 23:59:45 +01:00
|
|
|
};
|
2019-01-25 04:56:54 +01:00
|
|
|
|
|
|
|
window.libloki.friends = {
|
|
|
|
friendRequestStatusEnum,
|
|
|
|
};
|
2019-01-09 23:59:45 +01:00
|
|
|
})();
|