]> Untitled Git - lemmy.git/blob - ui/src/services/WebSocketService.ts
Merge branch 'dev'
[lemmy.git] / ui / src / services / WebSocketService.ts
1 import { wsUri } from '../env';
2 import { LoginForm, RegisterForm, UserOperation, CommunityForm, PostForm, SavePostForm, CommentForm, SaveCommentForm, CommentLikeForm, GetPostsForm, CreatePostLikeForm, FollowCommunityForm, GetUserDetailsForm, ListCommunitiesForm, GetModlogForm, BanFromCommunityForm, AddModToCommunityForm, AddAdminForm, BanUserForm, SiteForm, Site, UserView, GetRepliesForm, SearchForm, UserSettingsForm } from '../interfaces';
3 import { webSocket } from 'rxjs/webSocket';
4 import { Subject } from 'rxjs';
5 import { retryWhen, delay, take } from 'rxjs/operators';
6 import { UserService } from './';
7 import { i18n } from '../i18next';
8
9 export class WebSocketService {
10   private static _instance: WebSocketService;
11   public subject: Subject<any>;
12
13   public site: Site;
14   public admins: Array<UserView>;
15   public banned: Array<UserView>;
16
17   private constructor() {
18     this.subject = webSocket(wsUri);
19
20     // Necessary to not keep reconnecting
21     this.subject
22       .pipe(retryWhen(errors => errors.pipe(delay(60000), take(999))))
23       .subscribe();
24
25       console.log(`Connected to ${wsUri}`);
26   }
27
28   public static get Instance(){
29     return this._instance || (this._instance = new this());
30   }
31    
32   public login(loginForm: LoginForm) {
33     this.subject.next(this.wsSendWrapper(UserOperation.Login, loginForm));
34   }
35
36   public register(registerForm: RegisterForm) {
37     this.subject.next(this.wsSendWrapper(UserOperation.Register, registerForm));
38   }
39
40   public createCommunity(communityForm: CommunityForm) {
41     this.setAuth(communityForm);
42     this.subject.next(this.wsSendWrapper(UserOperation.CreateCommunity, communityForm));
43   }
44
45   public editCommunity(communityForm: CommunityForm) {
46     this.setAuth(communityForm);
47     this.subject.next(this.wsSendWrapper(UserOperation.EditCommunity, communityForm));
48   }
49
50   public followCommunity(followCommunityForm: FollowCommunityForm) {
51     this.setAuth(followCommunityForm);
52     this.subject.next(this.wsSendWrapper(UserOperation.FollowCommunity, followCommunityForm));
53   }
54
55   public listCommunities(form: ListCommunitiesForm) {
56     this.setAuth(form, false);
57     this.subject.next(this.wsSendWrapper(UserOperation.ListCommunities, form));
58   }
59
60   public getFollowedCommunities() {
61     let data = {auth: UserService.Instance.auth };
62     this.subject.next(this.wsSendWrapper(UserOperation.GetFollowedCommunities, data));
63   }
64
65   public listCategories() {
66     this.subject.next(this.wsSendWrapper(UserOperation.ListCategories, undefined));
67   }
68
69   public createPost(postForm: PostForm) {
70     this.setAuth(postForm);
71     this.subject.next(this.wsSendWrapper(UserOperation.CreatePost, postForm));
72   }
73
74   public getPost(postId: number) {
75     let data = {id: postId, auth: UserService.Instance.auth };
76     this.subject.next(this.wsSendWrapper(UserOperation.GetPost, data));
77   }
78
79   public getCommunity(communityId: number) {
80     let data = {id: communityId, auth: UserService.Instance.auth };
81     this.subject.next(this.wsSendWrapper(UserOperation.GetCommunity, data));
82   }
83
84   public getCommunityByName(name: string) {
85     let data = {name: name, auth: UserService.Instance.auth };
86     this.subject.next(this.wsSendWrapper(UserOperation.GetCommunity, data));
87   }
88
89   public createComment(commentForm: CommentForm) {
90     this.setAuth(commentForm);
91     this.subject.next(this.wsSendWrapper(UserOperation.CreateComment, commentForm));
92   }
93
94   public editComment(commentForm: CommentForm) {
95     this.setAuth(commentForm);
96     this.subject.next(this.wsSendWrapper(UserOperation.EditComment, commentForm));
97   }
98
99   public likeComment(form: CommentLikeForm) {
100     this.setAuth(form);
101     this.subject.next(this.wsSendWrapper(UserOperation.CreateCommentLike, form));
102   }
103
104   public saveComment(form: SaveCommentForm) {
105     this.setAuth(form);
106     this.subject.next(this.wsSendWrapper(UserOperation.SaveComment, form));
107   }
108
109   public getPosts(form: GetPostsForm) {
110     this.setAuth(form, false);
111     this.subject.next(this.wsSendWrapper(UserOperation.GetPosts, form));
112   }
113
114   public likePost(form: CreatePostLikeForm) {
115     this.setAuth(form);
116     this.subject.next(this.wsSendWrapper(UserOperation.CreatePostLike, form));
117   }
118
119   public editPost(postForm: PostForm) {
120     this.setAuth(postForm);
121     this.subject.next(this.wsSendWrapper(UserOperation.EditPost, postForm));
122   }
123
124   public savePost(form: SavePostForm) {
125     this.setAuth(form);
126     this.subject.next(this.wsSendWrapper(UserOperation.SavePost, form));
127   }
128
129   public banFromCommunity(form: BanFromCommunityForm) {
130     this.setAuth(form);
131     this.subject.next(this.wsSendWrapper(UserOperation.BanFromCommunity, form));
132   }
133
134   public addModToCommunity(form: AddModToCommunityForm) {
135     this.setAuth(form);
136     this.subject.next(this.wsSendWrapper(UserOperation.AddModToCommunity, form));
137   }
138
139   public banUser(form: BanUserForm) {
140     this.setAuth(form);
141     this.subject.next(this.wsSendWrapper(UserOperation.BanUser, form));
142   }
143
144   public addAdmin(form: AddAdminForm) {
145     this.setAuth(form);
146     this.subject.next(this.wsSendWrapper(UserOperation.AddAdmin, form));
147   }
148
149   public getUserDetails(form: GetUserDetailsForm) {
150     this.setAuth(form, false);
151     this.subject.next(this.wsSendWrapper(UserOperation.GetUserDetails, form));
152   }
153
154   public getReplies(form: GetRepliesForm) {
155     this.setAuth(form);
156     this.subject.next(this.wsSendWrapper(UserOperation.GetReplies, form));
157   }
158
159   public getModlog(form: GetModlogForm) {
160     this.subject.next(this.wsSendWrapper(UserOperation.GetModlog, form));
161   }
162
163   public createSite(siteForm: SiteForm) {
164     this.setAuth(siteForm);
165     this.subject.next(this.wsSendWrapper(UserOperation.CreateSite, siteForm));
166   }
167
168   public editSite(siteForm: SiteForm) {
169     this.setAuth(siteForm);
170     this.subject.next(this.wsSendWrapper(UserOperation.EditSite, siteForm));
171   }
172
173   public getSite() {
174     this.subject.next(this.wsSendWrapper(UserOperation.GetSite, undefined));
175   }
176
177   public search(form: SearchForm) {
178     this.subject.next(this.wsSendWrapper(UserOperation.Search, form));
179   }
180
181   public markAllAsRead() {
182     let form = {};
183     this.setAuth(form);
184     this.subject.next(this.wsSendWrapper(UserOperation.MarkAllAsRead, form));
185   }
186
187   public saveUserSettings(userSettingsForm: UserSettingsForm) {
188     this.setAuth(userSettingsForm);
189     this.subject.next(this.wsSendWrapper(UserOperation.SaveUserSettings, userSettingsForm));
190   }
191
192   private wsSendWrapper(op: UserOperation, data: any) {
193     let send = { op: UserOperation[op], data: data };
194     console.log(send);
195     return send;
196   }
197
198   private setAuth(obj: any, throwErr: boolean = true) {
199     obj.auth = UserService.Instance.auth;
200     if (obj.auth == null && throwErr) {
201       alert(i18n.t('not_logged_in'));
202       throw "Not logged in";
203     }
204   }
205 }
206
207 window.onbeforeunload = (() => {
208   WebSocketService.Instance.subject.unsubscribe();
209   WebSocketService.Instance.subject = null;
210 });
211