Phase 3: own referral system (sats)

- Referral links carry a random 8 character base32 code (rf_<code>)
  built from the bot username from getMe. Old ref_<id> links still work
  but are never shown.
- The reward (REFERRAL_REWARD_SATS, old name REFERRAL_REWARD still read)
  is paid through the ledger once the invited user's first purchase
  settles, once per invited user, never for self-referrals.
- REFERRAL_DAILY_CAP limits what an inviter earns per UTC day; rewards
  above it wait as pending and are paid on a later day by the billing
  loop.
- /referral shows invited, converted, earned and pending, with Share,
  Copy link and QR code buttons. The Mini App Me page mirrors it.
- Migration 00008: users.ref_code (unique) and referral_rewards,
  backfilled from earlier referral credits in the ledger.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
Michilis
2026-11-18 20:58:43 +00:00
co-authored by Claude Opus 5.5
parent 123cede7f3
commit f7ceadc462
30 changed files with 945 additions and 148 deletions
+19 -5
View File
@@ -219,7 +219,16 @@ func (m *MiniApp) me(w http.ResponseWriter, r *http.Request, s *session) {
}
st, _ := m.Billing.State(ctx, db.OwnerUser, s.user.ID)
usage, _ := m.Service.Usage(ctx, s.user.ID)
refs, _ := m.Store.CountReferrals(ctx, s.user.ID)
refStats, _ := m.Bot.ReferralStats(ctx, s.user.ID)
refLink, err := m.Bot.ReferralLink(ctx, s.user.ID)
if err != nil {
m.explain(w, s, err)
return
}
refQR := ""
if png, err := m.Bot.ReferralQR(ctx, s.user.ID); err == nil {
refQR = "data:image/png;base64," + base64.StdEncoding.EncodeToString(png)
}
var langs []map[string]string
for _, l := range m.I18n.Langs() {
if !m.I18n.Hidden(l) || m.Cfg.IsOwner(s.user.ID) {
@@ -247,9 +256,14 @@ func (m *MiniApp) me(w http.ResponseWriter, r *http.Request, s *session) {
DailyDownloads: dec.Limits.DailyDownloads, DailyBytes: dec.Limits.DailyBytes,
},
"payments": m.Billing.Enabled(),
"referral_link": "https://t.me/" + m.Bot.Username() + "?start=ref_" + strconv.FormatInt(s.user.ID, 10),
"referral_count": refs,
"referral_reward": m.Cfg.Prices.ReferralReward,
"referral_link": refLink,
"referral_count": refStats.Invited,
"referral_converted": refStats.Converted,
"referral_earned": refStats.Earned,
"referral_pending": refStats.Pending,
"referral_qr": refQR,
"referral_reward": m.Cfg.Prices.ReferralReward,
"referral_daily_cap": m.Cfg.ReferralDailyCap,
"support": support,
"start_param": s.init.StartParam,
"bot": m.Bot.Username(),
@@ -327,7 +341,7 @@ func (m *MiniApp) updateMe(w http.ResponseWriter, r *http.Request, s *session) {
// appTables are the catalog tables the Mini App reads. Its own texts are
// under "app"; names it shares with the chat (products, units, button and
// setting labels) are not written twice.
var appTables = []string{"app", "product", "unit", "btn", "settings", "quality", "language"}
var appTables = []string{"app", "product", "unit", "btn", "settings", "quality", "language", "referral"}
// messages returns the Mini App's texts in the user's language, from the
// same catalogs the bot uses. Keys keep their full name: "app.tab.home".