gitscope
https://dev.kronoz.win/r/marzneshin.git
app/db/crud.py · v0.3.0
Source
1 12b255b Dawsh 2y ago import json
2 34a7612 Dawsh 2y ago import secrets
3 f446990 Dawsh 2y ago from datetime import datetime, timedelta
4 a47eb4c Dawsh 2y ago from enum import Enum
5 2de9d72 Dawsh 2y ago from typing import List, Optional, Tuple, Union
6 a47eb4c Dawsh 2y ago
7 512d183 Mrdn 2y ago from sqlalchemy import and_, delete, update, select
8 a47eb4c Dawsh 2y ago from sqlalchemy.orm import Session
9 a47eb4c Dawsh 2y ago
10 01cdf2e Dawsh 2y ago from app.db.models import (
11 01cdf2e Dawsh 2y ago JWT,
12 01cdf2e Dawsh 2y ago TLS,
13 01cdf2e Dawsh 2y ago Admin,
14 01cdf2e Dawsh 2y ago Node,
15 01cdf2e Dawsh 2y ago NodeUsage,
16 01cdf2e Dawsh 2y ago NodeUserUsage,
17 01cdf2e Dawsh 2y ago NotificationReminder,
18 01cdf2e Dawsh 2y ago InboundHost,
19 01cdf2e Dawsh 2y ago Service,
20 01cdf2e Dawsh 2y ago Inbound,
21 01cdf2e Dawsh 2y ago System,
22 01cdf2e Dawsh 2y ago User,
23 01cdf2e Dawsh 2y ago )
24 a47eb4c Dawsh 2y ago from app.models.admin import AdminCreate, AdminModify, AdminPartialModify
25 01cdf2e Dawsh 2y ago from app.models.node import NodeCreate, NodeModify, NodeStatus, NodeUsageResponse
26 a47eb4c Dawsh 2y ago from app.models.proxy import InboundHost as InboundHostModify
27 2de9d72 Dawsh 2y ago from app.models.service import Service as ServiceModify, ServiceCreate
28 01cdf2e Dawsh 2y ago from app.models.user import (
29 01cdf2e Dawsh 2y ago ReminderType,
30 01cdf2e Dawsh 2y ago UserCreate,
31 01cdf2e Dawsh 2y ago UserDataLimitResetStrategy,
32 01cdf2e Dawsh 2y ago UserModify,
33 01cdf2e Dawsh 2y ago UserStatus,
34 01cdf2e Dawsh 2y ago UserUsageResponse,
35 01cdf2e Dawsh 2y ago )
36 01cdf2e Dawsh 2y ago from app.utils.helpers import calculate_expiration_days, calculate_usage_percent
37 a47eb4c Dawsh 2y ago from config import NOTIFY_DAYS_LEFT, NOTIFY_REACHED_USAGE_PERCENT
38 a47eb4c Dawsh 2y ago
39 a47eb4c Dawsh 2y ago
40 a47eb4c Dawsh 2y ago def add_default_hosts(db: Session, inbounds: List[Inbound]):
41 01cdf2e Dawsh 2y ago hosts = [
42 01cdf2e Dawsh 2y ago InboundHost(
43 01cdf2e Dawsh 2y ago remark="🚀 Marz ({USERNAME}) [{PROTOCOL} - {TRANSPORT}]",
44 01cdf2e Dawsh 2y ago address="{SERVER_IP}",
45 01cdf2e Dawsh 2y ago inbound=i,
46 01cdf2e Dawsh 2y ago )
47 01cdf2e Dawsh 2y ago for i in inbounds
48 01cdf2e Dawsh 2y ago ]
49 a47eb4c Dawsh 2y ago db.add_all(hosts)
50 a47eb4c Dawsh 2y ago db.commit()
51 a47eb4c Dawsh 2y ago
52 a47eb4c Dawsh 2y ago
53 a47eb4c Dawsh 2y ago def assure_node_inbounds(db: Session, inbounds: List[Inbound], node_id: int):
54 01cdf2e Dawsh 2y ago current_tags = [
55 01cdf2e Dawsh 2y ago i[0]
56 01cdf2e Dawsh 2y ago for i in db.execute(
57 01cdf2e Dawsh 2y ago select(Inbound.tag).filter(Inbound.node_id == node_id)
58 01cdf2e Dawsh 2y ago ).all()
59 01cdf2e Dawsh 2y ago ]
60 512d183 Mrdn 2y ago updated_tags = set(i.tag for i in list(inbounds))
61 512d183 Mrdn 2y ago inbound_additions, tag_deletions = list(), set()
62 512d183 Mrdn 2y ago for tag in current_tags:
63 512d183 Mrdn 2y ago if tag not in updated_tags:
64 512d183 Mrdn 2y ago tag_deletions.add(tag)
65 01cdf2e Dawsh 2y ago db_inbounds = db.query(Inbound).where(
66 01cdf2e Dawsh 2y ago and_(Inbound.node_id == node_id, Inbound.tag.in_(tag_deletions))
67 01cdf2e Dawsh 2y ago )
68 512d183 Mrdn 2y ago for i in db_inbounds:
69 512d183 Mrdn 2y ago db.delete(i)
70 512d183 Mrdn 2y ago
71 512d183 Mrdn 2y ago for inb in inbounds:
72 512d183 Mrdn 2y ago if inb.tag in current_tags:
73 01cdf2e Dawsh 2y ago stmt = (
74 01cdf2e Dawsh 2y ago update(Inbound)
75 01cdf2e Dawsh 2y ago .where(and_(Inbound.node_id == node_id, Inbound.tag == inb.tag))
76 01cdf2e Dawsh 2y ago .values(protocol=json.loads(inb.config)["protocol"], config=inb.config)
77 a47eb4c Dawsh 2y ago )
78 512d183 Mrdn 2y ago db.execute(stmt)
79 512d183 Mrdn 2y ago else:
80 512d183 Mrdn 2y ago inbound_additions.append(inb)
81 01cdf2e Dawsh 2y ago new_inbounds = [
82 01cdf2e Dawsh 2y ago Inbound(
83 01cdf2e Dawsh 2y ago tag=inb.tag,
84 01cdf2e Dawsh 2y ago protocol=json.loads(inb.config)["protocol"],
85 01cdf2e Dawsh 2y ago config=inb.config,
86 01cdf2e Dawsh 2y ago node_id=node_id,
87 01cdf2e Dawsh 2y ago )
88 01cdf2e Dawsh 2y ago for inb in inbound_additions
89 01cdf2e Dawsh 2y ago ]
90 a47eb4c Dawsh 2y ago db.add_all(new_inbounds)
91 a47eb4c Dawsh 2y ago db.flush()
92 a47eb4c Dawsh 2y ago for i in new_inbounds:
93 2de9d72 Dawsh 2y ago db.refresh(i)
94 a47eb4c Dawsh 2y ago add_default_hosts(db, new_inbounds)
95 a47eb4c Dawsh 2y ago db.commit()
96 a47eb4c Dawsh 2y ago
97 a47eb4c Dawsh 2y ago
98 01cdf2e Dawsh 2y ago def get_node_users(
99 01cdf2e Dawsh 2y ago db: Session, node_id: Optional[int], statuses: Optional[List[UserStatus]] = None
100 01cdf2e Dawsh 2y ago ):
101 01cdf2e Dawsh 2y ago query = (
102 01cdf2e Dawsh 2y ago db.query(User.id, User.username, User.key, Inbound)
103 01cdf2e Dawsh 2y ago .distinct()
104 d0c5ea4 Dawsh 2y ago .where(User.enabled == True)
105 01cdf2e Dawsh 2y ago .join(Inbound.services)
106 01cdf2e Dawsh 2y ago .join(Service.users)
107 01cdf2e Dawsh 2y ago .filter(Inbound.node_id == node_id)
108 01cdf2e Dawsh 2y ago )
109 12b255b Dawsh 2y ago if isinstance(statuses, list):
110 a47eb4c Dawsh 2y ago query = query.filter(User.status.in_(statuses))
111 a47eb4c Dawsh 2y ago return query.all()
112 a47eb4c Dawsh 2y ago
113 12b255b Dawsh 2y ago
114 a47eb4c Dawsh 2y ago def get_user_hosts(db: Session, user_id: int):
115 01cdf2e Dawsh 2y ago return (
116 01cdf2e Dawsh 2y ago db.query(InboundHost)
117 01cdf2e Dawsh 2y ago .distinct()
118 01cdf2e Dawsh 2y ago .join(User.services)
119 01cdf2e Dawsh 2y ago .join(Service.inbounds)
120 01cdf2e Dawsh 2y ago .join(Inbound.hosts)
121 01cdf2e Dawsh 2y ago .filter(User.id == user_id)
122 01cdf2e Dawsh 2y ago .all()
123 01cdf2e Dawsh 2y ago )
124 a47eb4c Dawsh 2y ago
125 12b255b Dawsh 2y ago
126 a47eb4c Dawsh 2y ago def get_inbound_hosts(db: Session, inbound_id: int) -> List[InboundHost]:
127 a47eb4c Dawsh 2y ago return db.query(InboundHost).filter(InboundHost.inbound_id == inbound_id).all()
128 a47eb4c Dawsh 2y ago
129 12b255b Dawsh 2y ago
130 a47eb4c Dawsh 2y ago def get_all_inbounds(db: Session):
131 a47eb4c Dawsh 2y ago return db.query(Inbound).all()
132 a47eb4c Dawsh 2y ago
133 a47eb4c Dawsh 2y ago
134 2de9d72 Dawsh 2y ago def get_inbound(db: Session, inbound_id: int) -> Inbound | None:
135 2de9d72 Dawsh 2y ago return db.query(Inbound).filter(Inbound.id == inbound_id).first()
136 a47eb4c Dawsh 2y ago
137 a47eb4c Dawsh 2y ago
138 2de9d72 Dawsh 2y ago def get_host(db: Session, host_id) -> InboundHost:
139 2de9d72 Dawsh 2y ago return db.query(InboundHost).filter(InboundHost.id == host_id).first()
140 a47eb4c Dawsh 2y ago
141 a47eb4c Dawsh 2y ago
142 a47eb4c Dawsh 2y ago def add_host(db: Session, inbound: Inbound, host: InboundHostModify):
143 2de9d72 Dawsh 2y ago host = InboundHost(
144 2de9d72 Dawsh 2y ago remark=host.remark,
145 2de9d72 Dawsh 2y ago address=host.address,
146 2de9d72 Dawsh 2y ago port=host.port,
147 2de9d72 Dawsh 2y ago path=host.path,
148 2de9d72 Dawsh 2y ago sni=host.sni,
149 2de9d72 Dawsh 2y ago host=host.host,
150 2de9d72 Dawsh 2y ago inbound=inbound,
151 2de9d72 Dawsh 2y ago security=host.security,
152 2de9d72 Dawsh 2y ago alpn=host.alpn,
153 01cdf2e Dawsh 2y ago fingerprint=host.fingerprint,
154 0b152b5 Mrdn 2y ago fragment=host.fragment.model_dump() if host.fragment else None,
155 0b152b5 Mrdn 2y ago mux=host.mux,
156 a47eb4c Dawsh 2y ago )
157 2de9d72 Dawsh 2y ago inbound.hosts.append(host)
158 a47eb4c Dawsh 2y ago db.commit()
159 2de9d72 Dawsh 2y ago db.refresh(host)
160 2de9d72 Dawsh 2y ago return host
161 2de9d72 Dawsh 2y ago
162 2de9d72 Dawsh 2y ago
163 2de9d72 Dawsh 2y ago def update_host(db: Session, db_host: InboundHost, host: InboundHostModify):
164 2de9d72 Dawsh 2y ago db_host.remark = host.remark
165 2de9d72 Dawsh 2y ago db_host.address = host.address
166 2de9d72 Dawsh 2y ago db_host.port = host.port
167 2de9d72 Dawsh 2y ago db_host.path = host.path
168 2de9d72 Dawsh 2y ago db_host.sni = host.sni
169 2de9d72 Dawsh 2y ago db_host.host = host.host
170 2de9d72 Dawsh 2y ago db_host.security = host.security
171 2de9d72 Dawsh 2y ago db_host.alpn = host.alpn
172 2de9d72 Dawsh 2y ago db_host.fingerprint = host.fingerprint
173 0b152b5 Mrdn 2y ago db_host.fragment = host.fragment.model_dump() if host.fragment else None
174 0b152b5 Mrdn 2y ago db_host.mux = host.mux
175 2de9d72 Dawsh 2y ago db.commit()
176 2de9d72 Dawsh 2y ago db.refresh(db_host)
177 2de9d72 Dawsh 2y ago return db_host
178 a47eb4c Dawsh 2y ago
179 a47eb4c Dawsh 2y ago
180 a47eb4c Dawsh 2y ago def get_user(db: Session, username: str):
181 a47eb4c Dawsh 2y ago return db.query(User).filter(User.username == username).first()
182 a47eb4c Dawsh 2y ago
183 a47eb4c Dawsh 2y ago
184 a47eb4c Dawsh 2y ago def get_user_by_id(db: Session, user_id: int):
185 a47eb4c Dawsh 2y ago return db.query(User).filter(User.id == user_id).first()
186 a47eb4c Dawsh 2y ago
187 a47eb4c Dawsh 2y ago
188 01cdf2e Dawsh 2y ago UsersSortingOptions = Enum(
189 01cdf2e Dawsh 2y ago "UsersSortingOptions",
190 01cdf2e Dawsh 2y ago {
191 01cdf2e Dawsh 2y ago "username": User.username.asc(),
192 01cdf2e Dawsh 2y ago "used_traffic": User.used_traffic.asc(),
193 01cdf2e Dawsh 2y ago "data_limit": User.data_limit.asc(),
194 01cdf2e Dawsh 2y ago "expire": User.expire.asc(),
195 01cdf2e Dawsh 2y ago "created_at": User.created_at.asc(),
196 01cdf2e Dawsh 2y ago "-username": User.username.desc(),
197 01cdf2e Dawsh 2y ago "-used_traffic": User.used_traffic.desc(),
198 01cdf2e Dawsh 2y ago "-data_limit": User.data_limit.desc(),
199 01cdf2e Dawsh 2y ago "-expire": User.expire.desc(),
200 01cdf2e Dawsh 2y ago "-created_at": User.created_at.desc(),
201 01cdf2e Dawsh 2y ago },
202 01cdf2e Dawsh 2y ago )
203 01cdf2e Dawsh 2y ago
204 01cdf2e Dawsh 2y ago
205 01cdf2e Dawsh 2y ago def get_users(
206 01cdf2e Dawsh 2y ago db: Session,
207 01cdf2e Dawsh 2y ago offset: Optional[int] = None,
208 01cdf2e Dawsh 2y ago limit: Optional[int] = None,
209 01cdf2e Dawsh 2y ago usernames: Optional[List[str]] = None,
210 01cdf2e Dawsh 2y ago status: Optional[Union[UserStatus, list]] = None,
211 01cdf2e Dawsh 2y ago sort: Optional[List[UsersSortingOptions]] = None,
212 01cdf2e Dawsh 2y ago admin: Optional[Admin] = None,
213 01cdf2e Dawsh 2y ago reset_strategy: Optional[Union[UserDataLimitResetStrategy, list]] = None,
214 01cdf2e Dawsh 2y ago return_with_count: bool = False,
215 01cdf2e Dawsh 2y ago ) -> Union[List[User], Tuple[List[User], int]]:
216 a47eb4c Dawsh 2y ago query = db.query(User)
217 a47eb4c Dawsh 2y ago
218 a47eb4c Dawsh 2y ago if usernames:
219 a47eb4c Dawsh 2y ago if len(usernames) == 1:
220 a47eb4c Dawsh 2y ago query = query.filter(User.username.ilike(f"%{usernames[0]}%"))
221 a47eb4c Dawsh 2y ago else:
222 a47eb4c Dawsh 2y ago query = query.filter(User.username.in_(usernames))
223 a47eb4c Dawsh 2y ago
224 a47eb4c Dawsh 2y ago if status:
225 a47eb4c Dawsh 2y ago if isinstance(status, list):
226 a47eb4c Dawsh 2y ago query = query.filter(User.status.in_(status))
227 a47eb4c Dawsh 2y ago else:
228 a47eb4c Dawsh 2y ago query = query.filter(User.status == status)
229 a47eb4c Dawsh 2y ago
230 a47eb4c Dawsh 2y ago if reset_strategy:
231 a47eb4c Dawsh 2y ago if isinstance(reset_strategy, list):
232 a47eb4c Dawsh 2y ago query = query.filter(User.data_limit_reset_strategy.in_(reset_strategy))
233 a47eb4c Dawsh 2y ago else:
234 a47eb4c Dawsh 2y ago query = query.filter(User.data_limit_reset_strategy == reset_strategy)
235 a47eb4c Dawsh 2y ago
236 a47eb4c Dawsh 2y ago if admin:
237 a47eb4c Dawsh 2y ago query = query.filter(User.admin == admin)
238 a47eb4c Dawsh 2y ago
239 a47eb4c Dawsh 2y ago # count it before applying limit and offset
240 a47eb4c Dawsh 2y ago if return_with_count:
241 a47eb4c Dawsh 2y ago count = query.count()
242 a47eb4c Dawsh 2y ago
243 a47eb4c Dawsh 2y ago if sort:
244 a47eb4c Dawsh 2y ago query = query.order_by(*(opt.value for opt in sort))
245 a47eb4c Dawsh 2y ago
246 a47eb4c Dawsh 2y ago if offset:
247 a47eb4c Dawsh 2y ago query = query.offset(offset)
248 a47eb4c Dawsh 2y ago if limit:
249 a47eb4c Dawsh 2y ago query = query.limit(limit)
250 a47eb4c Dawsh 2y ago
251 a47eb4c Dawsh 2y ago if return_with_count:
252 a47eb4c Dawsh 2y ago return query.all(), count
253 a47eb4c Dawsh 2y ago
254 a47eb4c Dawsh 2y ago return query.all()
255 a47eb4c Dawsh 2y ago
256 a47eb4c Dawsh 2y ago
257 01cdf2e Dawsh 2y ago def get_user_usages(
258 01cdf2e Dawsh 2y ago db: Session,
259 01cdf2e Dawsh 2y ago dbuser: User,
260 01cdf2e Dawsh 2y ago start: datetime,
261 01cdf2e Dawsh 2y ago end: datetime,
262 01cdf2e Dawsh 2y ago ) -> List[UserUsageResponse]:
263 eb74041 Dawsh 2y ago usages = dict()
264 a47eb4c Dawsh 2y ago
265 a47eb4c Dawsh 2y ago for node in db.query(Node).all():
266 a47eb4c Dawsh 2y ago usages[node.id] = UserUsageResponse(
267 01cdf2e Dawsh 2y ago node_id=node.id, node_name=node.name, used_traffic=0
268 a47eb4c Dawsh 2y ago )
269 a47eb4c Dawsh 2y ago
270 01cdf2e Dawsh 2y ago cond = and_(
271 01cdf2e Dawsh 2y ago NodeUserUsage.user_id == dbuser.id,
272 01cdf2e Dawsh 2y ago NodeUserUsage.created_at >= start,
273 01cdf2e Dawsh 2y ago NodeUserUsage.created_at <= end,
274 01cdf2e Dawsh 2y ago )
275 a47eb4c Dawsh 2y ago
276 a47eb4c Dawsh 2y ago for v in db.query(NodeUserUsage).filter(cond):
277 a47eb4c Dawsh 2y ago try:
278 a47eb4c Dawsh 2y ago usages[v.node_id or 0].used_traffic += v.used_traffic
279 a47eb4c Dawsh 2y ago except KeyError:
280 a47eb4c Dawsh 2y ago pass
281 a47eb4c Dawsh 2y ago
282 a47eb4c Dawsh 2y ago return list(usages.values())
283 a47eb4c Dawsh 2y ago
284 a47eb4c Dawsh 2y ago
285 e5dcd82 Dawsh 2y ago def get_users_count(
286 e5dcd82 Dawsh 2y ago db: Session,
287 e5dcd82 Dawsh 2y ago status: UserStatus | None = None,
288 e5dcd82 Dawsh 2y ago admin: Admin | None = None,
289 e5dcd82 Dawsh 2y ago enabled: bool | None = None,
290 e5dcd82 Dawsh 2y ago online: bool | None = None,
291 e5dcd82 Dawsh 2y ago ):
292 a47eb4c Dawsh 2y ago query = db.query(User.id)
293 a47eb4c Dawsh 2y ago if admin:
294 a47eb4c Dawsh 2y ago query = query.filter(User.admin == admin)
295 a47eb4c Dawsh 2y ago if status:
296 a47eb4c Dawsh 2y ago query = query.filter(User.status == status)
297 e5dcd82 Dawsh 2y ago if enabled:
298 e5dcd82 Dawsh 2y ago query = query.filter(User.enabled == enabled)
299 e5dcd82 Dawsh 2y ago if online:
300 e5dcd82 Dawsh 2y ago query = query.filter(
301 e5dcd82 Dawsh 2y ago User.online_at > (datetime.utcnow() - timedelta(seconds=30))
302 e5dcd82 Dawsh 2y ago )
303 a47eb4c Dawsh 2y ago return query.count()
304 a47eb4c Dawsh 2y ago
305 a47eb4c Dawsh 2y ago
306 a47eb4c Dawsh 2y ago def create_user(db: Session, user: UserCreate, admin: Admin = None):
307 a47eb4c Dawsh 2y ago dbuser = User(
308 a47eb4c Dawsh 2y ago username=user.username,
309 a47eb4c Dawsh 2y ago # proxies=proxies,
310 a47eb4c Dawsh 2y ago key=user.key,
311 01cdf2e Dawsh 2y ago services=db.query(Service)
312 01cdf2e Dawsh 2y ago .filter(Service.id.in_(user.services))
313 01cdf2e Dawsh 2y ago .all(), # user.services,
314 a47eb4c Dawsh 2y ago status=user.status,
315 a47eb4c Dawsh 2y ago data_limit=(user.data_limit or None),
316 a47eb4c Dawsh 2y ago expire=(user.expire or None),
317 a47eb4c Dawsh 2y ago admin=admin,
318 a47eb4c Dawsh 2y ago data_limit_reset_strategy=user.data_limit_reset_strategy,
319 a47eb4c Dawsh 2y ago note=user.note,
320 a47eb4c Dawsh 2y ago on_hold_expire_duration=(user.on_hold_expire_duration or None),
321 a47eb4c Dawsh 2y ago on_hold_timeout=(user.on_hold_timeout or None),
322 a47eb4c Dawsh 2y ago )
323 a47eb4c Dawsh 2y ago db.add(dbuser)
324 a47eb4c Dawsh 2y ago db.commit()
325 a47eb4c Dawsh 2y ago db.refresh(dbuser)
326 a47eb4c Dawsh 2y ago return dbuser
327 a47eb4c Dawsh 2y ago
328 a47eb4c Dawsh 2y ago
329 a47eb4c Dawsh 2y ago def remove_user(db: Session, dbuser: User):
330 a47eb4c Dawsh 2y ago db.delete(dbuser)
331 a47eb4c Dawsh 2y ago # db.query(User).filter_by(id=user_id).delete()
332 a47eb4c Dawsh 2y ago db.commit()
333 a47eb4c Dawsh 2y ago
334 a47eb4c Dawsh 2y ago
335 a47eb4c Dawsh 2y ago def update_user(db: Session, dbuser: User, modify: UserModify):
336 a47eb4c Dawsh 2y ago if modify.status is not None:
337 a47eb4c Dawsh 2y ago dbuser.status = modify.status
338 a47eb4c Dawsh 2y ago
339 a47eb4c Dawsh 2y ago if modify.data_limit is not None:
340 a418d26 dawsh 2y ago # in case there is modification to a user's data limit
341 a418d26 dawsh 2y ago dbuser.data_limit = modify.data_limit or None # set it to the new limit
342 a418d26 dawsh 2y ago if dbuser.status is not UserStatus.expired:
343 a418d26 dawsh 2y ago # in case the user isn't already disabled/expired
344 a47eb4c Dawsh 2y ago if not dbuser.data_limit or dbuser.used_traffic < dbuser.data_limit:
345 a418d26 dawsh 2y ago # in case the user is unlimited or hasn't reached the limit already
346 a47eb4c Dawsh 2y ago if dbuser.status != UserStatus.on_hold:
347 a47eb4c Dawsh 2y ago dbuser.status = UserStatus.active
348 a47eb4c Dawsh 2y ago
349 01cdf2e Dawsh 2y ago if not dbuser.data_limit or (
350 01cdf2e Dawsh 2y ago calculate_usage_percent(dbuser.used_traffic, dbuser.data_limit)
351 01cdf2e Dawsh 2y ago < NOTIFY_REACHED_USAGE_PERCENT
352 01cdf2e Dawsh 2y ago ):
353 01cdf2e Dawsh 2y ago delete_notification_reminder_by_type(
354 01cdf2e Dawsh 2y ago db, dbuser.id, ReminderType.data_usage
355 01cdf2e Dawsh 2y ago )
356 a47eb4c Dawsh 2y ago else:
357 a418d26 dawsh 2y ago # the user has reached the new data limit
358 a47eb4c Dawsh 2y ago dbuser.status = UserStatus.limited
359 a47eb4c Dawsh 2y ago
360 a47eb4c Dawsh 2y ago if modify.expire is not None:
361 01cdf2e Dawsh 2y ago dbuser.expire = modify.expire or None
362 a47eb4c Dawsh 2y ago if dbuser.status in (UserStatus.active, UserStatus.expired):
363 3b7feef Mrdn 2y ago if not dbuser.expire or dbuser.expire > datetime.utcnow():
364 a47eb4c Dawsh 2y ago dbuser.status = UserStatus.active
365 01cdf2e Dawsh 2y ago if not dbuser.expire or (
366 01cdf2e Dawsh 2y ago calculate_expiration_days(dbuser.expire) > NOTIFY_DAYS_LEFT
367 01cdf2e Dawsh 2y ago ):
368 01cdf2e Dawsh 2y ago delete_notification_reminder_by_type(
369 01cdf2e Dawsh 2y ago db, dbuser.id, ReminderType.expiration_date
370 01cdf2e Dawsh 2y ago )
371 a47eb4c Dawsh 2y ago else:
372 a47eb4c Dawsh 2y ago dbuser.status = UserStatus.expired
373 a47eb4c Dawsh 2y ago
374 a47eb4c Dawsh 2y ago if modify.note is not None:
375 a47eb4c Dawsh 2y ago dbuser.note = modify.note or None
376 a47eb4c Dawsh 2y ago
377 a47eb4c Dawsh 2y ago if modify.data_limit_reset_strategy is not None:
378 a47eb4c Dawsh 2y ago dbuser.data_limit_reset_strategy = modify.data_limit_reset_strategy.value
379 a47eb4c Dawsh 2y ago
380 a47eb4c Dawsh 2y ago if modify.on_hold_timeout is not None:
381 a47eb4c Dawsh 2y ago dbuser.on_hold_timeout = modify.on_hold_timeout
382 a47eb4c Dawsh 2y ago
383 a47eb4c Dawsh 2y ago if modify.on_hold_expire_duration is not None:
384 a47eb4c Dawsh 2y ago dbuser.on_hold_expire_duration = modify.on_hold_expire_duration
385 2de9d72 Dawsh 2y ago
386 a47eb4c Dawsh 2y ago if modify.services is not None:
387 01cdf2e Dawsh 2y ago dbuser.services = (
388 01cdf2e Dawsh 2y ago db.query(Service).filter(Service.id.in_(modify.services)).all()
389 01cdf2e Dawsh 2y ago )
390 a47eb4c Dawsh 2y ago dbuser.edit_at = datetime.utcnow()
391 a47eb4c Dawsh 2y ago
392 a47eb4c Dawsh 2y ago db.commit()
393 a47eb4c Dawsh 2y ago db.refresh(dbuser)
394 a47eb4c Dawsh 2y ago return dbuser
395 a47eb4c Dawsh 2y ago
396 a47eb4c Dawsh 2y ago
397 a47eb4c Dawsh 2y ago def reset_user_data_usage(db: Session, dbuser: User):
398 42a1073 Dawsh 2y ago dbuser.lifetime_used_traffic += dbuser.used_traffic
399 42a1073 Dawsh 2y ago dbuser.traffic_reset_at = datetime.utcnow()
400 a47eb4c Dawsh 2y ago
401 a47eb4c Dawsh 2y ago dbuser.used_traffic = 0
402 42a1073 Dawsh 2y ago
403 6da1b8a Dawsh 2y ago if dbuser.status == UserStatus.limited:
404 a47eb4c Dawsh 2y ago dbuser.status = UserStatus.active.value
405 a47eb4c Dawsh 2y ago db.add(dbuser)
406 a47eb4c Dawsh 2y ago
407 a47eb4c Dawsh 2y ago db.commit()
408 a47eb4c Dawsh 2y ago db.refresh(dbuser)
409 a47eb4c Dawsh 2y ago return dbuser
410 a47eb4c Dawsh 2y ago
411 a47eb4c Dawsh 2y ago
412 a47eb4c Dawsh 2y ago def revoke_user_sub(db: Session, dbuser: User):
413 34a7612 Dawsh 2y ago dbuser.key = secrets.token_hex(16)
414 a47eb4c Dawsh 2y ago db.commit()
415 a47eb4c Dawsh 2y ago db.refresh(dbuser)
416 a47eb4c Dawsh 2y ago return dbuser
417 a47eb4c Dawsh 2y ago
418 a47eb4c Dawsh 2y ago
419 a47eb4c Dawsh 2y ago def update_user_sub(db: Session, dbuser: User, user_agent: str):
420 a47eb4c Dawsh 2y ago dbuser.sub_updated_at = datetime.utcnow()
421 a47eb4c Dawsh 2y ago dbuser.sub_last_user_agent = user_agent
422 a47eb4c Dawsh 2y ago
423 a47eb4c Dawsh 2y ago db.commit()
424 a47eb4c Dawsh 2y ago db.refresh(dbuser)
425 a47eb4c Dawsh 2y ago return dbuser
426 a47eb4c Dawsh 2y ago
427 a47eb4c Dawsh 2y ago
428 a47eb4c Dawsh 2y ago def reset_all_users_data_usage(db: Session, admin: Optional[Admin] = None):
429 a47eb4c Dawsh 2y ago query = db.query(User)
430 a47eb4c Dawsh 2y ago
431 a47eb4c Dawsh 2y ago if admin:
432 a47eb4c Dawsh 2y ago query = query.filter(User.admin == admin)
433 a47eb4c Dawsh 2y ago
434 a47eb4c Dawsh 2y ago for dbuser in query.all():
435 a47eb4c Dawsh 2y ago dbuser.used_traffic = 0
436 01cdf2e Dawsh 2y ago if dbuser.status not in [
437 01cdf2e Dawsh 2y ago UserStatus.on_hold,
438 01cdf2e Dawsh 2y ago UserStatus.expired,
439 01cdf2e Dawsh 2y ago ]:
440 a47eb4c Dawsh 2y ago dbuser.status = UserStatus.active
441 a47eb4c Dawsh 2y ago dbuser.usage_logs.clear()
442 a47eb4c Dawsh 2y ago dbuser.node_usages.clear()
443 a47eb4c Dawsh 2y ago db.add(dbuser)
444 a47eb4c Dawsh 2y ago
445 a47eb4c Dawsh 2y ago db.commit()
446 a47eb4c Dawsh 2y ago
447 a47eb4c Dawsh 2y ago
448 a47eb4c Dawsh 2y ago def update_user_status(db: Session, dbuser: User, status: UserStatus):
449 a47eb4c Dawsh 2y ago dbuser.status = status
450 a47eb4c Dawsh 2y ago db.commit()
451 a47eb4c Dawsh 2y ago db.refresh(dbuser)
452 a47eb4c Dawsh 2y ago return dbuser
453 a47eb4c Dawsh 2y ago
454 a47eb4c Dawsh 2y ago
455 a47eb4c Dawsh 2y ago def set_owner(db: Session, dbuser: User, admin: Admin):
456 a47eb4c Dawsh 2y ago dbuser.admin = admin
457 a47eb4c Dawsh 2y ago db.commit()
458 a47eb4c Dawsh 2y ago db.refresh(dbuser)
459 a47eb4c Dawsh 2y ago return dbuser
460 a47eb4c Dawsh 2y ago
461 a47eb4c Dawsh 2y ago
462 a47eb4c Dawsh 2y ago def start_user_expire(db: Session, dbuser: User):
463 f446990 Dawsh 2y ago expire = datetime.utcnow() + timedelta(seconds=dbuser.on_hold_expire_duration)
464 a47eb4c Dawsh 2y ago dbuser.expire = expire
465 a47eb4c Dawsh 2y ago db.commit()
466 a47eb4c Dawsh 2y ago db.refresh(dbuser)
467 a47eb4c Dawsh 2y ago return dbuser
468 a47eb4c Dawsh 2y ago
469 a47eb4c Dawsh 2y ago
470 a47eb4c Dawsh 2y ago def get_system_usage(db: Session):
471 a47eb4c Dawsh 2y ago return db.query(System).first()
472 a47eb4c Dawsh 2y ago
473 a47eb4c Dawsh 2y ago
474 a47eb4c Dawsh 2y ago def get_jwt_secret_key(db: Session):
475 a47eb4c Dawsh 2y ago return db.query(JWT).first().secret_key
476 a47eb4c Dawsh 2y ago
477 a47eb4c Dawsh 2y ago
478 a47eb4c Dawsh 2y ago def get_tls_certificate(db: Session):
479 a47eb4c Dawsh 2y ago return db.query(TLS).first()
480 a47eb4c Dawsh 2y ago
481 a47eb4c Dawsh 2y ago
482 a47eb4c Dawsh 2y ago def get_admin(db: Session, username: str):
483 a47eb4c Dawsh 2y ago return db.query(Admin).filter(Admin.username == username).first()
484 a47eb4c Dawsh 2y ago
485 a47eb4c Dawsh 2y ago
486 a47eb4c Dawsh 2y ago def create_admin(db: Session, admin: AdminCreate):
487 a47eb4c Dawsh 2y ago dbadmin = Admin(
488 a47eb4c Dawsh 2y ago username=admin.username,
489 a47eb4c Dawsh 2y ago hashed_password=admin.hashed_password,
490 01cdf2e Dawsh 2y ago is_sudo=admin.is_sudo,
491 a47eb4c Dawsh 2y ago )
492 a47eb4c Dawsh 2y ago db.add(dbadmin)
493 a47eb4c Dawsh 2y ago db.commit()
494 a47eb4c Dawsh 2y ago db.refresh(dbadmin)
495 a47eb4c Dawsh 2y ago return dbadmin
496 a47eb4c Dawsh 2y ago
497 a47eb4c Dawsh 2y ago
498 a47eb4c Dawsh 2y ago def update_admin(db: Session, dbadmin: Admin, modified_admin: AdminModify):
499 a47eb4c Dawsh 2y ago dbadmin.is_sudo = modified_admin.is_sudo
500 a47eb4c Dawsh 2y ago if dbadmin.hashed_password != modified_admin.hashed_password:
501 a47eb4c Dawsh 2y ago dbadmin.hashed_password = modified_admin.hashed_password
502 a47eb4c Dawsh 2y ago dbadmin.password_reset_at = datetime.utcnow()
503 a47eb4c Dawsh 2y ago db.commit()
504 a47eb4c Dawsh 2y ago db.refresh(dbadmin)
505 2de9d72 Dawsh 2y ago return dbadmin
506 a47eb4c Dawsh 2y ago
507 a47eb4c Dawsh 2y ago
508 01cdf2e Dawsh 2y ago def partial_update_admin(
509 01cdf2e Dawsh 2y ago db: Session, dbadmin: Admin, modified_admin: AdminPartialModify
510 01cdf2e Dawsh 2y ago ):
511 a47eb4c Dawsh 2y ago if modified_admin.is_sudo is not None:
512 a47eb4c Dawsh 2y ago dbadmin.is_sudo = modified_admin.is_sudo
513 01cdf2e Dawsh 2y ago if (
514 01cdf2e Dawsh 2y ago modified_admin.password is not None
515 01cdf2e Dawsh 2y ago and dbadmin.hashed_password != modified_admin.hashed_password
516 01cdf2e Dawsh 2y ago ):
517 a47eb4c Dawsh 2y ago dbadmin.hashed_password = modified_admin.hashed_password
518 a47eb4c Dawsh 2y ago dbadmin.password_reset_at = datetime.utcnow()
519 a47eb4c Dawsh 2y ago
520 a47eb4c Dawsh 2y ago db.commit()
521 a47eb4c Dawsh 2y ago db.refresh(dbadmin)
522 a47eb4c Dawsh 2y ago return dbadmin
523 a47eb4c Dawsh 2y ago
524 a47eb4c Dawsh 2y ago
525 a47eb4c Dawsh 2y ago def remove_admin(db: Session, dbadmin: Admin):
526 a47eb4c Dawsh 2y ago db.delete(dbadmin)
527 a47eb4c Dawsh 2y ago db.commit()
528 a47eb4c Dawsh 2y ago return dbadmin
529 a47eb4c Dawsh 2y ago
530 a47eb4c Dawsh 2y ago
531 01cdf2e Dawsh 2y ago def get_admins(
532 01cdf2e Dawsh 2y ago db: Session,
533 01cdf2e Dawsh 2y ago offset: Optional[int] = None,
534 01cdf2e Dawsh 2y ago limit: Optional[int] = None,
535 01cdf2e Dawsh 2y ago username: Optional[str] = None,
536 01cdf2e Dawsh 2y ago ):
537 a47eb4c Dawsh 2y ago query = db.query(Admin)
538 a47eb4c Dawsh 2y ago if username:
539 01cdf2e Dawsh 2y ago query = query.filter(Admin.username.ilike(f"%{username}%"))
540 a47eb4c Dawsh 2y ago if offset:
541 a47eb4c Dawsh 2y ago query = query.offset(offset)
542 a47eb4c Dawsh 2y ago if limit:
543 a47eb4c Dawsh 2y ago query = query.limit(limit)
544 a47eb4c Dawsh 2y ago return query.all()
545 a47eb4c Dawsh 2y ago
546 a47eb4c Dawsh 2y ago
547 ee1351a Dawsh 2y ago def create_service(db: Session, service: ServiceCreate) -> Service:
548 a47eb4c Dawsh 2y ago dbservice = Service(
549 a47eb4c Dawsh 2y ago name=service.name,
550 a47eb4c Dawsh 2y ago inbounds=db.query(Inbound).filter(Inbound.id.in_(service.inbounds)).all(),
551 01cdf2e Dawsh 2y ago users=db.query(User).filter(User.id.in_(service.users)).all(),
552 2de9d72 Dawsh 2y ago )
553 a47eb4c Dawsh 2y ago db.add(dbservice)
554 a47eb4c Dawsh 2y ago db.commit()
555 a47eb4c Dawsh 2y ago db.refresh(dbservice)
556 a47eb4c Dawsh 2y ago return dbservice
557 a47eb4c Dawsh 2y ago
558 a47eb4c Dawsh 2y ago
559 ee1351a Dawsh 2y ago def get_service(db: Session, service_id: id) -> Service:
560 ee1351a Dawsh 2y ago return db.query(Service).filter(Service.id == service_id).first()
561 a47eb4c Dawsh 2y ago
562 12b255b Dawsh 2y ago
563 a47eb4c Dawsh 2y ago def get_services(db: Session) -> List[Service]:
564 a47eb4c Dawsh 2y ago return db.query(Service).all()
565 a47eb4c Dawsh 2y ago
566 12b255b Dawsh 2y ago
567 ee1351a Dawsh 2y ago def update_service(db: Session, db_service: Service, modification: ServiceModify):
568 ee1351a Dawsh 2y ago if modification.name is not None:
569 ee1351a Dawsh 2y ago db_service.name = modification.name
570 ee1351a Dawsh 2y ago
571 ee1351a Dawsh 2y ago if modification.inbounds is not None:
572 01cdf2e Dawsh 2y ago db_service.inbounds = (
573 01cdf2e Dawsh 2y ago db.query(Inbound).filter(Inbound.id.in_(modification.inbounds)).all()
574 01cdf2e Dawsh 2y ago )
575 ee1351a Dawsh 2y ago
576 ee1351a Dawsh 2y ago db.commit()
577 ee1351a Dawsh 2y ago db.refresh(db_service)
578 ee1351a Dawsh 2y ago return db_service
579 ee1351a Dawsh 2y ago
580 ee1351a Dawsh 2y ago
581 ee1351a Dawsh 2y ago def remove_service(db: Session, db_service: Service):
582 ee1351a Dawsh 2y ago db.delete(db_service)
583 ee1351a Dawsh 2y ago db.commit()
584 ee1351a Dawsh 2y ago return db_service
585 ee1351a Dawsh 2y ago
586 ee1351a Dawsh 2y ago
587 a47eb4c Dawsh 2y ago def get_node(db: Session, name: str):
588 a47eb4c Dawsh 2y ago return db.query(Node).filter(Node.name == name).first()
589 a47eb4c Dawsh 2y ago
590 a47eb4c Dawsh 2y ago
591 a47eb4c Dawsh 2y ago def get_node_by_id(db: Session, node_id: int):
592 a47eb4c Dawsh 2y ago return db.query(Node).filter(Node.id == node_id).first()
593 a47eb4c Dawsh 2y ago
594 a47eb4c Dawsh 2y ago
595 01cdf2e Dawsh 2y ago def get_nodes(
596 01cdf2e Dawsh 2y ago db: Session, status: Optional[Union[NodeStatus, list]] = None, enabled: bool = None
597 01cdf2e Dawsh 2y ago ):
598 a47eb4c Dawsh 2y ago query = db.query(Node)
599 a47eb4c Dawsh 2y ago
600 a47eb4c Dawsh 2y ago if status:
601 a47eb4c Dawsh 2y ago if isinstance(status, list):
602 a47eb4c Dawsh 2y ago query = query.filter(Node.status.in_(status))
603 a47eb4c Dawsh 2y ago else:
604 a47eb4c Dawsh 2y ago query = query.filter(Node.status == status)
605 a47eb4c Dawsh 2y ago
606 a47eb4c Dawsh 2y ago if enabled:
607 a47eb4c Dawsh 2y ago query = query.filter(Node.status != NodeStatus.disabled)
608 a47eb4c Dawsh 2y ago
609 a47eb4c Dawsh 2y ago return query.all()
610 a47eb4c Dawsh 2y ago
611 a47eb4c Dawsh 2y ago
612 01cdf2e Dawsh 2y ago def get_nodes_usage(
613 01cdf2e Dawsh 2y ago db: Session, start: datetime, end: datetime
614 01cdf2e Dawsh 2y ago ) -> List[NodeUsageResponse]:
615 12b255b Dawsh 2y ago usages = dict()
616 a47eb4c Dawsh 2y ago
617 a47eb4c Dawsh 2y ago usages[0] = NodeUsageResponse( # Main Core
618 01cdf2e Dawsh 2y ago node_id=None, node_name="Master", uplink=0, downlink=0
619 a47eb4c Dawsh 2y ago )
620 a47eb4c Dawsh 2y ago for node in db.query(Node).all():
621 a47eb4c Dawsh 2y ago usages[node.id] = NodeUsageResponse(
622 01cdf2e Dawsh 2y ago node_id=node.id, node_name=node.name, uplink=0, downlink=0
623 a47eb4c Dawsh 2y ago )
624 a47eb4c Dawsh 2y ago
625 a47eb4c Dawsh 2y ago cond = and_(NodeUsage.created_at >= start, NodeUsage.created_at <= end)
626 a47eb4c Dawsh 2y ago
627 a47eb4c Dawsh 2y ago for v in db.query(NodeUsage).filter(cond):
628 a47eb4c Dawsh 2y ago try:
629 a47eb4c Dawsh 2y ago usages[v.node_id or 0].uplink += v.uplink
630 a47eb4c Dawsh 2y ago usages[v.node_id or 0].downlink += v.downlink
631 a47eb4c Dawsh 2y ago except KeyError:
632 a47eb4c Dawsh 2y ago pass
633 a47eb4c Dawsh 2y ago
634 a47eb4c Dawsh 2y ago return list(usages.values())
635 a47eb4c Dawsh 2y ago
636 a47eb4c Dawsh 2y ago
637 a47eb4c Dawsh 2y ago def create_node(db: Session, node: NodeCreate):
638 01cdf2e Dawsh 2y ago dbnode = Node(
639 01cdf2e Dawsh 2y ago name=node.name,
640 01cdf2e Dawsh 2y ago address=node.address,
641 01cdf2e Dawsh 2y ago port=node.port,
642 01cdf2e Dawsh 2y ago connection_backend=node.connection_backend,
643 01cdf2e Dawsh 2y ago )
644 a47eb4c Dawsh 2y ago
645 a47eb4c Dawsh 2y ago db.add(dbnode)
646 a47eb4c Dawsh 2y ago db.commit()
647 a47eb4c Dawsh 2y ago db.refresh(dbnode)
648 a47eb4c Dawsh 2y ago return dbnode
649 a47eb4c Dawsh 2y ago
650 a47eb4c Dawsh 2y ago
651 a47eb4c Dawsh 2y ago def remove_node(db: Session, dbnode: Node):
652 a47eb4c Dawsh 2y ago db.delete(dbnode)
653 a47eb4c Dawsh 2y ago db.commit()
654 a47eb4c Dawsh 2y ago return dbnode
655 a47eb4c Dawsh 2y ago
656 a47eb4c Dawsh 2y ago
657 a47eb4c Dawsh 2y ago def update_node(db: Session, dbnode: Node, modify: NodeModify):
658 a47eb4c Dawsh 2y ago if modify.name is not None:
659 a47eb4c Dawsh 2y ago dbnode.name = modify.name
660 a47eb4c Dawsh 2y ago
661 a47eb4c Dawsh 2y ago if modify.address is not None:
662 a47eb4c Dawsh 2y ago dbnode.address = modify.address
663 a47eb4c Dawsh 2y ago
664 a47eb4c Dawsh 2y ago if modify.port is not None:
665 a47eb4c Dawsh 2y ago dbnode.port = modify.port
666 a47eb4c Dawsh 2y ago
667 a47eb4c Dawsh 2y ago if modify.status is NodeStatus.disabled:
668 a47eb4c Dawsh 2y ago dbnode.status = modify.status
669 a47eb4c Dawsh 2y ago dbnode.xray_version = None
670 a47eb4c Dawsh 2y ago dbnode.message = None
671 a47eb4c Dawsh 2y ago else:
672 12b255b Dawsh 2y ago dbnode.status = NodeStatus.unhealthy
673 a47eb4c Dawsh 2y ago
674 a47eb4c Dawsh 2y ago if modify.usage_coefficient:
675 a47eb4c Dawsh 2y ago dbnode.usage_coefficient = modify.usage_coefficient
676 a47eb4c Dawsh 2y ago
677 5584d4a Mrdn 2y ago if modify.connection_backend:
678 5584d4a Mrdn 2y ago dbnode.connection_backend = modify.connection_backend
679 5584d4a Mrdn 2y ago
680 a47eb4c Dawsh 2y ago db.commit()
681 a47eb4c Dawsh 2y ago db.refresh(dbnode)
682 a47eb4c Dawsh 2y ago return dbnode
683 a47eb4c Dawsh 2y ago
684 a47eb4c Dawsh 2y ago
685 01cdf2e Dawsh 2y ago def update_node_status(
686 01cdf2e Dawsh 2y ago db: Session,
687 01cdf2e Dawsh 2y ago node_id: int,
688 01cdf2e Dawsh 2y ago status: NodeStatus,
689 01cdf2e Dawsh 2y ago message: str = None,
690 01cdf2e Dawsh 2y ago version: str = None,
691 01cdf2e Dawsh 2y ago ):
692 f6a32ea Dawsh 2y ago db_node = db.query(Node).where(Node.id == node_id).first()
693 f6a32ea Dawsh 2y ago db_node.status = status
694 f6a32ea Dawsh 2y ago if message:
695 f6a32ea Dawsh 2y ago db_node.message = message
696 f6a32ea Dawsh 2y ago db_node.last_status_change = datetime.utcnow()
697 a47eb4c Dawsh 2y ago db.commit()
698 a47eb4c Dawsh 2y ago
699 a47eb4c Dawsh 2y ago
700 a47eb4c Dawsh 2y ago def create_notification_reminder(
701 01cdf2e Dawsh 2y ago db: Session, reminder_type: ReminderType, expires_at: datetime, user_id: int
702 01cdf2e Dawsh 2y ago ) -> NotificationReminder:
703 01cdf2e Dawsh 2y ago reminder = NotificationReminder(
704 01cdf2e Dawsh 2y ago type=reminder_type, expires_at=expires_at, user_id=user_id
705 01cdf2e Dawsh 2y ago )
706 a47eb4c Dawsh 2y ago db.add(reminder)
707 a47eb4c Dawsh 2y ago db.commit()
708 a47eb4c Dawsh 2y ago db.refresh(reminder)
709 a47eb4c Dawsh 2y ago return reminder
710 a47eb4c Dawsh 2y ago
711 a47eb4c Dawsh 2y ago
712 a47eb4c Dawsh 2y ago def get_notification_reminder(
713 01cdf2e Dawsh 2y ago db: Session,
714 01cdf2e Dawsh 2y ago user_id: int,
715 01cdf2e Dawsh 2y ago reminder_type: ReminderType,
716 12b255b Dawsh 2y ago ) -> Optional[NotificationReminder]:
717 01cdf2e Dawsh 2y ago reminder = (
718 01cdf2e Dawsh 2y ago db.query(NotificationReminder)
719 01cdf2e Dawsh 2y ago .filter(NotificationReminder.user_id == user_id)
720 01cdf2e Dawsh 2y ago .filter(NotificationReminder.type == reminder_type)
721 01cdf2e Dawsh 2y ago .first()
722 01cdf2e Dawsh 2y ago )
723 a47eb4c Dawsh 2y ago if reminder is None:
724 a47eb4c Dawsh 2y ago return
725 a47eb4c Dawsh 2y ago if reminder.expires_at and reminder.expires_at < datetime.utcnow():
726 a47eb4c Dawsh 2y ago db.delete(reminder)
727 a47eb4c Dawsh 2y ago db.commit()
728 a47eb4c Dawsh 2y ago return
729 a47eb4c Dawsh 2y ago return reminder
730 a47eb4c Dawsh 2y ago
731 a47eb4c Dawsh 2y ago
732 01cdf2e Dawsh 2y ago def delete_notification_reminder_by_type(
733 01cdf2e Dawsh 2y ago db: Session, user_id: int, reminder_type: ReminderType
734 01cdf2e Dawsh 2y ago ) -> None:
735 a47eb4c Dawsh 2y ago """Deletes notification reminder filtered by user_id and type if exists"""
736 a47eb4c Dawsh 2y ago stmt = delete(NotificationReminder).where(
737 a47eb4c Dawsh 2y ago NotificationReminder.user_id == user_id,
738 a47eb4c Dawsh 2y ago NotificationReminder.type == reminder_type,
739 a47eb4c Dawsh 2y ago )
740 a47eb4c Dawsh 2y ago db.execute(stmt)
741 a47eb4c Dawsh 2y ago db.commit()
742 a47eb4c Dawsh 2y ago return
743 a47eb4c Dawsh 2y ago
744 a47eb4c Dawsh 2y ago
745 a47eb4c Dawsh 2y ago def delete_notification_reminder(db: Session, dbreminder: NotificationReminder) -> None:
746 a47eb4c Dawsh 2y ago db.delete(dbreminder)
747 a47eb4c Dawsh 2y ago db.commit()
748 a47eb4c Dawsh 2y ago return