Refresh stories in redux when data changes

This commit is contained in:
Fedor Indutny 2022-10-20 12:16:37 -07:00 committed by GitHub
parent b82234b119
commit e14c3241c5
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
5 changed files with 71 additions and 13 deletions

View file

@ -4,6 +4,7 @@
import PQueue from 'p-queue';
import { isNumber, omit } from 'lodash';
import { z } from 'zod';
import { EventEmitter } from 'events';
import {
Direction,
@ -195,12 +196,6 @@ export function freezeSignedPreKey(
return keyPair;
}
// We add a this parameter to avoid an 'implicit any' error on the next line
const EventsMixin = function EventsMixin(this: unknown) {
Object.assign(this, window.Backbone.Events);
// eslint-disable-next-line @typescript-eslint/no-explicit-any
} as any as typeof window.Backbone.EventsMixin;
type SessionCacheEntry = CacheEntryType<SessionType, SessionRecord>;
type SenderKeyCacheEntry = CacheEntryType<SenderKeyType, SenderKeyRecord>;
@ -209,7 +204,7 @@ type ZoneQueueEntryType = Readonly<{
callback(): void;
}>;
export class SignalProtocolStore extends EventsMixin {
export class SignalProtocolStore extends EventEmitter {
// Enums used across the app
VerifiedStatus = VerifiedStatus;
@ -388,7 +383,7 @@ export class SignalProtocolStore extends EventsMixin {
const id: PreKeyIdType = `${ourUuid.toString()}:${keyId}`;
try {
this.trigger('removePreKey', ourUuid);
this.emit('removePreKey', ourUuid);
} catch (error) {
log.error(
'removePreKey error triggering removePreKey:',
@ -1669,7 +1664,7 @@ export class SignalProtocolStore extends EventsMixin {
});
try {
this.trigger('keychange', encodedAddress.uuid);
this.emit('keychange', encodedAddress.uuid);
} catch (error) {
log.error(
'saveIdentity: error triggering keychange:',
@ -1839,7 +1834,7 @@ export class SignalProtocolStore extends EventsMixin {
if (identityRecord) {
try {
this.trigger('keychange', uuid);
this.emit('keychange', uuid);
} catch (error) {
log.error(
'processVerifiedMessage error triggering keychange:',
@ -2071,6 +2066,8 @@ export class SignalProtocolStore extends EventsMixin {
window.ConversationController.reset();
await window.ConversationController.load();
this.emit('removeAllData');
}
async removeAllConfiguration(mode: RemoveAllConfiguration): Promise<void> {
@ -2156,6 +2153,43 @@ export class SignalProtocolStore extends EventsMixin {
return Array.from(union.values());
}
//
// EventEmitter types
//
public override on(
name: 'removePreKey',
handler: (ourUuid: UUID) => unknown
): this;
public override on(
name: 'keychange',
handler: (theirUuid: UUID) => unknown
): this;
public override on(name: 'removeAllData', handler: () => unknown): this;
public override on(
eventName: string | symbol,
// eslint-disable-next-line @typescript-eslint/no-explicit-any
listener: (...args: Array<any>) => void
): this {
return super.on(eventName, listener);
}
public override emit(name: 'removePreKey', ourUuid: UUID): boolean;
public override emit(name: 'keychange', theirUuid: UUID): boolean;
public override emit(name: 'removeAllData'): boolean;
public override emit(
eventName: string | symbol,
// eslint-disable-next-line @typescript-eslint/no-explicit-any
...args: Array<any>
): boolean {
return super.emit(eventName, ...args);
}
}
window.SignalProtocolStore = SignalProtocolStore;

View file

@ -540,6 +540,10 @@ export async function startApp(): Promise<void> {
window.getAccountManager().refreshPreKeys(uuidKind);
});
window.textsecure.storage.protocol.on('removeAllData', () => {
window.reduxActions.stories.removeAllStories();
});
window.getSocketStatus = () => {
if (server === undefined) {
return SocketStatus.CLOSED;
@ -1179,6 +1183,8 @@ export async function startApp(): Promise<void> {
changedConvoBatcher.add(conversation);
});
// Called by SignalProtocolStore#removeAllData()
convoCollection.on('reset', removeAllConversations);
window.Whisper.events.on('userChanged', (reconnect = false) => {

View file

@ -4795,6 +4795,7 @@ async function removeAll(): Promise<void> {
DELETE FROM storyDistributions;
DELETE FROM storyReads;
DELETE FROM unprocessed;
DELETE FROM uninstalled_sticker_packs;
`);
})();
}

View file

@ -118,6 +118,7 @@ const SEND_STORY_MODAL_OPEN_STATE_CHANGED =
const STORY_CHANGED = 'stories/STORY_CHANGED';
const TOGGLE_VIEW = 'stories/TOGGLE_VIEW';
const VIEW_STORY = 'stories/VIEW_STORY';
const REMOVE_ALL_STORIES = 'stories/REMOVE_ALL_STORIES';
type DOEStoryActionType = {
type: typeof DOE_STORY;
@ -185,6 +186,10 @@ type ViewStoryActionType = {
payload: SelectedStoryDataType | undefined;
};
type RemoveAllStoriesActionType = {
type: typeof REMOVE_ALL_STORIES;
};
export type StoriesActionType =
| DOEStoryActionType
| ListMembersVerified
@ -199,7 +204,8 @@ export type StoriesActionType =
| SendStoryModalOpenStateChanged
| StoryChangedActionType
| ToggleViewActionType
| ViewStoryActionType;
| ViewStoryActionType
| RemoveAllStoriesActionType;
// Action Creators
@ -746,6 +752,12 @@ const viewUserStories: ViewUserStoriesActionCreatorType = ({
};
};
function removeAllStories(): RemoveAllStoriesActionType {
return {
type: REMOVE_ALL_STORIES,
};
}
type ViewStoryOptionsType =
| {
closeViewer: true;
@ -1129,6 +1141,7 @@ export const actions = {
markStoryRead,
queueStoryDownload,
reactToStory,
removeAllStories,
replyToStory,
sendStoryMessage,
sendStoryModalOpenStateChanged,
@ -1424,6 +1437,10 @@ export function reducer(
};
}
if (action.type === REMOVE_ALL_STORIES) {
return getEmptyState();
}
if (action.type === QUEUE_STORY_DOWNLOAD) {
const storyIndex = state.stories.findIndex(
story => story.messageId === action.payload

View file

@ -686,12 +686,12 @@ describe('SignalProtocolStore', () => {
beforeEach(() => {
keychangeTriggered = 0;
store.bind('keychange', () => {
store.on('keychange', () => {
keychangeTriggered += 1;
});
});
afterEach(() => {
store.unbind('keychange');
store.removeAllListeners('keychange');
});
describe('when the new verified status is DEFAULT', () => {