From 45049a0c843dd5e42ce4ab3e8d196b6bb097dd83 Mon Sep 17 00:00:00 2001 From: Nefrace Date: Sun, 18 Jun 2023 23:39:55 +0300 Subject: [PATCH] Chat table for db --- commands.go | 6 +++--- main.go | 1 + middleware.go | 36 ++++++++++++++++++++++++++++++++++++ rss.go | 5 ++--- types.go | 9 +++++++++ 5 files changed, 51 insertions(+), 6 deletions(-) diff --git a/commands.go b/commands.go index a09e365..ca50368 100644 --- a/commands.go +++ b/commands.go @@ -7,10 +7,10 @@ import ( var commandMe = neco.NewCommand("me", "Пишу ваш текст о вас в третьем лице", false) var commandHelp = neco.NewCommand("help", "Показываю данный текст", false) var commandSay = neco.NewCommand("say", "Пишу ваш текст от своего имени.", true) -var commandWarn = neco.NewCommand("warn", "Делаю предупреждение пользователю", true) var commandFeed = neco.NewCommand("subscribe", "Регистрирую данный чат в качестве получателя рассылки", true) var commandFeedUnsub = neco.NewCommand("unsubscribe", "Удаляю данный чат из рассылки", true) +var commandTop = neco.NewCommand("top", "Показываю ТОП-10 пользователей по числу кармы", false) -var defaultCommands = []*neco.Command{commandHelp, commandMe} -var adminCommands = []*neco.Command{commandSay, commandWarn, commandFeed} +var defaultCommands = []*neco.Command{commandHelp, commandMe, commandTop} +var adminCommands = []*neco.Command{commandSay, commandFeed, commandFeed, commandFeedUnsub} var allCommands = append(defaultCommands, adminCommands...) diff --git a/main.go b/main.go index c0cb965..c2e3e56 100644 --- a/main.go +++ b/main.go @@ -29,6 +29,7 @@ func main() { neco := nechotron.NewTron(token, &MainState) neco. Use(UserDBUpdater(db)). + Use(ChatUpdate(db)). Use(ErrorLogger). Use(ExecTimeLogger) api := echotron.NewAPI(token) diff --git a/middleware.go b/middleware.go index e90f318..6679fb3 100644 --- a/middleware.go +++ b/middleware.go @@ -28,6 +28,42 @@ func UserLogger(next nechotron.UpdateHandler) nechotron.UpdateHandler { } } +func ChatUpdate(db *tongo.Database) nechotron.Middleware { + return func(next nechotron.UpdateHandler) nechotron.UpdateHandler { + return func(u *nechotron.Update) error { + if u.Message == nil { + return next(u) + } + chats := tongo.NewStore[Chat](db) + config := tongo.NewStore[Config](db) + chat := u.Message.Chat + res, err := chats.GetOne(u.Ctx, tongo.E("id", chat.ID)) + if err != nil { + addNewChats, err := config.GetOne(u.Ctx, tongo.E("name", "newchats")) + if err != nil { + addNewChats = &Config{ + Item: tongo.NewID(), + Name: "newchats", + Value: true, + } + config.InsertOne(u.Ctx, addNewChats) + } + addNew := addNewChats.Value.(bool) + if !addNew { + return nil + } + res = &Chat{ + Item: tongo.NewID(), + ID: chat.ID, + Title: chat.Title, + } + chats.InsertOne(u.Ctx, res) + } + return next(u) + } + } +} + func UserDBUpdater(db *tongo.Database) nechotron.Middleware { return func(next nechotron.UpdateHandler) nechotron.UpdateHandler { return func(u *nechotron.Update) error { diff --git a/rss.go b/rss.go index e39b743..f615407 100644 --- a/rss.go +++ b/rss.go @@ -54,11 +54,10 @@ func ParseRSS(api *echotron.API) { author := "by " + item.Author.Name desc := item.Description translator := InitTranslator() - translates, err := translator.Translate("ru", item.Title, "by "+item.Author.Name, item.Description) + translates, err := translator.Translate("ru", item.Title, item.Description) if err == nil { title = translates[0] - author = translates[1] - desc = translates[2] + desc = translates[1] } text := fmt.Sprintf(text_template, nechotron.EscapeMd2(title), diff --git a/types.go b/types.go index c1f710a..3ec024b 100644 --- a/types.go +++ b/types.go @@ -52,6 +52,14 @@ var userIndex = mongo.IndexModel{ func (User) Coll() string { return "users" } +type Chat struct { + tongo.Item `bson:",inline"` + ID int64 + Title string +} + +func (Chat) Coll() string { return "chats" } + var _ tongo.Collectable = &KarmaShot{} type KarmaShot struct { @@ -113,6 +121,7 @@ type FeedChat struct { Title string ChatID int64 ThreadID int + Language string } func (FeedChat) Coll() string { return "feed_chats" }