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