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