Moved friend status to window, added start of p2p api stuff

This commit is contained in:
Beaudan 2019-01-25 14:56:54 +11:00
parent b417edfc9b
commit 07076c27ae
8 changed files with 103 additions and 42 deletions

View File

@ -90,7 +90,7 @@ module.exports = {
updateConversation,
removeConversation,
getAllConversations,
getAllFriendIds,
getPubKeysWithFriendStatus,
getAllConversationIds,
getAllPrivateConversations,
getAllGroupsInvolvingId,
@ -1281,10 +1281,15 @@ async function getAllConversations() {
return map(rows, row => jsonToObject(row.json));
}
async function getAllFriendIds() {
async function getPubKeysWithFriendStatus(status) {
// TODO: Maybe don't have this hardcoded to 4 (friends status in the enum)
const rows = await db.all(
'SELECT id FROM conversations WHERE friendRequestStatus = 4 ORDER BY id ASC;'
`SELECT id FROM conversations WHERE
friendRequestStatus = $status
ORDER BY id ASC;`,
{
$status: status,
}
);
return map(rows, row => row.id);
}

View File

@ -41,18 +41,8 @@
} = window.Signal.Migrations;
// 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,
});
const FriendRequestStatusEnum =
window.libloki.friends.friendRequestStatusEnum;
// Possible session reset states
const SessionResetEnum = Object.freeze({

View File

@ -120,7 +120,7 @@ module.exports = {
_removeConversations,
getAllConversations,
getAllFriendIds,
getPubKeysWithFriendStatus,
getAllConversationIds,
getAllPrivateConversations,
getAllGroupsInvolvingId,
@ -722,12 +722,8 @@ async function _removeConversations(ids) {
await channels.removeConversation(ids);
}
async function getAllFriendIds() {
const ids = (await channels.getAllFriendIds()).map(c =>
setifyProperty(c, 'swarmNodes')
);
return ids;
async function getPubKeysWithFriendStatus(status) {
return channels.getPubKeysWithFriendStatus(status);
}
async function getAllConversations({ ConversationCollection }) {

View File

@ -0,0 +1,32 @@
// const fetch = require('node-fetch');
class LokiP2pAPI {
constructor() {
this.contactP2pDetails = {};
}
addContactP2pDetails(pubKey, address, port) {
this.contactP2pDetails[pubKey] = {
address,
port,
};
}
getContactP2pDetails(pubKey) {
if (this.contactP2pDetails[pubKey]) {
return this.contactP2pDetails[pubKey];
}
return null;
}
removeContactP2pDetails(pubKey, address, port) {
this.contactP2pDetails[pubKey] = {
address,
port,
};
}
}
module.exports = {
LokiP2pAPI,
};

View File

@ -9,18 +9,21 @@
}
async function broadcastOnlineStatus() {
const friendKeys = await window.Signal.Data.getAllFriendIds();
const friendKeys = await window.Signal.Data.getPubKeysWithFriendStatus(
friendRequestStatusEnum.friends
);
friendKeys.forEach(pubKey => {
sendOnlineBroadcastMessage(pubKey)
sendOnlineBroadcastMessage(pubKey);
});
}
async function sendOnlineBroadcastMessage(pubKey) {
const onlineBroadcastMessage = new textsecure.protobuf.OnlineBroadcastMessage({
snappAddress: 'testAddress',
port: parseInt(window.localServerPort, 10),
timestamp: Date.now(),
});
const onlineBroadcastMessage = new textsecure.protobuf.OnlineBroadcastMessage(
{
p2pAddress: 'testAddress',
p2pPort: parseInt(window.localServerPort, 10),
}
);
const content = new textsecure.protobuf.Content({
onlineBroadcastMessage,
});
@ -88,10 +91,28 @@
}
}
// 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,
});
window.libloki.api = {
sendFriendRequestAccepted,
sendEmptyMessage,
sendOnlineBroadcastMessage,
broadcastOnlineStatus,
};
window.libloki.friends = {
friendRequestStatusEnum,
};
})();

View File

@ -82,13 +82,11 @@ MessageReceiver.prototype.extend({
}
});
this.localServer
.start(window.localServerPort)
.then(port => {
window.log.info(`Local Server started at localhost:${port}`);
window.libloki.api.broadcastOnlineStatus();
this.localServer.on('message', this.httpPollingResource.handleMessage);
});
this.localServer.start(window.localServerPort).then(port => {
window.log.info(`Local Server started at localhost:${port}`);
window.libloki.api.broadcastOnlineStatus();
this.localServer.on('message', this.httpPollingResource.handleMessage);
});
// TODO: Rework this socket stuff to work with online messaging
const useWebSocket = false;
@ -133,7 +131,10 @@ MessageReceiver.prototype.extend({
}
if (this.localServer) {
this.localServer.removeListener('message', this.httpPollingResource.handleMessage);
this.localServer.removeListener(
'message',
this.httpPollingResource.handleMessage
);
this.localServer = null;
}
},
@ -712,7 +713,10 @@ MessageReceiver.prototype.extend({
.then(handleSessionReset);
break;
case textsecure.protobuf.Envelope.Type.ONLINE_BROADCAST:
window.log.info('Online broadcast message from', this.getEnvelopeId(envelope));
window.log.info(
'Online broadcast message from',
this.getEnvelopeId(envelope)
);
promise = captureActiveSession()
.then(() => sessionCipher.decryptWhisperMessage(ciphertext))
.then(this.unpad)
@ -904,7 +908,13 @@ MessageReceiver.prototype.extend({
})
);
},
handleOnlineBroadcastMessage(envelope, onlineBroadcastMessage) {
async handleOnlineBroadcastMessage(envelope, onlineBroadcastMessage) {
const { p2pAddress, p2pPort } = onlineBroadcastMessage;
window.LokiP2pAPI.addContactP2pDetails(
envelope.source,
p2pAddress,
p2pPort
);
return this.removeFromCache(envelope);
},
handleDataMessage(envelope, msg) {
@ -1022,7 +1032,10 @@ MessageReceiver.prototype.extend({
content.preKeyBundleMessage
);
if (content.onlineBroadcastMessage)
return this.handleOnlineBroadcastMessage(envelope, content.onlineBroadcastMessage);
return this.handleOnlineBroadcastMessage(
envelope,
content.onlineBroadcastMessage
);
if (content.syncMessage)
return this.handleSyncMessage(envelope, content.syncMessage);
if (content.dataMessage)

View File

@ -276,6 +276,10 @@ window.LokiSnodeAPI = new LokiSnodeAPI({
swarmServerPort: config.swarmServerPort,
});
const { LokiP2pAPI } = require('./js/modules/loki_p2p_api');
window.LokiP2pAPI = new LokiP2pAPI();
const { LokiMessageAPI } = require('./js/modules/loki_message_api');
window.LokiMessageAPI = new LokiMessageAPI({

View File

@ -40,8 +40,8 @@ message Content {
}
message OnlineBroadcastMessage {
optional string snappAddress = 1;
optional uint32 port = 2;
optional string p2pAddress = 1;
optional uint32 p2pPort = 2;
}
message PreKeyBundleMessage {