comparison sat_frontends/jp/cmd_pubsub.py @ 3866:915fb230cb28

cli (blog, pubsub): new `attachments` subcommands: `pubsub/attachments` subcommands has a `get` and a `set` subcommand to manage attachments of a pubsub item. `blog` has the same subcommands, the only difference is that if `--node` is not specified, the default blog namespace will be used. rel 370
author Goffi <goffi@goffi.org>
date Wed, 20 Jul 2022 17:55:11 +0200
parents f599e0e36444
children 1ab5fb468a41
comparison
equal deleted inserted replaced
3865:59fbb66b2923 3866:915fb230cb28
2417 self.display_uri(jid_) 2417 self.display_uri(jid_)
2418 else: 2418 else:
2419 self.display_uri(None) 2419 self.display_uri(None)
2420 2420
2421 2421
2422 class AttachmentGet(base.CommandBase):
2423 def __init__(self, host):
2424 super().__init__(
2425 host,
2426 "get",
2427 use_output=C.OUTPUT_LIST_DICT,
2428 use_pubsub=True,
2429 pubsub_flags={C.SERVICE, C.NODE, C.SINGLE_ITEM},
2430 help=_("get data attached to an item"),
2431 )
2432
2433 def add_parser_options(self):
2434 self.parser.add_argument(
2435 "-j",
2436 "--jid",
2437 action="append",
2438 dest="jids",
2439 help=_(
2440 "get attached data published only by those JIDs (DEFAULT: get all "
2441 "attached data)"
2442 )
2443 )
2444
2445 async def start(self):
2446 try:
2447 attached_data, __ = await self.host.bridge.psAttachmentsGet(
2448 self.args.service,
2449 self.args.node,
2450 self.args.item,
2451 self.args.jids or [],
2452 "",
2453 self.profile,
2454 )
2455 except Exception as e:
2456 self.disp(f"can't get attached data: {e}", error=True)
2457 self.host.quit(C.EXIT_BRIDGE_ERRBACK)
2458 else:
2459 attached_data = data_format.deserialise(attached_data, type_check=list)
2460 await self.output(attached_data)
2461 self.host.quit(C.EXIT_OK)
2462
2463
2464 class AttachmentSet(base.CommandBase):
2465 def __init__(self, host):
2466 super().__init__(
2467 host,
2468 "set",
2469 use_pubsub=True,
2470 pubsub_flags={C.SERVICE, C.NODE, C.SINGLE_ITEM},
2471 help=_("attach data to an item"),
2472 )
2473
2474 def add_parser_options(self):
2475 self.parser.add_argument(
2476 "-R",
2477 "--replace",
2478 action="store_true",
2479 help=_(
2480 "replace previous versions of attachments (DEFAULT: update previous "
2481 "version)"
2482 )
2483 )
2484 self.parser.add_argument(
2485 "-N",
2486 "--noticed",
2487 metavar="BOOLEAN",
2488 nargs="?",
2489 default="keep",
2490 help=_("mark item as (un)noticed (DEFAULT: keep current value))")
2491 )
2492 self.parser.add_argument(
2493 "-r",
2494 "--reactions",
2495 # FIXME: to be replace by "extend" when we stop supporting python 3.7
2496 action="append",
2497 help=_("add emojis to react to an item")
2498 )
2499
2500 async def start(self):
2501 mb_data = {
2502 "service": self.args.service,
2503 "node": self.args.node,
2504 "id": self.args.item,
2505 "extra": {}
2506 }
2507 operation = "replace" if self.args.replace else "update"
2508 if self.args.noticed != "keep":
2509 if self.args.noticed is None:
2510 self.args.noticed = C.BOOL_TRUE
2511 mb_data["extra"]["noticed"] = C.bool(self.args.noticed)
2512
2513 if self.args.reactions is not None:
2514 mb_data["extra"]["reaction"] = {
2515 "operation": operation,
2516 "reactions": [r for emojis in self.args.reactions for r in emojis]
2517 }
2518
2519 if not mb_data["extra"]:
2520 self.parser.error(_("At leat one attachment must be specified."))
2521
2522 try:
2523 await self.host.bridge.psAttachmentsSet(
2524 data_format.serialise(mb_data),
2525 self.profile,
2526 )
2527 except Exception as e:
2528 self.disp(f"can't attach data to item: {e}", error=True)
2529 self.host.quit(C.EXIT_BRIDGE_ERRBACK)
2530 else:
2531 self.disp("data attached")
2532 self.host.quit(C.EXIT_OK)
2533
2534
2535 class Attachments(base.CommandBase):
2536 subcommands = (AttachmentGet, AttachmentSet)
2537
2538 def __init__(self, host):
2539 super().__init__(
2540 host,
2541 "attachments",
2542 use_profile=False,
2543 help=_("set or retrieve items attachments"),
2544 )
2545
2546
2422 class HookCreate(base.CommandBase): 2547 class HookCreate(base.CommandBase):
2423 def __init__(self, host): 2548 def __init__(self, host):
2424 base.CommandBase.__init__( 2549 base.CommandBase.__init__(
2425 self, 2550 self,
2426 host, 2551 host,
2578 Subscriptions, 2703 Subscriptions,
2579 Affiliations, 2704 Affiliations,
2580 Reference, 2705 Reference,
2581 Search, 2706 Search,
2582 Transform, 2707 Transform,
2708 Attachments,
2583 Hook, 2709 Hook,
2584 Uri, 2710 Uri,
2585 Node, 2711 Node,
2586 Cache, 2712 Cache,
2587 ) 2713 )