This commit is contained in:
nose
2025-12-19 03:25:52 -08:00
parent 52cf3f5c9f
commit d3edd6420c
9 changed files with 221 additions and 35 deletions

View File

@@ -15,6 +15,24 @@ import pipeline as ctx
_MATRIX_PENDING_ITEMS_KEY = "matrix_pending_items"
_MATRIX_PENDING_TEXT_KEY = "matrix_pending_text"
def _extract_text_arg(args: Sequence[str]) -> str:
"""Extract a `-text <value>` argument from a cmdnat args list."""
if not args:
return ""
try:
tokens = list(args)
except Exception:
return ""
for i, tok in enumerate(tokens):
try:
if str(tok).lower() == "-text" and i + 1 < len(tokens):
return str(tokens[i + 1] or "").strip()
except Exception:
continue
return ""
def _normalize_to_list(value: Any) -> List[Any]:
@@ -341,8 +359,16 @@ def _run(result: Any, args: Sequence[str], config: Dict[str, Any]) -> int:
log(f"Matrix not available: {exc}", file=sys.stderr)
return 1
text_value = _extract_text_arg(args)
if not text_value:
try:
text_value = str(ctx.load_value(_MATRIX_PENDING_TEXT_KEY, default="") or "").strip()
except Exception:
text_value = ""
any_failed = False
for rid in room_ids:
sent_any_for_room = False
for item in items:
file_path = _resolve_upload_path(item, config)
if not file_path:
@@ -350,16 +376,29 @@ def _run(result: Any, args: Sequence[str], config: Dict[str, Any]) -> int:
log("Matrix upload requires a local file (path) or a direct URL on the selected item", file=sys.stderr)
continue
try:
link = provider.upload_to_room(file_path, rid)
link = provider.upload_to_room(file_path, rid, pipe_obj=item)
debug(f"✓ Sent {Path(file_path).name} -> {rid}")
if link:
log(link)
sent_any_for_room = True
except Exception as exc:
any_failed = True
log(f"Matrix send failed for {Path(file_path).name}: {exc}", file=sys.stderr)
# Optional caption-like follow-up message (sent once per room).
if text_value and sent_any_for_room:
try:
provider.send_text_to_room(text_value, rid)
except Exception as exc:
any_failed = True
log(f"Matrix text send failed: {exc}", file=sys.stderr)
# Clear pending items once we've attempted to send.
ctx.store_value(_MATRIX_PENDING_ITEMS_KEY, [])
try:
ctx.store_value(_MATRIX_PENDING_TEXT_KEY, "")
except Exception:
pass
return 1 if any_failed else 0
# Default stage: show rooms, then wait for @N selection to resume sending.
@@ -369,6 +408,10 @@ def _run(result: Any, args: Sequence[str], config: Dict[str, Any]) -> int:
return 1
ctx.store_value(_MATRIX_PENDING_ITEMS_KEY, selected_items)
try:
ctx.store_value(_MATRIX_PENDING_TEXT_KEY, _extract_text_arg(args))
except Exception:
pass
from Provider.matrix import Matrix
try:
@@ -431,6 +474,7 @@ CMDLET = Cmdlet(
usage="@N | .matrix",
arg=[
CmdletArg(name="send", type="bool", description="(internal) Send to selected room(s)", required=False),
CmdletArg(name="text", type="string", description="Send a follow-up text message after each upload (caption-like)", required=False),
],
exec=_run
)