Removing monads. Fixes #884 (#886)

* Removing monads. Fixes #884

* Fixing post fetching.

* Dont show not_logged_in error for navbar.

* Adding the lemmy-js-client RC.

* Fixing registration application mode
This commit is contained in:
Dessalines 2023-01-04 11:56:24 -05:00 committed by GitHub
parent 37c200571b
commit b64f47cfe9
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
66 changed files with 5186 additions and 6250 deletions

View file

@ -1,4 +1,3 @@
import { None, Option, Some } from "@sniptt/monads";
import { Component, linkEvent } from "inferno";
import {
AddModToCommunityResponse,
@ -22,7 +21,6 @@ import {
PostView,
PurgeItemResponse,
SortType,
toOption,
UserOperation,
wsJsonToRes,
wsUserOp,
@ -36,7 +34,6 @@ import {
} from "../../interfaces";
import { UserService, WebSocketService } from "../../services";
import {
auth,
commentsToFlatNodes,
communityRSSUrl,
createCommentLikeRes,
@ -50,6 +47,7 @@ import {
getPageFromProps,
getSortTypeFromProps,
isPostBlocked,
myAuth,
notifyPost,
nsfwCheck,
postToCommentSortType,
@ -79,7 +77,7 @@ import { PostListings } from "../post/post-listings";
import { CommunityLink } from "./community-link";
interface State {
communityRes: Option<GetCommunityResponse>;
communityRes?: GetCommunityResponse;
siteRes: GetSiteResponse;
communityName: string;
communityLoading: boolean;
@ -106,15 +104,9 @@ interface UrlParams {
}
export class Community extends Component<any, State> {
private isoData = setIsoData(
this.context,
GetCommunityResponse,
GetPostsResponse,
GetCommentsResponse
);
private subscription: Subscription;
private emptyState: State = {
communityRes: None,
private isoData = setIsoData(this.context);
private subscription?: Subscription;
state: State = {
communityName: this.props.match.params.name,
communityLoading: true,
postsLoading: true,
@ -131,7 +123,6 @@ export class Community extends Component<any, State> {
constructor(props: any, context: any) {
super(props, context);
this.state = this.emptyState;
this.handleSortChange = this.handleSortChange.bind(this);
this.handleDataTypeChange = this.handleDataTypeChange.bind(this);
this.handlePageChange = this.handlePageChange.bind(this);
@ -143,17 +134,19 @@ export class Community extends Component<any, State> {
if (this.isoData.path == this.context.router.route.match.url) {
this.state = {
...this.state,
communityRes: Some(this.isoData.routeData[0] as GetCommunityResponse),
communityRes: this.isoData.routeData[0] as GetCommunityResponse,
};
let postsRes = Some(this.isoData.routeData[1] as GetPostsResponse);
let commentsRes = Some(this.isoData.routeData[2] as GetCommentsResponse);
let postsRes = this.isoData.routeData[1] as GetPostsResponse | undefined;
let commentsRes = this.isoData.routeData[2] as
| GetCommentsResponse
| undefined;
if (postsRes.isSome()) {
this.state = { ...this.state, posts: postsRes.unwrap().posts };
if (postsRes) {
this.state = { ...this.state, posts: postsRes.posts };
}
if (commentsRes.isSome()) {
this.state = { ...this.state, comments: commentsRes.unwrap().comments };
if (commentsRes) {
this.state = { ...this.state, comments: commentsRes.comments };
}
this.state = {
@ -169,11 +162,10 @@ export class Community extends Component<any, State> {
}
fetchCommunity() {
let form = new GetCommunity({
name: Some(this.state.communityName),
id: None,
auth: auth(false).ok(),
});
let form: GetCommunity = {
name: this.state.communityName,
auth: myAuth(false),
};
WebSocketService.Instance.send(wsClient.getCommunity(form));
}
@ -183,7 +175,7 @@ export class Community extends Component<any, State> {
componentWillUnmount() {
saveScrollPosition(this.context);
this.subscription.unsubscribe();
this.subscription?.unsubscribe();
}
static getDerivedStateFromProps(props: any): CommunityProps {
@ -199,58 +191,50 @@ export class Community extends Component<any, State> {
let promises: Promise<any>[] = [];
let communityName = pathSplit[2];
let communityForm = new GetCommunity({
name: Some(communityName),
id: None,
let communityForm: GetCommunity = {
name: communityName,
auth: req.auth,
});
};
promises.push(req.client.getCommunity(communityForm));
let dataType: DataType = pathSplit[4]
? DataType[pathSplit[4]]
: DataType.Post;
let sort: Option<SortType> = toOption(
pathSplit[6]
? SortType[pathSplit[6]]
: UserService.Instance.myUserInfo.match({
some: mui =>
Object.values(SortType)[
mui.local_user_view.local_user.default_sort_type
],
none: SortType.Active,
})
);
let mui = UserService.Instance.myUserInfo;
let page = toOption(pathSplit[8] ? Number(pathSplit[8]) : 1);
let sort: SortType = pathSplit[6]
? SortType[pathSplit[6]]
: mui
? Object.values(SortType)[
mui.local_user_view.local_user.default_sort_type
]
: SortType.Active;
let page = pathSplit[8] ? Number(pathSplit[8]) : 1;
if (dataType == DataType.Post) {
let getPostsForm = new GetPosts({
community_name: Some(communityName),
community_id: None,
let getPostsForm: GetPosts = {
community_name: communityName,
page,
limit: Some(fetchLimit),
limit: fetchLimit,
sort,
type_: Some(ListingType.All),
saved_only: Some(false),
type_: ListingType.All,
saved_only: false,
auth: req.auth,
});
};
promises.push(req.client.getPosts(getPostsForm));
promises.push(Promise.resolve());
} else {
let getCommentsForm = new GetComments({
community_name: Some(communityName),
community_id: None,
let getCommentsForm: GetComments = {
community_name: communityName,
page,
limit: Some(fetchLimit),
max_depth: None,
sort: sort.map(postToCommentSortType),
type_: Some(ListingType.All),
saved_only: Some(false),
post_id: None,
parent_id: None,
limit: fetchLimit,
sort: postToCommentSortType(sort),
type_: ListingType.All,
saved_only: false,
auth: req.auth,
});
};
promises.push(Promise.resolve());
promises.push(req.client.getComments(getCommentsForm));
}
@ -270,23 +254,19 @@ export class Community extends Component<any, State> {
}
get documentTitle(): string {
return this.state.communityRes.match({
some: res =>
`${res.community_view.community.title} - ${this.state.siteRes.site_view.site.name}`,
none: "",
});
let cRes = this.state.communityRes;
return cRes
? `${cRes.community_view.community.title} - ${this.state.siteRes.site_view.site.name}`
: "";
}
render() {
// For some reason, this returns an empty vec if it matches the site langs
let communityLangs = this.state.communityRes.map(r => {
let langs = r.discussion_languages;
if (langs.length == 0) {
return this.state.siteRes.all_languages.map(l => l.id);
} else {
return langs;
}
});
let res = this.state.communityRes;
let communityLangs =
res?.discussion_languages.length == 0
? this.state.siteRes.all_languages.map(l => l.id)
: res?.discussion_languages;
return (
<div className="container-lg">
@ -295,103 +275,86 @@ export class Community extends Component<any, State> {
<Spinner large />
</h5>
) : (
this.state.communityRes.match({
some: res => (
<>
<HtmlTags
title={this.documentTitle}
path={this.context.router.route.match.url}
description={res.community_view.community.description}
image={res.community_view.community.icon}
/>
res && (
<>
<HtmlTags
title={this.documentTitle}
path={this.context.router.route.match.url}
description={res.community_view.community.description}
image={res.community_view.community.icon}
/>
<div className="row">
<div className="col-12 col-md-8">
{this.communityInfo()}
<div className="d-block d-md-none">
<button
className="btn btn-secondary d-inline-block mb-2 mr-3"
onClick={linkEvent(this, this.handleShowSidebarMobile)}
>
{i18n.t("sidebar")}{" "}
<Icon
icon={
this.state.showSidebarMobile
? `minus-square`
: `plus-square`
<div className="row">
<div className="col-12 col-md-8">
{this.communityInfo()}
<div className="d-block d-md-none">
<button
className="btn btn-secondary d-inline-block mb-2 mr-3"
onClick={linkEvent(this, this.handleShowSidebarMobile)}
>
{i18n.t("sidebar")}{" "}
<Icon
icon={
this.state.showSidebarMobile
? `minus-square`
: `plus-square`
}
classes="icon-inline"
/>
</button>
{this.state.showSidebarMobile && (
<>
<Sidebar
community_view={res.community_view}
moderators={res.moderators}
admins={this.state.siteRes.admins}
online={res.online}
enableNsfw={enableNsfw(this.state.siteRes)}
editable
allLanguages={this.state.siteRes.all_languages}
siteLanguages={
this.state.siteRes.discussion_languages
}
classes="icon-inline"
communityLanguages={communityLangs}
/>
</button>
{this.state.showSidebarMobile && (
<>
<Sidebar
community_view={res.community_view}
moderators={res.moderators}
admins={this.state.siteRes.admins}
online={res.online}
enableNsfw={enableNsfw(this.state.siteRes)}
editable
allLanguages={this.state.siteRes.all_languages}
siteLanguages={
this.state.siteRes.discussion_languages
}
communityLanguages={communityLangs}
/>
{!res.community_view.community.local &&
res.site.match({
some: site => (
<SiteSidebar
site={site}
showLocal={showLocal(this.isoData)}
admins={None}
counts={None}
online={None}
/>
),
none: <></>,
})}
</>
)}
</div>
{this.selects()}
{this.listings()}
<Paginator
page={this.state.page}
onChange={this.handlePageChange}
/>
</div>
<div className="d-none d-md-block col-md-4">
<Sidebar
community_view={res.community_view}
moderators={res.moderators}
admins={this.state.siteRes.admins}
online={res.online}
enableNsfw={enableNsfw(this.state.siteRes)}
editable
allLanguages={this.state.siteRes.all_languages}
siteLanguages={this.state.siteRes.discussion_languages}
communityLanguages={communityLangs}
/>
{!res.community_view.community.local &&
res.site.match({
some: site => (
{!res.community_view.community.local && res.site && (
<SiteSidebar
site={site}
site={res.site}
showLocal={showLocal(this.isoData)}
admins={None}
counts={None}
online={None}
/>
),
none: <></>,
})}
)}
</>
)}
</div>
{this.selects()}
{this.listings()}
<Paginator
page={this.state.page}
onChange={this.handlePageChange}
/>
</div>
</>
),
none: <></>,
})
<div className="d-none d-md-block col-md-4">
<Sidebar
community_view={res.community_view}
moderators={res.moderators}
admins={this.state.siteRes.admins}
online={res.online}
enableNsfw={enableNsfw(this.state.siteRes)}
editable
allLanguages={this.state.siteRes.all_languages}
siteLanguages={this.state.siteRes.discussion_languages}
communityLanguages={communityLangs}
/>
{!res.community_view.community.local && res.site && (
<SiteSidebar
site={res.site}
showLocal={showLocal(this.isoData)}
/>
)}
</div>
</div>
</>
)
)}
</div>
);
@ -424,9 +387,8 @@ export class Community extends Component<any, State> {
noIndent
showContext
enableDownvotes={enableDownvotes(this.state.siteRes)}
moderators={this.state.communityRes.map(r => r.moderators)}
admins={Some(this.state.siteRes.admins)}
maxCommentsShown={None}
moderators={this.state.communityRes?.moderators}
admins={this.state.siteRes.admins}
allLanguages={this.state.siteRes.all_languages}
siteLanguages={this.state.siteRes.discussion_languages}
/>
@ -434,30 +396,33 @@ export class Community extends Component<any, State> {
}
communityInfo() {
return this.state.communityRes
.map(r => r.community_view.community)
.match({
some: community => (
<div className="mb-2">
<BannerIconHeader banner={community.banner} icon={community.icon} />
<h5 className="mb-0 overflow-wrap-anywhere">{community.title}</h5>
<CommunityLink
community={community}
realLink
useApubName
muted
hideAvatar
/>
</div>
),
none: <></>,
});
let community = this.state.communityRes?.community_view.community;
return (
community && (
<div className="mb-2">
<BannerIconHeader banner={community.banner} icon={community.icon} />
<h5 className="mb-0 overflow-wrap-anywhere">{community.title}</h5>
<CommunityLink
community={community}
realLink
useApubName
muted
hideAvatar
/>
</div>
)
);
}
selects() {
let communityRss = this.state.communityRes.map(r =>
communityRSSUrl(r.community_view.community.actor_id, this.state.sort)
);
// let communityRss = this.state.communityRes.map(r =>
// communityRSSUrl(r.community_view.community.actor_id, this.state.sort)
// );
let res = this.state.communityRes;
let communityRss = res
? communityRSSUrl(res.community_view.community.actor_id, this.state.sort)
: undefined;
return (
<div className="mb-3">
<span className="mr-3">
@ -469,17 +434,18 @@ export class Community extends Component<any, State> {
<span className="mr-2">
<SortSelect sort={this.state.sort} onChange={this.handleSortChange} />
</span>
{communityRss.match({
some: rss => (
<>
<a href={rss} title="RSS" rel={relTags}>
<Icon icon="rss" classes="text-muted small" />
</a>
<link rel="alternate" type="application/atom+xml" href={rss} />
</>
),
none: <></>,
})}
{communityRss && (
<>
<a href={communityRss} title="RSS" rel={relTags}>
<Icon icon="rss" classes="text-muted small" />
</a>
<link
rel="alternate"
type="application/atom+xml"
href={communityRss}
/>
</>
)}
</div>
);
}
@ -517,31 +483,26 @@ export class Community extends Component<any, State> {
fetchData() {
if (this.state.dataType == DataType.Post) {
let form = new GetPosts({
page: Some(this.state.page),
limit: Some(fetchLimit),
sort: Some(this.state.sort),
type_: Some(ListingType.All),
community_name: Some(this.state.communityName),
community_id: None,
saved_only: Some(false),
auth: auth(false).ok(),
});
let form: GetPosts = {
page: this.state.page,
limit: fetchLimit,
sort: this.state.sort,
type_: ListingType.All,
community_name: this.state.communityName,
saved_only: false,
auth: myAuth(false),
};
WebSocketService.Instance.send(wsClient.getPosts(form));
} else {
let form = new GetComments({
page: Some(this.state.page),
limit: Some(fetchLimit),
max_depth: None,
sort: Some(postToCommentSortType(this.state.sort)),
type_: Some(ListingType.All),
community_name: Some(this.state.communityName),
community_id: None,
saved_only: Some(false),
post_id: None,
parent_id: None,
auth: auth(false).ok(),
});
let form: GetComments = {
page: this.state.page,
limit: fetchLimit,
sort: postToCommentSortType(this.state.sort),
type_: ListingType.All,
community_name: this.state.communityName,
saved_only: false,
auth: myAuth(false),
};
WebSocketService.Instance.send(wsClient.getComments(form));
}
}
@ -549,25 +510,23 @@ export class Community extends Component<any, State> {
parseMessage(msg: any) {
let op = wsUserOp(msg);
console.log(msg);
let res = this.state.communityRes;
if (msg.error) {
toast(i18n.t(msg.error), "danger");
this.context.router.history.push("/");
return;
} else if (msg.reconnect) {
this.state.communityRes.match({
some: res => {
WebSocketService.Instance.send(
wsClient.communityJoin({
community_id: res.community_view.community.id,
})
);
},
none: void 0,
});
if (res) {
WebSocketService.Instance.send(
wsClient.communityJoin({
community_id: res.community_view.community.id,
})
);
}
this.fetchData();
} else if (op == UserOperation.GetCommunity) {
let data = wsJsonToRes<GetCommunityResponse>(msg, GetCommunityResponse);
this.setState({ communityRes: Some(data), communityLoading: false });
let data = wsJsonToRes<GetCommunityResponse>(msg);
this.setState({ communityRes: data, communityLoading: false });
// TODO why is there no auth in this form?
WebSocketService.Instance.send(
wsClient.communityJoin({
@ -579,28 +538,22 @@ export class Community extends Component<any, State> {
op == UserOperation.DeleteCommunity ||
op == UserOperation.RemoveCommunity
) {
let data = wsJsonToRes<CommunityResponse>(msg, CommunityResponse);
this.state.communityRes.match({
some: res => {
res.community_view = data.community_view;
res.discussion_languages = data.discussion_languages;
},
none: void 0,
});
let data = wsJsonToRes<CommunityResponse>(msg);
if (res) {
res.community_view = data.community_view;
res.discussion_languages = data.discussion_languages;
}
this.setState(this.state);
} else if (op == UserOperation.FollowCommunity) {
let data = wsJsonToRes<CommunityResponse>(msg, CommunityResponse);
this.state.communityRes.match({
some: res => {
res.community_view.subscribed = data.community_view.subscribed;
res.community_view.counts.subscribers =
data.community_view.counts.subscribers;
},
none: void 0,
});
let data = wsJsonToRes<CommunityResponse>(msg);
if (res) {
res.community_view.subscribed = data.community_view.subscribed;
res.community_view.counts.subscribers =
data.community_view.counts.subscribers;
}
this.setState(this.state);
} else if (op == UserOperation.GetPosts) {
let data = wsJsonToRes<GetPostsResponse>(msg, GetPostsResponse);
let data = wsJsonToRes<GetPostsResponse>(msg);
this.setState({ posts: data.posts, postsLoading: false });
restoreScrollPosition(this.context);
setupTippy();
@ -612,15 +565,15 @@ export class Community extends Component<any, State> {
op == UserOperation.FeaturePost ||
op == UserOperation.SavePost
) {
let data = wsJsonToRes<PostResponse>(msg, PostResponse);
let data = wsJsonToRes<PostResponse>(msg);
editPostFindRes(data.post_view, this.state.posts);
this.setState(this.state);
} else if (op == UserOperation.CreatePost) {
let data = wsJsonToRes<PostResponse>(msg, PostResponse);
let data = wsJsonToRes<PostResponse>(msg);
let showPostNotifs = UserService.Instance.myUserInfo
.map(m => m.local_user_view.local_user.show_new_post_notifs)
.unwrapOr(false);
let showPostNotifs =
UserService.Instance.myUserInfo?.local_user_view.local_user
.show_new_post_notifs;
// Only push these if you're on the first page, you pass the nsfw check, and it isn't blocked
//
@ -636,24 +589,17 @@ export class Community extends Component<any, State> {
this.setState(this.state);
}
} else if (op == UserOperation.CreatePostLike) {
let data = wsJsonToRes<PostResponse>(msg, PostResponse);
let data = wsJsonToRes<PostResponse>(msg);
createPostLikeFindRes(data.post_view, this.state.posts);
this.setState(this.state);
} else if (op == UserOperation.AddModToCommunity) {
let data = wsJsonToRes<AddModToCommunityResponse>(
msg,
AddModToCommunityResponse
);
this.state.communityRes.match({
some: res => (res.moderators = data.moderators),
none: void 0,
});
let data = wsJsonToRes<AddModToCommunityResponse>(msg);
if (res) {
res.moderators = data.moderators;
}
this.setState(this.state);
} else if (op == UserOperation.BanFromCommunity) {
let data = wsJsonToRes<BanFromCommunityResponse>(
msg,
BanFromCommunityResponse
);
let data = wsJsonToRes<BanFromCommunityResponse>(msg);
// TODO this might be incorrect
this.state.posts
@ -662,18 +608,18 @@ export class Community extends Component<any, State> {
this.setState(this.state);
} else if (op == UserOperation.GetComments) {
let data = wsJsonToRes<GetCommentsResponse>(msg, GetCommentsResponse);
let data = wsJsonToRes<GetCommentsResponse>(msg);
this.setState({ comments: data.comments, commentsLoading: false });
} else if (
op == UserOperation.EditComment ||
op == UserOperation.DeleteComment ||
op == UserOperation.RemoveComment
) {
let data = wsJsonToRes<CommentResponse>(msg, CommentResponse);
let data = wsJsonToRes<CommentResponse>(msg);
editCommentRes(data.comment_view, this.state.comments);
this.setState(this.state);
} else if (op == UserOperation.CreateComment) {
let data = wsJsonToRes<CommentResponse>(msg, CommentResponse);
let data = wsJsonToRes<CommentResponse>(msg);
// Necessary since it might be a user reply
if (data.form_id) {
@ -681,41 +627,37 @@ export class Community extends Component<any, State> {
this.setState(this.state);
}
} else if (op == UserOperation.SaveComment) {
let data = wsJsonToRes<CommentResponse>(msg, CommentResponse);
let data = wsJsonToRes<CommentResponse>(msg);
saveCommentRes(data.comment_view, this.state.comments);
this.setState(this.state);
} else if (op == UserOperation.CreateCommentLike) {
let data = wsJsonToRes<CommentResponse>(msg, CommentResponse);
let data = wsJsonToRes<CommentResponse>(msg);
createCommentLikeRes(data.comment_view, this.state.comments);
this.setState(this.state);
} else if (op == UserOperation.BlockPerson) {
let data = wsJsonToRes<BlockPersonResponse>(msg, BlockPersonResponse);
let data = wsJsonToRes<BlockPersonResponse>(msg);
updatePersonBlock(data);
} else if (op == UserOperation.CreatePostReport) {
let data = wsJsonToRes<PostReportResponse>(msg, PostReportResponse);
let data = wsJsonToRes<PostReportResponse>(msg);
if (data) {
toast(i18n.t("report_created"));
}
} else if (op == UserOperation.CreateCommentReport) {
let data = wsJsonToRes<CommentReportResponse>(msg, CommentReportResponse);
let data = wsJsonToRes<CommentReportResponse>(msg);
if (data) {
toast(i18n.t("report_created"));
}
} else if (op == UserOperation.PurgeCommunity) {
let data = wsJsonToRes<PurgeItemResponse>(msg, PurgeItemResponse);
let data = wsJsonToRes<PurgeItemResponse>(msg);
if (data.success) {
toast(i18n.t("purge_success"));
this.context.router.history.push(`/`);
}
} else if (op == UserOperation.BlockCommunity) {
let data = wsJsonToRes<BlockCommunityResponse>(
msg,
BlockCommunityResponse
);
this.state.communityRes.match({
some: res => (res.community_view.blocked = data.blocked),
none: void 0,
});
let data = wsJsonToRes<BlockCommunityResponse>(msg);
if (res) {
res.community_view.blocked = data.blocked;
}
updateCommunityBlock(data);
this.setState(this.state);
}