import { IPersistence, IPersistenceRead } from '@rocket.chat/apps-engine/definition/accessors';
import { RocketChatAssociationModel, RocketChatAssociationRecord } from '@rocket.chat/apps-engine/definition/metadata';
import { IRoom } from '@rocket.chat/apps-engine/definition/rooms';
export class MessagePersistence {
public static async persist(persis: IPersistence, room: IRoom, id: string): Promise<boolean> {
const associations: Array<RocketChatAssociationRecord> = [
new RocketChatAssociationRecord(RocketChatAssociationModel.MISC, 'message'),
new RocketChatAssociationRecord(RocketChatAssociationModel.ROOM, room.id),
new RocketChatAssociationRecord(RocketChatAssociationModel.MISC, id),
await persis.updateByAssociations(associations, { id }, true);
// query all records within the "scope" - message
public static async findAll(persis: IPersistenceRead): Promise<Array<string>> {
const associations: Array<RocketChatAssociationRecord> = [
new RocketChatAssociationRecord(RocketChatAssociationModel.MISC, 'message'),
let result: Array<string> = [];
const records: Array<{ id: string }> = (await persis.readByAssociations(associations)) as Array<{ id: string }>;
result = records.map(({ id }) => id);
// query all records by room within the "scope" - message
public static async findByRoom(persis: IPersistenceRead, room: IRoom): Promise<Array<string>> {
const associations: Array<RocketChatAssociationRecord> = [
new RocketChatAssociationRecord(RocketChatAssociationModel.MISC, 'message'),
new RocketChatAssociationRecord(RocketChatAssociationModel.ROOM, room.id),
let result: Array<string> = [];
const records: Array<{ id: string }> = (await persis.readByAssociations(associations)) as Array<{ id: string }>;
result = records.map(({ id }) => id);
// query all records by room within the "scope" - message
public static async removeByRoom(persis: IPersistence, room: IRoom): Promise<boolean> {
const associations: Array<RocketChatAssociationRecord> = [
new RocketChatAssociationRecord(RocketChatAssociationModel.MISC, 'message'),
new RocketChatAssociationRecord(RocketChatAssociationModel.ROOM, room.id),
await persis.removeByAssociations(associations);
// remove all records by id within the "scope" - message
public static async removeById(persis: IPersistence, id: string): Promise<boolean> {
const associations: Array<RocketChatAssociationRecord> = [
new RocketChatAssociationRecord(RocketChatAssociationModel.MISC, 'message'),
new RocketChatAssociationRecord(RocketChatAssociationModel.MISC, id),
await persis.removeByAssociations(associations);
// remove all records within the "scope" - message
public static async clear(persis): Promise<boolean> {
const associations: Array<RocketChatAssociationRecord> = [
new RocketChatAssociationRecord(RocketChatAssociationModel.MISC, 'message'),
await persis.removeByAssociations(associations);