mirror of
https://github.com/tiennm99/store-scraper-bot.git
synced 2026-08-31 04:30:40 +00:00
- Switch Telegram parse mode to HTML to match Java client. - Rename command identifiers to Java's: delgroup/delapple/delgoogle/ checkappscore/rawappleapp/rawgoogleapp. - Move admin singleton to "common" collection at _id="admin". - Store group _id as string-of-int64 to match Java AbstractModel schema. - Add `class` discriminator field on persisted models. - Expand AppleAppResponse (42 fields) and GoogleAppResponse (63 fields, with nested Category/Feature) to match Java records. - Add AppleAppRequest (track id or bundle id) / GoogleAppRequest types. - Implement Raw* commands as Telegram document attachments instead of truncated text. - Group-authorization gate (admin.HasGroup(chatId)) on non-admin commands. - Cache TTL via millis timestamp; round score to 1 decimal; weekend silent send in scheduler. - Match Java table renderer: " │ " separators, "─┼─" rows every 5 lines. - Prefer MONGODB_CONNECTION_STRING env (Java parity); auto-extract DB name from URI with fallback to "store-scraper-bot". Note: This is an AI-assisted port (not human-verified). Behavior parity with the Java implementation has not been tested end-to-end; treat as a starting point.
109 lines
2.7 KiB
Go
109 lines
2.7 KiB
Go
package repository
|
|
|
|
import (
|
|
"context"
|
|
"fmt"
|
|
"time"
|
|
|
|
"github.com/miti99/store-scraper-bot-go/internal/model"
|
|
"go.mongodb.org/mongo-driver/bson"
|
|
"go.mongodb.org/mongo-driver/mongo"
|
|
"go.mongodb.org/mongo-driver/mongo/options"
|
|
)
|
|
|
|
// AdminRepository persists the singleton Admin document.
|
|
// Java equivalent stores it in the "common" collection at _id="admin".
|
|
type AdminRepository struct {
|
|
collection *mongo.Collection
|
|
}
|
|
|
|
func NewAdminRepository() *AdminRepository {
|
|
return &AdminRepository{collection: GetCollection("common")}
|
|
}
|
|
|
|
// Init creates the singleton document if it does not yet exist.
|
|
func (r *AdminRepository) Init() error {
|
|
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
|
|
defer cancel()
|
|
|
|
count, err := r.collection.CountDocuments(ctx, bson.M{"_id": model.AdminID})
|
|
if err != nil {
|
|
return fmt.Errorf("failed to count admin: %w", err)
|
|
}
|
|
if count > 0 {
|
|
return nil
|
|
}
|
|
return r.Save(ctx, model.NewAdmin())
|
|
}
|
|
|
|
func (r *AdminRepository) Get(ctx context.Context) (*model.Admin, error) {
|
|
admin := &model.Admin{}
|
|
err := r.collection.FindOne(ctx, bson.M{"_id": model.AdminID}).Decode(admin)
|
|
if err != nil {
|
|
if err == mongo.ErrNoDocuments {
|
|
return model.NewAdmin(), nil
|
|
}
|
|
return nil, fmt.Errorf("failed to get admin: %w", err)
|
|
}
|
|
return admin, nil
|
|
}
|
|
|
|
func (r *AdminRepository) Save(ctx context.Context, admin *model.Admin) error {
|
|
opts := options.Replace().SetUpsert(true)
|
|
_, err := r.collection.ReplaceOne(ctx, bson.M{"_id": model.AdminID}, admin, opts)
|
|
if err != nil {
|
|
return fmt.Errorf("failed to save admin: %w", err)
|
|
}
|
|
return nil
|
|
}
|
|
|
|
func (r *AdminRepository) AddGroup(groupID int64) (added bool, err error) {
|
|
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
|
|
defer cancel()
|
|
|
|
admin, err := r.Get(ctx)
|
|
if err != nil {
|
|
return false, err
|
|
}
|
|
if !admin.AddGroup(groupID) {
|
|
return false, nil
|
|
}
|
|
return true, r.Save(ctx, admin)
|
|
}
|
|
|
|
func (r *AdminRepository) RemoveGroup(groupID int64) (removed bool, err error) {
|
|
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
|
|
defer cancel()
|
|
|
|
admin, err := r.Get(ctx)
|
|
if err != nil {
|
|
return false, err
|
|
}
|
|
if !admin.RemoveGroup(groupID) {
|
|
return false, nil
|
|
}
|
|
return true, r.Save(ctx, admin)
|
|
}
|
|
|
|
func (r *AdminRepository) HasGroup(groupID int64) (bool, error) {
|
|
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
|
|
defer cancel()
|
|
|
|
admin, err := r.Get(ctx)
|
|
if err != nil {
|
|
return false, err
|
|
}
|
|
return admin.HasGroup(groupID), nil
|
|
}
|
|
|
|
func (r *AdminRepository) GetAllGroups() ([]int64, error) {
|
|
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
|
|
defer cancel()
|
|
|
|
admin, err := r.Get(ctx)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
return admin.Groups, nil
|
|
}
|