95 lines
3.1 KiB
Go
95 lines
3.1 KiB
Go
|
|
package main
|
||
|
|
|
||
|
|
import (
|
||
|
|
"database/sql"
|
||
|
|
"fmt"
|
||
|
|
"time"
|
||
|
|
)
|
||
|
|
|
||
|
|
// SeedNotifications creates notifications and notification_preferences.
|
||
|
|
func SeedNotifications(db *sql.DB, cfg Config, users []SeededUser) error {
|
||
|
|
fmt.Println("\n═══ NOTIFICATIONS ═══")
|
||
|
|
|
||
|
|
// ── 1. Notifications ─────────────────────────────────────────────────────
|
||
|
|
p := NewProgress("notifications", cfg.Notifications)
|
||
|
|
notifRows := make([][]interface{}, 0, cfg.Notifications)
|
||
|
|
|
||
|
|
notifTypes := []string{
|
||
|
|
"follow", "like", "comment", "message", "system",
|
||
|
|
"milestone", "track_upload", "mention", "playlist_follow",
|
||
|
|
}
|
||
|
|
|
||
|
|
notifTemplates := map[string][2]string{
|
||
|
|
"follow": {"New follower", "Someone started following you"},
|
||
|
|
"like": {"Track liked", "Someone liked your track"},
|
||
|
|
"comment": {"New comment", "New comment on your track"},
|
||
|
|
"message": {"New message", "You have a new message"},
|
||
|
|
"system": {"System", "Welcome to Veza!"},
|
||
|
|
"milestone": {"Milestone", "Your track reached 100 plays!"},
|
||
|
|
"track_upload": {"New release", "An artist you follow uploaded a new track"},
|
||
|
|
"mention": {"Mentioned", "You were mentioned in a comment"},
|
||
|
|
"playlist_follow": {"Playlist followed", "Someone followed your playlist"},
|
||
|
|
}
|
||
|
|
|
||
|
|
for i := 0; i < cfg.Notifications; i++ {
|
||
|
|
user := users[rng.Intn(len(users))]
|
||
|
|
ntype := pick(notifTypes)
|
||
|
|
tmpl := notifTemplates[ntype]
|
||
|
|
createdAt := RandomTimeAfter(user.CreatedAt)
|
||
|
|
createdAt = RealisticHour(createdAt)
|
||
|
|
|
||
|
|
// 70% read, 30% unread
|
||
|
|
isRead := randChance(70)
|
||
|
|
var readAt interface{}
|
||
|
|
if isRead {
|
||
|
|
readAt = RandomTimeAfter(createdAt)
|
||
|
|
}
|
||
|
|
|
||
|
|
notifRows = append(notifRows, []interface{}{
|
||
|
|
newUUID(), user.ID, ntype, tmpl[0], tmpl[1],
|
||
|
|
nil, isRead, createdAt, createdAt, readAt,
|
||
|
|
})
|
||
|
|
}
|
||
|
|
|
||
|
|
_, err := BulkInsert(db, "notifications",
|
||
|
|
"id, user_id, type, title, content, link, read, created_at, updated_at, read_at",
|
||
|
|
notifRows)
|
||
|
|
if err != nil {
|
||
|
|
return fmt.Errorf("insert notifications: %w", err)
|
||
|
|
}
|
||
|
|
p.Update(cfg.Notifications)
|
||
|
|
p.Done()
|
||
|
|
|
||
|
|
// ── 2. Notification preferences ──────────────────────────────────────────
|
||
|
|
// Give ~30% of users custom notification preferences
|
||
|
|
prefCount := len(users) * 30 / 100
|
||
|
|
p = NewProgress("notification_preferences", prefCount)
|
||
|
|
prefRows := make([][]interface{}, 0, prefCount)
|
||
|
|
npSeen := make(map[string]bool)
|
||
|
|
|
||
|
|
for i := 0; i < prefCount && i < len(users); i++ {
|
||
|
|
user := users[rng.Intn(len(users))]
|
||
|
|
if npSeen[user.ID] {
|
||
|
|
continue
|
||
|
|
}
|
||
|
|
npSeen[user.ID] = true
|
||
|
|
prefRows = append(prefRows, []interface{}{
|
||
|
|
user.ID,
|
||
|
|
randChance(80), // push_follow
|
||
|
|
randChance(80), // push_like
|
||
|
|
randChance(80), // push_comment
|
||
|
|
randChance(90), // push_message
|
||
|
|
randChance(70), // push_mention
|
||
|
|
time.Now(), time.Now(),
|
||
|
|
})
|
||
|
|
}
|
||
|
|
|
||
|
|
_, _ = BulkInsert(db, "notification_preferences",
|
||
|
|
"user_id, push_follow, push_like, push_comment, push_message, push_mention, created_at, updated_at",
|
||
|
|
prefRows)
|
||
|
|
p.Update(len(prefRows))
|
||
|
|
p.Done()
|
||
|
|
|
||
|
|
return nil
|
||
|
|
}
|