Files
store-scraper-bot/internal/model/group.go
T
tiennm99 e695d9b223 feat: port logic from Java original to align with current Java behavior
- 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.
2026-04-25 20:26:25 +07:00

81 lines
2.0 KiB
Go

package model
import "strconv"
// AppInfo mirrors Java AppleAppInfo / GoogleAppInfo records.
type AppInfo struct {
AppID string `bson:"appId" json:"appId"`
Country string `bson:"country" json:"country"`
}
type Group struct {
AbstractModel `bson:",inline"`
AppleApps []AppInfo `bson:"appleApps" json:"appleApps"`
GoogleApps []AppInfo `bson:"googleApps" json:"googleApps"`
}
// GroupIDToKey converts a Telegram chat ID to the string _id used by Java.
func GroupIDToKey(groupID int64) string {
return strconv.FormatInt(groupID, 10)
}
// GroupKeyToID parses a stored _id back to int64.
func GroupKeyToID(key string) (int64, error) {
return strconv.ParseInt(key, 10, 64)
}
func NewGroup(groupID int64) *Group {
return &Group{
AbstractModel: AbstractModel{ID: GroupIDToKey(groupID), Class: "Group"},
AppleApps: []AppInfo{},
GoogleApps: []AppInfo{},
}
}
// GroupID returns the int64 chat ID parsed from the stored string _id.
// Returns 0 if parsing fails (matches Java behaviour where _id always parses).
func (g *Group) GroupID() int64 {
id, _ := GroupKeyToID(g.ID)
return id
}
func (g *Group) AddAppleApp(appID, country string) bool {
for _, app := range g.AppleApps {
if app.AppID == appID {
return false
}
}
g.AppleApps = append(g.AppleApps, AppInfo{AppID: appID, Country: country})
return true
}
func (g *Group) RemoveAppleApp(appID string) bool {
for i, app := range g.AppleApps {
if app.AppID == appID {
g.AppleApps = append(g.AppleApps[:i], g.AppleApps[i+1:]...)
return true
}
}
return false
}
func (g *Group) AddGoogleApp(appID, country string) bool {
for _, app := range g.GoogleApps {
if app.AppID == appID {
return false
}
}
g.GoogleApps = append(g.GoogleApps, AppInfo{AppID: appID, Country: country})
return true
}
func (g *Group) RemoveGoogleApp(appID string) bool {
for i, app := range g.GoogleApps {
if app.AppID == appID {
g.GoogleApps = append(g.GoogleApps[:i], g.GoogleApps[i+1:]...)
return true
}
}
return false
}