2021-06-03 20:14:35 +05:30
|
|
|
/*
|
|
|
|
Copyright 2021 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.
|
|
|
|
*/
|
2021-08-12 02:24:33 +05:30
|
|
|
import {MIN_UNICODE, MAX_UNICODE} from "./common";
|
2021-08-12 02:37:44 +05:30
|
|
|
import {Store} from "../Store";
|
2021-06-03 20:14:35 +05:30
|
|
|
|
2021-08-12 02:37:44 +05:30
|
|
|
function encodeKey(roomId: string, targetEventId: string, relType: string, sourceEventId: string): string {
|
2021-06-03 20:14:35 +05:30
|
|
|
return `${roomId}|${targetEventId}|${relType}|${sourceEventId}`;
|
|
|
|
}
|
|
|
|
|
2021-08-12 02:37:44 +05:30
|
|
|
interface RelationEntry {
|
|
|
|
roomId: string;
|
|
|
|
targetEventId: string;
|
|
|
|
sourceEventId: string;
|
|
|
|
relType: string;
|
|
|
|
}
|
|
|
|
|
|
|
|
function decodeKey(key: string): RelationEntry {
|
2021-06-03 20:14:35 +05:30
|
|
|
const [roomId, targetEventId, relType, sourceEventId] = key.split("|");
|
|
|
|
return {roomId, targetEventId, relType, sourceEventId};
|
|
|
|
}
|
|
|
|
|
|
|
|
export class TimelineRelationStore {
|
2021-08-12 02:37:44 +05:30
|
|
|
private _store: Store<{ key: string }>;
|
|
|
|
|
|
|
|
constructor(store: Store<{ key: string }>) {
|
2021-06-03 20:14:35 +05:30
|
|
|
this._store = store;
|
|
|
|
}
|
|
|
|
|
2021-09-01 00:11:07 +05:30
|
|
|
add(roomId: string, targetEventId: string, relType: string, sourceEventId: string): void {
|
|
|
|
this._store.add({key: encodeKey(roomId, targetEventId, relType, sourceEventId)});
|
2021-06-03 20:14:35 +05:30
|
|
|
}
|
|
|
|
|
2021-09-17 21:54:24 +05:30
|
|
|
remove(roomId: string, targetEventId: string, relType: string, sourceEventId: string): void {
|
|
|
|
this._store.delete(encodeKey(roomId, targetEventId, relType, sourceEventId));
|
2021-06-03 20:14:35 +05:30
|
|
|
}
|
|
|
|
|
2021-09-17 21:54:24 +05:30
|
|
|
removeAllForTarget(roomId: string, targetId: string): void {
|
2021-06-03 20:14:35 +05:30
|
|
|
const range = this._store.IDBKeyRange.bound(
|
|
|
|
encodeKey(roomId, targetId, MIN_UNICODE, MIN_UNICODE),
|
|
|
|
encodeKey(roomId, targetId, MAX_UNICODE, MAX_UNICODE),
|
|
|
|
true,
|
|
|
|
true
|
|
|
|
);
|
2021-09-17 21:54:24 +05:30
|
|
|
this._store.delete(range);
|
2021-06-03 20:14:35 +05:30
|
|
|
}
|
|
|
|
|
2021-08-12 02:37:44 +05:30
|
|
|
async getForTargetAndType(roomId: string, targetId: string, relType: string): Promise<RelationEntry[]> {
|
2021-06-03 20:14:35 +05:30
|
|
|
// exclude both keys as they are theoretical min and max,
|
|
|
|
// but we should't have a match for just the room id, or room id with max
|
|
|
|
const range = this._store.IDBKeyRange.bound(
|
|
|
|
encodeKey(roomId, targetId, relType, MIN_UNICODE),
|
|
|
|
encodeKey(roomId, targetId, relType, MAX_UNICODE),
|
|
|
|
true,
|
|
|
|
true
|
|
|
|
);
|
2021-06-24 19:46:15 +05:30
|
|
|
const items = await this._store.selectAll(range);
|
|
|
|
return items.map(i => decodeKey(i.key));
|
2021-06-03 20:14:35 +05:30
|
|
|
}
|
2021-06-16 21:10:29 +05:30
|
|
|
|
2021-08-12 02:37:44 +05:30
|
|
|
async getAllForTarget(roomId: string, targetId: string): Promise<RelationEntry[]> {
|
2021-06-16 21:10:29 +05:30
|
|
|
// exclude both keys as they are theoretical min and max,
|
|
|
|
// but we should't have a match for just the room id, or room id with max
|
|
|
|
const range = this._store.IDBKeyRange.bound(
|
|
|
|
encodeKey(roomId, targetId, MIN_UNICODE, MIN_UNICODE),
|
|
|
|
encodeKey(roomId, targetId, MAX_UNICODE, MAX_UNICODE),
|
|
|
|
true,
|
|
|
|
true
|
|
|
|
);
|
2021-06-24 19:46:15 +05:30
|
|
|
const items = await this._store.selectAll(range);
|
|
|
|
return items.map(i => decodeKey(i.key));
|
2021-06-16 21:10:29 +05:30
|
|
|
}
|
2021-06-03 20:14:35 +05:30
|
|
|
}
|