Skip to content

Commit

Permalink
chatinfo: resync ghosts more often
Browse files Browse the repository at this point in the history
  • Loading branch information
tulir committed Jul 18, 2024
1 parent ce7c9e3 commit 0c34dc8
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 8 deletions.
6 changes: 5 additions & 1 deletion pkg/connector/capabilities.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,11 @@ import (
)

func (s *SlackConnector) GetCapabilities() *bridgev2.NetworkGeneralCapabilities {
return &bridgev2.NetworkGeneralCapabilities{}
return &bridgev2.NetworkGeneralCapabilities{
// GetUserInfo has an internal rate limit of 1 fetch per 24 hours,
// so we're fine to tell the bridge to fetch user info all the time.
AggressiveUpdateInfo: true,
}
}

var roomCaps = &bridgev2.NetworkRoomCapabilities{
Expand Down
19 changes: 14 additions & 5 deletions pkg/connector/chatinfo.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ import (

"github.com/rs/zerolog"
"github.com/slack-go/slack"
"go.mau.fi/util/jsontime"
"go.mau.fi/util/ptr"
"maunium.net/go/mautrix/bridgev2"
"maunium.net/go/mautrix/bridgev2/database"
Expand Down Expand Up @@ -295,15 +296,23 @@ func (s *SlackClient) fetchUserInfo(ctx context.Context, userID string) (*bridge
isBot = true
}
return &bridgev2.UserInfo{
Identifiers: []string{fmt.Sprintf("slack-internal:%s", userID)},
Name: name,
Avatar: makeAvatar(avatarURL),
IsBot: &isBot,
ExtraUpdates: nil,
Identifiers: []string{fmt.Sprintf("slack-internal:%s", userID)},
Name: name,
Avatar: makeAvatar(avatarURL),
IsBot: &isBot,
ExtraUpdates: func(ctx context.Context, ghost *bridgev2.Ghost) bool {
ghost.Metadata.(*GhostMetadata).LastSync = jsontime.UnixNow()
return true
},
}, nil
}

const MinGhostSyncInterval = 24 * time.Hour

func (s *SlackClient) GetUserInfo(ctx context.Context, ghost *bridgev2.Ghost) (*bridgev2.UserInfo, error) {
if time.Since(ghost.Metadata.(*GhostMetadata).LastSync.Time) < MinGhostSyncInterval {
return nil, nil
}
_, userID := slackid.ParseUserID(ghost.ID)
return s.fetchUserInfo(ctx, userID)
}
12 changes: 10 additions & 2 deletions pkg/connector/dbmeta.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,17 @@
package connector

import (
"go.mau.fi/util/jsontime"

"maunium.net/go/mautrix/bridgev2/database"
)

func (s *SlackConnector) GetDBMetaTypes() database.MetaTypes {
return database.MetaTypes{
Portal: nil,
Ghost: nil,
Portal: nil,
Ghost: func() any {
return &GhostMetadata{}
},
Message: nil,
Reaction: nil,
UserLogin: func() any {
Expand All @@ -32,6 +36,10 @@ func (s *SlackConnector) GetDBMetaTypes() database.MetaTypes {
}
}

type GhostMetadata struct {
LastSync jsontime.Unix `json:"last_sync"`
}

type UserLoginMetadata struct {
Email string `json:"email"`
Token string `json:"token"`
Expand Down

0 comments on commit 0c34dc8

Please sign in to comment.