mirror of
https://github.com/stoatchat/javascript-client-sdk.git
synced 2026-07-20 01:23:33 -04:00
Add relationship methods.
This commit is contained in:
+22
-5
@@ -3,8 +3,8 @@ import axios, { AxiosRequestConfig } from 'axios';
|
||||
import { defaultsDeep } from 'lodash';
|
||||
|
||||
import { EventEmitter } from 'events';
|
||||
import { User } from './objects';
|
||||
import { Request, Response, Account } from './api';
|
||||
import { Channel, User } from './objects';
|
||||
import { Request, Account, Users } from './api';
|
||||
|
||||
const API_URL = "http://86.11.153.158:5500/api";
|
||||
const WS_URI = "ws://86.11.153.158:9999"
|
||||
@@ -36,7 +36,7 @@ export class Client extends EventEmitter {
|
||||
user?: User;
|
||||
|
||||
users: Map<string, User>;
|
||||
channels: Map<string, undefined>;
|
||||
channels: Map<string, Channel>;
|
||||
|
||||
autoReconnect: boolean;
|
||||
constructor(config?: { autoReconnect?: boolean }) {
|
||||
@@ -103,7 +103,7 @@ export class Client extends EventEmitter {
|
||||
}
|
||||
}
|
||||
|
||||
async $request<T extends Request>(method: 'GET' | 'POST', url: string, data?: T, config?: AxiosRequestConfig) {
|
||||
async $request<T extends Request>(method: AxiosRequestConfig['method'], url: string, data?: T, config?: AxiosRequestConfig) {
|
||||
return await
|
||||
axios(
|
||||
defaultsDeep(
|
||||
@@ -120,7 +120,7 @@ export class Client extends EventEmitter {
|
||||
)
|
||||
}
|
||||
|
||||
async $req<T extends Request, R extends Response>(method: 'GET' | 'POST', url: string, data?: T, config?: AxiosRequestConfig): Promise<R> {
|
||||
async $req<T extends Request, R>(method: AxiosRequestConfig['method'], url: string, data?: T, config?: AxiosRequestConfig): Promise<R> {
|
||||
return (await this.$request(method, url, data, config)).data;
|
||||
}
|
||||
|
||||
@@ -144,4 +144,21 @@ export class Client extends EventEmitter {
|
||||
this.emit('error', err);
|
||||
}
|
||||
}
|
||||
|
||||
async lookup(query: Users.LookupRequest) {
|
||||
let users = [];
|
||||
for (let x of await this.$req<Users.LookupRequest, Users.LookupResponse>('POST', '/users/lookup', query)) {
|
||||
users.push(await User.from(x.id, this, x));
|
||||
}
|
||||
|
||||
return users;
|
||||
}
|
||||
|
||||
findUser(id: string) {
|
||||
return User.from(id, this);
|
||||
}
|
||||
|
||||
findChannel(id: string) {
|
||||
return Channel.from(id, this);
|
||||
}
|
||||
}
|
||||
|
||||
+2
-4
@@ -34,12 +34,10 @@ export type RawMessage = {
|
||||
|
||||
export namespace Channels {
|
||||
// GET /:id
|
||||
export type Channel = RawChannel & Response;
|
||||
export type ChannelResponse = RawChannel;
|
||||
|
||||
// GET /:id/messages
|
||||
export interface MessagesResponse extends Response {
|
||||
[index: number]: RawMessage
|
||||
}
|
||||
export type MessagesResponse = RawMessage[];
|
||||
|
||||
// POST /:id/messages
|
||||
export interface SendMessageRequest extends Request {
|
||||
|
||||
+10
-17
@@ -24,34 +24,27 @@ export namespace Users {
|
||||
username?: string,
|
||||
}
|
||||
|
||||
export interface LookupResponse extends Response {
|
||||
[index: number]: {
|
||||
id: string,
|
||||
username: string,
|
||||
}
|
||||
}
|
||||
export type LookupResponse = UserResponse[];
|
||||
|
||||
// GET /@me/friend
|
||||
export interface FriendsResponse extends Response {
|
||||
[index: number]: {
|
||||
id: string,
|
||||
status: Relationship,
|
||||
}
|
||||
}
|
||||
export type FriendsResponse = {
|
||||
id: string,
|
||||
status: Relationship,
|
||||
}[];
|
||||
|
||||
// GET /@me/dms
|
||||
export interface DMsResponse extends Response {
|
||||
[index: number]: RawChannel,
|
||||
}
|
||||
export type DMsResponse = RawChannel[];
|
||||
|
||||
// GET /:id/friend
|
||||
export interface FriendResponse extends Response {
|
||||
export interface FriendResponse {
|
||||
id: string,
|
||||
status: Relationship,
|
||||
}
|
||||
|
||||
// PUT /:id/friend
|
||||
export interface AddFriendResponse extends Response { }
|
||||
export interface AddFriendResponse extends Response {
|
||||
status: Relationship,
|
||||
}
|
||||
|
||||
// DELETE /:id/friend
|
||||
export interface RemoveFriendResponse extends Response { }
|
||||
|
||||
@@ -0,0 +1,42 @@
|
||||
import { Channels, ChannelType } from '../api';
|
||||
import { Client } from '../Client';
|
||||
import { User } from './User';
|
||||
|
||||
export class Channel {
|
||||
client: Client;
|
||||
|
||||
id: string;
|
||||
type: ChannelType;
|
||||
|
||||
recipients?: User[];
|
||||
|
||||
constructor(client: Client, data: Channels.ChannelResponse) {
|
||||
this.client = client;
|
||||
this.update(data);
|
||||
}
|
||||
|
||||
update(data: Channels.ChannelResponse) {
|
||||
if (data.type !== ChannelType.GUILDCHANNEL) {
|
||||
let ids = data.recipients;
|
||||
delete data.recipients;
|
||||
ids.map(x => User.from(x, this.client));
|
||||
}
|
||||
|
||||
Object.assign(this, data);
|
||||
}
|
||||
|
||||
static async from(id: string, client: Client, data?: Channels.ChannelResponse) {
|
||||
let channel;
|
||||
if (client.channels.has(id)) {
|
||||
channel = client.channels.get(id) as Channel;
|
||||
data && channel.update(data);
|
||||
} else if (data) {
|
||||
channel = new Channel(client, data);
|
||||
} else {
|
||||
channel = new Channel(client, await client.$req<Request, Channels.ChannelResponse>('GET', '/channels/' + id));
|
||||
}
|
||||
|
||||
client.channels.set(id, channel);
|
||||
return channel;
|
||||
}
|
||||
}
|
||||
+33
-10
@@ -1,4 +1,4 @@
|
||||
import { Users } from '../api';
|
||||
import { Users, Relationship } from '../api';
|
||||
import { Client } from '../Client';
|
||||
|
||||
export class User {
|
||||
@@ -7,20 +7,17 @@ export class User {
|
||||
id: string;
|
||||
username: string;
|
||||
|
||||
relationship?: Relationship;
|
||||
|
||||
email?: string;
|
||||
verified?: boolean;
|
||||
|
||||
constructor(client: Client, data: Users.UserResponse) {
|
||||
this.client = client;
|
||||
|
||||
this.id = data.id;
|
||||
this.username = data.username;
|
||||
|
||||
this.email = data.email;
|
||||
this.verified = data.verified;
|
||||
this.$update(data);
|
||||
}
|
||||
|
||||
update(data: Users.UserResponse) {
|
||||
$update(data: Users.UserResponse) {
|
||||
Object.assign(this, data);
|
||||
}
|
||||
|
||||
@@ -28,14 +25,40 @@ export class User {
|
||||
let user;
|
||||
if (client.users.has(id)) {
|
||||
user = client.users.get(id) as User;
|
||||
data && user?.update(data);
|
||||
data && user.$update(data);
|
||||
} else if (data) {
|
||||
user = new User(client, data);
|
||||
} else {
|
||||
user = new User(client, await client.$req<Request, Users.UserResponse>('GET', '/users/@me'));
|
||||
user = new User(client, await client.$req<Request, Users.UserResponse>('GET', '/users/' + id));
|
||||
}
|
||||
|
||||
client.users.set(id, user);
|
||||
return user;
|
||||
}
|
||||
|
||||
async fetchRelationship() {
|
||||
let friend = await this.client.$req<Request, Users.FriendResponse>('GET', '/users/' + this.id + '/friend');
|
||||
this.relationship = friend.status;
|
||||
return friend.status;
|
||||
}
|
||||
|
||||
async addFriend() {
|
||||
let res = await this.client.$req<Request, Users.AddFriendResponse>('PUT', '/users/' + this.id + '/friend');
|
||||
|
||||
if (res.success) {
|
||||
this.relationship = res.status;
|
||||
}
|
||||
|
||||
return res;
|
||||
}
|
||||
|
||||
async removeFriend() {
|
||||
let res = await this.client.$req<Request, Users.RemoveFriendResponse>('DELETE', '/users/' + this.id + '/friend');
|
||||
|
||||
if (res.success) {
|
||||
this.relationship = Relationship.NONE;
|
||||
}
|
||||
|
||||
return res;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1 +1,2 @@
|
||||
export * from './User';
|
||||
export * from './Channel';
|
||||
|
||||
@@ -7,6 +7,11 @@ let client = new Client();
|
||||
|
||||
client.on('ready', () => {
|
||||
console.log(`Logged in as ${client.user?.username}!`);
|
||||
|
||||
client.lookup({ username: 'password' })
|
||||
.then(async x => {
|
||||
|
||||
})
|
||||
});
|
||||
|
||||
client.on('connected', () => {
|
||||
|
||||
+1
-1
@@ -26,7 +26,7 @@
|
||||
// "strictNullChecks": true, /* Enable strict null checks. */
|
||||
// "strictFunctionTypes": true, /* Enable strict checking of function types. */
|
||||
// "strictBindCallApply": true, /* Enable strict 'bind', 'call', and 'apply' methods on functions. */
|
||||
// "strictPropertyInitialization": true, /* Enable strict checking of property initialization in classes. */
|
||||
"strictPropertyInitialization": false, /* Enable strict checking of property initialization in classes. */
|
||||
// "noImplicitThis": true, /* Raise error on 'this' expressions with an implied 'any' type. */
|
||||
// "alwaysStrict": true, /* Parse in strict mode and emit "use strict" for each source file. */
|
||||
|
||||
|
||||
Reference in New Issue
Block a user