1 import { wsUri } from '../env';
17 GetFollowedCommunitiesForm,
22 AddModToCommunityForm,
23 TransferCommunityForm,
38 EditPrivateMessageForm,
39 GetPrivateMessagesForm,
45 WebSocketJsonResponse,
46 } from '../interfaces';
47 import { UserService } from './';
48 import { i18n } from '../i18next';
49 import { toast } from '../utils';
50 import { Observable } from 'rxjs';
51 import { share } from 'rxjs/operators';
52 import ReconnectingWebSocket from 'reconnecting-websocket';
54 export class WebSocketService {
55 private static _instance: WebSocketService;
56 public ws: ReconnectingWebSocket;
57 public subject: Observable<any>;
59 public admins: Array<UserView>;
60 public banned: Array<UserView>;
62 private constructor() {
63 this.ws = new ReconnectingWebSocket(wsUri);
64 let firstConnect = true;
66 this.subject = Observable.create((obs: any) => {
67 this.ws.onmessage = e => {
68 obs.next(JSON.parse(e.data));
70 this.ws.onopen = () => {
71 console.log(`Connected to ${wsUri}`);
73 if (UserService.Instance.user) {
78 let res: WebSocketJsonResponse = {
89 public static get Instance() {
90 return this._instance || (this._instance = new this());
94 let form: UserJoinForm = { auth: UserService.Instance.auth };
95 this.ws.send(this.wsSendWrapper(UserOperation.UserJoin, form));
98 public login(loginForm: LoginForm) {
99 this.ws.send(this.wsSendWrapper(UserOperation.Login, loginForm));
102 public register(registerForm: RegisterForm) {
103 this.ws.send(this.wsSendWrapper(UserOperation.Register, registerForm));
106 public createCommunity(communityForm: CommunityForm) {
107 this.setAuth(communityForm);
109 this.wsSendWrapper(UserOperation.CreateCommunity, communityForm)
113 public editCommunity(communityForm: CommunityForm) {
114 this.setAuth(communityForm);
116 this.wsSendWrapper(UserOperation.EditCommunity, communityForm)
120 public followCommunity(followCommunityForm: FollowCommunityForm) {
121 this.setAuth(followCommunityForm);
123 this.wsSendWrapper(UserOperation.FollowCommunity, followCommunityForm)
127 public listCommunities(form: ListCommunitiesForm) {
128 this.setAuth(form, false);
129 this.ws.send(this.wsSendWrapper(UserOperation.ListCommunities, form));
132 public getFollowedCommunities() {
133 let form: GetFollowedCommunitiesForm = { auth: UserService.Instance.auth };
135 this.wsSendWrapper(UserOperation.GetFollowedCommunities, form)
139 public listCategories() {
140 this.ws.send(this.wsSendWrapper(UserOperation.ListCategories, {}));
143 public createPost(postForm: PostForm) {
144 this.setAuth(postForm);
145 this.ws.send(this.wsSendWrapper(UserOperation.CreatePost, postForm));
148 public getPost(form: GetPostForm) {
149 this.setAuth(form, false);
150 this.ws.send(this.wsSendWrapper(UserOperation.GetPost, form));
153 public getCommunity(form: GetCommunityForm) {
154 this.setAuth(form, false);
155 this.ws.send(this.wsSendWrapper(UserOperation.GetCommunity, form));
158 public createComment(commentForm: CommentForm) {
159 this.setAuth(commentForm);
160 this.ws.send(this.wsSendWrapper(UserOperation.CreateComment, commentForm));
163 public editComment(commentForm: CommentForm) {
164 this.setAuth(commentForm);
165 this.ws.send(this.wsSendWrapper(UserOperation.EditComment, commentForm));
168 public likeComment(form: CommentLikeForm) {
170 this.ws.send(this.wsSendWrapper(UserOperation.CreateCommentLike, form));
173 public saveComment(form: SaveCommentForm) {
175 this.ws.send(this.wsSendWrapper(UserOperation.SaveComment, form));
178 public getPosts(form: GetPostsForm) {
179 this.setAuth(form, false);
180 this.ws.send(this.wsSendWrapper(UserOperation.GetPosts, form));
183 public getComments(form: GetCommentsForm) {
184 this.setAuth(form, false);
185 this.ws.send(this.wsSendWrapper(UserOperation.GetComments, form));
188 public likePost(form: CreatePostLikeForm) {
190 this.ws.send(this.wsSendWrapper(UserOperation.CreatePostLike, form));
193 public editPost(postForm: PostForm) {
194 this.setAuth(postForm);
195 this.ws.send(this.wsSendWrapper(UserOperation.EditPost, postForm));
198 public savePost(form: SavePostForm) {
200 this.ws.send(this.wsSendWrapper(UserOperation.SavePost, form));
203 public banFromCommunity(form: BanFromCommunityForm) {
205 this.ws.send(this.wsSendWrapper(UserOperation.BanFromCommunity, form));
208 public addModToCommunity(form: AddModToCommunityForm) {
210 this.ws.send(this.wsSendWrapper(UserOperation.AddModToCommunity, form));
213 public transferCommunity(form: TransferCommunityForm) {
215 this.ws.send(this.wsSendWrapper(UserOperation.TransferCommunity, form));
218 public transferSite(form: TransferSiteForm) {
220 this.ws.send(this.wsSendWrapper(UserOperation.TransferSite, form));
223 public banUser(form: BanUserForm) {
225 this.ws.send(this.wsSendWrapper(UserOperation.BanUser, form));
228 public addAdmin(form: AddAdminForm) {
230 this.ws.send(this.wsSendWrapper(UserOperation.AddAdmin, form));
233 public getUserDetails(form: GetUserDetailsForm) {
234 this.setAuth(form, false);
235 this.ws.send(this.wsSendWrapper(UserOperation.GetUserDetails, form));
238 public getReplies(form: GetRepliesForm) {
240 this.ws.send(this.wsSendWrapper(UserOperation.GetReplies, form));
243 public getUserMentions(form: GetUserMentionsForm) {
245 this.ws.send(this.wsSendWrapper(UserOperation.GetUserMentions, form));
248 public editUserMention(form: EditUserMentionForm) {
250 this.ws.send(this.wsSendWrapper(UserOperation.EditUserMention, form));
253 public getModlog(form: GetModlogForm) {
254 this.ws.send(this.wsSendWrapper(UserOperation.GetModlog, form));
257 public createSite(siteForm: SiteForm) {
258 this.setAuth(siteForm);
259 this.ws.send(this.wsSendWrapper(UserOperation.CreateSite, siteForm));
262 public editSite(siteForm: SiteForm) {
263 this.setAuth(siteForm);
264 this.ws.send(this.wsSendWrapper(UserOperation.EditSite, siteForm));
268 this.ws.send(this.wsSendWrapper(UserOperation.GetSite, {}));
271 public getSiteConfig() {
272 let siteConfig: GetSiteConfig = {};
273 this.setAuth(siteConfig);
274 this.ws.send(this.wsSendWrapper(UserOperation.GetSiteConfig, siteConfig));
277 public search(form: SearchForm) {
278 this.setAuth(form, false);
279 this.ws.send(this.wsSendWrapper(UserOperation.Search, form));
282 public markAllAsRead() {
285 this.ws.send(this.wsSendWrapper(UserOperation.MarkAllAsRead, form));
288 public saveUserSettings(userSettingsForm: UserSettingsForm) {
289 this.setAuth(userSettingsForm);
291 this.wsSendWrapper(UserOperation.SaveUserSettings, userSettingsForm)
295 public deleteAccount(form: DeleteAccountForm) {
297 this.ws.send(this.wsSendWrapper(UserOperation.DeleteAccount, form));
300 public passwordReset(form: PasswordResetForm) {
301 this.ws.send(this.wsSendWrapper(UserOperation.PasswordReset, form));
304 public passwordChange(form: PasswordChangeForm) {
305 this.ws.send(this.wsSendWrapper(UserOperation.PasswordChange, form));
308 public createPrivateMessage(form: PrivateMessageForm) {
310 this.ws.send(this.wsSendWrapper(UserOperation.CreatePrivateMessage, form));
313 public editPrivateMessage(form: EditPrivateMessageForm) {
315 this.ws.send(this.wsSendWrapper(UserOperation.EditPrivateMessage, form));
318 public getPrivateMessages(form: GetPrivateMessagesForm) {
320 this.ws.send(this.wsSendWrapper(UserOperation.GetPrivateMessages, form));
323 public saveSiteConfig(form: SiteConfigForm) {
325 this.ws.send(this.wsSendWrapper(UserOperation.SaveSiteConfig, form));
328 private wsSendWrapper(op: UserOperation, data: MessageType) {
329 let send = { op: UserOperation[op], data: data };
331 return JSON.stringify(send);
334 private setAuth(obj: any, throwErr: boolean = true) {
335 obj.auth = UserService.Instance.auth;
336 if (obj.auth == null && throwErr) {
337 toast(i18n.t('not_logged_in'), 'danger');
338 throw 'Not logged in';
343 window.onbeforeunload = () => {
344 WebSocketService.Instance.ws.close();