polfan-server-js-client 0.1.99922 → 0.1.99924
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/.gitmodules +3 -3
- package/.idea/deployment.xml +1 -8
- package/.idea/workspace.xml +153 -0
- package/build/index.js +157 -8
- package/build/index.js.map +1 -1
- package/build/types/AbstractChatClient.d.ts +114 -108
- package/build/types/AbstractRestClient.d.ts +21 -21
- package/build/types/AuthClient.d.ts +16 -16
- package/build/types/EventTarget.d.ts +18 -18
- package/build/types/FilesClient.d.ts +15 -15
- package/build/types/IndexedObjectCollection.d.ts +56 -56
- package/build/types/Permissions.d.ts +77 -69
- package/build/types/WebApiChatClient.d.ts +30 -30
- package/build/types/WebSocketChatClient.d.ts +38 -38
- package/build/types/index.d.ts +9 -9
- package/build/types/state-tracker/AsyncUtils.d.ts +15 -15
- package/build/types/state-tracker/ChatStateTracker.d.ts +31 -26
- package/build/types/state-tracker/EmoticonsManager.d.ts +15 -0
- package/build/types/state-tracker/MessagesManager.d.ts +55 -55
- package/build/types/state-tracker/PermissionsManager.d.ts +42 -42
- package/build/types/state-tracker/RoomMessagesHistory.d.ts +17 -17
- package/build/types/state-tracker/RoomsManager.d.ts +53 -53
- package/build/types/state-tracker/SpacesManager.d.ts +52 -52
- package/build/types/state-tracker/TopicHistoryWindow.d.ts +75 -75
- package/build/types/state-tracker/functions.d.ts +2 -2
- package/build/types/types/src/index.d.ts +102 -95
- package/build/types/types/src/schemes/ChatLocation.d.ts +5 -5
- package/build/types/types/src/schemes/Emoticon.d.ts +8 -0
- package/build/types/types/src/schemes/Envelope.d.ts +5 -5
- package/build/types/types/src/schemes/FollowedTopic.d.ts +6 -6
- package/build/types/types/src/schemes/Message.d.ts +13 -13
- package/build/types/types/src/schemes/PermissionOverwritesValue.d.ts +4 -4
- package/build/types/types/src/schemes/Role.d.ts +6 -6
- package/build/types/types/src/schemes/Room.d.ts +16 -16
- package/build/types/types/src/schemes/RoomMember.d.ts +7 -7
- package/build/types/types/src/schemes/RoomSummary.d.ts +5 -5
- package/build/types/types/src/schemes/Space.d.ts +13 -13
- package/build/types/types/src/schemes/SpaceMember.d.ts +5 -5
- package/build/types/types/src/schemes/SpaceSummary.d.ts +7 -7
- package/build/types/types/src/schemes/Topic.d.ts +8 -8
- package/build/types/types/src/schemes/User.d.ts +8 -8
- package/build/types/types/src/schemes/UserState.d.ts +6 -6
- package/build/types/types/src/schemes/commands/Ack.d.ts +5 -5
- package/build/types/types/src/schemes/commands/AssignRole.d.ts +6 -6
- package/build/types/types/src/schemes/commands/CreateEmoticon.d.ts +5 -0
- package/build/types/types/src/schemes/commands/CreateMessage.d.ts +6 -6
- package/build/types/types/src/schemes/commands/CreateOwner.d.ts +5 -5
- package/build/types/types/src/schemes/commands/CreateRole.d.ts +5 -5
- package/build/types/types/src/schemes/commands/CreateRoom.d.ts +8 -8
- package/build/types/types/src/schemes/commands/CreateSpace.d.ts +3 -3
- package/build/types/types/src/schemes/commands/CreateTopic.d.ts +8 -8
- package/build/types/types/src/schemes/commands/DeassignRole.d.ts +6 -6
- package/build/types/types/src/schemes/commands/DeleteEmoticon.d.ts +3 -0
- package/build/types/types/src/schemes/commands/DeleteOwner.d.ts +5 -5
- package/build/types/types/src/schemes/commands/DeleteRole.d.ts +4 -4
- package/build/types/types/src/schemes/commands/DeleteRoom.d.ts +3 -3
- package/build/types/types/src/schemes/commands/DeleteSpace.d.ts +3 -3
- package/build/types/types/src/schemes/commands/DeleteTopic.d.ts +4 -4
- package/build/types/types/src/schemes/commands/FollowTopic.d.ts +4 -4
- package/build/types/types/src/schemes/commands/GetComputedPermissions.d.ts +4 -4
- package/build/types/types/src/schemes/commands/GetDiscoverableSpaces.d.ts +2 -2
- package/build/types/types/src/schemes/commands/GetEmoticons.d.ts +3 -0
- package/build/types/types/src/schemes/commands/GetFollowedTopics.d.ts +4 -4
- package/build/types/types/src/schemes/commands/GetMessages.d.ts +6 -6
- package/build/types/types/src/schemes/commands/GetOwners.d.ts +4 -4
- package/build/types/types/src/schemes/commands/GetPermissionOverwriteTargets.d.ts +4 -4
- package/build/types/types/src/schemes/commands/GetPermissionOverwrites.d.ts +6 -6
- package/build/types/types/src/schemes/commands/GetRoomMembers.d.ts +3 -3
- package/build/types/types/src/schemes/commands/GetSession.d.ts +2 -2
- package/build/types/types/src/schemes/commands/GetSpaceMembers.d.ts +3 -3
- package/build/types/types/src/schemes/commands/GetSpaceRooms.d.ts +3 -3
- package/build/types/types/src/schemes/commands/GetTopics.d.ts +4 -4
- package/build/types/types/src/schemes/commands/JoinRoom.d.ts +3 -3
- package/build/types/types/src/schemes/commands/JoinSpace.d.ts +3 -3
- package/build/types/types/src/schemes/commands/LeaveRoom.d.ts +3 -3
- package/build/types/types/src/schemes/commands/LeaveSpace.d.ts +3 -3
- package/build/types/types/src/schemes/commands/SetPermissionOverwrites.d.ts +8 -8
- package/build/types/types/src/schemes/commands/UnfollowTopic.d.ts +4 -4
- package/build/types/types/src/schemes/commands/UpdateRole.d.ts +7 -7
- package/build/types/types/src/schemes/commands/UpdateRoom.d.ts +6 -6
- package/build/types/types/src/schemes/commands/UpdateSpace.d.ts +11 -9
- package/build/types/types/src/schemes/commands/UpdateTopic.d.ts +5 -5
- package/build/types/types/src/schemes/events/Bye.d.ts +3 -3
- package/build/types/types/src/schemes/events/ComputedPermissions.d.ts +5 -5
- package/build/types/types/src/schemes/events/DiscoverableSpaces.d.ts +4 -4
- package/build/types/types/src/schemes/events/EmoticonDeleted.d.ts +4 -0
- package/build/types/types/src/schemes/events/Emoticons.d.ts +6 -0
- package/build/types/types/src/schemes/events/Error.d.ts +4 -4
- package/build/types/types/src/schemes/events/FollowedTopicUpdated.d.ts +4 -4
- package/build/types/types/src/schemes/events/FollowedTopics.d.ts +6 -6
- package/build/types/types/src/schemes/events/Messages.d.ts +6 -6
- package/build/types/types/src/schemes/events/NewEmoticon.d.ts +4 -0
- package/build/types/types/src/schemes/events/NewMessage.d.ts +4 -4
- package/build/types/types/src/schemes/events/NewRole.d.ts +5 -5
- package/build/types/types/src/schemes/events/NewRoom.d.ts +5 -5
- package/build/types/types/src/schemes/events/NewTopic.d.ts +5 -5
- package/build/types/types/src/schemes/events/Ok.d.ts +2 -2
- package/build/types/types/src/schemes/events/Owners.d.ts +8 -8
- package/build/types/types/src/schemes/events/PermissionOverwriteTargets.d.ts +10 -10
- package/build/types/types/src/schemes/events/PermissionOverwrites.d.ts +8 -8
- package/build/types/types/src/schemes/events/PermissionOverwritesUpdated.d.ts +8 -8
- package/build/types/types/src/schemes/events/RoleDeleted.d.ts +4 -4
- package/build/types/types/src/schemes/events/RoleUpdated.d.ts +5 -5
- package/build/types/types/src/schemes/events/RoomDeleted.d.ts +3 -3
- package/build/types/types/src/schemes/events/RoomJoined.d.ts +4 -4
- package/build/types/types/src/schemes/events/RoomLeft.d.ts +3 -3
- package/build/types/types/src/schemes/events/RoomMemberJoined.d.ts +5 -5
- package/build/types/types/src/schemes/events/RoomMemberLeft.d.ts +4 -4
- package/build/types/types/src/schemes/events/RoomMemberUpdated.d.ts +6 -6
- package/build/types/types/src/schemes/events/RoomMembers.d.ts +5 -5
- package/build/types/types/src/schemes/events/RoomUpdated.d.ts +4 -4
- package/build/types/types/src/schemes/events/Session.d.ts +7 -7
- package/build/types/types/src/schemes/events/SpaceDeleted.d.ts +3 -3
- package/build/types/types/src/schemes/events/SpaceJoined.d.ts +4 -4
- package/build/types/types/src/schemes/events/SpaceLeft.d.ts +3 -3
- package/build/types/types/src/schemes/events/SpaceMemberJoined.d.ts +5 -5
- package/build/types/types/src/schemes/events/SpaceMemberLeft.d.ts +4 -4
- package/build/types/types/src/schemes/events/SpaceMemberUpdated.d.ts +6 -6
- package/build/types/types/src/schemes/events/SpaceMembers.d.ts +5 -5
- package/build/types/types/src/schemes/events/SpaceRooms.d.ts +5 -5
- package/build/types/types/src/schemes/events/SpaceUpdated.d.ts +4 -4
- package/build/types/types/src/schemes/events/TopicDeleted.d.ts +4 -4
- package/build/types/types/src/schemes/events/TopicFollowed.d.ts +4 -4
- package/build/types/types/src/schemes/events/TopicUnfollowed.d.ts +4 -4
- package/build/types/types/src/schemes/events/TopicUpdated.d.ts +6 -6
- package/build/types/types/src/schemes/events/Topics.d.ts +6 -6
- package/build/types/types/src/schemes/events/UserUpdated.d.ts +4 -4
- package/jest.config.ts +199 -199
- package/package.json +1 -1
- package/src/AbstractChatClient.ts +17 -2
- package/src/AuthClient.ts +45 -45
- package/src/EventTarget.ts +48 -48
- package/src/IndexedObjectCollection.ts +225 -225
- package/src/Permissions.ts +2 -0
- package/src/WebApiChatClient.ts +94 -94
- package/src/WebSocketChatClient.ts +131 -131
- package/src/index.ts +22 -22
- package/src/state-tracker/ChatStateTracker.ts +46 -41
- package/src/state-tracker/EmoticonsManager.ts +67 -0
- package/src/state-tracker/RoomMessagesHistory.ts +73 -73
- package/src/types/src/index.ts +14 -0
- package/src/types/src/schemes/Emoticon.ts +9 -0
- package/src/types/src/schemes/commands/CreateEmoticon.ts +5 -0
- package/src/types/src/schemes/commands/DeleteEmoticon.ts +3 -0
- package/src/types/src/schemes/commands/GetEmoticons.ts +3 -0
- package/src/types/src/schemes/commands/UpdateSpace.ts +3 -0
- package/src/types/src/schemes/events/EmoticonDeleted.ts +4 -0
- package/src/types/src/schemes/events/Emoticons.ts +7 -0
- package/src/types/src/schemes/events/NewEmoticon.ts +5 -0
- package/.idea/inspectionProfiles/Project_Default.xml +0 -6
- package/.idea/modules.xml +0 -8
- package/.idea/php.xml +0 -18
- package/.idea/polfan-server-js-client.iml +0 -10
|
@@ -1,226 +1,226 @@
|
|
|
1
|
-
import {EventTarget, ObservableInterface} from "./EventTarget";
|
|
2
|
-
|
|
3
|
-
export class IndexedCollection<KeyT, ValueT> {
|
|
4
|
-
protected _items: Map<KeyT, ValueT> = new Map();
|
|
5
|
-
|
|
6
|
-
public constructor(items: [key: KeyT, value: ValueT][] = []) {
|
|
7
|
-
this.set(...items);
|
|
8
|
-
}
|
|
9
|
-
|
|
10
|
-
public get items(): Map<KeyT, ValueT> {
|
|
11
|
-
return this._items;
|
|
12
|
-
}
|
|
13
|
-
|
|
14
|
-
public get length(): number {
|
|
15
|
-
return this._items.size;
|
|
16
|
-
}
|
|
17
|
-
|
|
18
|
-
public set(...items: [KeyT, ValueT][]): void {
|
|
19
|
-
for (const item of items) {
|
|
20
|
-
this._items.set(item[0], item[1]);
|
|
21
|
-
}
|
|
22
|
-
}
|
|
23
|
-
|
|
24
|
-
public get(id: KeyT): ValueT | undefined {
|
|
25
|
-
return this.items.get(id);
|
|
26
|
-
}
|
|
27
|
-
|
|
28
|
-
public has(id: KeyT): boolean {
|
|
29
|
-
return this.items.has(id);
|
|
30
|
-
}
|
|
31
|
-
|
|
32
|
-
public delete(...ids: KeyT[]): void {
|
|
33
|
-
for (const id of ids) {
|
|
34
|
-
this.items.delete(id);
|
|
35
|
-
}
|
|
36
|
-
}
|
|
37
|
-
|
|
38
|
-
public deleteAll(): void {
|
|
39
|
-
this.items.clear();
|
|
40
|
-
}
|
|
41
|
-
|
|
42
|
-
public findBy(field: keyof ValueT, valueToFind: any, limit: number = null): IndexedCollection<KeyT, ValueT> {
|
|
43
|
-
const result = new IndexedCollection<KeyT, ValueT>();
|
|
44
|
-
let item;
|
|
45
|
-
while (!(item = this.items.entries().next().value).done) {
|
|
46
|
-
if (limit && result.length === limit) {
|
|
47
|
-
break;
|
|
48
|
-
}
|
|
49
|
-
if (item[1][field] === valueToFind) {
|
|
50
|
-
result.set(item);
|
|
51
|
-
}
|
|
52
|
-
}
|
|
53
|
-
return result;
|
|
54
|
-
}
|
|
55
|
-
|
|
56
|
-
public map<MapT = any>(callback: (item: ValueT, index: KeyT) => MapT): MapT[] {
|
|
57
|
-
return Array.from(this.items.entries()).map((entry) => callback(entry[1], entry[0]));
|
|
58
|
-
}
|
|
59
|
-
}
|
|
60
|
-
|
|
61
|
-
export class IndexedObjectCollection<T> {
|
|
62
|
-
protected _items: IndexedCollection<string, T>;
|
|
63
|
-
|
|
64
|
-
public constructor(
|
|
65
|
-
public readonly id: keyof T | ((item: T) => any),
|
|
66
|
-
items: T[] = [],
|
|
67
|
-
) {
|
|
68
|
-
this._items = new IndexedCollection<string, T>();
|
|
69
|
-
this.set(...items);
|
|
70
|
-
}
|
|
71
|
-
|
|
72
|
-
public get items(): T[] {
|
|
73
|
-
return Array.from(this._items.items.values());
|
|
74
|
-
}
|
|
75
|
-
|
|
76
|
-
public get length(): number {
|
|
77
|
-
return this._items.length;
|
|
78
|
-
}
|
|
79
|
-
|
|
80
|
-
public set(...items: T[]): void {
|
|
81
|
-
this._items.set(...(items.map(item => [this.getId(item), item] as [string, T])));
|
|
82
|
-
}
|
|
83
|
-
|
|
84
|
-
public get(id: any): T | undefined {
|
|
85
|
-
return this._items.get(id);
|
|
86
|
-
}
|
|
87
|
-
|
|
88
|
-
public getAt(index: number): T | undefined {
|
|
89
|
-
return this.items[index];
|
|
90
|
-
}
|
|
91
|
-
|
|
92
|
-
public has(id: any): boolean {
|
|
93
|
-
return this._items.has(id);
|
|
94
|
-
}
|
|
95
|
-
|
|
96
|
-
public delete(...ids: any[]): void {
|
|
97
|
-
this._items.delete(...ids);
|
|
98
|
-
}
|
|
99
|
-
|
|
100
|
-
public deleteAll(): void {
|
|
101
|
-
this._items.deleteAll();
|
|
102
|
-
}
|
|
103
|
-
|
|
104
|
-
public findBy(field: keyof T, valueToFind: any, limit: number = null): IndexedObjectCollection<T> {
|
|
105
|
-
const result = new IndexedObjectCollection<T>(this.id);
|
|
106
|
-
for (const value of this.items) {
|
|
107
|
-
if (limit && result.length === limit) {
|
|
108
|
-
break;
|
|
109
|
-
}
|
|
110
|
-
if (value[field] === valueToFind) {
|
|
111
|
-
result.set(value);
|
|
112
|
-
}
|
|
113
|
-
}
|
|
114
|
-
return result;
|
|
115
|
-
}
|
|
116
|
-
|
|
117
|
-
public map<MapT = any>(callback: (item: T, index: number, array: T[]) => MapT): MapT[] {
|
|
118
|
-
return this.items.map(callback);
|
|
119
|
-
}
|
|
120
|
-
|
|
121
|
-
protected getId(item: T): any {
|
|
122
|
-
return typeof this.id === 'function' ? this.id(item) : item[this.id];
|
|
123
|
-
}
|
|
124
|
-
}
|
|
125
|
-
|
|
126
|
-
interface ObservableCollectionEvent<KeyT> {
|
|
127
|
-
setItems?: KeyT[],
|
|
128
|
-
deletedItems?: KeyT[],
|
|
129
|
-
}
|
|
130
|
-
|
|
131
|
-
export class ObservableIndexedCollection<KeyT, ValueT> extends IndexedCollection<KeyT, ValueT> implements ObservableInterface {
|
|
132
|
-
protected eventTarget: EventTarget<ObservableCollectionEvent<KeyT>>;
|
|
133
|
-
|
|
134
|
-
public constructor(items: [key: KeyT, value: ValueT][] = []) {
|
|
135
|
-
super();
|
|
136
|
-
this.eventTarget = new EventTarget<ObservableCollectionEvent<KeyT>>();
|
|
137
|
-
this.set(...items);
|
|
138
|
-
}
|
|
139
|
-
|
|
140
|
-
public set(...items: [KeyT, ValueT][]) {
|
|
141
|
-
if (items.length) {
|
|
142
|
-
super.set(...items);
|
|
143
|
-
this.eventTarget.emit('change', {setItems: items.map(item => item[0])});
|
|
144
|
-
}
|
|
145
|
-
}
|
|
146
|
-
|
|
147
|
-
public delete(...ids: KeyT[]) {
|
|
148
|
-
if (ids.length) {
|
|
149
|
-
super.delete(...ids);
|
|
150
|
-
this.eventTarget.emit('change', {deletedItems: ids});
|
|
151
|
-
}
|
|
152
|
-
}
|
|
153
|
-
|
|
154
|
-
public deleteAll() {
|
|
155
|
-
if (this.length) {
|
|
156
|
-
const ids = this._items.keys();
|
|
157
|
-
super.deleteAll();
|
|
158
|
-
this.eventTarget.emit('change', {deletedItems: Array.from(ids)});
|
|
159
|
-
}
|
|
160
|
-
}
|
|
161
|
-
|
|
162
|
-
public on(eventName: 'change', handler: (ev?: ObservableCollectionEvent<KeyT>) => void): this {
|
|
163
|
-
this.eventTarget.on(eventName, handler);
|
|
164
|
-
return this;
|
|
165
|
-
}
|
|
166
|
-
|
|
167
|
-
public once(eventName: 'change', handler: (ev?: ObservableCollectionEvent<KeyT>) => void): this {
|
|
168
|
-
this.eventTarget.once(eventName, handler);
|
|
169
|
-
return this;
|
|
170
|
-
}
|
|
171
|
-
|
|
172
|
-
public off(eventName: string, handler: (ev?: ObservableCollectionEvent<KeyT>) => void): this {
|
|
173
|
-
this.eventTarget.off(eventName, handler);
|
|
174
|
-
return this;
|
|
175
|
-
}
|
|
176
|
-
}
|
|
177
|
-
|
|
178
|
-
export class ObservableIndexedObjectCollection<T> extends IndexedObjectCollection<T> implements ObservableInterface {
|
|
179
|
-
protected eventTarget: EventTarget<ObservableCollectionEvent<string>>;
|
|
180
|
-
|
|
181
|
-
public constructor(
|
|
182
|
-
public readonly id: keyof T | ((item: T) => string),
|
|
183
|
-
items: T[] = [],
|
|
184
|
-
) {
|
|
185
|
-
super(id);
|
|
186
|
-
this.eventTarget = new EventTarget();
|
|
187
|
-
this.set(...items);
|
|
188
|
-
}
|
|
189
|
-
|
|
190
|
-
public set(...items: T[]) {
|
|
191
|
-
if (items.length) {
|
|
192
|
-
super.set(...items);
|
|
193
|
-
this.eventTarget.emit('change', {setItems: items.map(item => this.getId(item))});
|
|
194
|
-
}
|
|
195
|
-
}
|
|
196
|
-
|
|
197
|
-
public delete(...ids: string[]) {
|
|
198
|
-
if (ids.length) {
|
|
199
|
-
super.delete(...ids);
|
|
200
|
-
this.eventTarget.emit('change', {deletedItems: ids});
|
|
201
|
-
}
|
|
202
|
-
}
|
|
203
|
-
|
|
204
|
-
public deleteAll() {
|
|
205
|
-
if (this.length) {
|
|
206
|
-
const ids = this._items.items.keys();
|
|
207
|
-
super.deleteAll();
|
|
208
|
-
this.eventTarget.emit('change', {deletedItems: Array.from(ids)});
|
|
209
|
-
}
|
|
210
|
-
}
|
|
211
|
-
|
|
212
|
-
public on(eventName: 'change', handler: (ev?: ObservableCollectionEvent<string>) => void): this {
|
|
213
|
-
this.eventTarget.on(eventName, handler);
|
|
214
|
-
return this;
|
|
215
|
-
}
|
|
216
|
-
|
|
217
|
-
public once(eventName: 'change', handler: (ev?: ObservableCollectionEvent<string>) => void): this {
|
|
218
|
-
this.eventTarget.once(eventName, handler);
|
|
219
|
-
return this;
|
|
220
|
-
}
|
|
221
|
-
|
|
222
|
-
public off(eventName: string, handler: (ev?: ObservableCollectionEvent<string>) => void): this {
|
|
223
|
-
this.eventTarget.off(eventName, handler);
|
|
224
|
-
return this;
|
|
225
|
-
}
|
|
1
|
+
import {EventTarget, ObservableInterface} from "./EventTarget";
|
|
2
|
+
|
|
3
|
+
export class IndexedCollection<KeyT, ValueT> {
|
|
4
|
+
protected _items: Map<KeyT, ValueT> = new Map();
|
|
5
|
+
|
|
6
|
+
public constructor(items: [key: KeyT, value: ValueT][] = []) {
|
|
7
|
+
this.set(...items);
|
|
8
|
+
}
|
|
9
|
+
|
|
10
|
+
public get items(): Map<KeyT, ValueT> {
|
|
11
|
+
return this._items;
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
public get length(): number {
|
|
15
|
+
return this._items.size;
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
public set(...items: [KeyT, ValueT][]): void {
|
|
19
|
+
for (const item of items) {
|
|
20
|
+
this._items.set(item[0], item[1]);
|
|
21
|
+
}
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
public get(id: KeyT): ValueT | undefined {
|
|
25
|
+
return this.items.get(id);
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
public has(id: KeyT): boolean {
|
|
29
|
+
return this.items.has(id);
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
public delete(...ids: KeyT[]): void {
|
|
33
|
+
for (const id of ids) {
|
|
34
|
+
this.items.delete(id);
|
|
35
|
+
}
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
public deleteAll(): void {
|
|
39
|
+
this.items.clear();
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
public findBy(field: keyof ValueT, valueToFind: any, limit: number = null): IndexedCollection<KeyT, ValueT> {
|
|
43
|
+
const result = new IndexedCollection<KeyT, ValueT>();
|
|
44
|
+
let item;
|
|
45
|
+
while (!(item = this.items.entries().next().value).done) {
|
|
46
|
+
if (limit && result.length === limit) {
|
|
47
|
+
break;
|
|
48
|
+
}
|
|
49
|
+
if (item[1][field] === valueToFind) {
|
|
50
|
+
result.set(item);
|
|
51
|
+
}
|
|
52
|
+
}
|
|
53
|
+
return result;
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
public map<MapT = any>(callback: (item: ValueT, index: KeyT) => MapT): MapT[] {
|
|
57
|
+
return Array.from(this.items.entries()).map((entry) => callback(entry[1], entry[0]));
|
|
58
|
+
}
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
export class IndexedObjectCollection<T> {
|
|
62
|
+
protected _items: IndexedCollection<string, T>;
|
|
63
|
+
|
|
64
|
+
public constructor(
|
|
65
|
+
public readonly id: keyof T | ((item: T) => any),
|
|
66
|
+
items: T[] = [],
|
|
67
|
+
) {
|
|
68
|
+
this._items = new IndexedCollection<string, T>();
|
|
69
|
+
this.set(...items);
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
public get items(): T[] {
|
|
73
|
+
return Array.from(this._items.items.values());
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
public get length(): number {
|
|
77
|
+
return this._items.length;
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
public set(...items: T[]): void {
|
|
81
|
+
this._items.set(...(items.map(item => [this.getId(item), item] as [string, T])));
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
public get(id: any): T | undefined {
|
|
85
|
+
return this._items.get(id);
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
public getAt(index: number): T | undefined {
|
|
89
|
+
return this.items[index];
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
public has(id: any): boolean {
|
|
93
|
+
return this._items.has(id);
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
public delete(...ids: any[]): void {
|
|
97
|
+
this._items.delete(...ids);
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
public deleteAll(): void {
|
|
101
|
+
this._items.deleteAll();
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
public findBy(field: keyof T, valueToFind: any, limit: number = null): IndexedObjectCollection<T> {
|
|
105
|
+
const result = new IndexedObjectCollection<T>(this.id);
|
|
106
|
+
for (const value of this.items) {
|
|
107
|
+
if (limit && result.length === limit) {
|
|
108
|
+
break;
|
|
109
|
+
}
|
|
110
|
+
if (value[field] === valueToFind) {
|
|
111
|
+
result.set(value);
|
|
112
|
+
}
|
|
113
|
+
}
|
|
114
|
+
return result;
|
|
115
|
+
}
|
|
116
|
+
|
|
117
|
+
public map<MapT = any>(callback: (item: T, index: number, array: T[]) => MapT): MapT[] {
|
|
118
|
+
return this.items.map(callback);
|
|
119
|
+
}
|
|
120
|
+
|
|
121
|
+
protected getId(item: T): any {
|
|
122
|
+
return typeof this.id === 'function' ? this.id(item) : item[this.id];
|
|
123
|
+
}
|
|
124
|
+
}
|
|
125
|
+
|
|
126
|
+
interface ObservableCollectionEvent<KeyT> {
|
|
127
|
+
setItems?: KeyT[],
|
|
128
|
+
deletedItems?: KeyT[],
|
|
129
|
+
}
|
|
130
|
+
|
|
131
|
+
export class ObservableIndexedCollection<KeyT, ValueT> extends IndexedCollection<KeyT, ValueT> implements ObservableInterface {
|
|
132
|
+
protected eventTarget: EventTarget<ObservableCollectionEvent<KeyT>>;
|
|
133
|
+
|
|
134
|
+
public constructor(items: [key: KeyT, value: ValueT][] = []) {
|
|
135
|
+
super();
|
|
136
|
+
this.eventTarget = new EventTarget<ObservableCollectionEvent<KeyT>>();
|
|
137
|
+
this.set(...items);
|
|
138
|
+
}
|
|
139
|
+
|
|
140
|
+
public set(...items: [KeyT, ValueT][]) {
|
|
141
|
+
if (items.length) {
|
|
142
|
+
super.set(...items);
|
|
143
|
+
this.eventTarget.emit('change', {setItems: items.map(item => item[0])});
|
|
144
|
+
}
|
|
145
|
+
}
|
|
146
|
+
|
|
147
|
+
public delete(...ids: KeyT[]) {
|
|
148
|
+
if (ids.length) {
|
|
149
|
+
super.delete(...ids);
|
|
150
|
+
this.eventTarget.emit('change', {deletedItems: ids});
|
|
151
|
+
}
|
|
152
|
+
}
|
|
153
|
+
|
|
154
|
+
public deleteAll() {
|
|
155
|
+
if (this.length) {
|
|
156
|
+
const ids = this._items.keys();
|
|
157
|
+
super.deleteAll();
|
|
158
|
+
this.eventTarget.emit('change', {deletedItems: Array.from(ids)});
|
|
159
|
+
}
|
|
160
|
+
}
|
|
161
|
+
|
|
162
|
+
public on(eventName: 'change', handler: (ev?: ObservableCollectionEvent<KeyT>) => void): this {
|
|
163
|
+
this.eventTarget.on(eventName, handler);
|
|
164
|
+
return this;
|
|
165
|
+
}
|
|
166
|
+
|
|
167
|
+
public once(eventName: 'change', handler: (ev?: ObservableCollectionEvent<KeyT>) => void): this {
|
|
168
|
+
this.eventTarget.once(eventName, handler);
|
|
169
|
+
return this;
|
|
170
|
+
}
|
|
171
|
+
|
|
172
|
+
public off(eventName: string, handler: (ev?: ObservableCollectionEvent<KeyT>) => void): this {
|
|
173
|
+
this.eventTarget.off(eventName, handler);
|
|
174
|
+
return this;
|
|
175
|
+
}
|
|
176
|
+
}
|
|
177
|
+
|
|
178
|
+
export class ObservableIndexedObjectCollection<T> extends IndexedObjectCollection<T> implements ObservableInterface {
|
|
179
|
+
protected eventTarget: EventTarget<ObservableCollectionEvent<string>>;
|
|
180
|
+
|
|
181
|
+
public constructor(
|
|
182
|
+
public readonly id: keyof T | ((item: T) => string),
|
|
183
|
+
items: T[] = [],
|
|
184
|
+
) {
|
|
185
|
+
super(id);
|
|
186
|
+
this.eventTarget = new EventTarget();
|
|
187
|
+
this.set(...items);
|
|
188
|
+
}
|
|
189
|
+
|
|
190
|
+
public set(...items: T[]) {
|
|
191
|
+
if (items.length) {
|
|
192
|
+
super.set(...items);
|
|
193
|
+
this.eventTarget.emit('change', {setItems: items.map(item => this.getId(item))});
|
|
194
|
+
}
|
|
195
|
+
}
|
|
196
|
+
|
|
197
|
+
public delete(...ids: string[]) {
|
|
198
|
+
if (ids.length) {
|
|
199
|
+
super.delete(...ids);
|
|
200
|
+
this.eventTarget.emit('change', {deletedItems: ids});
|
|
201
|
+
}
|
|
202
|
+
}
|
|
203
|
+
|
|
204
|
+
public deleteAll() {
|
|
205
|
+
if (this.length) {
|
|
206
|
+
const ids = this._items.items.keys();
|
|
207
|
+
super.deleteAll();
|
|
208
|
+
this.eventTarget.emit('change', {deletedItems: Array.from(ids)});
|
|
209
|
+
}
|
|
210
|
+
}
|
|
211
|
+
|
|
212
|
+
public on(eventName: 'change', handler: (ev?: ObservableCollectionEvent<string>) => void): this {
|
|
213
|
+
this.eventTarget.on(eventName, handler);
|
|
214
|
+
return this;
|
|
215
|
+
}
|
|
216
|
+
|
|
217
|
+
public once(eventName: 'change', handler: (ev?: ObservableCollectionEvent<string>) => void): this {
|
|
218
|
+
this.eventTarget.once(eventName, handler);
|
|
219
|
+
return this;
|
|
220
|
+
}
|
|
221
|
+
|
|
222
|
+
public off(eventName: string, handler: (ev?: ObservableCollectionEvent<string>) => void): this {
|
|
223
|
+
this.eventTarget.off(eventName, handler);
|
|
224
|
+
return this;
|
|
225
|
+
}
|
|
226
226
|
}
|
package/src/Permissions.ts
CHANGED
|
@@ -25,6 +25,8 @@ export class Permissions {
|
|
|
25
25
|
ManagePermissions: {value: 1 << 10, maxLayer: Layer.Topic},
|
|
26
26
|
CreateSpaceRooms: {value: 1 << 11, maxLayer: Layer.Space},
|
|
27
27
|
ManageSpaceRooms: {value: 1 << 12, maxLayer: Layer.Space},
|
|
28
|
+
CreateEmoticons: {value: 1 << 13, maxLayer: Layer.Global},
|
|
29
|
+
ManageEmoticon: {value: 1 << 14, maxLayer: Layer.Space},
|
|
28
30
|
};
|
|
29
31
|
|
|
30
32
|
public static getNames(): (keyof typeof this.list)[] {
|
package/src/WebApiChatClient.ts
CHANGED
|
@@ -1,95 +1,95 @@
|
|
|
1
|
-
import {AbstractChatClient, CommandResult, CommandsMap} from "./AbstractChatClient";
|
|
2
|
-
import {ObservableInterface} from "./EventTarget";
|
|
3
|
-
import {Envelope} from "./types/src";
|
|
4
|
-
|
|
5
|
-
export interface WebApiChatClientOptions {
|
|
6
|
-
url: string;
|
|
7
|
-
token?: string;
|
|
8
|
-
temporaryNick?: string;
|
|
9
|
-
attemptsToSend?: number;
|
|
10
|
-
attemptDelayMs?: number;
|
|
11
|
-
}
|
|
12
|
-
|
|
13
|
-
enum WebApiChatClientEvent {
|
|
14
|
-
message = 'message',
|
|
15
|
-
error = 'error',
|
|
16
|
-
destroy = 'destroy',
|
|
17
|
-
}
|
|
18
|
-
|
|
19
|
-
export class WebApiChatClient extends AbstractChatClient implements ObservableInterface {
|
|
20
|
-
public readonly Event = WebApiChatClientEvent;
|
|
21
|
-
|
|
22
|
-
protected sendStack: {data: any, attempts: number, lastTimeoutId: any}[];
|
|
23
|
-
|
|
24
|
-
public constructor(private readonly options: WebApiChatClientOptions) {
|
|
25
|
-
super();
|
|
26
|
-
if (!this.options.token && !this.options.temporaryNick) {
|
|
27
|
-
throw new Error('Token or temporary nick is required');
|
|
28
|
-
}
|
|
29
|
-
}
|
|
30
|
-
|
|
31
|
-
public async send<CommandType extends keyof CommandsMap>(commandType: CommandType, commandData: CommandsMap[CommandType][0]):
|
|
32
|
-
Promise<CommandResult<CommandsMap[CommandType][1]>> {
|
|
33
|
-
const envelope = this.createEnvelope(commandType, commandData);
|
|
34
|
-
this.sendStack.push({data: envelope, attempts: 0, lastTimeoutId: null});
|
|
35
|
-
this.makeApiCall(this.sendStack.length - 1);
|
|
36
|
-
return this.createPromiseFromCommandEnvelope(envelope);
|
|
37
|
-
}
|
|
38
|
-
|
|
39
|
-
public destroy(): void {
|
|
40
|
-
// Cancel all awaiting requests
|
|
41
|
-
this.sendStack.forEach(item => {
|
|
42
|
-
if (item.lastTimeoutId) {
|
|
43
|
-
clearTimeout(item.lastTimeoutId);
|
|
44
|
-
}
|
|
45
|
-
this.awaitingResponse.delete(item.data.ref);
|
|
46
|
-
});
|
|
47
|
-
this.sendStack = [];
|
|
48
|
-
this.emit(this.Event.destroy, false);
|
|
49
|
-
}
|
|
50
|
-
|
|
51
|
-
protected async onMessage(reqId: number, response: Response): Promise<void> {
|
|
52
|
-
this.sendStack.splice(reqId, 1);
|
|
53
|
-
const envelope: Envelope = await response.json();
|
|
54
|
-
this.handleIncomingEnvelope(envelope);
|
|
55
|
-
this.emit(envelope.type, envelope.data);
|
|
56
|
-
this.emit(this.Event.message, envelope);
|
|
57
|
-
}
|
|
58
|
-
|
|
59
|
-
protected onError(reqId: number, body: string): void {
|
|
60
|
-
if (this.sendStack[reqId].attempts >= (this.options.attemptsToSend ?? 10)) {
|
|
61
|
-
this.sendStack.splice(reqId, 1);
|
|
62
|
-
this.handleEnvelopeSendError(this.sendStack[reqId].data, new Error(
|
|
63
|
-
`Cannot send ${body}; aborting after reaching the maximum connection errors`
|
|
64
|
-
));
|
|
65
|
-
return;
|
|
66
|
-
}
|
|
67
|
-
this.sendStack[reqId].lastTimeoutId = setTimeout(
|
|
68
|
-
() => this.makeApiCall(reqId),
|
|
69
|
-
this.options.attemptDelayMs ?? 3000
|
|
70
|
-
);
|
|
71
|
-
}
|
|
72
|
-
|
|
73
|
-
protected makeApiCall(reqId: number): void {
|
|
74
|
-
this.sendStack[reqId].attempts++;
|
|
75
|
-
const bodyJson = JSON.stringify(this.sendStack[reqId].data);
|
|
76
|
-
const headers: any = {
|
|
77
|
-
'Content-Type': 'application/json',
|
|
78
|
-
Accept: 'application/json'
|
|
79
|
-
};
|
|
80
|
-
|
|
81
|
-
if (this.options.token) {
|
|
82
|
-
headers.Authorization = `Bearer ${this.options.token}`;
|
|
83
|
-
} else if (this.options.temporaryNick) {
|
|
84
|
-
headers.Authorization = `Temp ${this.options.temporaryNick}`;
|
|
85
|
-
}
|
|
86
|
-
|
|
87
|
-
fetch(this.options.url, {
|
|
88
|
-
headers,
|
|
89
|
-
body: bodyJson,
|
|
90
|
-
method: 'POST',
|
|
91
|
-
})
|
|
92
|
-
.then(response => this.onMessage(reqId, response))
|
|
93
|
-
.catch(() => this.onError(reqId, bodyJson));
|
|
94
|
-
}
|
|
1
|
+
import {AbstractChatClient, CommandResult, CommandsMap} from "./AbstractChatClient";
|
|
2
|
+
import {ObservableInterface} from "./EventTarget";
|
|
3
|
+
import {Envelope} from "./types/src";
|
|
4
|
+
|
|
5
|
+
export interface WebApiChatClientOptions {
|
|
6
|
+
url: string;
|
|
7
|
+
token?: string;
|
|
8
|
+
temporaryNick?: string;
|
|
9
|
+
attemptsToSend?: number;
|
|
10
|
+
attemptDelayMs?: number;
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
enum WebApiChatClientEvent {
|
|
14
|
+
message = 'message',
|
|
15
|
+
error = 'error',
|
|
16
|
+
destroy = 'destroy',
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
export class WebApiChatClient extends AbstractChatClient implements ObservableInterface {
|
|
20
|
+
public readonly Event = WebApiChatClientEvent;
|
|
21
|
+
|
|
22
|
+
protected sendStack: {data: any, attempts: number, lastTimeoutId: any}[];
|
|
23
|
+
|
|
24
|
+
public constructor(private readonly options: WebApiChatClientOptions) {
|
|
25
|
+
super();
|
|
26
|
+
if (!this.options.token && !this.options.temporaryNick) {
|
|
27
|
+
throw new Error('Token or temporary nick is required');
|
|
28
|
+
}
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
public async send<CommandType extends keyof CommandsMap>(commandType: CommandType, commandData: CommandsMap[CommandType][0]):
|
|
32
|
+
Promise<CommandResult<CommandsMap[CommandType][1]>> {
|
|
33
|
+
const envelope = this.createEnvelope(commandType, commandData);
|
|
34
|
+
this.sendStack.push({data: envelope, attempts: 0, lastTimeoutId: null});
|
|
35
|
+
this.makeApiCall(this.sendStack.length - 1);
|
|
36
|
+
return this.createPromiseFromCommandEnvelope(envelope);
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
public destroy(): void {
|
|
40
|
+
// Cancel all awaiting requests
|
|
41
|
+
this.sendStack.forEach(item => {
|
|
42
|
+
if (item.lastTimeoutId) {
|
|
43
|
+
clearTimeout(item.lastTimeoutId);
|
|
44
|
+
}
|
|
45
|
+
this.awaitingResponse.delete(item.data.ref);
|
|
46
|
+
});
|
|
47
|
+
this.sendStack = [];
|
|
48
|
+
this.emit(this.Event.destroy, false);
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
protected async onMessage(reqId: number, response: Response): Promise<void> {
|
|
52
|
+
this.sendStack.splice(reqId, 1);
|
|
53
|
+
const envelope: Envelope = await response.json();
|
|
54
|
+
this.handleIncomingEnvelope(envelope);
|
|
55
|
+
this.emit(envelope.type, envelope.data);
|
|
56
|
+
this.emit(this.Event.message, envelope);
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
protected onError(reqId: number, body: string): void {
|
|
60
|
+
if (this.sendStack[reqId].attempts >= (this.options.attemptsToSend ?? 10)) {
|
|
61
|
+
this.sendStack.splice(reqId, 1);
|
|
62
|
+
this.handleEnvelopeSendError(this.sendStack[reqId].data, new Error(
|
|
63
|
+
`Cannot send ${body}; aborting after reaching the maximum connection errors`
|
|
64
|
+
));
|
|
65
|
+
return;
|
|
66
|
+
}
|
|
67
|
+
this.sendStack[reqId].lastTimeoutId = setTimeout(
|
|
68
|
+
() => this.makeApiCall(reqId),
|
|
69
|
+
this.options.attemptDelayMs ?? 3000
|
|
70
|
+
);
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
protected makeApiCall(reqId: number): void {
|
|
74
|
+
this.sendStack[reqId].attempts++;
|
|
75
|
+
const bodyJson = JSON.stringify(this.sendStack[reqId].data);
|
|
76
|
+
const headers: any = {
|
|
77
|
+
'Content-Type': 'application/json',
|
|
78
|
+
Accept: 'application/json'
|
|
79
|
+
};
|
|
80
|
+
|
|
81
|
+
if (this.options.token) {
|
|
82
|
+
headers.Authorization = `Bearer ${this.options.token}`;
|
|
83
|
+
} else if (this.options.temporaryNick) {
|
|
84
|
+
headers.Authorization = `Temp ${this.options.temporaryNick}`;
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
fetch(this.options.url, {
|
|
88
|
+
headers,
|
|
89
|
+
body: bodyJson,
|
|
90
|
+
method: 'POST',
|
|
91
|
+
})
|
|
92
|
+
.then(response => this.onMessage(reqId, response))
|
|
93
|
+
.catch(() => this.onError(reqId, bodyJson));
|
|
94
|
+
}
|
|
95
95
|
}
|