Skip to content

Commit

Permalink
Improve channel naming
Browse files Browse the repository at this point in the history
  • Loading branch information
tulir committed Apr 20, 2024
1 parent 230f357 commit b041792
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 12 deletions.
7 changes: 4 additions & 3 deletions config/bridge.go
Original file line number Diff line number Diff line change
Expand Up @@ -191,9 +191,10 @@ func (bc *BridgeConfig) FormatBotDisplayname(bot *slack.Bot) string {

type ChannelNameParams struct {
*slack.Channel
Type database.ChannelType
TeamName string
TeamDomain string
Type database.ChannelType
TeamName string
TeamDomain string
IsNoteToSelf bool
}

func (bc *BridgeConfig) FormatChannelName(params ChannelNameParams) string {
Expand Down
3 changes: 2 additions & 1 deletion example-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,7 @@ bridge:
# .TeamName - The name of the team the channel is in
# .TeamDomain - The Slack subdomain of the team the channel is in
# .ID - The internal ID of the channel
# .IsNoteToSelf - Whether the channel is a DM with yourself
# .IsGeneral - Whether the channel is the #general channel
# .IsChannel - Whether the channel is a channel (rather than a DM)
# .IsPrivate - Whether the channel is private
Expand All @@ -112,7 +113,7 @@ bridge:
# .IsShared - Whether the channel is shared with another workspace.
# .IsExtShared - Whether the channel is shared with an external organization.
# .IsOrgShared - Whether the channel is shared with an organization in the same enterprise grid.
channel_name_template: '{{if .IsChannel}}{{if .IsPrivate}}🔒️{{else}}#{{end}}{{end}}{{.Name}}'
channel_name_template: '{{if and .IsChannel (not .IsPrivate)}}#{{end}}{{.Name}}{{if .IsNoteToSelf}} (you){{end}}'
# Displayname template for Slack workspaces. Available variables:
# .Name - The name of the team
# .Domain - The Slack subdomain of the team
Expand Down
53 changes: 45 additions & 8 deletions portal.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@ import (
"strings"
"sync"
"time"
"unicode"
"unicode/utf8"

"github.com/rs/zerolog"
"github.com/slack-go/slack"
Expand Down Expand Up @@ -98,10 +100,15 @@ func (portal *Portal) MarkEncrypted() {
portal.Update(context.TODO())
}

func (portal *Portal) IsNoteToSelf() bool {
return portal.Type == database.ChannelTypeDM && portal.DMUserID != "" && portal.DMUserID == portal.Receiver
}

func (portal *Portal) shouldSetDMRoomMetadata() bool {
if portal.Type == database.ChannelTypeDM {
return portal.bridge.Config.Bridge.PrivateChatPortalMeta == "always" ||
(portal.IsEncrypted() && portal.bridge.Config.Bridge.PrivateChatPortalMeta != "never")
(portal.IsEncrypted() && portal.bridge.Config.Bridge.PrivateChatPortalMeta != "never") ||
portal.IsNoteToSelf()
} else if portal.Type == database.ChannelTypeGroupDM {
return portal.bridge.Config.Bridge.PrivateChatPortalMeta != "never"
} else {
Expand Down Expand Up @@ -1065,16 +1072,45 @@ func (portal *Portal) updateChannelType(channel *slack.Channel) bool {
return true
}

func compareStringFold(a, b string) int {
for {
if a == "" {
if b == "" {
return 0
}
return -1
} else if b == "" {
return 1
}
aRune, aSize := utf8.DecodeRuneInString(a)
bRune, bSize := utf8.DecodeRuneInString(b)

aLower := unicode.ToLower(aRune)
bLower := unicode.ToLower(bRune)
if aLower < bLower {
return -1
} else if bLower > aLower {
return 1
}
a = a[aSize:]
b = b[bSize:]
}
}

func (portal *Portal) GetPlainName(meta *slack.Channel) string {
switch portal.Type {
case database.ChannelTypeDM:
return portal.GetDMPuppet().Name
case database.ChannelTypeGroupDM:
puppetNames := make([]string, len(meta.Members))
for i, member := range meta.Members {
puppetNames := make([]string, 0, len(meta.Members))
for _, member := range meta.Members {
if member == portal.Receiver {
continue
}
puppet := portal.Team.GetPuppetByID(member)
puppetNames[i] = puppet.Name
puppetNames = append(puppetNames, puppet.Name)
}
slices.SortFunc(puppetNames, compareStringFold)
return strings.Join(puppetNames, ", ")
default:
return meta.Name
Expand Down Expand Up @@ -1107,10 +1143,11 @@ func (portal *Portal) UpdateName(ctx context.Context, meta *slack.Channel) bool
plainNameChanged := portal.PlainName != meta.Name
portal.PlainName = meta.Name
formattedName := portal.bridge.Config.Bridge.FormatChannelName(config.ChannelNameParams{
Channel: meta,
Type: portal.Type,
TeamName: portal.Team.Name,
TeamDomain: portal.Team.Domain,
Channel: meta,
Type: portal.Type,
TeamName: portal.Team.Name,
TeamDomain: portal.Team.Domain,
IsNoteToSelf: portal.IsNoteToSelf(),
})

return portal.UpdateNameDirect(ctx, formattedName) || plainNameChanged
Expand Down

0 comments on commit b041792

Please sign in to comment.