Skip to content

Commit

Permalink
web/timeline: render policy list events (#586)
Browse files Browse the repository at this point in the history
Co-authored-by: Tulir Asokan <[email protected]>
  • Loading branch information
nexy7574 and tulir authored Jan 6, 2025
1 parent 158745b commit bdc8237
Show file tree
Hide file tree
Showing 4 changed files with 77 additions and 0 deletions.
6 changes: 6 additions & 0 deletions web/src/api/types/mxtypes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,12 @@ export interface ACLEventContent {
deny?: string[]
}

export interface PolicyRuleContent {
entity: string
reason: string
recommendation: string
}

export interface PowerLevelEventContent {
users?: Record<UserID, number>
users_default?: number
Expand Down
1 change: 1 addition & 0 deletions web/src/ui/MainScreen.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -217,6 +217,7 @@ class ContextFields implements MainScreenContextFields {
}

clickRightPanelOpener = (evt: React.MouseEvent) => {
evt.preventDefault()
const type = evt.currentTarget.getAttribute("data-target-panel")
if (type === "pinned-messages" || type === "members") {
this.setRightPanel({ type })
Expand Down
61 changes: 61 additions & 0 deletions web/src/ui/timeline/content/PolicyRuleBody.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
// 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 { JSX, use } from "react"
import { PolicyRuleContent } from "@/api/types"
import { getDisplayname } from "@/util/validation.ts"
import MainScreenContext from "../../MainScreenContext.ts"
import EventContentProps from "./props.ts"

const PolicyRuleBody = ({ event, sender }: EventContentProps) => {
const content = event.content as PolicyRuleContent
const prevContent = event.unsigned.prev_content as PolicyRuleContent | undefined
const mainScreen = use(MainScreenContext)

const entity = content.entity ?? prevContent?.entity
const recommendation = content.recommendation ?? prevContent?.recommendation
if (!entity || !recommendation) {
return <div className="policy-body">
{getDisplayname(event.sender, sender?.content)} sent an invalid policy rule
</div>
}
let entityElement = <>{entity}</>
if(event.type === "m.policy.rule.user" && !entity?.includes("*") && !entity?.includes("?")) {
entityElement = (
<a
className="hicli-matrix-uri hicli-matrix-uri-user"
href={`matrix:u/${entity.slice(1)}`}
onClick={mainScreen.clickRightPanelOpener}
data-target-panel="user"
data-target-user={entity}
>
{entity}
</a>
)
}
let recommendationElement: JSX.Element | string = <code>{recommendation}</code>
if (recommendation === "m.ban") {
recommendationElement = "ban"
}
const action = prevContent ? ((content.entity && content.recommendation) ? "updated" : "removed") : "added"
const target = event.type.replace(/^m\.policy\.rule\./, "")
return <div className="policy-body">
{getDisplayname(event.sender, sender?.content)} {action} a {recommendationElement} rule
for {target}s matching <code>{entityElement}</code>
{content.reason ? <> for <code>{content.reason}</code></> : null}
</div>
}

export default PolicyRuleBody
9 changes: 9 additions & 0 deletions web/src/ui/timeline/content/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import LocationMessageBody from "./LocationMessageBody.tsx"
import MediaMessageBody from "./MediaMessageBody.tsx"
import MemberBody from "./MemberBody.tsx"
import PinnedEventsBody from "./PinnedEventsBody.tsx"
import PolicyRuleBody from "./PolicyRuleBody.tsx"
import PowerLevelBody from "./PowerLevelBody.tsx"
import RedactedBody from "./RedactedBody.tsx"
import RoomAvatarBody from "./RoomAvatarBody.tsx"
Expand All @@ -24,6 +25,7 @@ export { default as MediaMessageBody } from "./MediaMessageBody.tsx"
export { default as LocationMessageBody } from "./LocationMessageBody.tsx"
export { default as MemberBody } from "./MemberBody.tsx"
export { default as PinnedEventsBody } from "./PinnedEventsBody.tsx"
export { default as PolicyRuleBody } from "./PolicyRuleBody.tsx"
export { default as PowerLevelBody } from "./PowerLevelBody.tsx"
export { default as RedactedBody } from "./RedactedBody.tsx"
export { default as RoomAvatarBody } from "./RoomAvatarBody.tsx"
Expand Down Expand Up @@ -82,6 +84,12 @@ export function getBodyType(evt: MemDBEvent, forReply = false): React.FunctionCo
return RoomAvatarBody
case "m.room.server_acl":
return ACLBody
case "m.policy.rule.user":
return PolicyRuleBody
case "m.policy.rule.room":
return PolicyRuleBody
case "m.policy.rule.server":
return PolicyRuleBody
case "m.room.pinned_events":
return PinnedEventsBody
case "m.room.power_levels":
Expand All @@ -97,6 +105,7 @@ export function isSmallEvent(bodyType: React.FunctionComponent<EventContentProps
case RoomNameBody:
case RoomAvatarBody:
case ACLBody:
case PolicyRuleBody:
case PinnedEventsBody:
case PowerLevelBody:
return true
Expand Down

0 comments on commit bdc8237

Please sign in to comment.