Skip to content

Commit

Permalink
wip
Browse files Browse the repository at this point in the history
  • Loading branch information
nnqq committed Nov 2, 2021
1 parent 1205abe commit 1771abf
Show file tree
Hide file tree
Showing 7 changed files with 88 additions and 24 deletions.
2 changes: 2 additions & 0 deletions cmd/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,8 @@ func main() {
userRepo,
peerRepo,
childBotRepo,
replyRepo,
childStateRepo,
cfg.ChildBot.Host,
cfg.ChildBot.TokenPathPrefix,
cfg.ChildBot.BotsLimitPerUser,
Expand Down
8 changes: 4 additions & 4 deletions pkg/child_bot/repo.go
Original file line number Diff line number Diff line change
Expand Up @@ -103,13 +103,13 @@ func (r *Repo) Create(c context.Context, userID primitive.ObjectID, token string
return nil
}

func (r *Repo) Delete(c context.Context, userID, botID primitive.ObjectID) error {
func (r *Repo) Delete(c context.Context, userID, id primitive.ObjectID) error {
ctx, cancel := context.WithTimeout(c, 10*time.Second)
defer cancel()

_, err := r.coll.DeleteOne(ctx, Bot{
ID: botID,
OwnerUserID: userID,
_, err := r.coll.DeleteOne(ctx, bson.M{
"_id": id,
"ui": userID,
})
if err != nil {
return fmt.Errorf("r.coll.DeleteOne: %w", err)
Expand Down
16 changes: 9 additions & 7 deletions pkg/child_bot/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -147,8 +147,6 @@ func (s *service) Serve(ctx context.Context) error {

token := strings.TrimPrefix(r.URL.Path, pathPrefix)

s.logger.Debug().Str("path", r.URL.Path).Str("token", token).Msg("got request")

whOK, err := s.handle(rc, token, r.Body)
if err != nil {
s.logger.Warn().Err(err).Send()
Expand Down Expand Up @@ -227,8 +225,6 @@ func (s *service) handle(ctx context.Context, token string, body io.ReadCloser)
return true, fmt.Errorf("io.ReadAll: %w", err)
}

s.logger.Debug().Str("body", string(b)).Send()

var upd update
err = json.Unmarshal(b, &upd)
if err != nil {
Expand Down Expand Up @@ -641,7 +637,7 @@ func (s *service) handlePeer(ctx context.Context, api *tgbotapi.BotAPI, upd upda
}

if bot.Mode == OnlyFirst && peerFound {
id, e := s.replyRepo.Create(ctx, upd.Message.From.ID, upd.Message.Chat.ID, upd.Message.MessageID)
id, e := s.replyRepo.Create(ctx, bot.ID, upd.Message.From.ID, upd.Message.Chat.ID, upd.Message.MessageID)
if e != nil {
return fmt.Errorf("s.replyRepo.Create: %w", e)
}
Expand Down Expand Up @@ -690,7 +686,13 @@ kws:
}

if bot.OwnerUserChatID != 0 {
id, er := s.replyRepo.Create(ctx, upd.Message.From.ID, upd.Message.Chat.ID, upd.Message.MessageID)
id, er := s.replyRepo.Create(
ctx,
bot.ID,
upd.Message.From.ID,
upd.Message.Chat.ID,
upd.Message.MessageID,
)
if er != nil {
return fmt.Errorf("s.replyRepo.Create: %w", er)
}
Expand Down Expand Up @@ -725,7 +727,7 @@ kws:
}

if !match {
id, e := s.replyRepo.Create(ctx, upd.Message.From.ID, upd.Message.Chat.ID, upd.Message.MessageID)
id, e := s.replyRepo.Create(ctx, bot.ID, upd.Message.From.ID, upd.Message.Chat.ID, upd.Message.MessageID)
if e != nil {
return fmt.Errorf("s.replyRepo.Create: %w", e)
}
Expand Down
12 changes: 12 additions & 0 deletions pkg/child_state/repo.go
Original file line number Diff line number Diff line change
Expand Up @@ -98,3 +98,15 @@ func (r *Repo) GetScene(c context.Context, userID, childBotID primitive.ObjectID

return st.Scene, nil
}

func (r *Repo) Delete(c context.Context, userID, childBotID primitive.ObjectID) error {
_, err := r.coll.DeleteOne(c, bson.M{
"ui": userID,
"cbi": childBotID,
})
if err != nil {
return fmt.Errorf("r.coll.DeleteOne: %w", err)
}

return nil
}
30 changes: 25 additions & 5 deletions pkg/parent_bot/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,10 @@ import (
tgbotapi "github.com/go-telegram-bot-api/telegram-bot-api"
"github.com/rs/zerolog"
"github.com/vahter-robot/backend/pkg/child_bot"
"github.com/vahter-robot/backend/pkg/child_state"
"github.com/vahter-robot/backend/pkg/parent_state"
"github.com/vahter-robot/backend/pkg/peer"
"github.com/vahter-robot/backend/pkg/reply"
"github.com/vahter-robot/backend/pkg/user"
"go.mongodb.org/mongo-driver/bson/primitive"
tb "gopkg.in/tucnak/telebot.v2"
Expand All @@ -22,6 +24,8 @@ type service struct {
userRepo *user.Repo
peerRepo *peer.Repo
childBotRepo *child_bot.Repo
replyRepo *reply.Repo
childStateRepo *child_state.Repo
childBotHost string
childTokenPathPrefix string
childBotsLimitPerUser uint16
Expand All @@ -46,6 +50,8 @@ func NewService(
userRepo *user.Repo,
peerRepo *peer.Repo,
childBotRepo *child_bot.Repo,
replyRepo *reply.Repo,
childStateRepo *child_state.Repo,
childBotHost,
childTokenPathPrefix string,
childBotsLimitPerUser uint16,
Expand Down Expand Up @@ -75,6 +81,8 @@ func NewService(
userRepo: userRepo,
peerRepo: peerRepo,
childBotRepo: childBotRepo,
replyRepo: replyRepo,
childStateRepo: childStateRepo,
childBotHost: childBotHost,
childTokenPathPrefix: childTokenPathPrefix,
childBotsLimitPerUser: childBotsLimitPerUser,
Expand Down Expand Up @@ -238,10 +246,22 @@ func (b *service) deleteChildBot(ctx context.Context, userID, childBotID primiti
return fmt.Errorf("b.childBotRepo.Delete: %w", err)
}

err = b.peerRepo.DeleteByChildBotID(ctx, childBotID)
if err != nil {
return fmt.Errorf("b.peerRepo.DeleteByChildBotID: %w", err)
}
go func() {
bg := context.Background()
err = b.peerRepo.DeleteByChildBotID(bg, childBotID)
if err != nil {
b.logger.Error().Err(fmt.Errorf("b.peerRepo.DeleteByChildBotID: %w", err)).Send()
}
err = b.replyRepo.DeleteByChildBotID(bg, childBotID)
if err != nil {
b.logger.Error().Err(fmt.Errorf("b.replyRepo.DeleteByChildBotID: %w", err)).Send()
}
err = b.childStateRepo.Delete(bg, userID, childBotID)
if err != nil {
b.logger.Error().Err(fmt.Errorf("b.childStateRepo.Delete: %w", err)).Send()
}
}()

return nil
}

Expand Down Expand Up @@ -295,7 +315,7 @@ func (b *service) handleOnText(msg *tb.Message) {

e = b.childBotRepo.Create(ctx, usr.ID, token)
if e != nil {
b.replyFatalErr(msg, e)
b.replyErr(msg, "Бот с таким токеном уже существует")
return
}

Expand Down
5 changes: 1 addition & 4 deletions pkg/peer/repo.go
Original file line number Diff line number Diff line change
Expand Up @@ -98,10 +98,7 @@ func (r *Repo) CreateMuted(c context.Context, childBotID primitive.ObjectID, tgU
}

func (r *Repo) DeleteByChildBotID(c context.Context, childBotID primitive.ObjectID) error {
ctx, cancel := context.WithTimeout(c, 10*time.Second)
defer cancel()

_, err := r.coll.DeleteMany(ctx, bson.M{
_, err := r.coll.DeleteMany(c, bson.M{
"cbi": childBotID,
})
if err != nil {
Expand Down
39 changes: 35 additions & 4 deletions pkg/reply/repo.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import (

type Reply struct {
ID primitive.ObjectID `bson:"_id,omitempty"`
ChildBotID primitive.ObjectID `bson:"cbi,omitempty"`
TgUserID int64 `bson:"tui,omitempty"`
TgChatID int64 `bson:"tci,omitempty"`
TgMessageID int64 `bson:"tmi,omitempty"`
Expand All @@ -36,7 +37,7 @@ func NewRepo(ctx context.Context, db *mongo.Database) (*Repo, error) {
}

func (r *Repo) createIndex(ctx context.Context) error {
_, err := r.coll.Indexes().CreateOne(ctx, mongo.IndexModel{
_, err := r.coll.Indexes().CreateMany(ctx, []mongo.IndexModel{{
Keys: bson.D{{
Key: "tui",
Value: 1,
Expand All @@ -46,17 +47,33 @@ func (r *Repo) createIndex(ctx context.Context) error {
}, {
Key: "tmi",
Value: 1,
}, {
Key: "cbi",
Value: 1,
}},
Options: options.Index().SetUnique(true),
})
}, {
Keys: bson.M{
"cbi": 1,
},
}})
if err != nil {
return fmt.Errorf("r.coll.Indexes().CreateOne: %w", err)
return fmt.Errorf("r.coll.Indexes().CreateMany: %w", err)
}

return nil
}

func (r *Repo) Create(c context.Context, tgUserID, tgChatID, tgMessageID int64) (primitive.ObjectID, error) {
func (r *Repo) Create(
c context.Context,
childBotID primitive.ObjectID,
tgUserID,
tgChatID,
tgMessageID int64,
) (
primitive.ObjectID,
error,
) {
ctx, cancel := context.WithTimeout(c, 10*time.Second)
defer cancel()

Expand All @@ -66,11 +83,13 @@ func (r *Repo) Create(c context.Context, tgUserID, tgChatID, tgMessageID int64)
"tui": tgUserID,
"tci": tgChatID,
"tmi": tgMessageID,
"cbi": childBotID,
}, bson.M{
"$setOnInsert": bson.M{
"tui": tgUserID,
"tci": tgChatID,
"tmi": tgMessageID,
"cbi": childBotID,
},
}, options.Update().SetUpsert(true))
if err != nil {
Expand All @@ -87,6 +106,7 @@ func (r *Repo) Create(c context.Context, tgUserID, tgChatID, tgMessageID int64)
"tui": tgUserID,
"tci": tgChatID,
"tmi": tgMessageID,
"cbi": childBotID,
}, options.FindOne().SetProjection(bson.M{
"_id": 1,
})).Decode(&doc)
Expand All @@ -111,3 +131,14 @@ func (r *Repo) GetByID(c context.Context, id primitive.ObjectID) (Reply, error)

return reply, nil
}

func (r *Repo) DeleteByChildBotID(c context.Context, childBotID primitive.ObjectID) error {
_, err := r.coll.DeleteMany(c, bson.M{
"cbi": childBotID,
})
if err != nil {
return fmt.Errorf("r.coll.DeleteMany: %w", err)
}

return nil
}

0 comments on commit 1771abf

Please sign in to comment.