1 import { wsUri } from '../env';
17 GetFollowedCommunitiesForm,
22 AddModToCommunityForm,
23 TransferCommunityForm,
39 EditPrivateMessageForm,
40 GetPrivateMessagesForm,
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';
51 export class WebSocketService {
52 private static _instance: WebSocketService;
53 public ws: ReconnectingWebSocket;
54 public subject: Observable<any>;
57 public admins: Array<UserView>;
58 public banned: Array<UserView>;
60 private constructor() {
61 this.ws = new ReconnectingWebSocket(wsUri);
62 this.ws.onopen = () => {
63 console.log(`Connected to ${wsUri}`);
66 this.subject = Observable.create((obs: any) => {
67 this.ws.onmessage = e => {
68 obs.next(JSON.parse(e.data));
72 if (UserService.Instance.user) {
77 public static get Instance() {
78 return this._instance || (this._instance = new this());
82 let form: UserJoinForm = { auth: UserService.Instance.auth };
83 this.ws.send(this.wsSendWrapper(UserOperation.UserJoin, form));
86 public login(loginForm: LoginForm) {
87 this.ws.send(this.wsSendWrapper(UserOperation.Login, loginForm));
90 public register(registerForm: RegisterForm) {
91 this.ws.send(this.wsSendWrapper(UserOperation.Register, registerForm));
94 public createCommunity(communityForm: CommunityForm) {
95 this.setAuth(communityForm);
97 this.wsSendWrapper(UserOperation.CreateCommunity, communityForm)
101 public editCommunity(communityForm: CommunityForm) {
102 this.setAuth(communityForm);
104 this.wsSendWrapper(UserOperation.EditCommunity, communityForm)
108 public followCommunity(followCommunityForm: FollowCommunityForm) {
109 this.setAuth(followCommunityForm);
111 this.wsSendWrapper(UserOperation.FollowCommunity, followCommunityForm)
115 public listCommunities(form: ListCommunitiesForm) {
116 this.setAuth(form, false);
117 this.ws.send(this.wsSendWrapper(UserOperation.ListCommunities, form));
120 public getFollowedCommunities() {
121 let form: GetFollowedCommunitiesForm = { auth: UserService.Instance.auth };
123 this.wsSendWrapper(UserOperation.GetFollowedCommunities, form)
127 public listCategories() {
128 this.ws.send(this.wsSendWrapper(UserOperation.ListCategories, {}));
131 public createPost(postForm: PostForm) {
132 this.setAuth(postForm);
133 this.ws.send(this.wsSendWrapper(UserOperation.CreatePost, postForm));
136 public getPost(form: GetPostForm) {
137 this.setAuth(form, false);
138 this.ws.send(this.wsSendWrapper(UserOperation.GetPost, form));
141 public getCommunity(form: GetCommunityForm) {
142 this.setAuth(form, false);
143 this.ws.send(this.wsSendWrapper(UserOperation.GetCommunity, form));
146 public createComment(commentForm: CommentForm) {
147 this.setAuth(commentForm);
148 this.ws.send(this.wsSendWrapper(UserOperation.CreateComment, commentForm));
151 public editComment(commentForm: CommentForm) {
152 this.setAuth(commentForm);
153 this.ws.send(this.wsSendWrapper(UserOperation.EditComment, commentForm));
156 public likeComment(form: CommentLikeForm) {
158 this.ws.send(this.wsSendWrapper(UserOperation.CreateCommentLike, form));
161 public saveComment(form: SaveCommentForm) {
163 this.ws.send(this.wsSendWrapper(UserOperation.SaveComment, form));
166 public getPosts(form: GetPostsForm) {
167 this.setAuth(form, false);
168 this.ws.send(this.wsSendWrapper(UserOperation.GetPosts, form));
171 public likePost(form: CreatePostLikeForm) {
173 this.ws.send(this.wsSendWrapper(UserOperation.CreatePostLike, form));
176 public editPost(postForm: PostForm) {
177 this.setAuth(postForm);
178 this.ws.send(this.wsSendWrapper(UserOperation.EditPost, postForm));
181 public savePost(form: SavePostForm) {
183 this.ws.send(this.wsSendWrapper(UserOperation.SavePost, form));
186 public banFromCommunity(form: BanFromCommunityForm) {
188 this.ws.send(this.wsSendWrapper(UserOperation.BanFromCommunity, form));
191 public addModToCommunity(form: AddModToCommunityForm) {
193 this.ws.send(this.wsSendWrapper(UserOperation.AddModToCommunity, form));
196 public transferCommunity(form: TransferCommunityForm) {
198 this.ws.send(this.wsSendWrapper(UserOperation.TransferCommunity, form));
201 public transferSite(form: TransferSiteForm) {
203 this.ws.send(this.wsSendWrapper(UserOperation.TransferSite, form));
206 public banUser(form: BanUserForm) {
208 this.ws.send(this.wsSendWrapper(UserOperation.BanUser, form));
211 public addAdmin(form: AddAdminForm) {
213 this.ws.send(this.wsSendWrapper(UserOperation.AddAdmin, form));
216 public getUserDetails(form: GetUserDetailsForm) {
217 this.setAuth(form, false);
218 this.ws.send(this.wsSendWrapper(UserOperation.GetUserDetails, form));
221 public getReplies(form: GetRepliesForm) {
223 this.ws.send(this.wsSendWrapper(UserOperation.GetReplies, form));
226 public getUserMentions(form: GetUserMentionsForm) {
228 this.ws.send(this.wsSendWrapper(UserOperation.GetUserMentions, form));
231 public editUserMention(form: EditUserMentionForm) {
233 this.ws.send(this.wsSendWrapper(UserOperation.EditUserMention, form));
236 public getModlog(form: GetModlogForm) {
237 this.ws.send(this.wsSendWrapper(UserOperation.GetModlog, form));
240 public createSite(siteForm: SiteForm) {
241 this.setAuth(siteForm);
242 this.ws.send(this.wsSendWrapper(UserOperation.CreateSite, siteForm));
245 public editSite(siteForm: SiteForm) {
246 this.setAuth(siteForm);
247 this.ws.send(this.wsSendWrapper(UserOperation.EditSite, siteForm));
251 this.ws.send(this.wsSendWrapper(UserOperation.GetSite, {}));
254 public search(form: SearchForm) {
255 this.setAuth(form, false);
256 this.ws.send(this.wsSendWrapper(UserOperation.Search, form));
259 public markAllAsRead() {
262 this.ws.send(this.wsSendWrapper(UserOperation.MarkAllAsRead, form));
265 public saveUserSettings(userSettingsForm: UserSettingsForm) {
266 this.setAuth(userSettingsForm);
268 this.wsSendWrapper(UserOperation.SaveUserSettings, userSettingsForm)
272 public deleteAccount(form: DeleteAccountForm) {
274 this.ws.send(this.wsSendWrapper(UserOperation.DeleteAccount, form));
277 public passwordReset(form: PasswordResetForm) {
278 this.ws.send(this.wsSendWrapper(UserOperation.PasswordReset, form));
281 public passwordChange(form: PasswordChangeForm) {
282 this.ws.send(this.wsSendWrapper(UserOperation.PasswordChange, form));
285 public createPrivateMessage(form: PrivateMessageForm) {
287 this.ws.send(this.wsSendWrapper(UserOperation.CreatePrivateMessage, form));
290 public editPrivateMessage(form: EditPrivateMessageForm) {
292 this.ws.send(this.wsSendWrapper(UserOperation.EditPrivateMessage, form));
295 public getPrivateMessages(form: GetPrivateMessagesForm) {
297 this.ws.send(this.wsSendWrapper(UserOperation.GetPrivateMessages, form));
300 private wsSendWrapper(op: UserOperation, data: MessageType) {
301 let send = { op: UserOperation[op], data: data };
303 return JSON.stringify(send);
306 private setAuth(obj: any, throwErr: boolean = true) {
307 obj.auth = UserService.Instance.auth;
308 if (obj.auth == null && throwErr) {
309 toast(i18n.t('not_logged_in'), 'danger');
310 throw 'Not logged in';
315 window.onbeforeunload = () => {
316 WebSocketService.Instance.ws.close();