gear-orders/orders.py

78 lines
2.9 KiB
Python
Raw Normal View History

2025-11-14 04:03:20 +00:00
import logging
import datetime
from util import make_session
from generate import generate_order, generate_punishment
from db import Database
from mastodon import Mastodon
from settings import MASTODON_USERNAME, ORDER_TIMEOUT
logger = logging.getLogger(__name__)
async def order_issue():
orders_info = generate_order()
if 'orders' not in orders_info:
logger.info("No orders for today")
return
post = "Here are today's orders for @%s - \n\n" % MASTODON_USERNAME
if "count" in orders_info and orders_info['count'] > 1:
post += f"These are the same orders from the last {orders_info['count']} days\n\n"
post += "\n".join(orders_info['orders'])
async with make_session() as session:
m = Mastodon(session)
status = await m.statusPost(post)
db = Database()
db.order_status_put(
status['id'],
status['created_at'],
post
)
async def order_check():
async with make_session() as session:
m = Mastodon(session)
db = Database()
outstanding_orders = db.order_status_outstanding()
for outstanding_order in outstanding_orders:
context = await m.statusContext(outstanding_order['mastodon_id'])
confirmed_at = None
for d in context['descendants']:
if (
d['in_reply_to_id'] == outstanding_order['mastodon_id'] and
d['account']['username'] == MASTODON_USERNAME and
len(d['media_attachments']) > 0
):
confirmed_at = d['created_at']
db.order_status_confirm(outstanding_order['id'], confirmed_at)
logger.info('Confirmed order %s' % (outstanding_order['created_at']))
break
if confirmed_at is None:
logger.info('Order %s remains unconfirmed' % (outstanding_order['created_at']))
oo_created_at = datetime.datetime.fromisoformat(outstanding_order['created_at'])
if(oo_created_at + ORDER_TIMEOUT < datetime.datetime.now(datetime.UTC)):
logger.info('Time to issue a punishment for %s' % outstanding_order['created_at'])
punishment = generate_punishment()
post = "@%s has failed to post proof of compliance. Punishment is as follows -\n\n" % MASTODON_USERNAME
post += "\n".join(punishment)
punishment_status = await m.statusPost(
post,
in_reply_to_id=outstanding_order['mastodon_id']
)
db.punishment_status_put(
outstanding_order['id'],
punishment_status['id'],
punishment_status['created_at'],
post
)