Skip to content

Commit

Permalink
feat: refactor telegram usernames (#5)
Browse files Browse the repository at this point in the history
* feat: refactor telegram usernames

* fix: fixed unsubscribing on Telegram

* chore: fix compile

* fix: remove notifier from the database correctly
  • Loading branch information
freak12techno committed Aug 3, 2023
1 parent ca5fd72 commit 7392302
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 10 deletions.
6 changes: 3 additions & 3 deletions pkg/database/database.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
9 changes: 8 additions & 1 deletion pkg/reporters/telegram/subscribe.go
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand All @@ -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 {
Expand Down
10 changes: 5 additions & 5 deletions pkg/reporters/telegram/telegram.go
Original file line number Diff line number Diff line change
Expand Up @@ -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(
"<a href=\"tg://user?id=%s\">%s</a>",
notifier.UserID,
notifier.UserName,
)
}
2 changes: 1 addition & 1 deletion pkg/reporters/telegram/unsubscribe.go
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down

0 comments on commit 7392302

Please sign in to comment.