diff --git a/pkg/connector/capabilities.go b/pkg/connector/capabilities.go index 4b8288f..855ff81 100644 --- a/pkg/connector/capabilities.go +++ b/pkg/connector/capabilities.go @@ -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{ diff --git a/pkg/connector/chatinfo.go b/pkg/connector/chatinfo.go index c410b45..b9187a8 100644 --- a/pkg/connector/chatinfo.go +++ b/pkg/connector/chatinfo.go @@ -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" @@ -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) } diff --git a/pkg/connector/dbmeta.go b/pkg/connector/dbmeta.go index 3c5b07c..04948d0 100644 --- a/pkg/connector/dbmeta.go +++ b/pkg/connector/dbmeta.go @@ -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 { @@ -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"`