package main import ( "context" "time" "git.nefrace.ru/nefrace/tongo" "github.com/NicoNex/echotron/v3" "go.mongodb.org/mongo-driver/mongo" ) var _ tongo.Collectable = &User{} type User struct { tongo.Item `bson:",inline"` Username string Name string ID int64 Karma int64 LastMessage time.Time } func UpdateUser(ctx context.Context, db *tongo.Database, user *echotron.User, updateLastMessage bool) (*User, error) { store := tongo.NewStore[User](db) u, err := store.GetOne(ctx, tongo.E("id", user.ID)) if err != nil { u = &User{ Item: tongo.NewID(), ID: user.ID, } } name := user.FirstName if user.LastName != "" { name += " " + user.LastName } u.Name = name u.Username = user.Username if updateLastMessage { u.LastMessage = time.Now() } err = store.ReplaceItem(ctx, *u, true) if err != nil { // log.Println("Cant replace user: ", err) return nil, err } return u, nil } var userIndex = mongo.IndexModel{ Keys: tongo.D(tongo.E("id", 1)), } func (User) Coll() string { return "users" } type RatedUser struct { tongo.Item `bson:",inline"` Name string ID int64 Karma int64 `bson:"karma"` } func (RatedUser) Coll() string { return "top10" } type Chat struct { tongo.Item `bson:",inline"` ID int64 Title string } func (Chat) Coll() string { return "chats" } var _ tongo.Collectable = &KarmaShot{} type KarmaShot struct { tongo.Item `bson:",inline"` From int64 To int64 Count int MessageText string When time.Time } var karmaFromIndex = mongo.IndexModel{ Keys: tongo.D(tongo.E("from", 1)), } var karmaToIndex = mongo.IndexModel{ Keys: tongo.D(tongo.E("to", 1)), } func (KarmaShot) Coll() string { return "karma" } type KarmaMessage struct { tongo.Item `bson:",inline"` Template string `bson:"template"` WordPlus string `bson:"word_plus"` WordMinus string `bson:"word_minus"` } func (KarmaMessage) Coll() string { return "karma_messages" } type Warn struct { tongo.Item `bson:",inline"` From int64 To int64 MessageText string WarnText string When time.Time Active bool } func (Warn) Coll() string { return "warns" } type TriggerText struct { tongo.Item `bson:",inline"` Trigger string Text string } func (TriggerText) Coll() string { return "triggers" } type Config struct { tongo.Item `bson:",inline"` Name string Value interface{} } func (Config) Coll() string { return "config" } type Feeds struct { tongo.Item `bson:",inline"` Title string Published time.Time } func (Feeds) Coll() string { return "feeds" } type FeedChat struct { tongo.Item `bson:",inline"` Title string ChatID int64 ThreadID int Language string } func (FeedChat) Coll() string { return "feed_chats" } type RemovableMessage struct { tongo.Item `bson:",inline"` ChatID int64 MessageID int DeleteBy time.Time } func (RemovableMessage) Coll() string { return "removables" }