forked from mystiq/hydrogen-web
Migrate RoomMemberStore.js to TypeScript
This commit is contained in:
parent
38a38e8287
commit
7c56ac7746
3 changed files with 32 additions and 17 deletions
|
@ -23,7 +23,7 @@ import {InviteStore} from "./stores/InviteStore.js";
|
||||||
import {TimelineEventStore} from "./stores/TimelineEventStore.js";
|
import {TimelineEventStore} from "./stores/TimelineEventStore.js";
|
||||||
import {TimelineRelationStore} from "./stores/TimelineRelationStore.js";
|
import {TimelineRelationStore} from "./stores/TimelineRelationStore.js";
|
||||||
import {RoomStateStore} from "./stores/RoomStateStore.js";
|
import {RoomStateStore} from "./stores/RoomStateStore.js";
|
||||||
import {RoomMemberStore} from "./stores/RoomMemberStore.js";
|
import {RoomMemberStore} from "./stores/RoomMemberStore";
|
||||||
import {TimelineFragmentStore} from "./stores/TimelineFragmentStore.js";
|
import {TimelineFragmentStore} from "./stores/TimelineFragmentStore.js";
|
||||||
import {PendingEventStore} from "./stores/PendingEventStore.js";
|
import {PendingEventStore} from "./stores/PendingEventStore.js";
|
||||||
import {UserIdentityStore} from "./stores/UserIdentityStore.js";
|
import {UserIdentityStore} from "./stores/UserIdentityStore.js";
|
||||||
|
|
|
@ -1,6 +1,6 @@
|
||||||
import {iterateCursor, reqAsPromise} from "./utils";
|
import {iterateCursor, reqAsPromise} from "./utils";
|
||||||
import {RoomMember, EVENT_TYPE as MEMBER_EVENT_TYPE} from "../../room/members/RoomMember.js";
|
import {RoomMember, EVENT_TYPE as MEMBER_EVENT_TYPE} from "../../room/members/RoomMember.js";
|
||||||
import {RoomMemberStore} from "./stores/RoomMemberStore.js";
|
import {RoomMemberStore} from "./stores/RoomMemberStore";
|
||||||
import {SessionStore} from "./stores/SessionStore";
|
import {SessionStore} from "./stores/SessionStore";
|
||||||
import {encodeScopeTypeKey} from "./stores/OperationStore.js";
|
import {encodeScopeTypeKey} from "./stores/OperationStore.js";
|
||||||
|
|
||||||
|
|
|
@ -16,43 +16,58 @@ limitations under the License.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
import {MAX_UNICODE} from "./common";
|
import {MAX_UNICODE} from "./common";
|
||||||
|
import {Store} from "../Store";
|
||||||
|
|
||||||
function encodeKey(roomId, userId) {
|
function encodeKey(roomId: string, userId: string) {
|
||||||
return `${roomId}|${userId}`;
|
return `${roomId}|${userId}`;
|
||||||
}
|
}
|
||||||
|
|
||||||
function decodeKey(key) {
|
function decodeKey(key: string): { roomId: string, userId: string } {
|
||||||
const [roomId, userId] = key.split("|");
|
const [roomId, userId] = key.split("|");
|
||||||
return {roomId, userId};
|
return {roomId, userId};
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// TODO: Move to RoomMember when that's TypeScript.
|
||||||
|
export interface MemberData {
|
||||||
|
roomId: string;
|
||||||
|
userId: string;
|
||||||
|
avatarUrl: string;
|
||||||
|
displayName: string;
|
||||||
|
membership: "join" | "leave" | "invite" | "ban";
|
||||||
|
}
|
||||||
|
|
||||||
|
type MemberStorageEntry = MemberData & { key: string }
|
||||||
|
|
||||||
// no historical members
|
// no historical members
|
||||||
export class RoomMemberStore {
|
export class RoomMemberStore {
|
||||||
constructor(roomMembersStore) {
|
private _roomMembersStore: Store<MemberStorageEntry>;
|
||||||
|
|
||||||
|
constructor(roomMembersStore: Store<MemberStorageEntry>) {
|
||||||
this._roomMembersStore = roomMembersStore;
|
this._roomMembersStore = roomMembersStore;
|
||||||
}
|
}
|
||||||
|
|
||||||
get(roomId, userId) {
|
get(roomId: string, userId: string): Promise<MemberStorageEntry | null> {
|
||||||
return this._roomMembersStore.get(encodeKey(roomId, userId));
|
return this._roomMembersStore.get(encodeKey(roomId, userId));
|
||||||
}
|
}
|
||||||
|
|
||||||
async set(member) {
|
async set(member: MemberData): Promise<IDBValidKey> {
|
||||||
member.key = encodeKey(member.roomId, member.userId);
|
// Object.assign would be more typesafe, but small objects
|
||||||
return this._roomMembersStore.put(member);
|
(member as any).key = encodeKey(member.roomId, member.userId);
|
||||||
}
|
return this._roomMembersStore.put(member as MemberStorageEntry);
|
||||||
|
}
|
||||||
|
|
||||||
getAll(roomId) {
|
getAll(roomId: string): Promise<MemberData[]> {
|
||||||
const range = this._roomMembersStore.IDBKeyRange.lowerBound(encodeKey(roomId, ""));
|
const range = this._roomMembersStore.IDBKeyRange.lowerBound(encodeKey(roomId, ""));
|
||||||
return this._roomMembersStore.selectWhile(range, member => {
|
return this._roomMembersStore.selectWhile(range, member => {
|
||||||
return member.roomId === roomId;
|
return member.roomId === roomId;
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
async getAllUserIds(roomId) {
|
async getAllUserIds(roomId: string): Promise<string[]> {
|
||||||
const userIds = [];
|
const userIds: string[] = [];
|
||||||
const range = this._roomMembersStore.IDBKeyRange.lowerBound(encodeKey(roomId, ""));
|
const range = this._roomMembersStore.IDBKeyRange.lowerBound(encodeKey(roomId, ""));
|
||||||
await this._roomMembersStore.iterateKeys(range, key => {
|
await this._roomMembersStore.iterateKeys(range, key => {
|
||||||
const decodedKey = decodeKey(key);
|
const decodedKey = decodeKey(key as string);
|
||||||
// prevent running into the next room
|
// prevent running into the next room
|
||||||
if (decodedKey.roomId === roomId) {
|
if (decodedKey.roomId === roomId) {
|
||||||
userIds.push(decodedKey.userId);
|
userIds.push(decodedKey.userId);
|
||||||
|
@ -63,10 +78,10 @@ export class RoomMemberStore {
|
||||||
return userIds;
|
return userIds;
|
||||||
}
|
}
|
||||||
|
|
||||||
removeAllForRoom(roomId) {
|
removeAllForRoom(roomId: string): Promise<undefined> {
|
||||||
// exclude both keys as they are theoretical min and max,
|
// 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
|
// but we should't have a match for just the room id, or room id with max
|
||||||
const range = this._roomMembersStore.IDBKeyRange.bound(roomId, `${roomId}|${MAX_UNICODE}`, true, true);
|
const range = this._roomMembersStore.IDBKeyRange.bound(roomId, `${roomId}|${MAX_UNICODE}`, true, true);
|
||||||
this._roomMembersStore.delete(range);
|
return this._roomMembersStore.delete(range);
|
||||||
}
|
}
|
||||||
}
|
}
|
Loading…
Reference in a new issue