-
Notifications
You must be signed in to change notification settings - Fork 25
/
sats4ads.go
567 lines (495 loc) · 14.5 KB
/
sats4ads.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
package main
import (
"context"
"database/sql"
"errors"
"fmt"
"strconv"
"strings"
"time"
"github.com/docopt/docopt-go"
"github.com/fiatjaf/lntxbot/t"
tgbotapi "github.com/go-telegram-bot-api/telegram-bot-api"
"github.com/rs/zerolog"
)
const SATS4ADSUNACTIVITYDATEFORMAT = "20060102"
type Sats4AdsData struct {
On bool `json:"on"`
Rate int `json:"rate"` // in msatoshi per character
Banned bool `json:"banned,omitempty"`
}
type Sats4AdsRateGroup struct {
NUsers int `db:"nusers"`
UpToRate int `db:"uptorate"` // in msatoshi per character
}
func handleSats4Ads(ctx context.Context, u *User, opts docopt.Opts) {
switch {
case opts["rate"].(bool):
rate, _ := getSats4AdsRate(u)
send(ctx, u, strconv.Itoa(rate)+" msatoshi per character.")
break
case opts["on"].(bool):
rate, err := opts.Int("<msat_per_character>")
if err != nil {
rate = 1
}
if rate > 1000 {
send(ctx, u, t.ERROR, t.T{"App": "sats4ads", "Err": "max = 1000, msatoshi"})
return
}
go u.track("sats4ads on", map[string]interface{}{"rate": rate})
err = turnSats4AdsOn(u, rate)
if err != nil {
send(ctx, u, t.ERROR, t.T{"App": "sats4ads", "Err": err.Error()})
return
}
send(ctx, u, t.SATS4ADSTOGGLE, t.T{"On": true, "Sats": float64(rate) / 1000})
case opts["off"].(bool):
err := turnSats4AdsOff(u)
if err != nil {
send(ctx, u, t.ERROR, t.T{"App": "sats4ads", "Err": err.Error()})
return
}
go u.track("sats4ads off", nil)
send(ctx, u, t.SATS4ADSTOGGLE, t.T{"On": false})
case opts["rates"].(bool):
rates, err := getSats4AdsRates()
if err != nil {
send(ctx, u, t.ERROR, t.T{"App": "sats4ads", "Err": err.Error()})
return
}
go u.track("sats4ads rates", nil)
send(ctx, u, t.SATS4ADSPRICETABLE, t.T{"Rates": rates})
case opts["broadcast"].(bool):
// check user banned
var data Sats4AdsData
err := u.getAppData("sats4ads", &data)
if err != nil {
send(ctx, u, t.ERROR, t.T{"App": "sats4ads", "Err": err.Error()})
return
}
if data.Banned {
send(ctx, u, t.ERROR, t.T{"App": "sats4ads", "Err": "user, banned"})
return
}
msats, err := parseSatoshis(opts)
if err != nil {
send(ctx, u, t.ERROR, t.T{"App": "sats4ads", "Err": err.Error()})
return
}
satoshis := int(msats / 1000)
go u.track("sats4ads broadcast", map[string]interface{}{"sats": satoshis})
contentMessage := getContentMessage(ctx, opts)
if contentMessage == nil {
handleHelp(ctx, "sats4ads")
return
}
// optional args
maxrate, _ := opts.Int("--max-rate")
offset, _ := opts.Int("--skip")
send(ctx, t.SATS4ADSSTART, ctx.Value("message"))
go func() {
nmessagesSent, totalCost, errMsg, err := broadcastSats4Ads(ctx,
satoshis, contentMessage, maxrate, offset)
if err != nil {
log.Warn().Err(err).Stringer("user", u).
Msg("sats4ads broadcast fail")
send(ctx, u, t.ERROR, t.T{"App": "sats4ads", "Err": errMsg})
return
}
send(ctx, t.SATS4ADSBROADCAST, t.T{"NSent": nmessagesSent, "Sats": totalCost}, ctx.Value("message"))
}()
case opts["preview"].(bool):
go u.track("sats4ads preview", nil)
contentMessage := getContentMessage(ctx, opts)
if contentMessage == nil {
handleHelp(ctx, "sats4ads")
return
}
ad, _, _, _ := buildSats4AdsMessage(log, contentMessage, u, 0, nil)
if ad == nil {
send(ctx, u, t.ERROR, t.T{"App": "sats4ads", "Err": "invalid message used as ad content"})
return
}
bot.Send(ad)
}
}
func turnSats4AdsOn(user *User, rate int) error {
var data Sats4AdsData
err := user.getAppData("sats4ads", &data)
if err != nil {
return err
}
if data.Banned {
return errors.New("user banned")
}
data.On = true
data.Rate = rate
return user.setAppData("sats4ads", data)
}
func turnSats4AdsOff(user *User) error {
var data Sats4AdsData
err := user.getAppData("sats4ads", &data)
if err != nil {
return err
}
data.On = false
return user.setAppData("sats4ads", data)
}
func getSats4AdsRate(user *User) (rate int, err error) {
err = pg.Get(&rate, `
SELECT (appdata->'sats4ads'->>'rate')::integer
FROM account
WHERE id = $1
`, user.Id)
return
}
func getSats4AdsRates() (rates []Sats4AdsRateGroup, err error) {
err = pg.Select(&rates, `
WITH enabled_listeners AS (
SELECT (appdata->'sats4ads'->>'rate')::integer AS rate
FROM account
WHERE appdata->'sats4ads'->'on' = 'true'::jsonb
), rategroups AS (
SELECT generate_series ^ 3 AS uptorate FROM generate_series(1, 10)
)
SELECT uptorate, (SELECT count(*) FROM enabled_listeners WHERE rate <= uptorate) AS nusers
FROM rategroups
`)
return
}
func broadcastSats4Ads(
ctx context.Context,
budgetSatoshis int,
contentMessage *tgbotapi.Message,
maxrate int,
offset int,
) (messagesSent int, roundedCostSatoshis int, errMsg string, err error) {
user := ctx.Value("initiator").(*User)
costSatoshis := 0.0
if maxrate == 0 {
maxrate = 500
}
// decide on a unique hash for the source payment (so payments can be aggregated
// like Payer-3->Proxy, then Proxy-1->TargetA, Proxy-2->TargetB, Proxy-3->TargetC)
random, err := randomHex()
if err != nil {
return
}
sourcehash := hashString(random)
logger := log.With().Str("sourcehash", sourcehash).Int("budget", budgetSatoshis).Int("max", maxrate).Logger()
rows, err := pg.Queryx(`
SELECT id, (appdata->'sats4ads'->>'rate')::int AS rate
FROM account
WHERE appdata->'sats4ads'->'on' = 'true'::jsonb
AND id != $1
AND (appdata->'sats4ads'->>'rate')::integer <= $2
ORDER BY appdata->'sats4ads'->'rate' ASC, random()
OFFSET $3
`, user.Id, maxrate, offset)
if err != nil {
return
}
// send messages and queue receivers to be paid
for rows.Next() {
var row struct {
Id int `db:"id"`
Rate int `db:"rate"`
}
err = rows.StructScan(&row)
if err != nil {
return
}
// fetch the target user
var target *User
target, err = loadUser(row.Id)
if err != nil || target.TelegramChatId == 0 {
continue
}
// identifier for the received payment
// will be pending until the user clicks the "Viewed" button
targethash := hashString("%d:%s:%d",
contentMessage.MessageID, sourcehash, target.Id)
data := "s4a=v-" + targethash[:10]
// build ad message based on the message that was replied to
ad, nchars, thisCostMsat, thisCostSatoshis := buildSats4AdsMessage(
logger,
contentMessage, target, row.Rate,
tgbotapi.InlineKeyboardMarkup{
InlineKeyboard: [][]tgbotapi.InlineKeyboardButton{
{
tgbotapi.InlineKeyboardButton{
Text: translate(
context.WithValue(ctx, "locale", target.Locale),
t.SATS4ADSVIEWED,
),
CallbackData: &data,
},
},
},
},
)
if ad == nil {
break
}
if int(costSatoshis+thisCostSatoshis) > budgetSatoshis {
// budget ended, stop queueing messages
logger.Info().Float64("spent", costSatoshis).
Float64("next", thisCostSatoshis).
Msg("budget ended")
break
}
var message tgbotapi.Message
message, err = bot.Send(ad)
if err != nil {
// message wasn't sent
// logger.Info().Err(err).Msg("message wasn't sent. skipping.")
err = nil
continue
}
// commit payment (pending for receiver)
errMsg, err = user.sendThroughProxy(
ctx,
sourcehash,
targethash,
contentMessage.MessageID,
message.MessageID,
target,
thisCostMsat,
fmt.Sprintf("ad dispatched to %d", messagesSent+1),
fmt.Sprintf("%d characters ad (%s) at %d msat/char", nchars, sourcehash, row.Rate),
true, // pending
"sats4ads",
)
if err != nil {
logger.Error().Err(err).Msg("error saving proxied payment. abort all.")
return
}
// we will store this for 7 days so we can use this information on a task
// if someone fail to see an ad for more than 3 days they will be excluded
rds.SetNX(redisKeyUnviewedAd(
target.Id),
time.Now().Format(SATS4ADSUNACTIVITYDATEFORMAT),
time.Hour*24*7,
)
messagesSent += 1
costSatoshis += thisCostSatoshis
}
roundedCostSatoshis = int(costSatoshis)
return
}
func buildSats4AdsMessage(
logger zerolog.Logger,
contentMessage *tgbotapi.Message,
target *User,
rate int,
keyboard interface{},
) (ad tgbotapi.Chattable, nchars int, thisCostMsat int, thisCostSatoshis float64) {
ctx := context.WithValue(context.Background(), "locale", target.Locale)
thisCostMsat = 1000 // fixed 1sat fee for each message
baseChat := tgbotapi.BaseChat{
ChatID: target.TelegramChatId,
ReplyMarkup: keyboard,
}
switch {
case contentMessage.Text != "":
nchars = len(contentMessage.Text)
if strings.Index(contentMessage.Text, "https://") != -1 {
nchars += 300
}
thisCostMsat += rate * nchars
thisCostSatoshis = float64(thisCostMsat) / 1000
footer := "\n\n" + translateTemplate(ctx, t.SATS4ADSADFOOTER, t.T{
"Sats": thisCostSatoshis,
})
ad = tgbotapi.MessageConfig{
BaseChat: baseChat,
Text: contentMessage.Text + footer,
DisableWebPagePreview: false,
}
case contentMessage.Animation != nil:
nchars = 100 + len(contentMessage.Caption)
thisCostMsat += rate * nchars
thisCostSatoshis = float64(thisCostMsat) / 1000
footer := "\n\n" + translateTemplate(ctx, t.SATS4ADSADFOOTER, t.T{
"Sats": thisCostSatoshis,
})
ad = tgbotapi.AnimationConfig{
Caption: contentMessage.Caption + footer,
BaseFile: tgbotapi.BaseFile{
BaseChat: baseChat,
FileID: contentMessage.Animation.FileID,
UseExisting: true,
},
}
case contentMessage.Photo != nil:
nchars = 100 + len(contentMessage.Caption)
thisCostMsat += rate * nchars
thisCostSatoshis = float64(thisCostMsat) / 1000
footer := "\n\n" + translateTemplate(ctx, t.SATS4ADSADFOOTER, t.T{
"Sats": thisCostSatoshis,
})
photos := *contentMessage.Photo
ad = tgbotapi.PhotoConfig{
Caption: contentMessage.Caption + footer,
BaseFile: tgbotapi.BaseFile{
BaseChat: baseChat,
FileID: photos[0].FileID,
UseExisting: true,
},
}
case contentMessage.Video != nil:
nchars = 300 + len(contentMessage.Caption)
thisCostMsat += rate * nchars
thisCostSatoshis = float64(thisCostMsat) / 1000
footer := "\n\n" + translateTemplate(ctx, t.SATS4ADSADFOOTER, t.T{
"Sats": thisCostSatoshis,
})
ad = tgbotapi.VideoConfig{
Caption: contentMessage.Caption + footer,
BaseFile: tgbotapi.BaseFile{
BaseChat: baseChat,
FileID: contentMessage.Video.FileID,
UseExisting: true,
},
}
case contentMessage.Document != nil:
nchars = 200 + len(contentMessage.Caption)
thisCostMsat += rate * nchars
thisCostSatoshis = float64(thisCostMsat) / 1000
footer := "\n\n" + translateTemplate(ctx, t.SATS4ADSADFOOTER, t.T{
"Sats": thisCostSatoshis,
})
ad = tgbotapi.DocumentConfig{
Caption: contentMessage.Caption + footer,
BaseFile: tgbotapi.BaseFile{
BaseChat: baseChat,
FileID: contentMessage.Document.FileID,
UseExisting: true,
},
}
case contentMessage.Audio != nil:
nchars = 150 + len(contentMessage.Caption)
thisCostMsat += rate * nchars
thisCostSatoshis = float64(thisCostMsat) / 1000
footer := "\n\n" + translateTemplate(ctx, t.SATS4ADSADFOOTER, t.T{
"Sats": thisCostSatoshis,
})
ad = tgbotapi.AudioConfig{
Caption: contentMessage.Caption + footer,
BaseFile: tgbotapi.BaseFile{
BaseChat: baseChat,
FileID: contentMessage.Audio.FileID,
UseExisting: true,
},
}
default:
logger.Info().Msg("invalid message used as ad content")
}
return
}
func confirmAdViewed(user *User, hashfirst10chars string) {
_, err := pg.Exec(`
UPDATE lightning.transaction
SET pending = false
WHERE to_id = $1 AND payment_hash LIKE $2 || '%'
`, user.Id, hashfirst10chars)
if err != nil {
log.Warn().Err(err).Str("hash", hashfirst10chars).Stringer("user", user).
Msg("failed to mark sats4ads tx as not pending")
}
// user viewed (any) ad, so prevent unsubscribing him
rds.Del(redisKeyUnviewedAd(user.Id))
}
func cleanupUnviewedAds() {
ctx := context.WithValue(context.Background(), "origin", "background")
// for every person who has received an ad over 3 days ago and haven't seen it
// we will cancel that payment (which is pending) and remove that person from
// the sats4ads list
txn, err := pg.BeginTxx(ctx, &sql.TxOptions{})
if err != nil {
return
}
defer txn.Rollback()
var deletedReceiverIds []int
err = txn.Select(&deletedReceiverIds, `
WITH adsreceivedtxs AS (
SELECT to_id, amount, payment_hash, proxied_with FROM lightning.transaction
WHERE tag = 'sats4ads' AND time < (now() - interval '3 days') AND pending
), groupedbyproxy AS (
SELECT proxied_with, sum(amount) AS amount FROM adsreceivedtxs
GROUP BY proxied_with
), sourceupdates AS (
UPDATE lightning.transaction AS s
SET amount = s.amount - t.amount
FROM groupedbyproxy AS t
WHERE t.proxied_with = s.payment_hash
), deletes AS (
DELETE FROM lightning.transaction
WHERE payment_hash IN (SELECT payment_hash FROM adsreceivedtxs)
)
SELECT DISTINCT to_id FROM adsreceivedtxs
`)
if err != nil {
log.Warn().Err(err).Msg("failed to delete sats4ads pending tx")
return
}
// check proxy balance (should be always zero)
if err := checkProxyBalance(txn); err != nil {
log.Error().Err(err).Msg("proxy balance check on cleanupUnviewedAds")
return
}
err = txn.Commit()
if err != nil {
return
}
// for each deleted we check redis for sats4ads viewer inactivity and unsubscribe
threedaysago := time.Now().AddDate(0, 0, -3)
for _, receiverId := range deletedReceiverIds {
key := redisKeyUnviewedAd(receiverId)
if val, err := rds.Get(key).Result(); err == nil {
if rec, err := time.Parse(SATS4ADSUNACTIVITYDATEFORMAT, val); err == nil {
if rec.Before(threedaysago) {
if receiver, err := loadUser(receiverId); err == nil {
err = turnSats4AdsOff(receiver)
if err != nil {
log.Warn().Stringer("user", receiver).
Msg("failed to turn off sats4ads for inactive user")
continue
}
send(ctx, receiver, t.SATS4ADSTOGGLE, t.T{"On": false})
rds.Del(key)
}
}
}
}
}
}
func sats4adsCleanupRoutine() {
for {
cleanupUnviewedAds()
time.Sleep(time.Hour * 6)
}
}
func redisKeyUnviewedAd(userId int) string {
return fmt.Sprintf("sats4ads:unviewed:%d", userId)
}
func getContentMessage(ctx context.Context, opts docopt.Opts) *tgbotapi.Message {
// we'll use either a message passed as an argument...
var contentMessage *tgbotapi.Message
if imessage := ctx.Value("message"); imessage != nil {
if message, ok := imessage.(*tgbotapi.Message); ok {
contentMessage = message.ReplyToMessage
}
}
// or the contents of the message being replied to
if contentMessage == nil {
if itext, ok := opts["<text>"]; ok {
text := strings.Join(itext.([]string), " ")
if text != "" {
contentMessage = &tgbotapi.Message{Text: text}
}
}
}
return contentMessage
}