2019-03-23 01:42:57 +00:00
|
|
|
import { wsUri } from '../env';
|
2019-04-10 06:19:12 +00:00
|
|
|
import { LoginForm, RegisterForm, UserOperation, CommunityForm, PostForm, CommentForm, CommentLikeForm, GetPostsForm, CreatePostLikeForm, FollowCommunityForm, GetUserDetailsForm, ListCommunitiesForm } 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-03-23 01:42:57 +00:00
|
|
|
|
|
|
|
private constructor() {
|
|
|
|
this.subject = webSocket(wsUri);
|
2019-03-26 18:00:18 +00:00
|
|
|
|
|
|
|
// Even tho this isn't used, its necessary to not keep reconnecting
|
|
|
|
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-03-23 01:42:57 +00:00
|
|
|
console.log(`Connected to ${wsUri}`);
|
|
|
|
}
|
|
|
|
|
|
|
|
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
|
|
|
}
|
|
|
|
|
|
|
|
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-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-08 05:19:02 +00:00
|
|
|
public getUserDetails(form: GetUserDetailsForm) {
|
|
|
|
this.setAuth(form, false);
|
|
|
|
this.subject.next(this.wsSendWrapper(UserOperation.GetUserDetails, 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-28 19:32:08 +00:00
|
|
|
|
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
|
|
|
});
|
|
|
|
|