-
Notifications
You must be signed in to change notification settings - Fork 24
/
api.ts
123 lines (101 loc) · 3.92 KB
/
api.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
import events from "events";
import { acceptContactRequest } from "./api/accept-contact-request";
import { declineContactRequest } from "./api/decline-contact-request";
import { getContact } from "./api/get-contact";
import { getConversation } from "./api/get-conversation";
import { getConversations } from "./api/get-conversations";
import { sendImage } from "./api/send-image";
import { sendMessage } from "./api/send-message";
import { setStatus } from "./api/set-status";
import { ContactsInterface, ContactsService } from "./contacts/contacts";
import * as api from "./interfaces/api/api";
import { Contact as _Contact } from "./interfaces/api/contact";
import { Context as ApiContext } from "./interfaces/api/context";
import { Conversation } from "./interfaces/api/conversation";
import * as apiEvents from "./interfaces/api/events";
import { HttpIo } from "./interfaces/http-io";
import { MessagesPoller } from "./polling/messages-poller";
import { Contact } from "./types/contact";
import { Invite } from "./types/invite";
export interface ApiEvents extends NodeJS.EventEmitter {
}
export class Api extends events.EventEmitter implements ApiEvents {
io: HttpIo;
context: ApiContext;
messagesPoller: MessagesPoller;
private readonly contactsService: ContactsInterface;
constructor(context: ApiContext, io: HttpIo) {
super();
this.context = context;
this.io = io;
this.messagesPoller = new MessagesPoller(this.io, this.context);
this.messagesPoller.on("error", (err: Error) => this.emit("error", err));
// tslint:disable-next-line:no-void-expression
this.messagesPoller.on("event-message", (ev: apiEvents.EventMessage) => this.handlePollingEvent(ev));
this.contactsService = new ContactsService(this.io);
}
async acceptContactRequest(contactUsername: string): Promise<this> {
await acceptContactRequest(this.io, this.context, contactUsername);
return this;
}
async declineContactRequest(contactUsername: string): Promise<this> {
await declineContactRequest(this.io, this.context, contactUsername);
return this;
}
async getContactInvites(): Promise<Invite[]> {
return this.contactsService.getInvites(this.context);
}
async getContact(contactId: string): Promise<_Contact> {
return getContact(this.io, this.context, contactId);
}
async getContacts(): Promise<Contact[]> {
return this.contactsService.getContacts(this.context);
}
async getConversation(conversationId: string): Promise<Conversation> {
return getConversation(this.io, this.context, conversationId);
}
async getConversations(): Promise<Conversation[]> {
return getConversations(this.io, this.context);
}
async sendMessage(message: api.NewMessage, conversationId: string): Promise<api.SendMessageResult> {
return sendMessage(this.io, this.context, message, conversationId);
}
async sendImage(message: api.NewImage, conversationId: string): Promise<api.SendMessageResult> {
return sendImage(this.io, this.context, message, conversationId);
}
getState(): ApiContext.Json {
return ApiContext.toJson(this.context);
}
async setStatus(status: api.Status): Promise<void> {
return setStatus(this.io, this.context, status);
}
/**
* Start polling and emitting events
*/
async listen(): Promise<this> {
this.messagesPoller.run();
return Promise.resolve(this);
}
/**
* Stop polling and emitting events
*/
async stopListening(): Promise<this> {
this.messagesPoller.stop();
return Promise.resolve(this);
}
protected handlePollingEvent(ev: apiEvents.EventMessage): void {
this.emit("event", ev);
if (ev.resource === null) {
return;
}
// Prevent infinite-loop (echo itself)
if (ev.resource.from.username === this.context.username) {
return;
}
if (ev.resource.type === "Text") {
this.emit("Text", ev.resource);
} else if (ev.resource.type === "RichText") {
this.emit("RichText", ev.resource);
}
}
}