84 lines
1.6 KiB
Go
84 lines
1.6 KiB
Go
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
|
|
FirstName string
|
|
LastName string
|
|
ID int64
|
|
KarmaOffset 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,
|
|
}
|
|
}
|
|
u.FirstName = user.FirstName
|
|
u.LastName = user.LastName
|
|
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" }
|
|
|
|
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 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" }
|