]> Untitled Git - lemmy.git/blob - ui/src/services/WebSocketService.ts
Starting to work on websocket scopes.
[lemmy.git] / ui / src / services / WebSocketService.ts
1 import { wsUri } from '../env';
2 import {
3   LoginForm,
4   RegisterForm,
5   UserOperation,
6   CommunityForm,
7   PostForm,
8   SavePostForm,
9   CommentForm,
10   SaveCommentForm,
11   CommentLikeForm,
12   GetPostsForm,
13   CreatePostLikeForm,
14   FollowCommunityForm,
15   GetUserDetailsForm,
16   ListCommunitiesForm,
17   GetModlogForm,
18   BanFromCommunityForm,
19   AddModToCommunityForm,
20   TransferCommunityForm,
21   AddAdminForm,
22   TransferSiteForm,
23   BanUserForm,
24   SiteForm,
25   Site,
26   UserView,
27   GetRepliesForm,
28   GetUserMentionsForm,
29   EditUserMentionForm,
30   SearchForm,
31   UserSettingsForm,
32   DeleteAccountForm,
33   PasswordResetForm,
34   PasswordChangeForm,
35   PrivateMessageForm,
36   EditPrivateMessageForm,
37   GetPrivateMessagesForm,
38 } from '../interfaces';
39 import { webSocket } from 'rxjs/webSocket';
40 import { Subject } from 'rxjs';
41 import { retryWhen, delay } from 'rxjs/operators';
42 import { UserService } from './';
43 import { i18n } from '../i18next';
44 import { toast } from '../utils';
45
46 export class WebSocketService {
47   private static _instance: WebSocketService;
48   public subject: Subject<any>;
49
50   public site: Site;
51   public admins: Array<UserView>;
52   public banned: Array<UserView>;
53
54   private constructor() {
55     this.subject = webSocket(wsUri);
56
57     // Necessary to not keep reconnecting
58     this.subject
59       .pipe(
60         retryWhen(errors =>
61           errors.pipe(
62             delay(1000)
63             // take(999)
64           )
65         )
66       )
67       .subscribe();
68
69     console.log(`Connected to ${wsUri}`);
70
71     if (UserService.Instance.user) {
72     }
73   }
74
75   public static get Instance() {
76     return this._instance || (this._instance = new this());
77   }
78
79   public userJoin() {
80     this.wsSendWrapper(UserOperation.ListCategories, undefined);
81   }
82
83   public login(loginForm: LoginForm) {
84     this.subject.next(this.wsSendWrapper(UserOperation.Login, loginForm));
85   }
86
87   public register(registerForm: RegisterForm) {
88     this.subject.next(this.wsSendWrapper(UserOperation.Register, registerForm));
89   }
90
91   public createCommunity(communityForm: CommunityForm) {
92     this.setAuth(communityForm);
93     this.subject.next(
94       this.wsSendWrapper(UserOperation.CreateCommunity, communityForm)
95     );
96   }
97
98   public editCommunity(communityForm: CommunityForm) {
99     this.setAuth(communityForm);
100     this.subject.next(
101       this.wsSendWrapper(UserOperation.EditCommunity, communityForm)
102     );
103   }
104
105   public followCommunity(followCommunityForm: FollowCommunityForm) {
106     this.setAuth(followCommunityForm);
107     this.subject.next(
108       this.wsSendWrapper(UserOperation.FollowCommunity, followCommunityForm)
109     );
110   }
111
112   public listCommunities(form: ListCommunitiesForm) {
113     this.setAuth(form, false);
114     this.subject.next(this.wsSendWrapper(UserOperation.ListCommunities, form));
115   }
116
117   public getFollowedCommunities() {
118     let data = { auth: UserService.Instance.auth };
119     this.subject.next(
120       this.wsSendWrapper(UserOperation.GetFollowedCommunities, data)
121     );
122   }
123
124   public listCategories() {
125     this.subject.next(
126       this.wsSendWrapper(UserOperation.ListCategories, undefined)
127     );
128   }
129
130   public createPost(postForm: PostForm) {
131     this.setAuth(postForm);
132     this.subject.next(this.wsSendWrapper(UserOperation.CreatePost, postForm));
133   }
134
135   // TODO strictly type these
136   public getPost(postId: number) {
137     let data = { id: postId, auth: UserService.Instance.auth };
138     this.subject.next(this.wsSendWrapper(UserOperation.GetPost, data));
139   }
140
141   public getCommunity(communityId: number) {
142     let data = { id: communityId, auth: UserService.Instance.auth };
143     this.subject.next(this.wsSendWrapper(UserOperation.GetCommunity, data));
144   }
145
146   public getCommunityByName(name: string) {
147     let data = { name: name, auth: UserService.Instance.auth };
148     this.subject.next(this.wsSendWrapper(UserOperation.GetCommunity, data));
149   }
150
151   public createComment(commentForm: CommentForm) {
152     this.setAuth(commentForm);
153     this.subject.next(
154       this.wsSendWrapper(UserOperation.CreateComment, commentForm)
155     );
156   }
157
158   public editComment(commentForm: CommentForm) {
159     this.setAuth(commentForm);
160     this.subject.next(
161       this.wsSendWrapper(UserOperation.EditComment, commentForm)
162     );
163   }
164
165   public likeComment(form: CommentLikeForm) {
166     this.setAuth(form);
167     this.subject.next(
168       this.wsSendWrapper(UserOperation.CreateCommentLike, form)
169     );
170   }
171
172   public saveComment(form: SaveCommentForm) {
173     this.setAuth(form);
174     this.subject.next(this.wsSendWrapper(UserOperation.SaveComment, form));
175   }
176
177   public getPosts(form: GetPostsForm) {
178     this.setAuth(form, false);
179     this.subject.next(this.wsSendWrapper(UserOperation.GetPosts, form));
180   }
181
182   public likePost(form: CreatePostLikeForm) {
183     this.setAuth(form);
184     this.subject.next(this.wsSendWrapper(UserOperation.CreatePostLike, form));
185   }
186
187   public editPost(postForm: PostForm) {
188     this.setAuth(postForm);
189     this.subject.next(this.wsSendWrapper(UserOperation.EditPost, postForm));
190   }
191
192   public savePost(form: SavePostForm) {
193     this.setAuth(form);
194     this.subject.next(this.wsSendWrapper(UserOperation.SavePost, form));
195   }
196
197   public banFromCommunity(form: BanFromCommunityForm) {
198     this.setAuth(form);
199     this.subject.next(this.wsSendWrapper(UserOperation.BanFromCommunity, form));
200   }
201
202   public addModToCommunity(form: AddModToCommunityForm) {
203     this.setAuth(form);
204     this.subject.next(
205       this.wsSendWrapper(UserOperation.AddModToCommunity, form)
206     );
207   }
208
209   public transferCommunity(form: TransferCommunityForm) {
210     this.setAuth(form);
211     this.subject.next(
212       this.wsSendWrapper(UserOperation.TransferCommunity, form)
213     );
214   }
215
216   public transferSite(form: TransferSiteForm) {
217     this.setAuth(form);
218     this.subject.next(this.wsSendWrapper(UserOperation.TransferSite, form));
219   }
220
221   public banUser(form: BanUserForm) {
222     this.setAuth(form);
223     this.subject.next(this.wsSendWrapper(UserOperation.BanUser, form));
224   }
225
226   public addAdmin(form: AddAdminForm) {
227     this.setAuth(form);
228     this.subject.next(this.wsSendWrapper(UserOperation.AddAdmin, form));
229   }
230
231   public getUserDetails(form: GetUserDetailsForm) {
232     this.setAuth(form, false);
233     this.subject.next(this.wsSendWrapper(UserOperation.GetUserDetails, form));
234   }
235
236   public getReplies(form: GetRepliesForm) {
237     this.setAuth(form);
238     this.subject.next(this.wsSendWrapper(UserOperation.GetReplies, form));
239   }
240
241   public getUserMentions(form: GetUserMentionsForm) {
242     this.setAuth(form);
243     this.subject.next(this.wsSendWrapper(UserOperation.GetUserMentions, form));
244   }
245
246   public editUserMention(form: EditUserMentionForm) {
247     this.setAuth(form);
248     this.subject.next(this.wsSendWrapper(UserOperation.EditUserMention, form));
249   }
250
251   public getModlog(form: GetModlogForm) {
252     this.subject.next(this.wsSendWrapper(UserOperation.GetModlog, form));
253   }
254
255   public createSite(siteForm: SiteForm) {
256     this.setAuth(siteForm);
257     this.subject.next(this.wsSendWrapper(UserOperation.CreateSite, siteForm));
258   }
259
260   public editSite(siteForm: SiteForm) {
261     this.setAuth(siteForm);
262     this.subject.next(this.wsSendWrapper(UserOperation.EditSite, siteForm));
263   }
264
265   public getSite() {
266     this.subject.next(this.wsSendWrapper(UserOperation.GetSite, undefined));
267   }
268
269   public search(form: SearchForm) {
270     this.setAuth(form, false);
271     this.subject.next(this.wsSendWrapper(UserOperation.Search, form));
272   }
273
274   public markAllAsRead() {
275     let form = {};
276     this.setAuth(form);
277     this.subject.next(this.wsSendWrapper(UserOperation.MarkAllAsRead, form));
278   }
279
280   public saveUserSettings(userSettingsForm: UserSettingsForm) {
281     this.setAuth(userSettingsForm);
282     this.subject.next(
283       this.wsSendWrapper(UserOperation.SaveUserSettings, userSettingsForm)
284     );
285   }
286
287   public deleteAccount(form: DeleteAccountForm) {
288     this.setAuth(form);
289     this.subject.next(this.wsSendWrapper(UserOperation.DeleteAccount, form));
290   }
291
292   public passwordReset(form: PasswordResetForm) {
293     this.subject.next(this.wsSendWrapper(UserOperation.PasswordReset, form));
294   }
295
296   public passwordChange(form: PasswordChangeForm) {
297     this.subject.next(this.wsSendWrapper(UserOperation.PasswordChange, form));
298   }
299
300   public createPrivateMessage(form: PrivateMessageForm) {
301     this.setAuth(form);
302     this.subject.next(
303       this.wsSendWrapper(UserOperation.CreatePrivateMessage, form)
304     );
305   }
306
307   public editPrivateMessage(form: EditPrivateMessageForm) {
308     this.setAuth(form);
309     this.subject.next(
310       this.wsSendWrapper(UserOperation.EditPrivateMessage, form)
311     );
312   }
313
314   public getPrivateMessages(form: GetPrivateMessagesForm) {
315     this.setAuth(form);
316     this.subject.next(
317       this.wsSendWrapper(UserOperation.GetPrivateMessages, form)
318     );
319   }
320
321   private wsSendWrapper(op: UserOperation, data: any) {
322     let send = { op: UserOperation[op], data: data };
323     console.log(send);
324     return send;
325   }
326
327   private setAuth(obj: any, throwErr: boolean = true) {
328     obj.auth = UserService.Instance.auth;
329     if (obj.auth == null && throwErr) {
330       toast(i18n.t('not_logged_in'), 'danger');
331       throw 'Not logged in';
332     }
333   }
334 }
335
336 window.onbeforeunload = () => {
337   WebSocketService.Instance.subject.unsubscribe();
338   WebSocketService.Instance.subject = null;
339 };