2019-03-22 21:42:57 -04:00
|
|
|
import { wsUri } from '../env';
|
2019-04-10 02:19:12 -04:00
|
|
|
import { LoginForm, RegisterForm, UserOperation, CommunityForm, PostForm, CommentForm, CommentLikeForm, GetPostsForm, CreatePostLikeForm, FollowCommunityForm, GetUserDetailsForm, ListCommunitiesForm } from '../interfaces';
|
2019-03-22 21:42:57 -04:00
|
|
|
import { webSocket } from 'rxjs/webSocket';
|
|
|
|
import { Subject } from 'rxjs';
|
2019-03-26 14:00:18 -04:00
|
|
|
import { retryWhen, delay, take } from 'rxjs/operators';
|
2019-03-22 21:42:57 -04:00
|
|
|
import { UserService } from './';
|
|
|
|
|
|
|
|
export class WebSocketService {
|
|
|
|
private static _instance: WebSocketService;
|
2019-03-26 14:00:18 -04:00
|
|
|
public subject: Subject<any>;
|
2019-03-22 21:42:57 -04:00
|
|
|
|
|
|
|
private constructor() {
|
|
|
|
this.subject = webSocket(wsUri);
|
2019-03-26 14:00:18 -04:00
|
|
|
|
|
|
|
// Even tho this isn't used, its necessary to not keep reconnecting
|
|
|
|
this.subject
|
2019-03-29 00:56:23 -04:00
|
|
|
.pipe(retryWhen(errors => errors.pipe(delay(60000), take(999))))
|
2019-03-26 14:00:18 -04:00
|
|
|
.subscribe();
|
|
|
|
|
2019-03-22 21:42:57 -04: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 14:00:18 -04:00
|
|
|
this.setAuth(communityForm);
|
|
|
|
this.subject.next(this.wsSendWrapper(UserOperation.CreateCommunity, communityForm));
|
|
|
|
}
|
|
|
|
|
2019-04-04 18:29:14 -04:00
|
|
|
public editCommunity(communityForm: CommunityForm) {
|
|
|
|
this.setAuth(communityForm);
|
|
|
|
this.subject.next(this.wsSendWrapper(UserOperation.EditCommunity, communityForm));
|
|
|
|
}
|
|
|
|
|
2019-04-05 02:26:38 -04:00
|
|
|
public followCommunity(followCommunityForm: FollowCommunityForm) {
|
|
|
|
this.setAuth(followCommunityForm);
|
|
|
|
this.subject.next(this.wsSendWrapper(UserOperation.FollowCommunity, followCommunityForm));
|
|
|
|
}
|
|
|
|
|
2019-04-10 02:19:12 -04:00
|
|
|
public listCommunities(form: ListCommunitiesForm) {
|
|
|
|
this.setAuth(form, false);
|
|
|
|
this.subject.next(this.wsSendWrapper(UserOperation.ListCommunities, form));
|
2019-03-26 14:00:18 -04:00
|
|
|
}
|
|
|
|
|
2019-04-05 15:14:54 -04:00
|
|
|
public getFollowedCommunities() {
|
|
|
|
let data = {auth: UserService.Instance.auth };
|
|
|
|
this.subject.next(this.wsSendWrapper(UserOperation.GetFollowedCommunities, data));
|
|
|
|
}
|
|
|
|
|
2019-04-03 19:01:20 -04:00
|
|
|
public listCategories() {
|
|
|
|
this.subject.next(this.wsSendWrapper(UserOperation.ListCategories, undefined));
|
|
|
|
}
|
|
|
|
|
2019-03-26 14:00:18 -04:00
|
|
|
public createPost(postForm: PostForm) {
|
|
|
|
this.setAuth(postForm);
|
|
|
|
this.subject.next(this.wsSendWrapper(UserOperation.CreatePost, postForm));
|
|
|
|
}
|
|
|
|
|
|
|
|
public getPost(postId: number) {
|
2019-03-28 15:32:08 -04:00
|
|
|
let data = {id: postId, auth: UserService.Instance.auth };
|
|
|
|
this.subject.next(this.wsSendWrapper(UserOperation.GetPost, data));
|
2019-03-22 21:42:57 -04:00
|
|
|
}
|
|
|
|
|
2019-03-26 14:00:18 -04:00
|
|
|
public getCommunity(communityId: number) {
|
2019-04-05 02:26:38 -04:00
|
|
|
let data = {id: communityId, auth: UserService.Instance.auth };
|
|
|
|
this.subject.next(this.wsSendWrapper(UserOperation.GetCommunity, data));
|
2019-03-26 14:00:18 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
public createComment(commentForm: CommentForm) {
|
|
|
|
this.setAuth(commentForm);
|
|
|
|
this.subject.next(this.wsSendWrapper(UserOperation.CreateComment, commentForm));
|
|
|
|
}
|
|
|
|
|
2019-03-29 00:56:23 -04:00
|
|
|
public editComment(commentForm: CommentForm) {
|
|
|
|
this.setAuth(commentForm);
|
|
|
|
this.subject.next(this.wsSendWrapper(UserOperation.EditComment, commentForm));
|
|
|
|
}
|
|
|
|
|
2019-03-28 15:32:08 -04:00
|
|
|
public likeComment(form: CommentLikeForm) {
|
|
|
|
this.setAuth(form);
|
|
|
|
this.subject.next(this.wsSendWrapper(UserOperation.CreateCommentLike, form));
|
|
|
|
}
|
|
|
|
|
2019-04-04 18:29:14 -04:00
|
|
|
public getPosts(form: GetPostsForm) {
|
2019-04-03 02:49:32 -04: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 16:59:37 -04:00
|
|
|
public editPost(postForm: PostForm) {
|
|
|
|
this.setAuth(postForm);
|
|
|
|
this.subject.next(this.wsSendWrapper(UserOperation.EditPost, postForm));
|
|
|
|
}
|
|
|
|
|
2019-04-08 01:19:02 -04:00
|
|
|
public getUserDetails(form: GetUserDetailsForm) {
|
|
|
|
this.setAuth(form, false);
|
|
|
|
this.subject.next(this.wsSendWrapper(UserOperation.GetUserDetails, form));
|
|
|
|
}
|
|
|
|
|
2019-03-26 14:00:18 -04:00
|
|
|
private wsSendWrapper(op: UserOperation, data: any) {
|
|
|
|
let send = { op: UserOperation[op], data: data };
|
2019-03-22 21:42:57 -04:00
|
|
|
console.log(send);
|
|
|
|
return send;
|
|
|
|
}
|
2019-03-26 14:00:18 -04:00
|
|
|
|
2019-04-03 02:49:32 -04:00
|
|
|
private setAuth(obj: any, throwErr: boolean = true) {
|
2019-03-26 14:00:18 -04:00
|
|
|
obj.auth = UserService.Instance.auth;
|
2019-04-03 02:49:32 -04:00
|
|
|
if (obj.auth == null && throwErr) {
|
2019-03-26 14:00:18 -04:00
|
|
|
alert("Not logged in.");
|
|
|
|
throw "Not logged in";
|
|
|
|
}
|
|
|
|
}
|
2019-03-28 15:32:08 -04:00
|
|
|
|
2019-03-22 21:42:57 -04:00
|
|
|
}
|
2019-03-28 15:32:08 -04:00
|
|
|
|
2019-04-08 01:19:02 -04:00
|
|
|
window.onbeforeunload = (() => {
|
2019-03-28 15:32:08 -04:00
|
|
|
WebSocketService.Instance.subject.unsubscribe();
|
2019-03-30 02:08:02 -04:00
|
|
|
WebSocketService.Instance.subject = null;
|
2019-03-28 15:32:08 -04:00
|
|
|
});
|
|
|
|
|