2021-07-18 03:56:33 +00:00
|
|
|
import { Component, linkEvent } from "inferno";
|
2023-04-15 14:47:10 +00:00
|
|
|
import { RouteComponentProps } from "inferno-router/dist/Route";
|
2020-09-06 16:15:25 +00:00
|
|
|
import {
|
|
|
|
AddModToCommunityResponse,
|
|
|
|
BanFromCommunityResponse,
|
2022-07-30 03:38:37 +00:00
|
|
|
BlockCommunityResponse,
|
2021-08-20 02:56:18 +00:00
|
|
|
BlockPersonResponse,
|
2021-07-17 20:42:55 +00:00
|
|
|
CommentResponse,
|
2020-12-24 01:58:27 +00:00
|
|
|
CommentView,
|
2021-07-17 20:42:55 +00:00
|
|
|
CommunityResponse,
|
2020-12-24 01:58:27 +00:00
|
|
|
GetComments,
|
2020-09-06 16:15:25 +00:00
|
|
|
GetCommentsResponse,
|
2021-07-17 20:42:55 +00:00
|
|
|
GetCommunity,
|
|
|
|
GetCommunityResponse,
|
|
|
|
GetPosts,
|
|
|
|
GetPostsResponse,
|
2021-09-28 10:38:59 +00:00
|
|
|
PostReportResponse,
|
2021-07-17 20:42:55 +00:00
|
|
|
PostResponse,
|
|
|
|
PostView,
|
2022-06-23 19:44:05 +00:00
|
|
|
PurgeItemResponse,
|
2021-07-17 20:42:55 +00:00
|
|
|
SortType,
|
|
|
|
UserOperation,
|
2022-06-21 21:42:29 +00:00
|
|
|
wsJsonToRes,
|
|
|
|
wsUserOp,
|
2021-02-22 02:39:04 +00:00
|
|
|
} from "lemmy-js-client";
|
2021-07-17 20:42:55 +00:00
|
|
|
import { Subscription } from "rxjs";
|
|
|
|
import { i18n } from "../../i18next";
|
2022-07-30 13:28:08 +00:00
|
|
|
import {
|
|
|
|
CommentViewType,
|
|
|
|
DataType,
|
|
|
|
InitialFetchRequest,
|
|
|
|
} from "../../interfaces";
|
2021-07-17 20:42:55 +00:00
|
|
|
import { UserService, WebSocketService } from "../../services";
|
2020-09-06 16:15:25 +00:00
|
|
|
import {
|
2023-05-11 17:06:32 +00:00
|
|
|
QueryParams,
|
2023-05-30 00:40:00 +00:00
|
|
|
WithPromiseKeys,
|
2021-07-17 20:42:55 +00:00
|
|
|
commentsToFlatNodes,
|
|
|
|
communityRSSUrl,
|
2020-09-06 16:15:25 +00:00
|
|
|
createCommentLikeRes,
|
|
|
|
createPostLikeFindRes,
|
2021-07-17 20:42:55 +00:00
|
|
|
editCommentRes,
|
2020-09-06 16:15:25 +00:00
|
|
|
editPostFindRes,
|
2022-06-21 21:42:29 +00:00
|
|
|
enableDownvotes,
|
|
|
|
enableNsfw,
|
2021-07-17 20:42:55 +00:00
|
|
|
fetchLimit,
|
2023-04-15 14:47:10 +00:00
|
|
|
getDataTypeString,
|
|
|
|
getPageFromString,
|
|
|
|
getQueryParams,
|
|
|
|
getQueryString,
|
2022-09-22 17:13:59 +00:00
|
|
|
isPostBlocked,
|
2023-01-04 16:56:24 +00:00
|
|
|
myAuth,
|
2020-09-06 16:15:25 +00:00
|
|
|
notifyPost,
|
2022-09-22 17:13:59 +00:00
|
|
|
nsfwCheck,
|
2022-07-30 13:28:08 +00:00
|
|
|
postToCommentSortType,
|
2022-02-24 15:31:44 +00:00
|
|
|
relTags,
|
2021-07-17 20:42:55 +00:00
|
|
|
restoreScrollPosition,
|
|
|
|
saveCommentRes,
|
|
|
|
saveScrollPosition,
|
2020-09-08 15:17:49 +00:00
|
|
|
setIsoData,
|
2021-07-17 20:42:55 +00:00
|
|
|
setupTippy,
|
2022-05-23 19:19:14 +00:00
|
|
|
showLocal,
|
2021-07-17 20:42:55 +00:00
|
|
|
toast,
|
2022-07-30 03:38:37 +00:00
|
|
|
updateCommunityBlock,
|
2021-08-20 02:56:18 +00:00
|
|
|
updatePersonBlock,
|
2021-07-17 20:42:55 +00:00
|
|
|
wsClient,
|
2020-09-08 15:17:49 +00:00
|
|
|
wsSubscribe,
|
2021-07-17 20:42:55 +00:00
|
|
|
} from "../../utils";
|
|
|
|
import { CommentNodes } from "../comment/comment-nodes";
|
|
|
|
import { BannerIconHeader } from "../common/banner-icon-header";
|
|
|
|
import { DataTypeSelect } from "../common/data-type-select";
|
|
|
|
import { HtmlTags } from "../common/html-tags";
|
|
|
|
import { Icon, Spinner } from "../common/icon";
|
|
|
|
import { Paginator } from "../common/paginator";
|
|
|
|
import { SortSelect } from "../common/sort-select";
|
|
|
|
import { Sidebar } from "../community/sidebar";
|
2022-04-28 20:42:15 +00:00
|
|
|
import { SiteSidebar } from "../home/site-sidebar";
|
2021-07-17 20:42:55 +00:00
|
|
|
import { PostListings } from "../post/post-listings";
|
|
|
|
import { CommunityLink } from "./community-link";
|
2020-09-06 16:15:25 +00:00
|
|
|
|
2023-05-30 00:40:00 +00:00
|
|
|
interface CommunityData {
|
|
|
|
communityResponse: GetCommunityResponse;
|
|
|
|
postsResponse?: GetPostsResponse;
|
|
|
|
commentsResponse?: GetCommentsResponse;
|
|
|
|
}
|
|
|
|
|
2020-09-06 16:15:25 +00:00
|
|
|
interface State {
|
2023-01-04 16:56:24 +00:00
|
|
|
communityRes?: GetCommunityResponse;
|
2020-12-02 15:13:01 +00:00
|
|
|
communityLoading: boolean;
|
2023-04-15 14:47:10 +00:00
|
|
|
listingsLoading: boolean;
|
2020-12-24 01:58:27 +00:00
|
|
|
posts: PostView[];
|
|
|
|
comments: CommentView[];
|
2021-07-18 03:56:33 +00:00
|
|
|
showSidebarMobile: boolean;
|
2020-09-06 16:15:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
interface CommunityProps {
|
|
|
|
dataType: DataType;
|
|
|
|
sort: SortType;
|
2023-05-15 19:53:29 +00:00
|
|
|
page: number;
|
2020-09-06 16:15:25 +00:00
|
|
|
}
|
|
|
|
|
2023-04-15 14:47:10 +00:00
|
|
|
function getCommunityQueryParams() {
|
|
|
|
return getQueryParams<CommunityProps>({
|
|
|
|
dataType: getDataTypeFromQuery,
|
|
|
|
page: getPageFromString,
|
|
|
|
sort: getSortTypeFromQuery,
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2023-05-11 18:32:32 +00:00
|
|
|
function getDataTypeFromQuery(type?: string): DataType {
|
|
|
|
return type ? DataType[type] : DataType.Post;
|
|
|
|
}
|
2023-04-15 14:47:10 +00:00
|
|
|
|
|
|
|
function getSortTypeFromQuery(type?: string): SortType {
|
|
|
|
const mySortType =
|
|
|
|
UserService.Instance.myUserInfo?.local_user_view.local_user
|
|
|
|
.default_sort_type;
|
|
|
|
|
2023-05-11 18:32:32 +00:00
|
|
|
return type ? (type as SortType) : mySortType ?? "Active";
|
2020-09-06 16:15:25 +00:00
|
|
|
}
|
|
|
|
|
2023-04-15 14:47:10 +00:00
|
|
|
export class Community extends Component<
|
|
|
|
RouteComponentProps<{ name: string }>,
|
|
|
|
State
|
|
|
|
> {
|
2023-05-30 00:40:00 +00:00
|
|
|
private isoData = setIsoData<CommunityData>(this.context);
|
2023-01-04 16:56:24 +00:00
|
|
|
private subscription?: Subscription;
|
|
|
|
state: State = {
|
2020-12-02 15:13:01 +00:00
|
|
|
communityLoading: true,
|
2023-04-15 14:47:10 +00:00
|
|
|
listingsLoading: true,
|
2020-09-06 16:15:25 +00:00
|
|
|
posts: [],
|
|
|
|
comments: [],
|
2021-07-18 03:56:33 +00:00
|
|
|
showSidebarMobile: false,
|
2020-09-06 16:15:25 +00:00
|
|
|
};
|
|
|
|
|
2023-04-15 14:47:10 +00:00
|
|
|
constructor(props: RouteComponentProps<{ name: string }>, context: any) {
|
2020-09-06 16:15:25 +00:00
|
|
|
super(props, context);
|
|
|
|
|
|
|
|
this.handleSortChange = this.handleSortChange.bind(this);
|
|
|
|
this.handleDataTypeChange = this.handleDataTypeChange.bind(this);
|
2021-07-16 19:40:56 +00:00
|
|
|
this.handlePageChange = this.handlePageChange.bind(this);
|
2020-09-06 16:15:25 +00:00
|
|
|
|
2020-09-08 15:17:49 +00:00
|
|
|
this.parseMessage = this.parseMessage.bind(this);
|
|
|
|
this.subscription = wsSubscribe(this.parseMessage);
|
2020-09-06 16:15:25 +00:00
|
|
|
|
2020-09-08 15:17:49 +00:00
|
|
|
// Only fetch the data if coming from another route
|
|
|
|
if (this.isoData.path == this.context.router.route.match.url) {
|
2023-05-30 00:40:00 +00:00
|
|
|
const { communityResponse, commentsResponse, postsResponse } =
|
|
|
|
this.isoData.routeData;
|
|
|
|
|
2022-09-22 15:03:35 +00:00
|
|
|
this.state = {
|
|
|
|
...this.state,
|
2023-05-30 00:40:00 +00:00
|
|
|
communityRes: communityResponse,
|
2022-09-22 15:03:35 +00:00
|
|
|
};
|
2023-05-30 00:40:00 +00:00
|
|
|
|
|
|
|
if (postsResponse) {
|
|
|
|
this.state = { ...this.state, posts: postsResponse.posts };
|
2022-09-22 15:03:35 +00:00
|
|
|
}
|
2022-06-21 21:42:29 +00:00
|
|
|
|
2023-05-30 00:40:00 +00:00
|
|
|
if (commentsResponse) {
|
|
|
|
this.state = { ...this.state, comments: commentsResponse.comments };
|
2022-09-22 15:03:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
this.state = {
|
|
|
|
...this.state,
|
|
|
|
communityLoading: false,
|
2023-04-15 14:47:10 +00:00
|
|
|
listingsLoading: false,
|
2022-09-22 15:03:35 +00:00
|
|
|
};
|
2020-09-08 15:17:49 +00:00
|
|
|
} else {
|
|
|
|
this.fetchCommunity();
|
|
|
|
this.fetchData();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fetchCommunity() {
|
2023-04-15 14:47:10 +00:00
|
|
|
const form: GetCommunity = {
|
|
|
|
name: this.props.match.params.name,
|
2023-01-04 16:56:24 +00:00
|
|
|
auth: myAuth(false),
|
|
|
|
};
|
2020-12-24 22:05:57 +00:00
|
|
|
WebSocketService.Instance.send(wsClient.getCommunity(form));
|
2020-09-06 16:15:25 +00:00
|
|
|
}
|
|
|
|
|
2021-11-25 17:40:48 +00:00
|
|
|
componentDidMount() {
|
|
|
|
setupTippy();
|
|
|
|
}
|
|
|
|
|
2020-09-06 16:15:25 +00:00
|
|
|
componentWillUnmount() {
|
2021-02-02 18:36:59 +00:00
|
|
|
saveScrollPosition(this.context);
|
2023-01-04 16:56:24 +00:00
|
|
|
this.subscription?.unsubscribe();
|
2020-09-06 16:15:25 +00:00
|
|
|
}
|
|
|
|
|
2023-04-15 14:47:10 +00:00
|
|
|
static fetchInitialData({
|
|
|
|
client,
|
|
|
|
path,
|
|
|
|
query: { dataType: urlDataType, page: urlPage, sort: urlSort },
|
|
|
|
auth,
|
2023-05-30 00:40:00 +00:00
|
|
|
}: InitialFetchRequest<
|
|
|
|
QueryParams<CommunityProps>
|
|
|
|
>): WithPromiseKeys<CommunityData> {
|
2023-04-15 14:47:10 +00:00
|
|
|
const pathSplit = path.split("/");
|
|
|
|
|
|
|
|
const communityName = pathSplit[2];
|
|
|
|
const communityForm: GetCommunity = {
|
2023-01-04 16:56:24 +00:00
|
|
|
name: communityName,
|
2023-04-15 14:47:10 +00:00
|
|
|
auth,
|
2023-01-04 16:56:24 +00:00
|
|
|
};
|
2020-09-08 15:17:49 +00:00
|
|
|
|
2023-04-15 14:47:10 +00:00
|
|
|
const dataType = getDataTypeFromQuery(urlDataType);
|
2020-09-08 15:17:49 +00:00
|
|
|
|
2023-04-15 14:47:10 +00:00
|
|
|
const sort = getSortTypeFromQuery(urlSort);
|
2023-01-04 16:56:24 +00:00
|
|
|
|
2023-04-15 14:47:10 +00:00
|
|
|
const page = getPageFromString(urlPage);
|
2020-09-08 15:17:49 +00:00
|
|
|
|
2023-05-30 00:40:00 +00:00
|
|
|
let postsResponse: Promise<GetPostsResponse> | undefined = undefined;
|
|
|
|
let commentsResponse: Promise<GetCommentsResponse> | undefined = undefined;
|
|
|
|
|
2023-04-15 14:47:10 +00:00
|
|
|
if (dataType === DataType.Post) {
|
|
|
|
const getPostsForm: GetPosts = {
|
2023-01-04 16:56:24 +00:00
|
|
|
community_name: communityName,
|
2020-09-08 15:17:49 +00:00
|
|
|
page,
|
2023-01-04 16:56:24 +00:00
|
|
|
limit: fetchLimit,
|
2020-09-08 15:17:49 +00:00
|
|
|
sort,
|
2023-05-11 18:32:32 +00:00
|
|
|
type_: "All",
|
2023-01-04 16:56:24 +00:00
|
|
|
saved_only: false,
|
2023-04-15 14:47:10 +00:00
|
|
|
auth,
|
2023-01-04 16:56:24 +00:00
|
|
|
};
|
2023-05-30 00:40:00 +00:00
|
|
|
|
|
|
|
postsResponse = client.getPosts(getPostsForm);
|
2020-09-08 15:17:49 +00:00
|
|
|
} else {
|
2023-04-15 14:47:10 +00:00
|
|
|
const getCommentsForm: GetComments = {
|
2023-01-04 16:56:24 +00:00
|
|
|
community_name: communityName,
|
2020-09-08 15:17:49 +00:00
|
|
|
page,
|
2023-01-04 16:56:24 +00:00
|
|
|
limit: fetchLimit,
|
|
|
|
sort: postToCommentSortType(sort),
|
2023-05-11 18:32:32 +00:00
|
|
|
type_: "All",
|
2023-01-04 16:56:24 +00:00
|
|
|
saved_only: false,
|
2023-04-15 14:47:10 +00:00
|
|
|
auth,
|
2023-01-04 16:56:24 +00:00
|
|
|
};
|
2023-05-30 00:40:00 +00:00
|
|
|
|
|
|
|
commentsResponse = client.getComments(getCommentsForm);
|
2020-09-08 15:17:49 +00:00
|
|
|
}
|
|
|
|
|
2023-05-30 00:40:00 +00:00
|
|
|
return {
|
|
|
|
communityResponse: client.getCommunity(communityForm),
|
|
|
|
commentsResponse,
|
|
|
|
postsResponse,
|
|
|
|
};
|
2020-09-08 15:17:49 +00:00
|
|
|
}
|
|
|
|
|
2020-09-06 16:15:25 +00:00
|
|
|
get documentTitle(): string {
|
2023-04-15 14:47:10 +00:00
|
|
|
const cRes = this.state.communityRes;
|
2023-01-04 16:56:24 +00:00
|
|
|
return cRes
|
2023-04-15 14:47:10 +00:00
|
|
|
? `${cRes.community_view.community.title} - ${this.isoData.site_res.site_view.site.name}`
|
2023-01-04 16:56:24 +00:00
|
|
|
: "";
|
2020-09-06 16:15:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
render() {
|
2023-04-15 14:47:10 +00:00
|
|
|
const res = this.state.communityRes;
|
|
|
|
const { page } = getCommunityQueryParams();
|
2022-12-19 15:57:29 +00:00
|
|
|
|
2020-09-06 16:15:25 +00:00
|
|
|
return (
|
2022-10-03 18:16:36 +00:00
|
|
|
<div className="container-lg">
|
2020-12-02 15:13:01 +00:00
|
|
|
{this.state.communityLoading ? (
|
2020-09-06 16:15:25 +00:00
|
|
|
<h5>
|
2021-07-17 20:21:31 +00:00
|
|
|
<Spinner large />
|
2020-09-06 16:15:25 +00:00
|
|
|
</h5>
|
|
|
|
) : (
|
2023-01-04 16:56:24 +00:00
|
|
|
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">
|
2023-04-15 14:47:10 +00:00
|
|
|
{this.communityInfo}
|
2023-01-04 16:56:24 +00:00
|
|
|
<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>
|
2023-04-15 14:47:10 +00:00
|
|
|
{this.state.showSidebarMobile && this.sidebar(res)}
|
2022-06-21 21:42:29 +00:00
|
|
|
</div>
|
2023-04-15 14:47:10 +00:00
|
|
|
{this.selects}
|
|
|
|
{this.listings}
|
|
|
|
<Paginator page={page} onChange={this.handlePageChange} />
|
2021-07-18 03:56:33 +00:00
|
|
|
</div>
|
2023-01-04 16:56:24 +00:00
|
|
|
<div className="d-none d-md-block col-md-4">
|
2023-04-15 14:47:10 +00:00
|
|
|
{this.sidebar(res)}
|
2023-01-04 16:56:24 +00:00
|
|
|
</div>
|
|
|
|
</div>
|
|
|
|
</>
|
|
|
|
)
|
2020-09-06 16:15:25 +00:00
|
|
|
)}
|
|
|
|
</div>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2023-04-15 14:47:10 +00:00
|
|
|
sidebar({
|
|
|
|
community_view,
|
|
|
|
moderators,
|
|
|
|
online,
|
|
|
|
discussion_languages,
|
|
|
|
site,
|
|
|
|
}: GetCommunityResponse) {
|
|
|
|
const { site_res } = this.isoData;
|
|
|
|
// For some reason, this returns an empty vec if it matches the site langs
|
|
|
|
const communityLangs =
|
|
|
|
discussion_languages.length === 0
|
|
|
|
? site_res.all_languages.map(({ id }) => id)
|
|
|
|
: discussion_languages;
|
|
|
|
|
|
|
|
return (
|
|
|
|
<>
|
|
|
|
<Sidebar
|
|
|
|
community_view={community_view}
|
|
|
|
moderators={moderators}
|
|
|
|
admins={site_res.admins}
|
|
|
|
online={online}
|
|
|
|
enableNsfw={enableNsfw(site_res)}
|
|
|
|
editable
|
|
|
|
allLanguages={site_res.all_languages}
|
|
|
|
siteLanguages={site_res.discussion_languages}
|
|
|
|
communityLanguages={communityLangs}
|
|
|
|
/>
|
|
|
|
{!community_view.community.local && site && (
|
|
|
|
<SiteSidebar site={site} showLocal={showLocal(this.isoData)} />
|
|
|
|
)}
|
|
|
|
</>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
get listings() {
|
|
|
|
const { dataType } = getCommunityQueryParams();
|
|
|
|
const { site_res } = this.isoData;
|
|
|
|
const { listingsLoading, posts, comments, communityRes } = this.state;
|
|
|
|
|
|
|
|
if (listingsLoading) {
|
|
|
|
return (
|
2020-12-02 15:13:01 +00:00
|
|
|
<h5>
|
2021-07-17 20:21:31 +00:00
|
|
|
<Spinner large />
|
2020-12-02 15:13:01 +00:00
|
|
|
</h5>
|
2023-04-15 14:47:10 +00:00
|
|
|
);
|
|
|
|
} else if (dataType === DataType.Post) {
|
|
|
|
return (
|
2020-12-02 15:13:01 +00:00
|
|
|
<PostListings
|
2023-04-15 14:47:10 +00:00
|
|
|
posts={posts}
|
2020-12-02 15:13:01 +00:00
|
|
|
removeDuplicates
|
2023-04-15 14:47:10 +00:00
|
|
|
enableDownvotes={enableDownvotes(site_res)}
|
|
|
|
enableNsfw={enableNsfw(site_res)}
|
|
|
|
allLanguages={site_res.all_languages}
|
|
|
|
siteLanguages={site_res.discussion_languages}
|
2020-12-02 15:13:01 +00:00
|
|
|
/>
|
2023-04-15 14:47:10 +00:00
|
|
|
);
|
|
|
|
} else {
|
|
|
|
return (
|
|
|
|
<CommentNodes
|
|
|
|
nodes={commentsToFlatNodes(comments)}
|
|
|
|
viewType={CommentViewType.Flat}
|
|
|
|
noIndent
|
|
|
|
showContext
|
|
|
|
enableDownvotes={enableDownvotes(site_res)}
|
|
|
|
moderators={communityRes?.moderators}
|
|
|
|
admins={site_res.admins}
|
|
|
|
allLanguages={site_res.all_languages}
|
|
|
|
siteLanguages={site_res.discussion_languages}
|
|
|
|
/>
|
|
|
|
);
|
|
|
|
}
|
2020-09-06 16:15:25 +00:00
|
|
|
}
|
|
|
|
|
2023-04-15 14:47:10 +00:00
|
|
|
get communityInfo() {
|
|
|
|
const community = this.state.communityRes?.community_view.community;
|
|
|
|
|
2023-01-04 16:56:24 +00:00
|
|
|
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>
|
|
|
|
)
|
|
|
|
);
|
2020-09-06 16:15:25 +00:00
|
|
|
}
|
|
|
|
|
2023-04-15 14:47:10 +00:00
|
|
|
get selects() {
|
2023-01-04 16:56:24 +00:00
|
|
|
// let communityRss = this.state.communityRes.map(r =>
|
|
|
|
// communityRSSUrl(r.community_view.community.actor_id, this.state.sort)
|
|
|
|
// );
|
2023-04-15 14:47:10 +00:00
|
|
|
const { dataType, sort } = getCommunityQueryParams();
|
|
|
|
const res = this.state.communityRes;
|
|
|
|
const communityRss = res
|
|
|
|
? communityRSSUrl(res.community_view.community.actor_id, sort)
|
2023-01-04 16:56:24 +00:00
|
|
|
: undefined;
|
|
|
|
|
2020-09-06 16:15:25 +00:00
|
|
|
return (
|
2022-09-22 15:03:35 +00:00
|
|
|
<div className="mb-3">
|
|
|
|
<span className="mr-3">
|
2020-09-06 16:15:25 +00:00
|
|
|
<DataTypeSelect
|
2023-04-15 14:47:10 +00:00
|
|
|
type_={dataType}
|
2020-09-06 16:15:25 +00:00
|
|
|
onChange={this.handleDataTypeChange}
|
|
|
|
/>
|
|
|
|
</span>
|
2022-09-22 15:03:35 +00:00
|
|
|
<span className="mr-2">
|
2023-04-15 14:47:10 +00:00
|
|
|
<SortSelect sort={sort} onChange={this.handleSortChange} />
|
2020-09-06 16:15:25 +00:00
|
|
|
</span>
|
2023-01-04 16:56:24 +00:00
|
|
|
{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}
|
|
|
|
/>
|
|
|
|
</>
|
|
|
|
)}
|
2020-09-06 16:15:25 +00:00
|
|
|
</div>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2023-05-15 19:53:29 +00:00
|
|
|
handlePageChange(page: number) {
|
2021-07-16 19:40:56 +00:00
|
|
|
this.updateUrl({ page });
|
2020-09-06 16:15:25 +00:00
|
|
|
window.scrollTo(0, 0);
|
|
|
|
}
|
|
|
|
|
2023-04-15 14:47:10 +00:00
|
|
|
handleSortChange(sort: SortType) {
|
2023-05-15 19:53:29 +00:00
|
|
|
this.updateUrl({ sort, page: 1 });
|
2020-09-06 16:15:25 +00:00
|
|
|
window.scrollTo(0, 0);
|
|
|
|
}
|
|
|
|
|
2023-04-15 14:47:10 +00:00
|
|
|
handleDataTypeChange(dataType: DataType) {
|
2023-05-15 19:53:29 +00:00
|
|
|
this.updateUrl({ dataType, page: 1 });
|
2020-09-06 16:15:25 +00:00
|
|
|
window.scrollTo(0, 0);
|
|
|
|
}
|
|
|
|
|
2021-07-18 03:56:33 +00:00
|
|
|
handleShowSidebarMobile(i: Community) {
|
2023-04-15 14:47:10 +00:00
|
|
|
i.setState(({ showSidebarMobile }) => ({
|
|
|
|
showSidebarMobile: !showSidebarMobile,
|
|
|
|
}));
|
2021-07-18 03:56:33 +00:00
|
|
|
}
|
|
|
|
|
2023-04-15 14:47:10 +00:00
|
|
|
updateUrl({ dataType, page, sort }: Partial<CommunityProps>) {
|
|
|
|
const {
|
|
|
|
dataType: urlDataType,
|
|
|
|
page: urlPage,
|
|
|
|
sort: urlSort,
|
|
|
|
} = getCommunityQueryParams();
|
|
|
|
|
|
|
|
const queryParams: QueryParams<CommunityProps> = {
|
|
|
|
dataType: getDataTypeString(dataType ?? urlDataType),
|
|
|
|
page: (page ?? urlPage).toString(),
|
|
|
|
sort: sort ?? urlSort,
|
|
|
|
};
|
2021-02-08 20:14:34 +00:00
|
|
|
|
2020-09-06 16:15:25 +00:00
|
|
|
this.props.history.push(
|
2023-04-15 14:47:10 +00:00
|
|
|
`/c/${this.props.match.params.name}${getQueryString(queryParams)}`
|
2020-09-06 16:15:25 +00:00
|
|
|
);
|
2023-04-15 14:47:10 +00:00
|
|
|
|
|
|
|
this.setState({
|
|
|
|
comments: [],
|
|
|
|
posts: [],
|
|
|
|
listingsLoading: true,
|
|
|
|
});
|
|
|
|
|
|
|
|
this.fetchData();
|
2020-09-06 16:15:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
fetchData() {
|
2023-04-15 14:47:10 +00:00
|
|
|
const { dataType, page, sort } = getCommunityQueryParams();
|
|
|
|
const { name } = this.props.match.params;
|
|
|
|
|
|
|
|
let req: string;
|
|
|
|
if (dataType === DataType.Post) {
|
|
|
|
const form: GetPosts = {
|
|
|
|
page,
|
2023-01-04 16:56:24 +00:00
|
|
|
limit: fetchLimit,
|
2023-04-15 14:47:10 +00:00
|
|
|
sort,
|
2023-05-11 18:32:32 +00:00
|
|
|
type_: "All",
|
2023-04-15 14:47:10 +00:00
|
|
|
community_name: name,
|
2023-01-04 16:56:24 +00:00
|
|
|
saved_only: false,
|
|
|
|
auth: myAuth(false),
|
|
|
|
};
|
2023-04-15 14:47:10 +00:00
|
|
|
req = wsClient.getPosts(form);
|
2020-09-06 16:15:25 +00:00
|
|
|
} else {
|
2023-04-15 14:47:10 +00:00
|
|
|
const form: GetComments = {
|
|
|
|
page,
|
2023-01-04 16:56:24 +00:00
|
|
|
limit: fetchLimit,
|
2023-04-15 14:47:10 +00:00
|
|
|
sort: postToCommentSortType(sort),
|
2023-05-11 18:32:32 +00:00
|
|
|
type_: "All",
|
2023-04-15 14:47:10 +00:00
|
|
|
community_name: name,
|
2023-01-04 16:56:24 +00:00
|
|
|
saved_only: false,
|
|
|
|
auth: myAuth(false),
|
|
|
|
};
|
2023-04-15 14:47:10 +00:00
|
|
|
|
|
|
|
req = wsClient.getComments(form);
|
2020-09-06 16:15:25 +00:00
|
|
|
}
|
2023-04-15 14:47:10 +00:00
|
|
|
|
|
|
|
WebSocketService.Instance.send(req);
|
2020-09-06 16:15:25 +00:00
|
|
|
}
|
|
|
|
|
2020-12-24 01:58:27 +00:00
|
|
|
parseMessage(msg: any) {
|
2023-04-15 14:47:10 +00:00
|
|
|
const { page } = getCommunityQueryParams();
|
|
|
|
const op = wsUserOp(msg);
|
2021-04-07 15:54:38 +00:00
|
|
|
console.log(msg);
|
2023-04-15 14:47:10 +00:00
|
|
|
const res = this.state.communityRes;
|
|
|
|
|
2020-09-06 16:15:25 +00:00
|
|
|
if (msg.error) {
|
2021-02-22 02:39:04 +00:00
|
|
|
toast(i18n.t(msg.error), "danger");
|
|
|
|
this.context.router.history.push("/");
|
2020-09-06 16:15:25 +00:00
|
|
|
} else if (msg.reconnect) {
|
2023-01-04 16:56:24 +00:00
|
|
|
if (res) {
|
|
|
|
WebSocketService.Instance.send(
|
|
|
|
wsClient.communityJoin({
|
|
|
|
community_id: res.community_view.community.id,
|
|
|
|
})
|
|
|
|
);
|
|
|
|
}
|
2023-04-15 14:47:10 +00:00
|
|
|
|
2020-09-06 16:15:25 +00:00
|
|
|
this.fetchData();
|
2023-04-15 14:47:10 +00:00
|
|
|
} else {
|
|
|
|
switch (op) {
|
|
|
|
case UserOperation.GetCommunity: {
|
|
|
|
const data = wsJsonToRes<GetCommunityResponse>(msg);
|
|
|
|
|
|
|
|
this.setState({ communityRes: data, communityLoading: false });
|
|
|
|
// TODO why is there no auth in this form?
|
|
|
|
WebSocketService.Instance.send(
|
|
|
|
wsClient.communityJoin({
|
|
|
|
community_id: data.community_view.community.id,
|
|
|
|
})
|
|
|
|
);
|
|
|
|
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
case UserOperation.EditCommunity:
|
|
|
|
case UserOperation.DeleteCommunity:
|
|
|
|
case UserOperation.RemoveCommunity: {
|
|
|
|
const { community_view, discussion_languages } =
|
|
|
|
wsJsonToRes<CommunityResponse>(msg);
|
|
|
|
|
|
|
|
if (res) {
|
|
|
|
res.community_view = community_view;
|
|
|
|
res.discussion_languages = discussion_languages;
|
|
|
|
this.setState(this.state);
|
|
|
|
}
|
|
|
|
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
case UserOperation.FollowCommunity: {
|
|
|
|
const {
|
|
|
|
community_view: {
|
|
|
|
subscribed,
|
|
|
|
counts: { subscribers },
|
|
|
|
},
|
|
|
|
} = wsJsonToRes<CommunityResponse>(msg);
|
|
|
|
|
|
|
|
if (res) {
|
|
|
|
res.community_view.subscribed = subscribed;
|
|
|
|
res.community_view.counts.subscribers = subscribers;
|
|
|
|
this.setState(this.state);
|
|
|
|
}
|
|
|
|
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
case UserOperation.GetPosts: {
|
|
|
|
const { posts } = wsJsonToRes<GetPostsResponse>(msg);
|
|
|
|
|
|
|
|
this.setState({ posts, listingsLoading: false });
|
|
|
|
restoreScrollPosition(this.context);
|
|
|
|
setupTippy();
|
|
|
|
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
case UserOperation.EditPost:
|
|
|
|
case UserOperation.DeletePost:
|
|
|
|
case UserOperation.RemovePost:
|
|
|
|
case UserOperation.LockPost:
|
|
|
|
case UserOperation.FeaturePost:
|
|
|
|
case UserOperation.SavePost: {
|
|
|
|
const { post_view } = wsJsonToRes<PostResponse>(msg);
|
|
|
|
|
|
|
|
editPostFindRes(post_view, this.state.posts);
|
|
|
|
this.setState(this.state);
|
|
|
|
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
case UserOperation.CreatePost: {
|
|
|
|
const { post_view } = wsJsonToRes<PostResponse>(msg);
|
|
|
|
|
|
|
|
const 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
|
2023-05-15 19:53:29 +00:00
|
|
|
if (page === 1 && nsfwCheck(post_view) && !isPostBlocked(post_view)) {
|
2023-04-15 14:47:10 +00:00
|
|
|
this.state.posts.unshift(post_view);
|
|
|
|
if (showPostNotifs) {
|
|
|
|
notifyPost(post_view, this.context.router);
|
|
|
|
}
|
|
|
|
this.setState(this.state);
|
|
|
|
}
|
|
|
|
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
case UserOperation.CreatePostLike: {
|
|
|
|
const { post_view } = wsJsonToRes<PostResponse>(msg);
|
|
|
|
|
|
|
|
createPostLikeFindRes(post_view, this.state.posts);
|
|
|
|
this.setState(this.state);
|
|
|
|
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
case UserOperation.AddModToCommunity: {
|
|
|
|
const { moderators } = wsJsonToRes<AddModToCommunityResponse>(msg);
|
|
|
|
|
|
|
|
if (res) {
|
|
|
|
res.moderators = moderators;
|
|
|
|
this.setState(this.state);
|
|
|
|
}
|
|
|
|
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
case UserOperation.BanFromCommunity: {
|
|
|
|
const {
|
|
|
|
person_view: {
|
|
|
|
person: { id: personId },
|
|
|
|
},
|
|
|
|
banned,
|
|
|
|
} = wsJsonToRes<BanFromCommunityResponse>(msg);
|
|
|
|
|
|
|
|
// TODO this might be incorrect
|
|
|
|
this.state.posts
|
|
|
|
.filter(p => p.creator.id === personId)
|
|
|
|
.forEach(p => (p.creator_banned_from_community = banned));
|
|
|
|
|
|
|
|
this.setState(this.state);
|
|
|
|
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
case UserOperation.GetComments: {
|
|
|
|
const { comments } = wsJsonToRes<GetCommentsResponse>(msg);
|
|
|
|
this.setState({ comments, listingsLoading: false });
|
|
|
|
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
case UserOperation.EditComment:
|
|
|
|
case UserOperation.DeleteComment:
|
|
|
|
case UserOperation.RemoveComment: {
|
|
|
|
const { comment_view } = wsJsonToRes<CommentResponse>(msg);
|
|
|
|
editCommentRes(comment_view, this.state.comments);
|
|
|
|
this.setState(this.state);
|
|
|
|
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
case UserOperation.CreateComment: {
|
|
|
|
const { form_id, comment_view } = wsJsonToRes<CommentResponse>(msg);
|
|
|
|
|
|
|
|
// Necessary since it might be a user reply
|
|
|
|
if (form_id) {
|
|
|
|
this.setState(({ comments }) => ({
|
|
|
|
comments: [comment_view].concat(comments),
|
|
|
|
}));
|
|
|
|
}
|
|
|
|
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
case UserOperation.SaveComment: {
|
|
|
|
const { comment_view } = wsJsonToRes<CommentResponse>(msg);
|
|
|
|
|
|
|
|
saveCommentRes(comment_view, this.state.comments);
|
|
|
|
this.setState(this.state);
|
|
|
|
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
case UserOperation.CreateCommentLike: {
|
|
|
|
const { comment_view } = wsJsonToRes<CommentResponse>(msg);
|
|
|
|
|
|
|
|
createCommentLikeRes(comment_view, this.state.comments);
|
|
|
|
this.setState(this.state);
|
|
|
|
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
case UserOperation.BlockPerson: {
|
|
|
|
const data = wsJsonToRes<BlockPersonResponse>(msg);
|
|
|
|
updatePersonBlock(data);
|
|
|
|
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
case UserOperation.CreatePostReport:
|
|
|
|
case UserOperation.CreateCommentReport: {
|
|
|
|
const data = wsJsonToRes<PostReportResponse>(msg);
|
|
|
|
|
|
|
|
if (data) {
|
|
|
|
toast(i18n.t("report_created"));
|
|
|
|
}
|
|
|
|
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
case UserOperation.PurgeCommunity: {
|
|
|
|
const { success } = wsJsonToRes<PurgeItemResponse>(msg);
|
|
|
|
|
|
|
|
if (success) {
|
|
|
|
toast(i18n.t("purge_success"));
|
|
|
|
this.context.router.history.push(`/`);
|
|
|
|
}
|
|
|
|
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
case UserOperation.BlockCommunity: {
|
|
|
|
const data = wsJsonToRes<BlockCommunityResponse>(msg);
|
|
|
|
if (res) {
|
|
|
|
res.community_view.blocked = data.blocked;
|
|
|
|
this.setState(this.state);
|
|
|
|
}
|
|
|
|
updateCommunityBlock(data);
|
|
|
|
|
|
|
|
break;
|
2022-09-22 17:13:59 +00:00
|
|
|
}
|
2023-01-04 16:56:24 +00:00
|
|
|
}
|
2020-09-06 16:15:25 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|