]> Untitled Git - lemmy.git/blob - ui/src/services/WebSocketService.ts
Merge pull request #2 from dessalines/master
[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   MessageType,
42 } from '../interfaces';
43 import { webSocket } from 'rxjs/webSocket';
44 import { Subject } from 'rxjs';
45 import { retryWhen, delay } from 'rxjs/operators';
46 import { UserService } from './';
47 import { i18n } from '../i18next';
48 import { toast } from '../utils';
49
50 export class WebSocketService {
51   private static _instance: WebSocketService;
52   public subject: Subject<any>;
53
54   public site: Site;
55   public admins: Array<UserView>;
56   public banned: Array<UserView>;
57
58   private constructor() {
59     this.subject = webSocket(wsUri);
60
61     // Necessary to not keep reconnecting
62     this.subject
63       .pipe(
64         retryWhen(errors =>
65           errors.pipe(
66             delay(1000)
67             // take(999)
68           )
69         )
70       )
71       .subscribe();
72
73     console.log(`Connected to ${wsUri}`);
74   }
75
76   public static get Instance() {
77     return this._instance || (this._instance = new this());
78   }
79
80   public login(loginForm: LoginForm) {
81     this.subject.next(this.wsSendWrapper(UserOperation.Login, loginForm));
82   }
83
84   public register(registerForm: RegisterForm) {
85     this.subject.next(this.wsSendWrapper(UserOperation.Register, registerForm));
86   }
87
88   public createCommunity(communityForm: CommunityForm) {
89     this.setAuth(communityForm);
90     this.subject.next(
91       this.wsSendWrapper(UserOperation.CreateCommunity, communityForm)
92     );
93   }
94
95   public editCommunity(communityForm: CommunityForm) {
96     this.setAuth(communityForm);
97     this.subject.next(
98       this.wsSendWrapper(UserOperation.EditCommunity, communityForm)
99     );
100   }
101
102   public followCommunity(followCommunityForm: FollowCommunityForm) {
103     this.setAuth(followCommunityForm);
104     this.subject.next(
105       this.wsSendWrapper(UserOperation.FollowCommunity, followCommunityForm)
106     );
107   }
108
109   public listCommunities(form: ListCommunitiesForm) {
110     this.setAuth(form, false);
111     this.subject.next(this.wsSendWrapper(UserOperation.ListCommunities, form));
112   }
113
114   public getFollowedCommunities() {
115     let form: GetFollowedCommunitiesForm = { auth: UserService.Instance.auth };
116     this.subject.next(
117       this.wsSendWrapper(UserOperation.GetFollowedCommunities, form)
118     );
119   }
120
121   public listCategories() {
122     this.subject.next(
123       this.wsSendWrapper(UserOperation.ListCategories, undefined)
124     );
125   }
126
127   public createPost(postForm: PostForm) {
128     this.setAuth(postForm);
129     this.subject.next(this.wsSendWrapper(UserOperation.CreatePost, postForm));
130   }
131
132   public getPost(form: GetPostForm) {
133     this.setAuth(form, false);
134     this.subject.next(this.wsSendWrapper(UserOperation.GetPost, form));
135   }
136
137   public getCommunity(form: GetCommunityForm) {
138     this.setAuth(form, false);
139     this.subject.next(this.wsSendWrapper(UserOperation.GetCommunity, form));
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: MessageType) {
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       toast(i18n.t('not_logged_in'), 'danger');
322       throw 'Not logged in';
323     }
324   }
325 }
326
327 window.onbeforeunload = () => {
328   WebSocketService.Instance.subject.unsubscribe();
329   WebSocketService.Instance.subject = null;
330 };