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