2020-06-11 03:37:59 +03:00
|
|
|
/*
|
|
|
|
Copyright 2020 The Matrix.org Foundation C.I.C.
|
|
|
|
|
|
|
|
Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
you may not use this file except in compliance with the License.
|
|
|
|
You may obtain a copy of the License at
|
|
|
|
|
|
|
|
http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
|
|
|
|
Unless required by applicable law or agreed to in writing, software
|
|
|
|
distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
See the License for the specific language governing permissions and
|
|
|
|
limitations under the License.
|
|
|
|
*/
|
|
|
|
|
|
|
|
import { Room } from "matrix-js-sdk/src/models/room";
|
2020-06-24 21:23:09 +03:00
|
|
|
import { ActionPayload } from "../../dispatcher/payloads";
|
|
|
|
import { AsyncStoreWithClient } from "../AsyncStoreWithClient";
|
|
|
|
import defaultDispatcher from "../../dispatcher/dispatcher";
|
2020-06-26 01:26:07 +03:00
|
|
|
import { MessageEventPreview } from "./previews/MessageEventPreview";
|
|
|
|
import { TagID } from "./models";
|
|
|
|
import { isNullOrUndefined } from "matrix-js-sdk/src/utils";
|
|
|
|
import { CallInviteEventPreview } from "./previews/CallInviteEventPreview";
|
|
|
|
import { CallAnswerEventPreview } from "./previews/CallAnswerEventPreview";
|
|
|
|
import { CallHangupEvent } from "./previews/CallHangupEvent";
|
|
|
|
import { StickerEventPreview } from "./previews/StickerEventPreview";
|
|
|
|
import { ReactionEventPreview } from "./previews/ReactionEventPreview";
|
|
|
|
|
|
|
|
const PREVIEWS = {
|
|
|
|
'm.room.message': {
|
|
|
|
isState: false,
|
|
|
|
previewer: new MessageEventPreview(),
|
|
|
|
},
|
|
|
|
'm.call.invite': {
|
|
|
|
isState: false,
|
|
|
|
previewer: new CallInviteEventPreview(),
|
|
|
|
},
|
|
|
|
'm.call.answer': {
|
|
|
|
isState: false,
|
|
|
|
previewer: new CallAnswerEventPreview(),
|
|
|
|
},
|
|
|
|
'm.call.hangup': {
|
|
|
|
isState: false,
|
|
|
|
previewer: new CallHangupEvent(),
|
|
|
|
},
|
|
|
|
'm.sticker': {
|
|
|
|
isState: false,
|
|
|
|
previewer: new StickerEventPreview(),
|
|
|
|
},
|
|
|
|
'm.reaction': {
|
|
|
|
isState: false,
|
|
|
|
previewer: new ReactionEventPreview(),
|
|
|
|
},
|
|
|
|
};
|
2020-06-11 03:37:59 +03:00
|
|
|
|
2020-06-11 03:56:21 +03:00
|
|
|
// The maximum number of events we're willing to look back on to get a preview.
|
|
|
|
const MAX_EVENTS_BACKWARDS = 50;
|
|
|
|
|
2020-06-26 01:26:07 +03:00
|
|
|
// type merging ftw
|
|
|
|
type TAG_ANY = "im.vector.any";
|
|
|
|
const TAG_ANY: TAG_ANY = "im.vector.any";
|
|
|
|
|
2020-06-11 03:37:59 +03:00
|
|
|
interface IState {
|
2020-06-26 01:26:07 +03:00
|
|
|
[roomId: string]: Map<TagID | TAG_ANY, string | null>; // null indicates the preview is empty / irrelevant
|
2020-06-11 03:37:59 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
export class MessagePreviewStore extends AsyncStoreWithClient<IState> {
|
|
|
|
private static internalInstance = new MessagePreviewStore();
|
|
|
|
|
|
|
|
private constructor() {
|
|
|
|
super(defaultDispatcher, {});
|
|
|
|
}
|
|
|
|
|
|
|
|
public static get instance(): MessagePreviewStore {
|
|
|
|
return MessagePreviewStore.internalInstance;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Gets the pre-translated preview for a given room
|
|
|
|
* @param room The room to get the preview for.
|
2020-06-26 01:26:07 +03:00
|
|
|
* @param inTagId The tag ID in which the room resides
|
2020-06-11 03:37:59 +03:00
|
|
|
* @returns The preview, or null if none present.
|
|
|
|
*/
|
2020-06-26 01:26:07 +03:00
|
|
|
public getPreviewForRoom(room: Room, inTagId: TagID): string {
|
2020-06-11 03:37:59 +03:00
|
|
|
if (!room) return null; // invalid room, just return nothing
|
|
|
|
|
|
|
|
const val = this.state[room.roomId];
|
2020-06-26 01:26:07 +03:00
|
|
|
if (!val) this.generatePreview(room, inTagId);
|
2020-06-11 03:37:59 +03:00
|
|
|
|
2020-06-26 01:26:07 +03:00
|
|
|
const previews = this.state[room.roomId];
|
|
|
|
if (!previews) return null;
|
|
|
|
|
|
|
|
if (!previews.has(inTagId)) {
|
|
|
|
return previews.get(TAG_ANY);
|
|
|
|
}
|
|
|
|
return previews.get(inTagId);
|
2020-06-11 03:37:59 +03:00
|
|
|
}
|
|
|
|
|
2020-06-26 01:26:07 +03:00
|
|
|
private generatePreview(room: Room, tagId?: TagID) {
|
2020-06-23 00:57:13 +03:00
|
|
|
const events = room.timeline;
|
|
|
|
if (!events) return; // should only happen in tests
|
2020-06-11 03:37:59 +03:00
|
|
|
|
2020-06-26 01:26:07 +03:00
|
|
|
let map = this.state[room.roomId];
|
|
|
|
if (!map) {
|
|
|
|
map = new Map<TagID | TAG_ANY, string | null>();
|
|
|
|
|
|
|
|
// We set the state later with the map, so no need to send an update now
|
|
|
|
}
|
|
|
|
|
|
|
|
// Set the tags so we know what to generate
|
|
|
|
if (!map.has(TAG_ANY)) map.set(TAG_ANY, null);
|
|
|
|
if (tagId && !map.has(tagId)) map.set(tagId, null);
|
|
|
|
|
|
|
|
let changed = false;
|
2020-06-11 03:37:59 +03:00
|
|
|
for (let i = events.length - 1; i >= 0; i--) {
|
2020-06-11 03:56:21 +03:00
|
|
|
if (i === events.length - MAX_EVENTS_BACKWARDS) return; // limit reached
|
2020-06-11 03:37:59 +03:00
|
|
|
|
|
|
|
const event = events[i];
|
2020-06-26 01:26:07 +03:00
|
|
|
const previewDef = PREVIEWS[event.getType()];
|
|
|
|
if (!previewDef) continue;
|
|
|
|
if (previewDef.isState && isNullOrUndefined(event.getStateKey())) continue;
|
|
|
|
|
|
|
|
const anyPreview = previewDef.previewer.getTextFor(event, null);
|
|
|
|
if (!anyPreview) continue; // not previewable for some reason
|
|
|
|
|
|
|
|
changed = changed || anyPreview !== map.get(TAG_ANY);
|
|
|
|
map.set(TAG_ANY, anyPreview);
|
|
|
|
|
|
|
|
const tagsToGenerate = Array.from(map.keys()).filter(t => t !== TAG_ANY); // we did the any tag above
|
|
|
|
for (const genTagId of tagsToGenerate) {
|
|
|
|
const realTagId: TagID = genTagId === TAG_ANY ? null : genTagId;
|
|
|
|
const preview = previewDef.previewer.getTextFor(event, realTagId);
|
|
|
|
if (preview === anyPreview) {
|
|
|
|
changed = changed || anyPreview !== map.get(genTagId);
|
|
|
|
map.delete(genTagId);
|
|
|
|
} else {
|
|
|
|
changed = changed || preview !== map.get(genTagId);
|
|
|
|
map.set(genTagId, preview);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (changed) {
|
|
|
|
// Update state for good measure - causes emit for update
|
2020-06-11 03:37:59 +03:00
|
|
|
// noinspection JSIgnoredPromiseFromCall - the AsyncStore handles concurrent calls
|
2020-06-26 01:26:07 +03:00
|
|
|
this.updateState({[room.roomId]: map});
|
2020-06-11 03:37:59 +03:00
|
|
|
}
|
2020-06-26 01:26:07 +03:00
|
|
|
return; // we're done
|
2020-06-11 03:37:59 +03:00
|
|
|
}
|
|
|
|
|
2020-06-26 01:26:07 +03:00
|
|
|
// At this point, we didn't generate a preview so clear it
|
2020-06-11 03:37:59 +03:00
|
|
|
// noinspection JSIgnoredPromiseFromCall - the AsyncStore handles concurrent calls
|
|
|
|
this.updateState({[room.roomId]: null});
|
|
|
|
}
|
|
|
|
|
|
|
|
protected async onAction(payload: ActionPayload) {
|
|
|
|
if (!this.matrixClient) return;
|
|
|
|
|
|
|
|
if (payload.action === 'MatrixActions.Room.timeline' || payload.action === 'MatrixActions.Event.decrypted') {
|
|
|
|
const event = payload.event; // TODO: Type out the dispatcher
|
|
|
|
if (!Object.keys(this.state).includes(event.getRoomId())) return; // not important
|
2020-06-26 01:26:07 +03:00
|
|
|
this.generatePreview(this.matrixClient.getRoom(event.getRoomId()), TAG_ANY);
|
2020-06-11 03:37:59 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|