fix: allow any sogs to be awaited until messages are fetched

pull/2480/head
Audric Ackermann 3 years ago
parent 38d85a653a
commit d9621f79c3

@ -19,10 +19,13 @@ import useKey from 'react-use/lib/useKey';
import { getOverlayMode } from '../../../state/selectors/section';
import { openConversationWithMessages } from '../../../state/ducks/conversations';
async function joinOpenGroup(serverUrl: string) {
async function joinOpenGroup(
serverUrl: string,
uiCallback?: (args: JoinSogsRoomUICallbackArgs) => void
) {
// guess if this is an open
if (serverUrl.match(openGroupV2CompleteURLRegex)) {
const groupCreated = await joinOpenGroupV2WithUIEvents(serverUrl, true, false);
const groupCreated = await joinOpenGroupV2WithUIEvents(serverUrl, true, false, uiCallback);
return groupCreated;
} else {
ToastUtils.pushToastError('invalidOpenGroupUrl', window.i18n('invalidOpenGroupUrl'));
@ -42,16 +45,12 @@ export const OverlayCommunity = () => {
dispatch(resetOverlayMode());
}
async function onEnterPressed() {
async function onTryJoinRoom(completeUrl?: string) {
try {
if (loading) {
return;
}
setLoading(true);
const groupCreated = await joinOpenGroup(groupUrl);
if (groupCreated) {
closeOverlay();
}
await joinOpenGroup(completeUrl || groupUrl, joinSogsUICallback);
} catch (e) {
window.log.warn(e);
} finally {
@ -59,7 +58,7 @@ export const OverlayCommunity = () => {
}
}
function onJoinSessionSogsRoom(args: JoinSogsRoomUICallbackArgs) {
function joinSogsUICallback(args: JoinSogsRoomUICallbackArgs) {
setLoading(args.loadingState === 'started');
if (args.loadingState === 'finished' && overlayModeIsCommunity && args.conversationKey) {
@ -87,7 +86,7 @@ export const OverlayCommunity = () => {
isGroup={true}
maxLength={300}
onChange={setGroupUrl}
onPressEnter={onEnterPressed}
onPressEnter={onTryJoinRoom}
/>
</div>
@ -96,14 +95,11 @@ export const OverlayCommunity = () => {
buttonType={SessionButtonType.BrandOutline}
text={buttonText}
disabled={!groupUrl}
onClick={onEnterPressed}
onClick={onTryJoinRoom}
/>
<SessionSpinner loading={loading} />
<SessionJoinableRooms
onJoinSessionSogsRoom={onJoinSessionSogsRoom}
alreadyJoining={loading}
/>
<SessionJoinableRooms onJoinClick={onTryJoinRoom} alreadyJoining={loading} />
</div>
);
};

@ -2,11 +2,7 @@ import React, { useEffect } from 'react';
import { useDispatch, useSelector } from 'react-redux';
import styled from 'styled-components';
import {
joinOpenGroupV2WithUIEvents,
JoinSogsRoomUICallbackArgs,
parseOpenGroupV2,
} from '../../../session/apis/open_group_api/opengroupV2/JoinOpenGroupV2';
import { parseOpenGroupV2 } from '../../../session/apis/open_group_api/opengroupV2/JoinOpenGroupV2';
import { sogsV3FetchPreviewBase64 } from '../../../session/apis/open_group_api/sogsv3/sogsV3FetchFile';
import { updateDefaultBase64RoomData } from '../../../state/ducks/defaultRooms';
import { StateType } from '../../../state/reducer';
@ -113,17 +109,11 @@ const SessionJoinableRoomRow = (props: JoinableRoomProps) => {
};
const JoinableRooms = (props: {
onJoinSessionSogsRoom: (args: JoinSogsRoomUICallbackArgs) => void;
alreadyJoining: boolean;
onJoinClick?: (completeUrl: string) => void;
}) => {
const joinableRooms = useSelector((state: StateType) => state.defaultRooms);
const onClick = props.alreadyJoining
? undefined
: (completeUrl: string) => {
void joinOpenGroupV2WithUIEvents(completeUrl, true, false, props.onJoinSessionSogsRoom);
};
return (
<>
{joinableRooms.rooms.map(r => {
@ -135,7 +125,7 @@ const JoinableRooms = (props: {
roomId={r.id}
imageId={r.imageId}
base64Data={r.base64Data}
onClick={onClick}
onClick={props.onJoinClick}
/>
);
})}
@ -144,7 +134,7 @@ const JoinableRooms = (props: {
};
export const SessionJoinableRooms = (props: {
onJoinSessionSogsRoom: (args: JoinSogsRoomUICallbackArgs) => void;
onJoinClick?: (completeUrl: string) => void;
alreadyJoining: boolean;
}) => {
const joinableRooms = useSelector((state: StateType) => state.defaultRooms);

@ -84,8 +84,6 @@ let newVersion = false;
window.document.title = window.getTitle();
// Whisper.events =
// window.Whisper.events = WhisperEvents ?
const WhisperEvents = _.clone(Backbone.Events);
window.Whisper = window.Whisper || {};
window.Whisper.events = WhisperEvents;

@ -2,7 +2,7 @@ import _ from 'lodash';
import { OpenGroupV2Room } from '../../../../data/opengroups';
import { getConversationController } from '../../../conversations';
import { PromiseUtils, ToastUtils } from '../../../utils';
import { getEventSessionSogsFirstPoll } from '../../../utils/GlobalEvents';
import { getEventSogsFirstPoll } from '../../../utils/GlobalEvents';
import { sleepFor, waitForTask } from '../../../utils/Promise';
import { forceSyncConfigurationNowIfNeeded } from '../../../utils/syncUtils';
@ -12,7 +12,7 @@ import {
prefixify,
publicKeyParam,
} from '../utils/OpenGroupUtils';
import { defaultServer, hasExistingOpenGroup } from './ApiUtil';
import { hasExistingOpenGroup } from './ApiUtil';
import { getOpenGroupManager } from './OpenGroupManagerV2';
// tslint:disable: variable-name
@ -154,12 +154,12 @@ export async function joinOpenGroupV2WithUIEvents(
await joinOpenGroupV2(parsedRoom, fromConfigMessage);
if (parsedRoom.serverUrl === defaultServer) {
if (!fromConfigMessage && showToasts) {
// this is very hacky but is made so we wait for the poller to receive the first messages related to that room.
// once the poller added all the messages to the queue of jobs to be run, we still wait a bit for them to be processed.
// This won't age well, but I am not too sure how we can design better.
await waitForTask(done => {
const eventToWait = getEventSessionSogsFirstPoll(parsedRoom.roomId);
const eventToWait = getEventSogsFirstPoll(parsedRoom.serverPublicKey, parsedRoom.roomId);
window.Whisper.events.on(eventToWait, async () => {
window.Whisper.events.off(eventToWait);
await sleepFor(5000);

@ -1,6 +1,6 @@
import { AbortController } from 'abort-controller';
import { getOpenGroupV2ConversationId } from '../utils/OpenGroupUtils';
import { defaultServer, OpenGroupRequestCommonType } from './ApiUtil';
import { OpenGroupRequestCommonType } from './ApiUtil';
import _, { isNumber, isObject } from 'lodash';
import { OpenGroupData } from '../../../../data/opengroups';
@ -20,7 +20,7 @@ import {
roomHasBlindEnabled,
} from '../sogsv3/sogsV3Capabilities';
import { OpenGroupReaction } from '../../../../types/Reaction';
import { getEventSessionSogsFirstPoll } from '../../../utils/GlobalEvents';
import { getEventSogsFirstPoll } from '../../../utils/GlobalEvents';
export type OpenGroupMessageV4 = {
/** AFAIK: indicates the number of the message in the group. e.g. 2nd message will be 1 or 2 */
@ -319,10 +319,13 @@ export class OpenGroupServerPoller {
// ==> At this point all those results need to trigger conversation updates, so update what we have to update
await handleBatchPollResults(this.serverUrl, batchPollResults, subrequestOptions);
if (this.serverUrl === defaultServer) {
const roomsInDb = OpenGroupData.getV2OpenGroupRoomsByServerUrl(this.serverUrl);
if (roomsInDb?.[0]?.serverPublicKey) {
for (const room of subrequestOptions) {
if (room.type === 'messages' && !room.messages?.sinceSeqNo && room.messages?.roomId) {
window.Whisper.events.trigger(getEventSessionSogsFirstPoll(room.messages.roomId));
window.Whisper.events.trigger(
getEventSogsFirstPoll(roomsInDb[0].serverPublicKey, room.messages.roomId)
);
}
}
}

@ -1,3 +1,3 @@
export function getEventSessionSogsFirstPoll(roomId: string) {
return `first-poll-session-sogs:${roomId}`;
export function getEventSogsFirstPoll(serverpubkey: string, roomId: string) {
return `first-poll-sogs:${roomId}-${serverpubkey}`;
}

Loading…
Cancel
Save