magic-campus-cli 0.1.0__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- magic_campus_cli-0.1.0.dist-info/METADATA +51 -0
- magic_campus_cli-0.1.0.dist-info/RECORD +53 -0
- magic_campus_cli-0.1.0.dist-info/WHEEL +5 -0
- magic_campus_cli-0.1.0.dist-info/entry_points.txt +2 -0
- magic_campus_cli-0.1.0.dist-info/top_level.txt +2 -0
- magiccampus_cli/__init__.py +1 -0
- magiccampus_cli/__main__.py +9 -0
- magiccampus_cli/accounts.py +144 -0
- magiccampus_cli/cli.py +176 -0
- magiccampus_cli/client.py +54 -0
- magiccampus_cli/commands/auth.py +32 -0
- magiccampus_cli/commands/comment.py +38 -0
- magiccampus_cli/commands/config.py +95 -0
- magiccampus_cli/commands/feed.py +26 -0
- magiccampus_cli/commands/group.py +346 -0
- magiccampus_cli/commands/im.py +47 -0
- magiccampus_cli/commands/notify.py +81 -0
- magiccampus_cli/commands/playgroup.py +49 -0
- magiccampus_cli/commands/reaction.py +50 -0
- magiccampus_cli/commands/space.py +134 -0
- magiccampus_cli/commands/user.py +311 -0
- magiccampus_cli/config_store.py +61 -0
- magiccampus_cli/notify.py +493 -0
- magiccampus_cli/output.py +104 -0
- magiccampus_cli/prompts.py +58 -0
- magiccampus_cli/targets.py +26 -0
- magiccampus_cli/update.py +149 -0
- magiccampus_cli/utils.py +67 -0
- magiccampus_sdk/__init__.py +43 -0
- magiccampus_sdk/auth.py +111 -0
- magiccampus_sdk/comment_client.py +107 -0
- magiccampus_sdk/feed_api.py +61 -0
- magiccampus_sdk/feed_client.py +30 -0
- magiccampus_sdk/group_api.py +139 -0
- magiccampus_sdk/group_client.py +242 -0
- magiccampus_sdk/http/__init__.py +1 -0
- magiccampus_sdk/http/group_info_api.py +47 -0
- magiccampus_sdk/http/http_api.py +58 -0
- magiccampus_sdk/http/http_send.py +758 -0
- magiccampus_sdk/http/media.py +839 -0
- magiccampus_sdk/http/models.py +645 -0
- magiccampus_sdk/http/self_user_info_api.py +182 -0
- magiccampus_sdk/http/text.py +75 -0
- magiccampus_sdk/models.py +279 -0
- magiccampus_sdk/platform_client.py +137 -0
- magiccampus_sdk/platform_http.py +71 -0
- magiccampus_sdk/playgroup_api.py +104 -0
- magiccampus_sdk/playgroup_client.py +45 -0
- magiccampus_sdk/reaction_api.py +110 -0
- magiccampus_sdk/space_api.py +170 -0
- magiccampus_sdk/space_client.py +93 -0
- magiccampus_sdk/user_client.py +211 -0
- magiccampus_sdk/user_info_api.py +593 -0
|
@@ -0,0 +1,95 @@
|
|
|
1
|
+
from __future__ import annotations
|
|
2
|
+
|
|
3
|
+
from ..accounts import account_config_to_raw, get_account_config, list_account_summaries, remove_stored_account, upsert_stored_account
|
|
4
|
+
from ..config_store import get_config_path, read_config_root, write_config_root
|
|
5
|
+
from ..prompts import prompt_for_account
|
|
6
|
+
from ..utils import env_string, parse_optional_bool, trim_string
|
|
7
|
+
|
|
8
|
+
|
|
9
|
+
def _first_non_empty(*values: object) -> str:
|
|
10
|
+
for value in values:
|
|
11
|
+
text = trim_string(value)
|
|
12
|
+
if text:
|
|
13
|
+
return text
|
|
14
|
+
return ""
|
|
15
|
+
|
|
16
|
+
|
|
17
|
+
def _has_non_interactive_input(options: dict[str, object]) -> bool:
|
|
18
|
+
return any(
|
|
19
|
+
options.get(key) is not None
|
|
20
|
+
for key in (
|
|
21
|
+
"apiurl",
|
|
22
|
+
"api-url",
|
|
23
|
+
"apikey",
|
|
24
|
+
"api-key",
|
|
25
|
+
"platform",
|
|
26
|
+
"enabled",
|
|
27
|
+
"default",
|
|
28
|
+
)
|
|
29
|
+
)
|
|
30
|
+
|
|
31
|
+
|
|
32
|
+
def _build_input(existing: dict[str, object], options: dict[str, object], account_id: str) -> dict[str, object]:
|
|
33
|
+
return {
|
|
34
|
+
"apiUrl": _first_non_empty(options.get("apiurl"), options.get("api-url"), existing.get("apiUrl"), env_string("MAGIC_CAMPUS_BASE_URL")),
|
|
35
|
+
"apiKey": _first_non_empty(options.get("apikey"), options.get("api-key"), existing.get("apiKey"), env_string("MAGIC_CAMPUS_API_KEY")),
|
|
36
|
+
"platform": _first_non_empty(options.get("platform"), existing.get("platform"), "IOS"),
|
|
37
|
+
"enabled": parse_optional_bool(options.get("enabled"), existing.get("enabled") is not False),
|
|
38
|
+
"makeDefault": parse_optional_bool(options.get("default"), account_id == "default"),
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
|
|
42
|
+
def run_config_command(subcommand: str | None, argv: list[str], parse_options):
|
|
43
|
+
root = read_config_root()
|
|
44
|
+
options, positionals = parse_options(argv)
|
|
45
|
+
|
|
46
|
+
if subcommand == "init":
|
|
47
|
+
account_id = trim_string(options.get("account")) or "default"
|
|
48
|
+
resolved_existing = get_account_config(root, account_id)
|
|
49
|
+
existing = account_config_to_raw(resolved_existing) if resolved_existing is not None else {}
|
|
50
|
+
configured = _build_input(existing, options, account_id)
|
|
51
|
+
prompted = configured if _has_non_interactive_input(options) else prompt_for_account(existing, account_id)
|
|
52
|
+
|
|
53
|
+
if not trim_string(prompted.get("apiUrl")) or not trim_string(prompted.get("apiKey")):
|
|
54
|
+
raise RuntimeError("apiurl and apikey are required.")
|
|
55
|
+
|
|
56
|
+
next_root = upsert_stored_account(root, account_id, prompted, bool(prompted.get("makeDefault")))
|
|
57
|
+
write_config_root(next_root)
|
|
58
|
+
return {
|
|
59
|
+
"ok": True,
|
|
60
|
+
"accountId": account_id,
|
|
61
|
+
"defaultAccountId": next_root["defaultAccountId"],
|
|
62
|
+
"configPath": str(get_config_path()),
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
if subcommand == "list":
|
|
66
|
+
return list_account_summaries(root)
|
|
67
|
+
|
|
68
|
+
if subcommand == "use":
|
|
69
|
+
account_id = positionals[0] if positionals else ""
|
|
70
|
+
accounts = root.get("accounts")
|
|
71
|
+
if not account_id:
|
|
72
|
+
raise RuntimeError("config use requires <accountId>.")
|
|
73
|
+
if not isinstance(accounts, dict) or account_id not in accounts:
|
|
74
|
+
raise RuntimeError(f"Unknown account: {account_id}")
|
|
75
|
+
next_root = dict(root)
|
|
76
|
+
next_root["defaultAccountId"] = account_id
|
|
77
|
+
write_config_root(next_root)
|
|
78
|
+
return {"ok": True, "defaultAccountId": account_id}
|
|
79
|
+
|
|
80
|
+
if subcommand == "remove":
|
|
81
|
+
account_id = positionals[0] if positionals else ""
|
|
82
|
+
accounts = root.get("accounts")
|
|
83
|
+
if not account_id:
|
|
84
|
+
raise RuntimeError("config remove requires <accountId>.")
|
|
85
|
+
if not isinstance(accounts, dict) or account_id not in accounts:
|
|
86
|
+
raise RuntimeError(f"Unknown account: {account_id}")
|
|
87
|
+
next_root = remove_stored_account(root, account_id)
|
|
88
|
+
write_config_root(next_root)
|
|
89
|
+
return {
|
|
90
|
+
"ok": True,
|
|
91
|
+
"removedAccountId": account_id,
|
|
92
|
+
"defaultAccountId": next_root["defaultAccountId"],
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
raise RuntimeError(f"Unknown config subcommand: {subcommand}")
|
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
from __future__ import annotations
|
|
2
|
+
|
|
3
|
+
from magiccampus_sdk import MagicCampusClient, MagicCampusClientConfig
|
|
4
|
+
|
|
5
|
+
from ..config_store import read_config_root
|
|
6
|
+
from .auth import resolve_account_or_throw
|
|
7
|
+
|
|
8
|
+
|
|
9
|
+
def run_feed_command(subcommand: str | None, argv: list[str], parse_options):
|
|
10
|
+
if subcommand != "list":
|
|
11
|
+
raise RuntimeError(f"Unknown feed subcommand: {subcommand}")
|
|
12
|
+
|
|
13
|
+
options, positionals = parse_options(argv)
|
|
14
|
+
unknown = set(options) - {"account", "limit", "cursor"}
|
|
15
|
+
if unknown or positionals:
|
|
16
|
+
raise RuntimeError("feed list accepts only --limit, --cursor and --account default.")
|
|
17
|
+
root = read_config_root()
|
|
18
|
+
account = resolve_account_or_throw(root, options.get("account"))
|
|
19
|
+
client = MagicCampusClient(
|
|
20
|
+
MagicCampusClientConfig(
|
|
21
|
+
apiurl=account.apiurl,
|
|
22
|
+
apikey=account.apikey,
|
|
23
|
+
platform=account.platform,
|
|
24
|
+
)
|
|
25
|
+
)
|
|
26
|
+
return client.feed.list(limit=options.get("limit", 20), cursor=options.get("cursor"))
|
|
@@ -0,0 +1,346 @@
|
|
|
1
|
+
from __future__ import annotations
|
|
2
|
+
|
|
3
|
+
import json
|
|
4
|
+
|
|
5
|
+
from ..accounts import with_overridden_platform
|
|
6
|
+
from ..config_store import read_config_root
|
|
7
|
+
from ..utils import parse_optional_bool, trim_string
|
|
8
|
+
from .auth import resolve_account_or_throw
|
|
9
|
+
|
|
10
|
+
|
|
11
|
+
def _parse_ids(raw: object, *, option_name: str) -> list[str]:
|
|
12
|
+
text = trim_string(raw)
|
|
13
|
+
if not text:
|
|
14
|
+
return []
|
|
15
|
+
if text.startswith("["):
|
|
16
|
+
try:
|
|
17
|
+
decoded = json.loads(text)
|
|
18
|
+
except json.JSONDecodeError as error:
|
|
19
|
+
raise RuntimeError(f"{option_name} must be a JSON array or a comma-separated list.") from error
|
|
20
|
+
if not isinstance(decoded, list):
|
|
21
|
+
raise RuntimeError(f"{option_name} must be a JSON array or a comma-separated list.")
|
|
22
|
+
return [str(item).strip() for item in decoded if str(item).strip()]
|
|
23
|
+
return [item.strip() for item in text.split(",") if item.strip()]
|
|
24
|
+
|
|
25
|
+
|
|
26
|
+
def _parse_positive_int(raw: object, *, option_name: str, default: int) -> int:
|
|
27
|
+
if raw is None or raw is False:
|
|
28
|
+
return default
|
|
29
|
+
try:
|
|
30
|
+
value = int(str(raw).strip())
|
|
31
|
+
except (TypeError, ValueError) as error:
|
|
32
|
+
raise RuntimeError(f"{option_name} must be a positive integer.") from error
|
|
33
|
+
if value <= 0:
|
|
34
|
+
raise RuntimeError(f"{option_name} must be a positive integer.")
|
|
35
|
+
return value
|
|
36
|
+
|
|
37
|
+
|
|
38
|
+
def _runtime_magiccampus(account, *, sdk, ws_addr: str, api_addr: str) -> dict[str, object]:
|
|
39
|
+
return {
|
|
40
|
+
"apiUrl": account.apiurl,
|
|
41
|
+
"platform": account.platform,
|
|
42
|
+
"loginUserID": sdk.user_id,
|
|
43
|
+
"wsAddr": ws_addr,
|
|
44
|
+
"apiAddr": api_addr,
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
|
|
48
|
+
def run_group_command(subcommand: str | None, argv: list[str], parse_options):
|
|
49
|
+
options, _ = parse_options(argv)
|
|
50
|
+
root = read_config_root()
|
|
51
|
+
account = resolve_account_or_throw(root, options.get("account"))
|
|
52
|
+
account = with_overridden_platform(account, options.get("platform"))
|
|
53
|
+
|
|
54
|
+
from ..client import build_group_client, with_group_client
|
|
55
|
+
|
|
56
|
+
if subcommand == "kick":
|
|
57
|
+
group_id = trim_string(options.get("group"))
|
|
58
|
+
user_ids = _parse_ids(options.get("users"), option_name="--users")
|
|
59
|
+
reason = trim_string(options.get("reason"))
|
|
60
|
+
if not group_id or not user_ids:
|
|
61
|
+
raise RuntimeError("im group kick requires --group and --users.")
|
|
62
|
+
result = build_group_client(account).kick_members(group_id=group_id, user_ids=user_ids, reason=reason)
|
|
63
|
+
return {"ok": True, "action": "kick", "transport": "http_api", "groupId": group_id,
|
|
64
|
+
"kickedUserIDs": user_ids, "result": result}
|
|
65
|
+
|
|
66
|
+
if subcommand in {"get", "get-batch"}:
|
|
67
|
+
if subcommand == "get":
|
|
68
|
+
raw_group_id = options.get("group") or options.get("group-id")
|
|
69
|
+
group_id = "" if raw_group_id is True else trim_string(raw_group_id)
|
|
70
|
+
if not group_id:
|
|
71
|
+
raise RuntimeError("im group get requires --group <groupId>.")
|
|
72
|
+
else:
|
|
73
|
+
raw_group_ids = options.get("groups") or options.get("group-ids")
|
|
74
|
+
group_ids = [] if raw_group_ids is True else _parse_ids(raw_group_ids, option_name="--groups")
|
|
75
|
+
if not group_ids:
|
|
76
|
+
raise RuntimeError("im group get-batch requires --groups <groupId1,groupId2,...>.")
|
|
77
|
+
|
|
78
|
+
def _get_and_collect(sdk):
|
|
79
|
+
payload = sdk.get_group_info(group_id) if subcommand == "get" else sdk.get_groups_info(group_ids)
|
|
80
|
+
credentials = sdk.credentials
|
|
81
|
+
return {
|
|
82
|
+
"payload": payload,
|
|
83
|
+
"magiccampus": _runtime_magiccampus(
|
|
84
|
+
account,
|
|
85
|
+
sdk=sdk,
|
|
86
|
+
ws_addr=credentials.ws_addr if credentials else "",
|
|
87
|
+
api_addr=credentials.api_addr if credentials else "",
|
|
88
|
+
),
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
runtime = with_group_client(account, _get_and_collect)
|
|
92
|
+
result = {
|
|
93
|
+
"ok": True,
|
|
94
|
+
"accountId": account.account_id,
|
|
95
|
+
"provider": "magiccampus",
|
|
96
|
+
"resource": "group",
|
|
97
|
+
"action": "get" if subcommand == "get" else "getBatch",
|
|
98
|
+
"transport": "http_api",
|
|
99
|
+
"magiccampus": runtime["magiccampus"],
|
|
100
|
+
}
|
|
101
|
+
if subcommand == "get":
|
|
102
|
+
result.update(groupId=group_id, groupInfo=runtime["payload"])
|
|
103
|
+
else:
|
|
104
|
+
result.update(groupIds=group_ids, groupInfos=runtime["payload"])
|
|
105
|
+
return result
|
|
106
|
+
|
|
107
|
+
if subcommand == "create":
|
|
108
|
+
group_name = trim_string(options.get("name") or options.get("group-name"))
|
|
109
|
+
if not group_name:
|
|
110
|
+
raise RuntimeError("im group create requires --name <groupName>.")
|
|
111
|
+
|
|
112
|
+
member_user_ids = _parse_ids(options.get("members") or options.get("member-user-ids"), option_name="--members")
|
|
113
|
+
admin_user_ids = _parse_ids(options.get("admins") or options.get("admin-user-ids"), option_name="--admins")
|
|
114
|
+
owner_user_id = trim_string(options.get("owner") or options.get("owner-user-id"))
|
|
115
|
+
group_id = trim_string(options.get("group-id"))
|
|
116
|
+
notification = trim_string(options.get("notification"))
|
|
117
|
+
introduction = trim_string(options.get("introduction"))
|
|
118
|
+
face_url = trim_string(options.get("face-url"))
|
|
119
|
+
ex = trim_string(options.get("ex"))
|
|
120
|
+
send_message = parse_optional_bool(options.get("send-message"))
|
|
121
|
+
|
|
122
|
+
def _create_and_collect(sdk):
|
|
123
|
+
credentials = sdk.credentials
|
|
124
|
+
effective_owner_user_id = owner_user_id or sdk.user_id
|
|
125
|
+
payload = sdk.create_group(
|
|
126
|
+
group_name=group_name,
|
|
127
|
+
member_user_ids=member_user_ids,
|
|
128
|
+
admin_user_ids=admin_user_ids,
|
|
129
|
+
owner_user_id=effective_owner_user_id,
|
|
130
|
+
group_id=group_id,
|
|
131
|
+
notification=notification,
|
|
132
|
+
introduction=introduction,
|
|
133
|
+
face_url=face_url,
|
|
134
|
+
ex=ex,
|
|
135
|
+
send_message=send_message,
|
|
136
|
+
group_type=2,
|
|
137
|
+
)
|
|
138
|
+
return {
|
|
139
|
+
"payload": payload,
|
|
140
|
+
"ownerUserID": effective_owner_user_id,
|
|
141
|
+
"magiccampus": _runtime_magiccampus(
|
|
142
|
+
account,
|
|
143
|
+
sdk=sdk,
|
|
144
|
+
ws_addr=credentials.ws_addr if credentials else "",
|
|
145
|
+
api_addr=credentials.api_addr if credentials else "",
|
|
146
|
+
),
|
|
147
|
+
}
|
|
148
|
+
|
|
149
|
+
runtime = with_group_client(account, _create_and_collect)
|
|
150
|
+
group_info = runtime["payload"].get("groupInfo", {}) if isinstance(runtime["payload"], dict) else {}
|
|
151
|
+
return {
|
|
152
|
+
"ok": True,
|
|
153
|
+
"accountId": account.account_id,
|
|
154
|
+
"provider": "magiccampus",
|
|
155
|
+
"resource": "group",
|
|
156
|
+
"action": "create",
|
|
157
|
+
"transport": "http_api",
|
|
158
|
+
"groupId": group_info.get("groupID", ""),
|
|
159
|
+
"groupName": group_info.get("groupName", group_name),
|
|
160
|
+
"ownerUserID": group_info.get("ownerUserID") or runtime["ownerUserID"],
|
|
161
|
+
"memberUserIDs": member_user_ids,
|
|
162
|
+
"adminUserIDs": admin_user_ids,
|
|
163
|
+
"sendMessage": send_message,
|
|
164
|
+
"groupInfo": group_info,
|
|
165
|
+
"magiccampus": runtime["magiccampus"],
|
|
166
|
+
}
|
|
167
|
+
|
|
168
|
+
if subcommand == "invite":
|
|
169
|
+
group_id = trim_string(options.get("group") or options.get("group-id"))
|
|
170
|
+
if not group_id:
|
|
171
|
+
raise RuntimeError("im group invite requires --group <groupId>.")
|
|
172
|
+
invited_user_ids = _parse_ids(
|
|
173
|
+
options.get("users") or options.get("user-ids") or options.get("invitees"),
|
|
174
|
+
option_name="--users",
|
|
175
|
+
)
|
|
176
|
+
if not invited_user_ids:
|
|
177
|
+
raise RuntimeError("im group invite requires --users <userId1,userId2,...>.")
|
|
178
|
+
reason = trim_string(options.get("reason"))
|
|
179
|
+
send_message = parse_optional_bool(options.get("send-message"))
|
|
180
|
+
sdk = build_group_client(account)
|
|
181
|
+
payload = sdk.invite_user_to_group(
|
|
182
|
+
group_id=group_id,
|
|
183
|
+
invited_user_ids=invited_user_ids,
|
|
184
|
+
reason=reason,
|
|
185
|
+
send_message=send_message,
|
|
186
|
+
)
|
|
187
|
+
return {
|
|
188
|
+
"ok": True,
|
|
189
|
+
"accountId": account.account_id,
|
|
190
|
+
"provider": "magiccampus",
|
|
191
|
+
"resource": "group",
|
|
192
|
+
"action": "invite",
|
|
193
|
+
"transport": "http_api",
|
|
194
|
+
"groupId": group_id,
|
|
195
|
+
"invitedUserIDs": invited_user_ids,
|
|
196
|
+
"reason": reason,
|
|
197
|
+
"sendMessage": send_message,
|
|
198
|
+
"result": payload,
|
|
199
|
+
"magiccampus": _runtime_magiccampus(
|
|
200
|
+
account,
|
|
201
|
+
sdk=sdk,
|
|
202
|
+
ws_addr="",
|
|
203
|
+
api_addr="",
|
|
204
|
+
),
|
|
205
|
+
}
|
|
206
|
+
|
|
207
|
+
if subcommand == "dismiss":
|
|
208
|
+
group_id = trim_string(options.get("group") or options.get("group-id"))
|
|
209
|
+
if not group_id:
|
|
210
|
+
raise RuntimeError("im group dismiss requires --group <groupId>.")
|
|
211
|
+
delete_member = bool(parse_optional_bool(options.get("delete-member"), fallback=False))
|
|
212
|
+
send_message = parse_optional_bool(options.get("send-message"))
|
|
213
|
+
|
|
214
|
+
def _dismiss_and_collect(sdk):
|
|
215
|
+
credentials = sdk.credentials
|
|
216
|
+
payload = sdk.dismiss_group(
|
|
217
|
+
group_id=group_id,
|
|
218
|
+
delete_member=delete_member,
|
|
219
|
+
send_message=send_message,
|
|
220
|
+
)
|
|
221
|
+
return {
|
|
222
|
+
"payload": payload,
|
|
223
|
+
"magiccampus": _runtime_magiccampus(
|
|
224
|
+
account,
|
|
225
|
+
sdk=sdk,
|
|
226
|
+
ws_addr=credentials.ws_addr if credentials else "",
|
|
227
|
+
api_addr=credentials.api_addr if credentials else "",
|
|
228
|
+
),
|
|
229
|
+
}
|
|
230
|
+
|
|
231
|
+
runtime = with_group_client(account, _dismiss_and_collect)
|
|
232
|
+
return {
|
|
233
|
+
"ok": True,
|
|
234
|
+
"accountId": account.account_id,
|
|
235
|
+
"provider": "magiccampus",
|
|
236
|
+
"resource": "group",
|
|
237
|
+
"action": "dismiss",
|
|
238
|
+
"transport": "http_api",
|
|
239
|
+
"groupId": group_id,
|
|
240
|
+
"deleteMember": delete_member,
|
|
241
|
+
"sendMessage": send_message,
|
|
242
|
+
"result": runtime["payload"],
|
|
243
|
+
"magiccampus": runtime["magiccampus"],
|
|
244
|
+
}
|
|
245
|
+
|
|
246
|
+
if subcommand == "transfer-owner":
|
|
247
|
+
group_id = trim_string(options.get("group") or options.get("group-id"))
|
|
248
|
+
if not group_id:
|
|
249
|
+
raise RuntimeError("im group transfer-owner requires --group <groupId>.")
|
|
250
|
+
new_owner_user_id = trim_string(options.get("new-owner") or options.get("new-owner-user-id"))
|
|
251
|
+
if not new_owner_user_id:
|
|
252
|
+
raise RuntimeError("im group transfer-owner requires --new-owner <userId>.")
|
|
253
|
+
old_owner_user_id = trim_string(options.get("old-owner") or options.get("old-owner-user-id"))
|
|
254
|
+
|
|
255
|
+
def _transfer_and_collect(sdk):
|
|
256
|
+
credentials = sdk.credentials
|
|
257
|
+
payload = sdk.transfer_group_owner(
|
|
258
|
+
group_id=group_id,
|
|
259
|
+
new_owner_user_id=new_owner_user_id,
|
|
260
|
+
old_owner_user_id=old_owner_user_id,
|
|
261
|
+
)
|
|
262
|
+
return {
|
|
263
|
+
"payload": payload,
|
|
264
|
+
"magiccampus": _runtime_magiccampus(
|
|
265
|
+
account,
|
|
266
|
+
sdk=sdk,
|
|
267
|
+
ws_addr=credentials.ws_addr if credentials else "",
|
|
268
|
+
api_addr=credentials.api_addr if credentials else "",
|
|
269
|
+
),
|
|
270
|
+
}
|
|
271
|
+
|
|
272
|
+
runtime = with_group_client(account, _transfer_and_collect)
|
|
273
|
+
return {
|
|
274
|
+
"ok": True,
|
|
275
|
+
"accountId": account.account_id,
|
|
276
|
+
"provider": "magiccampus",
|
|
277
|
+
"resource": "group",
|
|
278
|
+
"action": "transferOwner",
|
|
279
|
+
"transport": "http_api",
|
|
280
|
+
"groupId": group_id,
|
|
281
|
+
"newOwnerUserID": new_owner_user_id,
|
|
282
|
+
"oldOwnerUserID": old_owner_user_id or runtime["magiccampus"]["loginUserID"],
|
|
283
|
+
"result": runtime["payload"],
|
|
284
|
+
"magiccampus": runtime["magiccampus"],
|
|
285
|
+
}
|
|
286
|
+
|
|
287
|
+
raise RuntimeError(f"Unknown im group subcommand: {subcommand}")
|
|
288
|
+
|
|
289
|
+
|
|
290
|
+
def run_chat_members_command(subcommand: str | None, argv: list[str], parse_options):
|
|
291
|
+
if subcommand != "get":
|
|
292
|
+
raise RuntimeError(f"Unknown im chat.members subcommand: {subcommand}")
|
|
293
|
+
|
|
294
|
+
options, _ = parse_options(argv)
|
|
295
|
+
root = read_config_root()
|
|
296
|
+
account = resolve_account_or_throw(root, options.get("account"))
|
|
297
|
+
account = with_overridden_platform(account, options.get("platform"))
|
|
298
|
+
|
|
299
|
+
raw_group_id = options.get("group") or options.get("group-id")
|
|
300
|
+
group_id = "" if raw_group_id is True else trim_string(raw_group_id)
|
|
301
|
+
if not group_id:
|
|
302
|
+
raise RuntimeError("im chat.members get requires --group <groupId>.")
|
|
303
|
+
|
|
304
|
+
raw_keyword = options.get("keyword")
|
|
305
|
+
if raw_keyword is True:
|
|
306
|
+
raise RuntimeError("im chat.members get --keyword requires a value.")
|
|
307
|
+
keyword = trim_string(raw_keyword)
|
|
308
|
+
page_number = _parse_positive_int(
|
|
309
|
+
options.get("page-number"),
|
|
310
|
+
option_name="--page-number",
|
|
311
|
+
default=1,
|
|
312
|
+
)
|
|
313
|
+
show_number = _parse_positive_int(
|
|
314
|
+
options.get("show-number"),
|
|
315
|
+
option_name="--show-number",
|
|
316
|
+
default=100,
|
|
317
|
+
)
|
|
318
|
+
|
|
319
|
+
from ..client import build_group_client
|
|
320
|
+
|
|
321
|
+
sdk = build_group_client(account)
|
|
322
|
+
payload = sdk.get_group_members(
|
|
323
|
+
group_id=group_id,
|
|
324
|
+
keyword=keyword,
|
|
325
|
+
page_number=page_number,
|
|
326
|
+
show_number=show_number,
|
|
327
|
+
)
|
|
328
|
+
return {
|
|
329
|
+
"ok": True,
|
|
330
|
+
"accountId": account.account_id,
|
|
331
|
+
"provider": "magiccampus",
|
|
332
|
+
"resource": "chat.members",
|
|
333
|
+
"action": "get",
|
|
334
|
+
"transport": "http_api",
|
|
335
|
+
"groupId": group_id,
|
|
336
|
+
"keyword": keyword,
|
|
337
|
+
"pageNumber": page_number,
|
|
338
|
+
"showNumber": show_number,
|
|
339
|
+
"result": payload,
|
|
340
|
+
"magiccampus": _runtime_magiccampus(
|
|
341
|
+
account,
|
|
342
|
+
sdk=sdk,
|
|
343
|
+
ws_addr="",
|
|
344
|
+
api_addr="",
|
|
345
|
+
),
|
|
346
|
+
}
|
|
@@ -0,0 +1,47 @@
|
|
|
1
|
+
from __future__ import annotations
|
|
2
|
+
|
|
3
|
+
from .group import run_chat_members_command, run_group_command
|
|
4
|
+
from .notify import run_notify_command
|
|
5
|
+
from .reaction import run_reaction_command
|
|
6
|
+
|
|
7
|
+
|
|
8
|
+
def run_im_command(subcommand: str | None, argv: list[str], parse_options):
|
|
9
|
+
nested = argv[0] if argv else None
|
|
10
|
+
nested_argv = argv[1:] if argv else []
|
|
11
|
+
|
|
12
|
+
if subcommand == "reaction":
|
|
13
|
+
return run_reaction_command(nested, nested_argv, parse_options)
|
|
14
|
+
|
|
15
|
+
if subcommand == "notify":
|
|
16
|
+
alias = {
|
|
17
|
+
"send": "+send",
|
|
18
|
+
"send-link-card": "+send-link-card",
|
|
19
|
+
"history": "+history",
|
|
20
|
+
"+send": "+send",
|
|
21
|
+
"+history": "+history",
|
|
22
|
+
}.get(nested)
|
|
23
|
+
if alias is None:
|
|
24
|
+
raise RuntimeError(f"Unknown im notify subcommand: {nested}")
|
|
25
|
+
return run_notify_command(alias, nested_argv, parse_options)
|
|
26
|
+
|
|
27
|
+
if subcommand == "group":
|
|
28
|
+
alias = {
|
|
29
|
+
"get": "get",
|
|
30
|
+
"get-batch": "get-batch",
|
|
31
|
+
"create": "create",
|
|
32
|
+
"kick": "kick",
|
|
33
|
+
"invite": "invite",
|
|
34
|
+
"dismiss": "dismiss",
|
|
35
|
+
"transfer-owner": "transfer-owner",
|
|
36
|
+
"transfer_owner": "transfer-owner",
|
|
37
|
+
}.get(nested)
|
|
38
|
+
if alias is None:
|
|
39
|
+
raise RuntimeError(f"Unknown im group subcommand: {nested}")
|
|
40
|
+
return run_group_command(alias, nested_argv, parse_options)
|
|
41
|
+
|
|
42
|
+
if subcommand == "chat.members":
|
|
43
|
+
if nested != "get":
|
|
44
|
+
raise RuntimeError(f"Unknown im chat.members subcommand: {nested}")
|
|
45
|
+
return run_chat_members_command("get", nested_argv, parse_options)
|
|
46
|
+
|
|
47
|
+
raise RuntimeError(f"Unknown im subcommand: {subcommand}")
|
|
@@ -0,0 +1,81 @@
|
|
|
1
|
+
from __future__ import annotations
|
|
2
|
+
|
|
3
|
+
from ..accounts import with_overridden_platform
|
|
4
|
+
from ..config_store import read_config_root
|
|
5
|
+
from ..notify import build_history_result, build_send_result, execute_send, select_history, resolve_history_input, resolve_link_card_input, resolve_send_input, validate_send_account
|
|
6
|
+
from .auth import resolve_account_or_throw
|
|
7
|
+
|
|
8
|
+
|
|
9
|
+
def run_notify_command(subcommand: str | None, argv: list[str], parse_options):
|
|
10
|
+
options, _ = parse_options(argv)
|
|
11
|
+
root = read_config_root()
|
|
12
|
+
account = resolve_account_or_throw(root, options.get("account"))
|
|
13
|
+
account = with_overridden_platform(account, options.get("platform"))
|
|
14
|
+
|
|
15
|
+
if subcommand in {"+send", "+send-link-card"}:
|
|
16
|
+
send_input = resolve_link_card_input(options) if subcommand == "+send-link-card" else resolve_send_input(options)
|
|
17
|
+
compact_link_card = subcommand == "+send-link-card" and options.get("debug") is not True
|
|
18
|
+
|
|
19
|
+
if send_input.dry_run:
|
|
20
|
+
is_inline_mode = send_input.mode in {"text", "markdown", "custom", "link_card"}
|
|
21
|
+
extra = {
|
|
22
|
+
"preview": send_input.value if is_inline_mode else None,
|
|
23
|
+
"source": send_input.cover_image if send_input.mode == "link_card" else (None if is_inline_mode else send_input.value),
|
|
24
|
+
"fileName": send_input.name,
|
|
25
|
+
"coverImage": send_input.cover_image,
|
|
26
|
+
}
|
|
27
|
+
result = build_send_result(account, send_input, extra=extra, transport="http_api")
|
|
28
|
+
return {"ok": result["ok"], "serverMsgID": "", "sendTime": 0} if compact_link_card else result
|
|
29
|
+
|
|
30
|
+
validate_send_account(account, send_input)
|
|
31
|
+
|
|
32
|
+
from ..client import with_http_send_client
|
|
33
|
+
|
|
34
|
+
def _send_and_collect(client):
|
|
35
|
+
credentials = client.credentials
|
|
36
|
+
return {
|
|
37
|
+
"ack": execute_send(client, send_input),
|
|
38
|
+
"loginUserID": client.user_id,
|
|
39
|
+
"wsAddr": credentials.ws_addr if credentials else "",
|
|
40
|
+
"apiAddr": credentials.api_addr if credentials else "",
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
payload = with_http_send_client(account, _send_and_collect)
|
|
44
|
+
ack = payload["ack"]
|
|
45
|
+
is_inline_mode = send_input.mode in {"text", "markdown", "custom", "link_card"}
|
|
46
|
+
result = build_send_result(
|
|
47
|
+
account,
|
|
48
|
+
send_input,
|
|
49
|
+
extra={
|
|
50
|
+
"preview": send_input.value if is_inline_mode else None,
|
|
51
|
+
"source": send_input.cover_image if send_input.mode == "link_card" else (None if is_inline_mode else send_input.value),
|
|
52
|
+
"fileName": send_input.name,
|
|
53
|
+
"coverImage": send_input.cover_image,
|
|
54
|
+
"clientMsgID": ack.get("clientMsgID", ""),
|
|
55
|
+
"serverMsgID": ack.get("serverMsgID", ""),
|
|
56
|
+
"sendTime": ack.get("sendTime", 0),
|
|
57
|
+
"magiccampus": {
|
|
58
|
+
**build_send_result(account, send_input, transport="http_api")["magiccampus"],
|
|
59
|
+
"loginUserID": payload["loginUserID"],
|
|
60
|
+
"wsAddr": payload["wsAddr"],
|
|
61
|
+
"apiAddr": payload["apiAddr"],
|
|
62
|
+
"sendApi": account.apiurl.rstrip("/") + "/api/v1/im/sendMessage",
|
|
63
|
+
},
|
|
64
|
+
},
|
|
65
|
+
transport="http_api",
|
|
66
|
+
)
|
|
67
|
+
if compact_link_card:
|
|
68
|
+
return {"ok": result["ok"], "serverMsgID": result["serverMsgID"], "sendTime": result["sendTime"]}
|
|
69
|
+
return result
|
|
70
|
+
|
|
71
|
+
if subcommand == "+history":
|
|
72
|
+
history_input = resolve_history_input(options)
|
|
73
|
+
|
|
74
|
+
from ..client import build_group_client
|
|
75
|
+
client = build_group_client(account)
|
|
76
|
+
data = client.query_messages(limit=history_input.limit,
|
|
77
|
+
include_groups=history_input.target["kind"] == "group") or {}
|
|
78
|
+
messages = select_history(data, login_user_id=client.user_id, history_input=history_input)
|
|
79
|
+
return build_history_result(account, history_input, messages)
|
|
80
|
+
|
|
81
|
+
raise RuntimeError(f"Unknown notify subcommand: {subcommand}")
|
|
@@ -0,0 +1,49 @@
|
|
|
1
|
+
from __future__ import annotations
|
|
2
|
+
|
|
3
|
+
import json
|
|
4
|
+
|
|
5
|
+
from magiccampus_sdk import MagicCampusClient, MagicCampusClientConfig
|
|
6
|
+
|
|
7
|
+
from ..config_store import read_config_root
|
|
8
|
+
from ..utils import trim_string
|
|
9
|
+
from .auth import resolve_account_or_throw
|
|
10
|
+
|
|
11
|
+
|
|
12
|
+
def run_playgroup_command(subcommand: str | None, argv: list[str], parse_options):
|
|
13
|
+
if subcommand not in {"list", "get", "join", "invite-members"}:
|
|
14
|
+
raise RuntimeError(f"Unknown playgroup subcommand: {subcommand}")
|
|
15
|
+
options, positionals = parse_options(argv)
|
|
16
|
+
allowed = {"account"} | ({"id"} if subcommand != "list" else set())
|
|
17
|
+
if subcommand == "invite-members":
|
|
18
|
+
allowed |= {"users", "reason"}
|
|
19
|
+
if positionals or set(options) - allowed:
|
|
20
|
+
raise RuntimeError(f"Unsupported arguments for playgroup {subcommand}.")
|
|
21
|
+
space_id = options.get("id")
|
|
22
|
+
if subcommand != "list":
|
|
23
|
+
if not isinstance(space_id, str) or not trim_string(space_id):
|
|
24
|
+
raise RuntimeError(f"playgroup {subcommand} requires --id <spaceId>.")
|
|
25
|
+
space_id = trim_string(space_id)
|
|
26
|
+
user_ids = None
|
|
27
|
+
if subcommand == "invite-members":
|
|
28
|
+
raw_users = options.get("users")
|
|
29
|
+
if not isinstance(raw_users, str) or not raw_users.strip():
|
|
30
|
+
raise RuntimeError("playgroup invite-members requires --users <id1,id2|JSON-array>.")
|
|
31
|
+
user_ids = json.loads(raw_users) if raw_users.lstrip().startswith("[") else raw_users.split(",")
|
|
32
|
+
if (not isinstance(user_ids, list) or not user_ids
|
|
33
|
+
or any(not isinstance(value, str) or not value.strip() for value in user_ids)):
|
|
34
|
+
raise RuntimeError("--users must contain non-empty user ID strings.")
|
|
35
|
+
if "reason" in options and not isinstance(options["reason"], str):
|
|
36
|
+
raise RuntimeError("--reason requires a value.")
|
|
37
|
+
account = resolve_account_or_throw(read_config_root(), options.get("account"))
|
|
38
|
+
client = MagicCampusClient(MagicCampusClientConfig(
|
|
39
|
+
apiurl=account.apiurl, apikey=account.apikey, platform=account.platform,
|
|
40
|
+
))
|
|
41
|
+
if subcommand == "list":
|
|
42
|
+
return client.playgroup.list()
|
|
43
|
+
if subcommand == "get":
|
|
44
|
+
return client.playgroup.get(space_id)
|
|
45
|
+
if subcommand == "join":
|
|
46
|
+
result = client.playgroup.join(space_id)
|
|
47
|
+
else:
|
|
48
|
+
result = client.playgroup.invite_members(space_id, user_ids, reason=options.get("reason"))
|
|
49
|
+
return {"ok": True, "resource": "playgroup", "action": subcommand, "result": result}
|