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