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