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