Transition to initialising rss feeds in schema update

pull/429/head
Beaudan Brown 6 years ago
parent 48f2637ff9
commit 3f4b94ba74

@ -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

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

@ -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()) {

@ -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();

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

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

Loading…
Cancel
Save