Strictly typing websocket forms.

This commit is contained in:
Dessalines 2020-01-27 21:04:30 -05:00
parent 22883c94ea
commit e7c90bee01
5 changed files with 81 additions and 21 deletions

View file

@ -11,6 +11,7 @@ import {
SortType, SortType,
Post, Post,
GetPostsForm, GetPostsForm,
GetCommunityForm,
ListingType, ListingType,
GetPostsResponse, GetPostsResponse,
CreatePostLikeResponse, CreatePostLikeResponse,
@ -98,11 +99,11 @@ export class Community extends Component<any, State> {
() => console.log('complete') () => console.log('complete')
); );
if (this.state.communityId) { let form: GetCommunityForm = {
WebSocketService.Instance.getCommunity(this.state.communityId); id: this.state.communityId ? this.state.communityId : null,
} else if (this.state.communityName) { name: this.state.communityName ? this.state.communityName : null,
WebSocketService.Instance.getCommunityByName(this.state.communityName); };
} WebSocketService.Instance.getCommunity(form);
} }
componentWillUnmount() { componentWillUnmount() {

View file

@ -38,6 +38,8 @@ enum UnreadType {
Messages, Messages,
} }
type ReplyType = Comment | PrivateMessageI;
interface InboxState { interface InboxState {
unreadOrAll: UnreadOrAll; unreadOrAll: UnreadOrAll;
unreadType: UnreadType; unreadType: UnreadType;
@ -186,7 +188,7 @@ export class Inbox extends Component<any, InboxState> {
} }
all() { all() {
let combined: Array<Comment | PrivateMessageI> = []; let combined: Array<ReplyType> = [];
combined.push(...this.state.replies); combined.push(...this.state.replies);
combined.push(...this.state.mentions); combined.push(...this.state.mentions);

View file

@ -23,6 +23,7 @@ import {
SearchType, SearchType,
SortType, SortType,
SearchForm, SearchForm,
GetPostForm,
SearchResponse, SearchResponse,
GetSiteResponse, GetSiteResponse,
GetCommunityResponse, GetCommunityResponse,
@ -84,7 +85,10 @@ export class Post extends Component<any, PostState> {
() => console.log('complete') () => console.log('complete')
); );
WebSocketService.Instance.getPost(postId); let form: GetPostForm = {
id: postId,
};
WebSocketService.Instance.getPost(form);
} }
componentWillUnmount() { componentWillUnmount() {

54
ui/src/interfaces.ts vendored
View file

@ -248,6 +248,10 @@ export interface FollowCommunityForm {
auth?: string; auth?: string;
} }
export interface GetFollowedCommunitiesForm {
auth: string;
}
export interface GetFollowedCommunitiesResponse { export interface GetFollowedCommunitiesResponse {
communities: Array<CommunityUser>; communities: Array<CommunityUser>;
} }
@ -523,6 +527,12 @@ export interface CommunityForm {
auth?: string; auth?: string;
} }
export interface GetCommunityForm {
id?: number;
name?: string;
auth?: string;
}
export interface GetCommunityResponse { export interface GetCommunityResponse {
community: Community; community: Community;
moderators: Array<CommunityUser>; moderators: Array<CommunityUser>;
@ -572,6 +582,11 @@ export interface PostFormParams {
community?: string; community?: string;
} }
export interface GetPostForm {
id: number;
auth?: string;
}
export interface GetPostResponse { export interface GetPostResponse {
post: Post; post: Post;
comments: Array<Comment>; comments: Array<Comment>;
@ -759,6 +774,45 @@ export interface PrivateMessageResponse {
message: PrivateMessage; message: PrivateMessage;
} }
export type MessageType =
| EditPrivateMessageForm
| LoginForm
| RegisterForm
| CommunityForm
| FollowCommunityForm
| ListCommunitiesForm
| GetFollowedCommunitiesForm
| PostForm
| GetPostForm
| GetPostsForm
| GetCommunityForm
| CommentForm
| CommentLikeForm
| SaveCommentForm
| CreatePostLikeForm
| BanFromCommunityForm
| AddAdminForm
| AddModToCommunityForm
| TransferCommunityForm
| TransferSiteForm
| SaveCommentForm
| BanUserForm
| AddAdminForm
| GetUserDetailsForm
| GetRepliesForm
| GetUserMentionsForm
| EditUserMentionForm
| GetModlogForm
| SiteForm
| SearchForm
| UserSettingsForm
| DeleteAccountForm
| PasswordResetForm
| PasswordChangeForm
| PrivateMessageForm
| EditPrivateMessageForm
| GetPrivateMessagesForm;
type ResponseType = type ResponseType =
| SiteResponse | SiteResponse
| GetFollowedCommunitiesResponse | GetFollowedCommunitiesResponse

View file

@ -9,9 +9,12 @@ import {
CommentForm, CommentForm,
SaveCommentForm, SaveCommentForm,
CommentLikeForm, CommentLikeForm,
GetPostForm,
GetPostsForm, GetPostsForm,
CreatePostLikeForm, CreatePostLikeForm,
GetCommunityForm,
FollowCommunityForm, FollowCommunityForm,
GetFollowedCommunitiesForm,
GetUserDetailsForm, GetUserDetailsForm,
ListCommunitiesForm, ListCommunitiesForm,
GetModlogForm, GetModlogForm,
@ -35,6 +38,7 @@ import {
PrivateMessageForm, PrivateMessageForm,
EditPrivateMessageForm, EditPrivateMessageForm,
GetPrivateMessagesForm, GetPrivateMessagesForm,
MessageType,
} from '../interfaces'; } from '../interfaces';
import { webSocket } from 'rxjs/webSocket'; import { webSocket } from 'rxjs/webSocket';
import { Subject } from 'rxjs'; import { Subject } from 'rxjs';
@ -108,9 +112,9 @@ export class WebSocketService {
} }
public getFollowedCommunities() { public getFollowedCommunities() {
let data = { auth: UserService.Instance.auth }; let form: GetFollowedCommunitiesForm = { auth: UserService.Instance.auth };
this.subject.next( this.subject.next(
this.wsSendWrapper(UserOperation.GetFollowedCommunities, data) this.wsSendWrapper(UserOperation.GetFollowedCommunities, form)
); );
} }
@ -125,19 +129,14 @@ export class WebSocketService {
this.subject.next(this.wsSendWrapper(UserOperation.CreatePost, postForm)); this.subject.next(this.wsSendWrapper(UserOperation.CreatePost, postForm));
} }
public getPost(postId: number) { public getPost(form: GetPostForm) {
let data = { id: postId, auth: UserService.Instance.auth }; this.setAuth(form);
this.subject.next(this.wsSendWrapper(UserOperation.GetPost, data)); this.subject.next(this.wsSendWrapper(UserOperation.GetPost, form));
} }
public getCommunity(communityId: number) { public getCommunity(form: GetCommunityForm) {
let data = { id: communityId, auth: UserService.Instance.auth }; this.setAuth(form);
this.subject.next(this.wsSendWrapper(UserOperation.GetCommunity, data)); this.subject.next(this.wsSendWrapper(UserOperation.GetCommunity, form));
}
public getCommunityByName(name: string) {
let data = { name: name, auth: UserService.Instance.auth };
this.subject.next(this.wsSendWrapper(UserOperation.GetCommunity, data));
} }
public createComment(commentForm: CommentForm) { public createComment(commentForm: CommentForm) {
@ -310,7 +309,7 @@ export class WebSocketService {
); );
} }
private wsSendWrapper(op: UserOperation, data: any) { private wsSendWrapper(op: UserOperation, data: MessageType) {
let send = { op: UserOperation[op], data: data }; let send = { op: UserOperation[op], data: data };
console.log(send); console.log(send);
return send; return send;