mirror of
https://github.com/tiennm99/store-scraper-bot.git
synced 2026-10-19 18:16:10 +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.
59 lines
1.6 KiB
Go
59 lines
1.6 KiB
Go
package repository
|
|
|
|
import (
|
|
"context"
|
|
"fmt"
|
|
"time"
|
|
|
|
"github.com/miti99/store-scraper-bot-go/internal/config"
|
|
"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"
|
|
)
|
|
|
|
// GoogleAppRepository caches Google Play responses in the "google_app" collection.
|
|
type GoogleAppRepository struct {
|
|
collection *mongo.Collection
|
|
}
|
|
|
|
func NewGoogleAppRepository() *GoogleAppRepository {
|
|
return &GoogleAppRepository{collection: GetCollection("google_app")}
|
|
}
|
|
|
|
func (r *GoogleAppRepository) Get(ctx context.Context, appID string) (*model.GoogleApp, error) {
|
|
app := &model.GoogleApp{}
|
|
err := r.collection.FindOne(ctx, bson.M{"_id": appID}).Decode(app)
|
|
if err != nil {
|
|
if err == mongo.ErrNoDocuments {
|
|
return nil, nil
|
|
}
|
|
return nil, fmt.Errorf("failed to get google app: %w", err)
|
|
}
|
|
return app, nil
|
|
}
|
|
|
|
func (r *GoogleAppRepository) Save(ctx context.Context, app *model.GoogleApp) error {
|
|
opts := options.Replace().SetUpsert(true)
|
|
_, err := r.collection.ReplaceOne(ctx, bson.M{"_id": app.ID}, app, opts)
|
|
if err != nil {
|
|
return fmt.Errorf("failed to save google app: %w", err)
|
|
}
|
|
return nil
|
|
}
|
|
|
|
func (r *GoogleAppRepository) GetCached(appID string) (*model.GoogleApp, error) {
|
|
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
|
|
defer cancel()
|
|
|
|
app, err := r.Get(ctx, appID)
|
|
if err != nil || app == nil {
|
|
return nil, err
|
|
}
|
|
cacheMillis := int64(config.GlobalConfig.AppCacheSeconds) * 1000
|
|
if app.IsExpired(time.Now().UnixMilli(), cacheMillis) {
|
|
return nil, nil
|
|
}
|
|
return app, nil
|
|
}
|