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