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