import { Component, linkEvent } from "inferno"; import { RouteComponentProps } from "inferno-router/dist/Route"; import { AddModToCommunityResponse, BanFromCommunityResponse, BlockCommunityResponse, BlockPersonResponse, CommentResponse, CommentView, CommunityResponse, GetComments, GetCommentsResponse, GetCommunity, GetCommunityResponse, GetPosts, GetPostsResponse, ListingType, PostReportResponse, PostResponse, PostView, PurgeItemResponse, SortType, UserOperation, wsJsonToRes, wsUserOp, } from "lemmy-js-client"; import { Subscription } from "rxjs"; import { i18n } from "../../i18next"; import { CommentViewType, DataType, InitialFetchRequest, } from "../../interfaces"; import { UserService, WebSocketService } from "../../services"; import { QueryParams, commentsToFlatNodes, communityRSSUrl, createCommentLikeRes, createPostLikeFindRes, editCommentRes, editPostFindRes, enableDownvotes, enableNsfw, fetchLimit, getDataTypeString, getPageFromString, getQueryParams, getQueryString, isPostBlocked, myAuth, notifyPost, nsfwCheck, postToCommentSortType, relTags, restoreScrollPosition, routeDataTypeToEnum, routeSortTypeToEnum, saveCommentRes, saveScrollPosition, setIsoData, setupTippy, showLocal, toast, updateCommunityBlock, updatePersonBlock, wsClient, wsSubscribe, } 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"; import { SiteSidebar } from "../home/site-sidebar"; import { PostListings } from "../post/post-listings"; import { CommunityLink } from "./community-link"; interface State { communityRes?: GetCommunityResponse; communityLoading: boolean; listingsLoading: boolean; posts: PostView[]; comments: CommentView[]; showSidebarMobile: boolean; } interface CommunityProps { dataType: DataType; sort: SortType; page: number; } function getCommunityQueryParams() { return getQueryParams({ dataType: getDataTypeFromQuery, page: getPageFromString, sort: getSortTypeFromQuery, }); } const getDataTypeFromQuery = (type?: string): DataType => routeDataTypeToEnum(type ?? "", DataType.Post); function getSortTypeFromQuery(type?: string): SortType { const mySortType = UserService.Instance.myUserInfo?.local_user_view.local_user .default_sort_type; return routeSortTypeToEnum( type ?? "", mySortType ? Object.values(SortType)[mySortType] : SortType.Active ); } export class Community extends Component< RouteComponentProps<{ name: string }>, State > { private isoData = setIsoData(this.context); private subscription?: Subscription; state: State = { communityLoading: true, listingsLoading: true, posts: [], comments: [], showSidebarMobile: false, }; constructor(props: RouteComponentProps<{ name: string }>, context: any) { super(props, context); this.handleSortChange = this.handleSortChange.bind(this); this.handleDataTypeChange = this.handleDataTypeChange.bind(this); this.handlePageChange = this.handlePageChange.bind(this); this.parseMessage = this.parseMessage.bind(this); this.subscription = wsSubscribe(this.parseMessage); // Only fetch the data if coming from another route if (this.isoData.path == this.context.router.route.match.url) { this.state = { ...this.state, communityRes: this.isoData.routeData[0] as GetCommunityResponse, }; const postsRes = this.isoData.routeData[1] as | GetPostsResponse | undefined; const commentsRes = this.isoData.routeData[2] as | GetCommentsResponse | undefined; if (postsRes) { this.state = { ...this.state, posts: postsRes.posts }; } if (commentsRes) { this.state = { ...this.state, comments: commentsRes.comments }; } this.state = { ...this.state, communityLoading: false, listingsLoading: false, }; } else { this.fetchCommunity(); this.fetchData(); } } fetchCommunity() { const form: GetCommunity = { name: this.props.match.params.name, auth: myAuth(false), }; WebSocketService.Instance.send(wsClient.getCommunity(form)); } componentDidMount() { setupTippy(); } componentWillUnmount() { saveScrollPosition(this.context); this.subscription?.unsubscribe(); } static fetchInitialData({ client, path, query: { dataType: urlDataType, page: urlPage, sort: urlSort }, auth, }: InitialFetchRequest>): Promise[] { const pathSplit = path.split("/"); const promises: Promise[] = []; const communityName = pathSplit[2]; const communityForm: GetCommunity = { name: communityName, auth, }; promises.push(client.getCommunity(communityForm)); const dataType = getDataTypeFromQuery(urlDataType); const sort = getSortTypeFromQuery(urlSort); const page = getPageFromString(urlPage); if (dataType === DataType.Post) { const getPostsForm: GetPosts = { community_name: communityName, page, limit: fetchLimit, sort, type_: ListingType.All, saved_only: false, auth, }; promises.push(client.getPosts(getPostsForm)); promises.push(Promise.resolve()); } else { const getCommentsForm: GetComments = { community_name: communityName, page, limit: fetchLimit, sort: postToCommentSortType(sort), type_: ListingType.All, saved_only: false, auth, }; promises.push(Promise.resolve()); promises.push(client.getComments(getCommentsForm)); } return promises; } get documentTitle(): string { const cRes = this.state.communityRes; return cRes ? `${cRes.community_view.community.title} - ${this.isoData.site_res.site_view.site.name}` : ""; } render() { const res = this.state.communityRes; const { page } = getCommunityQueryParams(); return (
{this.state.communityLoading ? (
) : ( res && ( <>
{this.communityInfo}
{this.state.showSidebarMobile && this.sidebar(res)}
{this.selects} {this.listings}
{this.sidebar(res)}
) )}
); } 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 ( <> {!community_view.community.local && site && ( )} ); } get listings() { const { dataType } = getCommunityQueryParams(); const { site_res } = this.isoData; const { listingsLoading, posts, comments, communityRes } = this.state; if (listingsLoading) { return (
); } else if (dataType === DataType.Post) { return ( ); } else { return ( ); } } get communityInfo() { const community = this.state.communityRes?.community_view.community; return ( community && (
{community.title}
) ); } get selects() { // let communityRss = this.state.communityRes.map(r => // communityRSSUrl(r.community_view.community.actor_id, this.state.sort) // ); const { dataType, sort } = getCommunityQueryParams(); const res = this.state.communityRes; const communityRss = res ? communityRSSUrl(res.community_view.community.actor_id, sort) : undefined; return (
{communityRss && ( <> )}
); } handlePageChange(page: number) { this.updateUrl({ page }); window.scrollTo(0, 0); } handleSortChange(sort: SortType) { this.updateUrl({ sort, page: 1 }); window.scrollTo(0, 0); } handleDataTypeChange(dataType: DataType) { this.updateUrl({ dataType, page: 1 }); window.scrollTo(0, 0); } handleShowSidebarMobile(i: Community) { i.setState(({ showSidebarMobile }) => ({ showSidebarMobile: !showSidebarMobile, })); } updateUrl({ dataType, page, sort }: Partial) { const { dataType: urlDataType, page: urlPage, sort: urlSort, } = getCommunityQueryParams(); const queryParams: QueryParams = { dataType: getDataTypeString(dataType ?? urlDataType), page: (page ?? urlPage).toString(), sort: sort ?? urlSort, }; this.props.history.push( `/c/${this.props.match.params.name}${getQueryString(queryParams)}` ); this.setState({ comments: [], posts: [], listingsLoading: true, }); this.fetchData(); } fetchData() { const { dataType, page, sort } = getCommunityQueryParams(); const { name } = this.props.match.params; let req: string; if (dataType === DataType.Post) { const form: GetPosts = { page, limit: fetchLimit, sort, type_: ListingType.All, community_name: name, saved_only: false, auth: myAuth(false), }; req = wsClient.getPosts(form); } else { const form: GetComments = { page, limit: fetchLimit, sort: postToCommentSortType(sort), type_: ListingType.All, community_name: name, saved_only: false, auth: myAuth(false), }; req = wsClient.getComments(form); } WebSocketService.Instance.send(req); } parseMessage(msg: any) { const { page } = getCommunityQueryParams(); const op = wsUserOp(msg); console.log(msg); const res = this.state.communityRes; if (msg.error) { toast(i18n.t(msg.error), "danger"); this.context.router.history.push("/"); } else if (msg.reconnect) { if (res) { WebSocketService.Instance.send( wsClient.communityJoin({ community_id: res.community_view.community.id, }) ); } this.fetchData(); } else { switch (op) { case UserOperation.GetCommunity: { const data = wsJsonToRes(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(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(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(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(msg); editPostFindRes(post_view, this.state.posts); this.setState(this.state); break; } case UserOperation.CreatePost: { const { post_view } = wsJsonToRes(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 if (page === 1 && nsfwCheck(post_view) && !isPostBlocked(post_view)) { 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(msg); createPostLikeFindRes(post_view, this.state.posts); this.setState(this.state); break; } case UserOperation.AddModToCommunity: { const { moderators } = wsJsonToRes(msg); if (res) { res.moderators = moderators; this.setState(this.state); } break; } case UserOperation.BanFromCommunity: { const { person_view: { person: { id: personId }, }, banned, } = wsJsonToRes(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(msg); this.setState({ comments, listingsLoading: false }); break; } case UserOperation.EditComment: case UserOperation.DeleteComment: case UserOperation.RemoveComment: { const { comment_view } = wsJsonToRes(msg); editCommentRes(comment_view, this.state.comments); this.setState(this.state); break; } case UserOperation.CreateComment: { const { form_id, comment_view } = wsJsonToRes(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(msg); saveCommentRes(comment_view, this.state.comments); this.setState(this.state); break; } case UserOperation.CreateCommentLike: { const { comment_view } = wsJsonToRes(msg); createCommentLikeRes(comment_view, this.state.comments); this.setState(this.state); break; } case UserOperation.BlockPerson: { const data = wsJsonToRes(msg); updatePersonBlock(data); break; } case UserOperation.CreatePostReport: case UserOperation.CreateCommentReport: { const data = wsJsonToRes(msg); if (data) { toast(i18n.t("report_created")); } break; } case UserOperation.PurgeCommunity: { const { success } = wsJsonToRes(msg); if (success) { toast(i18n.t("purge_success")); this.context.router.history.push(`/`); } break; } case UserOperation.BlockCommunity: { const data = wsJsonToRes(msg); if (res) { res.community_view.blocked = data.blocked; this.setState(this.state); } updateCommunityBlock(data); break; } } } } }