join openGroup => allow only one at a time for each channel/server

pull/1528/head
Audric Ackermann 5 years ago
parent 8c87935378
commit f6e3b354d0

@ -1,3 +1,4 @@
import { allowOnlyOneAtATime } from '../../../js/modules/loki_primitives';
import { ConversationModel } from '../../models/conversation'; import { ConversationModel } from '../../models/conversation';
import { ConversationController } from '../conversations'; import { ConversationController } from '../conversations';
import { PromiseUtils } from '../utils'; import { PromiseUtils } from '../utils';
@ -142,7 +143,7 @@ export class OpenGroup {
// Try to connect to server // Try to connect to server
try { try {
conversation = await PromiseUtils.timeout( conversation = await PromiseUtils.timeout(
OpenGroup.attemptConnection(prefixedServer, channel), OpenGroup.attemptConnectionOneAtATime(prefixedServer, channel),
20000 20000
); );
@ -245,8 +246,32 @@ export class OpenGroup {
return `http${hasSSL ? 's' : ''}://${server}`; return `http${hasSSL ? 's' : ''}://${server}`;
} }
/**
* When we get our configuration from the network, we might get a few times the same open group on two different messages.
* If we don't do anything, we will join them multiple times.
* Even if the convo exists only once, the lokiPublicChat API will have several instances polling for the same open group.
* Which will cause a lot of duplicate messages as they will be merged on a single conversation.
*
* To avoid this issue, we allow only a single join of a specific opengroup at a time.
*/
private static async attemptConnectionOneAtATime(
serverURL: string,
channelId: number = 1
): Promise<ConversationModel> {
if (!serverURL) {
throw new Error('Cannot join open group with empty URL');
}
const oneAtaTimeStr = `oneAtaTimeOpenGroupJoin:${serverURL}${channelId}`;
return allowOnlyOneAtATime(oneAtaTimeStr, async () => {
return OpenGroup.attemptConnection(serverURL, channelId);
});
}
// Attempts a connection to an open group server // Attempts a connection to an open group server
private static async attemptConnection(serverURL: string, channelId: number) { private static async attemptConnection(
serverURL: string,
channelId: number
): Promise<ConversationModel> {
let completeServerURL = serverURL.toLowerCase(); let completeServerURL = serverURL.toLowerCase();
const valid = OpenGroup.validate(completeServerURL); const valid = OpenGroup.validate(completeServerURL);
if (!valid) { if (!valid) {

Loading…
Cancel
Save