]> Untitled Git - lemmy-ui.git/blob - src/shared/services/WebSocketService.ts
Adding support for join messages. Fixes #8
[lemmy-ui.git] / src / shared / services / WebSocketService.ts
1 import { wsUri } from '../env';
2 import {
3   LemmyWebsocket,
4   LoginForm,
5   RegisterForm,
6   CommunityForm,
7   DeleteCommunityForm,
8   RemoveCommunityForm,
9   PostForm,
10   DeletePostForm,
11   RemovePostForm,
12   LockPostForm,
13   StickyPostForm,
14   SavePostForm,
15   CommentForm,
16   DeleteCommentForm,
17   RemoveCommentForm,
18   MarkCommentAsReadForm,
19   SaveCommentForm,
20   CommentLikeForm,
21   GetPostForm,
22   GetPostsForm,
23   CreatePostLikeForm,
24   GetCommunityForm,
25   FollowCommunityForm,
26   GetFollowedCommunitiesForm,
27   GetUserDetailsForm,
28   ListCommunitiesForm,
29   GetModlogForm,
30   BanFromCommunityForm,
31   AddModToCommunityForm,
32   TransferCommunityForm,
33   AddAdminForm,
34   TransferSiteForm,
35   BanUserForm,
36   SiteForm,
37   UserView,
38   GetRepliesForm,
39   GetUserMentionsForm,
40   MarkUserMentionAsReadForm,
41   SearchForm,
42   UserSettingsForm,
43   DeleteAccountForm,
44   PasswordResetForm,
45   PasswordChangeForm,
46   PrivateMessageForm,
47   EditPrivateMessageForm,
48   DeletePrivateMessageForm,
49   MarkPrivateMessageAsReadForm,
50   GetPrivateMessagesForm,
51   GetCommentsForm,
52   UserJoinForm,
53   GetSiteConfig,
54   GetSiteForm,
55   SiteConfigForm,
56   MarkAllAsReadForm,
57   WebSocketJsonResponse,
58   CommunityJoinForm,
59   PostJoinForm,
60 } from 'lemmy-js-client';
61 import { UserService } from './';
62 import { i18n } from '../i18next';
63 import { toast, isBrowser } from '../utils';
64 import { Observable } from 'rxjs';
65 import { share } from 'rxjs/operators';
66 import WebSocket from 'isomorphic-ws';
67 import {
68   Options as WSOptions,
69   default as ReconnectingWebSocket,
70 } from 'reconnecting-websocket';
71
72 export class WebSocketService {
73   private static _instance: WebSocketService;
74   public ws: ReconnectingWebSocket;
75   public wsOptions: WSOptions = {
76     WebSocket: WebSocket,
77     connectionTimeout: 1000,
78     maxRetries: 10,
79   };
80   public subject: Observable<any>;
81
82   public admins: UserView[];
83   public banned: UserView[];
84   private client = new LemmyWebsocket();
85
86   private constructor() {
87     this.ws = new ReconnectingWebSocket(wsUri, [], this.wsOptions);
88     let firstConnect = true;
89
90     this.subject = new Observable((obs: any) => {
91       this.ws.onmessage = e => {
92         obs.next(JSON.parse(e.data));
93       };
94       this.ws.onopen = () => {
95         console.log(`Connected to ${wsUri}`);
96
97         if (!firstConnect) {
98           let res: WebSocketJsonResponse = {
99             reconnect: true,
100           };
101           obs.next(res);
102         }
103
104         firstConnect = false;
105       };
106     }).pipe(share());
107   }
108
109   public static get Instance() {
110     return this._instance || (this._instance = new this());
111   }
112
113   public userJoin() {
114     let form: UserJoinForm = { auth: UserService.Instance.auth };
115     this.ws.send(this.client.userJoin(form));
116   }
117
118   public postJoin(form: PostJoinForm) {
119     this.ws.send(this.client.postJoin(form));
120   }
121
122   public communityJoin(form: CommunityJoinForm) {
123     this.ws.send(this.client.communityJoin(form));
124   }
125
126   public login(form: LoginForm) {
127     this.ws.send(this.client.login(form));
128   }
129
130   public register(form: RegisterForm) {
131     this.ws.send(this.client.register(form));
132   }
133
134   public getCaptcha() {
135     this.ws.send(this.client.getCaptcha());
136   }
137
138   public createCommunity(form: CommunityForm) {
139     this.setAuth(form); // TODO all these setauths at some point would be good to make required
140     this.ws.send(this.client.createCommunity(form));
141   }
142
143   public editCommunity(form: CommunityForm) {
144     this.setAuth(form);
145     this.ws.send(this.client.editCommunity(form));
146   }
147
148   public deleteCommunity(form: DeleteCommunityForm) {
149     this.setAuth(form);
150     this.ws.send(this.client.deleteCommunity(form));
151   }
152
153   public removeCommunity(form: RemoveCommunityForm) {
154     this.setAuth(form);
155     this.ws.send(this.client.removeCommunity(form));
156   }
157
158   public followCommunity(form: FollowCommunityForm) {
159     this.setAuth(form);
160     this.ws.send(this.client.followCommunity(form));
161   }
162
163   public listCommunities(form: ListCommunitiesForm) {
164     this.setAuth(form, false);
165     this.ws.send(this.client.listCommunities(form));
166   }
167
168   public getFollowedCommunities() {
169     let form: GetFollowedCommunitiesForm = { auth: UserService.Instance.auth };
170     this.ws.send(this.client.getFollowedCommunities(form));
171   }
172
173   public listCategories() {
174     this.ws.send(this.client.listCategories());
175   }
176
177   public createPost(form: PostForm) {
178     this.setAuth(form);
179     this.ws.send(this.client.createPost(form));
180   }
181
182   public getPost(form: GetPostForm) {
183     this.setAuth(form, false);
184     this.ws.send(this.client.getPost(form));
185   }
186
187   public getCommunity(form: GetCommunityForm) {
188     this.setAuth(form, false);
189     this.ws.send(this.client.getCommunity(form));
190   }
191
192   public createComment(form: CommentForm) {
193     this.setAuth(form);
194     this.ws.send(this.client.createComment(form));
195   }
196
197   public editComment(form: CommentForm) {
198     this.setAuth(form);
199     this.ws.send(this.client.editComment(form));
200   }
201
202   public deleteComment(form: DeleteCommentForm) {
203     this.setAuth(form);
204     this.ws.send(this.client.deleteComment(form));
205   }
206
207   public removeComment(form: RemoveCommentForm) {
208     this.setAuth(form);
209     this.ws.send(this.client.removeComment(form));
210   }
211
212   public markCommentAsRead(form: MarkCommentAsReadForm) {
213     this.setAuth(form);
214     this.ws.send(this.client.markCommentAsRead(form));
215   }
216
217   public likeComment(form: CommentLikeForm) {
218     this.setAuth(form);
219     this.ws.send(this.client.likeComment(form));
220   }
221
222   public saveComment(form: SaveCommentForm) {
223     this.setAuth(form);
224     this.ws.send(this.client.saveComment(form));
225   }
226
227   public getPosts(form: GetPostsForm) {
228     this.setAuth(form, false);
229     this.ws.send(this.client.getPosts(form));
230   }
231
232   public getComments(form: GetCommentsForm) {
233     this.setAuth(form, false);
234     this.ws.send(this.client.getComments(form));
235   }
236
237   public likePost(form: CreatePostLikeForm) {
238     this.setAuth(form);
239     this.ws.send(this.client.likePost(form));
240   }
241
242   public editPost(form: PostForm) {
243     this.setAuth(form);
244     this.ws.send(this.client.editPost(form));
245   }
246
247   public deletePost(form: DeletePostForm) {
248     this.setAuth(form);
249     this.ws.send(this.client.deletePost(form));
250   }
251
252   public removePost(form: RemovePostForm) {
253     this.setAuth(form);
254     this.ws.send(this.client.removePost(form));
255   }
256
257   public lockPost(form: LockPostForm) {
258     this.setAuth(form);
259     this.ws.send(this.client.lockPost(form));
260   }
261
262   public stickyPost(form: StickyPostForm) {
263     this.setAuth(form);
264     this.ws.send(this.client.stickyPost(form));
265   }
266
267   public savePost(form: SavePostForm) {
268     this.setAuth(form);
269     this.ws.send(this.client.savePost(form));
270   }
271
272   public banFromCommunity(form: BanFromCommunityForm) {
273     this.setAuth(form);
274     this.ws.send(this.client.banFromCommunity(form));
275   }
276
277   public addModToCommunity(form: AddModToCommunityForm) {
278     this.setAuth(form);
279     this.ws.send(this.client.addModToCommunity(form));
280   }
281
282   public transferCommunity(form: TransferCommunityForm) {
283     this.setAuth(form);
284     this.ws.send(this.client.transferCommunity(form));
285   }
286
287   public transferSite(form: TransferSiteForm) {
288     this.setAuth(form);
289     this.ws.send(this.client.transferSite(form));
290   }
291
292   public banUser(form: BanUserForm) {
293     this.setAuth(form);
294     this.ws.send(this.client.banUser(form));
295   }
296
297   public addAdmin(form: AddAdminForm) {
298     this.setAuth(form);
299     this.ws.send(this.client.addAdmin(form));
300   }
301
302   public getUserDetails(form: GetUserDetailsForm) {
303     this.setAuth(form, false);
304     this.ws.send(this.client.getUserDetails(form));
305   }
306
307   public getReplies(form: GetRepliesForm) {
308     this.setAuth(form);
309     this.ws.send(this.client.getReplies(form));
310   }
311
312   public getUserMentions(form: GetUserMentionsForm) {
313     this.setAuth(form);
314     this.ws.send(this.client.getUserMentions(form));
315   }
316
317   public markUserMentionAsRead(form: MarkUserMentionAsReadForm) {
318     this.setAuth(form);
319     this.ws.send(this.client.markUserMentionAsRead(form));
320   }
321
322   public getModlog(form: GetModlogForm) {
323     this.ws.send(this.client.getModlog(form));
324   }
325
326   public createSite(form: SiteForm) {
327     this.setAuth(form);
328     this.ws.send(this.client.createSite(form));
329   }
330
331   public editSite(form: SiteForm) {
332     this.setAuth(form);
333     this.ws.send(this.client.editSite(form));
334   }
335
336   public getSite(form: GetSiteForm = {}) {
337     this.setAuth(form, false);
338     this.ws.send(this.client.getSite(form));
339   }
340
341   public getSiteConfig() {
342     let form: GetSiteConfig = {};
343     this.setAuth(form);
344     this.ws.send(this.client.getSiteConfig(form));
345   }
346
347   public search(form: SearchForm) {
348     this.setAuth(form, false);
349     this.ws.send(this.client.search(form));
350   }
351
352   public markAllAsRead() {
353     let form: MarkAllAsReadForm;
354     this.setAuth(form);
355     this.ws.send(this.client.markAllAsRead(form));
356   }
357
358   public saveUserSettings(form: UserSettingsForm) {
359     this.setAuth(form);
360     this.ws.send(this.client.saveUserSettings(form));
361   }
362
363   public deleteAccount(form: DeleteAccountForm) {
364     this.setAuth(form);
365     this.ws.send(this.client.deleteAccount(form));
366   }
367
368   public passwordReset(form: PasswordResetForm) {
369     this.ws.send(this.client.passwordReset(form));
370   }
371
372   public passwordChange(form: PasswordChangeForm) {
373     this.ws.send(this.client.passwordChange(form));
374   }
375
376   public createPrivateMessage(form: PrivateMessageForm) {
377     this.setAuth(form);
378     this.ws.send(this.client.createPrivateMessage(form));
379   }
380
381   public editPrivateMessage(form: EditPrivateMessageForm) {
382     this.setAuth(form);
383     this.ws.send(this.client.editPrivateMessage(form));
384   }
385
386   public deletePrivateMessage(form: DeletePrivateMessageForm) {
387     this.setAuth(form);
388     this.ws.send(this.client.deletePrivateMessage(form));
389   }
390
391   public markPrivateMessageAsRead(form: MarkPrivateMessageAsReadForm) {
392     this.setAuth(form);
393     this.ws.send(this.client.markPrivateMessageAsRead(form));
394   }
395
396   public getPrivateMessages(form: GetPrivateMessagesForm) {
397     this.setAuth(form);
398     this.ws.send(this.client.getPrivateMessages(form));
399   }
400
401   public saveSiteConfig(form: SiteConfigForm) {
402     this.setAuth(form);
403     this.ws.send(this.client.saveSiteConfig(form));
404   }
405
406   public setAuth(obj: any, throwErr: boolean = true) {
407     obj.auth = UserService.Instance.auth;
408     if (obj.auth == null && throwErr) {
409       toast(i18n.t('not_logged_in'), 'danger');
410       throw 'Not logged in';
411     }
412   }
413 }
414
415 if (isBrowser()) {
416   window.onbeforeunload = () => {
417     WebSocketService.Instance.ws.close();
418   };
419 }