From 7392302088bc1511dbaece1331f2c0eb03f3723f Mon Sep 17 00:00:00 2001 From: Sergey <83376337+freak12techno@users.noreply.github.com> Date: Thu, 3 Aug 2023 22:02:19 +0300 Subject: [PATCH] feat: refactor telegram usernames (#5) * feat: refactor telegram usernames * fix: fixed unsubscribing on Telegram * chore: fix compile * fix: remove notifier from the database correctly --- pkg/database/database.go | 6 +++--- pkg/reporters/telegram/subscribe.go | 9 ++++++++- pkg/reporters/telegram/telegram.go | 10 +++++----- pkg/reporters/telegram/unsubscribe.go | 2 +- 4 files changed, 17 insertions(+), 10 deletions(-) diff --git a/pkg/database/database.go b/pkg/database/database.go index fac6aaf..1a93644 100644 --- a/pkg/database/database.go +++ b/pkg/database/database.go @@ -278,16 +278,16 @@ func (d *Database) RemoveNotifier( chain string, operatorAddress string, reporter constants.ReporterName, - notifier string, + userID string, ) error { d.MaybeMutexLock() defer d.MaybeMutexUnlock() _, err := d.client.Exec( - "DELETE FROM notifiers WHERE operator_address = $1 AND reporter = $2 AND notifier = $3 AND chain = $4", + "DELETE FROM notifiers WHERE operator_address = $1 AND reporter = $2 AND user_id = $3 AND chain = $4", operatorAddress, reporter, - notifier, + userID, chain, ) if err != nil { diff --git a/pkg/reporters/telegram/subscribe.go b/pkg/reporters/telegram/subscribe.go index 3c07d21..b7da5b3 100644 --- a/pkg/reporters/telegram/subscribe.go +++ b/pkg/reporters/telegram/subscribe.go @@ -18,6 +18,13 @@ func (reporter *Reporter) HandleSubscribe(c tele.Context) error { reporter.MetricsManager.LogReporterQuery(reporter.Config.Name, constants.TelegramReporterName, "subscribe") + username := c.Sender().Username + if username == "" { + username = c.Sender().FirstName + } else { + username = "@" + username + } + args := strings.Split(c.Text(), " ") if len(args) < 2 { return reporter.BotReply(c, html.EscapeString(fmt.Sprintf( @@ -41,7 +48,7 @@ func (reporter *Reporter) HandleSubscribe(c tele.Context) error { address, reporter.Name(), strconv.FormatInt(c.Sender().ID, 10), - c.Sender().Username, + username, ) if !added { diff --git a/pkg/reporters/telegram/telegram.go b/pkg/reporters/telegram/telegram.go index e84f7c2..9759a87 100644 --- a/pkg/reporters/telegram/telegram.go +++ b/pkg/reporters/telegram/telegram.go @@ -249,9 +249,9 @@ func (reporter *Reporter) SerializeNotifiers(notifiers []*types.Notifier) string } func (reporter *Reporter) SerializeNotifier(notifier *types.Notifier) string { - if strings.HasPrefix(notifier.UserName, "@") { - return notifier.UserName - } - - return "@" + notifier.UserName + return fmt.Sprintf( + "%s", + notifier.UserID, + notifier.UserName, + ) } diff --git a/pkg/reporters/telegram/unsubscribe.go b/pkg/reporters/telegram/unsubscribe.go index 4efca9c..1af445b 100644 --- a/pkg/reporters/telegram/unsubscribe.go +++ b/pkg/reporters/telegram/unsubscribe.go @@ -35,7 +35,7 @@ func (reporter *Reporter) HandleUnsubscribe(c tele.Context) error { )) } - removed := reporter.Manager.RemoveNotifier(address, reporter.Name(), c.Sender().Username) + removed := reporter.Manager.RemoveNotifier(address, reporter.Name(), fmt.Sprintf("%d", c.Sender().ID)) if !removed { return reporter.BotReply(c, "You are not subscribed to this validator's notifications")