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