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