Hooked up LokiP2pAPI with online stuff.

pull/165/head
Mikunj 6 years ago
parent 9b382de6da
commit f4e9bc655a

@ -1,4 +1,4 @@
/* global _, Whisper, Backbone, storage */ /* global _, Whisper, Backbone, storage, lokiP2pAPI */
/* eslint-disable more/no-then */ /* eslint-disable more/no-then */
@ -248,6 +248,13 @@
async load() { async load() {
window.log.info('ConversationController: starting initial fetch'); window.log.info('ConversationController: starting initial fetch');
// We setup online and offline listeners here because we want
// to minimize the amount of listeners we have to avoid memory leaks
if (!this.p2pListenersSet) {
lokiP2pAPI.on('online', this._handleOnline.bind(this));
lokiP2pAPI.on('offline', this._handleOffline.bind(this));
}
if (conversations.length) { if (conversations.length) {
throw new Error('ConversationController: Already loaded!'); throw new Error('ConversationController: Already loaded!');
} }
@ -263,12 +270,12 @@
this._initialFetchComplete = true; this._initialFetchComplete = true;
const promises = []; const promises = [];
conversations.forEach(conversation => { conversations.forEach(conversation => {
// TODO This needs to be synchronous (one after the other)
promises.concat([ promises.concat([
conversation.updateLastMessage(), conversation.updateLastMessage(),
conversation.updateProfile(), conversation.updateProfile(),
conversation.updateProfileAvatar(), conversation.updateProfileAvatar(),
conversation.resetPendingSend(), conversation.resetPendingSend(),
conversation.updateProfile(),
]); ]);
}); });
await Promise.all(promises); await Promise.all(promises);
@ -292,5 +299,17 @@
return this._initialPromise; return this._initialPromise;
}, },
_handleOnline(pubKey) {
try {
const conversation = this.get(pubKey);
conversation.set({ isOnline: true });
} catch (e) {} // eslint-disable-line
},
_handleOffline(pubKey) {
try {
const conversation = this.get(pubKey);
conversation.set({ isOnline: false });
} catch (e) {} // eslint-disable-line
},
}; };
})(); })();

@ -156,6 +156,9 @@
this.setFriendRequestExpiryTimeout(); this.setFriendRequestExpiryTimeout();
this.typingRefreshTimer = null; this.typingRefreshTimer = null;
this.typingPauseTimer = null; this.typingPauseTimer = null;
// Online status handling
this.set({ isOnline: lokiP2pAPI.isOnline(this.id) });
}, },
isMe() { isMe() {
@ -253,13 +256,6 @@
); );
}, },
async setIsOnline(online) {
this.set({ isOnline: online });
await window.Signal.Data.updateConversation(this.id, this.attributes, {
Conversation: Whisper.Conversation,
});
},
async cleanup() { async cleanup() {
await window.Signal.Types.Conversation.deleteExternalFiles( await window.Signal.Types.Conversation.deleteExternalFiles(
this.attributes, this.attributes,

@ -11,6 +11,7 @@ const {
map, map,
merge, merge,
set, set,
omit,
} = require('lodash'); } = require('lodash');
const { base64ToArrayBuffer, arrayBufferToBase64 } = require('./crypto'); const { base64ToArrayBuffer, arrayBufferToBase64 } = require('./crypto');
@ -688,11 +689,13 @@ async function getConversationCount() {
} }
async function saveConversation(data) { async function saveConversation(data) {
await channels.saveConversation(data); const cleaned = omit(data, 'isOnine');
await channels.saveConversation(cleaned);
} }
async function saveConversations(data) { async function saveConversations(data) {
await channels.saveConversations(data); const cleaned = data.map(d => omit(d, 'isOnline'));
await channels.saveConversations(cleaned);
} }
async function getConversationById(id, { Conversation }) { async function getConversationById(id, { Conversation }) {
@ -712,7 +715,10 @@ async function updateConversation(id, data, { Conversation }) {
if (merged.swarmNodes instanceof Set) { if (merged.swarmNodes instanceof Set) {
merged.swarmNodes = Array.from(merged.swarmNodes); merged.swarmNodes = Array.from(merged.swarmNodes);
} }
await channels.updateConversation(merged);
// Don't save the online status of the object
const cleaned = omit(merged, 'isOnline');
await channels.updateConversation(cleaned);
} }
async function removeConversation(id, { Conversation }) { async function removeConversation(id, { Conversation }) {

@ -66,6 +66,10 @@ class LokiP2pAPI extends EventEmitter {
); );
} }
isOnline(pubKey) {
return !!(this.contactP2pDetails[pubKey] && this.contactP2pDetails[pubKey].isOnline);
}
pingContact(pubKey) { pingContact(pubKey) {
if (!this.contactP2pDetails[pubKey]) { if (!this.contactP2pDetails[pubKey]) {
return; return;

Loading…
Cancel
Save