Skip to content
This repository has been archived by the owner on Feb 14, 2022. It is now read-only.

Commit

Permalink
Changes for 2.0.8
Browse files Browse the repository at this point in the history
- Add .created (Date) to Houses, Rooms. and Users)
- Added SnowflakeToDate utility function
- Fixed House.owner
  • Loading branch information
Dustin Rouillard committed Aug 6, 2020
1 parent 328954c commit 9095720
Show file tree
Hide file tree
Showing 6 changed files with 63 additions and 36 deletions.
44 changes: 24 additions & 20 deletions lib/Client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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;
Expand All @@ -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) => {
Expand Down Expand Up @@ -165,25 +165,29 @@ export class Client extends EventEmitter {
return this.emit(e, d);
}
case 'MESSAGE_CREATE': {
const house = this.houses.resolve<HouseStore>(d.house_id);
const room = this.rooms.resolve<Room>(d.room_id);

const message = {
id: d.id,
room,
content: d.content,
timestamp: new Date(d.timestamp),
house: this.houses.resolve<House>(d.house_id),
author: this.users.resolve<User>(d.author_id),
member: house?.members?.resolve<Member>(d.author_id)
};
try {
const house = this.houses.resolve<HouseStore>(d.house_id);
const room = this.rooms.resolve<Room>(d.room_id);

const message = {
id: d.id,
room,
content: d.content,
timestamp: new Date(d.timestamp),
house,
author: this.users.resolve<User>(d.author_id),
member: house?.members?.resolve<Member>(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);
Expand Down Expand Up @@ -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,
Expand Down
37 changes: 22 additions & 15 deletions lib/Collections/House.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand All @@ -25,6 +31,7 @@ export class House extends BaseCollection {

collect<T = any>(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;
Expand All @@ -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;
Expand All @@ -51,7 +58,7 @@ export class House extends BaseCollection {
if (!name) throw 'missing_name';

// Create Room
let createRoom = await rest.post<MessageRoom>(`/houses/${this.id}/rooms`, {
const createRoom = await rest.post<MessageRoom>(`/houses/${this.id}/rooms`, {
data: { name }
});
if (!createRoom) throw 'failed_to_create_room';
Expand All @@ -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
Expand All @@ -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;
}
}
3 changes: 3 additions & 0 deletions lib/Collections/Room.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -17,6 +18,7 @@ export declare interface Room {
author: User;
messages: Message;
recipients: User;
created: Date;
}

export class Room extends BaseCollection {
Expand All @@ -35,6 +37,7 @@ export class Room extends BaseCollection {
*/
public collect<T = any>(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;
Expand Down
5 changes: 5 additions & 0 deletions lib/Collections/User.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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);
};

Expand Down
8 changes: 8 additions & 0 deletions lib/Utils/Snowflake.ts
Original file line number Diff line number Diff line change
@@ -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);
}
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -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": {
Expand Down

0 comments on commit 9095720

Please sign in to comment.