2019-03-23 01:42:57 +00:00
|
|
|
import { wsUri } from '../env';
|
2019-10-19 00:20:27 +00:00
|
|
|
import {
|
|
|
|
LoginForm,
|
|
|
|
RegisterForm,
|
|
|
|
UserOperation,
|
|
|
|
CommunityForm,
|
|
|
|
PostForm,
|
|
|
|
SavePostForm,
|
|
|
|
CommentForm,
|
|
|
|
SaveCommentForm,
|
|
|
|
CommentLikeForm,
|
2020-01-28 02:04:30 +00:00
|
|
|
GetPostForm,
|
2019-10-19 00:20:27 +00:00
|
|
|
GetPostsForm,
|
|
|
|
CreatePostLikeForm,
|
2020-01-28 02:04:30 +00:00
|
|
|
GetCommunityForm,
|
2019-10-19 00:20:27 +00:00
|
|
|
FollowCommunityForm,
|
2020-01-28 02:04:30 +00:00
|
|
|
GetFollowedCommunitiesForm,
|
2019-10-19 00:20:27 +00:00
|
|
|
GetUserDetailsForm,
|
|
|
|
ListCommunitiesForm,
|
|
|
|
GetModlogForm,
|
|
|
|
BanFromCommunityForm,
|
|
|
|
AddModToCommunityForm,
|
|
|
|
TransferCommunityForm,
|
|
|
|
AddAdminForm,
|
|
|
|
TransferSiteForm,
|
|
|
|
BanUserForm,
|
|
|
|
SiteForm,
|
|
|
|
Site,
|
|
|
|
UserView,
|
|
|
|
GetRepliesForm,
|
2019-10-20 00:46:29 +00:00
|
|
|
GetUserMentionsForm,
|
|
|
|
EditUserMentionForm,
|
2019-10-19 00:20:27 +00:00
|
|
|
SearchForm,
|
|
|
|
UserSettingsForm,
|
|
|
|
DeleteAccountForm,
|
2019-10-30 03:35:39 +00:00
|
|
|
PasswordResetForm,
|
2019-11-02 06:41:57 +00:00
|
|
|
PasswordChangeForm,
|
2020-01-22 21:35:29 +00:00
|
|
|
PrivateMessageForm,
|
|
|
|
EditPrivateMessageForm,
|
|
|
|
GetPrivateMessagesForm,
|
2020-02-01 01:02:20 +00:00
|
|
|
UserJoinForm,
|
2020-01-28 02:04:30 +00:00
|
|
|
MessageType,
|
2019-10-19 00:20:27 +00:00
|
|
|
} from '../interfaces';
|
2019-03-23 01:42:57 +00:00
|
|
|
import { UserService } from './';
|
2019-08-10 00:14:43 +00:00
|
|
|
import { i18n } from '../i18next';
|
2020-01-23 03:29:11 +00:00
|
|
|
import { toast } from '../utils';
|
2020-02-02 03:52:16 +00:00
|
|
|
import { Observable } from 'rxjs';
|
|
|
|
import { share } from 'rxjs/operators';
|
|
|
|
import ReconnectingWebSocket from 'reconnecting-websocket';
|
2019-03-23 01:42:57 +00:00
|
|
|
|
|
|
|
export class WebSocketService {
|
|
|
|
private static _instance: WebSocketService;
|
2020-02-02 03:52:16 +00:00
|
|
|
public ws: ReconnectingWebSocket;
|
|
|
|
public subject: Observable<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() {
|
2020-02-02 03:52:16 +00:00
|
|
|
this.ws = new ReconnectingWebSocket(wsUri);
|
|
|
|
this.ws.onopen = () => {
|
|
|
|
console.log(`Connected to ${wsUri}`);
|
|
|
|
};
|
2019-03-26 18:00:18 +00:00
|
|
|
|
2020-02-02 03:52:16 +00:00
|
|
|
this.subject = Observable.create((obs: any) => {
|
|
|
|
this.ws.onmessage = e => {
|
|
|
|
obs.next(JSON.parse(e.data));
|
|
|
|
};
|
|
|
|
}).pipe(share());
|
2020-01-26 17:23:28 +00:00
|
|
|
|
|
|
|
if (UserService.Instance.user) {
|
2020-02-01 01:02:20 +00:00
|
|
|
this.userJoin();
|
2020-01-26 17:23:28 +00:00
|
|
|
}
|
2019-03-23 01:42:57 +00:00
|
|
|
}
|
|
|
|
|
2019-10-19 00:20:27 +00:00
|
|
|
public static get Instance() {
|
2019-03-23 01:42:57 +00:00
|
|
|
return this._instance || (this._instance = new this());
|
|
|
|
}
|
2019-10-19 00:20:27 +00:00
|
|
|
|
2020-01-26 17:23:28 +00:00
|
|
|
public userJoin() {
|
2020-02-01 01:02:20 +00:00
|
|
|
let form: UserJoinForm = { auth: UserService.Instance.auth };
|
2020-02-02 14:15:02 +00:00
|
|
|
this.ws.send(this.wsSendWrapper(UserOperation.UserJoin, form));
|
2020-01-26 17:23:28 +00:00
|
|
|
}
|
|
|
|
|
2019-03-23 01:42:57 +00:00
|
|
|
public login(loginForm: LoginForm) {
|
2020-02-02 03:52:16 +00:00
|
|
|
this.ws.send(this.wsSendWrapper(UserOperation.Login, loginForm));
|
2019-03-23 01:42:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
public register(registerForm: RegisterForm) {
|
2020-02-02 03:52:16 +00:00
|
|
|
this.ws.send(this.wsSendWrapper(UserOperation.Register, registerForm));
|
2019-03-23 01:42:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
public createCommunity(communityForm: CommunityForm) {
|
2019-03-26 18:00:18 +00:00
|
|
|
this.setAuth(communityForm);
|
2020-02-02 03:52:16 +00:00
|
|
|
this.ws.send(
|
2019-10-19 00:20:27 +00:00
|
|
|
this.wsSendWrapper(UserOperation.CreateCommunity, communityForm)
|
|
|
|
);
|
2019-03-26 18:00:18 +00:00
|
|
|
}
|
|
|
|
|
2019-04-04 22:29:14 +00:00
|
|
|
public editCommunity(communityForm: CommunityForm) {
|
|
|
|
this.setAuth(communityForm);
|
2020-02-02 03:52:16 +00:00
|
|
|
this.ws.send(
|
2019-10-19 00:20:27 +00:00
|
|
|
this.wsSendWrapper(UserOperation.EditCommunity, communityForm)
|
|
|
|
);
|
2019-04-04 22:29:14 +00:00
|
|
|
}
|
|
|
|
|
2019-04-05 06:26:38 +00:00
|
|
|
public followCommunity(followCommunityForm: FollowCommunityForm) {
|
|
|
|
this.setAuth(followCommunityForm);
|
2020-02-02 03:52:16 +00:00
|
|
|
this.ws.send(
|
2019-10-19 00:20:27 +00:00
|
|
|
this.wsSendWrapper(UserOperation.FollowCommunity, followCommunityForm)
|
|
|
|
);
|
2019-04-05 06:26:38 +00:00
|
|
|
}
|
|
|
|
|
2019-04-10 06:19:12 +00:00
|
|
|
public listCommunities(form: ListCommunitiesForm) {
|
|
|
|
this.setAuth(form, false);
|
2020-02-02 03:52:16 +00:00
|
|
|
this.ws.send(this.wsSendWrapper(UserOperation.ListCommunities, form));
|
2019-03-26 18:00:18 +00:00
|
|
|
}
|
|
|
|
|
2019-04-05 19:14:54 +00:00
|
|
|
public getFollowedCommunities() {
|
2020-01-28 02:04:30 +00:00
|
|
|
let form: GetFollowedCommunitiesForm = { auth: UserService.Instance.auth };
|
2020-02-02 03:52:16 +00:00
|
|
|
this.ws.send(
|
2020-01-28 02:04:30 +00:00
|
|
|
this.wsSendWrapper(UserOperation.GetFollowedCommunities, form)
|
2019-10-19 00:20:27 +00:00
|
|
|
);
|
2019-04-05 19:14:54 +00:00
|
|
|
}
|
|
|
|
|
2019-04-03 23:01:20 +00:00
|
|
|
public listCategories() {
|
2020-02-02 03:52:16 +00:00
|
|
|
this.ws.send(this.wsSendWrapper(UserOperation.ListCategories, {}));
|
2019-04-03 23:01:20 +00:00
|
|
|
}
|
|
|
|
|
2019-03-26 18:00:18 +00:00
|
|
|
public createPost(postForm: PostForm) {
|
|
|
|
this.setAuth(postForm);
|
2020-02-02 03:52:16 +00:00
|
|
|
this.ws.send(this.wsSendWrapper(UserOperation.CreatePost, postForm));
|
2019-03-26 18:00:18 +00:00
|
|
|
}
|
|
|
|
|
2020-01-28 02:04:30 +00:00
|
|
|
public getPost(form: GetPostForm) {
|
2020-01-28 13:47:37 +00:00
|
|
|
this.setAuth(form, false);
|
2020-02-02 03:52:16 +00:00
|
|
|
this.ws.send(this.wsSendWrapper(UserOperation.GetPost, form));
|
2019-03-26 18:00:18 +00:00
|
|
|
}
|
|
|
|
|
2020-01-28 02:04:30 +00:00
|
|
|
public getCommunity(form: GetCommunityForm) {
|
2020-01-28 13:47:37 +00:00
|
|
|
this.setAuth(form, false);
|
2020-02-02 03:52:16 +00:00
|
|
|
this.ws.send(this.wsSendWrapper(UserOperation.GetCommunity, form));
|
2019-04-25 21:52:18 +00:00
|
|
|
}
|
|
|
|
|
2019-03-26 18:00:18 +00:00
|
|
|
public createComment(commentForm: CommentForm) {
|
|
|
|
this.setAuth(commentForm);
|
2020-02-02 03:52:16 +00:00
|
|
|
this.ws.send(this.wsSendWrapper(UserOperation.CreateComment, commentForm));
|
2019-03-26 18:00:18 +00:00
|
|
|
}
|
|
|
|
|
2019-03-29 04:56:23 +00:00
|
|
|
public editComment(commentForm: CommentForm) {
|
|
|
|
this.setAuth(commentForm);
|
2020-02-02 03:52:16 +00:00
|
|
|
this.ws.send(this.wsSendWrapper(UserOperation.EditComment, commentForm));
|
2019-03-29 04:56:23 +00:00
|
|
|
}
|
|
|
|
|
2019-03-28 19:32:08 +00:00
|
|
|
public likeComment(form: CommentLikeForm) {
|
|
|
|
this.setAuth(form);
|
2020-02-02 03:52:16 +00:00
|
|
|
this.ws.send(this.wsSendWrapper(UserOperation.CreateCommentLike, form));
|
2019-03-28 19:32:08 +00:00
|
|
|
}
|
|
|
|
|
2019-04-20 04:06:25 +00:00
|
|
|
public saveComment(form: SaveCommentForm) {
|
|
|
|
this.setAuth(form);
|
2020-02-02 03:52:16 +00:00
|
|
|
this.ws.send(this.wsSendWrapper(UserOperation.SaveComment, form));
|
2019-04-20 04:06:25 +00:00
|
|
|
}
|
|
|
|
|
2019-04-04 22:29:14 +00:00
|
|
|
public getPosts(form: GetPostsForm) {
|
2019-04-03 06:49:32 +00:00
|
|
|
this.setAuth(form, false);
|
2020-02-02 03:52:16 +00:00
|
|
|
this.ws.send(this.wsSendWrapper(UserOperation.GetPosts, form));
|
2019-04-03 06:49:32 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
public likePost(form: CreatePostLikeForm) {
|
|
|
|
this.setAuth(form);
|
2020-02-02 03:52:16 +00:00
|
|
|
this.ws.send(this.wsSendWrapper(UserOperation.CreatePostLike, form));
|
2019-04-03 06:49:32 +00:00
|
|
|
}
|
|
|
|
|
2019-04-03 20:59:37 +00:00
|
|
|
public editPost(postForm: PostForm) {
|
|
|
|
this.setAuth(postForm);
|
2020-02-02 03:52:16 +00:00
|
|
|
this.ws.send(this.wsSendWrapper(UserOperation.EditPost, postForm));
|
2019-04-03 20:59:37 +00:00
|
|
|
}
|
|
|
|
|
2019-04-20 04:06:25 +00:00
|
|
|
public savePost(form: SavePostForm) {
|
|
|
|
this.setAuth(form);
|
2020-02-02 03:52:16 +00:00
|
|
|
this.ws.send(this.wsSendWrapper(UserOperation.SavePost, form));
|
2019-04-20 04:06:25 +00:00
|
|
|
}
|
|
|
|
|
2019-04-15 23:12:06 +00:00
|
|
|
public banFromCommunity(form: BanFromCommunityForm) {
|
|
|
|
this.setAuth(form);
|
2020-02-02 03:52:16 +00:00
|
|
|
this.ws.send(this.wsSendWrapper(UserOperation.BanFromCommunity, form));
|
2019-04-15 23:12:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
public addModToCommunity(form: AddModToCommunityForm) {
|
|
|
|
this.setAuth(form);
|
2020-02-02 03:52:16 +00:00
|
|
|
this.ws.send(this.wsSendWrapper(UserOperation.AddModToCommunity, form));
|
2019-04-15 23:12:06 +00:00
|
|
|
}
|
|
|
|
|
2019-08-24 02:40:41 +00:00
|
|
|
public transferCommunity(form: TransferCommunityForm) {
|
|
|
|
this.setAuth(form);
|
2020-02-02 03:52:16 +00:00
|
|
|
this.ws.send(this.wsSendWrapper(UserOperation.TransferCommunity, form));
|
2019-08-24 02:40:41 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
public transferSite(form: TransferSiteForm) {
|
|
|
|
this.setAuth(form);
|
2020-02-02 03:52:16 +00:00
|
|
|
this.ws.send(this.wsSendWrapper(UserOperation.TransferSite, form));
|
2019-08-24 02:40:41 +00:00
|
|
|
}
|
2019-10-19 00:20:27 +00:00
|
|
|
|
2019-04-20 04:06:25 +00:00
|
|
|
public banUser(form: BanUserForm) {
|
|
|
|
this.setAuth(form);
|
2020-02-02 03:52:16 +00:00
|
|
|
this.ws.send(this.wsSendWrapper(UserOperation.BanUser, form));
|
2019-04-20 04:06:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
public addAdmin(form: AddAdminForm) {
|
|
|
|
this.setAuth(form);
|
2020-02-02 03:52:16 +00:00
|
|
|
this.ws.send(this.wsSendWrapper(UserOperation.AddAdmin, form));
|
2019-04-20 04:06:25 +00:00
|
|
|
}
|
|
|
|
|
2019-04-08 05:19:02 +00:00
|
|
|
public getUserDetails(form: GetUserDetailsForm) {
|
2019-04-29 21:19:00 +00:00
|
|
|
this.setAuth(form, false);
|
2020-02-02 03:52:16 +00:00
|
|
|
this.ws.send(this.wsSendWrapper(UserOperation.GetUserDetails, form));
|
2019-04-08 05:19:02 +00:00
|
|
|
}
|
2019-04-20 18:17:00 +00:00
|
|
|
|
|
|
|
public getReplies(form: GetRepliesForm) {
|
|
|
|
this.setAuth(form);
|
2020-02-02 03:52:16 +00:00
|
|
|
this.ws.send(this.wsSendWrapper(UserOperation.GetReplies, form));
|
2019-04-20 18:17:00 +00:00
|
|
|
}
|
2019-04-08 05:19:02 +00:00
|
|
|
|
2019-10-20 00:46:29 +00:00
|
|
|
public getUserMentions(form: GetUserMentionsForm) {
|
|
|
|
this.setAuth(form);
|
2020-02-02 03:52:16 +00:00
|
|
|
this.ws.send(this.wsSendWrapper(UserOperation.GetUserMentions, form));
|
2019-10-20 00:46:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
public editUserMention(form: EditUserMentionForm) {
|
|
|
|
this.setAuth(form);
|
2020-02-02 03:52:16 +00:00
|
|
|
this.ws.send(this.wsSendWrapper(UserOperation.EditUserMention, form));
|
2019-10-20 00:46:29 +00:00
|
|
|
}
|
|
|
|
|
2019-04-15 23:12:06 +00:00
|
|
|
public getModlog(form: GetModlogForm) {
|
2020-02-02 03:52:16 +00:00
|
|
|
this.ws.send(this.wsSendWrapper(UserOperation.GetModlog, form));
|
2019-04-15 23:12:06 +00:00
|
|
|
}
|
|
|
|
|
2019-04-16 23:04:23 +00:00
|
|
|
public createSite(siteForm: SiteForm) {
|
|
|
|
this.setAuth(siteForm);
|
2020-02-02 03:52:16 +00:00
|
|
|
this.ws.send(this.wsSendWrapper(UserOperation.CreateSite, siteForm));
|
2019-04-16 23:04:23 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
public editSite(siteForm: SiteForm) {
|
|
|
|
this.setAuth(siteForm);
|
2020-02-02 03:52:16 +00:00
|
|
|
this.ws.send(this.wsSendWrapper(UserOperation.EditSite, siteForm));
|
2019-04-16 23:04:23 +00:00
|
|
|
}
|
2019-04-23 22:05:50 +00:00
|
|
|
|
2019-04-16 23:04:23 +00:00
|
|
|
public getSite() {
|
2020-02-02 03:52:16 +00:00
|
|
|
this.ws.send(this.wsSendWrapper(UserOperation.GetSite, {}));
|
2019-04-16 23:04:23 +00:00
|
|
|
}
|
|
|
|
|
2019-04-23 22:05:50 +00:00
|
|
|
public search(form: SearchForm) {
|
2020-01-20 23:39:45 +00:00
|
|
|
this.setAuth(form, false);
|
2020-02-02 03:52:16 +00:00
|
|
|
this.ws.send(this.wsSendWrapper(UserOperation.Search, form));
|
2019-04-23 22:05:50 +00:00
|
|
|
}
|
|
|
|
|
2019-04-29 16:51:13 +00:00
|
|
|
public markAllAsRead() {
|
|
|
|
let form = {};
|
|
|
|
this.setAuth(form);
|
2020-02-02 03:52:16 +00:00
|
|
|
this.ws.send(this.wsSendWrapper(UserOperation.MarkAllAsRead, form));
|
2019-04-29 16:51:13 +00:00
|
|
|
}
|
|
|
|
|
2019-08-14 02:52:43 +00:00
|
|
|
public saveUserSettings(userSettingsForm: UserSettingsForm) {
|
|
|
|
this.setAuth(userSettingsForm);
|
2020-02-02 03:52:16 +00:00
|
|
|
this.ws.send(
|
2019-10-19 00:20:27 +00:00
|
|
|
this.wsSendWrapper(UserOperation.SaveUserSettings, userSettingsForm)
|
|
|
|
);
|
2019-08-14 02:52:43 +00:00
|
|
|
}
|
|
|
|
|
2019-10-18 04:25:23 +00:00
|
|
|
public deleteAccount(form: DeleteAccountForm) {
|
2019-10-15 22:09:01 +00:00
|
|
|
this.setAuth(form);
|
2020-02-02 03:52:16 +00:00
|
|
|
this.ws.send(this.wsSendWrapper(UserOperation.DeleteAccount, form));
|
2019-10-15 22:09:01 +00:00
|
|
|
}
|
|
|
|
|
2019-10-30 03:35:39 +00:00
|
|
|
public passwordReset(form: PasswordResetForm) {
|
2020-02-02 03:52:16 +00:00
|
|
|
this.ws.send(this.wsSendWrapper(UserOperation.PasswordReset, form));
|
2019-10-30 03:35:39 +00:00
|
|
|
}
|
|
|
|
|
2019-11-02 06:41:57 +00:00
|
|
|
public passwordChange(form: PasswordChangeForm) {
|
2020-02-02 03:52:16 +00:00
|
|
|
this.ws.send(this.wsSendWrapper(UserOperation.PasswordChange, form));
|
2019-11-02 06:41:57 +00:00
|
|
|
}
|
|
|
|
|
2020-01-22 21:35:29 +00:00
|
|
|
public createPrivateMessage(form: PrivateMessageForm) {
|
|
|
|
this.setAuth(form);
|
2020-02-02 03:52:16 +00:00
|
|
|
this.ws.send(this.wsSendWrapper(UserOperation.CreatePrivateMessage, form));
|
2020-01-22 21:35:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
public editPrivateMessage(form: EditPrivateMessageForm) {
|
|
|
|
this.setAuth(form);
|
2020-02-02 03:52:16 +00:00
|
|
|
this.ws.send(this.wsSendWrapper(UserOperation.EditPrivateMessage, form));
|
2020-01-22 21:35:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
public getPrivateMessages(form: GetPrivateMessagesForm) {
|
|
|
|
this.setAuth(form);
|
2020-02-02 03:52:16 +00:00
|
|
|
this.ws.send(this.wsSendWrapper(UserOperation.GetPrivateMessages, form));
|
2020-01-22 21:35:29 +00:00
|
|
|
}
|
|
|
|
|
2020-01-28 02:04:30 +00:00
|
|
|
private wsSendWrapper(op: UserOperation, data: MessageType) {
|
2019-03-26 18:00:18 +00:00
|
|
|
let send = { op: UserOperation[op], data: data };
|
2019-03-23 01:42:57 +00:00
|
|
|
console.log(send);
|
2020-02-02 03:52:16 +00:00
|
|
|
return JSON.stringify(send);
|
2019-03-23 01:42:57 +00:00
|
|
|
}
|
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) {
|
2020-01-23 03:29:11 +00:00
|
|
|
toast(i18n.t('not_logged_in'), 'danger');
|
2019-10-19 00:20:27 +00:00
|
|
|
throw 'Not logged in';
|
2019-03-26 18:00:18 +00:00
|
|
|
}
|
|
|
|
}
|
2019-03-23 01:42:57 +00:00
|
|
|
}
|
2019-03-28 19:32:08 +00:00
|
|
|
|
2019-10-19 00:20:27 +00:00
|
|
|
window.onbeforeunload = () => {
|
2020-02-02 03:52:16 +00:00
|
|
|
WebSocketService.Instance.ws.close();
|
2019-10-19 00:20:27 +00:00
|
|
|
};
|