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