2023-05-26 15:33:55 +02:00
|
|
|
import requests
|
|
|
|
from requests import RequestException
|
|
|
|
|
|
|
|
from app import config
|
2024-08-23 13:32:32 +02:00
|
|
|
from app.db import Session
|
2024-05-23 10:27:08 +02:00
|
|
|
from app.events.event_dispatcher import EventDispatcher
|
2024-06-07 15:36:18 +02:00
|
|
|
from app.events.generated.event_pb2 import EventContent, UserPlanChanged
|
2023-05-26 15:33:55 +02:00
|
|
|
from app.log import LOG
|
|
|
|
from app.models import User
|
|
|
|
|
|
|
|
|
|
|
|
def execute_subscription_webhook(user: User):
|
|
|
|
webhook_url = config.SUBSCRIPTION_CHANGE_WEBHOOK
|
|
|
|
if webhook_url is None:
|
|
|
|
return
|
|
|
|
subscription_end = user.get_active_subscription_end(
|
|
|
|
include_partner_subscription=False
|
|
|
|
)
|
|
|
|
sl_subscription_end = None
|
|
|
|
if subscription_end:
|
|
|
|
sl_subscription_end = subscription_end.timestamp
|
|
|
|
payload = {
|
|
|
|
"user_id": user.id,
|
|
|
|
"is_premium": user.is_premium(),
|
|
|
|
"active_subscription_end": sl_subscription_end,
|
|
|
|
}
|
|
|
|
try:
|
2023-05-31 14:19:43 +02:00
|
|
|
response = requests.post(webhook_url, json=payload, timeout=2)
|
2023-05-26 15:33:55 +02:00
|
|
|
if response.status_code == 200:
|
|
|
|
LOG.i("Sent request to subscription update webhook successfully")
|
|
|
|
else:
|
|
|
|
LOG.i(
|
2024-08-23 13:32:32 +02:00
|
|
|
f"Request to webhook failed with status {response.status_code}: {response.text}"
|
2023-05-26 15:33:55 +02:00
|
|
|
)
|
|
|
|
except RequestException as e:
|
2023-05-31 14:19:43 +02:00
|
|
|
LOG.error(f"Subscription request exception: {e}")
|
2024-05-23 10:27:08 +02:00
|
|
|
|
2024-06-07 15:36:18 +02:00
|
|
|
event = UserPlanChanged(plan_end_time=sl_subscription_end)
|
2024-05-23 10:27:08 +02:00
|
|
|
EventDispatcher.send_event(user, EventContent(user_plan_change=event))
|
2024-08-23 13:32:32 +02:00
|
|
|
Session.commit()
|