meta.sr.ht/metasrht-daily

195 lines
6.5 KiB
Python
Executable File

#!/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, PGPKey
db.init()
import sys
import time
from metasrht.audit import expire_audit_logs
from metasrht.email import send_email
from datetime import datetime, timedelta
from string import Template
origin = cfg("meta.sr.ht", "origin")
owner_name = cfg("sr.ht", "owner-name")
owner_email = cfg("sr.ht", "owner-email")
site_name = cfg("sr.ht", "site-name")
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())
.filter(User.user_type != UserType.admin)
).all()
ncharges = 0
for user in users:
print(f"Billing ~{user.username} ({ncharges+1}/{len(users)})")
result, error = charge_user(user)
db.session.commit()
if result == ChargeResult.failed:
tmpl = Template("""Subject: Action required: your $site_name payment was not processed
Reply-To: $owner_name <$owner_email>
Hi ~$username! We attempted to charge the payment method we have
on file for your account today, but it failed for the following reason:
$reason
You can update your billing information here:
$root/billing
Please do this soon to avoid an interruption in service. If you have any
questions, feel free to reply to this email.
--
$owner_name
$site_name
""")
rendered = tmpl.substitute(**{
'site_name': site_name,
'owner_name': owner_name,
'owner_email': owner_email,
'username': user.username,
'reason': error,
'root': origin
})
send_email(user.email, rendered)
elif result == ChargeResult.success:
amount = user.payment_cents
if user.payment_interval == PaymentInterval.yearly:
amount = amount * 10 # Apply yearly discount
tmpl = Template("""Subject: Your payment to $site_name was processed successfully
Reply-To: $owner_name <$owner_email>
Hi ~$username! Your $interval payment was processed
successfully for $amount. You can download the invoice and view or
update your billing information here:
$root/billing
If you have any questions, feel free to reply to this email.
Thank you for your subscription!
--
$owner_name
$site_name
""")
rendered = tmpl.substitute(**{
'site_name': site_name,
'owner_name': owner_name,
'owner_email': owner_email,
'username': user.username,
'interval': user.payment_interval.value,
'amount': "${:.2f}".format(amount / 100),
'root': origin
})
send_email(user.email, rendered)
ncharges += 1
# XXX: Temporary hack to try and please Stripe
time.sleep(3)
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:
print(f"Sending to ~{user.username} ({nsent + 1}/{len(users)})")
try:
tmpl = Template("""Subject: $site_name introduction
Reply-To: $owner_name <$owner_email>
Welcome to $site_name, ~$username! I'm $owner_name, your
friendly neighborhood sysadmin. You can send me questions or feedback by
replying to this email, or emailing $owner_email at any time. There's
a human being on the other end.
Here are a few resources you can use to get started:
Announcements list: https://lists.sr.ht/~sircmpwn/sr.ht-announce
Community mailing list: https://lists.sr.ht/~sircmpwn/sr.ht-discuss
Documentation & tutorials: https://man.sr.ht
IRC support & chat: #sr.ht on irc.libera.chat
This is the only automated email you're going to get, so if you want to
get updates later, you'll have to subscribe to the sr.ht-announce list.
You can do this from your mail client by sending an email to
<~sircmpwn/sr.ht-announce+subscribe@lists.sr.ht>, or visit the URL
above and click subscribe.
Also: payment is optional during the alpha, but be aware that it will
become mandatory later. This service is funded by its users, not by
investors. If you wish to support the alpha now, you can set up billing
here: https://meta.sr.ht/billing/initial
Thanks for signing up, and let me know if you need anything!
--
$owner_name
$site_name
""")
rendered = tmpl.substitute(**{
'site_name': site_name,
'owner_name': owner_name,
'owner_email': owner_email,
'username': user.username
})
send_email(user.email, rendered)
except:
print("Failed!")
user.welcome_emails += 1
db.session.commit()
nsent += 1
if cfg("meta.sr.ht::settings", "key-expiration-emails", default="no") == "yes":
print("Sending key expiration emails")
keys = (PGPKey.query
.filter(PGPKey.expiration < datetime.utcnow() + timedelta(days=31))
.filter(PGPKey.expiration > datetime.utcnow() + timedelta(days=30))
).all()
nsent = 0
for key in keys:
user = key.user
print(f"Sending to ~{user.username} for key {key.id} ({nsent + 1}/{len(users)})")
try:
tmpl = Template("""Subject: Your PGP key uploaded on $site_name expires soon
Reply-To: $owner_name <$owner_email>
Hi ~$username!
This is a once-only reminder that the following PGP key associated with your
account will expire soon:
Fingerprint: $fingerprint
Expires: $expiration
--
$owner_name
$site_name
""")
rendered = tmpl.substitute(**{
'site_name': site_name,
'owner_name': owner_name,
'owner_email': owner_email,
'username': user.username,
'fingerprint': key.fingerprint_hex,
'expiration': key.expiration,
})
send_email(user.email, rendered)
except:
print("Failed")
nsent += 1