comparison sat_frontends/jp/cmd_identity.py @ 3254:6cf4bd6972c2

core, frontends: avatar refactoring: /!\ huge commit Avatar logic has been reworked around the IDENTITY plugin: plugins able to handle avatar or other identity related metadata (like nicknames) register to IDENTITY plugin in the same way as for other features like download/upload. Once registered, IDENTITY plugin will call them when suitable in order of priority, and handle caching. Methods to manage those metadata from frontend now use serialised data. For now `avatar` and `nicknames` are handled: - `avatar` is now a dict with `path` + metadata like `media_type`, instead of just a string path - `nicknames` is now a list of nicknames in order of priority. This list is never empty, and `nicknames[0]` should be the preferred nickname to use by frontends in most cases. In addition to contact specified nicknames, user set nickname (the one set in roster) is used in priority when available. Among the side changes done with this commit, there are: - a new `contactGet` bridge method to get roster metadata for a single contact - SatPresenceProtocol.send returns a Deferred to check when it has actually been sent - memory's methods to handle entities data now use `client` as first argument - metadata filter can be specified with `getIdentity` - `getAvatar` and `setAvatar` are now part of the IDENTITY plugin instead of XEP-0054 (and there signature has changed) - `isRoom` and `getBareOrFull` are now part of XEP-0045 plugin - jp avatar/get command uses `xdg-open` first when available for `--show` flag - `--no-cache` has been added to jp avatar/get and identity/get - jp identity/set has been simplified, explicit options (`--nickname` only for now) are used instead of `--field`. `--field` may come back in the future if necessary for extra data. - QuickContactList `SetContact` now handle None as a value, and doesn't use it to delete the metadata anymore - improved cache handling for `metadata` and `nicknames` in quick frontend - new `default` argument in QuickContactList `getCache`
author Goffi <goffi@goffi.org>
date Tue, 14 Apr 2020 21:00:33 +0200
parents 559a625a236b
children be6d91572633
comparison
equal deleted inserted replaced
3253:1af840e84af7 3254:6cf4bd6972c2
19 19
20 20
21 from . import base 21 from . import base
22 from sat.core.i18n import _ 22 from sat.core.i18n import _
23 from sat_frontends.jp.constants import Const as C 23 from sat_frontends.jp.constants import Const as C
24 from sat.tools.common import data_format
24 25
25 __commands__ = ["Identity"] 26 __commands__ = ["Identity"]
26 27
27 28
28 class Get(base.CommandBase): 29 class Get(base.CommandBase):
36 help=_("get identity data"), 37 help=_("get identity data"),
37 ) 38 )
38 39
39 def add_parser_options(self): 40 def add_parser_options(self):
40 self.parser.add_argument( 41 self.parser.add_argument(
42 "--no-cache", action="store_true", help=_("do no use cached values")
43 )
44 self.parser.add_argument(
41 "jid", help=_("entity to check") 45 "jid", help=_("entity to check")
42 ) 46 )
43 47
44 async def start(self): 48 async def start(self):
45 jid_ = (await self.host.check_jids([self.args.jid]))[0] 49 jid_ = (await self.host.check_jids([self.args.jid]))[0]
46 try: 50 try:
47 data = await self.host.bridge.identityGet( 51 data = await self.host.bridge.identityGet(
48 jid_, 52 jid_,
49 self.profile, 53 [],
54 not self.args.no_cache,
55 self.profile
50 ) 56 )
51 except Exception as e: 57 except Exception as e:
52 self.disp(f"can't get identity data: {e}", error=True) 58 self.disp(f"can't get identity data: {e}", error=True)
53 self.host.quit(C.EXIT_BRIDGE_ERRBACK) 59 self.host.quit(C.EXIT_BRIDGE_ERRBACK)
54 else: 60 else:
61 data = data_format.deserialise(data)
55 await self.output(data) 62 await self.output(data)
56 self.host.quit() 63 self.host.quit()
57 64
58 65
59 class Set(base.CommandBase): 66 class Set(base.CommandBase):
60 def __init__(self, host): 67 def __init__(self, host):
61 super(Set, self).__init__(host, "set", help=_("modify an existing event")) 68 super(Set, self).__init__(host, "set", help=_("update identity data"))
62 69
63 def add_parser_options(self): 70 def add_parser_options(self):
64 self.parser.add_argument( 71 self.parser.add_argument(
65 "-f", 72 "-n",
66 "--field", 73 "--nickname",
67 action="append", 74 action="append",
68 nargs=2, 75 dest="nicknames",
69 dest="fields",
70 metavar=("KEY", "VALUE"),
71 required=True, 76 required=True,
72 help=_("identity field(s) to set"), 77 help=_("nicknames of the entity"),
73 ) 78 )
74 79
75 async def start(self): 80 async def start(self):
76 fields = dict(self.args.fields) 81 id_data = {
82 "nicknames": self.args.nicknames,
83 }
77 try: 84 try:
78 self.host.bridge.identitySet( 85 self.host.bridge.identitySet(
79 fields, 86 data_format.serialise(id_data),
80 self.profile, 87 self.profile,
81 ) 88 )
82 except Exception as e: 89 except Exception as e:
83 self.disp(f"can't set identity data: {e}", error=True) 90 self.disp(f"can't set identity data: {e}", error=True)
84 self.host.quit(C.EXIT_BRIDGE_ERRBACK) 91 self.host.quit(C.EXIT_BRIDGE_ERRBACK)