lemmy/ui/src/services/WebSocketService.ts

318 lines
8.6 KiB
TypeScript
Raw Normal View History

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