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

pull/157/head
Beaudan 6 years ago
parent b417edfc9b
commit 07076c27ae

@ -90,7 +90,7 @@ module.exports = {
updateConversation, updateConversation,
removeConversation, removeConversation,
getAllConversations, getAllConversations,
getAllFriendIds, getPubKeysWithFriendStatus,
getAllConversationIds, getAllConversationIds,
getAllPrivateConversations, getAllPrivateConversations,
getAllGroupsInvolvingId, getAllGroupsInvolvingId,
@ -1281,10 +1281,15 @@ async function getAllConversations() {
return map(rows, row => jsonToObject(row.json)); 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) // TODO: Maybe don't have this hardcoded to 4 (friends status in the enum)
const rows = await db.all( 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); return map(rows, row => row.id);
} }

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

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

@ -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,
};

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

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

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

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

Loading…
Cancel
Save