mirror of
https://github.com/stoatchat/javascript-client-sdk.git
synced 2026-07-21 04:25:27 -04:00
124 lines
5.0 KiB
TypeScript
124 lines
5.0 KiB
TypeScript
import { Channel, Channels as ChannelsNS } from '../api/objects';
|
|
import { Route } from '../api/routes';
|
|
import Collection from './Collection';
|
|
import { Client } from '..';
|
|
import { ulid } from 'ulid';
|
|
|
|
export default class Channels extends Collection<Channel> {
|
|
constructor(client: Client) {
|
|
super(client, 'channels');
|
|
}
|
|
|
|
getRecipient(id: string) {
|
|
let channel = this.getThrow(id);
|
|
if (channel.channel_type !== 'DirectMessage') throw "Not a DirectMessage.";
|
|
return channel.recipients.find(user => user !== this.client.user?._id) as string;
|
|
}
|
|
|
|
tryParseSystemMessage(content: string): ChannelsNS.SystemMessage {
|
|
try {
|
|
return JSON.parse(content);
|
|
} catch (e) {
|
|
return { type: 'text', content }
|
|
}
|
|
}
|
|
|
|
async fetchMutable(id: string) {
|
|
if (this.map[id]) return this.get(id) as Channel;
|
|
let res = await this.client.req<'GET', '/channels/:id'>('GET', `/channels/${id}` as any);
|
|
|
|
// Fetch user information if this is the first time we are seeing this object.
|
|
// We shouldn't need to fetch for anything apart from groups.
|
|
if (res.channel_type === 'Group') {
|
|
for (let member of res.recipients) {
|
|
await this.client.users.fetch(member);
|
|
}
|
|
}
|
|
|
|
this.set(res);
|
|
return this.get(id) as Channel;
|
|
}
|
|
|
|
async fetch(id: string) {
|
|
return await this.fetchMutable(id) as Readonly<Channel>;
|
|
}
|
|
|
|
async delete(id: string, avoidRequest?: boolean) {
|
|
let channel = this.getMutable(id);
|
|
if (channel?.channel_type === 'SavedMessages') throw "Cannot delete Saved Messages.";
|
|
if (!avoidRequest)
|
|
await this.client.req<'DELETE', '/channels/:id'>('DELETE', `/channels/${id}` as any);
|
|
|
|
if (channel?.channel_type === 'DirectMessage') {
|
|
channel.active = false;
|
|
} else {
|
|
super.delete(id);
|
|
}
|
|
}
|
|
|
|
async createGroup(data: Route<'POST', '/channels/create'>["data"]) {
|
|
let res = await this.client.req('POST', `/channels/create`, data);
|
|
this.set(res);
|
|
return this.get(res._id) as Readonly<ChannelsNS.GroupChannel>;
|
|
}
|
|
|
|
async addMember(id: string, user_id: string) {
|
|
let channel = this.getThrow(id);
|
|
if (channel.channel_type !== 'Group') throw "Channel is not group channel.";
|
|
await this.client.req<'PUT', '/channels/:id/recipients/:id'>('PUT', `/channels/${id}/recipients/${user_id}` as any);
|
|
channel.recipients = [
|
|
...channel.recipients.filter(user => user !== user_id),
|
|
user_id
|
|
];
|
|
}
|
|
|
|
async removeMember(id: string, user_id: string) {
|
|
let channel = this.getThrow(id);
|
|
if (channel.channel_type !== 'Group') throw "Channel is not group channel.";
|
|
await this.client.req<'DELETE', '/channels/:id/recipients/:id'>('DELETE', `/channels/${id}/recipients/${user_id}` as any);
|
|
channel.recipients = channel.recipients
|
|
.filter(user => user !== user_id);
|
|
}
|
|
|
|
async sendMessage(id: string, data: string | (Omit<Route<'POST', '/channels/:id/messages'>["data"], 'nonce'> & { nonce?: string })) {
|
|
let msg: Route<'POST', '/channels/:id/messages'>["data"] = {
|
|
nonce: ulid(),
|
|
...(typeof data === 'string' ? { content: data } : data)
|
|
};
|
|
|
|
this.getThrow(id);
|
|
let message = await this.client.req<'POST', '/channels/:id/messages'>('POST', `/channels/${id}/messages` as any, msg);
|
|
if (!this.client.messages.includes(id)) {
|
|
this.client.messages.push(id);
|
|
this.client.emit('message', message);
|
|
}
|
|
return message;
|
|
}
|
|
|
|
async fetchMessage(id: string, message_id: string) {
|
|
return await this.client.req<'GET', '/channels/:id/messages/:id'>('GET', `/channels/${id}/messages/${message_id}` as any);
|
|
}
|
|
|
|
async fetchMessages(id: string, params: Route<'GET', '/channels/:id/messages'>["data"]) {
|
|
return await this.client.request<'GET', '/channels/:id/messages'>('GET', `/channels/${id}/messages` as any, { params });
|
|
}
|
|
|
|
async fetchStale(id: string, ids: string[]) {
|
|
return await this.client.req<'POST', '/channels/:id/messages/stale'>('POST', `/channels/${id}/messages/stale` as any, { ids });
|
|
}
|
|
|
|
async editMessage(id: string, message_id: string, data: Route<'PATCH', '/channels/:id/messages/:id'>["data"]) {
|
|
await this.client.req<'PATCH', '/channels/:id/messages/:id'>('PATCH', `/channels/${id}/messages/${message_id}` as any, data);
|
|
this.client.emit('message/edit', message_id, data);
|
|
}
|
|
|
|
async deleteMessage(id: string, message_id: string) {
|
|
await this.client.req<'DELETE', '/channels/:id/messages/:id'>('DELETE', `/channels/${id}/messages/${message_id}` as any);
|
|
this.client.emit('message/delete', message_id);
|
|
}
|
|
|
|
async joinCall(id: string) {
|
|
return await this.client.req<'POST', '/channels/:id/join_call'>('POST', `/channels/${id}/join_call` as any);
|
|
}
|
|
}
|