From 9095720105152b720dde799c9aa2afdc91caef92 Mon Sep 17 00:00:00 2001 From: Dustin Rouillard Date: Thu, 6 Aug 2020 05:31:39 -0700 Subject: [PATCH] Changes for 2.0.8 - Add .created (Date) to Houses, Rooms. and Users) - Added SnowflakeToDate utility function - Fixed House.owner --- lib/Client.ts | 44 ++++++++++++++++++++++------------------ lib/Collections/House.ts | 37 +++++++++++++++++++-------------- lib/Collections/Room.ts | 3 +++ lib/Collections/User.ts | 5 +++++ lib/Utils/Snowflake.ts | 8 ++++++++ package.json | 2 +- 6 files changed, 63 insertions(+), 36 deletions(-) create mode 100644 lib/Utils/Snowflake.ts diff --git a/lib/Client.ts b/lib/Client.ts index 4a1fd71..3776894 100644 --- a/lib/Client.ts +++ b/lib/Client.ts @@ -17,7 +17,7 @@ import { Member } from './Collections/Member'; import { Message } from './Collections/Message'; import { Room } from './Collections/Room'; import { APIBaseRoom, BaseRoom } from './Types/Room'; -import { APIMember } from 'Types/Member'; +import { APIMember } from './Types/Member'; export declare let rest: Rest; export declare let ws: WS; @@ -90,7 +90,6 @@ export class Client extends EventEmitter { }; this.emit('RAW', raw); - // Detect if it's the init event to set the user in the instance switch (e) { case 'INIT_STATE': { this.user = d.user; @@ -109,7 +108,8 @@ export class Client extends EventEmitter { recipients: new UserStore(client), typing: room.typing, last_message_id: room.last_message_id, - emoji: room.emoji + emoji: room.emoji, + messages: new MessageStore(client) }; room.recipients?.forEach((recipient) => { @@ -165,25 +165,29 @@ export class Client extends EventEmitter { return this.emit(e, d); } case 'MESSAGE_CREATE': { - const house = this.houses.resolve(d.house_id); - const room = this.rooms.resolve(d.room_id); - - const message = { - id: d.id, - room, - content: d.content, - timestamp: new Date(d.timestamp), - house: this.houses.resolve(d.house_id), - author: this.users.resolve(d.author_id), - member: house?.members?.resolve(d.author_id) - }; + try { + const house = this.houses.resolve(d.house_id); + const room = this.rooms.resolve(d.room_id); + + const message = { + id: d.id, + room, + content: d.content, + timestamp: new Date(d.timestamp), + house, + author: this.users.resolve(d.author_id), + member: house?.members?.resolve(d.author_id) + }; - if (room) room.messages.collect(message.id, message); + if (room) room.messages.collect(message.id, message); - this.messages.collect(d.id, message); + this.messages.collect(d.id, message); - this.emit('message', message); - return this.emit(e, message); + this.emit('message', message); + return this.emit(e, message); + } catch (error) { + console.log(error); + } } case 'MESSAGE_UPDATE': { this.emit('message_update', d); @@ -244,7 +248,7 @@ export class Client extends EventEmitter { this.houses.collect(house.id || '', { id: house.id, name: house.name, - owner: this.users.resolve(house.owner_id || ''), + owner: this.users.resolve(joinHouse.owner_id) || joinHouse.owner_id, icon: house.icon, members: house.members, rooms: house.rooms, diff --git a/lib/Collections/House.ts b/lib/Collections/House.ts index 09b4f67..08b57e2 100644 --- a/lib/Collections/House.ts +++ b/lib/Collections/House.ts @@ -5,18 +5,24 @@ import { MessageRoom } from '../Types/Room'; import { rest, Client } from '../Client'; import { Room } from './Room'; import { Member } from './Member'; +import { SnowflakeToDate } from '../Utils/Snowflake'; +import { User } from './User'; + +export declare interface House { + id: string; + name: string; + owner?: User; + icon?: string; + banner?: string; + synced?: boolean; + members?: Member; + rooms?: Room; + created: Date; +} // House class export class House extends BaseCollection { private client?: Client; - public rooms?: Room; - public id?: string; - public name?: string; - public owner_id?: string; - public icon?: string; - public banner?: string; - public synced?: boolean; - public members?: Member; constructor(client: Client) { super(); @@ -25,6 +31,7 @@ export class House extends BaseCollection { collect(key: string, value: any): T { if (typeof value == 'object') { + if (value.id && SnowflakeToDate(value.id)) value.created = SnowflakeToDate(value.id); value.createInvite = this.createInvite; value.leave = this.leave; value.createRoom = this.createRoom; @@ -34,14 +41,14 @@ export class House extends BaseCollection { } async create(name: string) { - let createHouse = await rest.post('/houses', { + const createHouse = await rest.post('/houses', { data: { name, icon: null } }); return createHouse; } async createInvite(uses: number, age: number) { - let createInvite = await rest.post(`/houses/${this.id}/invites`, { + const createInvite = await rest.post(`/houses/${this.id}/invites`, { data: { max_uses: uses, max_age: age } }); return createInvite.data.data; @@ -51,7 +58,7 @@ export class House extends BaseCollection { if (!name) throw 'missing_name'; // Create Room - let createRoom = await rest.post(`/houses/${this.id}/rooms`, { + const createRoom = await rest.post(`/houses/${this.id}/rooms`, { data: { name } }); if (!createRoom) throw 'failed_to_create_room'; @@ -74,10 +81,10 @@ export class House extends BaseCollection { } async Join(code: string) { - let getInvite = await rest.get(`/invites/${code}`); + const getInvite = await rest.get(`/invites/${code}`); if (!getInvite.data) throw 'failed_to_fetch_invite'; - let useInvite = await rest.post(`/invites/${code}`, {}); + const useInvite = await rest.post(`/invites/${code}`, {}); if (!useInvite.data) throw 'failed_to_use_invite'; // Fetch the house from the house store by the ID and return that with the callback @@ -93,12 +100,12 @@ export class House extends BaseCollection { async leave() { // Leave the house - let leaveHouse = await rest.delete(`/users/@me/houses/${this.id}`); + const leaveHouse = await rest.delete(`/users/@me/houses/${this.id}`); return leaveHouse; } async destroy() { - let deleteHouse = await rest.delete(`/houses/${this.id}`); + const deleteHouse = await rest.delete(`/houses/${this.id}`); return deleteHouse; } } diff --git a/lib/Collections/Room.ts b/lib/Collections/Room.ts index 7aa435d..493a268 100644 --- a/lib/Collections/Room.ts +++ b/lib/Collections/Room.ts @@ -6,6 +6,7 @@ import { House } from './House'; import { Voice } from '../Voice'; import { Message } from './Message'; import { User } from './User'; +import { SnowflakeToDate } from '../Utils/Snowflake'; export declare interface Room { id: string; @@ -17,6 +18,7 @@ export declare interface Room { author: User; messages: Message; recipients: User; + created: Date; } export class Room extends BaseCollection { @@ -35,6 +37,7 @@ export class Room extends BaseCollection { */ public collect(key: string, value: any): T { if (typeof value == 'object') { + if (value.id && SnowflakeToDate(value.id)) value.created = SnowflakeToDate(value.id); value.client = this.client; value.send = this.send; value.delete = this.delete; diff --git a/lib/Collections/User.ts b/lib/Collections/User.ts index 43b6e25..ea99ccc 100644 --- a/lib/Collections/User.ts +++ b/lib/Collections/User.ts @@ -3,12 +3,14 @@ import { BaseCollection } from './BaseCollection'; import { rest, Client } from '../Client'; import { APIMessage } from '../Types/Message'; import { Message } from './Message'; +import { SnowflakeToDate } from '../Utils/Snowflake'; export declare interface User { id: string; name: string; username: string; bot: boolean; + created: Date; } export class User extends BaseCollection { @@ -19,6 +21,9 @@ export class User extends BaseCollection { this.client = client; } collect = (key: string, value: any) => { + // If the value has an id add a .created attribute + if (typeof value == 'object' && value.id && SnowflakeToDate(value.id)) value.created = SnowflakeToDate(value.id); + return super.set(key, value); }; diff --git a/lib/Utils/Snowflake.ts b/lib/Utils/Snowflake.ts new file mode 100644 index 0000000..9b64d4e --- /dev/null +++ b/lib/Utils/Snowflake.ts @@ -0,0 +1,8 @@ +const HIVEN_EPOCH = 1562544e6; + +export function SnowflakeToDate(snowflake: string): Date { + const time = parseInt(snowflake, 10) / 4194304 + HIVEN_EPOCH; + const timestamp = !Number.isNaN(time) ? time : ''; + + return new Date(timestamp); +} diff --git a/package.json b/package.json index 683b9a1..f3d35ca 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "hiven", - "version": "2.0.7", + "version": "2.0.8", "description": "Hiven.js - TS/JS bot library for Hiven", "main": "dist/index.js", "scripts": {