Skip to content

Commit

Permalink
Disabled telegram notifications except socials (new) and new_referral…
Browse files Browse the repository at this point in the history
… (new). Disabled replies. Fixed telegram long polling getUpdates limit arg.
  • Loading branch information
ice-myles committed Jul 11, 2024
1 parent 46b6b79 commit 988171f
Show file tree
Hide file tree
Showing 32 changed files with 239 additions and 161 deletions.
18 changes: 10 additions & 8 deletions application.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -90,16 +90,16 @@ notifications: &notifications
wintr/multimedia/picture:
urlDownload: https://ice-staging.b-cdn.net
socials:
- notificationType: follow_ion_on_x
link: https://x.com/ice_blockchain/status/1743221098349535361
- notificationType: follow_us_on_x
link: https://x.com/ice_blockchain/status/1793988190614823165
- notificationType: follow_zeus_on_x
link: https://x.com/ice_blockchain/status/1793256299519418521
- notificationType: join_ion_on_telegram
link: https://x.com/ice_blockchain/status/1793256299519418521
link: https://x.com/sunwaves_token
- notificationType: join_our_telegram
link: https://x.com/ice_blockchain/status/1793256299519418521
link: 'https://t.me/sunwavestoken'
- notificationType: follow_ion_on_x
link: https://x.com/ice_blockchain
- notificationType: join_ion_on_telegram
link: 'https://t.me/iceblockchain'
- notificationType: follow_zeus_on_x
link: https://x.com/ice_z3us
weeklyStats:
weekday: 1
hour: 10
Expand All @@ -118,6 +118,8 @@ notifications: &notifications
password: pass
replicaURLs:
- postgresql://root:pass@localhost:5434/husky
wintr/connectors/storage/v3:
url: redis://default:@localhost:6379
tenantName: BogusName
tokenName: BN
wintr/notifications/telegram:
Expand Down
6 changes: 3 additions & 3 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ require (
github.com/goccy/go-json v0.10.3
github.com/google/uuid v1.6.0
github.com/hashicorp/go-multierror v1.1.1
github.com/ice-blockchain/eskimo v1.368.0
github.com/ice-blockchain/freezer v1.485.0
github.com/ice-blockchain/eskimo v1.369.0
github.com/ice-blockchain/freezer v1.488.0
github.com/ice-blockchain/go-tarantool-client v0.0.0-20230327200757-4fc71fa3f7bb
github.com/ice-blockchain/wintr v1.144.0
github.com/imroc/req/v3 v3.43.7
Expand Down Expand Up @@ -95,7 +95,7 @@ require (
github.com/golang-jwt/jwt/v5 v5.2.1 // indirect
github.com/golang/groupcache v0.0.0-20210331224755-41bb18bfe9da // indirect
github.com/golang/protobuf v1.5.4 // indirect
github.com/google/pprof v0.0.0-20240625030939-27f56978b8b0 // indirect
github.com/google/pprof v0.0.0-20240711041743-f6c9dda6c6da // indirect
github.com/google/s2a-go v0.1.7 // indirect
github.com/googleapis/enterprise-certificate-proxy v0.3.2 // indirect
github.com/googleapis/gax-go/v2 v2.12.5 // indirect
Expand Down
12 changes: 6 additions & 6 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -247,8 +247,8 @@ github.com/google/gofuzz v1.2.0 h1:xRy4A+RhZaiKjJ1bPfwQ8sedCA+YS2YcCHW6ec7JMi0=
github.com/google/gofuzz v1.2.0/go.mod h1:dBl0BpW6vV/+mYPU4Po3pmUjxk6FQPldtuIdl/M65Eg=
github.com/google/martian/v3 v3.3.3 h1:DIhPTQrbPkgs2yJYdXU/eNACCG5DVQjySNRNlflZ9Fc=
github.com/google/martian/v3 v3.3.3/go.mod h1:iEPrYcgCF7jA9OtScMFQyAlZZ4YXTKEtJ1E6RWzmBA0=
github.com/google/pprof v0.0.0-20240625030939-27f56978b8b0 h1:e+8XbKB6IMn8A4OAyZccO4pYfB3s7bt6azNIPE7AnPg=
github.com/google/pprof v0.0.0-20240625030939-27f56978b8b0/go.mod h1:K1liHPHnj73Fdn/EKuT8nrFqBihUSKXoLYU0BuatOYo=
github.com/google/pprof v0.0.0-20240711041743-f6c9dda6c6da h1:xRmpO92tb8y+Z85iUOMOicpCfaYcv7o3Cg3wKrIpg8g=
github.com/google/pprof v0.0.0-20240711041743-f6c9dda6c6da/go.mod h1:K1liHPHnj73Fdn/EKuT8nrFqBihUSKXoLYU0BuatOYo=
github.com/google/s2a-go v0.1.7 h1:60BLSyTrOV4/haCDW4zb1guZItoSq8foHCXrAnjBo/o=
github.com/google/s2a-go v0.1.7/go.mod h1:50CgR4k1jNlWBu4UfS4AcfhVe1r6pdZPygJ3R8F0Qdw=
github.com/google/subcommands v1.2.0/go.mod h1:ZjhPrFU+Olkh9WazFPsl27BQ4UPiG37m3yTrtFlrHVk=
Expand Down Expand Up @@ -282,10 +282,10 @@ github.com/holiman/uint256 v1.3.0 h1:4wdcm/tnd0xXdu7iS3ruNvxkWwrb4aeBQv19ayYn8F4
github.com/holiman/uint256 v1.3.0/go.mod h1:EOMSn4q6Nyt9P6efbI3bueV4e1b3dGlUCXeiRV4ng7E=
github.com/huin/goupnp v1.3.0 h1:UvLUlWDNpoUdYzb2TCn+MuTWtcjXKSza2n6CBdQ0xXc=
github.com/huin/goupnp v1.3.0/go.mod h1:gnGPsThkYa7bFi/KWmEysQRf48l2dvR5bxr2OFckNX8=
github.com/ice-blockchain/eskimo v1.368.0 h1:4xougCqbB62CEvBRzfoqEzFa/6ZYf1OeKoa2EoV53f8=
github.com/ice-blockchain/eskimo v1.368.0/go.mod h1:OpR88RU/bYLmjugzyumwo/sx0fvG8B9pYsLZIy/DFgI=
github.com/ice-blockchain/freezer v1.485.0 h1:znX6LMcYFbOArD/zHvwm/bYvgAzyX6Z/aYFmrJet90Q=
github.com/ice-blockchain/freezer v1.485.0/go.mod h1:NmqNJ34CIRUHzBZ7Tgd42Ei30qKeSqflQh4PM/iW4M4=
github.com/ice-blockchain/eskimo v1.369.0 h1:DFguku6xR4rb8G1Ay4LAh0s6qzYPnoKsO28uFJGvAko=
github.com/ice-blockchain/eskimo v1.369.0/go.mod h1:l4MZKGo/Lpq+LFr65HUAGc/SvN4IclA0kpFqXjjQsZ8=
github.com/ice-blockchain/freezer v1.488.0 h1:Q3XGS16eDQ4zeSTqfYLLwAt+H3TbDda9hI+VhEfhTx0=
github.com/ice-blockchain/freezer v1.488.0/go.mod h1:bSwFbBC95BBF+/WrPJp+l9qyrGruim0pnTpAmkkWoLQ=
github.com/ice-blockchain/go-tarantool-client v0.0.0-20230327200757-4fc71fa3f7bb h1:8TnFP3mc7O+tc44kv2e0/TpZKnEVUaKH+UstwfBwRkk=
github.com/ice-blockchain/go-tarantool-client v0.0.0-20230327200757-4fc71fa3f7bb/go.mod h1:ZsQU7i3mxhgBBu43Oev7WPFbIjP4TniN/b1UPNGbrq8=
github.com/ice-blockchain/wintr v1.144.0 h1:YQE0olkPdSI6AOlw7r/j5jGI6uLciZQrvXFIkN4C4l4=
Expand Down
13 changes: 6 additions & 7 deletions notifications/contract.go
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,7 @@ const (
SocialsFollowOurTelegramNotificationType NotificationType = "join_our_telegram"
WeeklyStatsNotificationType NotificationType = "weekly_stats"
ReplyNotificationType NotificationType = "reply"
SocialsNotificationType NotificationType = "socials"
)

var (
Expand Down Expand Up @@ -125,11 +126,8 @@ var (
MiningExpiredNotificationType,
MiningNotActiveNotificationType,
InviteFriendNotificationType,
SocialsFollowIceOnXNotificationType,
SocialsFollowUsOnXNotificationType,
SocialsFollowZeusOnXNotificationType,
SocialsFollowIONOnTelegramNotificationType,
SocialsFollowOurTelegramNotificationType,
NewReferralNotificationType,
SocialsNotificationType,
ReplyNotificationType,
}
//nolint:gochecknoglobals // It's just for more descriptive validation messages.
Expand Down Expand Up @@ -218,8 +216,9 @@ const (
requestingUserIDCtxValueKey = "requestingUserIDCtxValueKey"
requestDeadline = 30 * stdlibtime.Second

schedulerWorkersCount int64 = 10
schedulerPushBatchSize int64 = 250
schedulerWorkersCount int64 = 10
schedulerPushBatchSize int64 = 250
telegramLongPollingLimit int64 = 100

defaultLanguage = "en"
// Protection from getting ErrDuplicate on session creation due to ReferralsCountChangeGuardUpdatedAt on freezer.
Expand Down
4 changes: 2 additions & 2 deletions notifications/notification_invite_friend.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,8 @@ func (r *repository) addScheduledInviteFriendNotifications(ctx context.Context,
dayDuration = 24 * stdlibtime.Hour
firstNotificationDuration = 1 * stdlibtime.Hour
}
availableChannels := []NotificationChannel{PushNotificationChannel, TelegramNotificationChannel}
scheduled := make([]*scheduledNotification, 0, 2*len(availableChannels)) //nolint:gomnd,mnd // .
availableChannels := []NotificationChannel{PushNotificationChannel}
scheduled := make([]*scheduledNotification, 0, len(availableChannels))
for _, channel := range availableChannels {
scheduled = append(scheduled, &scheduledNotification{
ScheduledAt: now,
Expand Down
8 changes: 4 additions & 4 deletions notifications/notification_type_badge_unlocked.go
Original file line number Diff line number Diff line change
Expand Up @@ -137,8 +137,8 @@ func (s *achievedBadgesSource) Process(ctx context.Context, msg *messagebroker.M
return errors.Wrapf(s.sendInAppNotification(ctx, in), "failed to sendInAppNotification for %v, notif:%#v", notifType, in)
})
}
if tokens.TelegramBotID != "" && tokens.TelegramBotID != tokens.UserID && //nolint:nestif // .
tokens.TelegramUserID != "" && tokens.TelegramUserID != tokens.UserID {
if false && (tokens.TelegramBotID != "" && tokens.TelegramBotID != tokens.UserID && //nolint:revive,nestif // .
tokens.TelegramUserID != "" && tokens.TelegramUserID != tokens.UserID) {
tmplTelegram, found := allTelegramNotificationTemplates[notifType][tokens.Language]
if !found {
log.Warn(fmt.Sprintf("language `%v` was not found in the `%v` telegram config", tokens.Language, notifType))
Expand All @@ -162,8 +162,8 @@ func (s *achievedBadgesSource) Process(ctx context.Context, msg *messagebroker.M
},
},
}
buttonText := tmplTelegram.getButtonText(nil)
buttonLink := getTelegramDeeplink(notifType, s.cfg, "", "")
buttonText := tmplTelegram.getButtonText(nil, 0)
buttonLink := getTelegramDeeplink(notifType, s.cfg, "", "", "")
if buttonText != "" && buttonLink != "" {
tn.tn.Buttons = append(tn.tn.Buttons, struct {
Text string `json:"text,omitempty"`
Expand Down
8 changes: 4 additions & 4 deletions notifications/notification_type_level_changed.go
Original file line number Diff line number Diff line change
Expand Up @@ -115,8 +115,8 @@ func (s *completedLevelsSource) Process(ctx context.Context, msg *messagebroker.
return errors.Wrapf(s.sendInAppNotification(ctx, in), "failed to sendInAppNotification for %v, notif:%#v", LevelChangedNotificationType, in)
})
}
if tokens.TelegramBotID != "" && tokens.TelegramBotID != tokens.UserID && //nolint:nestif // .
tokens.TelegramUserID != "" && tokens.TelegramUserID != tokens.UserID {
if false && (tokens.TelegramBotID != "" && tokens.TelegramBotID != tokens.UserID && //nolint:revive,nestif // .
tokens.TelegramUserID != "" && tokens.TelegramUserID != tokens.UserID) {
if tmplTelegram, found := allTelegramNotificationTemplates[LevelChangedNotificationType][tokens.Language]; !found {
log.Warn(fmt.Sprintf("language `%v` was not found in the `%v` telegram config", tokens.Language, LevelChangedNotificationType))
} else { //nolint:dupl // .
Expand All @@ -139,8 +139,8 @@ func (s *completedLevelsSource) Process(ctx context.Context, msg *messagebroker.
},
},
}
buttonText := tmplTelegram.getButtonText(nil)
buttonLink := getTelegramDeeplink(LevelChangedNotificationType, s.cfg, "", "")
buttonText := tmplTelegram.getButtonText(nil, 0)
buttonLink := getTelegramDeeplink(LevelChangedNotificationType, s.cfg, "", "", "")
if buttonText != "" && buttonLink != "" {
tn.tn.Buttons = append(tn.tn.Buttons, struct {
Text string `json:"text,omitempty"`
Expand Down
4 changes: 2 additions & 2 deletions notifications/notification_type_mining.go
Original file line number Diff line number Diff line change
Expand Up @@ -106,8 +106,8 @@ func (m *miningSessionSource) insertMiningScheduledNotifications(ctx context.Con
dayDuration = 24 * stdlibtime.Hour
uniquenessTime = fmt.Sprintf("%v:%02d:%02d %02d:%02d:%02d", now.Year(), int(now.Month()), now.Day(), now.Hour(), 0, 0)
}
scheduled := make([]*scheduledNotification, 0, 2*notificationsNum) //nolint:gomnd,mnd // .
for _, channel := range []NotificationChannel{PushNotificationChannel, TelegramNotificationChannel} {
scheduled := make([]*scheduledNotification, 0, notificationsNum)
for _, channel := range []NotificationChannel{PushNotificationChannel} {
scheduled = append(scheduled, &scheduledNotification{
ScheduledAt: now,
ScheduledFor: message.ResettableStartingAt,
Expand Down
118 changes: 82 additions & 36 deletions notifications/notification_type_new_referral.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ package notifications
import (
"context"
"fmt"
"strings"

"github.com/hashicorp/go-multierror"
"github.com/pkg/errors"
Expand All @@ -16,10 +17,11 @@ import (
"github.com/ice-blockchain/wintr/log"
"github.com/ice-blockchain/wintr/notifications/inapp"
"github.com/ice-blockchain/wintr/notifications/push"
"github.com/ice-blockchain/wintr/notifications/telegram"
"github.com/ice-blockchain/wintr/time"
)

func (r *repository) sendNewReferralNotification(ctx context.Context, us *users.UserSnapshot) error { //nolint:funlen,gocyclo,revive,cyclop // .
func (r *repository) sendNewReferralNotification(ctx context.Context, us *users.UserSnapshot) error { //nolint:funlen,gocyclo,revive,cyclop,gocognit // .
if ctx.Err() != nil {
return errors.Wrap(ctx.Err(), "unexpected deadline")
}
Expand Down Expand Up @@ -66,19 +68,13 @@ func (r *repository) sendNewReferralNotification(ctx context.Context, us *users.
},
}
tokens, err := r.getPushNotificationTokens(ctx, MicroCommunityNotificationDomain, us.User.ReferredBy)
if err != nil || tokens == nil || tokens.PushNotificationTokens == nil || len(*tokens.PushNotificationTokens) == 0 {
if err != nil || tokens == nil {
return multierror.Append( //nolint:wrapcheck // .
err,
errors.Wrapf(r.sendInAppNotification(ctx, in), "failed to sendInAppNotification for %v, notif:%#v", NewReferralNotificationType, in),
).ErrorOrNil()
}
tmpl, found := allPushNotificationTemplates[NewReferralNotificationType][tokens.Language]
if !found {
log.Warn(fmt.Sprintf("language `%v` was not found in the `%v` push config", tokens.Language, NewReferralNotificationType))

return errors.Wrapf(r.sendInAppNotification(ctx, in), "failed to sendInAppNotification for %v, notif:%#v", NewReferralNotificationType, in)
}
pn := make([]*pushNotification, 0, len(*tokens.PushNotificationTokens))
var exConcurrently []func() error
data := struct {
Username, Coin string
Amount uint64
Expand All @@ -87,40 +83,90 @@ func (r *repository) sendNewReferralNotification(ctx context.Context, us *users.
Coin: r.cfg.TokenName,
Amount: r.getNewReferralCoinAmount(ctx, us.User.ReferredBy),
}
for _, token := range *tokens.PushNotificationTokens {
pn = append(pn, &pushNotification{
pn: &push.Notification[push.DeviceToken]{
Data: map[string]string{"deeplink": deeplink},
Target: token,
Title: tmpl.getTitle(data),
Body: tmpl.getBody(data),
ImageURL: us.User.ProfilePictureURL,
},
sn: &sentNotification{
SentAt: now,
Language: tokens.Language,
sentNotificationPK: sentNotificationPK{
UserID: us.User.ReferredBy,
Uniqueness: us.User.ID,
NotificationType: NewReferralNotificationType,
NotificationChannel: PushNotificationChannel,
NotificationChannelValue: string(token),
if tokens.PushNotificationTokens != nil && len(*tokens.PushNotificationTokens) != 0 {
tmpl, found := allPushNotificationTemplates[NewReferralNotificationType][tokens.Language]
if !found {
log.Warn(fmt.Sprintf("language `%v` was not found in the `%v` push config", tokens.Language, NewReferralNotificationType))

return errors.Wrapf(r.sendInAppNotification(ctx, in), "failed to sendInAppNotification for %v, notif:%#v", NewReferralNotificationType, in)
}
pn := make([]*pushNotification, 0, len(*tokens.PushNotificationTokens))
for _, token := range *tokens.PushNotificationTokens {
pn = append(pn, &pushNotification{
pn: &push.Notification[push.DeviceToken]{
Data: map[string]string{"deeplink": deeplink},
Target: token,
Title: tmpl.getTitle(data),
Body: tmpl.getBody(data),
ImageURL: us.User.ProfilePictureURL,
},
},
sn: &sentNotification{
SentAt: now,
Language: tokens.Language,
sentNotificationPK: sentNotificationPK{
UserID: us.User.ReferredBy,
Uniqueness: us.User.ID,
NotificationType: NewReferralNotificationType,
NotificationChannel: PushNotificationChannel,
NotificationChannelValue: string(token),
},
},
})
}
exConcurrently = append(exConcurrently, func() error {
return errors.Wrapf(runConcurrently(ctx, r.sendPushNotification, pn), "failed to sendPushNotifications atleast to some devices for %v, args:%#v", NewReferralNotificationType, pn) //nolint:lll // .
}, func() error {
return errors.Wrapf(r.sendInAppNotification(ctx, in), "failed to sendInAppNotification for %v, notif:%#v", NewReferralNotificationType, in)
})
}
if tokens.TelegramBotID != "" && tokens.TelegramBotID != tokens.UserID && //nolint:nestif // .
tokens.TelegramUserID != "" && tokens.TelegramUserID != tokens.UserID {
tmplTelegram, found := allTelegramNotificationTemplates[NewReferralNotificationType][tokens.Language]
if !found {
log.Warn(fmt.Sprintf("language `%v` was not found in the `%v` telegram config", tokens.Language, NewReferralNotificationType))
} else {
if botInfo, botFound := r.cfg.TelegramBots[strings.ToLower(tokens.TelegramBotID)]; botFound {
tn := &telegramNotification{
tn: &telegram.Notification{
ChatID: tokens.TelegramUserID,
Text: tmplTelegram.getBody(data),
BotToken: botInfo.BotToken,
},
sn: &sentNotification{
SentAt: now,
Language: tokens.Language,
sentNotificationPK: sentNotificationPK{
UserID: us.User.ReferredBy,
Uniqueness: us.User.ID,
NotificationType: NewReferralNotificationType,
NotificationChannel: TelegramNotificationChannel,
NotificationChannelValue: us.User.ID,
},
},
}
buttonText := tmplTelegram.getButtonText(nil, 0)
buttonLink := getTelegramDeeplink(NewReferralNotificationType, r.cfg, "", "", us.User.ID)
if buttonText != "" && buttonLink != "" {
tn.tn.Buttons = append(tn.tn.Buttons, struct {
Text string `json:"text,omitempty"`
URL string `json:"url,omitempty"`
}{
Text: buttonText,
URL: buttonLink,
})
}
exConcurrently = append(exConcurrently, func() error {
return errors.Wrapf(r.sendTelegramNotification(ctx, tn), "failed to send telegram notification for %v, notif:%#v", NewReferralNotificationType, in)
})
}
}
}

return errors.Wrap(executeConcurrently(func() error {
return errors.Wrapf(runConcurrently(ctx, r.sendPushNotification, pn), "failed to sendPushNotifications atleast to some devices for %v, args:%#v", NewReferralNotificationType, pn) //nolint:lll // .
}, func() error {
return errors.Wrapf(r.sendInAppNotification(ctx, in), "failed to sendInAppNotification for %v, notif:%#v", NewReferralNotificationType, in)
}), "failed to executeConcurrently")
return errors.Wrap(executeConcurrently(exConcurrently...), "failed to executeConcurrently")
}

func (r *repository) getNewReferralCoinAmount(ctx context.Context, referredBy string) uint64 {
const defaultNewReferralCoinAmount = 500.0
//nolint:gocritic,godot // TODO: Uncomment this asap!
// const defaultNewReferralCoinAmount = tokenomics.WelcomeBonusV2Amount
const defaultNewReferralCoinAmount = tokenomics.WelcomeBonusV2Amount
freezerInternalID, err := tokenomics.GetInternalID(ctx, r.freezerDB, referredBy)
if err != nil {
log.Error(errors.Wrapf(err, "failed to tokenomics.GetInternalID for referredBy: %v", referredBy))
Expand Down
8 changes: 4 additions & 4 deletions notifications/notification_type_role_changed.go
Original file line number Diff line number Diff line change
Expand Up @@ -115,8 +115,8 @@ func (s *enabledRolesSource) Process(ctx context.Context, msg *messagebroker.Mes
return errors.Wrapf(s.sendInAppNotification(ctx, in), "failed to sendInAppNotification for %v, notif:%#v", RoleChangedNotificationType, in)
})
}
if tokens.TelegramBotID != "" && tokens.TelegramBotID != tokens.UserID && //nolint:nestif // .
tokens.TelegramUserID != "" && tokens.TelegramUserID != tokens.UserID {
if false && (tokens.TelegramBotID != "" && tokens.TelegramBotID != tokens.UserID && //nolint:revive,nestif // .
tokens.TelegramUserID != "" && tokens.TelegramUserID != tokens.UserID) {
tmplTelegram, found := allTelegramNotificationTemplates[RoleChangedNotificationType][tokens.Language]
if !found {
log.Warn(fmt.Sprintf("language `%v` was not found in the `%v` telegram config", tokens.Language, RoleChangedNotificationType))
Expand All @@ -140,8 +140,8 @@ func (s *enabledRolesSource) Process(ctx context.Context, msg *messagebroker.Mes
},
},
}
buttonText := tmplTelegram.getButtonText(nil)
buttonLink := getTelegramDeeplink(LevelChangedNotificationType, s.cfg, "", "")
buttonText := tmplTelegram.getButtonText(nil, 0)
buttonLink := getTelegramDeeplink(LevelChangedNotificationType, s.cfg, "", "", "")
if buttonText != "" && buttonLink != "" {
tn.tn.Buttons = append(tn.tn.Buttons, struct {
Text string `json:"text,omitempty"`
Expand Down
Loading

0 comments on commit 988171f

Please sign in to comment.