Adding creator_is_admin and creator_is_mod, removing pointless functions (#2228)

This commit is contained in:
Dessalines 2023-11-22 11:13:00 -05:00 committed by GitHub
parent dd3842655a
commit cc2ad2d113
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
13 changed files with 64 additions and 101 deletions

View file

@ -70,7 +70,7 @@
"inferno-router": "^8.2.2", "inferno-router": "^8.2.2",
"inferno-server": "^8.2.2", "inferno-server": "^8.2.2",
"jwt-decode": "^3.1.2", "jwt-decode": "^3.1.2",
"lemmy-js-client": "0.19.0-alpha.16", "lemmy-js-client": "0.19.0-rc.15",
"lodash.isequal": "^4.5.0", "lodash.isequal": "^4.5.0",
"markdown-it": "^13.0.1", "markdown-it": "^13.0.1",
"markdown-it-bidi": "^0.1.0", "markdown-it-bidi": "^0.1.0",

View file

@ -1,13 +1,6 @@
import { colorList, getCommentParentId, showScores } from "@utils/app"; import { colorList, getCommentParentId, showScores } from "@utils/app";
import { futureDaysToUnixTime, numToSI } from "@utils/helpers"; import { futureDaysToUnixTime, numToSI } from "@utils/helpers";
import { import { amCommunityCreator, canAdmin, canMod, isBanned } from "@utils/roles";
amCommunityCreator,
canAdmin,
canMod,
isAdmin,
isBanned,
isMod,
} from "@utils/roles";
import classNames from "classnames"; import classNames from "classnames";
import isBefore from "date-fns/isBefore"; import isBefore from "date-fns/isBefore";
import parseISO from "date-fns/parseISO"; import parseISO from "date-fns/parseISO";
@ -257,8 +250,8 @@ export class CommentNode extends Component<CommentNodeProps, CommentNodeState> {
UserService.Instance.myUserInfo, UserService.Instance.myUserInfo,
true, true,
); );
const isMod_ = isMod(cv.creator.id, this.props.moderators); const isMod_ = cv.creator_is_moderator;
const isAdmin_ = isAdmin(cv.creator.id, this.props.admins); const isAdmin_ = cv.creator_is_admin;
const amCommunityCreator_ = amCommunityCreator( const amCommunityCreator_ = amCommunityCreator(
cv.creator.id, cv.creator.id,
this.props.moderators, this.props.moderators,
@ -1461,7 +1454,7 @@ export class CommentNode extends Component<CommentNodeProps, CommentNodeState> {
handleAddModToCommunity(i: CommentNode) { handleAddModToCommunity(i: CommentNode) {
i.setState({ addModLoading: true }); i.setState({ addModLoading: true });
const added = !isMod(i.commentView.comment.creator_id, i.props.moderators); const added = !i.commentView.creator_is_moderator;
i.props.onAddModToCommunity({ i.props.onAddModToCommunity({
community_id: i.commentView.community.id, community_id: i.commentView.community.id,
person_id: i.commentView.creator.id, person_id: i.commentView.creator.id,
@ -1472,7 +1465,7 @@ export class CommentNode extends Component<CommentNodeProps, CommentNodeState> {
handleAddAdmin(i: CommentNode) { handleAddAdmin(i: CommentNode) {
i.setState({ addAdminLoading: true }); i.setState({ addAdminLoading: true });
const added = !isAdmin(i.commentView.comment.creator_id, i.props.admins); const added = !i.commentView.creator_is_admin;
i.props.onAddAdmin({ i.props.onAddAdmin({
person_id: i.commentView.creator.id, person_id: i.commentView.creator.id,
added, added,

View file

@ -57,6 +57,7 @@ export class CommentReport extends Component<
community: r.community, community: r.community,
creator_banned_from_community: r.creator_banned_from_community, creator_banned_from_community: r.creator_banned_from_community,
creator_is_moderator: false, creator_is_moderator: false,
creator_is_admin: false,
counts: r.counts, counts: r.counts,
subscribed: "NotSubscribed", subscribed: "NotSubscribed",
saved: false, saved: false,

View file

@ -268,7 +268,7 @@ export class Sidebar extends Component<SidebarProps, SidebarState> {
return ( return (
<> <>
<ul className="list-inline mb-1 text-muted fw-bold"> <ul className="list-inline mb-1 text-muted fw-bold">
{amMod(this.props.moderators) && ( {amMod(this.props.community_view.community.id) && (
<> <>
<li className="list-inline-item-action"> <li className="list-inline-item-action">
<button <button
@ -468,7 +468,7 @@ export class Sidebar extends Component<SidebarProps, SidebarState> {
get canPost(): boolean { get canPost(): boolean {
return ( return (
!this.props.community_view.community.posting_restricted_to_mods || !this.props.community_view.community.posting_restricted_to_mods ||
amMod(this.props.moderators) || amMod(this.props.community_view.community.id) ||
amAdmin() amAdmin()
); );
} }

View file

@ -747,7 +747,7 @@ export class Modlog extends Component<
get amAdminOrMod(): boolean { get amAdminOrMod(): boolean {
const amMod_ = const amMod_ =
this.state.communityRes.state === "success" && this.state.communityRes.state === "success" &&
amMod(this.state.communityRes.data.moderators); amMod(this.state.communityRes.data.community_view.community.id);
return amAdmin() || amMod_; return amAdmin() || amMod_;
} }

View file

@ -18,7 +18,7 @@ import {
numToSI, numToSI,
randomStr, randomStr,
} from "@utils/helpers"; } from "@utils/helpers";
import { canMod, isAdmin, isBanned } from "@utils/roles"; import { canMod, isBanned } from "@utils/roles";
import type { QueryParams } from "@utils/types"; import type { QueryParams } from "@utils/types";
import { RouteDataResponse } from "@utils/types"; import { RouteDataResponse } from "@utils/types";
import classNames from "classnames"; import classNames from "classnames";
@ -497,7 +497,7 @@ export class Profile extends Component<
classNames="ms-1" classNames="ms-1"
isBanned={isBanned(pv.person)} isBanned={isBanned(pv.person)}
isDeleted={pv.person.deleted} isDeleted={pv.person.deleted}
isAdmin={isAdmin(pv.person.id, admins)} isAdmin={pv.is_admin}
isBot={pv.person.bot_account} isBot={pv.person.bot_account}
/> />
</li> </li>
@ -553,7 +553,7 @@ export class Profile extends Component<
)} )}
{canMod(pv.person.id, undefined, admins) && {canMod(pv.person.id, undefined, admins) &&
!isAdmin(pv.person.id, admins) && !pv.is_admin &&
!showBanDialog && !showBanDialog &&
(!isBanned(pv.person) ? ( (!isBanned(pv.person) ? (
<button <button

View file

@ -13,9 +13,7 @@ import {
amMod, amMod,
canAdmin, canAdmin,
canMod, canMod,
isAdmin,
isBanned, isBanned,
isMod,
} from "@utils/roles"; } from "@utils/roles";
import classNames from "classnames"; import classNames from "classnames";
import { Component, linkEvent } from "inferno"; import { Component, linkEvent } from "inferno";
@ -299,14 +297,13 @@ export class PostListing extends Component<PostListingProps, PostListingState> {
} }
imgThumb(src: string) { imgThumb(src: string) {
const post_view = this.postView; const pv = this.postView;
return ( return (
<PictrsImage <PictrsImage
src={src} src={src}
thumbnail thumbnail
alt="" alt=""
nsfw={post_view.post.nsfw || post_view.community.nsfw} nsfw={pv.post.nsfw || pv.community.nsfw}
/> />
); );
} }
@ -412,38 +409,34 @@ export class PostListing extends Component<PostListingProps, PostListingState> {
} }
createdLine() { createdLine() {
const post_view = this.postView; const pv = this.postView;
return ( return (
<div className="small mb-1 mb-md-0"> <div className="small mb-1 mb-md-0">
<PersonListing person={post_view.creator} /> <PersonListing person={pv.creator} />
<UserBadges <UserBadges
classNames="ms-1" classNames="ms-1"
isMod={this.creatorIsMod_} isMod={pv.creator_is_moderator}
isAdmin={this.creatorIsAdmin_} isAdmin={pv.creator_is_admin}
isBot={post_view.creator.bot_account} isBot={pv.creator.bot_account}
/> />
{this.props.showCommunity && ( {this.props.showCommunity && (
<> <>
{" "} {" "}
{I18NextService.i18n.t("to")}{" "} {I18NextService.i18n.t("to")}{" "}
<CommunityLink community={post_view.community} /> <CommunityLink community={pv.community} />
</> </>
)} )}
{post_view.post.language_id !== 0 && ( {pv.post.language_id !== 0 && (
<span className="mx-1 badge text-bg-light"> <span className="mx-1 badge text-bg-light">
{ {
this.props.allLanguages.find( this.props.allLanguages.find(
lang => lang.id === post_view.post.language_id, lang => lang.id === pv.post.language_id,
)?.name )?.name
} }
</span> </span>
)}{" "} )}{" "}
{" "} <MomentTime published={pv.post.published} updated={pv.post.updated} />
<MomentTime
published={post_view.post.published}
updated={post_view.post.updated}
/>
</div> </div>
); );
} }
@ -604,8 +597,8 @@ export class PostListing extends Component<PostListingProps, PostListingState> {
} }
commentsLine(mobile = false) { commentsLine(mobile = false) {
const post_view = this.postView; const pv = this.postView;
const post = post_view.post; const post = pv.post;
return ( return (
<div className="d-flex align-items-center justify-content-start flex-wrap text-muted"> <div className="d-flex align-items-center justify-content-start flex-wrap text-muted">
@ -639,7 +632,7 @@ export class PostListing extends Component<PostListingProps, PostListingState> {
/> />
)} )}
{this.props.showBody && post_view.post.body && this.viewSourceButton} {this.props.showBody && pv.post.body && this.viewSourceButton}
{UserService.Instance.myUserInfo && {UserService.Instance.myUserInfo &&
!this.props.viewOnly && !this.props.viewOnly &&
@ -651,8 +644,8 @@ export class PostListing extends Component<PostListingProps, PostListingState> {
postActions() { postActions() {
// Possible enhancement: Priority+ pattern instead of just hard coding which get hidden behind the show more button. // Possible enhancement: Priority+ pattern instead of just hard coding which get hidden behind the show more button.
// Possible enhancement: Make each button a component. // Possible enhancement: Make each button a component.
const post_view = this.postView; const pv = this.postView;
const post = post_view.post; const post = pv.post;
return ( return (
<> <>
@ -689,7 +682,7 @@ export class PostListing extends Component<PostListingProps, PostListingState> {
)} )}
{/* Any mod can do these, not limited to hierarchy*/} {/* Any mod can do these, not limited to hierarchy*/}
{(amMod(this.props.moderators) || amAdmin()) && ( {(amMod(this.postView.community.id) || amAdmin()) && (
<> <>
<li> <li>
<hr className="dropdown-divider" /> <hr className="dropdown-divider" />
@ -708,21 +701,23 @@ export class PostListing extends Component<PostListingProps, PostListingState> {
<li> <li>
<hr className="dropdown-divider" /> <hr className="dropdown-divider" />
</li> </li>
{!this.creatorIsMod_ && {!pv.creator_is_moderator &&
(!post_view.creator_banned_from_community ? ( (!pv.creator_banned_from_community ? (
<li>{this.modBanFromCommunityButton}</li> <li>{this.modBanFromCommunityButton}</li>
) : ( ) : (
<li>{this.modUnbanFromCommunityButton}</li> <li>{this.modUnbanFromCommunityButton}</li>
))} ))}
{!post_view.creator_banned_from_community && ( {!pv.creator_banned_from_community && (
<li>{this.addModToCommunityButton}</li> <li>{this.addModToCommunityButton}</li>
)} )}
</> </>
)} )}
{(amCommunityCreator(post_view.creator.id, this.props.moderators) || {(amCommunityCreator(pv.creator.id, this.props.moderators) ||
this.canAdmin_) && this.canAdmin_) &&
this.creatorIsMod_ && <li>{this.transferCommunityButton}</li>} pv.creator_is_moderator && (
<li>{this.transferCommunityButton}</li>
)}
{/* Admins can ban from all, and appoint other admins */} {/* Admins can ban from all, and appoint other admins */}
{this.canAdmin_ && ( {this.canAdmin_ && (
@ -730,9 +725,9 @@ export class PostListing extends Component<PostListingProps, PostListingState> {
<li> <li>
<hr className="dropdown-divider" /> <hr className="dropdown-divider" />
</li> </li>
{!this.creatorIsAdmin_ && ( {!pv.creator_is_admin && (
<> <>
{!isBanned(post_view.creator) ? ( {!isBanned(pv.creator) ? (
<li>{this.modBanButton}</li> <li>{this.modBanButton}</li>
) : ( ) : (
<li>{this.modUnbanButton}</li> <li>{this.modUnbanButton}</li>
@ -741,7 +736,7 @@ export class PostListing extends Component<PostListingProps, PostListingState> {
<li>{this.purgePostButton}</li> <li>{this.purgePostButton}</li>
</> </>
)} )}
{!isBanned(post_view.creator) && post_view.creator.local && ( {!isBanned(pv.creator) && pv.creator.local && (
<li>{this.toggleAdminButton}</li> <li>{this.toggleAdminButton}</li>
)} )}
</> </>
@ -761,22 +756,22 @@ export class PostListing extends Component<PostListingProps, PostListingState> {
} }
get commentsButton() { get commentsButton() {
const post_view = this.postView; const pv = this.postView;
const title = I18NextService.i18n.t("number_of_comments", { const title = I18NextService.i18n.t("number_of_comments", {
count: Number(post_view.counts.comments), count: Number(pv.counts.comments),
formattedCount: Number(post_view.counts.comments), formattedCount: Number(pv.counts.comments),
}); });
return ( return (
<Link <Link
className="btn btn-link btn-sm text-muted ps-0" className="btn btn-link btn-sm text-muted ps-0"
title={title} title={title}
to={`/post/${post_view.post.id}?scrollToComments=true`} to={`/post/${pv.post.id}?scrollToComments=true`}
data-tippy-content={title} data-tippy-content={title}
target={this.linkTarget} target={this.linkTarget}
> >
<Icon icon="message-square" classes="me-1" inline /> <Icon icon="message-square" classes="me-1" inline />
{post_view.counts.comments} {pv.counts.comments}
{this.unreadCount && ( {this.unreadCount && (
<> <>
{" "} {" "}
@ -1049,7 +1044,7 @@ export class PostListing extends Component<PostListingProps, PostListingState> {
> >
{this.state.addModLoading ? ( {this.state.addModLoading ? (
<Spinner /> <Spinner />
) : this.creatorIsMod_ ? ( ) : this.postView.creator_is_moderator ? (
capitalizeFirstLetter(I18NextService.i18n.t("remove_as_mod")) capitalizeFirstLetter(I18NextService.i18n.t("remove_as_mod"))
) : ( ) : (
capitalizeFirstLetter(I18NextService.i18n.t("appoint_as_mod")) capitalizeFirstLetter(I18NextService.i18n.t("appoint_as_mod"))
@ -1114,7 +1109,7 @@ export class PostListing extends Component<PostListingProps, PostListingState> {
> >
{this.state.addAdminLoading ? ( {this.state.addAdminLoading ? (
<Spinner /> <Spinner />
) : this.creatorIsAdmin_ ? ( ) : this.postView.creator_is_admin ? (
capitalizeFirstLetter(I18NextService.i18n.t("remove_as_admin")) capitalizeFirstLetter(I18NextService.i18n.t("remove_as_admin"))
) : ( ) : (
capitalizeFirstLetter(I18NextService.i18n.t("appoint_as_admin")) capitalizeFirstLetter(I18NextService.i18n.t("appoint_as_admin"))
@ -1428,7 +1423,7 @@ export class PostListing extends Component<PostListingProps, PostListingState> {
} }
handleShare(i: PostListing) { handleShare(i: PostListing) {
const { name, body, id } = i.props.post_view.post; const { name, body, id } = i.postView.post;
share({ share({
title: name, title: name,
text: body?.slice(0, 50), text: body?.slice(0, 50),
@ -1633,12 +1628,12 @@ export class PostListing extends Component<PostListingProps, PostListingState> {
event.preventDefault(); event.preventDefault();
i.setState({ banLoading: true }); i.setState({ banLoading: true });
const ban = !i.props.post_view.creator_banned_from_community; const ban = !i.postView.creator_banned_from_community;
// If its an unban, restore all their data // If its an unban, restore all their data
if (ban === false) { if (ban === false) {
i.setState({ removeData: false }); i.setState({ removeData: false });
} }
const person_id = i.props.post_view.creator.id; const person_id = i.postView.creator.id;
const remove_data = i.state.removeData; const remove_data = i.state.removeData;
const reason = i.state.banReason; const reason = i.state.banReason;
const expires = futureDaysToUnixTime(i.state.banExpireDays); const expires = futureDaysToUnixTime(i.state.banExpireDays);
@ -1669,7 +1664,7 @@ export class PostListing extends Component<PostListingProps, PostListingState> {
i.props.onAddModToCommunity({ i.props.onAddModToCommunity({
community_id: i.postView.community.id, community_id: i.postView.community.id,
person_id: i.postView.creator.id, person_id: i.postView.creator.id,
added: !i.creatorIsMod_, added: !i.postView.creator_is_moderator,
}); });
} }
@ -1677,7 +1672,7 @@ export class PostListing extends Component<PostListingProps, PostListingState> {
i.setState({ addAdminLoading: true }); i.setState({ addAdminLoading: true });
i.props.onAddAdmin({ i.props.onAddAdmin({
person_id: i.postView.creator.id, person_id: i.postView.creator.id,
added: !i.creatorIsAdmin_, added: !i.postView.creator_is_admin,
}); });
} }
@ -1710,9 +1705,9 @@ export class PostListing extends Component<PostListingProps, PostListingState> {
i.setState({ imageExpanded: !i.state.imageExpanded }); i.setState({ imageExpanded: !i.state.imageExpanded });
setupTippy(); setupTippy();
if (myAuth() && !i.props.post_view.read) { if (myAuth() && !i.postView.read) {
i.props.onMarkPostAsRead({ i.props.onMarkPostAsRead({
post_ids: [i.props.post_view.post.id], post_ids: [i.postView.post.id],
read: true, read: true,
}); });
} }
@ -1780,12 +1775,4 @@ export class PostListing extends Component<PostListingProps, PostListingState> {
get canAdmin_(): boolean { get canAdmin_(): boolean {
return canAdmin(this.postView.creator.id, this.props.admins); return canAdmin(this.postView.creator.id, this.props.admins);
} }
get creatorIsMod_(): boolean {
return isMod(this.postView.creator.id, this.props.moderators);
}
get creatorIsAdmin_(): boolean {
return isAdmin(this.postView.creator.id, this.props.admins);
}
} }

View file

@ -57,6 +57,7 @@ export class PostReport extends Component<PostReportProps, PostReportState> {
my_vote: r.my_vote, my_vote: r.my_vote,
unread_comments: 0, unread_comments: 0,
creator_is_moderator: false, creator_is_moderator: false,
creator_is_admin: false,
}; };
return ( return (

View file

@ -1,10 +1,11 @@
import { isMod } from "@utils/roles"; import { CommunityId } from "lemmy-js-client";
import { CommunityModeratorView } from "lemmy-js-client";
import { UserService } from "../../services"; import { UserService } from "../../services";
export default function amMod( export default function amMod(
mods?: CommunityModeratorView[], communityId: CommunityId,
myUserInfo = UserService.Instance.myUserInfo, myUserInfo = UserService.Instance.myUserInfo,
): boolean { ): boolean {
return myUserInfo ? isMod(myUserInfo.local_user_view.person.id, mods) : false; return myUserInfo
? myUserInfo.moderates.map(cmv => cmv.community.id).includes(communityId)
: false;
} }

View file

@ -6,9 +6,7 @@ import amTopMod from "./am-top-mod";
import canAdmin from "./can-admin"; import canAdmin from "./can-admin";
import canCreateCommunity from "./can-create-community"; import canCreateCommunity from "./can-create-community";
import canMod from "./can-mod"; import canMod from "./can-mod";
import isAdmin from "./is-admin";
import isBanned from "./is-banned"; import isBanned from "./is-banned";
import isMod from "./is-mod";
export { export {
amAdmin, amAdmin,
@ -19,7 +17,5 @@ export {
canAdmin, canAdmin,
canCreateCommunity, canCreateCommunity,
canMod, canMod,
isAdmin,
isBanned, isBanned,
isMod,
}; };

View file

@ -1,8 +0,0 @@
import { PersonView } from "lemmy-js-client";
export default function isAdmin(
creatorId: number,
admins?: PersonView[],
): boolean {
return admins?.some(({ person: { id } }) => id === creatorId) ?? false;
}

View file

@ -1,8 +0,0 @@
import { CommunityModeratorView } from "lemmy-js-client";
export default function isMod(
creatorId: number,
mods?: CommunityModeratorView[],
): boolean {
return mods?.map(m => m.moderator.id).includes(creatorId) ?? false;
}

View file

@ -6069,10 +6069,10 @@ leac@^0.6.0:
resolved "https://registry.yarnpkg.com/leac/-/leac-0.6.0.tgz#dcf136e382e666bd2475f44a1096061b70dc0912" resolved "https://registry.yarnpkg.com/leac/-/leac-0.6.0.tgz#dcf136e382e666bd2475f44a1096061b70dc0912"
integrity sha512-y+SqErxb8h7nE/fiEX07jsbuhrpO9lL8eca7/Y1nuWV2moNlXhyd59iDGcRf6moVyDMbmTNzL40SUyrFU/yDpg== integrity sha512-y+SqErxb8h7nE/fiEX07jsbuhrpO9lL8eca7/Y1nuWV2moNlXhyd59iDGcRf6moVyDMbmTNzL40SUyrFU/yDpg==
lemmy-js-client@0.19.0-alpha.16: lemmy-js-client@0.19.0-rc.15:
version "0.19.0-alpha.16" version "0.19.0-rc.15"
resolved "https://registry.yarnpkg.com/lemmy-js-client/-/lemmy-js-client-0.19.0-alpha.16.tgz#4ec26e393856db7ddf86dba83f24633eeacee7c4" resolved "https://registry.yarnpkg.com/lemmy-js-client/-/lemmy-js-client-0.19.0-rc.15.tgz#03d600841f5e809bdc5d817c7295b8e6952c48dd"
integrity sha512-pmfrkPrHBkhEFhw/BDiTPy2/aQLoURLwBftMa4Lc+ZYiRfVClCOPkbKiqDSYXYlWcPz5MtwM/bjSNxasvVHfTA== integrity sha512-lCNvke29fPWTX4CIqDnewgNAtDk4D/syHiedyih7wXqW0yHfg2Z1+lm+GVao2QUa2NCNGIMO1vV7FJPiXl95rA==
dependencies: dependencies:
cross-fetch "^3.1.5" cross-fetch "^3.1.5"
form-data "^4.0.0" form-data "^4.0.0"