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