1 import { wsUri } from '../env';
17 GetFollowedCommunitiesForm,
22 AddModToCommunityForm,
23 TransferCommunityForm,
39 EditPrivateMessageForm,
40 GetPrivateMessagesForm,
43 } from '../interfaces';
44 import { webSocket } from 'rxjs/webSocket';
45 import { Subject } from 'rxjs';
46 import { retryWhen, delay } from 'rxjs/operators';
47 import { UserService } from './';
48 import { i18n } from '../i18next';
49 import { toast } from '../utils';
51 export class WebSocketService {
52 private static _instance: WebSocketService;
53 public subject: Subject<any>;
56 public admins: Array<UserView>;
57 public banned: Array<UserView>;
59 private constructor() {
60 this.subject = webSocket(wsUri);
62 // Necessary to not keep reconnecting
74 console.log(`Connected to ${wsUri}`);
76 if (UserService.Instance.user) {
81 public static get Instance() {
82 return this._instance || (this._instance = new this());
86 let form: UserJoinForm = { auth: UserService.Instance.auth };
87 this.subject.next(this.wsSendWrapper(UserOperation.UserJoin, form));
90 public login(loginForm: LoginForm) {
91 this.subject.next(this.wsSendWrapper(UserOperation.Login, loginForm));
94 public register(registerForm: RegisterForm) {
95 this.subject.next(this.wsSendWrapper(UserOperation.Register, registerForm));
98 public createCommunity(communityForm: CommunityForm) {
99 this.setAuth(communityForm);
101 this.wsSendWrapper(UserOperation.CreateCommunity, communityForm)
105 public editCommunity(communityForm: CommunityForm) {
106 this.setAuth(communityForm);
108 this.wsSendWrapper(UserOperation.EditCommunity, communityForm)
112 public followCommunity(followCommunityForm: FollowCommunityForm) {
113 this.setAuth(followCommunityForm);
115 this.wsSendWrapper(UserOperation.FollowCommunity, followCommunityForm)
119 public listCommunities(form: ListCommunitiesForm) {
120 this.setAuth(form, false);
121 this.subject.next(this.wsSendWrapper(UserOperation.ListCommunities, form));
124 public getFollowedCommunities() {
125 let form: GetFollowedCommunitiesForm = { auth: UserService.Instance.auth };
127 this.wsSendWrapper(UserOperation.GetFollowedCommunities, form)
131 public listCategories() {
133 this.wsSendWrapper(UserOperation.ListCategories, {})
137 public createPost(postForm: PostForm) {
138 this.setAuth(postForm);
139 this.subject.next(this.wsSendWrapper(UserOperation.CreatePost, postForm));
142 public getPost(form: GetPostForm) {
143 this.setAuth(form, false);
144 this.subject.next(this.wsSendWrapper(UserOperation.GetPost, form));
147 public getCommunity(form: GetCommunityForm) {
148 this.setAuth(form, false);
149 this.subject.next(this.wsSendWrapper(UserOperation.GetCommunity, form));
152 public createComment(commentForm: CommentForm) {
153 this.setAuth(commentForm);
155 this.wsSendWrapper(UserOperation.CreateComment, commentForm)
159 public editComment(commentForm: CommentForm) {
160 this.setAuth(commentForm);
162 this.wsSendWrapper(UserOperation.EditComment, commentForm)
166 public likeComment(form: CommentLikeForm) {
169 this.wsSendWrapper(UserOperation.CreateCommentLike, form)
173 public saveComment(form: SaveCommentForm) {
175 this.subject.next(this.wsSendWrapper(UserOperation.SaveComment, form));
178 public getPosts(form: GetPostsForm) {
179 this.setAuth(form, false);
180 this.subject.next(this.wsSendWrapper(UserOperation.GetPosts, form));
183 public likePost(form: CreatePostLikeForm) {
185 this.subject.next(this.wsSendWrapper(UserOperation.CreatePostLike, form));
188 public editPost(postForm: PostForm) {
189 this.setAuth(postForm);
190 this.subject.next(this.wsSendWrapper(UserOperation.EditPost, postForm));
193 public savePost(form: SavePostForm) {
195 this.subject.next(this.wsSendWrapper(UserOperation.SavePost, form));
198 public banFromCommunity(form: BanFromCommunityForm) {
200 this.subject.next(this.wsSendWrapper(UserOperation.BanFromCommunity, form));
203 public addModToCommunity(form: AddModToCommunityForm) {
206 this.wsSendWrapper(UserOperation.AddModToCommunity, form)
210 public transferCommunity(form: TransferCommunityForm) {
213 this.wsSendWrapper(UserOperation.TransferCommunity, form)
217 public transferSite(form: TransferSiteForm) {
219 this.subject.next(this.wsSendWrapper(UserOperation.TransferSite, form));
222 public banUser(form: BanUserForm) {
224 this.subject.next(this.wsSendWrapper(UserOperation.BanUser, form));
227 public addAdmin(form: AddAdminForm) {
229 this.subject.next(this.wsSendWrapper(UserOperation.AddAdmin, form));
232 public getUserDetails(form: GetUserDetailsForm) {
233 this.setAuth(form, false);
234 this.subject.next(this.wsSendWrapper(UserOperation.GetUserDetails, form));
237 public getReplies(form: GetRepliesForm) {
239 this.subject.next(this.wsSendWrapper(UserOperation.GetReplies, form));
242 public getUserMentions(form: GetUserMentionsForm) {
244 this.subject.next(this.wsSendWrapper(UserOperation.GetUserMentions, form));
247 public editUserMention(form: EditUserMentionForm) {
249 this.subject.next(this.wsSendWrapper(UserOperation.EditUserMention, form));
252 public getModlog(form: GetModlogForm) {
253 this.subject.next(this.wsSendWrapper(UserOperation.GetModlog, form));
256 public createSite(siteForm: SiteForm) {
257 this.setAuth(siteForm);
258 this.subject.next(this.wsSendWrapper(UserOperation.CreateSite, siteForm));
261 public editSite(siteForm: SiteForm) {
262 this.setAuth(siteForm);
263 this.subject.next(this.wsSendWrapper(UserOperation.EditSite, siteForm));
267 this.subject.next(this.wsSendWrapper(UserOperation.GetSite, {}));
270 public search(form: SearchForm) {
271 this.setAuth(form, false);
272 this.subject.next(this.wsSendWrapper(UserOperation.Search, form));
275 public markAllAsRead() {
278 this.subject.next(this.wsSendWrapper(UserOperation.MarkAllAsRead, form));
281 public saveUserSettings(userSettingsForm: UserSettingsForm) {
282 this.setAuth(userSettingsForm);
284 this.wsSendWrapper(UserOperation.SaveUserSettings, userSettingsForm)
288 public deleteAccount(form: DeleteAccountForm) {
290 this.subject.next(this.wsSendWrapper(UserOperation.DeleteAccount, form));
293 public passwordReset(form: PasswordResetForm) {
294 this.subject.next(this.wsSendWrapper(UserOperation.PasswordReset, form));
297 public passwordChange(form: PasswordChangeForm) {
298 this.subject.next(this.wsSendWrapper(UserOperation.PasswordChange, form));
301 public createPrivateMessage(form: PrivateMessageForm) {
304 this.wsSendWrapper(UserOperation.CreatePrivateMessage, form)
308 public editPrivateMessage(form: EditPrivateMessageForm) {
311 this.wsSendWrapper(UserOperation.EditPrivateMessage, form)
315 public getPrivateMessages(form: GetPrivateMessagesForm) {
318 this.wsSendWrapper(UserOperation.GetPrivateMessages, form)
322 private wsSendWrapper(op: UserOperation, data: MessageType) {
323 let send = { op: UserOperation[op], data: data };
328 private setAuth(obj: any, throwErr: boolean = true) {
329 obj.auth = UserService.Instance.auth;
330 if (obj.auth == null && throwErr) {
331 toast(i18n.t('not_logged_in'), 'danger');
332 throw 'Not logged in';
337 window.onbeforeunload = () => {
338 WebSocketService.Instance.subject.unsubscribe();
339 WebSocketService.Instance.subject = null;