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