go-dette/types.go

100 lines
1.9 KiB
Go
Raw Normal View History

2023-01-20 16:32:21 +03:00
package main
2023-01-24 00:37:27 +03:00
import (
2023-01-24 23:17:30 +03:00
"context"
2023-01-24 00:37:27 +03:00
"time"
"git.nefrace.ru/nefrace/tongo"
2023-01-24 23:17:30 +03:00
"github.com/NicoNex/echotron/v3"
"go.mongodb.org/mongo-driver/mongo"
2023-01-24 00:37:27 +03:00
)
2023-01-20 16:32:21 +03:00
var _ tongo.Collectable = &User{}
type User struct {
tongo.Item `bson:",inline"`
2023-01-24 00:37:27 +03:00
Username string
FirstName string
LastName string
2023-01-24 00:37:27 +03:00
ID int64
2023-01-24 23:17:30 +03:00
KarmaOffset int64
LastMessage time.Time
2023-01-20 16:32:21 +03:00
}
2023-01-24 23:17:30 +03:00
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)),
}
2023-01-20 16:32:21 +03:00
func (User) Coll() string { return "users" }
2023-01-24 00:37:27 +03:00
var _ tongo.Collectable = &KarmaShot{}
type KarmaShot struct {
2023-01-24 23:17:30 +03:00
tongo.Item `bson:",inline"`
From int64
To int64
Count int
MessageText string
When time.Time
2023-01-24 00:37:27 +03:00
}
var karmaFromIndex = mongo.IndexModel{
Keys: tongo.D(tongo.E("from", 1)),
}
var karmaToIndex = mongo.IndexModel{
Keys: tongo.D(tongo.E("to", 1)),
}
2023-01-24 00:37:27 +03:00
func (KarmaShot) Coll() string { return "karma" }
2023-01-24 23:17:30 +03:00
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" }
2023-04-13 11:13:24 +03:00
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" }