2019-03-23 01:42:57 +00:00
|
|
|
import { wsUri } from '../env';
|
2019-04-23 22:05:50 +00:00
|
|
|
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 } from '../interfaces';
|
2019-03-23 01:42:57 +00:00
|
|
|
import { webSocket } from 'rxjs/webSocket';
|
|
|
|
import { Subject } from 'rxjs';
|
2019-03-26 18:00:18 +00:00
|
|
|
import { retryWhen, delay, take } from 'rxjs/operators';
|
2019-03-23 01:42:57 +00:00
|
|
|
import { UserService } from './';
|
|
|
|
|
|
|
|
export class WebSocketService {
|
|
|
|
private static _instance: WebSocketService;
|
2019-03-26 18:00:18 +00:00
|
|
|
public subject: Subject<any>;
|
2019-04-16 23:04:23 +00:00
|
|
|
|
|
|
|
public site: Site;
|
|
|
|
public admins: Array<UserView>;
|
|
|
|
public banned: Array<UserView>;
|
2019-03-23 01:42:57 +00:00
|
|
|
|
|
|
|
private constructor() {
|
|
|
|
this.subject = webSocket(wsUri);
|
2019-03-26 18:00:18 +00:00
|
|
|
|
2019-04-16 23:04:23 +00:00
|
|
|
// Necessary to not keep reconnecting
|
2019-03-26 18:00:18 +00:00
|
|
|
this.subject
|
2019-03-29 04:56:23 +00:00
|
|
|
.pipe(retryWhen(errors => errors.pipe(delay(60000), take(999))))
|
2019-03-26 18:00:18 +00:00
|
|
|
.subscribe();
|
|
|
|
|
2019-04-16 23:04:23 +00:00
|
|
|
console.log(`Connected to ${wsUri}`);
|
2019-03-23 01:42:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
public static get Instance(){
|
|
|
|
return this._instance || (this._instance = new this());
|
|
|
|
}
|
|
|
|
|
|
|
|
public login(loginForm: LoginForm) {
|
|
|
|
this.subject.next(this.wsSendWrapper(UserOperation.Login, loginForm));
|
|
|
|
}
|
|
|
|
|
|
|
|
public register(registerForm: RegisterForm) {
|
|
|
|
this.subject.next(this.wsSendWrapper(UserOperation.Register, registerForm));
|
|
|
|
}
|
|
|
|
|
|
|
|
public createCommunity(communityForm: CommunityForm) {
|
2019-03-26 18:00:18 +00:00
|
|
|
this.setAuth(communityForm);
|
|
|
|
this.subject.next(this.wsSendWrapper(UserOperation.CreateCommunity, communityForm));
|
|
|
|
}
|
|
|
|
|
2019-04-04 22:29:14 +00:00
|
|
|
public editCommunity(communityForm: CommunityForm) {
|
|
|
|
this.setAuth(communityForm);
|
|
|
|
this.subject.next(this.wsSendWrapper(UserOperation.EditCommunity, communityForm));
|
|
|
|
}
|
|
|
|
|
2019-04-05 06:26:38 +00:00
|
|
|
public followCommunity(followCommunityForm: FollowCommunityForm) {
|
|
|
|
this.setAuth(followCommunityForm);
|
|
|
|
this.subject.next(this.wsSendWrapper(UserOperation.FollowCommunity, followCommunityForm));
|
|
|
|
}
|
|
|
|
|
2019-04-10 06:19:12 +00:00
|
|
|
public listCommunities(form: ListCommunitiesForm) {
|
|
|
|
this.setAuth(form, false);
|
|
|
|
this.subject.next(this.wsSendWrapper(UserOperation.ListCommunities, form));
|
2019-03-26 18:00:18 +00:00
|
|
|
}
|
|
|
|
|
2019-04-05 19:14:54 +00:00
|
|
|
public getFollowedCommunities() {
|
|
|
|
let data = {auth: UserService.Instance.auth };
|
|
|
|
this.subject.next(this.wsSendWrapper(UserOperation.GetFollowedCommunities, data));
|
|
|
|
}
|
|
|
|
|
2019-04-03 23:01:20 +00:00
|
|
|
public listCategories() {
|
|
|
|
this.subject.next(this.wsSendWrapper(UserOperation.ListCategories, undefined));
|
|
|
|
}
|
|
|
|
|
2019-03-26 18:00:18 +00:00
|
|
|
public createPost(postForm: PostForm) {
|
|
|
|
this.setAuth(postForm);
|
|
|
|
this.subject.next(this.wsSendWrapper(UserOperation.CreatePost, postForm));
|
|
|
|
}
|
|
|
|
|
|
|
|
public getPost(postId: number) {
|
2019-03-28 19:32:08 +00:00
|
|
|
let data = {id: postId, auth: UserService.Instance.auth };
|
|
|
|
this.subject.next(this.wsSendWrapper(UserOperation.GetPost, data));
|
2019-03-23 01:42:57 +00:00
|
|
|
}
|
|
|
|
|
2019-03-26 18:00:18 +00:00
|
|
|
public getCommunity(communityId: number) {
|
2019-04-05 06:26:38 +00:00
|
|
|
let data = {id: communityId, auth: UserService.Instance.auth };
|
|
|
|
this.subject.next(this.wsSendWrapper(UserOperation.GetCommunity, data));
|
2019-03-26 18:00:18 +00:00
|
|
|
}
|
|
|
|
|
2019-04-25 21:52:18 +00:00
|
|
|
public getCommunityByName(name: string) {
|
|
|
|
let data = {name: name, auth: UserService.Instance.auth };
|
|
|
|
this.subject.next(this.wsSendWrapper(UserOperation.GetCommunity, data));
|
|
|
|
}
|
|
|
|
|
2019-03-26 18:00:18 +00:00
|
|
|
public createComment(commentForm: CommentForm) {
|
|
|
|
this.setAuth(commentForm);
|
|
|
|
this.subject.next(this.wsSendWrapper(UserOperation.CreateComment, commentForm));
|
|
|
|
}
|
|
|
|
|
2019-03-29 04:56:23 +00:00
|
|
|
public editComment(commentForm: CommentForm) {
|
|
|
|
this.setAuth(commentForm);
|
|
|
|
this.subject.next(this.wsSendWrapper(UserOperation.EditComment, commentForm));
|
|
|
|
}
|
|
|
|
|
2019-03-28 19:32:08 +00:00
|
|
|
public likeComment(form: CommentLikeForm) {
|
|
|
|
this.setAuth(form);
|
|
|
|
this.subject.next(this.wsSendWrapper(UserOperation.CreateCommentLike, form));
|
|
|
|
}
|
|
|
|
|
2019-04-20 04:06:25 +00:00
|
|
|
public saveComment(form: SaveCommentForm) {
|
|
|
|
this.setAuth(form);
|
|
|
|
this.subject.next(this.wsSendWrapper(UserOperation.SaveComment, form));
|
|
|
|
}
|
|
|
|
|
2019-04-04 22:29:14 +00:00
|
|
|
public getPosts(form: GetPostsForm) {
|
2019-04-03 06:49:32 +00:00
|
|
|
this.setAuth(form, false);
|
|
|
|
this.subject.next(this.wsSendWrapper(UserOperation.GetPosts, form));
|
|
|
|
}
|
|
|
|
|
|
|
|
public likePost(form: CreatePostLikeForm) {
|
|
|
|
this.setAuth(form);
|
|
|
|
this.subject.next(this.wsSendWrapper(UserOperation.CreatePostLike, form));
|
|
|
|
}
|
|
|
|
|
2019-04-03 20:59:37 +00:00
|
|
|
public editPost(postForm: PostForm) {
|
|
|
|
this.setAuth(postForm);
|
|
|
|
this.subject.next(this.wsSendWrapper(UserOperation.EditPost, postForm));
|
|
|
|
}
|
|
|
|
|
2019-04-20 04:06:25 +00:00
|
|
|
public savePost(form: SavePostForm) {
|
|
|
|
this.setAuth(form);
|
|
|
|
this.subject.next(this.wsSendWrapper(UserOperation.SavePost, form));
|
|
|
|
}
|
|
|
|
|
2019-04-15 23:12:06 +00:00
|
|
|
public banFromCommunity(form: BanFromCommunityForm) {
|
|
|
|
this.setAuth(form);
|
|
|
|
this.subject.next(this.wsSendWrapper(UserOperation.BanFromCommunity, form));
|
|
|
|
}
|
|
|
|
|
|
|
|
public addModToCommunity(form: AddModToCommunityForm) {
|
|
|
|
this.setAuth(form);
|
|
|
|
this.subject.next(this.wsSendWrapper(UserOperation.AddModToCommunity, form));
|
|
|
|
}
|
|
|
|
|
2019-04-20 04:06:25 +00:00
|
|
|
public banUser(form: BanUserForm) {
|
|
|
|
this.setAuth(form);
|
|
|
|
this.subject.next(this.wsSendWrapper(UserOperation.BanUser, form));
|
|
|
|
}
|
|
|
|
|
|
|
|
public addAdmin(form: AddAdminForm) {
|
|
|
|
this.setAuth(form);
|
|
|
|
this.subject.next(this.wsSendWrapper(UserOperation.AddAdmin, form));
|
|
|
|
}
|
|
|
|
|
2019-04-08 05:19:02 +00:00
|
|
|
public getUserDetails(form: GetUserDetailsForm) {
|
2019-04-29 21:19:00 +00:00
|
|
|
this.setAuth(form, false);
|
2019-04-08 05:19:02 +00:00
|
|
|
this.subject.next(this.wsSendWrapper(UserOperation.GetUserDetails, form));
|
|
|
|
}
|
2019-04-20 18:17:00 +00:00
|
|
|
|
|
|
|
public getReplies(form: GetRepliesForm) {
|
|
|
|
this.setAuth(form);
|
|
|
|
this.subject.next(this.wsSendWrapper(UserOperation.GetReplies, form));
|
|
|
|
}
|
2019-04-08 05:19:02 +00:00
|
|
|
|
2019-04-15 23:12:06 +00:00
|
|
|
public getModlog(form: GetModlogForm) {
|
|
|
|
this.subject.next(this.wsSendWrapper(UserOperation.GetModlog, form));
|
|
|
|
}
|
|
|
|
|
2019-04-16 23:04:23 +00:00
|
|
|
public createSite(siteForm: SiteForm) {
|
|
|
|
this.setAuth(siteForm);
|
|
|
|
this.subject.next(this.wsSendWrapper(UserOperation.CreateSite, siteForm));
|
|
|
|
}
|
|
|
|
|
|
|
|
public editSite(siteForm: SiteForm) {
|
|
|
|
this.setAuth(siteForm);
|
|
|
|
this.subject.next(this.wsSendWrapper(UserOperation.EditSite, siteForm));
|
|
|
|
}
|
2019-04-23 22:05:50 +00:00
|
|
|
|
2019-04-16 23:04:23 +00:00
|
|
|
public getSite() {
|
2019-06-19 20:36:05 +00:00
|
|
|
this.subject.next(this.wsSendWrapper(UserOperation.GetSite, undefined));
|
2019-04-16 23:04:23 +00:00
|
|
|
}
|
|
|
|
|
2019-04-23 22:05:50 +00:00
|
|
|
public search(form: SearchForm) {
|
|
|
|
this.subject.next(this.wsSendWrapper(UserOperation.Search, form));
|
|
|
|
}
|
|
|
|
|
2019-04-29 16:51:13 +00:00
|
|
|
public markAllAsRead() {
|
|
|
|
let form = {};
|
|
|
|
this.setAuth(form);
|
|
|
|
this.subject.next(this.wsSendWrapper(UserOperation.MarkAllAsRead, form));
|
|
|
|
}
|
|
|
|
|
2019-03-26 18:00:18 +00:00
|
|
|
private wsSendWrapper(op: UserOperation, data: any) {
|
|
|
|
let send = { op: UserOperation[op], data: data };
|
2019-03-23 01:42:57 +00:00
|
|
|
console.log(send);
|
|
|
|
return send;
|
|
|
|
}
|
2019-03-26 18:00:18 +00:00
|
|
|
|
2019-04-03 06:49:32 +00:00
|
|
|
private setAuth(obj: any, throwErr: boolean = true) {
|
2019-03-26 18:00:18 +00:00
|
|
|
obj.auth = UserService.Instance.auth;
|
2019-04-03 06:49:32 +00:00
|
|
|
if (obj.auth == null && throwErr) {
|
2019-03-26 18:00:18 +00:00
|
|
|
alert("Not logged in.");
|
|
|
|
throw "Not logged in";
|
|
|
|
}
|
|
|
|
}
|
2019-03-23 01:42:57 +00:00
|
|
|
}
|
2019-03-28 19:32:08 +00:00
|
|
|
|
2019-04-08 05:19:02 +00:00
|
|
|
window.onbeforeunload = (() => {
|
2019-03-28 19:32:08 +00:00
|
|
|
WebSocketService.Instance.subject.unsubscribe();
|
2019-03-30 06:08:02 +00:00
|
|
|
WebSocketService.Instance.subject = null;
|
2019-03-28 19:32:08 +00:00
|
|
|
});
|
|
|
|
|