Skip to content

Commit

Permalink
Create notify channel
Browse files Browse the repository at this point in the history
  • Loading branch information
grulex committed Oct 31, 2023
1 parent 393d784 commit 4429cfc
Show file tree
Hide file tree
Showing 6 changed files with 91 additions and 31 deletions.
33 changes: 25 additions & 8 deletions bot/bot.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (
"github.com/grulex/go-wishlist/container"
authPkg "github.com/grulex/go-wishlist/pkg/auth"
imagePkg "github.com/grulex/go-wishlist/pkg/image"
"github.com/grulex/go-wishlist/pkg/notify"
productPkg "github.com/grulex/go-wishlist/pkg/product"
userPkg "github.com/grulex/go-wishlist/pkg/user"
wishlistPkg "github.com/grulex/go-wishlist/pkg/wishlist"
Expand Down Expand Up @@ -104,13 +105,13 @@ func (s TelegramBot) Start() error {
if update.MyChatMember != nil {
if update.MyChatMember.NewChatMember.Status == "member" {
lang := update.MyChatMember.From.LanguageCode
err := s.checkAndRegisterUser(ctx, update.MyChatMember.From)
err := s.checkAndRegisterUser(ctx, update.MyChatMember.From, update.MyChatMember.Chat)
if err != nil {
continue
}

go func() {
err := s.checkAvatar(ctx, update.MyChatMember.From.ID)
err := s.checkUpdates(ctx, update.MyChatMember.From.ID, update.MyChatMember.Chat.ID)
if err != nil {
log.Println(err)
}
Expand All @@ -127,6 +128,8 @@ func (s TelegramBot) Start() error {
if err != nil {
log.Println(err)
}
} else if update.MyChatMember.NewChatMember.Status == "kicked" {
// TODO: remove notify channel
}
}

Expand Down Expand Up @@ -162,7 +165,7 @@ func (s TelegramBot) Start() error {
return nil
}

func (s TelegramBot) checkAndRegisterUser(ctx context.Context, tgUser tgbotapi.User) error {
func (s TelegramBot) checkAndRegisterUser(ctx context.Context, tgUser tgbotapi.User, tgChat tgbotapi.Chat) error {
userSocialID := authPkg.SocialID(null.NewString(strconv.Itoa(int(tgUser.ID)), true))

auth, err := s.container.Auth.Get(ctx, authPkg.MethodTelegram, userSocialID)
Expand All @@ -172,7 +175,7 @@ func (s TelegramBot) checkAndRegisterUser(ctx context.Context, tgUser tgbotapi.U
}
}
if auth == nil {
err = s.register(ctx, tgUser)
err = s.register(ctx, tgUser, tgChat)
if err != nil {
return err
}
Expand All @@ -181,7 +184,7 @@ func (s TelegramBot) checkAndRegisterUser(ctx context.Context, tgUser tgbotapi.U
return nil
}

func (s TelegramBot) checkAvatar(ctx context.Context, tgUserID int64) error {
func (s TelegramBot) checkUpdates(ctx context.Context, tgUserID, tgChatID int64) error {
userSocialID := authPkg.SocialID(null.NewString(strconv.Itoa(int(tgUserID)), true))
auth, err := s.container.Auth.Get(ctx, authPkg.MethodTelegram, userSocialID)
if err != nil {
Expand All @@ -200,6 +203,16 @@ func (s TelegramBot) checkAvatar(ctx context.Context, tgUserID int64) error {
if err != nil {
return err
}
if user.NotifyType == nil {
tgType := notify.TypeTelegram
channelID := strconv.Itoa(int(tgChatID))
user.NotifyType = &tgType
user.NotifyChannelID = &channelID
err = s.container.User.Update(ctx, user)
if err != nil {
return err
}
}
wishlists, err := s.container.Wishlist.GetByUserID(ctx, user.ID)
if err != nil {
return err
Expand All @@ -216,7 +229,7 @@ func (s TelegramBot) checkAvatar(ctx context.Context, tgUserID int64) error {
return nil
}

func (s TelegramBot) register(ctx context.Context, tgUser tgbotapi.User) error {
func (s TelegramBot) register(ctx context.Context, tgUser tgbotapi.User, tgChat tgbotapi.Chat) error {
createAuthTransaction, err := s.container.Auth.MakeCreateTransaction(ctx)
if err != nil {
return err
Expand All @@ -228,9 +241,13 @@ func (s TelegramBot) register(ctx context.Context, tgUser tgbotapi.User) error {
}
}(createAuthTransaction)

notifyTg := notify.TypeTelegram
notifyChannel := strconv.Itoa(int(tgChat.ID))
user := &userPkg.User{
FullName: tgUser.FirstName + " " + tgUser.LastName,
Language: userPkg.Language(tgUser.LanguageCode),
FullName: tgUser.FirstName + " " + tgUser.LastName,
Language: userPkg.Language(tgUser.LanguageCode),
NotifyType: &notifyTg,
NotifyChannelID: &notifyChannel,
}

err = s.container.User.Create(ctx, user)
Expand Down
1 change: 1 addition & 0 deletions container/services.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ type subscribeService interface {
type userService interface {
Create(ctx context.Context, user *userPkg.User) error
Get(ctx context.Context, userID userPkg.ID) (*userPkg.User, error)
Update(ctx context.Context, user *userPkg.User) error
}

type wishlistService interface {
Expand Down
7 changes: 7 additions & 0 deletions pkg/notify/notify.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package notify

type Type string

const (
TypeTelegram Type = "telegram"
)
5 changes: 5 additions & 0 deletions pkg/user/service/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,11 @@ func (s *Service) Create(ctx context.Context, user *userPkg.User) error {
return s.storage.Upsert(ctx, user)
}

func (s *Service) Update(ctx context.Context, user *userPkg.User) error {
user.UpdatedAt = time.Now().UTC()
return s.storage.Upsert(ctx, user)
}

func (s *Service) Get(ctx context.Context, id userPkg.ID) (*userPkg.User, error) {
return s.storage.Get(ctx, id)
}
64 changes: 45 additions & 19 deletions pkg/user/storage/postgres/postgres.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,19 @@ import (
"context"
"database/sql"
"errors"
"github.com/grulex/go-wishlist/pkg/notify"
userPkg "github.com/grulex/go-wishlist/pkg/user"
"github.com/jmoiron/sqlx"
"time"
)

type userPersistent struct {
ID string `db:"id"`
FullName string `db:"full_name"`
CreatedAt time.Time `db:"created_at"`
Language string `db:"lang"`
ID string `db:"id"`
FullName string `db:"full_name"`
Language string `db:"lang"`
NotifyType *string `db:"notify_type"`
NotifyChannelID *string `db:"notify_channel_id"`
CreatedAt time.Time `db:"created_at"`
}

type Storage struct {
Expand All @@ -24,44 +27,67 @@ func NewUserStorage(db *sqlx.DB) *Storage {
return &Storage{db: db}
}

func (s *Storage) Upsert(_ context.Context, u *userPkg.User) error {
func (s *Storage) Upsert(ctx context.Context, u *userPkg.User) error {
query := `INSERT INTO users (
id,
fullname,
created_at,
lang
lang,
notify_type,
notify_channel_id
) VALUES (
:id,
:full_name,
:created_at,
:lang
:lang,
:notify_type,
:notify_channel_id
) ON CONFLICT (id) DO UPDATE SET
fullname = :full_name,
lang = :lang`
lang = :lang,
notify_type = :notify_type,
notify_channel_id = :notify_channel_id`

var notifyType *string
if u.NotifyType != nil {
typeString := string(*u.NotifyType)
notifyType = &typeString
}

userPersistent := userPersistent{
ID: string(u.ID),
FullName: u.FullName,
CreatedAt: u.CreatedAt,
Language: string(u.Language),
ID: string(u.ID),
FullName: u.FullName,
CreatedAt: u.CreatedAt,
Language: string(u.Language),
NotifyType: notifyType,
NotifyChannelID: u.NotifyChannelID,
}
_, err := s.db.NamedExec(query, userPersistent)
_, err := s.db.NamedExecContext(ctx, query, userPersistent)
return err
}

func (s *Storage) Get(_ context.Context, id userPkg.ID) (*userPkg.User, error) {
func (s *Storage) Get(ctx context.Context, id userPkg.ID) (*userPkg.User, error) {
query := `SELECT * FROM users WHERE id = $1`
userPersistent := userPersistent{}
err := s.db.Get(&userPersistent, query, string(id))
err := s.db.GetContext(ctx, &userPersistent, query, string(id))
if err != nil {
if errors.Is(err, sql.ErrNoRows) {
return nil, userPkg.ErrNotFound
}
return nil, err
}

var notifyType *notify.Type
if userPersistent.NotifyType != nil {
typedType := notify.Type(*userPersistent.NotifyType)
notifyType = &typedType
}
return &userPkg.User{
ID: userPkg.ID(userPersistent.ID),
FullName: userPersistent.FullName,
CreatedAt: userPersistent.CreatedAt,
Language: userPkg.Language(userPersistent.Language),
ID: userPkg.ID(userPersistent.ID),
FullName: userPersistent.FullName,
CreatedAt: userPersistent.CreatedAt,
Language: userPkg.Language(userPersistent.Language),
NotifyType: notifyType,
NotifyChannelID: userPersistent.NotifyChannelID,
}, nil
}
12 changes: 8 additions & 4 deletions pkg/user/user.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package user

import (
"errors"
"github.com/grulex/go-wishlist/pkg/notify"
"time"
)

Expand All @@ -11,8 +12,11 @@ type ID string
type Language string

type User struct {
ID ID
FullName string
CreatedAt time.Time
Language Language
ID ID
FullName string
Language Language
NotifyType *notify.Type
NotifyChannelID *string
CreatedAt time.Time
UpdatedAt time.Time
}

0 comments on commit 4429cfc

Please sign in to comment.