verdnatura-chat/app/lib/methods/getPermalinks.ts

39 lines
1005 B
TypeScript
Raw Normal View History

import log from './helpers/log';
2022-11-29 16:26:58 +00:00
import { IMessage, TSubscriptionModel } from '../../definitions';
import { store } from '../store/auxStore';
import { isGroupChat } from './helpers';
import { getRoom } from './getRoom';
type TRoomType = 'p' | 'c' | 'd';
2022-11-29 16:26:58 +00:00
export async function getPermalinkMessage(message: IMessage): Promise<string | null> {
let room: TSubscriptionModel;
try {
2022-11-29 16:26:58 +00:00
room = await getRoom(message.rid);
} catch (e) {
log(e);
return null;
}
const { server } = store.getState().server;
const roomType = {
p: 'group',
c: 'channel',
d: 'direct'
}[room.t as TRoomType];
return `${server}/${roomType}/${isGroupChat(room) ? room.rid : room.name}?msg=${message.id}`;
}
export function getPermalinkChannel(channel: TSubscriptionModel): string {
const { server } = store.getState().server;
const roomType = {
p: 'group',
c: 'channel',
d: 'direct'
};
// @ts-ignore - wrong SubscriptionType
const room = roomType[channel.t];
return `${server}/${room}/${channel.name}`;
}