From 3f4b94ba7414bf88d03c4980c4e5252054c7a712 Mon Sep 17 00:00:00 2001 From: Beaudan Brown Date: Fri, 23 Aug 2019 13:36:36 +1000 Subject: [PATCH] Transition to initialising rss feeds in schema update --- app/sql.js | 115 ++++++++++++++++++++---------- js/background.js | 32 ++++----- js/models/conversations.js | 11 +++ js/modules/data.js | 9 +++ js/modules/loki_rss_api.js | 23 ------ libtextsecure/message_receiver.js | 6 +- 6 files changed, 113 insertions(+), 83 deletions(-) diff --git a/app/sql.js b/app/sql.js index 64fa1848a..fdae69227 100644 --- a/app/sql.js +++ b/app/sql.js @@ -97,6 +97,7 @@ module.exports = { updateConversation, removeConversation, getAllConversations, + getAllRssFeedConversations, getAllPublicConversations, getPubKeysWithFriendStatus, getAllConversationIds, @@ -784,57 +785,84 @@ async function updateToLokiSchemaVersion1(currentVersion, instance) { console.log('updateToLokiSchemaVersion1: starting...'); await instance.run('BEGIN TRANSACTION;'); - const publicChatData = { - id: 'publicChat:1@chat.lokinet.org', + await instance.run( + `ALTER TABLE messages + ADD COLUMN serverId STRING;` + ); + + const initConversation = async (data) => { + let { id, type, name, friendRequestStatus } = data; + await instance.run( + `INSERT INTO conversations ( + id, + json, + + type, + members, + name, + friendRequestStatus + ) values ( + $id, + $json, + + $type, + $members, + $name, + $friendRequestStatus + );`, + { + $id: id, + $json: objectToJSON(data), + + $type: type, + $members: null, + $name: name, + $friendRequestStatus: friendRequestStatus, + } + ); + } + + const baseData = { friendRequestStatus: 4, // Friends sealedSender: 0, sessionResetStatus: 0, swarmNodes: [], type: 'group', - server: 'https://chat.lokinet.org', - name: 'Loki Public Chat', - channelId: '1', unlockTimestamp: null, unreadCount: 0, verified: 0, version: 2, - }; - - const { id, type, name, friendRequestStatus } = publicChatData; - - await instance.run( - `ALTER TABLE messages - ADD COLUMN serverId STRING;` - ); + } - await instance.run( - `INSERT INTO conversations ( - id, - json, + const publicChatData = { + ...baseData, + id: 'publicChat:1@chat.lokinet.org', + server: 'https://chat.lokinet.org', + name: 'Loki Public Chat', + channelId: '1', + }; - type, - members, - name, - friendRequestStatus - ) values ( - $id, - $json, + const newsRssFeedData = { + ...baseData, + id: 'rss://loki.network/feed/', + rssFeed: 'https://loki.network/feed/', + closable: true, + name: 'Loki.network News', + profileAvatar: 'images/loki/loki_icon.png', + }; - $type, - $members, - $name, - $friendRequestStatus - );`, - { - $id: id, - $json: objectToJSON(publicChatData), + const updatesRssFeedData = { + ...baseData, + id: 'rss://loki.network/category/messenger-updates/feed/', + rssFeed: 'https://loki.network/category/messenger-updates/feed/', + closable: false, + name: 'Messenger updates', + profileAvatar: 'images/loki/loki_icon.png', + }; - $type: type, - $members: null, - $name: name, - $friendRequestStatus: friendRequestStatus, - } - ); + await initConversation(publicChatData); + await initConversation(newsRssFeedData); + await initConversation(updatesRssFeedData); await instance.run( `INSERT INTO loki_schema ( @@ -1606,6 +1634,17 @@ async function getAllPrivateConversations() { return map(rows, row => jsonToObject(row.json)); } +async function getAllRssFeedConversations() { + const rows = await db.all( + `SELECT json FROM conversations WHERE + type = 'group' AND + id LIKE 'rss://%' + ORDER BY id ASC;` + ); + + return map(rows, row => jsonToObject(row.json)); +} + async function getAllPublicConversations() { const rows = await db.all( `SELECT json FROM conversations WHERE diff --git a/js/background.js b/js/background.js index fe84f03c2..dab134217 100644 --- a/js/background.js +++ b/js/background.js @@ -206,6 +206,17 @@ const initAPIs = async () => { const ourKey = textsecure.storage.user.getNumber(); + const rssFeedConversations = await window.Signal.Data.getAllRssFeedConversations( + { + ConversationCollection: Whisper.ConversationCollection, + } + ); + window.feeds = []; + rssFeedConversations.forEach(conversation => { + window.feeds.push( + new window.LokiRssAPI(conversation.getRssSettings()) + ); + }); window.lokiMessageAPI = new window.LokiMessageAPI(ourKey); window.lokiPublicChatAPI = new window.LokiPublicChatAPI(ourKey); const publicConversations = await window.Signal.Data.getAllPublicConversations( @@ -228,23 +239,6 @@ window.log.warn(`Could not set up channel for ${conversation.id}`); } }); - window.feeds = []; - window.feeds.push( - new window.LokiRssAPI({ - RSS_FEED: 'https://loki.network/category/messenger-updates/feed/', - CONVO_ID: 'rss://loki.network/category/messenger-updates/feed/', - title: 'Messenger updates', - closeable: false, - }) - ); - /* - window.feeds.push(new window.LokiRssAPI({ - RSS_FEED: 'https://loki.network/feed/', - CONVO_ID: 'rss://loki.network/feed/', - title: 'Loki.network News', - closeable: true - })); - */ window.lokiP2pAPI = new window.LokiP2pAPI(ourKey); window.lokiP2pAPI.on('pingContact', pubKey => { const isPing = true; @@ -594,7 +588,7 @@ window.log.info('Cleanup: complete'); window.log.info('listening for registration events'); - Whisper.events.on('registration_done', () => { + Whisper.events.on('registration_done', async () => { window.log.info('handling registration event'); startLocalLokiServer(); @@ -608,7 +602,7 @@ // logger: window.log, // }); - initAPIs(); + await initAPIs(); connect(true); }); diff --git a/js/models/conversations.js b/js/models/conversations.js index 2d2515f13..2fdaa0b1a 100644 --- a/js/models/conversations.js +++ b/js/models/conversations.js @@ -2041,6 +2041,17 @@ getNickname() { return this.get('nickname'); }, + getRssSettings() { + if (!this.isRss()) { + return null; + } + return { + RSS_FEED: this.get('rssFeed'), + CONVO_ID: this.id, + title: this.get('name'), + closeable: this.get('closable'), + }; + }, // maybe "Backend" instead of "Source"? getPublicSource() { if (!this.isPublic()) { diff --git a/js/modules/data.js b/js/modules/data.js index cebc102cb..36be09d6f 100644 --- a/js/modules/data.js +++ b/js/modules/data.js @@ -118,6 +118,7 @@ module.exports = { getPubKeysWithFriendStatus, getAllConversationIds, getAllPrivateConversations, + getAllRssFeedConversations, getAllPublicConversations, getAllGroupsInvolvingId, @@ -741,6 +742,14 @@ async function getAllConversationIds() { return ids; } +async function getAllRssFeedConversations({ ConversationCollection }) { + const conversations = await channels.getAllRssFeedConversations(); + + const collection = new ConversationCollection(); + collection.add(conversations); + return collection; +} + async function getAllPublicConversations({ ConversationCollection }) { const conversations = await channels.getAllPublicConversations(); diff --git a/js/modules/loki_rss_api.js b/js/modules/loki_rss_api.js index 438866dd1..4a02c04af 100644 --- a/js/modules/loki_rss_api.js +++ b/js/modules/loki_rss_api.js @@ -79,25 +79,6 @@ class LokiRssAPI extends EventEmitter { this.getFeed(); } - setupConversation() { - // only run once - if (this.conversationSetup) return; - // wait until conversations are loaded - if (ConversationController._initialFetchComplete) { - const conversation = ConversationController.getOrCreate( - this.groupId, - 'group' - ); - conversation.setFriendRequestStatus(friendRequestStatusEnum.friends); - conversation.setGroupNameAndAvatar( - this.feedTitle, - 'images/loki/loki_icon.png' - ); - conversation.updateTextInputState(); - this.conversationSetup = true; // prevent running again - } - } - async getFeed() { let response; let success = true; @@ -122,10 +103,6 @@ class LokiRssAPI extends EventEmitter { const feedObj = xml2json(feedDOM); let receivedAt = new Date().getTime(); - // make sure conversation is set up properly - // (delay to after the network response intentionally) - this.setupConversation(); - if (!feedObj || !feedObj.rss || !feedObj.rss.channel) { log.error('rsserror', feedObj, feedDOM, responseXML); return; diff --git a/libtextsecure/message_receiver.js b/libtextsecure/message_receiver.js index 153b067c4..8530d190b 100644 --- a/libtextsecure/message_receiver.js +++ b/libtextsecure/message_receiver.js @@ -77,10 +77,10 @@ MessageReceiver.prototype.extend({ }); this.httpPollingResource.pollServer(); localLokiServer.on('message', this.handleP2pMessage.bind(this)); - lokiPublicChatAPI.on('publicMessage', this.handlePublicMessage.bind(this)); + lokiPublicChatAPI.on('publicMessage', this.handleUnencryptedMessage.bind(this)); // set up pollers for any RSS feeds feeds.forEach(feed => { - feed.on('rssMessage', this.handleRssMessage.bind(this)); + feed.on('rssMessage', this.handleUnencryptedMessage.bind(this)); }); this.startLocalServer(); @@ -149,7 +149,7 @@ MessageReceiver.prototype.extend({ }; this.httpPollingResource.handleMessage(message, options); }, - handlePublicMessage({ message }) { + handleUnencryptedMessage({ message }) { const ev = new Event('message'); ev.confirm = function confirmTerm() {}; ev.data = message;