add the OpenGroupManagerV2 logic to add and remove rooms

pull/1576/head
Audric Ackermann 4 years ago
parent f7581cf4eb
commit 786cd384fb
No known key found for this signature in database
GPG Key ID: 999F434D76324AD4

@ -25,9 +25,7 @@ exports.open = (name, version, { onUpgradeNeeded } = {}) => {
}
reject(
new Error(
'Database upgrade required:' + ` oldVersion: ${oldVersion}, newVersion: ${newVersion}`
)
new Error(`Database upgrade required: oldVersion: ${oldVersion}, newVersion: ${newVersion}`)
);
};

@ -1,4 +1,3 @@
/* global clearTimeout, log */
// was timeoutDelay
const sleepFor = ms => new Promise(resolve => setTimeout(resolve, ms));

@ -22,7 +22,7 @@ exports.migrateDataToFileSystem = async (attachment, { writeNewAttachmentData }
const isValidData = isArrayBuffer(data);
if (!isValidData) {
throw new TypeError(
'Expected `attachment.data` to be an array buffer;' + ` got: ${typeof attachment.data}`
`Expected ${attachment.data} to be an array buffer got: ${typeof attachment.data}`
);
}

@ -15,7 +15,6 @@ const { clipboard } = electron;
window.PROTO_ROOT = 'protos';
const appConfig = require('./app/config');
const config = require('url').parse(window.location.toString(), true).query;
let title = config.name;
@ -50,7 +49,7 @@ window.getHostName = () => config.hostname;
window.getServerTrustRoot = () => config.serverTrustRoot;
window.JobQueue = JobQueue;
window.isBehindProxy = () => Boolean(config.proxyUrl);
//FIXME audric
// FIXME audric
window.getStoragePubKey = key => key.substring(0, key.length - 2);
// window.isDev() ? key.substring(0, key.length - 2) : key;

@ -15,7 +15,7 @@ describe('Privacy', () => {
const actual = Privacy.redactSessionID(text);
const expected =
'This is a log line with a session ID [REDACTED]\n' + 'and another one [REDACTED]';
'This is a log line with a session ID [REDACTED]\n and another one [REDACTED]';
assert.equal(actual, expected);
});
@ -31,8 +31,7 @@ describe('Privacy', () => {
describe('redactGroupIds', () => {
it('should redact all group IDs', () => {
const text =
'This is a log line with two group IDs: group(123456789)\n' + 'and group(abcdefghij)';
const text = 'This is a log line with two group IDs: group(123456789)\nand group(abcdefghij)';
const actual = Privacy.redactGroupIds(text);
const expected =
@ -43,7 +42,7 @@ describe('Privacy', () => {
it('should remove newlines from redacted group IDs', () => {
const text =
'This is a log line with two group IDs: group(12345678\n9)\n' + 'and group(abc\ndefghij)';
'This is a log line with two group IDs: group(12345678\n9)\nand group(abc\ndefghij)';
const actual = Privacy.redactGroupIds(text);
const expected =

@ -187,34 +187,14 @@ export const ActionsPanel = () => {
// 'https://opengroup.bilb.us/main?public_key=1352534ba73d4265973280431dbc72e097a3e43275d1ada984f9805b4943047d'
void syncConfiguration();
const parsedRoom = parseOpenGroupV2(
'https://opengroup.bilb.us/main?public_key=1352534ba73d4265973280431dbc72e097a3e43275d1ada984f9805b4943047d'
);
if (parsedRoom) {
setTimeout(async () => {
// await joinOpenGroupV2(parsedRoom);
// const oldMessages = await getMessages({
// serverUrl: parsedRoom.serverUrl,
// roomId: parsedRoom.roomId,
// });
// const msg = new OpenGroupMessageV2({
// base64EncodedData: 'dffdldfkldf',
// sentTimestamp: Date.now(),
// });
// const postedMessage = await postMessage(msg, {
// serverUrl: parsedRoom.serverUrl,
// roomId: parsedRoom.roomId,
// });
// const fileID = await downloadPreviewOpenGroupV2({
// serverUrl: parsedRoom.serverUrl,
// roomId: parsedRoom.roomId,
// });
// const rooms = [
// { serverUrl: 'https://opengroup.bilb.us', roomId: 'main' },
// ];
// await compactFetchEverything(rooms);
}, 6000);
}
// const parsedRoom = parseOpenGroupV2(
// 'https://opengroup.bilb.us/main?public_key=1352534ba73d4265973280431dbc72e097a3e43275d1ada984f9805b4943047d'
// );
// if (parsedRoom) {
// setTimeout(async () => {
// }, 6000);
// }
}, []);
// wait for cleanUpMediasInterval and then start cleaning up medias

@ -3,7 +3,9 @@ import { ConversationModel, ConversationType } from '../../models/conversation';
import { ConversationController } from '../../session/conversations';
import { allowOnlyOneAtATime } from '../../session/utils/Promise';
import { getOpenGroupV2ConversationId } from '../utils/OpenGroupUtils';
import { OpenGroupRequestCommonType } from './ApiUtil';
import { openGroupV2GetRoomInfo } from './OpenGroupAPIV2';
import { OpenGroupServerPoller } from './OpenGroupServerPoller';
/**
* When we get our configuration from the network, we might get a few times the same open group on two different messages.
@ -86,8 +88,9 @@ export class OpenGroupManagerV2 {
private static instance: OpenGroupManagerV2;
// private pollers: OpenGroupPollerV2 = [];
private readonly pollers: Map<string, OpenGroupServerPoller> = new Map();
private isPolling = false;
private wasStopped = false;
private constructor() {}
@ -102,6 +105,45 @@ export class OpenGroupManagerV2 {
if (this.isPolling) {
return;
}
if (this.wasStopped) {
throw new Error('OpengroupManager is not supposed to be starting again after being stopped.');
}
this.isPolling = true;
}
/**
* This is not designed to be restarted for now. If you stop polling
*/
public stopPolling() {
if (!this.isPolling) {
return;
}
this.wasStopped = true;
this.isPolling = false;
}
public addRoomToPolledRooms(roomInfos: OpenGroupRequestCommonType) {
const poller = this.pollers.get(roomInfos.serverUrl);
if (!poller) {
this.pollers.set(roomInfos.serverUrl, new OpenGroupServerPoller([roomInfos]));
return;
}
// this won't do a thing if the room is already polled for
poller.addRoomToPoll(roomInfos);
}
public removeRoomFromPolledRooms(roomInfos: OpenGroupRequestCommonType) {
const poller = this.pollers.get(roomInfos.serverUrl);
if (!poller) {
console.warn('No such poller');
return;
}
// this won't do a thing if the room is already polled for
poller.removeRoomFromPoll(roomInfos);
if (poller.getPolledRoomsCount() === 0) {
this.pollers.delete(roomInfos.serverUrl);
// this poller is not needed anymore, kill it
poller.stop();
}
}
}

@ -14,7 +14,7 @@ export class OpenGroupServerPoller {
private readonly serverUrl: string;
private readonly roomIdsToPoll: Set<string> = new Set();
private pollForEverythingTimer?: NodeJS.Timeout;
private abortController?: AbortController;
private readonly abortController: AbortController;
/**
* isPolling is set to true when we have a request going for this serverUrl.
@ -35,6 +35,8 @@ export class OpenGroupServerPoller {
if (!every) {
throw new Error('All rooms must be for the same serverUrl');
}
// first verify the rooms we got are all from on the same server
this.serverUrl = firstUrl;
roomInfos.forEach(r => {
this.roomIdsToPoll.add(r.roomId);
@ -42,8 +44,6 @@ export class OpenGroupServerPoller {
this.abortController = new AbortController();
this.pollForEverythingTimer = global.setInterval(this.compactPoll, pollForEverythingInterval);
// first verify the rooms we got are all from on the same server
}
/**
@ -77,6 +77,10 @@ export class OpenGroupServerPoller {
}
}
public getPolledRoomsCount() {
return this.roomIdsToPoll.size;
}
/**
* Stop polling.
* Requests currently being made will we canceled.
@ -92,23 +96,32 @@ export class OpenGroupServerPoller {
}
}
private async compactPoll() {
private shouldPoll() {
if (this.wasStopped) {
window.log.error('serverpoller was stopped. CompactPoll should not happen');
return;
window.log.error('Serverpoller was stopped. CompactPoll should not happen');
return false;
}
if (!this.roomIdsToPoll.size) {
return;
return false;
}
// return early if a poll is already in progress
if (this.isPolling) {
return false;
}
return true;
}
private async compactPoll() {
if (!this.shouldPoll()) {
return;
}
// do everything with throwing so we can check only at one place
// what we have to clean
try {
this.isPolling = true;
if (!this.abortController || this.abortController.signal.aborted) {
// don't try to make the request if we are aborted
if (this.abortController.signal.aborted) {
throw new Error('Poller aborted');
}
@ -118,16 +131,14 @@ export class OpenGroupServerPoller {
this.abortController.signal
);
if (this.abortController && this.abortController.signal.aborted) {
this.abortController = undefined;
window.log.warn('Abort controller was canceled. dropping request');
return;
// check that we are still not aborted
if (this.abortController.signal.aborted) {
throw new Error('Abort controller was canceled. dropping request');
}
if (!compactFetchResults) {
window.log.info('compactFetch: no results');
return;
throw new Error('compactFetch: no results');
}
// we were not aborted, just make sure to filter out roomIds we are not polling for anymore
// we were not aborted, make sure to filter out roomIds we are not polling for anymore
compactFetchResults = compactFetchResults.filter(result =>
this.roomIdsToPoll.has(result.roomId)
);
@ -135,10 +146,6 @@ export class OpenGroupServerPoller {
} catch (e) {
window.log.warn('Got error while compact fetch:', e);
} finally {
if (this.abortController && this.abortController.signal.aborted) {
this.abortController = undefined;
window.log.warn('Abort controller was canceled. dropping request');
}
this.isPolling = false;
}
}

@ -377,7 +377,7 @@ async function handleProfileUpdate(
if (!isIncoming) {
// We update our own profileKey if it's different from what we have
const ourNumber = UserUtils.getOurPubKeyStrFromCache();
const me = await ConversationController.getInstance().getOrCreate(
const me = ConversationController.getInstance().getOrCreate(
ourNumber,
ConversationType.PRIVATE
);

Loading…
Cancel
Save