changeset 3255:012e89fb2dd1

jp (roster): new roster/set command
author Goffi <goffi@goffi.org>
date Tue, 14 Apr 2020 21:01:04 +0200
parents 6cf4bd6972c2
children df26f1a9020a
files doc/jp/roster.rst sat_frontends/jp/cmd_roster.py
diffstat 2 files changed, 67 insertions(+), 3 deletions(-) [+]
line wrap: on
line diff
--- a/doc/jp/roster.rst	Tue Apr 14 21:00:33 2020 +0200
+++ b/doc/jp/roster.rst	Tue Apr 14 21:01:04 2020 +0200
@@ -49,6 +49,29 @@
 
   $ jp roster get -O json
 
+set
+===
+
+Set metadata for a roster entity. Only ``name`` and ``groups`` can be set, ``name`` being
+the user chosed name to use with a contact.
+
+By default, values are appended, i.e. if ``name`` is not set it won't delete existing one,
+and ``groups`` are appended to existing one. However, if you use the ``-R, --replace``
+option, former values will be entirely replaced by given ones (i.e. if you don't use ``-n
+NAME, --name NAME`` option, the former one will be deleted, and any former group no added
+using ``-g GROUP, --group GROUP`` will be removed).
+
+example
+-------
+
+Set a name used to privately identify your contact Louise::
+
+  $ jp roster set -n Enjolras louise@example.net
+
+Replace all groups of Pierre, to add him only to ``friends`` and ``housemates``::
+
+  $ jp roster set -g friends -g housemates pierre@example.net
+
 stats
 =====
 
--- a/sat_frontends/jp/cmd_roster.py	Tue Apr 14 21:00:33 2020 +0200
+++ b/sat_frontends/jp/cmd_roster.py	Tue Apr 14 21:01:04 2020 +0200
@@ -1,7 +1,6 @@
 #!/usr/bin/env python3
 
-
-# jp: a SAT command line tool
+# jp: a SàT command line tool
 # Copyright (C) 2009-2020 Jérôme Poisson (goffi@goffi.org)
 # Copyright (C) 2003-2016 Adrien Cossa (souliane@mailoo.org)
 
@@ -84,6 +83,48 @@
         self.host.quit()
 
 
+class Set(base.CommandBase):
+
+    def __init__(self, host):
+        super().__init__(host, 'set', help=_('set metadata for a roster entity'))
+
+    def add_parser_options(self):
+        self.parser.add_argument(
+            "-n", "--name", default="", help=_('name to use for this entity'))
+        self.parser.add_argument(
+            "-g", "--group", dest='groups', action='append', metavar='GROUP', default=[],
+            help=_('groups for this entity'))
+        self.parser.add_argument(
+            "-R", "--replace", action="store_true",
+            help=_("replace all metadata instead of adding them"))
+        self.parser.add_argument(
+            "jid", help=_("jid of the roster entity"))
+
+    async def start(self):
+
+        if self.args.replace:
+            name = self.args.name
+            groups = self.args.groups
+        else:
+            try:
+                entity_data = await self.host.bridge.contactGet(
+                    self.args.jid, self.host.profile)
+            except Exception as e:
+                self.disp(f"error while retrieving the contact: {e}", error=True)
+                self.host.quit(C.EXIT_BRIDGE_ERRBACK)
+            name = self.args.name or entity_data[0].get('name') or ''
+            groups = set(entity_data[1])
+            groups = list(groups.union(self.args.groups))
+
+        try:
+            await self.host.bridge.updateContact(
+                self.args.jid, name, groups, self.host.profile)
+        except Exception as e:
+            self.disp(f"error while updating the contact: {e}", error=True)
+            self.host.quit(C.EXIT_BRIDGE_ERRBACK)
+        self.host.quit()
+
+
 class Stats(base.CommandBase):
 
     def __init__(self, host):
@@ -252,7 +293,7 @@
 
 
 class Roster(base.CommandBase):
-    subcommands = (Get, Stats, Purge, Resync)
+    subcommands = (Get, Set, Stats, Purge, Resync)
 
     def __init__(self, host):
         super(Roster, self).__init__(