added full gifts support
This commit is contained in:
parent
ea5b86de8f
commit
354128c106
35 changed files with 1502 additions and 103 deletions
27
internal/store/gif_catalog.go
Normal file
27
internal/store/gif_catalog.go
Normal file
|
|
@ -0,0 +1,27 @@
|
|||
package store
|
||||
|
||||
import (
|
||||
"context"
|
||||
|
||||
"telesrv/internal/domain"
|
||||
)
|
||||
|
||||
// GifCatalogStore owns the admin-curated GIF catalog the built-in @gif inline
|
||||
// bot serves as results for the client's GIF picker.
|
||||
type GifCatalogStore interface {
|
||||
// CreateGifCatalogEntry inserts a new entry. entry.ID must already be set
|
||||
// by the caller (same convention as documents/photos elsewhere in this
|
||||
// codebase -- ids are app-generated, not database-serial).
|
||||
CreateGifCatalogEntry(ctx context.Context, entry domain.GifCatalogEntry) (domain.GifCatalogEntry, error)
|
||||
// ListGifCatalog returns every entry ordered by (sort_order, id).
|
||||
// onlyEnabled=true is what @gif serves; the admin panel lists everything.
|
||||
ListGifCatalog(ctx context.Context, onlyEnabled bool) ([]domain.GifCatalogEntry, error)
|
||||
// SetGifCatalogEnabled toggles whether an entry is served. changed=false
|
||||
// if the id doesn't exist.
|
||||
SetGifCatalogEnabled(ctx context.Context, id int64, enabled bool) (bool, error)
|
||||
// SetGifCatalogSortOrder rewrites an entry's display position.
|
||||
SetGifCatalogSortOrder(ctx context.Context, id int64, order int) (bool, error)
|
||||
// DeleteGifCatalogEntry removes an entry. The referenced document is left
|
||||
// alone -- catalog membership, not the document itself, is what's deleted.
|
||||
DeleteGifCatalogEntry(ctx context.Context, id int64) (bool, error)
|
||||
}
|
||||
|
|
@ -22,7 +22,7 @@ type UserStore struct {
|
|||
// 预置进表,与 postgres 的迁移种子保持双 store 行为一致。
|
||||
func NewUserStore() *UserStore {
|
||||
s := &UserStore{byID: make(map[int64]domain.User), nextID: domain.UserIDSequenceBase}
|
||||
for _, id := range []int64{domain.OfficialSystemUserID, domain.BotFatherUserID, domain.StickersBotUserID, domain.ChatBotUserID} {
|
||||
for _, id := range []int64{domain.OfficialSystemUserID, domain.BotFatherUserID, domain.StickersBotUserID, domain.ChatBotUserID, domain.GifBotUserID} {
|
||||
if u, ok := domain.SystemUserByID(id); ok {
|
||||
s.byID[u.ID] = u
|
||||
}
|
||||
|
|
|
|||
87
internal/store/postgres/gif_catalog.go
Normal file
87
internal/store/postgres/gif_catalog.go
Normal file
|
|
@ -0,0 +1,87 @@
|
|||
package postgres
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
|
||||
"telesrv/internal/domain"
|
||||
"telesrv/internal/store/postgres/sqlcgen"
|
||||
)
|
||||
|
||||
type GifCatalogStore struct {
|
||||
db sqlcgen.DBTX
|
||||
}
|
||||
|
||||
func NewGifCatalogStore(db sqlcgen.DBTX) *GifCatalogStore {
|
||||
return &GifCatalogStore{db: db}
|
||||
}
|
||||
|
||||
func (s *GifCatalogStore) CreateGifCatalogEntry(ctx context.Context, entry domain.GifCatalogEntry) (domain.GifCatalogEntry, error) {
|
||||
if entry.ID == 0 || entry.DocumentID == 0 {
|
||||
return domain.GifCatalogEntry{}, fmt.Errorf("create gif catalog entry: id and document_id are required")
|
||||
}
|
||||
row := s.db.QueryRow(ctx, `
|
||||
INSERT INTO gif_catalog (id, title, document_id, enabled, sort_order, created_by)
|
||||
VALUES ($1, $2, $3, true, $4, $5)
|
||||
RETURNING id, title, document_id, enabled, sort_order, created_by, created_at, updated_at`,
|
||||
entry.ID, entry.Title, entry.DocumentID, entry.SortOrder, entry.CreatedBy)
|
||||
out, err := scanGifCatalogEntry(row.Scan)
|
||||
if err != nil {
|
||||
return domain.GifCatalogEntry{}, fmt.Errorf("create gif catalog entry: %w", err)
|
||||
}
|
||||
return out, nil
|
||||
}
|
||||
|
||||
func (s *GifCatalogStore) ListGifCatalog(ctx context.Context, onlyEnabled bool) ([]domain.GifCatalogEntry, error) {
|
||||
rows, err := s.db.Query(ctx, `
|
||||
SELECT id, title, document_id, enabled, sort_order, created_by, created_at, updated_at
|
||||
FROM gif_catalog
|
||||
WHERE NOT $1 OR enabled
|
||||
ORDER BY sort_order, id
|
||||
LIMIT `+fmt.Sprint(domain.MaxGifCatalogEntries), onlyEnabled)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("list gif catalog: %w", err)
|
||||
}
|
||||
defer rows.Close()
|
||||
out := make([]domain.GifCatalogEntry, 0)
|
||||
for rows.Next() {
|
||||
item, err := scanGifCatalogEntry(rows.Scan)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("scan gif catalog entry: %w", err)
|
||||
}
|
||||
out = append(out, item)
|
||||
}
|
||||
return out, rows.Err()
|
||||
}
|
||||
|
||||
func (s *GifCatalogStore) SetGifCatalogEnabled(ctx context.Context, id int64, enabled bool) (bool, error) {
|
||||
tag, err := s.db.Exec(ctx, `UPDATE gif_catalog SET enabled = $2, updated_at = now() WHERE id = $1`, id, enabled)
|
||||
if err != nil {
|
||||
return false, fmt.Errorf("set gif catalog entry enabled: %w", err)
|
||||
}
|
||||
return tag.RowsAffected() > 0, nil
|
||||
}
|
||||
|
||||
func (s *GifCatalogStore) SetGifCatalogSortOrder(ctx context.Context, id int64, order int) (bool, error) {
|
||||
tag, err := s.db.Exec(ctx, `UPDATE gif_catalog SET sort_order = $2, updated_at = now() WHERE id = $1`, id, order)
|
||||
if err != nil {
|
||||
return false, fmt.Errorf("set gif catalog entry sort order: %w", err)
|
||||
}
|
||||
return tag.RowsAffected() > 0, nil
|
||||
}
|
||||
|
||||
func (s *GifCatalogStore) DeleteGifCatalogEntry(ctx context.Context, id int64) (bool, error) {
|
||||
tag, err := s.db.Exec(ctx, `DELETE FROM gif_catalog WHERE id = $1`, id)
|
||||
if err != nil {
|
||||
return false, fmt.Errorf("delete gif catalog entry: %w", err)
|
||||
}
|
||||
return tag.RowsAffected() > 0, nil
|
||||
}
|
||||
|
||||
func scanGifCatalogEntry(scan func(dest ...any) error) (domain.GifCatalogEntry, error) {
|
||||
var e domain.GifCatalogEntry
|
||||
if err := scan(&e.ID, &e.Title, &e.DocumentID, &e.Enabled, &e.SortOrder, &e.CreatedBy, &e.CreatedAt, &e.UpdatedAt); err != nil {
|
||||
return domain.GifCatalogEntry{}, err
|
||||
}
|
||||
return e, nil
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue