62 lines
1.4 KiB
Go
62 lines
1.4 KiB
Go
package main
|
|
|
|
import (
|
|
"context"
|
|
"log"
|
|
"time"
|
|
|
|
"git.nefrace.ru/nefrace/nechotron"
|
|
"git.nefrace.ru/nefrace/tongo"
|
|
)
|
|
|
|
func ExecTimeLogger(next nechotron.UpdateHandler) nechotron.UpdateHandler {
|
|
return func(u *nechotron.Update) error {
|
|
start := time.Now()
|
|
err := next(u)
|
|
t := time.Since(start)
|
|
log.Printf("Update [%s] was handled in %d microseconds", u.UpdateID.String(), t.Microseconds())
|
|
return err
|
|
}
|
|
}
|
|
|
|
func UserLogger(next nechotron.UpdateHandler) nechotron.UpdateHandler {
|
|
return func(u *nechotron.Update) error {
|
|
log.Println(u.From().FirstName)
|
|
err := next(u)
|
|
return err
|
|
}
|
|
}
|
|
|
|
func UserDBUpdater(db *tongo.Database) nechotron.Middleware {
|
|
return func(next nechotron.UpdateHandler) nechotron.UpdateHandler {
|
|
return func(u *nechotron.Update) error {
|
|
from := u.From()
|
|
userFrom, err := UpdateUser(u.Ctx, db, from, true)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
u.Ctx = context.WithValue(u.Ctx, "userfrom", userFrom)
|
|
if u.IsMessage() && u.Message.ReplyToMessage != nil {
|
|
to := u.Message.ReplyToMessage.From
|
|
userTo, err := UpdateUser(u.Ctx, db, to, false)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
u.Ctx = context.WithValue(u.Ctx, "userto", userTo)
|
|
}
|
|
|
|
return next(u)
|
|
}
|
|
}
|
|
}
|
|
|
|
func ErrorLogger(next nechotron.UpdateHandler) nechotron.UpdateHandler {
|
|
return func(u *nechotron.Update) error {
|
|
err := next(u)
|
|
if err != nil {
|
|
u.LogError("", err, true)
|
|
}
|
|
return err
|
|
}
|
|
}
|