unify return + linkify ban target

This commit is contained in:
nexy7574 2025-01-06 01:05:58 +00:00
parent aab32231a6
commit b7520e986a

View file

@ -1,27 +1,57 @@
// gomuks - A Matrix client written in Go.
// Copyright (C) 2024 Tulir Asokan
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU Affero General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Affero General Public License for more details.
//
// You should have received a copy of the GNU Affero General Public License
// along with this program. If not, see <https://www.gnu.org/licenses/>.
import { use } from "react"
import { PolicyRuleContent } from "@/api/types"
import MainScreenContext from "@/ui/MainScreenContext.ts"
import EventContentProps from "./props.ts"
const BanPolicyBody = ({ event, sender }: EventContentProps) => {
const content = event.content as PolicyRuleContent
const prevContent = event.unsigned.prev_content as PolicyRuleContent | undefined
const mainScreen = use(MainScreenContext)
if (prevContent !== undefined) {
// all fields for content are missing (this is against spec?) so we need to use the prev event's content
if (content.entity === undefined) {
// unban
return <div className="policy-body">
{sender?.content.displayname ?? event.sender} Removed the policy rule banning {prevContent.entity}
</div>
}
// update
return <div className="policy-body">
{sender?.content.displayname ?? event.sender} Updated a policy rule banning {content.entity} for
{content.reason}
</div>
let entity = <span>content.entity || prevContent?.entity</span>
if(event.type === "m.policy.rule.user" && !content.entity?.includes("*") && !content.entity?.includes("?")) {
// Is user policy, and does not include the glob chars * and ?
entity = (
<a
className="hicli-matrix-uri hicli-matrix-uri-user"
href={`matrix:u/${content.entity.slice(1)}`}
onClick={mainScreen.clickRightPanelOpener}
data-target-panel="user"
data-target-user={content.entity}
>
{content.entity}
</a>
)
}
let action = "added"
if (prevContent) {
if (!content) {
// If the content is empty, the ban is revoked.
action = "removed"
} else {
// There is still content, so the policy was updated
action = "updated"
}
}
// add
return <div className="policy-body">
{sender?.content.displayname ?? event.sender} Added a policy rule banning {content.entity} for {content.reason}
{sender?.content.displayname ?? event.sender} {action} a policy rule
{action === "removed" ? "un" : null}banning {entity} for {content.reason}
</div>
}