summaryrefslogtreecommitdiffstats
path: root/ui/src/services/WebSocketService.ts
blob: c192c2b77add21af3462149398dad568674f874d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
import { wsUri } from '../env';
import { LoginForm, RegisterForm, UserOperation, CommunityForm, PostForm, SavePostForm, CommentForm, SaveCommentForm, CommentLikeForm, GetPostsForm, CreatePostLikeForm, FollowCommunityForm, GetUserDetailsForm, ListCommunitiesForm, GetModlogForm, BanFromCommunityForm, AddModToCommunityForm, AddAdminForm, BanUserForm, SiteForm, Site, UserView, GetRepliesForm, SearchForm } from '../interfaces';
import { webSocket } from 'rxjs/webSocket';
import { Subject } from 'rxjs';
import { retryWhen, delay, take } from 'rxjs/operators';
import { UserService } from './';
import { i18n } from '../i18next';

export class WebSocketService {
  private static _instance: WebSocketService;
  public subject: Subject<any>;

  public site: Site;
  public admins: Array<UserView>;
  public banned: Array<UserView>;

  private constructor() {
    this.subject = webSocket(wsUri);

    // Necessary to not keep reconnecting
    this.subject
      .pipe(retryWhen(errors => errors.pipe(delay(60000), take(999))))
      .subscribe();

      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) {
    this.setAuth(communityForm);
    this.subject.next(this.wsSendWrapper(UserOperation.CreateCommunity, communityForm));
  }

  public editCommunity(communityForm: CommunityForm) {
    this.setAuth(communityForm);
    this.subject.next(this.wsSendWrapper(UserOperation.EditCommunity, communityForm));
  }

  public followCommunity(followCommunityForm: FollowCommunityForm) {
    this.setAuth(followCommunityForm);
    this.subject.next(this.wsSendWrapper(UserOperation.FollowCommunity, followCommunityForm));
  }

  public listCommunities(form: ListCommunitiesForm) {
    this.setAuth(form, false);
    this.subject.next(this.wsSendWrapper(UserOperation.ListCommunities, form));
  }

  public getFollowedCommunities() {
    let data = {auth: UserService.Instance.auth };
    this.subject.next(this.wsSendWrapper(UserOperation.GetFollowedCommunities, data));
  }

  public listCategories() {
    this.subject.next(this.wsSendWrapper(UserOperation.ListCategories, undefined));
  }

  public createPost(postForm: PostForm) {
    this.setAuth(postForm);
    this.subject.next(this.wsSendWrapper(UserOperation.CreatePost, postForm));
  }

  public getPost(postId: number) {
    let data = {id: postId, auth: UserService.Instance.auth };
    this.subject.next(this.wsSendWrapper(UserOperation.GetPost, data));
  }

  public getCommunity(communityId: number) {
    let data = {id: communityId, auth: UserService.Instance.auth };
    this.subject.next(this.wsSendWrapper(UserOperation.GetCommunity, data));
  }

  public getCommunityByName(name: string) {
    let data = {name: name, auth: UserService.Instance.auth };
    this.subject.next(this.wsSendWrapper(UserOperation.GetCommunity, data));
  }

  public createComment(commentForm: CommentForm) {
    this.setAuth(commentForm);
    this.subject.next(this.wsSendWrapper(UserOperation.CreateComment, commentForm));
  }

  public editComment(commentForm: CommentForm) {
    this.setAuth(commentForm);
    this.subject.next(this.wsSendWrapper(UserOperation.EditComment, commentForm));
  }

  public likeComment(form: CommentLikeForm) {
    this.setAuth(form);
    this.subject.next(this.wsSendWrapper(UserOperation.CreateCommentLike, form));
  }

  public saveComment(form: SaveCommentForm) {
    this.setAuth(form);
    this.subject.next(this.wsSendWrapper(UserOperation.SaveComment, form));
  }

  public getPosts(form: GetPostsForm) {
    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));
  }

  public editPost(postForm: PostForm) {
    this.setAuth(postForm);
    this.subject.next(this.wsSendWrapper(UserOperation.EditPost, postForm));
  }

  public savePost(form: SavePostForm) {
    this.setAuth(form);
    this.subject.next(this.wsSendWrapper(UserOperation.SavePost, form));
  }

  public banFromCommunity(form: BanFromCommunityForm) {
    this.setAuth(form);
    this.subject.next(this.wsSendWrapper(UserOperation.BanFromCommunity, form));
  }

  public addModToCommunity(form: AddModToCommunityForm) {
    this.setAuth(form);
    this.subject.next(this.wsSendWrapper(UserOperation.AddModToCommunity, form));
  }

  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));
  }

  public getUserDetails(form: GetUserDetailsForm) {
    this.setAuth(form, false);
    this.subject.next(this.wsSendWrapper(UserOperation.GetUserDetails, form));
  }

  public getReplies(form: GetRepliesForm) {
    this.setAuth(form);
    this.subject.next(this.wsSendWrapper(UserOperation.GetReplies, form));
  }

  public getModlog(form: GetModlogForm) {
    this.subject.next(this.wsSendWrapper(UserOperation.GetModlog, form));
  }

  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));
  }

  public getSite() {
    this.subject.next(this.wsSendWrapper(UserOperation.GetSite, undefined));
  }

  public search(form: SearchForm) {
    this.subject.next(this.wsSendWrapper(UserOperation.Search, form));
  }

  public markAllAsRead() {
    let form = {};
    this.setAuth(form);
    this.subject.next(this.wsSendWrapper(UserOperation.MarkAllAsRead, form));
  }

  private wsSendWrapper(op: UserOperation, data: any) {
    let send = { op: UserOperation[op], data: data };
    console.log(send);
    return send;
  }

  private setAuth(obj: any, throwErr: boolean = true) {
    obj.auth = UserService.Instance.auth;
    if (obj.auth == null && throwErr) {
      alert(i18n.t('not_logged_in'));
      throw "Not logged in";
    }
  }
}

window.onbeforeunload = (() => {
  WebSocketService.Instance.subject.unsubscribe();
  WebSocketService.Instance.subject = null;
});