feat: sync bot keyboards and callbacks
Sync telesrv b96f2dd (feat(bot): complete keyboards callbacks and durable delivery). Skipped private docs and preserved public README files per sync rules; normalized the appearance seed log label for public naming.
This commit is contained in:
parent
0c99ae0a9d
commit
bf965f610c
80 changed files with 7212 additions and 349 deletions
|
|
@ -183,6 +183,22 @@ func TestBotStoreRoundTripPostgres(t *testing.T) {
|
|||
if flagBot, _, _ := bots.GetBot(ctx, bot1.ID); !flagBot.Nochats || !flagBot.ChatHistory {
|
||||
t.Fatalf("flags = nochats=%v chat_history=%v, want both true", flagBot.Nochats, flagBot.ChatHistory)
|
||||
}
|
||||
requestedButton := domain.BotRequestedWebViewButton{
|
||||
WebAppReqID: fmt.Sprintf("pg-requested-%d", suffix), BotUserID: bot1.ID, UserID: owner.ID,
|
||||
ButtonID: 45, Text: "Share", PeerType: "user", MaxQuantity: 2,
|
||||
NameRequested: true, UsernameRequested: true, PhotoRequested: true,
|
||||
CreatedAt: time.Now(), ExpiresAt: time.Now().Add(time.Hour),
|
||||
}
|
||||
t.Cleanup(func() {
|
||||
_ = bots.DeleteRequestedWebViewButton(ctx, bot1.ID, owner.ID, requestedButton.WebAppReqID)
|
||||
})
|
||||
if err := bots.SaveRequestedWebViewButton(ctx, requestedButton); err != nil {
|
||||
t.Fatalf("save requested button: %v", err)
|
||||
}
|
||||
storedButton, found, err := bots.GetRequestedWebViewButton(ctx, bot1.ID, owner.ID, requestedButton.WebAppReqID)
|
||||
if err != nil || !found || !storedButton.NameRequested || !storedButton.UsernameRequested || !storedButton.PhotoRequested {
|
||||
t.Fatalf("requested button=%#v found=%v err=%v", storedButton, found, err)
|
||||
}
|
||||
if can, err := bots.CanBotSendMessage(ctx, bot1.ID, owner.ID); err != nil || can {
|
||||
t.Fatalf("CanBotSendMessage before allow = %v,%v, want false,nil", can, err)
|
||||
}
|
||||
|
|
|
|||
|
|
@ -416,16 +416,29 @@ func (s *BotStore) SaveRequestedWebViewButton(ctx context.Context, button domain
|
|||
if button.BotUserID == 0 || button.UserID == 0 || button.WebAppReqID == "" || button.ExpiresAt.IsZero() {
|
||||
return domain.ErrBotRequestedButtonInvalid
|
||||
}
|
||||
_, err := s.db.Exec(ctx, `
|
||||
INSERT INTO webview_requested_buttons (webapp_req_id, bot_user_id, user_id, button_id, text, peer_type, max_quantity, created_at, expires_at)
|
||||
VALUES ($1,$2,$3,$4,$5,$6,$7,$8,$9)
|
||||
peerFilter, err := json.Marshal(button.PeerFilter)
|
||||
if err != nil {
|
||||
return domain.ErrBotRequestedButtonInvalid
|
||||
}
|
||||
_, err = s.db.Exec(ctx, `
|
||||
INSERT INTO webview_requested_buttons (
|
||||
webapp_req_id, bot_user_id, user_id, button_id, text, peer_type, max_quantity,
|
||||
peer_filter, name_requested, username_requested, photo_requested, created_at, expires_at
|
||||
)
|
||||
VALUES ($1,$2,$3,$4,$5,$6,$7,$8,$9,$10,$11,$12,$13)
|
||||
ON CONFLICT (webapp_req_id) DO UPDATE SET
|
||||
button_id=EXCLUDED.button_id,
|
||||
text=EXCLUDED.text,
|
||||
peer_type=EXCLUDED.peer_type,
|
||||
max_quantity=EXCLUDED.max_quantity,
|
||||
peer_filter=EXCLUDED.peer_filter,
|
||||
name_requested=EXCLUDED.name_requested,
|
||||
username_requested=EXCLUDED.username_requested,
|
||||
photo_requested=EXCLUDED.photo_requested,
|
||||
expires_at=EXCLUDED.expires_at`,
|
||||
button.WebAppReqID, button.BotUserID, button.UserID, button.ButtonID, button.Text, button.PeerType, button.MaxQuantity, button.CreatedAt, button.ExpiresAt)
|
||||
button.WebAppReqID, button.BotUserID, button.UserID, button.ButtonID, button.Text,
|
||||
button.PeerType, button.MaxQuantity, peerFilter, button.NameRequested,
|
||||
button.UsernameRequested, button.PhotoRequested, button.CreatedAt, button.ExpiresAt)
|
||||
if err != nil {
|
||||
return fmt.Errorf("save requested webview button: %w", err)
|
||||
}
|
||||
|
|
@ -435,18 +448,28 @@ ON CONFLICT (webapp_req_id) DO UPDATE SET
|
|||
func (s *BotStore) GetRequestedWebViewButton(ctx context.Context, botUserID, userID int64, webAppReqID string) (domain.BotRequestedWebViewButton, bool, error) {
|
||||
_, _ = s.db.Exec(ctx, `DELETE FROM webview_requested_buttons WHERE expires_at <= now()`)
|
||||
var button domain.BotRequestedWebViewButton
|
||||
var peerFilter []byte
|
||||
err := s.db.QueryRow(ctx, `
|
||||
SELECT webapp_req_id, bot_user_id, user_id, button_id, text, peer_type, max_quantity, created_at, expires_at
|
||||
SELECT webapp_req_id, bot_user_id, user_id, button_id, text, peer_type, max_quantity,
|
||||
peer_filter, name_requested, username_requested, photo_requested, created_at, expires_at
|
||||
FROM webview_requested_buttons
|
||||
WHERE bot_user_id=$1 AND user_id=$2 AND webapp_req_id=$3 AND expires_at > now()`,
|
||||
botUserID, userID, webAppReqID).
|
||||
Scan(&button.WebAppReqID, &button.BotUserID, &button.UserID, &button.ButtonID, &button.Text, &button.PeerType, &button.MaxQuantity, &button.CreatedAt, &button.ExpiresAt)
|
||||
Scan(&button.WebAppReqID, &button.BotUserID, &button.UserID, &button.ButtonID,
|
||||
&button.Text, &button.PeerType, &button.MaxQuantity, &peerFilter,
|
||||
&button.NameRequested, &button.UsernameRequested, &button.PhotoRequested,
|
||||
&button.CreatedAt, &button.ExpiresAt)
|
||||
if err != nil {
|
||||
if errors.Is(err, pgx.ErrNoRows) {
|
||||
return domain.BotRequestedWebViewButton{}, false, nil
|
||||
}
|
||||
return domain.BotRequestedWebViewButton{}, false, fmt.Errorf("get requested webview button: %w", err)
|
||||
}
|
||||
if string(peerFilter) != "{}" && string(peerFilter) != "null" {
|
||||
if err := json.Unmarshal(peerFilter, &button.PeerFilter); err != nil {
|
||||
return domain.BotRequestedWebViewButton{}, false, fmt.Errorf("decode requested webview button filter: %w", err)
|
||||
}
|
||||
}
|
||||
return button, true, nil
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -20,17 +20,320 @@ func NewBotAPIUpdateStore(db sqlcgen.DBTX) *BotAPIUpdateStore {
|
|||
return &BotAPIUpdateStore{db: db}
|
||||
}
|
||||
|
||||
func (s *BotAPIUpdateStore) SetBotAPIWebhook(ctx context.Context, config domain.BotAPIWebhook, dropPending bool) error {
|
||||
if config.BotUserID <= 0 || config.URL == "" || config.MaxConnections < 1 || config.MaxConnections > 100 {
|
||||
return fmt.Errorf("invalid bot api webhook")
|
||||
}
|
||||
var allowed []string
|
||||
if len(config.AllowedUpdates) > 0 {
|
||||
allowed = make([]string, 0, len(config.AllowedUpdates))
|
||||
for _, kind := range config.AllowedUpdates {
|
||||
if kind != "" {
|
||||
allowed = append(allowed, string(kind))
|
||||
}
|
||||
}
|
||||
}
|
||||
if _, err := s.db.Exec(ctx, `
|
||||
WITH policy AS (
|
||||
SELECT CASE WHEN $6::boolean THEN $5::text[]
|
||||
ELSE (SELECT allowed_updates FROM bot_api_update_states WHERE bot_user_id = $1)
|
||||
END AS allowed_updates
|
||||
), configured AS (
|
||||
INSERT INTO bot_api_webhooks (
|
||||
bot_user_id, url, secret_token, max_connections, allowed_updates,
|
||||
failure_count, last_error_date, last_error_message, next_attempt_at,
|
||||
delivery_owner, delivery_expires_at, updated_at
|
||||
)
|
||||
SELECT $1, $2, $3, $4, allowed_updates, 0, 0, '', now(), '', NULL, now()
|
||||
FROM policy
|
||||
ON CONFLICT (bot_user_id) DO UPDATE
|
||||
SET url = EXCLUDED.url,
|
||||
secret_token = EXCLUDED.secret_token,
|
||||
max_connections = EXCLUDED.max_connections,
|
||||
allowed_updates = EXCLUDED.allowed_updates,
|
||||
failure_count = 0,
|
||||
last_error_date = 0,
|
||||
last_error_message = '',
|
||||
next_attempt_at = now(),
|
||||
delivery_owner = '',
|
||||
delivery_expires_at = NULL,
|
||||
updated_at = now()
|
||||
RETURNING bot_user_id
|
||||
), boundary AS (
|
||||
SELECT CASE WHEN $7::boolean THEN COALESCE(MAX(id), 0) ELSE 0 END AS confirmed_update_id
|
||||
FROM bot_api_updates
|
||||
WHERE bot_user_id = $1
|
||||
)
|
||||
INSERT INTO bot_api_update_states (
|
||||
bot_user_id, confirmed_update_id, allowed_updates, cursor_initialized
|
||||
)
|
||||
SELECT $1, confirmed_update_id, policy.allowed_updates, $7::boolean
|
||||
FROM boundary, configured, policy
|
||||
ON CONFLICT (bot_user_id) DO UPDATE
|
||||
SET confirmed_update_id = CASE WHEN $7::boolean
|
||||
THEN GREATEST(bot_api_update_states.confirmed_update_id, EXCLUDED.confirmed_update_id)
|
||||
ELSE bot_api_update_states.confirmed_update_id
|
||||
END,
|
||||
allowed_updates = EXCLUDED.allowed_updates,
|
||||
cursor_initialized = bot_api_update_states.cursor_initialized OR EXCLUDED.cursor_initialized,
|
||||
updated_at = now()
|
||||
`, config.BotUserID, config.URL, config.SecretToken, config.MaxConnections, allowed,
|
||||
config.AllowedUpdatesSet, dropPending); err != nil {
|
||||
return fmt.Errorf("set bot api webhook: %w", err)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (s *BotAPIUpdateStore) DeleteBotAPIWebhook(ctx context.Context, botUserID int64, dropPending bool) error {
|
||||
if botUserID <= 0 {
|
||||
return nil
|
||||
}
|
||||
if _, err := s.db.Exec(ctx, `
|
||||
WITH deleted AS (
|
||||
DELETE FROM bot_api_webhooks WHERE bot_user_id = $1 RETURNING bot_user_id
|
||||
), boundary AS (
|
||||
SELECT CASE WHEN $2::boolean THEN COALESCE(MAX(id), 0) ELSE 0 END AS confirmed_update_id
|
||||
FROM bot_api_updates
|
||||
WHERE bot_user_id = $1
|
||||
)
|
||||
INSERT INTO bot_api_update_states (bot_user_id, confirmed_update_id, cursor_initialized)
|
||||
SELECT $1, confirmed_update_id, $2::boolean
|
||||
FROM boundary
|
||||
ON CONFLICT (bot_user_id) DO UPDATE
|
||||
SET confirmed_update_id = CASE WHEN $2::boolean
|
||||
THEN GREATEST(bot_api_update_states.confirmed_update_id, EXCLUDED.confirmed_update_id)
|
||||
ELSE bot_api_update_states.confirmed_update_id
|
||||
END,
|
||||
cursor_initialized = bot_api_update_states.cursor_initialized OR EXCLUDED.cursor_initialized,
|
||||
updated_at = now()
|
||||
`, botUserID, dropPending); err != nil {
|
||||
return fmt.Errorf("delete bot api webhook: %w", err)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (s *BotAPIUpdateStore) BotAPIWebhook(ctx context.Context, botUserID int64) (domain.BotAPIWebhook, bool, error) {
|
||||
config, err := scanBotAPIWebhook(s.db.QueryRow(ctx, `
|
||||
SELECT bot_user_id, url, secret_token, max_connections, allowed_updates,
|
||||
failure_count, last_error_date, last_error_message, next_attempt_at
|
||||
FROM bot_api_webhooks
|
||||
WHERE bot_user_id = $1
|
||||
`, botUserID))
|
||||
if err == pgx.ErrNoRows {
|
||||
return domain.BotAPIWebhook{}, false, nil
|
||||
}
|
||||
if err != nil {
|
||||
return domain.BotAPIWebhook{}, false, fmt.Errorf("get bot api webhook: %w", err)
|
||||
}
|
||||
return config, true, nil
|
||||
}
|
||||
|
||||
func (s *BotAPIUpdateStore) ListDueBotAPIWebhooks(ctx context.Context, limit int) ([]domain.BotAPIWebhook, error) {
|
||||
if limit <= 0 || limit > 1000 {
|
||||
limit = 100
|
||||
}
|
||||
rows, err := s.db.Query(ctx, `
|
||||
SELECT bot_user_id, url, secret_token, max_connections, allowed_updates,
|
||||
failure_count, last_error_date, last_error_message, next_attempt_at
|
||||
FROM bot_api_webhooks
|
||||
WHERE next_attempt_at <= now()
|
||||
AND (delivery_owner = '' OR delivery_expires_at <= now())
|
||||
ORDER BY next_attempt_at, bot_user_id
|
||||
LIMIT $1
|
||||
`, limit)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("list due bot api webhooks: %w", err)
|
||||
}
|
||||
defer rows.Close()
|
||||
out := make([]domain.BotAPIWebhook, 0, limit)
|
||||
for rows.Next() {
|
||||
config, err := scanBotAPIWebhook(rows)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
out = append(out, config)
|
||||
}
|
||||
if err := rows.Err(); err != nil {
|
||||
return nil, fmt.Errorf("list due bot api webhook rows: %w", err)
|
||||
}
|
||||
return out, nil
|
||||
}
|
||||
|
||||
func (s *BotAPIUpdateStore) AcquireBotAPIWebhookLease(ctx context.Context, botUserID int64, owner string, ttl time.Duration) (bool, error) {
|
||||
if botUserID <= 0 || owner == "" || ttl <= 0 {
|
||||
return false, fmt.Errorf("invalid bot api webhook lease")
|
||||
}
|
||||
var acquiredOwner string
|
||||
err := s.db.QueryRow(ctx, `
|
||||
UPDATE bot_api_webhooks
|
||||
SET delivery_owner = $2,
|
||||
delivery_expires_at = now() + make_interval(secs => $3),
|
||||
updated_at = now()
|
||||
WHERE bot_user_id = $1
|
||||
AND (delivery_owner = $2 OR delivery_owner = '' OR delivery_expires_at <= now())
|
||||
RETURNING delivery_owner
|
||||
`, botUserID, owner, int64((ttl+time.Second-1)/time.Second)).Scan(&acquiredOwner)
|
||||
if err == pgx.ErrNoRows {
|
||||
return false, nil
|
||||
}
|
||||
if err != nil {
|
||||
return false, fmt.Errorf("acquire bot api webhook lease: %w", err)
|
||||
}
|
||||
return acquiredOwner == owner, nil
|
||||
}
|
||||
|
||||
func (s *BotAPIUpdateStore) ReleaseBotAPIWebhookLease(ctx context.Context, botUserID int64, owner string) error {
|
||||
if botUserID <= 0 || owner == "" {
|
||||
return nil
|
||||
}
|
||||
if _, err := s.db.Exec(ctx, `
|
||||
UPDATE bot_api_webhooks
|
||||
SET delivery_owner = '', delivery_expires_at = NULL, updated_at = now()
|
||||
WHERE bot_user_id = $1 AND delivery_owner = $2
|
||||
`, botUserID, owner); err != nil {
|
||||
return fmt.Errorf("release bot api webhook lease: %w", err)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (s *BotAPIUpdateStore) RecordBotAPIWebhookFailure(ctx context.Context, botUserID int64, owner string, nextAttempt time.Time, message string) error {
|
||||
if len(message) > 512 {
|
||||
message = message[:512]
|
||||
}
|
||||
if _, err := s.db.Exec(ctx, `
|
||||
UPDATE bot_api_webhooks
|
||||
SET failure_count = failure_count + 1,
|
||||
last_error_date = EXTRACT(EPOCH FROM now())::integer,
|
||||
last_error_message = $3,
|
||||
next_attempt_at = $4,
|
||||
delivery_owner = '', delivery_expires_at = NULL, updated_at = now()
|
||||
WHERE bot_user_id = $1 AND delivery_owner = $2
|
||||
`, botUserID, owner, message, nextAttempt); err != nil {
|
||||
return fmt.Errorf("record bot api webhook failure: %w", err)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (s *BotAPIUpdateStore) RecordBotAPIWebhookSuccess(ctx context.Context, botUserID int64, owner string, nextAttempt time.Time) error {
|
||||
if _, err := s.db.Exec(ctx, `
|
||||
UPDATE bot_api_webhooks
|
||||
SET failure_count = 0, last_error_date = 0, last_error_message = '',
|
||||
next_attempt_at = $3, delivery_owner = '', delivery_expires_at = NULL, updated_at = now()
|
||||
WHERE bot_user_id = $1 AND delivery_owner = $2
|
||||
`, botUserID, owner, nextAttempt); err != nil {
|
||||
return fmt.Errorf("record bot api webhook success: %w", err)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func scanBotAPIWebhook(row botAPIUpdateScanner) (domain.BotAPIWebhook, error) {
|
||||
var config domain.BotAPIWebhook
|
||||
var allowed []string
|
||||
if err := row.Scan(&config.BotUserID, &config.URL, &config.SecretToken, &config.MaxConnections, &allowed,
|
||||
&config.FailureCount, &config.LastErrorDate, &config.LastErrorMessage, &config.NextAttemptAt); err != nil {
|
||||
return domain.BotAPIWebhook{}, err
|
||||
}
|
||||
if allowed != nil {
|
||||
config.AllowedUpdates = make([]domain.BotAPIUpdateKind, 0, len(allowed))
|
||||
for _, kind := range allowed {
|
||||
config.AllowedUpdates = append(config.AllowedUpdates, domain.BotAPIUpdateKind(kind))
|
||||
}
|
||||
}
|
||||
return config, nil
|
||||
}
|
||||
|
||||
func (s *BotAPIUpdateStore) AcquireBotAPIPollLease(ctx context.Context, botUserID int64, owner string, ttl time.Duration) (bool, error) {
|
||||
if botUserID <= 0 || owner == "" || ttl <= 0 {
|
||||
return false, fmt.Errorf("invalid bot api poll lease")
|
||||
}
|
||||
var acquiredOwner string
|
||||
err := s.db.QueryRow(ctx, `
|
||||
INSERT INTO bot_api_update_states (
|
||||
bot_user_id, confirmed_update_id, poll_owner, poll_expires_at
|
||||
) VALUES ($1, 0, $2, now() + make_interval(secs => $3))
|
||||
ON CONFLICT (bot_user_id) DO UPDATE
|
||||
SET poll_owner = EXCLUDED.poll_owner,
|
||||
poll_expires_at = EXCLUDED.poll_expires_at,
|
||||
updated_at = now()
|
||||
WHERE bot_api_update_states.poll_owner = EXCLUDED.poll_owner
|
||||
OR bot_api_update_states.poll_expires_at IS NULL
|
||||
OR bot_api_update_states.poll_expires_at <= now()
|
||||
RETURNING poll_owner
|
||||
`, botUserID, owner, int64((ttl+time.Second-1)/time.Second)).Scan(&acquiredOwner)
|
||||
if err == pgx.ErrNoRows {
|
||||
return false, nil
|
||||
}
|
||||
if err != nil {
|
||||
return false, fmt.Errorf("acquire bot api poll lease: %w", err)
|
||||
}
|
||||
return acquiredOwner == owner, nil
|
||||
}
|
||||
|
||||
func (s *BotAPIUpdateStore) ReleaseBotAPIPollLease(ctx context.Context, botUserID int64, owner string) error {
|
||||
if botUserID <= 0 || owner == "" {
|
||||
return nil
|
||||
}
|
||||
if _, err := s.db.Exec(ctx, `
|
||||
UPDATE bot_api_update_states
|
||||
SET poll_owner = '', poll_expires_at = NULL, updated_at = now()
|
||||
WHERE bot_user_id = $1 AND poll_owner = $2
|
||||
`, botUserID, owner); err != nil {
|
||||
return fmt.Errorf("release bot api poll lease: %w", err)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (s *BotAPIUpdateStore) EnqueueBotAPIUpdate(ctx context.Context, req domain.EnqueueBotAPIUpdateRequest) (domain.BotAPIUpdate, bool, error) {
|
||||
if err := validateBotAPIUpdateRequest(req); err != nil {
|
||||
return domain.BotAPIUpdate{}, false, err
|
||||
}
|
||||
var callbackQueryID, callbackUserID, callbackChatInstance int64
|
||||
var callbackInlineDCID, callbackInlineMessageID int
|
||||
var callbackInlineOwnerID, callbackInlineAccessHash int64
|
||||
var callbackData []byte
|
||||
if req.Callback != nil {
|
||||
callbackQueryID = req.Callback.ID
|
||||
callbackUserID = req.Callback.UserID
|
||||
callbackChatInstance = req.Callback.ChatInstance
|
||||
callbackData = req.Callback.Data
|
||||
if req.Callback.InlineMessage != nil {
|
||||
callbackInlineDCID = req.Callback.InlineMessage.DCID
|
||||
callbackInlineOwnerID = req.Callback.InlineMessage.OwnerID
|
||||
callbackInlineMessageID = req.Callback.InlineMessage.ID
|
||||
callbackInlineAccessHash = req.Callback.InlineMessage.AccessHash
|
||||
}
|
||||
}
|
||||
row, err := s.scanBotAPIUpdate(s.db.QueryRow(ctx, `
|
||||
INSERT INTO bot_api_updates (
|
||||
bot_user_id, update_kind, peer_type, peer_id, message_id, source_pts, date
|
||||
) VALUES ($1, $2, $3, $4, $5, $6, $7)
|
||||
ON CONFLICT (bot_user_id, update_kind, peer_type, peer_id, message_id, source_pts) DO NOTHING
|
||||
RETURNING id, bot_user_id, update_kind, peer_type, peer_id, message_id, source_pts, date
|
||||
`, req.BotUserID, string(req.Kind), string(req.Peer.Type), req.Peer.ID, req.MessageID, req.SourcePts, req.Date))
|
||||
WITH inserted AS (
|
||||
INSERT INTO bot_api_updates (
|
||||
bot_user_id, update_kind, peer_type, peer_id, message_id, source_pts, date,
|
||||
callback_query_id, callback_user_id, callback_chat_instance, callback_data,
|
||||
callback_inline_dc_id, callback_inline_owner_id, callback_inline_message_id, callback_inline_access_hash
|
||||
) SELECT $1, $2::varchar(32), $3, $4, $5, $6, $7, $8, $9, $10, $11, $12, $13, $14, $15
|
||||
WHERE NOT EXISTS (
|
||||
SELECT 1
|
||||
FROM bot_api_update_states
|
||||
WHERE bot_user_id = $1
|
||||
AND allowed_updates IS NOT NULL
|
||||
AND NOT ($2::text = ANY(allowed_updates))
|
||||
)
|
||||
ON CONFLICT DO NOTHING
|
||||
RETURNING id, bot_user_id, update_kind, peer_type, peer_id, message_id, source_pts, date,
|
||||
callback_query_id, callback_user_id, callback_chat_instance, callback_data,
|
||||
callback_inline_dc_id, callback_inline_owner_id, callback_inline_message_id, callback_inline_access_hash
|
||||
), wake_webhook AS (
|
||||
UPDATE bot_api_webhooks
|
||||
SET next_attempt_at = now(), updated_at = now()
|
||||
WHERE bot_user_id = $1 AND EXISTS (SELECT 1 FROM inserted)
|
||||
RETURNING bot_user_id
|
||||
)
|
||||
SELECT id, bot_user_id, update_kind, peer_type, peer_id, message_id, source_pts, date,
|
||||
callback_query_id, callback_user_id, callback_chat_instance, callback_data,
|
||||
callback_inline_dc_id, callback_inline_owner_id, callback_inline_message_id, callback_inline_access_hash
|
||||
FROM inserted
|
||||
`, req.BotUserID, string(req.Kind), string(req.Peer.Type), req.Peer.ID, req.MessageID, req.SourcePts, req.Date,
|
||||
callbackQueryID, callbackUserID, callbackChatInstance, callbackData,
|
||||
callbackInlineDCID, callbackInlineOwnerID, callbackInlineMessageID, callbackInlineAccessHash))
|
||||
if err == nil {
|
||||
return row, true, nil
|
||||
}
|
||||
|
|
@ -38,21 +341,69 @@ RETURNING id, bot_user_id, update_kind, peer_type, peer_id, message_id, source_p
|
|||
return domain.BotAPIUpdate{}, false, fmt.Errorf("insert bot api update: %w", err)
|
||||
}
|
||||
row, err = s.scanBotAPIUpdate(s.db.QueryRow(ctx, `
|
||||
SELECT id, bot_user_id, update_kind, peer_type, peer_id, message_id, source_pts, date
|
||||
SELECT id, bot_user_id, update_kind, peer_type, peer_id, message_id, source_pts, date,
|
||||
callback_query_id, callback_user_id, callback_chat_instance, callback_data,
|
||||
callback_inline_dc_id, callback_inline_owner_id, callback_inline_message_id, callback_inline_access_hash
|
||||
FROM bot_api_updates
|
||||
WHERE bot_user_id = $1
|
||||
AND update_kind = $2
|
||||
AND peer_type = $3
|
||||
AND peer_id = $4
|
||||
AND message_id = $5
|
||||
AND source_pts = $6
|
||||
`, req.BotUserID, string(req.Kind), string(req.Peer.Type), req.Peer.ID, req.MessageID, req.SourcePts))
|
||||
AND (
|
||||
(update_kind = 'callback_query' AND callback_query_id = $7)
|
||||
OR
|
||||
(update_kind <> 'callback_query' AND peer_type = $3 AND peer_id = $4 AND message_id = $5 AND source_pts = $6)
|
||||
)
|
||||
`, req.BotUserID, string(req.Kind), string(req.Peer.Type), req.Peer.ID, req.MessageID, req.SourcePts, callbackQueryID))
|
||||
if err != nil {
|
||||
if err == pgx.ErrNoRows {
|
||||
return domain.BotAPIUpdate{}, false, nil
|
||||
}
|
||||
return domain.BotAPIUpdate{}, false, fmt.Errorf("select existing bot api update: %w", err)
|
||||
}
|
||||
return row, false, nil
|
||||
}
|
||||
|
||||
func (s *BotAPIUpdateStore) ListTailBotAPIUpdates(ctx context.Context, botUserID int64, tail, limit int) ([]domain.BotAPIUpdate, error) {
|
||||
if botUserID == 0 || tail <= 0 {
|
||||
return nil, nil
|
||||
}
|
||||
if limit <= 0 || limit > 100 {
|
||||
limit = 100
|
||||
}
|
||||
rows, err := s.db.Query(ctx, `
|
||||
SELECT id, bot_user_id, update_kind, peer_type, peer_id, message_id, source_pts, date,
|
||||
callback_query_id, callback_user_id, callback_chat_instance, callback_data,
|
||||
callback_inline_dc_id, callback_inline_owner_id, callback_inline_message_id, callback_inline_access_hash
|
||||
FROM (
|
||||
SELECT id, bot_user_id, update_kind, peer_type, peer_id, message_id, source_pts, date,
|
||||
callback_query_id, callback_user_id, callback_chat_instance, callback_data,
|
||||
callback_inline_dc_id, callback_inline_owner_id, callback_inline_message_id, callback_inline_access_hash
|
||||
FROM bot_api_updates
|
||||
WHERE bot_user_id = $1
|
||||
AND id > COALESCE((SELECT confirmed_update_id FROM bot_api_update_states WHERE bot_user_id = $1), 0)
|
||||
ORDER BY id DESC
|
||||
LIMIT $2
|
||||
) AS tail_updates
|
||||
ORDER BY id
|
||||
LIMIT $3
|
||||
`, botUserID, tail, limit)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("list bot api tail updates: %w", err)
|
||||
}
|
||||
defer rows.Close()
|
||||
out := make([]domain.BotAPIUpdate, 0, limit)
|
||||
for rows.Next() {
|
||||
item, err := scanBotAPIUpdateRows(rows)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
out = append(out, item)
|
||||
}
|
||||
if err := rows.Err(); err != nil {
|
||||
return nil, fmt.Errorf("list bot api tail update rows: %w", err)
|
||||
}
|
||||
return out, nil
|
||||
}
|
||||
|
||||
func (s *BotAPIUpdateStore) ListBotAPIUpdates(ctx context.Context, botUserID, fromUpdateID int64, limit int) ([]domain.BotAPIUpdate, error) {
|
||||
if botUserID == 0 {
|
||||
return nil, nil
|
||||
|
|
@ -64,7 +415,9 @@ func (s *BotAPIUpdateStore) ListBotAPIUpdates(ctx context.Context, botUserID, fr
|
|||
limit = 100
|
||||
}
|
||||
rows, err := s.db.Query(ctx, `
|
||||
SELECT id, bot_user_id, update_kind, peer_type, peer_id, message_id, source_pts, date
|
||||
SELECT id, bot_user_id, update_kind, peer_type, peer_id, message_id, source_pts, date,
|
||||
callback_query_id, callback_user_id, callback_chat_instance, callback_data,
|
||||
callback_inline_dc_id, callback_inline_owner_id, callback_inline_message_id, callback_inline_access_hash
|
||||
FROM bot_api_updates
|
||||
WHERE bot_user_id = $1 AND id >= $2
|
||||
ORDER BY id
|
||||
|
|
@ -93,23 +446,98 @@ func (s *BotAPIUpdateStore) ConfirmBotAPIUpdates(ctx context.Context, botUserID,
|
|||
return nil
|
||||
}
|
||||
if _, err := s.db.Exec(ctx, `
|
||||
INSERT INTO bot_api_update_states (bot_user_id, confirmed_update_id)
|
||||
VALUES ($1, $2)
|
||||
WITH bounded AS (
|
||||
SELECT COALESCE(MAX(id), 0) AS max_update_id,
|
||||
LEAST($2::bigint, COALESCE(MAX(id), 0)) AS confirmed_update_id
|
||||
FROM bot_api_updates
|
||||
WHERE bot_user_id = $1
|
||||
)
|
||||
INSERT INTO bot_api_update_states (bot_user_id, confirmed_update_id, cursor_initialized)
|
||||
SELECT $1, confirmed_update_id, true
|
||||
FROM bounded
|
||||
ON CONFLICT (bot_user_id) DO UPDATE
|
||||
SET confirmed_update_id = GREATEST(bot_api_update_states.confirmed_update_id, EXCLUDED.confirmed_update_id),
|
||||
SET confirmed_update_id = GREATEST(
|
||||
bot_api_update_states.confirmed_update_id,
|
||||
CASE
|
||||
WHEN $2::bigint > (SELECT max_update_id FROM bounded)
|
||||
AND bot_api_update_states.cursor_initialized
|
||||
THEN bot_api_update_states.confirmed_update_id
|
||||
ELSE EXCLUDED.confirmed_update_id
|
||||
END
|
||||
),
|
||||
cursor_initialized = true,
|
||||
updated_at = now()
|
||||
WHERE bot_api_update_states.confirmed_update_id < EXCLUDED.confirmed_update_id
|
||||
`, botUserID, confirmedUpdateID); err != nil {
|
||||
return fmt.Errorf("confirm bot api updates: %w", err)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (s *BotAPIUpdateStore) SetBotAPIAllowedUpdates(ctx context.Context, botUserID int64, allowed []domain.BotAPIUpdateKind) error {
|
||||
if botUserID == 0 {
|
||||
return nil
|
||||
}
|
||||
var values []string
|
||||
if len(allowed) > 0 {
|
||||
values = make([]string, 0, len(allowed))
|
||||
for _, kind := range allowed {
|
||||
if kind != "" {
|
||||
values = append(values, string(kind))
|
||||
}
|
||||
}
|
||||
}
|
||||
if _, err := s.db.Exec(ctx, `
|
||||
INSERT INTO bot_api_update_states (bot_user_id, confirmed_update_id, allowed_updates)
|
||||
VALUES ($1, 0, $2::text[])
|
||||
ON CONFLICT (bot_user_id) DO UPDATE
|
||||
SET allowed_updates = EXCLUDED.allowed_updates,
|
||||
updated_at = now()
|
||||
`, botUserID, values); err != nil {
|
||||
return fmt.Errorf("set bot api allowed updates: %w", err)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (s *BotAPIUpdateStore) DropPendingBotAPIUpdates(ctx context.Context, botUserID int64) error {
|
||||
if botUserID == 0 {
|
||||
return nil
|
||||
}
|
||||
if _, err := s.db.Exec(ctx, `
|
||||
INSERT INTO bot_api_update_states (bot_user_id, confirmed_update_id, cursor_initialized)
|
||||
SELECT $1, COALESCE(MAX(id), 0), true
|
||||
FROM bot_api_updates
|
||||
WHERE bot_user_id = $1
|
||||
ON CONFLICT (bot_user_id) DO UPDATE
|
||||
SET confirmed_update_id = GREATEST(bot_api_update_states.confirmed_update_id, EXCLUDED.confirmed_update_id),
|
||||
cursor_initialized = true,
|
||||
updated_at = now()
|
||||
`, botUserID); err != nil {
|
||||
return fmt.Errorf("drop pending bot api updates: %w", err)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (s *BotAPIUpdateStore) PendingBotAPIUpdateCount(ctx context.Context, botUserID int64) (int, error) {
|
||||
if botUserID == 0 {
|
||||
return 0, nil
|
||||
}
|
||||
var count int
|
||||
if err := s.db.QueryRow(ctx, `
|
||||
SELECT COUNT(*)
|
||||
FROM bot_api_updates
|
||||
WHERE bot_user_id = $1
|
||||
AND id > COALESCE((SELECT confirmed_update_id FROM bot_api_update_states WHERE bot_user_id = $1), 0)
|
||||
`, botUserID).Scan(&count); err != nil {
|
||||
return 0, fmt.Errorf("count pending bot api updates: %w", err)
|
||||
}
|
||||
return count, nil
|
||||
}
|
||||
|
||||
// DeleteDeliveredOrExpired 回收 Bot API 投递队列的死行(性能审计 H1):
|
||||
// 1. 已确认(id <= bot_api_update_states.confirmed_update_id)且入队超过 confirmedGrace 的行——
|
||||
// 官方 Bot API 语义下确认即弃,getUpdates 的 fromID 恒 > confirmed,删除不影响任何读路径;
|
||||
// 宽限仅防御 offset 回拨调试场景。
|
||||
// 2. 按消息 date 超过 maxAge 的行(无论确认与否)——对齐官方「updates 服务器最多保留 24 小时」
|
||||
// 2. 按队列 created_at 超过 maxAge 的行(无论确认与否)——对齐官方「updates 服务器最多保留 24 小时」
|
||||
// 语义,同时封顶 MTProto-only bot(从不调 getUpdates、无 state 行)成员身份带来的无界增长。
|
||||
//
|
||||
// 与 user_update_events 的「永久保留」约束无关:那是 TDesktop 账号级 differenceTooLong 缺陷所迫,
|
||||
|
|
@ -139,15 +567,15 @@ WHERE id IN (
|
|||
total += int(tag.RowsAffected())
|
||||
}
|
||||
if maxAge > 0 {
|
||||
cutoff := time.Now().Add(-maxAge).Unix()
|
||||
// 走 bot_api_updates_retention_idx(date, id)。
|
||||
cutoff := time.Now().Add(-maxAge)
|
||||
// 走 bot_api_updates_created_retention_idx(created_at, id)。
|
||||
tag, err := s.db.Exec(ctx, `
|
||||
DELETE FROM bot_api_updates
|
||||
WHERE id IN (
|
||||
SELECT id
|
||||
FROM bot_api_updates
|
||||
WHERE date < $1
|
||||
ORDER BY date, id
|
||||
WHERE created_at < $1
|
||||
ORDER BY created_at, id
|
||||
LIMIT $2
|
||||
)`, cutoff, limit)
|
||||
if err != nil {
|
||||
|
|
@ -187,28 +615,69 @@ type botAPIUpdateScanner interface {
|
|||
func scanBotAPIUpdateRows(row botAPIUpdateScanner) (domain.BotAPIUpdate, error) {
|
||||
var item domain.BotAPIUpdate
|
||||
var kind, peerType string
|
||||
if err := row.Scan(&item.ID, &item.BotUserID, &kind, &peerType, &item.Peer.ID, &item.MessageID, &item.SourcePts, &item.Date); err != nil {
|
||||
var callbackQueryID, callbackUserID, callbackChatInstance int64
|
||||
var callbackInlineDCID, callbackInlineMessageID int
|
||||
var callbackInlineOwnerID, callbackInlineAccessHash int64
|
||||
var callbackData []byte
|
||||
if err := row.Scan(&item.ID, &item.BotUserID, &kind, &peerType, &item.Peer.ID, &item.MessageID, &item.SourcePts, &item.Date,
|
||||
&callbackQueryID, &callbackUserID, &callbackChatInstance, &callbackData,
|
||||
&callbackInlineDCID, &callbackInlineOwnerID, &callbackInlineMessageID, &callbackInlineAccessHash); err != nil {
|
||||
return domain.BotAPIUpdate{}, err
|
||||
}
|
||||
item.Kind = domain.BotAPIUpdateKind(kind)
|
||||
item.Peer.Type = domain.PeerType(peerType)
|
||||
if item.Kind == domain.BotAPIUpdateCallbackQuery {
|
||||
item.Callback = &domain.BotCallbackQuery{
|
||||
ID: callbackQueryID,
|
||||
BotUserID: item.BotUserID,
|
||||
UserID: callbackUserID,
|
||||
Peer: item.Peer,
|
||||
MessageID: item.MessageID,
|
||||
ChatInstance: callbackChatInstance,
|
||||
Data: append([]byte(nil), callbackData...),
|
||||
}
|
||||
if callbackInlineMessageID > 0 {
|
||||
item.Callback.InlineMessage = &domain.BotInlineMessageID{DCID: callbackInlineDCID, OwnerID: callbackInlineOwnerID, ID: callbackInlineMessageID, AccessHash: callbackInlineAccessHash}
|
||||
}
|
||||
}
|
||||
return item, nil
|
||||
}
|
||||
|
||||
func validateBotAPIUpdateRequest(req domain.EnqueueBotAPIUpdateRequest) error {
|
||||
if req.BotUserID == 0 || req.MessageID <= 0 {
|
||||
if req.BotUserID == 0 {
|
||||
return fmt.Errorf("invalid bot api update")
|
||||
}
|
||||
if req.Kind != domain.BotAPIUpdateMessage && req.Kind != domain.BotAPIUpdateEditedMessage {
|
||||
if req.Kind != domain.BotAPIUpdateMessage && req.Kind != domain.BotAPIUpdateEditedMessage && req.Kind != domain.BotAPIUpdateCallbackQuery {
|
||||
return fmt.Errorf("invalid bot api update kind %q", req.Kind)
|
||||
}
|
||||
switch req.Peer.Type {
|
||||
case domain.PeerTypeUser, domain.PeerTypeChannel:
|
||||
if req.Peer.ID <= 0 {
|
||||
if req.Peer.ID <= 0 || req.MessageID <= 0 {
|
||||
return fmt.Errorf("invalid bot api update peer")
|
||||
}
|
||||
case "":
|
||||
if req.Kind != domain.BotAPIUpdateCallbackQuery || req.Peer.ID != 0 || req.MessageID != 0 {
|
||||
return fmt.Errorf("invalid bot api update peer")
|
||||
}
|
||||
default:
|
||||
return fmt.Errorf("invalid bot api update peer type %q", req.Peer.Type)
|
||||
}
|
||||
if req.Kind == domain.BotAPIUpdateCallbackQuery {
|
||||
cb := req.Callback
|
||||
if cb == nil || cb.ID == 0 || cb.BotUserID != req.BotUserID || cb.UserID <= 0 ||
|
||||
cb.Peer != req.Peer || cb.MessageID != req.MessageID || cb.ChatInstance == 0 ||
|
||||
len(cb.Data) > domain.MaxCallbackDataLen || req.SourcePts != 0 {
|
||||
return fmt.Errorf("invalid bot api callback query")
|
||||
}
|
||||
inline := cb.InlineMessage
|
||||
if req.MessageID == 0 && (inline == nil || inline.DCID <= 0 || inline.OwnerID <= 0 || inline.ID <= 0 || inline.AccessHash == 0) {
|
||||
return fmt.Errorf("invalid bot api inline callback query")
|
||||
}
|
||||
if req.MessageID > 0 && inline != nil {
|
||||
return fmt.Errorf("ambiguous bot api callback query")
|
||||
}
|
||||
} else if req.Callback != nil {
|
||||
return fmt.Errorf("unexpected bot api callback query")
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,6 +1,7 @@
|
|||
package postgres
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"context"
|
||||
"testing"
|
||||
"time"
|
||||
|
|
@ -8,10 +9,292 @@ import (
|
|||
"telesrv/internal/domain"
|
||||
)
|
||||
|
||||
func TestBotAPICallbackQueryQueueRoundTrip(t *testing.T) {
|
||||
pool := testPool(t)
|
||||
ctx := context.Background()
|
||||
suffix := randomSuffix(t)
|
||||
users := NewUserStore(pool)
|
||||
bot, err := users.Create(ctx, domain.User{
|
||||
AccessHash: 921, Phone: "+1921" + suffix + "01", FirstName: "CallbackQueueBot",
|
||||
})
|
||||
if err != nil {
|
||||
t.Fatalf("create bot user: %v", err)
|
||||
}
|
||||
clicker, err := users.Create(ctx, domain.User{
|
||||
AccessHash: 922, Phone: "+1922" + suffix + "02", FirstName: "CallbackClicker",
|
||||
})
|
||||
if err != nil {
|
||||
t.Fatalf("create callback user: %v", err)
|
||||
}
|
||||
if _, err := pool.Exec(ctx, `
|
||||
INSERT INTO bots (bot_user_id, owner_user_id, token_secret)
|
||||
VALUES ($1, $1, 'callback-queue-secret')`, bot.ID); err != nil {
|
||||
t.Fatalf("seed bot: %v", err)
|
||||
}
|
||||
t.Cleanup(func() {
|
||||
_, _ = pool.Exec(ctx, "DELETE FROM bot_api_updates WHERE bot_user_id = $1", bot.ID)
|
||||
_, _ = pool.Exec(ctx, "DELETE FROM bot_api_update_states WHERE bot_user_id = $1", bot.ID)
|
||||
_, _ = pool.Exec(ctx, "DELETE FROM bots WHERE bot_user_id = $1", bot.ID)
|
||||
})
|
||||
|
||||
callback := &domain.BotCallbackQuery{
|
||||
ID: 880011, BotUserID: bot.ID, UserID: clicker.ID,
|
||||
Peer: domain.Peer{Type: domain.PeerTypeUser, ID: clicker.ID}, MessageID: 17,
|
||||
ChatInstance: 990022, Data: []byte{0, 1, 0xff, 'x'},
|
||||
}
|
||||
req := domain.EnqueueBotAPIUpdateRequest{
|
||||
BotUserID: bot.ID, Kind: domain.BotAPIUpdateCallbackQuery,
|
||||
Peer: callback.Peer, MessageID: callback.MessageID, Date: int(time.Now().Unix()), Callback: callback,
|
||||
}
|
||||
store := NewBotAPIUpdateStore(pool)
|
||||
first, created, err := store.EnqueueBotAPIUpdate(ctx, req)
|
||||
if err != nil || !created {
|
||||
t.Fatalf("enqueue callback: row=%+v created=%v err=%v", first, created, err)
|
||||
}
|
||||
again, created, err := store.EnqueueBotAPIUpdate(ctx, req)
|
||||
if err != nil || created || again.ID != first.ID {
|
||||
t.Fatalf("dedupe callback: row=%+v created=%v err=%v", again, created, err)
|
||||
}
|
||||
items, err := store.ListBotAPIUpdates(ctx, bot.ID, first.ID, 100)
|
||||
if err != nil || len(items) != 1 {
|
||||
t.Fatalf("list callback = %+v, %v", items, err)
|
||||
}
|
||||
got := items[0].Callback
|
||||
if got == nil || got.ID != callback.ID || got.BotUserID != bot.ID || got.UserID != clicker.ID ||
|
||||
got.Peer != callback.Peer || got.MessageID != callback.MessageID || got.ChatInstance != callback.ChatInstance ||
|
||||
!bytes.Equal(got.Data, callback.Data) {
|
||||
t.Fatalf("callback round trip = %+v, want %+v", got, callback)
|
||||
}
|
||||
}
|
||||
|
||||
func TestBotAPIInlineCallbackAndWebhookStateRoundTrip(t *testing.T) {
|
||||
pool := testPool(t)
|
||||
ctx := context.Background()
|
||||
suffix := randomSuffix(t)
|
||||
users := NewUserStore(pool)
|
||||
bot, err := users.Create(ctx, domain.User{AccessHash: 931, Phone: "+1931" + suffix + "01", FirstName: "WebhookBot"})
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
clicker, err := users.Create(ctx, domain.User{AccessHash: 932, Phone: "+1932" + suffix + "02", FirstName: "InlineClicker"})
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if _, err := pool.Exec(ctx, `INSERT INTO bots (bot_user_id, owner_user_id, token_secret) VALUES ($1, $1, 'webhook-secret')`, bot.ID); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
t.Cleanup(func() {
|
||||
_, _ = pool.Exec(ctx, "DELETE FROM bot_api_webhooks WHERE bot_user_id = $1", bot.ID)
|
||||
_, _ = pool.Exec(ctx, "DELETE FROM bot_api_updates WHERE bot_user_id = $1", bot.ID)
|
||||
_, _ = pool.Exec(ctx, "DELETE FROM bot_api_update_states WHERE bot_user_id = $1", bot.ID)
|
||||
_, _ = pool.Exec(ctx, "DELETE FROM bots WHERE bot_user_id = $1", bot.ID)
|
||||
})
|
||||
|
||||
s := NewBotAPIUpdateStore(pool)
|
||||
inline := &domain.BotInlineMessageID{DCID: 2, OwnerID: clicker.ID, ID: 17, AccessHash: 445566}
|
||||
callback := &domain.BotCallbackQuery{
|
||||
ID: 9911, BotUserID: bot.ID, UserID: clicker.ID, ChatInstance: 8811,
|
||||
Data: []byte{0, 1, 0xff}, InlineMessage: inline,
|
||||
}
|
||||
row, created, err := s.EnqueueBotAPIUpdate(ctx, domain.EnqueueBotAPIUpdateRequest{
|
||||
BotUserID: bot.ID, Kind: domain.BotAPIUpdateCallbackQuery, Date: int(time.Now().Unix()), Callback: callback,
|
||||
})
|
||||
if err != nil || !created {
|
||||
t.Fatalf("enqueue inline callback row=%#v created=%v err=%v", row, created, err)
|
||||
}
|
||||
items, err := s.ListBotAPIUpdates(ctx, bot.ID, row.ID, 100)
|
||||
if err != nil || len(items) != 1 || items[0].Peer != (domain.Peer{}) || items[0].MessageID != 0 ||
|
||||
items[0].Callback == nil || items[0].Callback.InlineMessage == nil || *items[0].Callback.InlineMessage != *inline ||
|
||||
!bytes.Equal(items[0].Callback.Data, callback.Data) {
|
||||
t.Fatalf("inline callback items=%#v err=%v", items, err)
|
||||
}
|
||||
|
||||
config := domain.BotAPIWebhook{
|
||||
BotUserID: bot.ID, URL: "https://example.test/hook", SecretToken: "safe_secret",
|
||||
MaxConnections: 8, AllowedUpdates: []domain.BotAPIUpdateKind{domain.BotAPIUpdateCallbackQuery}, AllowedUpdatesSet: true,
|
||||
}
|
||||
if err := s.SetBotAPIWebhook(ctx, config, false); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
stored, found, err := s.BotAPIWebhook(ctx, bot.ID)
|
||||
if err != nil || !found || stored.URL != config.URL || stored.SecretToken != config.SecretToken ||
|
||||
stored.MaxConnections != 8 || len(stored.AllowedUpdates) != 1 {
|
||||
t.Fatalf("webhook=%#v found=%v err=%v", stored, found, err)
|
||||
}
|
||||
config.URL = "https://example.test/reconfigured"
|
||||
config.AllowedUpdates = nil
|
||||
config.AllowedUpdatesSet = false
|
||||
if err := s.SetBotAPIWebhook(ctx, config, false); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
stored, found, err = s.BotAPIWebhook(ctx, bot.ID)
|
||||
if err != nil || !found || stored.URL != config.URL || len(stored.AllowedUpdates) != 1 || stored.AllowedUpdates[0] != domain.BotAPIUpdateCallbackQuery {
|
||||
t.Fatalf("preserved webhook=%#v found=%v err=%v", stored, found, err)
|
||||
}
|
||||
if acquired, err := s.AcquireBotAPIWebhookLease(ctx, bot.ID, "one", time.Minute); err != nil || !acquired {
|
||||
t.Fatalf("first lease=%v err=%v", acquired, err)
|
||||
}
|
||||
if acquired, err := s.AcquireBotAPIWebhookLease(ctx, bot.ID, "two", time.Minute); err != nil || acquired {
|
||||
t.Fatalf("second lease=%v err=%v", acquired, err)
|
||||
}
|
||||
if err := s.ReleaseBotAPIWebhookLease(ctx, bot.ID, "stale"); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if acquired, _ := s.AcquireBotAPIWebhookLease(ctx, bot.ID, "two", time.Minute); acquired {
|
||||
t.Fatal("stale webhook release removed active lease")
|
||||
}
|
||||
next := time.Now().Add(time.Hour)
|
||||
if err := s.RecordBotAPIWebhookSuccess(ctx, bot.ID, "one", next); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if due, err := s.ListDueBotAPIWebhooks(ctx, 10); err != nil || len(due) != 0 {
|
||||
t.Fatalf("idle due=%#v err=%v", due, err)
|
||||
}
|
||||
// A newly inserted allowed callback wakes the idle webhook in the same SQL statement.
|
||||
callback2 := *callback
|
||||
callback2.ID++
|
||||
callback2.InlineMessage = &domain.BotInlineMessageID{DCID: 2, OwnerID: clicker.ID, ID: 18, AccessHash: 556677}
|
||||
if _, created, err := s.EnqueueBotAPIUpdate(ctx, domain.EnqueueBotAPIUpdateRequest{
|
||||
BotUserID: bot.ID, Kind: domain.BotAPIUpdateCallbackQuery, Date: int(time.Now().Unix()), Callback: &callback2,
|
||||
}); err != nil || !created {
|
||||
t.Fatalf("enqueue wake created=%v err=%v", created, err)
|
||||
}
|
||||
if due, err := s.ListDueBotAPIWebhooks(ctx, 10); err != nil || len(due) != 1 || due[0].BotUserID != bot.ID {
|
||||
t.Fatalf("woken due=%#v err=%v", due, err)
|
||||
}
|
||||
if err := s.DeleteBotAPIWebhook(ctx, bot.ID, true); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if _, found, err := s.BotAPIWebhook(ctx, bot.ID); err != nil || found {
|
||||
t.Fatalf("webhook after delete found=%v err=%v", found, err)
|
||||
}
|
||||
if pending, err := s.PendingBotAPIUpdateCount(ctx, bot.ID); err != nil || pending != 0 {
|
||||
t.Fatalf("pending after delete/drop=%d err=%v", pending, err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestBotAPIPollLeaseCrossStoreInstance(t *testing.T) {
|
||||
pool := testPool(t)
|
||||
ctx := context.Background()
|
||||
suffix := randomSuffix(t)
|
||||
users := NewUserStore(pool)
|
||||
bot, err := users.Create(ctx, domain.User{AccessHash: 933, Phone: "+1933" + suffix + "01", FirstName: "PollLeaseBot"})
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if _, err := pool.Exec(ctx, `INSERT INTO bots (bot_user_id, owner_user_id, token_secret) VALUES ($1, $1, 'poll-lease-secret')`, bot.ID); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
t.Cleanup(func() {
|
||||
_, _ = pool.Exec(ctx, "DELETE FROM bot_api_update_states WHERE bot_user_id = $1", bot.ID)
|
||||
_, _ = pool.Exec(ctx, "DELETE FROM bots WHERE bot_user_id = $1", bot.ID)
|
||||
})
|
||||
a, b := NewBotAPIUpdateStore(pool), NewBotAPIUpdateStore(pool)
|
||||
if acquired, err := a.AcquireBotAPIPollLease(ctx, bot.ID, "one", time.Minute); err != nil || !acquired {
|
||||
t.Fatalf("first acquire=%v err=%v", acquired, err)
|
||||
}
|
||||
if acquired, err := b.AcquireBotAPIPollLease(ctx, bot.ID, "two", time.Minute); err != nil || acquired {
|
||||
t.Fatalf("cross-instance acquire=%v err=%v", acquired, err)
|
||||
}
|
||||
if err := b.ReleaseBotAPIPollLease(ctx, bot.ID, "stale"); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if acquired, _ := b.AcquireBotAPIPollLease(ctx, bot.ID, "two", time.Minute); acquired {
|
||||
t.Fatal("stale release removed active poll lease")
|
||||
}
|
||||
if err := a.ReleaseBotAPIPollLease(ctx, bot.ID, "one"); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if acquired, err := b.AcquireBotAPIPollLease(ctx, bot.ID, "two", time.Minute); err != nil || !acquired {
|
||||
t.Fatalf("successor acquire=%v err=%v", acquired, err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestBotAPIPollingStateClampFilterTailAndDrop(t *testing.T) {
|
||||
pool := testPool(t)
|
||||
ctx := context.Background()
|
||||
users := NewUserStore(pool)
|
||||
suffix := randomSuffix(t)
|
||||
bot, err := users.Create(ctx, domain.User{
|
||||
AccessHash: 923, Phone: "+1923" + suffix + "01", FirstName: "PollingStateBot",
|
||||
})
|
||||
if err != nil {
|
||||
t.Fatalf("create bot user: %v", err)
|
||||
}
|
||||
if _, err := pool.Exec(ctx, `INSERT INTO bots (bot_user_id, owner_user_id, token_secret) VALUES ($1, $1, 'poll-state-secret')`, bot.ID); err != nil {
|
||||
t.Fatalf("seed bot: %v", err)
|
||||
}
|
||||
t.Cleanup(func() {
|
||||
_, _ = pool.Exec(ctx, "DELETE FROM bot_api_updates WHERE bot_user_id = $1", bot.ID)
|
||||
_, _ = pool.Exec(ctx, "DELETE FROM bot_api_update_states WHERE bot_user_id = $1", bot.ID)
|
||||
_, _ = pool.Exec(ctx, "DELETE FROM bots WHERE bot_user_id = $1", bot.ID)
|
||||
})
|
||||
|
||||
s := NewBotAPIUpdateStore(pool)
|
||||
enqueue := func(kind domain.BotAPIUpdateKind, messageID int) (domain.BotAPIUpdate, bool) {
|
||||
t.Helper()
|
||||
row, created, err := s.EnqueueBotAPIUpdate(ctx, domain.EnqueueBotAPIUpdateRequest{
|
||||
BotUserID: bot.ID, Kind: kind,
|
||||
Peer: domain.Peer{Type: domain.PeerTypeUser, ID: bot.ID + 1},
|
||||
MessageID: messageID, SourcePts: messageID, Date: int(time.Now().Unix()),
|
||||
})
|
||||
if err != nil {
|
||||
t.Fatalf("enqueue %s/%d: %v", kind, messageID, err)
|
||||
}
|
||||
return row, created
|
||||
}
|
||||
for id := 1; id <= 3; id++ {
|
||||
if _, created := enqueue(domain.BotAPIUpdateMessage, id); !created {
|
||||
t.Fatalf("initial message %d was not created", id)
|
||||
}
|
||||
}
|
||||
if err := s.SetBotAPIAllowedUpdates(ctx, bot.ID, []domain.BotAPIUpdateKind{domain.BotAPIUpdateEditedMessage}); err != nil {
|
||||
t.Fatalf("set allowed updates: %v", err)
|
||||
}
|
||||
if row, created := enqueue(domain.BotAPIUpdateMessage, 4); created || row.ID != 0 {
|
||||
t.Fatalf("filtered row=%+v created=%v", row, created)
|
||||
}
|
||||
lastBeforeBaseline, created := enqueue(domain.BotAPIUpdateEditedMessage, 5)
|
||||
if !created {
|
||||
t.Fatal("allowed edit was filtered")
|
||||
}
|
||||
if err := s.ConfirmBotAPIUpdates(ctx, bot.ID, 1<<60); err != nil {
|
||||
t.Fatalf("initialize external cursor: %v", err)
|
||||
}
|
||||
confirmed, found, err := s.ConfirmedBotAPIUpdateID(ctx, bot.ID)
|
||||
if err != nil || !found || confirmed != lastBeforeBaseline.ID {
|
||||
t.Fatalf("baseline confirmed=%d found=%v err=%v want=%d", confirmed, found, err, lastBeforeBaseline.ID)
|
||||
}
|
||||
pendingRow, created := enqueue(domain.BotAPIUpdateEditedMessage, 6)
|
||||
if !created {
|
||||
t.Fatal("post-baseline edit was filtered")
|
||||
}
|
||||
if err := s.ConfirmBotAPIUpdates(ctx, bot.ID, 1<<60); err != nil {
|
||||
t.Fatalf("repeat external cursor: %v", err)
|
||||
}
|
||||
confirmed, _, _ = s.ConfirmedBotAPIUpdateID(ctx, bot.ID)
|
||||
if confirmed != lastBeforeBaseline.ID {
|
||||
t.Fatalf("repeat external cursor advanced to %d, want %d", confirmed, lastBeforeBaseline.ID)
|
||||
}
|
||||
tail, err := s.ListTailBotAPIUpdates(ctx, bot.ID, 1, 100)
|
||||
if err != nil || len(tail) != 1 || tail[0].ID != pendingRow.ID {
|
||||
t.Fatalf("tail=%+v err=%v want=%d", tail, err, pendingRow.ID)
|
||||
}
|
||||
if count, err := s.PendingBotAPIUpdateCount(ctx, bot.ID); err != nil || count != 1 {
|
||||
t.Fatalf("pending count=%d err=%v", count, err)
|
||||
}
|
||||
if err := s.DropPendingBotAPIUpdates(ctx, bot.ID); err != nil {
|
||||
t.Fatalf("drop pending: %v", err)
|
||||
}
|
||||
if count, err := s.PendingBotAPIUpdateCount(ctx, bot.ID); err != nil || count != 0 {
|
||||
t.Fatalf("pending after drop=%d err=%v", count, err)
|
||||
}
|
||||
}
|
||||
|
||||
// TestBotAPIUpdateRetention 锁定 H1 场景矩阵:
|
||||
// - 已确认 + 超宽限 → 删;已确认 + 宽限内 → 留;
|
||||
// - 未确认 + date 超保留期 → 删(含无 state 行的 MTProto-only bot);
|
||||
// - 未确认 + date 在保留期内 → 留;
|
||||
// - 未确认 + created_at 超保留期 → 删(含无 state 行的 MTProto-only bot);
|
||||
// - 未确认 + created_at 在保留期内 → 留;
|
||||
// - 删除后 getUpdates 读路径(fromID > confirmed)不受影响。
|
||||
func TestBotAPIUpdateRetention(t *testing.T) {
|
||||
pool := testPool(t)
|
||||
|
|
@ -47,7 +330,6 @@ ON CONFLICT (bot_user_id) DO NOTHING`, u.ID); err != nil {
|
|||
|
||||
s := NewBotAPIUpdateStore(pool)
|
||||
now := time.Now().Unix()
|
||||
stale := now - int64((48 * time.Hour).Seconds())
|
||||
enqueue := func(botID int64, messageID int, date int64) domain.BotAPIUpdate {
|
||||
t.Helper()
|
||||
row, created, err := s.EnqueueBotAPIUpdate(ctx, domain.EnqueueBotAPIUpdateRequest{
|
||||
|
|
@ -67,7 +349,7 @@ ON CONFLICT (bot_user_id) DO NOTHING`, u.ID); err != nil {
|
|||
confirmedOld := enqueue(confirmedBot, 1, now) // 已确认 + created_at 回拨超宽限 → 删
|
||||
confirmedFresh := enqueue(confirmedBot, 2, now) // 已确认 + 宽限内 → 留
|
||||
unconfirmedFresh := enqueue(confirmedBot, 3, now)
|
||||
expiredNoState := enqueue(mtprotoOnlyBot, 4, stale) // 无 state 行 + date 超保留期 → 删
|
||||
expiredNoState := enqueue(mtprotoOnlyBot, 4, now) // 无 state 行 + created_at 超保留期 → 删
|
||||
freshNoState := enqueue(mtprotoOnlyBot, 5, now)
|
||||
|
||||
if err := s.ConfirmBotAPIUpdates(ctx, confirmedBot, confirmedFresh.ID); err != nil {
|
||||
|
|
@ -77,6 +359,10 @@ ON CONFLICT (bot_user_id) DO NOTHING`, u.ID); err != nil {
|
|||
"UPDATE bot_api_updates SET created_at = now() - interval '1 hour' WHERE id = $1", confirmedOld.ID); err != nil {
|
||||
t.Fatalf("backdate confirmed row: %v", err)
|
||||
}
|
||||
if _, err := pool.Exec(ctx,
|
||||
"UPDATE bot_api_updates SET created_at = now() - interval '48 hours' WHERE id = $1", expiredNoState.ID); err != nil {
|
||||
t.Fatalf("backdate expired row: %v", err)
|
||||
}
|
||||
|
||||
deleted, err := s.DeleteDeliveredOrExpired(ctx, 15*time.Minute, 24*time.Hour, 1000)
|
||||
if err != nil {
|
||||
|
|
@ -85,7 +371,7 @@ ON CONFLICT (bot_user_id) DO NOTHING`, u.ID); err != nil {
|
|||
// 共享测试库可能有其它历史行同被回收,只要求至少删掉本测试的 2 行;
|
||||
// 精确归属由下方 remaining 断言保证。
|
||||
if deleted < 2 {
|
||||
t.Fatalf("deleted = %d, want >= 2 (confirmed+grace expired, date expired)", deleted)
|
||||
t.Fatalf("deleted = %d, want >= 2 (confirmed+grace expired, created_at expired)", deleted)
|
||||
}
|
||||
|
||||
remaining := map[int64]bool{}
|
||||
|
|
|
|||
|
|
@ -550,6 +550,56 @@ func (s *MediaStore) GetPhoto(ctx context.Context, id int64) (domain.Photo, bool
|
|||
return photo, true, nil
|
||||
}
|
||||
|
||||
// GetPhotos resolves a bounded set of immutable photo metadata with one indexed
|
||||
// ANY query. Missing ids are omitted and the result follows first-seen caller order.
|
||||
func (s *MediaStore) GetPhotos(ctx context.Context, ids []int64) ([]domain.Photo, error) {
|
||||
if len(ids) == 0 {
|
||||
return nil, nil
|
||||
}
|
||||
unique := make([]int64, 0, len(ids))
|
||||
seen := make(map[int64]struct{}, len(ids))
|
||||
for _, id := range ids {
|
||||
if id == 0 {
|
||||
continue
|
||||
}
|
||||
if _, ok := seen[id]; ok {
|
||||
continue
|
||||
}
|
||||
seen[id] = struct{}{}
|
||||
unique = append(unique, id)
|
||||
}
|
||||
if len(unique) == 0 {
|
||||
return nil, nil
|
||||
}
|
||||
rows, err := s.db.Query(ctx, `
|
||||
SELECT id, access_hash, file_reference, date, dc_id, has_stickers, sizes::text
|
||||
FROM photos
|
||||
WHERE id = ANY($1::bigint[])
|
||||
`, unique)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
defer rows.Close()
|
||||
byID := make(map[int64]domain.Photo, len(unique))
|
||||
for rows.Next() {
|
||||
photo, err := scanPhotoRow(rows)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
byID[photo.ID] = photo
|
||||
}
|
||||
if err := rows.Err(); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
out := make([]domain.Photo, 0, len(byID))
|
||||
for _, id := range unique {
|
||||
if photo, ok := byID[id]; ok {
|
||||
out = append(out, photo)
|
||||
}
|
||||
}
|
||||
return out, nil
|
||||
}
|
||||
|
||||
type photoScanner interface {
|
||||
Scan(dest ...any) error
|
||||
}
|
||||
|
|
|
|||
|
|
@ -2,6 +2,7 @@ package postgres
|
|||
|
||||
import (
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"reflect"
|
||||
|
||||
"telesrv/internal/domain"
|
||||
|
|
@ -44,9 +45,12 @@ func decodeMessageMedia(s string) (*domain.MessageMedia, error) {
|
|||
return &m, nil
|
||||
}
|
||||
|
||||
// encodeReplyMarkup 把 inline keyboard 快照序列化为 JSONB;空 markup 序列化为 "{}"。
|
||||
// encodeReplyMarkup 把 reply/inline keyboard 快照序列化为 JSONB;空 markup 序列化为 "{}"。
|
||||
// callback data 是 []byte,json.Marshal 自动 base64(保证经 JSONB 字节级 round-trip)。
|
||||
func encodeReplyMarkup(m *domain.MessageReplyMarkup) ([]byte, error) {
|
||||
if err := domain.ValidateReplyMarkup(m); err != nil {
|
||||
return nil, fmt.Errorf("encode reply markup: %w", err)
|
||||
}
|
||||
if m.IsZero() {
|
||||
return []byte("{}"), nil
|
||||
}
|
||||
|
|
@ -63,6 +67,9 @@ func decodeReplyMarkup(s string) (*domain.MessageReplyMarkup, error) {
|
|||
if err := json.Unmarshal([]byte(s), &m); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if err := domain.ValidateReplyMarkup(&m); err != nil {
|
||||
return nil, fmt.Errorf("decode reply markup: %w", err)
|
||||
}
|
||||
if m.IsZero() {
|
||||
return nil, nil
|
||||
}
|
||||
|
|
|
|||
|
|
@ -91,6 +91,10 @@ func TestMediaStoreRoundTrip(t *testing.T) {
|
|||
if err != nil || !ok || len(gotPhoto.Sizes) != 1 || gotPhoto.Sizes[0].Type != "x" {
|
||||
t.Fatalf("get photo mismatch: ok=%v err=%v photo=%+v", ok, err, gotPhoto)
|
||||
}
|
||||
photos, err := s.GetPhotos(ctx, []int64{photoID, 0, photoID, photoID + 99})
|
||||
if err != nil || len(photos) != 1 || photos[0].ID != photoID || len(photos[0].Sizes) != 1 {
|
||||
t.Fatalf("get photos mismatch: photos=%+v err=%v", photos, err)
|
||||
}
|
||||
|
||||
// ---- sticker set ----
|
||||
set := domain.StickerSet{
|
||||
|
|
|
|||
|
|
@ -51,6 +51,27 @@ func (s *MessageStore) GetByIDs(ctx context.Context, userID int64, ids []int) (d
|
|||
return out, nil
|
||||
}
|
||||
|
||||
// GetByUID resolves one owner's box row by the indexed shared private_message_id.
|
||||
func (s *MessageStore) GetByUID(ctx context.Context, userID, uid int64) (domain.Message, bool, error) {
|
||||
if userID == 0 || uid == 0 {
|
||||
return domain.Message{}, false, nil
|
||||
}
|
||||
row, err := s.q.GetMessageBoxByPrivateMessage(ctx, sqlcgen.GetMessageBoxByPrivateMessageParams{
|
||||
OwnerUserID: userID,
|
||||
PrivateMessageID: uid,
|
||||
})
|
||||
if errors.Is(err, pgx.ErrNoRows) {
|
||||
return domain.Message{}, false, nil
|
||||
}
|
||||
if err != nil {
|
||||
return domain.Message{}, false, fmt.Errorf("get message by uid: %w", err)
|
||||
}
|
||||
if _, err := decodeReplyMarkup(row.ReplyMarkupJson); err != nil {
|
||||
return domain.Message{}, false, fmt.Errorf("get message by uid reply markup: %w", err)
|
||||
}
|
||||
return messageFromGetBoxRow(row), true, nil
|
||||
}
|
||||
|
||||
func (s *MessageStore) ListByUser(ctx context.Context, userID int64, filter domain.MessageFilter) (domain.MessageList, error) {
|
||||
limit := filter.Limit
|
||||
if limit <= 0 {
|
||||
|
|
|
|||
42
internal/store/postgres/message_markup_codec_test.go
Normal file
42
internal/store/postgres/message_markup_codec_test.go
Normal file
|
|
@ -0,0 +1,42 @@
|
|||
package postgres
|
||||
|
||||
import (
|
||||
"testing"
|
||||
|
||||
"telesrv/internal/domain"
|
||||
)
|
||||
|
||||
func TestReplyMarkupCodecValidatesTaggedUnion(t *testing.T) {
|
||||
keyboard := &domain.MessageReplyMarkup{
|
||||
Type: domain.MessageReplyMarkupKeyboard,
|
||||
Keyboard: [][]domain.MarkupButton{{{Type: domain.MarkupButtonText, Text: "Help"}}},
|
||||
Resize: true,
|
||||
Placeholder: "Choose",
|
||||
}
|
||||
raw, err := encodeReplyMarkup(keyboard)
|
||||
if err != nil {
|
||||
t.Fatalf("encode reply keyboard: %v", err)
|
||||
}
|
||||
got, err := decodeReplyMarkup(string(raw))
|
||||
if err != nil || got == nil || got.Kind() != domain.MessageReplyMarkupKeyboard ||
|
||||
len(got.Keyboard) != 1 || got.Keyboard[0][0].Text != "Help" || !got.Resize || got.Placeholder != "Choose" {
|
||||
t.Fatalf("decoded reply keyboard = %#v, err=%v", got, err)
|
||||
}
|
||||
|
||||
// Pre-union inline snapshots intentionally remain readable.
|
||||
legacy, err := decodeReplyMarkup(`{"inline":[[{"type":"callback","text":"OK","data":"b2s="}]]}`)
|
||||
if err != nil || legacy == nil || legacy.Kind() != domain.MessageReplyMarkupInline {
|
||||
t.Fatalf("legacy inline markup = %#v, err=%v", legacy, err)
|
||||
}
|
||||
|
||||
malformed := &domain.MessageReplyMarkup{
|
||||
Type: domain.MessageReplyMarkupInline,
|
||||
Keyboard: [][]domain.MarkupButton{{{Type: domain.MarkupButtonText, Text: "wrong"}}},
|
||||
}
|
||||
if _, err := encodeReplyMarkup(malformed); err == nil {
|
||||
t.Fatal("malformed union must fail at the write boundary")
|
||||
}
|
||||
if _, err := decodeReplyMarkup(`{"type":"inline","keyboard":[[{"type":"text","text":"wrong"}]]}`); err == nil {
|
||||
t.Fatal("malformed stored union must fail at the read boundary")
|
||||
}
|
||||
}
|
||||
|
|
@ -117,7 +117,7 @@ func (s *MessageStore) sendPrivateTextOnce(ctx context.Context, req domain.SendP
|
|||
if err != nil {
|
||||
return domain.SendPrivateTextResult{}, err
|
||||
}
|
||||
// reply_markup(bot inline keyboard)随消息一并入双盒;普通用户发送恒 nil → "{}"。
|
||||
// reply_markup(bot reply/inline keyboard)随消息一并入双盒;普通用户发送恒 nil → "{}"。
|
||||
replyMarkupJSON, err := encodeReplyMarkup(req.ReplyMarkup)
|
||||
if err != nil {
|
||||
return domain.SendPrivateTextResult{}, err
|
||||
|
|
|
|||
|
|
@ -205,6 +205,70 @@ func TestMessageStoreWebViewDataServiceActionRoundTrip(t *testing.T) {
|
|||
assertWebViewData("recipient event", events[0].Message)
|
||||
}
|
||||
|
||||
func TestMessageStoreRequestedPeerDisclosureSnapshotRoundTrip(t *testing.T) {
|
||||
pool := testPool(t)
|
||||
ctx := context.Background()
|
||||
suffix := randomSuffix(t)
|
||||
|
||||
users := NewUserStore(pool)
|
||||
sender := createTestUser(t, ctx, users, "+1666"+suffix+"33", "RequestedSender", "")
|
||||
recipient := createTestUser(t, ctx, users, "+1666"+suffix+"34", "RequestedRecipient", "")
|
||||
t.Cleanup(func() {
|
||||
_, _ = pool.Exec(ctx, "DELETE FROM users WHERE id = ANY($1::bigint[])", []int64{sender.ID, recipient.ID})
|
||||
})
|
||||
|
||||
requestedPeer := domain.Peer{Type: domain.PeerTypeChannel, ID: 5501}
|
||||
photo := domain.Photo{ID: 8201, Sizes: []domain.PhotoSize{{
|
||||
Kind: domain.PhotoSizeKindDefault, Type: "m", W: 320, H: 320, Size: 4096,
|
||||
}}}
|
||||
messages := NewMessageStore(pool)
|
||||
got, err := messages.SendPrivateText(ctx, domain.SendPrivateTextRequest{
|
||||
SenderUserID: sender.ID, RecipientUserID: recipient.ID, RandomID: 9002, Date: 1700000212,
|
||||
Media: &domain.MessageMedia{Kind: domain.MessageMediaKindService, ServiceAction: &domain.MessageServiceAction{
|
||||
Kind: domain.MessageServiceActionRequestedPeer,
|
||||
RequestedPeer: &domain.MessageRequestedPeerAction{
|
||||
ButtonID: 88, Peers: []domain.Peer{requestedPeer},
|
||||
Details: []domain.MessageRequestedPeerDetails{{
|
||||
Peer: requestedPeer, Title: "Shared Chat", Username: "shared_chat", Photo: &photo,
|
||||
}},
|
||||
NameRequested: true, UsernameRequested: true, PhotoRequested: true,
|
||||
},
|
||||
}},
|
||||
})
|
||||
if err != nil {
|
||||
t.Fatalf("SendPrivateText: %v", err)
|
||||
}
|
||||
assertSnapshot := func(name string, msg domain.Message) {
|
||||
t.Helper()
|
||||
if msg.Media == nil || msg.Media.ServiceAction == nil || msg.Media.ServiceAction.RequestedPeer == nil {
|
||||
t.Fatalf("%s media=%+v, want requested-peer action", name, msg.Media)
|
||||
}
|
||||
action := msg.Media.ServiceAction.RequestedPeer
|
||||
if action.ButtonID != 88 || len(action.Peers) != 1 || action.Peers[0] != requestedPeer ||
|
||||
len(action.Details) != 1 || action.Details[0].Title != "Shared Chat" ||
|
||||
action.Details[0].Username != "shared_chat" || action.Details[0].Photo == nil ||
|
||||
len(action.Details[0].Photo.Sizes) != 1 || action.Details[0].Photo.Sizes[0].W != 320 ||
|
||||
!action.NameRequested || !action.UsernameRequested || !action.PhotoRequested {
|
||||
t.Fatalf("%s requested-peer=%+v", name, action)
|
||||
}
|
||||
}
|
||||
assertSnapshot("sender", got.SenderMessage)
|
||||
assertSnapshot("recipient", got.RecipientMessage)
|
||||
|
||||
history, err := messages.ListByUser(ctx, recipient.ID, domain.MessageFilter{
|
||||
HasPeer: true, Peer: domain.Peer{Type: domain.PeerTypeUser, ID: sender.ID}, Limit: 10,
|
||||
})
|
||||
if err != nil || len(history.Messages) != 1 {
|
||||
t.Fatalf("recipient history=%+v err=%v", history, err)
|
||||
}
|
||||
assertSnapshot("recipient history", history.Messages[0])
|
||||
events, err := NewUpdateEventStore(pool).ListAfter(ctx, recipient.ID, 0, 10)
|
||||
if err != nil || len(events) != 1 {
|
||||
t.Fatalf("recipient events=%+v err=%v", events, err)
|
||||
}
|
||||
assertSnapshot("recipient event", events[0].Message)
|
||||
}
|
||||
|
||||
func TestMessageStorePhoneCallServiceFirstMessageFeedsDialogsAndUpdates(t *testing.T) {
|
||||
pool := testPool(t)
|
||||
ctx := context.Background()
|
||||
|
|
|
|||
|
|
@ -294,21 +294,33 @@ type Bot struct {
|
|||
}
|
||||
|
||||
type BotApiUpdate struct {
|
||||
ID int64
|
||||
BotUserID int64
|
||||
UpdateKind string
|
||||
PeerType string
|
||||
PeerID int64
|
||||
MessageID int32
|
||||
SourcePts int32
|
||||
Date int32
|
||||
CreatedAt pgtype.Timestamptz
|
||||
ID int64
|
||||
BotUserID int64
|
||||
UpdateKind string
|
||||
PeerType string
|
||||
PeerID int64
|
||||
MessageID int32
|
||||
SourcePts int32
|
||||
Date int32
|
||||
CreatedAt pgtype.Timestamptz
|
||||
CallbackQueryID int64
|
||||
CallbackUserID int64
|
||||
CallbackChatInstance int64
|
||||
CallbackData []byte
|
||||
CallbackInlineDcID int32
|
||||
CallbackInlineOwnerID int64
|
||||
CallbackInlineMessageID int32
|
||||
CallbackInlineAccessHash int64
|
||||
}
|
||||
|
||||
type BotApiUpdateState struct {
|
||||
BotUserID int64
|
||||
ConfirmedUpdateID int64
|
||||
UpdatedAt pgtype.Timestamptz
|
||||
AllowedUpdates []string
|
||||
CursorInitialized bool
|
||||
PollOwner string
|
||||
PollExpiresAt pgtype.Timestamptz
|
||||
}
|
||||
|
||||
type BotApp struct {
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue