File size: 3,751 Bytes
223ca08
640b9a5
 
223ca08
 
 
 
28f0a71
640b9a5
28f0a71
 
 
 
 
 
640b9a5
 
 
 
 
 
 
28f0a71
 
 
 
 
 
 
 
 
 
 
 
223ca08
 
640b9a5
28f0a71
 
223ca08
 
640b9a5
28f0a71
 
223ca08
 
640b9a5
28f0a71
223ca08
 
28f0a71
223ca08
28f0a71
 
640b9a5
28f0a71
 
 
 
223ca08
 
28f0a71
 
640b9a5
28f0a71
 
 
 
223ca08
 
28f0a71
 
640b9a5
28f0a71
 
 
 
223ca08
 
640b9a5
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
28f0a71
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
# PATH: bot/integrations/cf_worker2.py
from __future__ import annotations

from bot.config import Workers
from bot.integrations.http import post_json
from bot.core.settings import ROTATE_AFTER


def _h_hf():
    return {
        "Authorization": f"Bearer {Workers.HF_API_KEY}",
        "Content-Type": "application/json",
    }


def _h_bot():
    return {
        "Authorization": f"Bearer {Workers.BOT_BACKEND_KEY}",
        "Content-Type": "application/json",
    }


def _unwrap(r: dict) -> dict:
    """
    Unwrap post_json wrapper:
      {"ok": true, "status": 200, "data": {...}} -> {...}
    """
    if isinstance(r, dict) and isinstance(r.get("data"), dict):
        data = r["data"]
        if "ok" not in data and "ok" in r:
            data["ok"] = r["ok"]
        return data
    return r


async def allow_user(tg_id: int) -> dict:
    r = await post_json(f"{Workers.WORKER2_URL}/api/allow_user", _h_hf(), {"tg_id": str(tg_id)})
    return _unwrap(r)


async def disallow_user(tg_id: int) -> dict:
    r = await post_json(f"{Workers.WORKER2_URL}/api/disallow_user", _h_hf(), {"tg_id": str(tg_id)})
    return _unwrap(r)


async def is_allowed(tg_id: int) -> bool:
    r = await post_json(f"{Workers.WORKER2_URL}/api/is_allowed", _h_hf(), {"tg_id": str(tg_id)})
    j = _unwrap(r)
    return bool(j.get("ok") and j.get("allowed") is True)


async def pick_profile(tg_id: int, channel_id: str, rotate_after: int = ROTATE_AFTER) -> dict:
    r = await post_json(
        f"{Workers.WORKER2_URL}/api/pick_profile",
        _h_hf(),
        {"tg_id": str(tg_id), "channel_id": str(channel_id), "rotate_after": int(rotate_after)},
    )
    return _unwrap(r)


async def access_token(tg_id: int, profile_id: str) -> dict:
    r = await post_json(
        f"{Workers.WORKER2_URL}/api/access_token",
        _h_hf(),
        {"tg_id": str(tg_id), "profile_id": profile_id},
    )
    return _unwrap(r)


async def record_upload(tg_id: int, profile_id: str) -> dict:
    r = await post_json(
        f"{Workers.WORKER2_URL}/api/record_upload",
        _h_hf(),
        {"tg_id": str(tg_id), "profile_id": profile_id},
    )
    return _unwrap(r)


async def stats_today() -> dict:
    r = await post_json(f"{Workers.WORKER2_URL}/api/stats_today", _h_hf(), {})
    return _unwrap(r)


# ✅ Added: handlers.py expects list_profiles(uid, only_connected=...)
async def list_profiles(tg_id: int, only_connected: bool = False) -> dict:
    r = await post_json(
        f"{Workers.WORKER2_URL}/api/list_profiles",
        _h_hf(),
        {"tg_id": str(tg_id), "only_connected": bool(only_connected)},
    )
    return _unwrap(r)


# ✅ Added: handlers.py expects get_default_profile(uid) -> {ok, profile_id, access_token}
async def get_default_profile(tg_id: int) -> dict:
    # worker2 supports default profile when profile_id is "" (it uses idx.default_profile_id)
    j = await access_token(tg_id, "")
    if isinstance(j, dict) and j.get("ok"):
        return {
            "ok": True,
            "profile_id": j.get("profile_id"),
            "access_token": j.get("access_token"),
            "expires_in": j.get("expires_in"),
            "channel_id": j.get("channel_id"),
            "channel_title": j.get("channel_title"),
        }
    return j if isinstance(j, dict) else {"ok": False, "err": "access_token_failed"}


# ✅ Added: handlers.py expects set_default_profile(uid, pid)
# Your Pages backend protects /api/profile/* with BOT_BACKEND_KEY, so we call WORKER1_URL here.
async def set_default_profile(tg_id: int, profile_id: str) -> dict:
    r = await post_json(
        f"{Workers.WORKER1_URL}/api/profile/set_default",
        _h_bot(),
        {"tg_id": str(tg_id), "profile_id": str(profile_id)},
    )
    return _unwrap(r)