gitscope
https://dev.kronoz.win/r/marzneshin.git
app/tasks/review_users.py · v0.7.3
Source
1 9a6b0fa Dawsh 2y ago import asyncio
2 f7af2b3 Dawsh 2y ago import logging
3 d377c4c Dawsh 2y ago from datetime import datetime, timedelta
4 a47eb4c Dawsh 2y ago from typing import TYPE_CHECKING
5 a47eb4c Dawsh 2y ago
6 f7af2b3 Dawsh 2y ago from app import marznode
7 01cdf2e Dawsh 2y ago from app.db import (
8 01cdf2e Dawsh 2y ago GetDB,
9 01cdf2e Dawsh 2y ago get_users,
10 01cdf2e Dawsh 2y ago )
11 8440155 mahdim10 1y ago from app.models.notification import UserNotification
12 d377c4c Dawsh 2y ago from app.models.user import (
13 d377c4c Dawsh 2y ago UserResponse,
14 d377c4c Dawsh 2y ago UserExpireStrategy,
15 f1ab16c Dawsh 2y ago )
16 8440155 mahdim10 1y ago from app.notification import notify
17 a47eb4c Dawsh 2y ago
18 a47eb4c Dawsh 2y ago if TYPE_CHECKING:
19 d377c4c Dawsh 2y ago pass
20 a47eb4c Dawsh 2y ago
21 a47eb4c Dawsh 2y ago
22 f7af2b3 Dawsh 2y ago logger = logging.getLogger(__name__)
23 f7af2b3 Dawsh 2y ago
24 f7af2b3 Dawsh 2y ago
25 f7af2b3 Dawsh 2y ago async def review_users():
26 a47eb4c Dawsh 2y ago now = datetime.utcnow()
27 9a6b0fa Dawsh 2y ago with GetDB() as db:
28 d377c4c Dawsh 2y ago for user in get_users(db, activated=True, is_active=False):
29 d377c4c Dawsh 2y ago """looking for expired/to be limited users who are still active"""
30 d377c4c Dawsh 2y ago
31 d377c4c Dawsh 2y ago marznode.operations.update_user(user, remove=True)
32 d377c4c Dawsh 2y ago user.activated = False
33 d377c4c Dawsh 2y ago db.commit()
34 d377c4c Dawsh 2y ago db.refresh(user)
35 8440155 mahdim10 1y ago
36 d377c4c Dawsh 2y ago asyncio.ensure_future(
37 8440155 mahdim10 1y ago notify(
38 8440155 mahdim10 1y ago action=UserNotification.Action.user_deactivated,
39 8440155 mahdim10 1y ago user=UserResponse.model_validate(user),
40 01cdf2e Dawsh 2y ago )
41 01cdf2e Dawsh 2y ago )
42 a47eb4c Dawsh 2y ago
43 d377c4c Dawsh 2y ago logger.info(
44 d377c4c Dawsh 2y ago "User `%s` activation state changed to `%s`",
45 d377c4c Dawsh 2y ago user.username,
46 d377c4c Dawsh 2y ago str(user.activated),
47 d377c4c Dawsh 2y ago )
48 a47eb4c Dawsh 2y ago
49 d377c4c Dawsh 2y ago for user in get_users(
50 d377c4c Dawsh 2y ago db,
51 d377c4c Dawsh 2y ago expire_strategy=UserExpireStrategy.START_ON_FIRST_USE,
52 d377c4c Dawsh 2y ago is_active=True,
53 d377c4c Dawsh 2y ago ):
54 d377c4c Dawsh 2y ago """looking for to be activated, on hold users"""
55 d377c4c Dawsh 2y ago base_time = user.edit_at or user.created_at
56 d377c4c Dawsh 2y ago
57 d377c4c Dawsh 2y ago # Check if the user is online After or at 'base_time' or...
58 d377c4c Dawsh 2y ago # If the user didn't connect until activation_deadline; change status to "Active"
59 d377c4c Dawsh 2y ago if not (
60 d377c4c Dawsh 2y ago (user.online_at and base_time <= user.online_at)
61 d377c4c Dawsh 2y ago or (
62 d377c4c Dawsh 2y ago user.activation_deadline
63 d377c4c Dawsh 2y ago and (user.activation_deadline <= now)
64 d377c4c Dawsh 2y ago )
65 d377c4c Dawsh 2y ago ):
66 a47eb4c Dawsh 2y ago continue
67 a47eb4c Dawsh 2y ago
68 d377c4c Dawsh 2y ago user.expire_date = datetime.utcnow() + timedelta(
69 d377c4c Dawsh 2y ago seconds=user.usage_duration
70 d377c4c Dawsh 2y ago )
71 d377c4c Dawsh 2y ago user.expire_strategy = UserExpireStrategy.FIXED_DATE
72 d377c4c Dawsh 2y ago db.commit()
73 d377c4c Dawsh 2y ago db.refresh(user)
74 d377c4c Dawsh 2y ago logger.info("on hold user `%s` has been activated", user.username)