2022-04-24 19:35:18 +00:00
|
|
|
package db
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"git.user-penguin.space/vladimir/all-tg-bot/config"
|
|
|
|
"go.mongodb.org/mongo-driver/bson"
|
|
|
|
"go.mongodb.org/mongo-driver/mongo"
|
|
|
|
"go.mongodb.org/mongo-driver/mongo/options"
|
|
|
|
"time"
|
|
|
|
)
|
|
|
|
|
|
|
|
type chatDTO struct {
|
2022-04-24 20:11:23 +00:00
|
|
|
ChatID int64 `bson:"chat_id"`
|
|
|
|
}
|
|
|
|
|
|
|
|
type chatInfoDTO struct {
|
2022-04-24 20:07:52 +00:00
|
|
|
ChatID int64 `bson:"chat_id"`
|
|
|
|
Users []string `bson:"users"`
|
2022-04-24 19:35:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
var cfg *config.Config
|
|
|
|
|
|
|
|
func init() {
|
|
|
|
cfg = config.NewConfig()
|
|
|
|
}
|
|
|
|
|
|
|
|
func AddChat(chatID int64) error {
|
|
|
|
ctx, cancel := context.WithTimeout(context.Background(), time.Second)
|
|
|
|
defer cancel()
|
|
|
|
|
|
|
|
client, err := mongo.Connect(ctx, options.Client().ApplyURI(cfg.MongoURL))
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
collection := client.Database(cfg.DBName).Collection(cfg.ChatsCollectionName)
|
|
|
|
|
2022-04-24 19:57:53 +00:00
|
|
|
result := collection.FindOne(ctx, bson.M{"chat_id": chatID})
|
2022-04-24 19:35:18 +00:00
|
|
|
if result.Err() == mongo.ErrNoDocuments {
|
|
|
|
if _, err := collection.InsertOne(ctx, &chatDTO{ChatID: chatID}); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
return result.Err()
|
|
|
|
}
|
2022-04-24 19:54:43 +00:00
|
|
|
|
|
|
|
func AddUserInChat(chatID int64, username string) error {
|
2022-04-24 20:07:52 +00:00
|
|
|
if username == "" {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2022-04-24 19:54:43 +00:00
|
|
|
if err := AddChat(chatID); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
ctx, cancel := context.WithTimeout(context.Background(), time.Second)
|
|
|
|
defer cancel()
|
|
|
|
|
|
|
|
client, err := mongo.Connect(ctx, options.Client().ApplyURI(cfg.MongoURL))
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
collection := client.Database(cfg.DBName).Collection(cfg.ChatsCollectionName)
|
|
|
|
_, err = collection.UpdateOne(
|
|
|
|
ctx,
|
2022-04-24 19:57:53 +00:00
|
|
|
bson.M{"chat_id": chatID},
|
2022-04-24 19:59:44 +00:00
|
|
|
bson.M{"$addToSet": bson.M{"users": username}},
|
2022-04-24 19:54:43 +00:00
|
|
|
)
|
|
|
|
return err
|
|
|
|
}
|
2022-04-24 20:07:52 +00:00
|
|
|
|
|
|
|
func GetUsers(chatID int64) ([]string, error) {
|
|
|
|
ctx, cancel := context.WithTimeout(context.Background(), time.Second)
|
|
|
|
defer cancel()
|
|
|
|
|
|
|
|
client, err := mongo.Connect(ctx, options.Client().ApplyURI(cfg.MongoURL))
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
collection := client.Database(cfg.DBName).Collection(cfg.ChatsCollectionName)
|
2022-04-24 20:11:23 +00:00
|
|
|
chatInfoDTO := &chatInfoDTO{}
|
|
|
|
if err := collection.FindOne(ctx, bson.M{"chat_id": chatID}).Decode(chatInfoDTO); err != nil {
|
2022-04-24 20:07:52 +00:00
|
|
|
return nil, err
|
|
|
|
}
|
2022-04-24 20:11:23 +00:00
|
|
|
return chatInfoDTO.Users, err
|
2022-04-24 20:07:52 +00:00
|
|
|
}
|