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-server": "^8.2.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",
"markdown-it": "^13.0.1",
"markdown-it-bidi": "^0.1.0",

View file

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

View file

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

View file

@ -268,7 +268,7 @@ export class Sidebar extends Component<SidebarProps, SidebarState> {
return (
<>
<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">
<button
@ -468,7 +468,7 @@ export class Sidebar extends Component<SidebarProps, SidebarState> {
get canPost(): boolean {
return (
!this.props.community_view.community.posting_restricted_to_mods ||
amMod(this.props.moderators) ||
amMod(this.props.community_view.community.id) ||
amAdmin()
);
}

View file

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

View file

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

View file

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

View file

@ -1,10 +1,11 @@
import { isMod } from "@utils/roles";
import { CommunityModeratorView } from "lemmy-js-client";
import { CommunityId } from "lemmy-js-client";
import { UserService } from "../../services";
export default function amMod(
mods?: CommunityModeratorView[],
communityId: CommunityId,
myUserInfo = UserService.Instance.myUserInfo,
): 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 canCreateCommunity from "./can-create-community";
import canMod from "./can-mod";
import isAdmin from "./is-admin";
import isBanned from "./is-banned";
import isMod from "./is-mod";
export {
amAdmin,
@ -19,7 +17,5 @@ export {
canAdmin,
canCreateCommunity,
canMod,
isAdmin,
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"
integrity sha512-y+SqErxb8h7nE/fiEX07jsbuhrpO9lL8eca7/Y1nuWV2moNlXhyd59iDGcRf6moVyDMbmTNzL40SUyrFU/yDpg==
lemmy-js-client@0.19.0-alpha.16:
version "0.19.0-alpha.16"
resolved "https://registry.yarnpkg.com/lemmy-js-client/-/lemmy-js-client-0.19.0-alpha.16.tgz#4ec26e393856db7ddf86dba83f24633eeacee7c4"
integrity sha512-pmfrkPrHBkhEFhw/BDiTPy2/aQLoURLwBftMa4Lc+ZYiRfVClCOPkbKiqDSYXYlWcPz5MtwM/bjSNxasvVHfTA==
lemmy-js-client@0.19.0-rc.15:
version "0.19.0-rc.15"
resolved "https://registry.yarnpkg.com/lemmy-js-client/-/lemmy-js-client-0.19.0-rc.15.tgz#03d600841f5e809bdc5d817c7295b8e6952c48dd"
integrity sha512-lCNvke29fPWTX4CIqDnewgNAtDk4D/syHiedyih7wXqW0yHfg2Z1+lm+GVao2QUa2NCNGIMO1vV7FJPiXl95rA==
dependencies:
cross-fetch "^3.1.5"
form-data "^4.0.0"