Skip to content

Commit

Permalink
Fix more bugs
Browse files Browse the repository at this point in the history
  • Loading branch information
tulir committed Apr 20, 2024
1 parent 7d7f93e commit 4743225
Show file tree
Hide file tree
Showing 6 changed files with 29 additions and 11 deletions.
11 changes: 8 additions & 3 deletions config/bridge.go
Original file line number Diff line number Diff line change
Expand Up @@ -177,9 +177,14 @@ func (bc *BridgeConfig) FormatDisplayname(user *slack.User) string {

func (bc *BridgeConfig) FormatBotDisplayname(bot *slack.Bot) string {
return bc.FormatDisplayname(&slack.User{
ID: bot.ID,
Name: bot.Name,
IsBot: true,
ID: bot.ID,
Name: bot.Name,
IsBot: true,
Deleted: bot.Deleted,
Updated: bot.Updated,
Profile: slack.UserProfile{
DisplayName: bot.Name,
},
})
}

Expand Down
3 changes: 3 additions & 0 deletions database/message.go
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,9 @@ func (pid *PartID) Scan(i any) error {
if !ok {
return fmt.Errorf("invalid type %T for PartID.Scan", i)
}
if strVal == "" {
return nil
}
parts := strings.Split(strVal, ":")
if len(parts) != 3 {
return fmt.Errorf("invalid PartID format: %q", strVal)
Expand Down
5 changes: 3 additions & 2 deletions example-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -82,9 +82,10 @@ bridge:
# {{.}} is replaced with the internal ID of the Slack user.
username_template: slack_{{.}}
# Displayname template for Slack users. Available variables:
# .Name - The displayname of the user
# .Name - The username of the user
# .ID - The internal ID of the user
# .IsBot - Whether the user is a bot
# .Profile.DisplayName - The username or real name of the user (depending on settings)
# Variables only available for users (not bots):
# .TeamID - The internal ID of the workspace the user is in
# .TZ - The timezone region of the user (e.g. Europe/London)
Expand All @@ -97,7 +98,7 @@ bridge:
# .Profile.Pronouns - The pronouns of the user
# .Profile.Email - The email address of the user
# .Profile.Phone - The formatted phone number of the user
displayname_template: '{{.Name}}{{if .IsBot}} (bot){{end}}'
displayname_template: '{{.Profile.DisplayName}}{{if .IsBot}} (bot){{end}}'
# Channel name template for Slack channels (all types). Available variables:
# .Name - The name of the channel
# .TeamName - The name of the team the channel is in
Expand Down
5 changes: 4 additions & 1 deletion portal.go
Original file line number Diff line number Diff line change
Expand Up @@ -1059,7 +1059,7 @@ func (portal *Portal) updateChannelType(channel *slack.Channel) bool {
portal.zlog.Debug().Stringer("channel_type", newType).Msg("Found channel type")
portal.Type = newType
} else if portal.Type != newType {
portal.zlog.Warn().Stringer("channel_type", newType).Msg("Channel type changed")
portal.zlog.Warn().Stringer("old_type", portal.Type).Stringer("channel_type", newType).Msg("Channel type changed")
portal.Type = newType
} else {
return false
Expand Down Expand Up @@ -1253,6 +1253,8 @@ func (portal *Portal) UpdateInfo(ctx context.Context, source *UserTeam, meta *sl
}
}

portal.zlog.Trace().Any("channel_info", meta).Msg("Syncing channel")

changed := portal.updateChannelType(meta)

if portal.DMUserID == "" && portal.IsPrivateChat() {
Expand Down Expand Up @@ -1323,6 +1325,7 @@ func (portal *Portal) handleSlackEvent(source *UserTeam, rawEvt any) {
log.Err(err).Msg("Failed to create portal room after join event")
}
} else {
log.Debug().Msg("Syncing Matrix room from joined channel event")
portal.UpdateInfo(ctx, source, ch, true)
}
case *slack.ChannelLeftEvent, *slack.GroupLeftEvent:
Expand Down
7 changes: 5 additions & 2 deletions teamportal.go
Original file line number Diff line number Diff line change
Expand Up @@ -369,8 +369,10 @@ func (team *Team) RemoveMXID(ctx context.Context) {
// }

func (team *Team) AddPortalToSpace(ctx context.Context, portal *Portal) bool {
if len(team.MXID) == 0 {
team.log.Error().Msg("Tried to add portal to team that has no matrix ID")
if team.MXID == "" || portal.MXID == "" {
if team.MXID == "" {
team.log.Error().Str("portal_channel_id", portal.ChannelID).Msg("Tried to add portal to team that has no matrix ID")
}
if portal.InSpace {
portal.InSpace = false
return true
Expand All @@ -380,6 +382,7 @@ func (team *Team) AddPortalToSpace(ctx context.Context, portal *Portal) bool {
return false
}

team.log.Info().Stringer("room_mxid", portal.MXID).Str("portal_channel_id", portal.ChannelID).Msg("Adding portal to space")
_, err := team.bridge.Bot.SendStateEvent(ctx, team.MXID, event.StateSpaceChild, portal.MXID.String(), &event.SpaceChildEventContent{
Via: []string{team.bridge.AS.HomeserverDomain},
})
Expand Down
9 changes: 6 additions & 3 deletions userteam.go
Original file line number Diff line number Diff line change
Expand Up @@ -276,7 +276,9 @@ func (ut *UserTeam) syncPortals(ctx context.Context) {
if channel.IsIM && (channel.Latest == nil || channel.Latest.SubType == "") {
continue
}
serverInfo[channel.ID] = &channel
// TODO remove this after switching to Go 1.22 with loop var fix
channelCopy := channel
serverInfo[channel.ID] = &channelCopy
}
if nextCursor == "" || len(channelsChunk) == 0 {
break
Expand All @@ -288,7 +290,8 @@ func (ut *UserTeam) syncPortals(ctx context.Context) {
existingPortals := ut.bridge.GetAllPortalsForUserTeam(ut.UserTeamMXIDKey)
for _, portal := range existingPortals {
if portal.MXID != "" {
portal.UpdateInfo(ctx, ut, serverInfo[portal.ChannelID], true)
// Don't actually use the fetched metadata, it doesn't have enough info
portal.UpdateInfo(ctx, ut, nil, true)
delete(serverInfo, portal.ChannelID)
}
}
Expand All @@ -306,7 +309,7 @@ func (ut *UserTeam) syncPortals(ctx context.Context) {
if portal == nil {
continue
}
err := portal.CreateMatrixRoom(ctx, ut, ch)
err := portal.CreateMatrixRoom(ctx, ut, nil)
if err != nil {
ut.Log.Err(err).Str("channel_id", ch.ID).Msg("Failed to create Matrix room for channel")
}
Expand Down

0 comments on commit 4743225

Please sign in to comment.