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