package api import ( "context" "time" "connectrpc.com/connect" apiv1 "tangled.org/beardic.us/chatline/gen/chatto/api/v1" "tangled.org/beardic.us/chatline/internal/output" ) // ManagedBot is a bot account as BotService shows it to someone who can // manage it: its owner, or a holder of bot.manage. A bot that creates bots // needs bot.create; the new bots belong to its own human owner. type ManagedBot struct { ID string `json:"id"` Login string `json:"login"` DisplayName string `json:"display_name,omitempty"` OwnerID string `json:"owner_id"` CreatedAt time.Time `json:"created_at"` Keys []BotKey `json:"keys"` } // BotKey is an active API key's metadata. Chatto shows the key itself only // once, when it issues it. type BotKey struct { ID string `json:"id"` Name string `json:"name"` CreatedAt time.Time `json:"created_at"` LastUsedAt *time.Time `json:"last_used_at,omitempty"` } // IssuedKey is a newly issued API key and the bot it belongs to. type IssuedKey struct { Bot *ManagedBot `json:"bot"` Key BotKey `json:"key"` Secret string `json:"-"` } func (c *Client) botFromProto(b *apiv1.Bot) *ManagedBot { u := b.GetUser() c.cacheUser(userFromProto(u)) out := &ManagedBot{ID: u.GetId(), Login: u.GetLogin(), DisplayName: u.GetDisplayName(), OwnerID: b.GetOwnerUserId(), CreatedAt: b.GetCreatedAt().AsTime(), Keys: []BotKey{}} for _, k := range b.GetApiKeys() { out.Keys = append(out.Keys, keyFromProto(k)) } return out } func keyFromProto(k *apiv1.BotApiKey) BotKey { key := BotKey{ID: k.GetId(), Name: k.GetName(), CreatedAt: k.GetCreatedAt().AsTime()} if k.GetLastUsedState() == apiv1.CredentialLastUsedState_CREDENTIAL_LAST_USED_STATE_RECORDED && k.LastUsedAt != nil { t := k.GetLastUsedAt().AsTime() key.LastUsedAt = &t } return key } // ListBots returns the bots the caller can manage, optionally matching // search against login and display name. func (c *Client) ListBots(ctx context.Context, search string) ([]*ManagedBot, error) { return listAll(func(page *apiv1.PageRequest) ([]*ManagedBot, *apiv1.PageInfo, error) { resp, err := c.Bots.ListBots(ctx, connect.NewRequest(&apiv1.ListBotsRequest{Search: search, Page: page})) if err != nil { return nil, nil, err } out := make([]*ManagedBot, 0, len(resp.Msg.GetBots())) for _, b := range resp.Msg.GetBots() { out = append(out, c.botFromProto(b)) } return out, resp.Msg.GetPage(), nil }) } // ResolveBot turns a bot's user ID, login, or @login into the bot. func (c *Client) ResolveBot(ctx context.Context, arg string) (*ManagedBot, error) { u, err := c.ResolveUser(ctx, arg) if err != nil { return nil, err } if !u.Bot { return nil, output.Usage("not_a_bot", "@%s is a person, not a bot", u.Login) } resp, err := c.Bots.GetBot(ctx, connect.NewRequest(&apiv1.GetBotRequest{BotUserId: u.ID})) if err != nil { return nil, err } return c.botFromProto(resp.Msg.GetBot()), nil } // CreateBot creates a bot. Chatto makes the caller's human owner its owner, // gives it no permissions, and issues its first API key. func (c *Client) CreateBot(ctx context.Context, login, displayName, keyName string) (*IssuedKey, error) { req := &apiv1.CreateBotRequest{Login: login, DisplayName: displayName} if keyName != "" { req.ApiKeyName = &keyName } resp, err := c.Bots.CreateBot(ctx, connect.NewRequest(req)) if err != nil { return nil, err } return &IssuedKey{Bot: c.botFromProto(resp.Msg.GetBot()), Key: keyFromProto(resp.Msg.GetApiKeyMetadata()), Secret: resp.Msg.GetApiKey()}, nil } // DeleteBot deletes a bot and invalidates all its API keys. func (c *Client) DeleteBot(ctx context.Context, botID string) error { _, err := c.Bots.DeleteBot(ctx, connect.NewRequest(&apiv1.DeleteBotRequest{BotUserId: botID})) return err } // CreateBotKey issues another API key for a bot. The key acts with that // bot's permissions, which can be more than the caller's. func (c *Client) CreateBotKey(ctx context.Context, botID, name string) (*IssuedKey, error) { resp, err := c.Bots.CreateBotApiKey(ctx, connect.NewRequest(&apiv1.CreateBotApiKeyRequest{BotUserId: botID, Name: name})) if err != nil { return nil, err } return &IssuedKey{Bot: c.botFromProto(resp.Msg.GetBot()), Key: keyFromProto(resp.Msg.GetApiKeyMetadata()), Secret: resp.Msg.GetApiKey()}, nil } // RevokeBotKey revokes one of a bot's API keys. func (c *Client) RevokeBotKey(ctx context.Context, botID, keyID string) (*ManagedBot, error) { resp, err := c.Bots.RevokeBotApiKey(ctx, connect.NewRequest(&apiv1.RevokeBotApiKeyRequest{BotUserId: botID, KeyId: keyID})) if err != nil { return nil, err } return c.botFromProto(resp.Msg.GetBot()), nil }