49 lines
1.7 KiB
TypeScript
49 lines
1.7 KiB
TypeScript
|
|
import { MatrixEvent } from 'matrix-js-sdk';
|
||
|
|
import { PackAddress } from '../custom-emoji/PackAddress';
|
||
|
|
import { SoundboardClipReader } from './SoundboardClipReader';
|
||
|
|
import { SoundboardClipsReader } from './SoundboardClipsReader';
|
||
|
|
import { SoundboardMetaReader } from './SoundboardMetaReader';
|
||
|
|
import { SoundboardContent } from './types';
|
||
|
|
|
||
|
|
/** Parallels custom-emoji/ImagePack. Holds a soundboard pack's meta + clips. */
|
||
|
|
export class SoundboardPack {
|
||
|
|
public readonly id: string;
|
||
|
|
|
||
|
|
public readonly deleted: boolean;
|
||
|
|
|
||
|
|
public readonly address: PackAddress | undefined;
|
||
|
|
|
||
|
|
public readonly meta: SoundboardMetaReader;
|
||
|
|
|
||
|
|
public readonly clips: SoundboardClipsReader;
|
||
|
|
|
||
|
|
private clipsMemo: SoundboardClipReader[] | undefined;
|
||
|
|
|
||
|
|
constructor(id: string, content: SoundboardContent, address: PackAddress | undefined) {
|
||
|
|
this.id = id;
|
||
|
|
this.address = address;
|
||
|
|
this.deleted = content.pack === undefined && content.clips === undefined;
|
||
|
|
this.meta = new SoundboardMetaReader(content.pack ?? {});
|
||
|
|
this.clips = new SoundboardClipsReader(content.clips ?? {});
|
||
|
|
}
|
||
|
|
|
||
|
|
static fromMatrixEvent(id: string, matrixEvent: MatrixEvent): SoundboardPack {
|
||
|
|
const roomId = matrixEvent.getRoomId();
|
||
|
|
const stateKey = matrixEvent.getStateKey();
|
||
|
|
const address =
|
||
|
|
roomId && typeof stateKey === 'string' ? new PackAddress(roomId, stateKey) : undefined;
|
||
|
|
return new SoundboardPack(id, matrixEvent.getContent<SoundboardContent>(), address);
|
||
|
|
}
|
||
|
|
|
||
|
|
getClips(): SoundboardClipReader[] {
|
||
|
|
if (this.clipsMemo) return this.clipsMemo;
|
||
|
|
this.clipsMemo = Array.from(this.clips.collection.values());
|
||
|
|
return this.clipsMemo;
|
||
|
|
}
|
||
|
|
|
||
|
|
getAvatarUrl(): string | undefined {
|
||
|
|
if (this.meta.avatar) return this.meta.avatar;
|
||
|
|
return undefined;
|
||
|
|
}
|
||
|
|
}
|