Skip to content

Commit

Permalink
Fixes #224.
Browse files Browse the repository at this point in the history
The @admin, @tho, @TwitarrTeam, and @moderator accounts now have distinctive styling in bylines and @mentions. Styling is based on the username and not access level, as the UserHeader is the only info we have on the user at the time we're styling their name.

This means that moderator users won't have their bylines highlighted. This is both because it's more involved of a change (we'd probably have to add a userLevel field to UserHeader), and also because mod users probably want to post as 'normal' users most of the time, and can post as @moderator (and get the special styling) when they need to.
  • Loading branch information
challf committed Jun 25, 2024
1 parent 5290f5e commit 02da8e5
Showing 1 changed file with 22 additions and 8 deletions.
30 changes: 22 additions & 8 deletions Sources/swiftarr/Site/Utilities/CustomLeafTags.swift
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,8 @@ struct FormatPostTextTag: UnsafeUnescapedLeafTag {
let range = Range(mentionMatch.range(at: 1), in: modifiedText)!
let mention = String(modifiedText[range])
let username = String(mention.dropFirst()) // Drop the initial "@"
let link = "<a class=\"link-primary\" href=\"/username/\(username)\">\(mention)</a>"
var linkStyle = ["admin", "THO", "TwitarrTeam", "moderator"].contains(username) ? "link-danger" : "link-primary"
let link = "<a class=\"\(linkStyle)\" href=\"/username/\(username)\">\(mention)</a>"
modifiedText.replaceSubrange(range, with: link)
}
string = modifiedText
Expand Down Expand Up @@ -568,14 +569,27 @@ struct UserBylineTag: UnsafeUnescapedLeafTag {
if ctx.parameters.count >= 2, let newStyle = ctx.parameters[1].string {
styling = newStyle
}
if styling == "nolink" {
func stylingSearch(style: String, in styling: inout String) -> Bool {
if styling.contains(style) {
styling = styling.replacingOccurrences(of: style, with: "")
return true
}
return false
}
let shortStyle = stylingSearch(style: "short", in: &styling)
let nolinkStyle = stylingSearch(style: "nolink", in: &styling)
let pronounStyle = stylingSearch(style: "pronoun", in: &styling)
if ["admin", "THO", "TwitarrTeam", "moderator"].contains(username) {
styling.append(" text-danger")
}
if nolinkStyle {
if let displayName = userHeader["displayName"]?.string?.htmlEscaped() {
return LeafData.string("<b>\(displayName)</b> @\(username)")
return LeafData.string("<span class=\"\(styling)\"><b>\(displayName)</b> @\(username)</span>")
}
return LeafData.string("@\(username)")
return LeafData.string("<span class=\"\(styling)\">@\(username)</span>")
}
else if styling != "short", let displayName = userHeader["displayName"]?.string?.htmlEscaped() {
if styling == "pronoun", let preferredPronoun = userHeader["preferredPronoun"]?.string?.htmlEscaped() {
else if !shortStyle, let displayName = userHeader["displayName"]?.string?.htmlEscaped() {
if pronounStyle, let preferredPronoun = userHeader["preferredPronoun"]?.string?.htmlEscaped() {
return LeafData.string(
"<a class=\"\(styling)\" href=\"/user/\(userID)\"><b>\(displayName)</b> @\(username) (\(preferredPronoun))</a>"
)
Expand All @@ -585,7 +599,7 @@ struct UserBylineTag: UnsafeUnescapedLeafTag {
)
}
else {
if styling == "pronoun", let preferredPronoun = userHeader["preferredPronoun"]?.string?.htmlEscaped() {
if pronounStyle, let preferredPronoun = userHeader["preferredPronoun"]?.string?.htmlEscaped() {
return LeafData.string(
"<a class=\"\(styling)\" href=\"/user/\(userID)\">@\(username) (\(preferredPronoun))</a>"
)
Expand Down Expand Up @@ -622,4 +636,4 @@ struct DinnerTeamTag: LeafTag {
}
return LeafData.string(dinnerTeam.label)
}
}
}

0 comments on commit 02da8e5

Please sign in to comment.