2019-03-22 21:42:57 -04:00
|
|
|
import { wsUri } from '../env';
|
2019-10-18 20:20:27 -04:00
|
|
|
import {
|
|
|
|
LoginForm,
|
|
|
|
RegisterForm,
|
|
|
|
UserOperation,
|
|
|
|
CommunityForm,
|
|
|
|
PostForm,
|
|
|
|
SavePostForm,
|
|
|
|
CommentForm,
|
|
|
|
SaveCommentForm,
|
|
|
|
CommentLikeForm,
|
|
|
|
GetPostsForm,
|
|
|
|
CreatePostLikeForm,
|
|
|
|
FollowCommunityForm,
|
|
|
|
GetUserDetailsForm,
|
|
|
|
ListCommunitiesForm,
|
|
|
|
GetModlogForm,
|
|
|
|
BanFromCommunityForm,
|
|
|
|
AddModToCommunityForm,
|
|
|
|
TransferCommunityForm,
|
|
|
|
AddAdminForm,
|
|
|
|
TransferSiteForm,
|
|
|
|
BanUserForm,
|
|
|
|
SiteForm,
|
|
|
|
Site,
|
|
|
|
UserView,
|
|
|
|
GetRepliesForm,
|
2019-10-19 20:46:29 -04:00
|
|
|
GetUserMentionsForm,
|
|
|
|
EditUserMentionForm,
|
2019-10-18 20:20:27 -04:00
|
|
|
SearchForm,
|
|
|
|
UserSettingsForm,
|
|
|
|
DeleteAccountForm,
|
2019-10-29 23:35:39 -04:00
|
|
|
PasswordResetForm,
|
2019-11-02 02:41:57 -04:00
|
|
|
PasswordChangeForm,
|
2019-10-18 20:20:27 -04:00
|
|
|
} 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 './';
|
2019-08-09 20:14:43 -04:00
|
|
|
import { i18n } from '../i18next';
|
2019-03-22 21:42:57 -04:00
|
|
|
|
|
|
|
export class WebSocketService {
|
|
|
|
private static _instance: WebSocketService;
|
2019-03-26 14:00:18 -04:00
|
|
|
public subject: Subject<any>;
|
2019-04-16 19:04:23 -04:00
|
|
|
|
|
|
|
public site: Site;
|
|
|
|
public admins: Array<UserView>;
|
|
|
|
public banned: Array<UserView>;
|
2019-03-22 21:42:57 -04:00
|
|
|
|
|
|
|
private constructor() {
|
|
|
|
this.subject = webSocket(wsUri);
|
2019-03-26 14:00:18 -04:00
|
|
|
|
2019-04-16 19:04:23 -04:00
|
|
|
// Necessary to not keep reconnecting
|
2019-03-26 14:00:18 -04:00
|
|
|
this.subject
|
2019-10-18 20:20:27 -04:00
|
|
|
.pipe(
|
|
|
|
retryWhen(errors =>
|
|
|
|
errors.pipe(
|
2019-10-21 01:19:22 -04:00
|
|
|
delay(1000)
|
|
|
|
// take(999)
|
2019-10-18 20:20:27 -04:00
|
|
|
)
|
|
|
|
)
|
|
|
|
)
|
2019-03-26 14:00:18 -04:00
|
|
|
.subscribe();
|
|
|
|
|
2019-10-18 20:20:27 -04:00
|
|
|
console.log(`Connected to ${wsUri}`);
|
2019-03-22 21:42:57 -04:00
|
|
|
}
|
|
|
|
|
2019-10-18 20:20:27 -04:00
|
|
|
public static get Instance() {
|
2019-03-22 21:42:57 -04:00
|
|
|
return this._instance || (this._instance = new this());
|
|
|
|
}
|
2019-10-18 20:20:27 -04:00
|
|
|
|
2019-03-22 21:42:57 -04:00
|
|
|
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);
|
2019-10-18 20:20:27 -04:00
|
|
|
this.subject.next(
|
|
|
|
this.wsSendWrapper(UserOperation.CreateCommunity, communityForm)
|
|
|
|
);
|
2019-03-26 14:00:18 -04:00
|
|
|
}
|
|
|
|
|
2019-04-04 18:29:14 -04:00
|
|
|
public editCommunity(communityForm: CommunityForm) {
|
|
|
|
this.setAuth(communityForm);
|
2019-10-18 20:20:27 -04:00
|
|
|
this.subject.next(
|
|
|
|
this.wsSendWrapper(UserOperation.EditCommunity, communityForm)
|
|
|
|
);
|
2019-04-04 18:29:14 -04:00
|
|
|
}
|
|
|
|
|
2019-04-05 02:26:38 -04:00
|
|
|
public followCommunity(followCommunityForm: FollowCommunityForm) {
|
|
|
|
this.setAuth(followCommunityForm);
|
2019-10-18 20:20:27 -04:00
|
|
|
this.subject.next(
|
|
|
|
this.wsSendWrapper(UserOperation.FollowCommunity, followCommunityForm)
|
|
|
|
);
|
2019-04-05 02:26:38 -04:00
|
|
|
}
|
|
|
|
|
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() {
|
2019-10-18 20:20:27 -04:00
|
|
|
let data = { auth: UserService.Instance.auth };
|
|
|
|
this.subject.next(
|
|
|
|
this.wsSendWrapper(UserOperation.GetFollowedCommunities, data)
|
|
|
|
);
|
2019-04-05 15:14:54 -04:00
|
|
|
}
|
|
|
|
|
2019-04-03 19:01:20 -04:00
|
|
|
public listCategories() {
|
2019-10-18 20:20:27 -04:00
|
|
|
this.subject.next(
|
|
|
|
this.wsSendWrapper(UserOperation.ListCategories, undefined)
|
|
|
|
);
|
2019-04-03 19:01:20 -04:00
|
|
|
}
|
|
|
|
|
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-10-18 20:20:27 -04:00
|
|
|
let data = { id: postId, auth: UserService.Instance.auth };
|
2019-03-28 15:32:08 -04:00
|
|
|
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-10-18 20:20:27 -04:00
|
|
|
let data = { id: communityId, auth: UserService.Instance.auth };
|
2019-04-05 02:26:38 -04:00
|
|
|
this.subject.next(this.wsSendWrapper(UserOperation.GetCommunity, data));
|
2019-03-26 14:00:18 -04:00
|
|
|
}
|
|
|
|
|
2019-04-25 17:52:18 -04:00
|
|
|
public getCommunityByName(name: string) {
|
2019-10-18 20:20:27 -04:00
|
|
|
let data = { name: name, auth: UserService.Instance.auth };
|
2019-04-25 17:52:18 -04:00
|
|
|
this.subject.next(this.wsSendWrapper(UserOperation.GetCommunity, data));
|
|
|
|
}
|
|
|
|
|
2019-03-26 14:00:18 -04:00
|
|
|
public createComment(commentForm: CommentForm) {
|
|
|
|
this.setAuth(commentForm);
|
2019-10-18 20:20:27 -04:00
|
|
|
this.subject.next(
|
|
|
|
this.wsSendWrapper(UserOperation.CreateComment, commentForm)
|
|
|
|
);
|
2019-03-26 14:00:18 -04:00
|
|
|
}
|
|
|
|
|
2019-03-29 00:56:23 -04:00
|
|
|
public editComment(commentForm: CommentForm) {
|
|
|
|
this.setAuth(commentForm);
|
2019-10-18 20:20:27 -04:00
|
|
|
this.subject.next(
|
|
|
|
this.wsSendWrapper(UserOperation.EditComment, commentForm)
|
|
|
|
);
|
2019-03-29 00:56:23 -04:00
|
|
|
}
|
|
|
|
|
2019-03-28 15:32:08 -04:00
|
|
|
public likeComment(form: CommentLikeForm) {
|
|
|
|
this.setAuth(form);
|
2019-10-18 20:20:27 -04:00
|
|
|
this.subject.next(
|
|
|
|
this.wsSendWrapper(UserOperation.CreateCommentLike, form)
|
|
|
|
);
|
2019-03-28 15:32:08 -04:00
|
|
|
}
|
|
|
|
|
2019-04-20 00:06:25 -04:00
|
|
|
public saveComment(form: SaveCommentForm) {
|
|
|
|
this.setAuth(form);
|
|
|
|
this.subject.next(this.wsSendWrapper(UserOperation.SaveComment, 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-20 00:06:25 -04:00
|
|
|
public savePost(form: SavePostForm) {
|
|
|
|
this.setAuth(form);
|
|
|
|
this.subject.next(this.wsSendWrapper(UserOperation.SavePost, form));
|
|
|
|
}
|
|
|
|
|
2019-04-15 19:12:06 -04:00
|
|
|
public banFromCommunity(form: BanFromCommunityForm) {
|
|
|
|
this.setAuth(form);
|
|
|
|
this.subject.next(this.wsSendWrapper(UserOperation.BanFromCommunity, form));
|
|
|
|
}
|
|
|
|
|
|
|
|
public addModToCommunity(form: AddModToCommunityForm) {
|
|
|
|
this.setAuth(form);
|
2019-10-18 20:20:27 -04:00
|
|
|
this.subject.next(
|
|
|
|
this.wsSendWrapper(UserOperation.AddModToCommunity, form)
|
|
|
|
);
|
2019-04-15 19:12:06 -04:00
|
|
|
}
|
|
|
|
|
2019-08-23 22:40:41 -04:00
|
|
|
public transferCommunity(form: TransferCommunityForm) {
|
|
|
|
this.setAuth(form);
|
2019-10-18 20:20:27 -04:00
|
|
|
this.subject.next(
|
|
|
|
this.wsSendWrapper(UserOperation.TransferCommunity, form)
|
|
|
|
);
|
2019-08-23 22:40:41 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
public transferSite(form: TransferSiteForm) {
|
|
|
|
this.setAuth(form);
|
|
|
|
this.subject.next(this.wsSendWrapper(UserOperation.TransferSite, form));
|
|
|
|
}
|
2019-10-18 20:20:27 -04:00
|
|
|
|
2019-04-20 00:06:25 -04: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 01:19:02 -04:00
|
|
|
public getUserDetails(form: GetUserDetailsForm) {
|
2019-04-29 17:19:00 -04:00
|
|
|
this.setAuth(form, false);
|
2019-04-08 01:19:02 -04:00
|
|
|
this.subject.next(this.wsSendWrapper(UserOperation.GetUserDetails, form));
|
|
|
|
}
|
2019-04-20 14:17:00 -04:00
|
|
|
|
|
|
|
public getReplies(form: GetRepliesForm) {
|
|
|
|
this.setAuth(form);
|
|
|
|
this.subject.next(this.wsSendWrapper(UserOperation.GetReplies, form));
|
|
|
|
}
|
2019-04-08 01:19:02 -04:00
|
|
|
|
2019-10-19 20:46:29 -04:00
|
|
|
public getUserMentions(form: GetUserMentionsForm) {
|
|
|
|
this.setAuth(form);
|
|
|
|
this.subject.next(this.wsSendWrapper(UserOperation.GetUserMentions, form));
|
|
|
|
}
|
|
|
|
|
|
|
|
public editUserMention(form: EditUserMentionForm) {
|
|
|
|
this.setAuth(form);
|
|
|
|
this.subject.next(this.wsSendWrapper(UserOperation.EditUserMention, form));
|
|
|
|
}
|
|
|
|
|
2019-04-15 19:12:06 -04:00
|
|
|
public getModlog(form: GetModlogForm) {
|
|
|
|
this.subject.next(this.wsSendWrapper(UserOperation.GetModlog, form));
|
|
|
|
}
|
|
|
|
|
2019-04-16 19:04:23 -04: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 18:05:50 -04:00
|
|
|
|
2019-04-16 19:04:23 -04:00
|
|
|
public getSite() {
|
2019-06-19 16:36:05 -04:00
|
|
|
this.subject.next(this.wsSendWrapper(UserOperation.GetSite, undefined));
|
2019-04-16 19:04:23 -04:00
|
|
|
}
|
|
|
|
|
2019-04-23 18:05:50 -04:00
|
|
|
public search(form: SearchForm) {
|
|
|
|
this.subject.next(this.wsSendWrapper(UserOperation.Search, form));
|
|
|
|
}
|
|
|
|
|
2019-04-29 12:51:13 -04:00
|
|
|
public markAllAsRead() {
|
|
|
|
let form = {};
|
|
|
|
this.setAuth(form);
|
|
|
|
this.subject.next(this.wsSendWrapper(UserOperation.MarkAllAsRead, form));
|
|
|
|
}
|
|
|
|
|
2019-08-13 22:52:43 -04:00
|
|
|
public saveUserSettings(userSettingsForm: UserSettingsForm) {
|
|
|
|
this.setAuth(userSettingsForm);
|
2019-10-18 20:20:27 -04:00
|
|
|
this.subject.next(
|
|
|
|
this.wsSendWrapper(UserOperation.SaveUserSettings, userSettingsForm)
|
|
|
|
);
|
2019-08-13 22:52:43 -04:00
|
|
|
}
|
|
|
|
|
2019-10-18 00:25:23 -04:00
|
|
|
public deleteAccount(form: DeleteAccountForm) {
|
2019-10-15 18:09:01 -04:00
|
|
|
this.setAuth(form);
|
|
|
|
this.subject.next(this.wsSendWrapper(UserOperation.DeleteAccount, form));
|
|
|
|
}
|
|
|
|
|
2019-10-29 23:35:39 -04:00
|
|
|
public passwordReset(form: PasswordResetForm) {
|
|
|
|
this.subject.next(this.wsSendWrapper(UserOperation.PasswordReset, form));
|
|
|
|
}
|
|
|
|
|
2019-11-02 02:41:57 -04:00
|
|
|
public passwordChange(form: PasswordChangeForm) {
|
|
|
|
this.subject.next(this.wsSendWrapper(UserOperation.PasswordChange, 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-08-09 20:14:43 -04:00
|
|
|
alert(i18n.t('not_logged_in'));
|
2019-10-18 20:20:27 -04:00
|
|
|
throw 'Not logged in';
|
2019-03-26 14:00:18 -04:00
|
|
|
}
|
|
|
|
}
|
2019-03-22 21:42:57 -04:00
|
|
|
}
|
2019-03-28 15:32:08 -04:00
|
|
|
|
2019-10-18 20:20:27 -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-10-18 20:20:27 -04:00
|
|
|
};
|