diff --git a/internal/rpc/bots_inline.go b/internal/rpc/bots_inline.go index 56e39b1d..2dc3d037 100644 --- a/internal/rpc/bots_inline.go +++ b/internal/rpc/bots_inline.go @@ -250,6 +250,7 @@ func (r *Router) onMessagesSendInlineBotResult(ctx context.Context, req *tg.Mess if !duplicate { r.pushInlineBotSendFeedback(ctx, userID, results, result, updates) r.inlines.consumeContext(ctx, req.QueryID) + r.autoSaveSentGif(ctx, userID, media) } return updates, nil } diff --git a/internal/rpc/messages_stickers_personal.go b/internal/rpc/messages_stickers_personal.go index 677df576..8567b7bb 100644 --- a/internal/rpc/messages_stickers_personal.go +++ b/internal/rpc/messages_stickers_personal.go @@ -6,6 +6,7 @@ import ( "fmt" "github.com/iamxvbaba/td/tg" + "go.uber.org/zap" "telesrv/internal/domain" ) @@ -125,6 +126,39 @@ func (r *Router) onMessagesSaveGif(ctx context.Context, req *tg.MessagesSaveGifR return true, nil } +// autoSaveSentGif mirrors official Telegram: sending a GIF adds it to the +// sender's Saved GIFs, and re-sending an already-saved one bumps it back to the +// front (SaveStickerCollectionItem's upsert refreshes used_at/order_key, so +// both cases are the same call). +// +// The server has to do this: clients never save on send. TDesktop only issues +// messages.saveGif for the explicit "Save GIF" toggle and its context-menu +// entry (api/api_toggling_media.cpp, history_view_context_menu.cpp), so without +// this a GIF picked out of @gif's catalog would send fine and still never show +// up in the user's own GIFs tab. +// +// Best-effort by design: the message is already sent and acknowledged by the +// time this runs, so a collection write failure is logged rather than turned +// into a send error the client would retry. +func (r *Router) autoSaveSentGif(ctx context.Context, userID int64, media *domain.MessageMedia) { + if media == nil || media.Kind != domain.MessageMediaKindDocument || media.Document == nil { + return + } + if !media.Document.IsGif() { + return + } + svc, ok := r.stickerCollectionSvc() + if !ok { + return + } + if err := svc.SaveStickerCollectionItem(ctx, userID, domain.StickerCollectionGif, media.Document.ID, false, int(r.clock.Now().Unix())); err != nil { + r.log.Warn("auto-save sent gif", + zap.Int64("user_id", userID), zap.Int64("document_id", media.Document.ID), zap.Error(err)) + return + } + r.pushStickerCollectionUpdate(ctx, userID, &tg.UpdateSavedGifs{}) +} + func (r *Router) onMessagesClearRecentStickers(ctx context.Context, req *tg.MessagesClearRecentStickersRequest) (bool, error) { userID, _, err := r.currentUserID(ctx) if err != nil {