comparison sat_frontends/jp/cmd_avatar.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
23 import asyncio 23 import asyncio
24 from . import base 24 from . import base
25 from sat.core.i18n import _ 25 from sat.core.i18n import _
26 from sat_frontends.jp.constants import Const as C 26 from sat_frontends.jp.constants import Const as C
27 from sat.tools import config 27 from sat.tools import config
28 from sat.tools.common import data_format
28 29
29 30
30 __commands__ = ["Avatar"] 31 __commands__ = ["Avatar"]
31 DISPLAY_CMD = ["xv", "display", "gwenview", "showtell"] 32 DISPLAY_CMD = ["xdg-open", "xv", "display", "gwenview", "showtell"]
32 33
33 34
34 class Get(base.CommandBase): 35 class Get(base.CommandBase):
35 def __init__(self, host): 36 def __init__(self, host):
36 super(Get, self).__init__( 37 super(Get, self).__init__(
37 host, "get", use_verbose=True, help=_("retrieve avatar of an entity") 38 host, "get", use_verbose=True, help=_("retrieve avatar of an entity")
38 ) 39 )
39 40
40 def add_parser_options(self): 41 def add_parser_options(self):
41 self.parser.add_argument("jid", help=_("entity")) 42 self.parser.add_argument(
43 "--no-cache", action="store_true", help=_("do no use cached values")
44 )
42 self.parser.add_argument( 45 self.parser.add_argument(
43 "-s", "--show", action="store_true", help=_("show avatar") 46 "-s", "--show", action="store_true", help=_("show avatar")
44 ) 47 )
48 self.parser.add_argument("jid", nargs='?', default='', help=_("entity"))
45 49
46 async def showImage(self, path): 50 async def showImage(self, path):
47 sat_conf = config.parseMainConf() 51 sat_conf = config.parseMainConf()
48 cmd = config.getConfig(sat_conf, "jp", "image_cmd") 52 cmd = config.getConfig(sat_conf, "jp", "image_cmd")
49 cmds = [cmd] + DISPLAY_CMD if cmd else DISPLAY_CMD 53 cmds = [cmd] + DISPLAY_CMD if cmd else DISPLAY_CMD
66 70
67 webbrowser.open(path) 71 webbrowser.open(path)
68 72
69 async def start(self): 73 async def start(self):
70 try: 74 try:
71 avatar_path = await self.host.bridge.avatarGet( 75 avatar_data_raw = await self.host.bridge.avatarGet(
72 self.args.jid, 76 self.args.jid,
73 False, 77 not self.args.no_cache,
74 False,
75 self.profile, 78 self.profile,
76 ) 79 )
77 except Exception as e: 80 except Exception as e:
78 self.disp(f"can't retrieve avatar: {e}", error=True) 81 self.disp(f"can't retrieve avatar: {e}", error=True)
79 self.host.quit(C.EXIT_BRIDGE_ERRBACK) 82 self.host.quit(C.EXIT_BRIDGE_ERRBACK)
80 83
81 if not avatar_path: 84 avatar_data = data_format.deserialise(avatar_data_raw, type_check=None)
85
86 if not avatar_data:
82 self.disp(_("No avatar found."), 1) 87 self.disp(_("No avatar found."), 1)
83 self.host.quit(C.EXIT_NOT_FOUND) 88 self.host.quit(C.EXIT_NOT_FOUND)
89
90 avatar_path = avatar_data['path']
84 91
85 self.disp(avatar_path) 92 self.disp(avatar_path)
86 if self.args.show: 93 if self.args.show:
87 await self.showImage(avatar_path) 94 await self.showImage(avatar_path)
88 95
90 97
91 98
92 class Set(base.CommandBase): 99 class Set(base.CommandBase):
93 def __init__(self, host): 100 def __init__(self, host):
94 super(Set, self).__init__( 101 super(Set, self).__init__(
95 host, "set", use_verbose=True, help=_("set avatar of the profile") 102 host, "set", use_verbose=True,
103 help=_("set avatar of the profile or an entity")
96 ) 104 )
97 105
98 def add_parser_options(self): 106 def add_parser_options(self):
107 self.parser.add_argument(
108 "-j", "--jid", default='', help=_("entity whose avatar must be changed"))
99 self.parser.add_argument( 109 self.parser.add_argument(
100 "image_path", type=str, help=_("path to the image to upload") 110 "image_path", type=str, help=_("path to the image to upload")
101 ) 111 )
102 112
103 async def start(self): 113 async def start(self):
105 if not os.path.exists(path): 115 if not os.path.exists(path):
106 self.disp(_(f"file {path!r} doesn't exist!"), error=True) 116 self.disp(_(f"file {path!r} doesn't exist!"), error=True)
107 self.host.quit(C.EXIT_BAD_ARG) 117 self.host.quit(C.EXIT_BAD_ARG)
108 path = os.path.abspath(path) 118 path = os.path.abspath(path)
109 try: 119 try:
110 await self.host.bridge.avatarSet(path, self.profile) 120 await self.host.bridge.avatarSet(path, self.args.jid, self.profile)
111 except Exception as e: 121 except Exception as e:
112 self.disp(f"can't set avatar: {e}", error=True) 122 self.disp(f"can't set avatar: {e}", error=True)
113 self.host.quit(C.EXIT_BRIDGE_ERRBACK) 123 self.host.quit(C.EXIT_BRIDGE_ERRBACK)
114 else: 124 else:
115 self.disp(_("avatar has been set"), 1) 125 self.disp(_("avatar has been set"), 1)