gitscope
← marzneshin / v0.3.0-alpha7 / app/marznode/database.py
https://dev.kronoz.win/r/marzneshin.git
app/marznode/database.py · Python
Blame Raw
 1from app.db import crud, GetDB
 2from app.models.node import NodeStatus
 3from app.models.user import UserStatus
 4
 5
 6class MarzNodeDB:
 7    def list_users(self):
 8        with GetDB() as db:
 9            relations = crud.get_node_users(
10                db, self.id, [UserStatus.active, UserStatus.on_hold]
11            )
12            users = dict()
13            for rel in relations:
14                if not users.get(rel[0]):
15                    users[rel[0]] = dict(
16                        username=rel[1], id=rel[0], key=rel[2], inbounds=[]
17                    )
18                users[rel[0]]["inbounds"].append(rel[3].tag)
19        return list(users.values())
20
21    def store_inbounds(self, inbounds):
22        with GetDB() as db:
23            crud.assure_node_inbounds(db, inbounds, self.id)
24
25    def set_status(self, status: NodeStatus, message: str | None = None):
26        with GetDB() as db:
27            crud.update_node_status(db, self.id, status, message)