Skip to content

Commit

Permalink
Revert "Scheduled telegram notifications. (#48)"
Browse files Browse the repository at this point in the history
This reverts commit c781e6f.
  • Loading branch information
ice-myles committed Jun 27, 2024
1 parent 0edaff2 commit 5cd30a9
Show file tree
Hide file tree
Showing 39 changed files with 488 additions and 1,113 deletions.
7 changes: 0 additions & 7 deletions application.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -119,13 +119,6 @@ notifications: &notifications
replicaURLs:
- postgresql://root:pass@localhost:5434/husky
tenantName: BogusName
tokenName: BN
wintr/notifications/telegram:
botToken: bogus
telegramBots:
bogusBot:
botToken: bogus
webAppLink: https://bogus.com/startapp
messageBroker: &notificationsMessageBroker
consumerGroup: husky-local-notifications-testing
createTopics: true
Expand Down
4 changes: 2 additions & 2 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ require (
github.com/bits-and-blooms/bitset v1.13.0 // indirect
github.com/bsm/redislock v0.9.4 // indirect
github.com/btcsuite/btcd/btcec/v2 v2.3.3 // indirect
github.com/bytedance/sonic v1.11.9 // indirect
github.com/bytedance/sonic v1.11.8 // indirect
github.com/bytedance/sonic/loader v0.1.1 // indirect
github.com/cenkalti/backoff/v4 v4.3.0 // indirect
github.com/cespare/xxhash/v2 v2.3.0 // indirect
Expand Down Expand Up @@ -98,7 +98,7 @@ require (
github.com/google/pprof v0.0.0-20240625030939-27f56978b8b0 // 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
github.com/googleapis/gax-go/v2 v2.12.4 // indirect
github.com/gorilla/websocket v1.5.3 // indirect
github.com/hashicorp/errwrap v1.1.0 // indirect
github.com/hashicorp/go-version v1.7.0 // indirect
Expand Down
8 changes: 4 additions & 4 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -56,8 +56,8 @@ github.com/btcsuite/btcd/btcec/v2 v2.3.3 h1:6+iXlDKE8RMtKsvK0gshlXIuPbyWM/h84Ens
github.com/btcsuite/btcd/btcec/v2 v2.3.3/go.mod h1:zYzJ8etWJQIv1Ogk7OzpWjowwOdXY1W/17j2MW85J04=
github.com/btcsuite/btcd/chaincfg/chainhash v1.0.1 h1:q0rUy8C/TYNBQS1+CGKw68tLOFYSNEs0TFnxxnS9+4U=
github.com/btcsuite/btcd/chaincfg/chainhash v1.0.1/go.mod h1:7SFka0XMvUgj3hfZtydOrQY2mwhPclbT2snogU7SQQc=
github.com/bytedance/sonic v1.11.9 h1:LFHENlIY/SLzDWverzdOvgMztTxcfcF+cqNsz9pK5zg=
github.com/bytedance/sonic v1.11.9/go.mod h1:LysEHSvpvDySVdC2f87zGWf6CIKJcAvqab1ZaiQtds4=
github.com/bytedance/sonic v1.11.8 h1:Zw/j1KfiS+OYTi9lyB3bb0CFxPJVkM17k1wyDG32LRA=
github.com/bytedance/sonic v1.11.8/go.mod h1:LysEHSvpvDySVdC2f87zGWf6CIKJcAvqab1ZaiQtds4=
github.com/bytedance/sonic/loader v0.1.1 h1:c+e5Pt1k/cy5wMveRDyk2X4B9hF4g7an8N3zCYjJFNM=
github.com/bytedance/sonic/loader v0.1.1/go.mod h1:ncP89zfokxS5LZrJxl5z0UJcsk4M4yY2JpfqGeCtNLU=
github.com/cenkalti/backoff/v4 v4.3.0 h1:MyRJ/UdXutAwSAT+s3wNd7MfTIcy71VQueUuFK343L8=
Expand Down Expand Up @@ -257,8 +257,8 @@ github.com/google/uuid v1.6.0 h1:NIvaJDMOsjHA8n1jAhLSgzrAzy1Hgr+hNrb57e+94F0=
github.com/google/uuid v1.6.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo=
github.com/googleapis/enterprise-certificate-proxy v0.3.2 h1:Vie5ybvEvT75RniqhfFxPRy3Bf7vr3h0cechB90XaQs=
github.com/googleapis/enterprise-certificate-proxy v0.3.2/go.mod h1:VLSiSSBs/ksPL8kq3OBOQ6WRI2QnaFynd1DCjZ62+V0=
github.com/googleapis/gax-go/v2 v2.12.5 h1:8gw9KZK8TiVKB6q3zHY3SBzLnrGp6HQjyfYBYGmXdxA=
github.com/googleapis/gax-go/v2 v2.12.5/go.mod h1:BUDKcWo+RaKq5SC9vVYL0wLADa3VcfswbOMMRmB9H3E=
github.com/googleapis/gax-go/v2 v2.12.4 h1:9gWcmF85Wvq4ryPFvGFaOgPIs1AQX0d0bcbGw4Z96qg=
github.com/googleapis/gax-go/v2 v2.12.4/go.mod h1:KYEYLorsnIGDi/rPC8b5TdlB9kbKoFubselGIoBMCwI=
github.com/gorilla/mux v1.8.0 h1:i40aqfkR1h2SlN9hojwV5ZA91wcXFOvkdNIeFDP5koI=
github.com/gorilla/mux v1.8.0/go.mod h1:DVbg23sWSpFRCP0SfiEN6jmj59UnW/n46BH5rLB71So=
github.com/gorilla/websocket v1.5.3 h1:saDtZ6Pbx/0u+bgYQ3q96pZgCzfhKXGPqt7kZ72aNNg=
Expand Down
6 changes: 1 addition & 5 deletions notifications/DDL.sql
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,8 @@ CREATE TABLE IF NOT EXISTS users (
profile_picture_name TEXT,
referred_by TEXT,
phone_number_hash TEXT,
telegram_user_id TEXT NOT NULL,
telegram_bot_id TEXT NOT NULL,
language TEXT NOT NULL default 'en'
);
ALTER TABLE users ADD COLUMN IF NOT EXISTS telegram_user_id text NOT NULL;
ALTER TABLE users ADD COLUMN IF NOT EXISTS telegram_bot_id text NOT NULL;
--************************************************************************************************************************************
-- sent_notifications
CREATE TABLE IF NOT EXISTS sent_notifications (
Expand Down Expand Up @@ -49,7 +45,7 @@ CREATE TABLE IF NOT EXISTS scheduled_notifications (
notification_channel_value TEXT NOT NULL,
primary key(user_id,uniqueness,notification_type,notification_channel,notification_channel_value));
CREATE UNIQUE INDEX IF NOT EXISTS scheduled_notifications_i_ix ON scheduled_notifications (i);
CREATE INDEX IF NOT EXISTS scheduled_notifications_mod_i_ix ON scheduled_notifications (MOD(i, %[1]v), scheduled_for, notification_channel ASC);
CREATE INDEX IF NOT EXISTS scheduled_notifications_mod_i_ix ON scheduled_notifications (MOD(i, %[1]v), scheduled_for ASC);
CREATE INDEX IF NOT EXISTS scheduled_notifications_user_id_notification_type_ix ON scheduled_notifications (user_id,notification_type);
--************************************************************************************************************************************
-- sent_announcements
Expand Down
97 changes: 26 additions & 71 deletions notifications/contract.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ import (
"github.com/ice-blockchain/wintr/multimedia/picture"
"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"
)

Expand All @@ -29,7 +28,6 @@ const (
SMSNotificationChannel NotificationChannel = "sms"
EmailNotificationChannel NotificationChannel = "email"
PushNotificationChannel NotificationChannel = "push"
TelegramNotificationChannel NotificationChannel = "telegram"
PushOrFallbackToEmailNotificationChannel NotificationChannel = "push||email"
)

Expand Down Expand Up @@ -84,7 +82,6 @@ var (
SMSNotificationChannel,
EmailNotificationChannel,
PushNotificationChannel,
TelegramNotificationChannel,
}
//nolint:gochecknoglobals // It's just for more descriptive validation messages.
AllNotificationTypes = users.Enum[NotificationType]{
Expand Down Expand Up @@ -112,24 +109,6 @@ var (
WeeklyStatsNotificationType,
}
//nolint:gochecknoglobals // It's just for more descriptive validation messages.
AllTelegramNotificationTypes = users.Enum[NotificationType]{
LevelBadgeUnlockedNotificationType,
CoinBadgeUnlockedNotificationType,
SocialBadgeUnlockedNotificationType,
RoleChangedNotificationType,
LevelChangedNotificationType,
MiningExtendNotificationType,
MiningEndingSoonNotificationType,
MiningExpiredNotificationType,
MiningNotActiveNotificationType,
InviteFriendNotificationType,
SocialsFollowIceOnXNotificationType,
SocialsFollowUsOnXNotificationType,
SocialsFollowZeusOnXNotificationType,
SocialsFollowIONOnTelegramNotificationType,
SocialsFollowOurTelegramNotificationType,
}
//nolint:gochecknoglobals // It's just for more descriptive validation messages.
AllNotificationDomains = map[NotificationChannel][]NotificationDomain{
EmailNotificationChannel: {
DisableAllNotificationDomain,
Expand Down Expand Up @@ -192,19 +171,14 @@ type (
CheckHealth(ctx context.Context) error
}
Scheduler struct {
pictureClient picture.Client
pushNotificationsClient push.Client
telegramNotificationsClient telegram.Client
cfg *config
schedulerPushAnnouncementsMX *sync.Mutex
schedulerPushNotificationsMX *sync.Mutex
schedulerTelegramNotificationsMX *sync.Mutex
telemetryPushNotifications *telemetry
telemetryTelegramNotifications *telemetry
telemetryAnnouncements *telemetry
db *storage.DB
wg *sync.WaitGroup
cancel context.CancelFunc
pictureClient picture.Client
pushNotificationsClient push.Client
cfg *config
schedulerAnnouncementsMX *sync.Mutex
schedulerNotificationsMX *sync.Mutex
telemetryNotifications *telemetry
telemetryAnnouncements *telemetry
db *storage.DB
}
)

Expand All @@ -215,8 +189,8 @@ const (
requestingUserIDCtxValueKey = "requestingUserIDCtxValueKey"
requestDeadline = 30 * stdlibtime.Second

schedulerWorkersCount int64 = 10
schedulerPushBatchSize int64 = 250
schedulerWorkersCount int64 = 10
schedulerBatchSize int64 = 250
)

var (
Expand All @@ -227,18 +201,14 @@ var (
//nolint:gochecknoglobals // Its loaded once at startup.
allPushNotificationTemplates map[NotificationType]map[languageCode]*pushNotificationTemplate
//nolint:gochecknoglobals // Its loaded once at startup.
allTelegramNotificationTemplates map[NotificationType]map[languageCode]*telegramNotificationTemplate
//nolint:gochecknoglobals // Its loaded once at startup.
internationalizedEmailDisplayNames = map[string]string{
"en": "ice: Decentralized Future",
}
)

type (
languageCode = string
telegramBotID = string
telegramBotToken = string
user struct {
languageCode = string
user struct {
LastPingCooldownEndedAt *time.Time `json:"lastPingCooldownEndedAt,omitempty"`
DisabledPushNotificationDomains *users.Enum[NotificationDomain] `json:"disabledPushNotificationDomains,omitempty"`
DisabledEmailNotificationDomains *users.Enum[NotificationDomain] `json:"disabledEmailNotificationDomains,omitempty"`
Expand All @@ -253,8 +223,6 @@ type (
ReferredBy string `json:"referredBy,omitempty"`
PhoneNumberHash string `json:"phoneNumberHash,omitempty"`
Language string `json:"language,omitempty"`
TelegramUserID string `json:"telegramUserId,omitempty"`
TelegramBotID string `json:"telegramBotId,omitempty"`
AgendaContactUserIDs []string `json:"agendaContactUserIDs,omitempty" db:"agenda_contact_user_ids"`
}
userTableSource struct {
Expand Down Expand Up @@ -291,16 +259,15 @@ type (
*processor
}
repository struct {
cfg *config
shutdown func() error
db *storage.DB
mb messagebroker.Client
pushNotificationsClient push.Client
telegramNotificationsClient telegram.Client
emailClient email.Client
pictureClient picture.Client
personalInAppFeed inapp.Client
globalInAppFeed inapp.Client
cfg *config
shutdown func() error
db *storage.DB
mb messagebroker.Client
pushNotificationsClient push.Client
emailClient email.Client
pictureClient picture.Client
personalInAppFeed inapp.Client
globalInAppFeed inapp.Client
}
processor struct {
*repository
Expand All @@ -310,8 +277,6 @@ type (
MaxNotificationDelaySec uint `yaml:"maxNotificationDelaySec"`
}
scheduledNotificationInfo struct {
TelegramUserID string
TelegramBotID string
PushNotificationTokens *users.Enum[push.DeviceToken]
DisabledPushNotificationDomains *users.Enum[NotificationDomain]
scheduledNotification
Expand Down Expand Up @@ -343,14 +308,8 @@ type (
UserID string
Token push.DeviceToken
}
telegramNotification struct {
tn *telegram.Notification
sn *sentNotification
scheduled scheduledNotification
}
config struct {
TenantName string `yaml:"tenantName"`
TokenName string `yaml:"tokenName"`
Socials []struct {
NotificationType string `yaml:"notificationType"`
Link string `yaml:"link"`
Expand All @@ -361,15 +320,11 @@ type (
Roles []string `yaml:"roles"`
} `yaml:"disabledAchievementsNotifications" `
NotificationDelaysByTopic map[push.SubscriptionTopic]notificationDelayConfig `yaml:"notificationDelaysByTopic" mapstructure:"notificationDelaysByTopic"` //nolint:lll // .
TelegramBots map[telegramBotID]struct {
BotToken telegramBotToken `yaml:"botToken"`
} `yaml:"telegramBots" mapstructure:"telegramBots"`
DeeplinkScheme string `yaml:"deeplinkScheme"`
WebAppLink string `yaml:"webAppLink"`
messagebroker.Config `mapstructure:",squash"` //nolint:tagliatelle // Nope.
notificationDelayConfig `mapstructure:",squash"`
PingCooldown stdlibtime.Duration `yaml:"pingCooldown"`
WeeklyStats struct {
DeeplinkScheme string `yaml:"deeplinkScheme"`
messagebroker.Config `mapstructure:",squash"` //nolint:tagliatelle // Nope.
notificationDelayConfig `mapstructure:",squash"`
PingCooldown stdlibtime.Duration `yaml:"pingCooldown"`
WeeklyStats struct {
Weekday stdlibtime.Weekday `yaml:"weekday"`
Hour int `yaml:"hour"`
Minutes int `yaml:"minutes"`
Expand Down
14 changes: 6 additions & 8 deletions notifications/notification_invite_friend.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,34 +27,32 @@ 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 // .
for _, channel := range availableChannels {
scheduled = append(scheduled, &scheduledNotification{
scheduled := []*scheduledNotification{
{
ScheduledAt: now,
ScheduledFor: time.New(us.CreatedAt.Add(firstNotificationDuration)),
Language: us.Language,
UserID: us.ID,
NotificationType: string(InviteFriendNotificationType),
Uniqueness: fmt.Sprintf("%v_%v_1h", us.ID, InviteFriendNotificationType),
NotificationChannel: string(channel),
NotificationChannel: string(PushNotificationChannel),
NotificationChannelValue: us.ID,
Data: &users.JSON{
"TenantName": r.cfg.TenantName,
},
}, &scheduledNotification{
}, {
ScheduledAt: now,
ScheduledFor: time.New(us.CreatedAt.Add(7 * dayDuration)), //nolint:gomnd,mnd // .
Language: us.Language,
UserID: us.ID,
NotificationType: string(InviteFriendNotificationType),
Uniqueness: fmt.Sprintf("%v_%v_7d", us.ID, InviteFriendNotificationType),
NotificationChannel: string(channel),
NotificationChannel: string(PushNotificationChannel),
NotificationChannelValue: us.ID,
Data: &users.JSON{
"TenantName": r.cfg.TenantName,
},
})
},
}

return errors.Wrapf(r.insertScheduledNotifications(ctx, scheduled), "can't execute insertScheduledNotifications:%#v", scheduled)
Expand Down
Loading

0 comments on commit 5cd30a9

Please sign in to comment.