client: refactor boot logic into SetUp method

This method only has heartbeat session population logic for now, so it's
actually a no-op during provisioning. However, there's probably some
value in "mandating" that clients call this shortly after construction,
so we have a chance to run any setup logic that we might need in the
future.

This _feels_ unidiomatic...?
This commit is contained in:
Skip R
2025-12-18 18:28:07 -08:00
parent 2075a4b853
commit 099b464f84
2 changed files with 23 additions and 7 deletions

View File

@@ -46,16 +46,9 @@ type DiscordClient struct {
}
func (d *DiscordConnector) LoadUserLogin(ctx context.Context, login *bridgev2.UserLogin) error {
log := login.Log
meta := login.Metadata.(*UserLoginMetadata)
session, err := discordgo.New(meta.Token)
if meta.HeartbeatSession.IsExpired() {
log.Info().Msg("Heartbeat session expired, creating a new one")
meta.HeartbeatSession = discordgo.NewHeartbeatSession()
}
meta.HeartbeatSession.BumpLastUsed()
session.HeartbeatSession = meta.HeartbeatSession
login.Save(ctx)
if err != nil {
@@ -71,6 +64,8 @@ func (d *DiscordConnector) LoadUserLogin(ctx context.Context, login *bridgev2.Us
ReuploadMedia: d.ReuploadMedia,
},
}
cl.SetUp(ctx, meta)
login.Client = &cl
return nil
@@ -78,6 +73,26 @@ func (d *DiscordConnector) LoadUserLogin(ctx context.Context, login *bridgev2.Us
var _ bridgev2.NetworkAPI = (*DiscordClient)(nil)
// SetUp performs basic bookkeeping and initialization that should be done
// immediately after a DiscordClient has been created.
//
// nil may be passed for meta, especially during provisioning where we need to
// connect to the Discord gateway, but don't have a UserLogin yet.
func (d *DiscordClient) SetUp(ctx context.Context, meta *UserLoginMetadata) {
log := zerolog.Ctx(ctx)
// We'll have UserLogin metadata if this UserLogin is being loaded from the
// database, i.e. it hasn't just been provisioned.
if meta != nil {
if meta.HeartbeatSession.IsExpired() {
log.Info().Msg("Heartbeat session expired, creating a new one")
meta.HeartbeatSession = discordgo.NewHeartbeatSession()
}
meta.HeartbeatSession.BumpLastUsed()
d.Session.HeartbeatSession = meta.HeartbeatSession
}
}
func (d *DiscordClient) Connect(ctx context.Context) {
log := zerolog.Ctx(ctx)

View File

@@ -84,6 +84,7 @@ func (dl *DiscordTokenLogin) SubmitUserInput(ctx context.Context, input map[stri
connector: dl.connector,
Session: session,
}
client.SetUp(ctx, nil)
err = client.connect(ctx)
if err != nil {
dl.softlyCloseSession()