]> Untitled Git - lemmy.git/blob - ui/src/services/WebSocketService.ts
Running prettier on code.
[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   GetPostsForm,
13   CreatePostLikeForm,
14   FollowCommunityForm,
15   GetUserDetailsForm,
16   ListCommunitiesForm,
17   GetModlogForm,
18   BanFromCommunityForm,
19   AddModToCommunityForm,
20   TransferCommunityForm,
21   AddAdminForm,
22   TransferSiteForm,
23   BanUserForm,
24   SiteForm,
25   Site,
26   UserView,
27   GetRepliesForm,
28   SearchForm,
29   UserSettingsForm,
30   DeleteAccountForm,
31 } from '../interfaces';
32 import { webSocket } from 'rxjs/webSocket';
33 import { Subject } from 'rxjs';
34 import { retryWhen, delay, take } from 'rxjs/operators';
35 import { UserService } from './';
36 import { i18n } from '../i18next';
37
38 export class WebSocketService {
39   private static _instance: WebSocketService;
40   public subject: Subject<any>;
41
42   public site: Site;
43   public admins: Array<UserView>;
44   public banned: Array<UserView>;
45
46   private constructor() {
47     this.subject = webSocket(wsUri);
48
49     // Necessary to not keep reconnecting
50     this.subject
51       .pipe(
52         retryWhen(errors =>
53           errors.pipe(
54             delay(60000),
55             take(999)
56           )
57         )
58       )
59       .subscribe();
60
61     console.log(`Connected to ${wsUri}`);
62   }
63
64   public static get Instance() {
65     return this._instance || (this._instance = new this());
66   }
67
68   public login(loginForm: LoginForm) {
69     this.subject.next(this.wsSendWrapper(UserOperation.Login, loginForm));
70   }
71
72   public register(registerForm: RegisterForm) {
73     this.subject.next(this.wsSendWrapper(UserOperation.Register, registerForm));
74   }
75
76   public createCommunity(communityForm: CommunityForm) {
77     this.setAuth(communityForm);
78     this.subject.next(
79       this.wsSendWrapper(UserOperation.CreateCommunity, communityForm)
80     );
81   }
82
83   public editCommunity(communityForm: CommunityForm) {
84     this.setAuth(communityForm);
85     this.subject.next(
86       this.wsSendWrapper(UserOperation.EditCommunity, communityForm)
87     );
88   }
89
90   public followCommunity(followCommunityForm: FollowCommunityForm) {
91     this.setAuth(followCommunityForm);
92     this.subject.next(
93       this.wsSendWrapper(UserOperation.FollowCommunity, followCommunityForm)
94     );
95   }
96
97   public listCommunities(form: ListCommunitiesForm) {
98     this.setAuth(form, false);
99     this.subject.next(this.wsSendWrapper(UserOperation.ListCommunities, form));
100   }
101
102   public getFollowedCommunities() {
103     let data = { auth: UserService.Instance.auth };
104     this.subject.next(
105       this.wsSendWrapper(UserOperation.GetFollowedCommunities, data)
106     );
107   }
108
109   public listCategories() {
110     this.subject.next(
111       this.wsSendWrapper(UserOperation.ListCategories, undefined)
112     );
113   }
114
115   public createPost(postForm: PostForm) {
116     this.setAuth(postForm);
117     this.subject.next(this.wsSendWrapper(UserOperation.CreatePost, postForm));
118   }
119
120   public getPost(postId: number) {
121     let data = { id: postId, auth: UserService.Instance.auth };
122     this.subject.next(this.wsSendWrapper(UserOperation.GetPost, data));
123   }
124
125   public getCommunity(communityId: number) {
126     let data = { id: communityId, auth: UserService.Instance.auth };
127     this.subject.next(this.wsSendWrapper(UserOperation.GetCommunity, data));
128   }
129
130   public getCommunityByName(name: string) {
131     let data = { name: name, auth: UserService.Instance.auth };
132     this.subject.next(this.wsSendWrapper(UserOperation.GetCommunity, data));
133   }
134
135   public createComment(commentForm: CommentForm) {
136     this.setAuth(commentForm);
137     this.subject.next(
138       this.wsSendWrapper(UserOperation.CreateComment, commentForm)
139     );
140   }
141
142   public editComment(commentForm: CommentForm) {
143     this.setAuth(commentForm);
144     this.subject.next(
145       this.wsSendWrapper(UserOperation.EditComment, commentForm)
146     );
147   }
148
149   public likeComment(form: CommentLikeForm) {
150     this.setAuth(form);
151     this.subject.next(
152       this.wsSendWrapper(UserOperation.CreateCommentLike, form)
153     );
154   }
155
156   public saveComment(form: SaveCommentForm) {
157     this.setAuth(form);
158     this.subject.next(this.wsSendWrapper(UserOperation.SaveComment, form));
159   }
160
161   public getPosts(form: GetPostsForm) {
162     this.setAuth(form, false);
163     this.subject.next(this.wsSendWrapper(UserOperation.GetPosts, form));
164   }
165
166   public likePost(form: CreatePostLikeForm) {
167     this.setAuth(form);
168     this.subject.next(this.wsSendWrapper(UserOperation.CreatePostLike, form));
169   }
170
171   public editPost(postForm: PostForm) {
172     this.setAuth(postForm);
173     this.subject.next(this.wsSendWrapper(UserOperation.EditPost, postForm));
174   }
175
176   public savePost(form: SavePostForm) {
177     this.setAuth(form);
178     this.subject.next(this.wsSendWrapper(UserOperation.SavePost, form));
179   }
180
181   public banFromCommunity(form: BanFromCommunityForm) {
182     this.setAuth(form);
183     this.subject.next(this.wsSendWrapper(UserOperation.BanFromCommunity, form));
184   }
185
186   public addModToCommunity(form: AddModToCommunityForm) {
187     this.setAuth(form);
188     this.subject.next(
189       this.wsSendWrapper(UserOperation.AddModToCommunity, form)
190     );
191   }
192
193   public transferCommunity(form: TransferCommunityForm) {
194     this.setAuth(form);
195     this.subject.next(
196       this.wsSendWrapper(UserOperation.TransferCommunity, form)
197     );
198   }
199
200   public transferSite(form: TransferSiteForm) {
201     this.setAuth(form);
202     this.subject.next(this.wsSendWrapper(UserOperation.TransferSite, form));
203   }
204
205   public banUser(form: BanUserForm) {
206     this.setAuth(form);
207     this.subject.next(this.wsSendWrapper(UserOperation.BanUser, form));
208   }
209
210   public addAdmin(form: AddAdminForm) {
211     this.setAuth(form);
212     this.subject.next(this.wsSendWrapper(UserOperation.AddAdmin, form));
213   }
214
215   public getUserDetails(form: GetUserDetailsForm) {
216     this.setAuth(form, false);
217     this.subject.next(this.wsSendWrapper(UserOperation.GetUserDetails, form));
218   }
219
220   public getReplies(form: GetRepliesForm) {
221     this.setAuth(form);
222     this.subject.next(this.wsSendWrapper(UserOperation.GetReplies, form));
223   }
224
225   public getModlog(form: GetModlogForm) {
226     this.subject.next(this.wsSendWrapper(UserOperation.GetModlog, form));
227   }
228
229   public createSite(siteForm: SiteForm) {
230     this.setAuth(siteForm);
231     this.subject.next(this.wsSendWrapper(UserOperation.CreateSite, siteForm));
232   }
233
234   public editSite(siteForm: SiteForm) {
235     this.setAuth(siteForm);
236     this.subject.next(this.wsSendWrapper(UserOperation.EditSite, siteForm));
237   }
238
239   public getSite() {
240     this.subject.next(this.wsSendWrapper(UserOperation.GetSite, undefined));
241   }
242
243   public search(form: SearchForm) {
244     this.subject.next(this.wsSendWrapper(UserOperation.Search, form));
245   }
246
247   public markAllAsRead() {
248     let form = {};
249     this.setAuth(form);
250     this.subject.next(this.wsSendWrapper(UserOperation.MarkAllAsRead, form));
251   }
252
253   public saveUserSettings(userSettingsForm: UserSettingsForm) {
254     this.setAuth(userSettingsForm);
255     this.subject.next(
256       this.wsSendWrapper(UserOperation.SaveUserSettings, userSettingsForm)
257     );
258   }
259
260   public deleteAccount(form: DeleteAccountForm) {
261     this.setAuth(form);
262     this.subject.next(this.wsSendWrapper(UserOperation.DeleteAccount, form));
263   }
264
265   private wsSendWrapper(op: UserOperation, data: any) {
266     let send = { op: UserOperation[op], data: data };
267     console.log(send);
268     return send;
269   }
270
271   private setAuth(obj: any, throwErr: boolean = true) {
272     obj.auth = UserService.Instance.auth;
273     if (obj.auth == null && throwErr) {
274       alert(i18n.t('not_logged_in'));
275       throw 'Not logged in';
276     }
277   }
278 }
279
280 window.onbeforeunload = () => {
281   WebSocketService.Instance.subject.unsubscribe();
282   WebSocketService.Instance.subject = null;
283 };