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