~fnux/meta.sr.ht

meta.sr.ht/metasrht-daily -rwxr-xr-x 3.4 KiB
28f5dba0Timothée Floure auth: remove duplicated password reset log for LDAP backend 5 months ago
                                                                                
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
#!/usr/bin/env python3
from srht.config import cfg
from srht.database import DbSession
db = DbSession(cfg("meta.sr.ht", "connection-string"))
from metasrht.types import User, UserType, PaymentInterval
db.init()

import sys
from metasrht.audit import expire_audit_logs
from metasrht.email import send_email
from datetime import datetime, timedelta

print("Running daily cron")
print("Expiring old audit log entires")
expire_audit_logs()

if cfg("meta.sr.ht::billing", "enabled") == "yes":
    print("Running billing")
    from metasrht.billing import charge_user, ChargeResult
    users = (User.query
        .filter(User.payment_cents != 0)
        .filter(User.payment_due < datetime.utcnow())
    ).all()
    ncharges = 0
    for user in users:
        print(f"Billing ~{user.username} ({ncharges+1}/{len(users)})")
        result, error = charge_user(user)
        db.session.commit()
        encrypt_key = None
        if user.pgp_key:
            encrypt_key = user.pgp_key.key
        if result == ChargeResult.failed:
            send_email("payment-failed", user.email,
                    f"Action required: your {cfg('sr.ht', 'site-name')} payment was not processed",
                    headers={
                        "From": f"{cfg('mail', 'smtp-from')}",
                        "To": "{} <{}>".format(user.username ,user.email),
                        "Reply-To": f"{cfg('sr.ht', 'owner-name')} <{cfg('sr.ht', 'owner-email')}>",
                    },
                    encrypt_key=encrypt_key, user=user, reason=error)
        elif result == ChargeResult.success:
            amount = user.payment_cents
            if user.payment_interval == PaymentInterval.yearly:
                amount = amount * 10 # Apply yearly discount
            send_email("payment-success", user.email,
                    f"Your payment to {cfg('sr.ht', 'site-name')} was processed successfully",
                    headers={
                        "From": f"{cfg('mail', 'smtp-from')}",
                        "To": "{} <{}>".format(user.username ,user.email),
                        "Reply-To": f"{cfg('sr.ht', 'owner-name')} <{cfg('sr.ht', 'owner-email')}>",
                    },
                    encrypt_key=encrypt_key, user=user,
                    amount="${:.2f}".format(amount / 100),
                    interval=user.payment_interval.value)
        ncharges += 1

if cfg("meta.sr.ht::settings", "welcome-emails", default="no") == "yes":
    print("Sending welcome emails")
    users = (User.query
            .filter(User.welcome_emails == 0)
            .filter(User.user_type != UserType.unconfirmed)
            .filter(User.created < datetime.utcnow() + timedelta(days=-1))
        ).all()
    nsent = 0
    for user in users:
        encrypt_key = None
        if user.pgp_key:
            encrypt_key = user.pgp_key.key
        print(f"Sending to ~{user.username} ({nsent + 1}/{len(users)})")
        try:
            send_email("welcome", user.email, f"{cfg('sr.ht', 'site-name')} introduction",
                    headers={
                        "From": f"{cfg('mail', 'smtp-from')}",
                        "To": "{} <{}>".format(user.username ,user.email),
                        "Reply-To": f"{cfg('sr.ht', 'owner-name')} <{cfg('sr.ht', 'owner-email')}>",
                    },
                    encrypt_key=encrypt_key,
                    user=user)
        except:
            print("Failed!")
        user.welcome_emails += 1
        db.session.commit()
        nsent += 1