mirror of
https://github.com/stoatchat/javascript-client-sdk.git
synced 2026-07-20 20:16:06 -04:00
Fetch users when they join.
This commit is contained in:
+1
-1
@@ -1,6 +1,6 @@
|
||||
{
|
||||
"name": "revolt.js",
|
||||
"version": "4.0.0-alpha.10",
|
||||
"version": "4.0.0-alpha.11",
|
||||
"main": "dist/index.js",
|
||||
"repository": "https://gitlab.insrt.uk/revolt/revolt.js",
|
||||
"author": "Paul Makles <insrt.uk>",
|
||||
|
||||
+3
-1
@@ -34,6 +34,8 @@ export declare interface Client {
|
||||
on(event: 'message/delete', listener: (id: string) => void): this;
|
||||
}
|
||||
|
||||
export const SYSTEM_USER_ID = '00000000000000000000000000';
|
||||
|
||||
export class Client extends EventEmitter {
|
||||
private db?: Db;
|
||||
heartbeat: number;
|
||||
@@ -98,7 +100,7 @@ export class Client extends EventEmitter {
|
||||
await this.users.restore(user => { return { ...user, online: false } });
|
||||
await this.channels.restore();
|
||||
this.users.set({
|
||||
_id: '00000000000000000000000000',
|
||||
_id: SYSTEM_USER_ID,
|
||||
username: 'revolt'
|
||||
});
|
||||
if (user_id) this.user = this.users.get(user_id);
|
||||
|
||||
@@ -101,6 +101,12 @@ export namespace Channels {
|
||||
attachment?: Attachment,
|
||||
edited?: { $date: string }
|
||||
}
|
||||
|
||||
export type SystemMessage =
|
||||
| { type: "text"; content: string }
|
||||
| { type: "user_added"; id: string; by: string }
|
||||
| { type: "user_remove"; id: string; by: string }
|
||||
| { type: "user_left"; id: string };
|
||||
}
|
||||
|
||||
export type User = Users.User;
|
||||
|
||||
+1
-1
@@ -1,7 +1,7 @@
|
||||
export * from './Client';
|
||||
export { Channel, User, Message } from './api/objects';
|
||||
|
||||
export const LIBRARY_VERSION = '4.0.0-alpha.10';
|
||||
export const LIBRARY_VERSION = '4.0.0-alpha.11';
|
||||
|
||||
export const defaultConfig = {
|
||||
apiURL: 'https://api.revolt.chat',
|
||||
|
||||
@@ -14,6 +14,14 @@ export default class Channels extends Collection<Channel> {
|
||||
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);
|
||||
|
||||
+18
-2
@@ -1,8 +1,8 @@
|
||||
import WebSocket from 'isomorphic-ws';
|
||||
import { backOff } from 'exponential-backoff';
|
||||
|
||||
import { Client } from '..';
|
||||
import { Auth, User } from '../api/objects';
|
||||
import { Client, SYSTEM_USER_ID } from '..';
|
||||
import { Auth, Channels, User } from '../api/objects';
|
||||
import { ServerboundNotification, ClientboundNotification } from './notifications';
|
||||
|
||||
export class WebSocketClient {
|
||||
@@ -117,6 +117,20 @@ export class WebSocketClient {
|
||||
case 'Message': {
|
||||
if (!this.client.messages.includes(packet._id)) {
|
||||
this.client.messages.push(packet._id);
|
||||
|
||||
if (packet.author === SYSTEM_USER_ID) {
|
||||
let system = this.client.channels.tryParseSystemMessage(packet.content);
|
||||
switch (system.type) {
|
||||
case 'user_added':
|
||||
case 'user_remove':
|
||||
await this.client.users.fetch(system.by);
|
||||
case 'user_left':
|
||||
await this.client.users.fetch(system.id);
|
||||
}
|
||||
} else {
|
||||
await this.client.users.fetch(packet.author);
|
||||
}
|
||||
|
||||
this.client.emit('message', packet);
|
||||
}
|
||||
break;
|
||||
@@ -130,6 +144,8 @@ export class WebSocketClient {
|
||||
let channel = await this.client.channels.fetchMutable(packet.id);
|
||||
if (channel.channel_type !== 'Group') throw "Not a group channel.";
|
||||
let user_id = packet.user;
|
||||
await this.client.users.fetch(user_id);
|
||||
|
||||
channel.recipients = [
|
||||
...channel.recipients.filter(user => user !== user_id),
|
||||
user_id
|
||||
|
||||
Reference in New Issue
Block a user