annotate sat/plugins/plugin_misc_identity.py @ 3327:ddc10f76fb7e

changelog: update
author Goffi <goffi@goffi.org>
date Sun, 02 Aug 2020 20:31:06 +0200
parents 9e1ba1e1179f
children 203a491fcd86
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
3028
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 2771
diff changeset
1 #!/usr/bin/env python3
3137
559a625a236b fixed shebangs
Goffi <goffi@goffi.org>
parents: 3136
diff changeset
2
3136
9d0df638c8b4 dates update
Goffi <goffi@goffi.org>
parents: 3028
diff changeset
3 # Copyright (C) 2009-2020 Jérôme Poisson (goffi@goffi.org)
2253
db468f24b9fc plugin identity: plugin identity first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
4
db468f24b9fc plugin identity: plugin identity first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
5 # This program is free software: you can redistribute it and/or modify
db468f24b9fc plugin identity: plugin identity first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
6 # it under the terms of the GNU Affero General Public License as published by
db468f24b9fc plugin identity: plugin identity first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
7 # the Free Software Foundation, either version 3 of the License, or
db468f24b9fc plugin identity: plugin identity first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
8 # (at your option) any later version.
db468f24b9fc plugin identity: plugin identity first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
9
db468f24b9fc plugin identity: plugin identity first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
10 # This program is distributed in the hope that it will be useful,
db468f24b9fc plugin identity: plugin identity first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
11 # but WITHOUT ANY WARRANTY; without even the implied warranty of
db468f24b9fc plugin identity: plugin identity first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
12 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
db468f24b9fc plugin identity: plugin identity first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
13 # GNU Affero General Public License for more details.
db468f24b9fc plugin identity: plugin identity first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
14
db468f24b9fc plugin identity: plugin identity first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
15 # You should have received a copy of the GNU Affero General Public License
db468f24b9fc plugin identity: plugin identity first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
16 # along with this program. If not, see <http://www.gnu.org/licenses/>.
db468f24b9fc plugin identity: plugin identity first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
17
3277
cf07641b764d plugin identity: fixed infinite loop on nicknames update
Goffi <goffi@goffi.org>
parents: 3262
diff changeset
18 from typing import Dict, Union, Coroutine, Any, Optional
3254
6cf4bd6972c2 core, frontends: avatar refactoring:
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
19 from collections import namedtuple
6cf4bd6972c2 core, frontends: avatar refactoring:
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
20 from pathlib import Path
6cf4bd6972c2 core, frontends: avatar refactoring:
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
21 from twisted.internet import defer
6cf4bd6972c2 core, frontends: avatar refactoring:
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
22 from twisted.words.protocols.jabber import jid
3277
cf07641b764d plugin identity: fixed infinite loop on nicknames update
Goffi <goffi@goffi.org>
parents: 3262
diff changeset
23 from sat.core.xmpp import SatXMPPEntity
2253
db468f24b9fc plugin identity: plugin identity first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
24 from sat.core.i18n import _
db468f24b9fc plugin identity: plugin identity first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
25 from sat.core.constants import Const as C
2463
65a6d2496504 plugin identity: return resource for MUC room occupants' nicks + added avatar and avatar_basename.
Goffi <goffi@goffi.org>
parents: 2427
diff changeset
26 from sat.core import exceptions
2253
db468f24b9fc plugin identity: plugin identity first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
27 from sat.core.log import getLogger
3254
6cf4bd6972c2 core, frontends: avatar refactoring:
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
28 from sat.memory import persistent
6cf4bd6972c2 core, frontends: avatar refactoring:
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
29 from sat.tools import image
6cf4bd6972c2 core, frontends: avatar refactoring:
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
30 from sat.tools import utils
6cf4bd6972c2 core, frontends: avatar refactoring:
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
31 from sat.tools.common import data_format
6cf4bd6972c2 core, frontends: avatar refactoring:
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
32
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
33
2253
db468f24b9fc plugin identity: plugin identity first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
34 log = getLogger(__name__)
db468f24b9fc plugin identity: plugin identity first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
35
db468f24b9fc plugin identity: plugin identity first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
36
3278
27d4b71e264a plugin identity: fixed update in avatarSetPostTreatment
Goffi <goffi@goffi.org>
parents: 3277
diff changeset
37 IMPORT_NAME = "IDENTITY"
27d4b71e264a plugin identity: fixed update in avatarSetPostTreatment
Goffi <goffi@goffi.org>
parents: 3277
diff changeset
38
27d4b71e264a plugin identity: fixed update in avatarSetPostTreatment
Goffi <goffi@goffi.org>
parents: 3277
diff changeset
39
2253
db468f24b9fc plugin identity: plugin identity first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
40 PLUGIN_INFO = {
db468f24b9fc plugin identity: plugin identity first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
41 C.PI_NAME: "Identity Plugin",
3278
27d4b71e264a plugin identity: fixed update in avatarSetPostTreatment
Goffi <goffi@goffi.org>
parents: 3277
diff changeset
42 C.PI_IMPORT_NAME: IMPORT_NAME,
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
43 C.PI_TYPE: C.PLUG_TYPE_MISC,
2253
db468f24b9fc plugin identity: plugin identity first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
44 C.PI_PROTOCOLS: [],
3254
6cf4bd6972c2 core, frontends: avatar refactoring:
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
45 C.PI_DEPENDENCIES: [],
6cf4bd6972c2 core, frontends: avatar refactoring:
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
46 C.PI_RECOMMENDATIONS: ["XEP-0045"],
2253
db468f24b9fc plugin identity: plugin identity first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
47 C.PI_MAIN: "Identity",
db468f24b9fc plugin identity: plugin identity first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
48 C.PI_HANDLER: "no",
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
49 C.PI_DESCRIPTION: _("""Identity manager"""),
2253
db468f24b9fc plugin identity: plugin identity first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
50 }
db468f24b9fc plugin identity: plugin identity first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
51
3277
cf07641b764d plugin identity: fixed infinite loop on nicknames update
Goffi <goffi@goffi.org>
parents: 3262
diff changeset
52 Callback = namedtuple("Callback", ("origin", "get", "set", "priority"))
2253
db468f24b9fc plugin identity: plugin identity first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
53
3254
6cf4bd6972c2 core, frontends: avatar refactoring:
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
54
6cf4bd6972c2 core, frontends: avatar refactoring:
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
55 class Identity:
6cf4bd6972c2 core, frontends: avatar refactoring:
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
56
2253
db468f24b9fc plugin identity: plugin identity first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
57 def __init__(self, host):
3028
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 2771
diff changeset
58 log.info(_("Plugin Identity initialization"))
2253
db468f24b9fc plugin identity: plugin identity first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
59 self.host = host
3254
6cf4bd6972c2 core, frontends: avatar refactoring:
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
60 self._m = host.plugins.get("XEP-0045")
6cf4bd6972c2 core, frontends: avatar refactoring:
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
61 self.metadata = {
6cf4bd6972c2 core, frontends: avatar refactoring:
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
62 "avatar": {
6cf4bd6972c2 core, frontends: avatar refactoring:
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
63 "type": dict,
6cf4bd6972c2 core, frontends: avatar refactoring:
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
64 # convert avatar path to avatar metadata (and check validity)
6cf4bd6972c2 core, frontends: avatar refactoring:
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
65 "set_data_filter": self.avatarSetDataFilter,
6cf4bd6972c2 core, frontends: avatar refactoring:
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
66 # update profile avatar, so all frontends are aware
6cf4bd6972c2 core, frontends: avatar refactoring:
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
67 "set_post_treatment": self.avatarSetPostTreatment,
6cf4bd6972c2 core, frontends: avatar refactoring:
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
68 "update_is_new_data": self.avatarUpdateIsNewData,
6cf4bd6972c2 core, frontends: avatar refactoring:
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
69 "update_data_filter": self.avatarUpdateDataFilter,
6cf4bd6972c2 core, frontends: avatar refactoring:
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
70 # we store the metadata in database, to restore it on next connection
6cf4bd6972c2 core, frontends: avatar refactoring:
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
71 # (it is stored only for roster entities)
6cf4bd6972c2 core, frontends: avatar refactoring:
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
72 "store": True,
6cf4bd6972c2 core, frontends: avatar refactoring:
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
73 },
6cf4bd6972c2 core, frontends: avatar refactoring:
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
74 "nicknames": {
6cf4bd6972c2 core, frontends: avatar refactoring:
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
75 "type": list,
6cf4bd6972c2 core, frontends: avatar refactoring:
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
76 # accumulate all nicknames from all callbacks in a list instead
6cf4bd6972c2 core, frontends: avatar refactoring:
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
77 # of returning only the data from the first successful callback
6cf4bd6972c2 core, frontends: avatar refactoring:
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
78 "get_all": True,
6cf4bd6972c2 core, frontends: avatar refactoring:
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
79 # append nicknames from roster, resource, etc.
6cf4bd6972c2 core, frontends: avatar refactoring:
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
80 "get_post_treatment": self.nicknamesGetPostTreatment,
6cf4bd6972c2 core, frontends: avatar refactoring:
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
81 "update_is_new_data": self.nicknamesUpdateIsNewData,
6cf4bd6972c2 core, frontends: avatar refactoring:
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
82 "store": True,
6cf4bd6972c2 core, frontends: avatar refactoring:
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
83 },
6cf4bd6972c2 core, frontends: avatar refactoring:
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
84 }
6cf4bd6972c2 core, frontends: avatar refactoring:
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
85 host.trigger.add("roster_update", self._rosterUpdateTrigger)
6cf4bd6972c2 core, frontends: avatar refactoring:
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
86 host.memory.setSignalOnUpdate("avatar")
6cf4bd6972c2 core, frontends: avatar refactoring:
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
87 host.memory.setSignalOnUpdate("nicknames")
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
88 host.bridge.addMethod(
3028
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 2771
diff changeset
89 "identityGet",
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 2771
diff changeset
90 ".plugin",
3254
6cf4bd6972c2 core, frontends: avatar refactoring:
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
91 in_sign="sasbs",
6cf4bd6972c2 core, frontends: avatar refactoring:
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
92 out_sign="s",
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
93 method=self._getIdentity,
3028
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 2771
diff changeset
94 async_=True,
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
95 )
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
96 host.bridge.addMethod(
3028
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 2771
diff changeset
97 "identitySet",
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 2771
diff changeset
98 ".plugin",
3254
6cf4bd6972c2 core, frontends: avatar refactoring:
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
99 in_sign="ss",
3028
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 2771
diff changeset
100 out_sign="",
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
101 method=self._setIdentity,
3028
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 2771
diff changeset
102 async_=True,
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
103 )
3254
6cf4bd6972c2 core, frontends: avatar refactoring:
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
104 host.bridge.addMethod(
6cf4bd6972c2 core, frontends: avatar refactoring:
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
105 "avatarGet",
6cf4bd6972c2 core, frontends: avatar refactoring:
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
106 ".plugin",
6cf4bd6972c2 core, frontends: avatar refactoring:
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
107 in_sign="sbs",
6cf4bd6972c2 core, frontends: avatar refactoring:
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
108 out_sign="s",
6cf4bd6972c2 core, frontends: avatar refactoring:
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
109 method=self._getAvatar,
6cf4bd6972c2 core, frontends: avatar refactoring:
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
110 async_=True,
6cf4bd6972c2 core, frontends: avatar refactoring:
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
111 )
6cf4bd6972c2 core, frontends: avatar refactoring:
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
112 host.bridge.addMethod(
6cf4bd6972c2 core, frontends: avatar refactoring:
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
113 "avatarSet",
6cf4bd6972c2 core, frontends: avatar refactoring:
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
114 ".plugin",
6cf4bd6972c2 core, frontends: avatar refactoring:
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
115 in_sign="sss",
6cf4bd6972c2 core, frontends: avatar refactoring:
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
116 out_sign="",
6cf4bd6972c2 core, frontends: avatar refactoring:
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
117 method=self._setAvatar,
6cf4bd6972c2 core, frontends: avatar refactoring:
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
118 async_=True,
6cf4bd6972c2 core, frontends: avatar refactoring:
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
119 )
2253
db468f24b9fc plugin identity: plugin identity first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
120
3254
6cf4bd6972c2 core, frontends: avatar refactoring:
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
121 async def profileConnecting(self, client):
3277
cf07641b764d plugin identity: fixed infinite loop on nicknames update
Goffi <goffi@goffi.org>
parents: 3262
diff changeset
122 client._identity_update_lock = []
3254
6cf4bd6972c2 core, frontends: avatar refactoring:
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
123 # we restore known identities from database
6cf4bd6972c2 core, frontends: avatar refactoring:
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
124 client._identity_storage = persistent.LazyPersistentBinaryDict(
6cf4bd6972c2 core, frontends: avatar refactoring:
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
125 "identity", client.profile)
6cf4bd6972c2 core, frontends: avatar refactoring:
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
126
6cf4bd6972c2 core, frontends: avatar refactoring:
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
127 stored_data = await client._identity_storage.all()
6cf4bd6972c2 core, frontends: avatar refactoring:
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
128
6cf4bd6972c2 core, frontends: avatar refactoring:
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
129 self.host.memory.storage.getPrivates(
6cf4bd6972c2 core, frontends: avatar refactoring:
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
130 namespace="identity", binary=True, profile=client.profile)
6cf4bd6972c2 core, frontends: avatar refactoring:
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
131
6cf4bd6972c2 core, frontends: avatar refactoring:
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
132 to_delete = []
6cf4bd6972c2 core, frontends: avatar refactoring:
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
133
6cf4bd6972c2 core, frontends: avatar refactoring:
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
134 for key, value in stored_data.items():
6cf4bd6972c2 core, frontends: avatar refactoring:
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
135 entity_s, name = key.split('\n')
6cf4bd6972c2 core, frontends: avatar refactoring:
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
136 if name not in self.metadata.keys():
6cf4bd6972c2 core, frontends: avatar refactoring:
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
137 log.debug(f"removing {key} from storage: not an allowed metadata name")
6cf4bd6972c2 core, frontends: avatar refactoring:
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
138 to_delete.append(key)
6cf4bd6972c2 core, frontends: avatar refactoring:
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
139 continue
6cf4bd6972c2 core, frontends: avatar refactoring:
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
140 entity = jid.JID(entity_s)
6cf4bd6972c2 core, frontends: avatar refactoring:
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
141
6cf4bd6972c2 core, frontends: avatar refactoring:
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
142 if name == 'avatar':
6cf4bd6972c2 core, frontends: avatar refactoring:
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
143 if value is not None:
6cf4bd6972c2 core, frontends: avatar refactoring:
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
144 try:
6cf4bd6972c2 core, frontends: avatar refactoring:
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
145 cache_uid = value['cache_uid']
6cf4bd6972c2 core, frontends: avatar refactoring:
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
146 if not cache_uid:
6cf4bd6972c2 core, frontends: avatar refactoring:
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
147 raise ValueError
3326
9e1ba1e1179f plugin identity: added "filename" metadata for avatar
Goffi <goffi@goffi.org>
parents: 3278
diff changeset
148 filename = value['filename']
9e1ba1e1179f plugin identity: added "filename" metadata for avatar
Goffi <goffi@goffi.org>
parents: 3278
diff changeset
149 if not filename:
9e1ba1e1179f plugin identity: added "filename" metadata for avatar
Goffi <goffi@goffi.org>
parents: 3278
diff changeset
150 raise ValueError
3254
6cf4bd6972c2 core, frontends: avatar refactoring:
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
151 except (ValueError, KeyError):
6cf4bd6972c2 core, frontends: avatar refactoring:
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
152 log.warning(
6cf4bd6972c2 core, frontends: avatar refactoring:
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
153 f"invalid data for {entity} avatar, it will be deleted: "
6cf4bd6972c2 core, frontends: avatar refactoring:
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
154 f"{value}")
6cf4bd6972c2 core, frontends: avatar refactoring:
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
155 to_delete.append(key)
6cf4bd6972c2 core, frontends: avatar refactoring:
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
156 continue
6cf4bd6972c2 core, frontends: avatar refactoring:
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
157 cache = self.host.common_cache.getMetadata(cache_uid)
6cf4bd6972c2 core, frontends: avatar refactoring:
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
158 if cache is None:
6cf4bd6972c2 core, frontends: avatar refactoring:
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
159 log.debug(
6cf4bd6972c2 core, frontends: avatar refactoring:
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
160 f"purging avatar for {entity}: it is not in cache anymore")
6cf4bd6972c2 core, frontends: avatar refactoring:
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
161 to_delete.append(key)
6cf4bd6972c2 core, frontends: avatar refactoring:
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
162 continue
6cf4bd6972c2 core, frontends: avatar refactoring:
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
163
6cf4bd6972c2 core, frontends: avatar refactoring:
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
164 self.host.memory.updateEntityData(
6cf4bd6972c2 core, frontends: avatar refactoring:
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
165 client, entity, name, value, silent=True
6cf4bd6972c2 core, frontends: avatar refactoring:
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
166 )
6cf4bd6972c2 core, frontends: avatar refactoring:
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
167
6cf4bd6972c2 core, frontends: avatar refactoring:
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
168 for key in to_delete:
6cf4bd6972c2 core, frontends: avatar refactoring:
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
169 await client._identity_storage.adel(key)
6cf4bd6972c2 core, frontends: avatar refactoring:
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
170
6cf4bd6972c2 core, frontends: avatar refactoring:
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
171 def _rosterUpdateTrigger(self, client, roster_item):
6cf4bd6972c2 core, frontends: avatar refactoring:
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
172 old_item = client.roster.getItem(roster_item.jid)
6cf4bd6972c2 core, frontends: avatar refactoring:
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
173 if old_item is None or old_item.name != roster_item.name:
6cf4bd6972c2 core, frontends: avatar refactoring:
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
174 log.debug(
6cf4bd6972c2 core, frontends: avatar refactoring:
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
175 f"roster nickname has been updated to {roster_item.name!r} for "
6cf4bd6972c2 core, frontends: avatar refactoring:
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
176 f"{roster_item.jid}"
6cf4bd6972c2 core, frontends: avatar refactoring:
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
177 )
6cf4bd6972c2 core, frontends: avatar refactoring:
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
178 defer.ensureDeferred(
6cf4bd6972c2 core, frontends: avatar refactoring:
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
179 self.update(
6cf4bd6972c2 core, frontends: avatar refactoring:
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
180 client,
6cf4bd6972c2 core, frontends: avatar refactoring:
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
181 "nicknames",
6cf4bd6972c2 core, frontends: avatar refactoring:
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
182 [roster_item.name],
6cf4bd6972c2 core, frontends: avatar refactoring:
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
183 roster_item.jid
6cf4bd6972c2 core, frontends: avatar refactoring:
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
184 )
6cf4bd6972c2 core, frontends: avatar refactoring:
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
185 )
6cf4bd6972c2 core, frontends: avatar refactoring:
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
186 return True
6cf4bd6972c2 core, frontends: avatar refactoring:
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
187
3277
cf07641b764d plugin identity: fixed infinite loop on nicknames update
Goffi <goffi@goffi.org>
parents: 3262
diff changeset
188 def register(
cf07641b764d plugin identity: fixed infinite loop on nicknames update
Goffi <goffi@goffi.org>
parents: 3262
diff changeset
189 self,
cf07641b764d plugin identity: fixed infinite loop on nicknames update
Goffi <goffi@goffi.org>
parents: 3262
diff changeset
190 origin: str,
cf07641b764d plugin identity: fixed infinite loop on nicknames update
Goffi <goffi@goffi.org>
parents: 3262
diff changeset
191 metadata_name: str,
cf07641b764d plugin identity: fixed infinite loop on nicknames update
Goffi <goffi@goffi.org>
parents: 3262
diff changeset
192 cb_get: Union[Coroutine, defer.Deferred],
cf07641b764d plugin identity: fixed infinite loop on nicknames update
Goffi <goffi@goffi.org>
parents: 3262
diff changeset
193 cb_set: Union[Coroutine, defer.Deferred],
cf07641b764d plugin identity: fixed infinite loop on nicknames update
Goffi <goffi@goffi.org>
parents: 3262
diff changeset
194 priority: int=0):
3254
6cf4bd6972c2 core, frontends: avatar refactoring:
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
195 """Register callbacks to handle identity metadata
6cf4bd6972c2 core, frontends: avatar refactoring:
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
196
3277
cf07641b764d plugin identity: fixed infinite loop on nicknames update
Goffi <goffi@goffi.org>
parents: 3262
diff changeset
197 @param origin: namespace of the plugin managing this metadata
cf07641b764d plugin identity: fixed infinite loop on nicknames update
Goffi <goffi@goffi.org>
parents: 3262
diff changeset
198 @param metadata_name: name of metadata can be:
3254
6cf4bd6972c2 core, frontends: avatar refactoring:
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
199 - avatar
6cf4bd6972c2 core, frontends: avatar refactoring:
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
200 - nicknames
3277
cf07641b764d plugin identity: fixed infinite loop on nicknames update
Goffi <goffi@goffi.org>
parents: 3262
diff changeset
201 @param cb_get: method to retrieve a metadata
3254
6cf4bd6972c2 core, frontends: avatar refactoring:
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
202 the method will get client and metadata names to retrieve as arguments.
3277
cf07641b764d plugin identity: fixed infinite loop on nicknames update
Goffi <goffi@goffi.org>
parents: 3262
diff changeset
203 @param cb_set: method to set a metadata
3254
6cf4bd6972c2 core, frontends: avatar refactoring:
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
204 the method will get client, metadata name to set, and value as argument.
3277
cf07641b764d plugin identity: fixed infinite loop on nicknames update
Goffi <goffi@goffi.org>
parents: 3262
diff changeset
205 @param priority: priority of this method for the given metadata.
3254
6cf4bd6972c2 core, frontends: avatar refactoring:
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
206 methods with bigger priorities will be called first
6cf4bd6972c2 core, frontends: avatar refactoring:
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
207 """
6cf4bd6972c2 core, frontends: avatar refactoring:
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
208 if not metadata_name in self.metadata.keys():
6cf4bd6972c2 core, frontends: avatar refactoring:
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
209 raise ValueError(f"Invalid metadata_name: {metadata_name!r}")
3277
cf07641b764d plugin identity: fixed infinite loop on nicknames update
Goffi <goffi@goffi.org>
parents: 3262
diff changeset
210 callback = Callback(origin=origin, get=cb_get, set=cb_set, priority=priority)
3254
6cf4bd6972c2 core, frontends: avatar refactoring:
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
211 cb_list = self.metadata[metadata_name].setdefault('callbacks', [])
6cf4bd6972c2 core, frontends: avatar refactoring:
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
212 cb_list.append(callback)
6cf4bd6972c2 core, frontends: avatar refactoring:
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
213 cb_list.sort(key=lambda c: c.priority, reverse=True)
6cf4bd6972c2 core, frontends: avatar refactoring:
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
214
6cf4bd6972c2 core, frontends: avatar refactoring:
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
215 def getIdentityJid(self, client, peer_jid):
6cf4bd6972c2 core, frontends: avatar refactoring:
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
216 """Return jid to use to set identity metadata
6cf4bd6972c2 core, frontends: avatar refactoring:
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
217
6cf4bd6972c2 core, frontends: avatar refactoring:
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
218 if it's a jid of a room occupant, full jid will be used
6cf4bd6972c2 core, frontends: avatar refactoring:
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
219 otherwise bare jid will be used
6cf4bd6972c2 core, frontends: avatar refactoring:
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
220 if None, bare jid of profile will be used
6cf4bd6972c2 core, frontends: avatar refactoring:
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
221 @return (jid.JID): jid to use for avatar
6cf4bd6972c2 core, frontends: avatar refactoring:
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
222 """
6cf4bd6972c2 core, frontends: avatar refactoring:
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
223 if peer_jid is None:
6cf4bd6972c2 core, frontends: avatar refactoring:
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
224 return client.jid.userhostJID()
6cf4bd6972c2 core, frontends: avatar refactoring:
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
225 if self._m is None:
6cf4bd6972c2 core, frontends: avatar refactoring:
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
226 return peer_jid.userhostJID()
6cf4bd6972c2 core, frontends: avatar refactoring:
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
227 else:
6cf4bd6972c2 core, frontends: avatar refactoring:
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
228 return self._m.getBareOrFull(client, peer_jid)
6cf4bd6972c2 core, frontends: avatar refactoring:
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
229
6cf4bd6972c2 core, frontends: avatar refactoring:
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
230 def checkType(self, metadata_name, value):
6cf4bd6972c2 core, frontends: avatar refactoring:
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
231 """Check that type used for a metadata is the one declared in self.metadata"""
6cf4bd6972c2 core, frontends: avatar refactoring:
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
232 value_type = self.metadata[metadata_name]["type"]
6cf4bd6972c2 core, frontends: avatar refactoring:
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
233 if not isinstance(value, value_type):
6cf4bd6972c2 core, frontends: avatar refactoring:
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
234 raise ValueError(
6cf4bd6972c2 core, frontends: avatar refactoring:
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
235 f"{value} has wrong type: it is {type(value)} while {value_type} was "
6cf4bd6972c2 core, frontends: avatar refactoring:
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
236 f"expected")
6cf4bd6972c2 core, frontends: avatar refactoring:
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
237
3277
cf07641b764d plugin identity: fixed infinite loop on nicknames update
Goffi <goffi@goffi.org>
parents: 3262
diff changeset
238 async def get(
cf07641b764d plugin identity: fixed infinite loop on nicknames update
Goffi <goffi@goffi.org>
parents: 3262
diff changeset
239 self,
cf07641b764d plugin identity: fixed infinite loop on nicknames update
Goffi <goffi@goffi.org>
parents: 3262
diff changeset
240 client: SatXMPPEntity,
cf07641b764d plugin identity: fixed infinite loop on nicknames update
Goffi <goffi@goffi.org>
parents: 3262
diff changeset
241 metadata_name: str,
cf07641b764d plugin identity: fixed infinite loop on nicknames update
Goffi <goffi@goffi.org>
parents: 3262
diff changeset
242 entity: Optional[jid.JID],
cf07641b764d plugin identity: fixed infinite loop on nicknames update
Goffi <goffi@goffi.org>
parents: 3262
diff changeset
243 use_cache: bool=True,
cf07641b764d plugin identity: fixed infinite loop on nicknames update
Goffi <goffi@goffi.org>
parents: 3262
diff changeset
244 prefilled_values: Optional[Dict[str, Any]]=None
cf07641b764d plugin identity: fixed infinite loop on nicknames update
Goffi <goffi@goffi.org>
parents: 3262
diff changeset
245 ):
3254
6cf4bd6972c2 core, frontends: avatar refactoring:
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
246 """Retrieve identity metadata of an entity
6cf4bd6972c2 core, frontends: avatar refactoring:
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
247
6cf4bd6972c2 core, frontends: avatar refactoring:
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
248 if metadata is already in cache, it is returned. Otherwise, registered callbacks
6cf4bd6972c2 core, frontends: avatar refactoring:
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
249 will be tried in priority order (bigger to lower)
3277
cf07641b764d plugin identity: fixed infinite loop on nicknames update
Goffi <goffi@goffi.org>
parents: 3262
diff changeset
250 @param metadata_name: name of the metadata
3254
6cf4bd6972c2 core, frontends: avatar refactoring:
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
251 must be one of self.metadata key
6cf4bd6972c2 core, frontends: avatar refactoring:
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
252 the name will also be used as entity data name in host.memory
3277
cf07641b764d plugin identity: fixed infinite loop on nicknames update
Goffi <goffi@goffi.org>
parents: 3262
diff changeset
253 @param entity: entity for which avatar is requested
3254
6cf4bd6972c2 core, frontends: avatar refactoring:
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
254 None to use profile's jid
3277
cf07641b764d plugin identity: fixed infinite loop on nicknames update
Goffi <goffi@goffi.org>
parents: 3262
diff changeset
255 @param use_cache: if False, cache won't be checked
cf07641b764d plugin identity: fixed infinite loop on nicknames update
Goffi <goffi@goffi.org>
parents: 3262
diff changeset
256 @param prefilled_values: map of origin => value to use when `get_all` is set
3254
6cf4bd6972c2 core, frontends: avatar refactoring:
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
257 """
6cf4bd6972c2 core, frontends: avatar refactoring:
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
258 entity = self.getIdentityJid(client, entity)
6cf4bd6972c2 core, frontends: avatar refactoring:
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
259 try:
6cf4bd6972c2 core, frontends: avatar refactoring:
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
260 metadata = self.metadata[metadata_name]
6cf4bd6972c2 core, frontends: avatar refactoring:
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
261 except KeyError:
6cf4bd6972c2 core, frontends: avatar refactoring:
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
262 raise ValueError(f"Invalid metadata name: {metadata_name!r}")
6cf4bd6972c2 core, frontends: avatar refactoring:
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
263 get_all = metadata.get('get_all', False)
6cf4bd6972c2 core, frontends: avatar refactoring:
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
264 if use_cache:
6cf4bd6972c2 core, frontends: avatar refactoring:
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
265 try:
6cf4bd6972c2 core, frontends: avatar refactoring:
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
266 data = self.host.memory.getEntityDatum(
6cf4bd6972c2 core, frontends: avatar refactoring:
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
267 client, entity, metadata_name)
6cf4bd6972c2 core, frontends: avatar refactoring:
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
268 except (KeyError, exceptions.UnknownEntityError):
6cf4bd6972c2 core, frontends: avatar refactoring:
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
269 pass
6cf4bd6972c2 core, frontends: avatar refactoring:
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
270 else:
6cf4bd6972c2 core, frontends: avatar refactoring:
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
271 return data
6cf4bd6972c2 core, frontends: avatar refactoring:
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
272
6cf4bd6972c2 core, frontends: avatar refactoring:
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
273 try:
6cf4bd6972c2 core, frontends: avatar refactoring:
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
274 callbacks = metadata['callbacks']
6cf4bd6972c2 core, frontends: avatar refactoring:
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
275 except KeyError:
6cf4bd6972c2 core, frontends: avatar refactoring:
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
276 log.warning(_("No callback registered for {metadata_name}")
6cf4bd6972c2 core, frontends: avatar refactoring:
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
277 .format(metadata_name=metadata_name))
6cf4bd6972c2 core, frontends: avatar refactoring:
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
278 return [] if get_all else None
6cf4bd6972c2 core, frontends: avatar refactoring:
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
279
6cf4bd6972c2 core, frontends: avatar refactoring:
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
280 if get_all:
6cf4bd6972c2 core, frontends: avatar refactoring:
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
281 all_data = []
3277
cf07641b764d plugin identity: fixed infinite loop on nicknames update
Goffi <goffi@goffi.org>
parents: 3262
diff changeset
282 elif prefilled_values is not None:
cf07641b764d plugin identity: fixed infinite loop on nicknames update
Goffi <goffi@goffi.org>
parents: 3262
diff changeset
283 raise exceptions.InternalError(
cf07641b764d plugin identity: fixed infinite loop on nicknames update
Goffi <goffi@goffi.org>
parents: 3262
diff changeset
284 "prefilled_values can only be used when `get_all` is set")
3254
6cf4bd6972c2 core, frontends: avatar refactoring:
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
285
6cf4bd6972c2 core, frontends: avatar refactoring:
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
286 for callback in callbacks:
6cf4bd6972c2 core, frontends: avatar refactoring:
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
287 try:
3277
cf07641b764d plugin identity: fixed infinite loop on nicknames update
Goffi <goffi@goffi.org>
parents: 3262
diff changeset
288 if prefilled_values is not None and callback.origin in prefilled_values:
cf07641b764d plugin identity: fixed infinite loop on nicknames update
Goffi <goffi@goffi.org>
parents: 3262
diff changeset
289 data = prefilled_values[callback.origin]
cf07641b764d plugin identity: fixed infinite loop on nicknames update
Goffi <goffi@goffi.org>
parents: 3262
diff changeset
290 log.debug(
cf07641b764d plugin identity: fixed infinite loop on nicknames update
Goffi <goffi@goffi.org>
parents: 3262
diff changeset
291 f"using prefilled values {data!r} for {metadata_name} with "
cf07641b764d plugin identity: fixed infinite loop on nicknames update
Goffi <goffi@goffi.org>
parents: 3262
diff changeset
292 f"{callback.origin}")
cf07641b764d plugin identity: fixed infinite loop on nicknames update
Goffi <goffi@goffi.org>
parents: 3262
diff changeset
293 else:
cf07641b764d plugin identity: fixed infinite loop on nicknames update
Goffi <goffi@goffi.org>
parents: 3262
diff changeset
294 data = await defer.ensureDeferred(callback.get(client, entity))
3254
6cf4bd6972c2 core, frontends: avatar refactoring:
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
295 except exceptions.CancelError:
6cf4bd6972c2 core, frontends: avatar refactoring:
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
296 continue
6cf4bd6972c2 core, frontends: avatar refactoring:
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
297 except Exception as e:
6cf4bd6972c2 core, frontends: avatar refactoring:
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
298 log.warning(
6cf4bd6972c2 core, frontends: avatar refactoring:
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
299 _("Error while trying to get {metadata_name} with {callback}: {e}")
6cf4bd6972c2 core, frontends: avatar refactoring:
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
300 .format(callback=callback.get, metadata_name=metadata_name, e=e))
6cf4bd6972c2 core, frontends: avatar refactoring:
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
301 else:
6cf4bd6972c2 core, frontends: avatar refactoring:
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
302 if data:
6cf4bd6972c2 core, frontends: avatar refactoring:
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
303 self.checkType(metadata_name, data)
6cf4bd6972c2 core, frontends: avatar refactoring:
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
304 if get_all:
6cf4bd6972c2 core, frontends: avatar refactoring:
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
305 all_data.extend(data)
6cf4bd6972c2 core, frontends: avatar refactoring:
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
306 else:
6cf4bd6972c2 core, frontends: avatar refactoring:
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
307 break
6cf4bd6972c2 core, frontends: avatar refactoring:
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
308 else:
6cf4bd6972c2 core, frontends: avatar refactoring:
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
309 data = None
6cf4bd6972c2 core, frontends: avatar refactoring:
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
310
6cf4bd6972c2 core, frontends: avatar refactoring:
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
311 if get_all:
6cf4bd6972c2 core, frontends: avatar refactoring:
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
312 data = all_data
6cf4bd6972c2 core, frontends: avatar refactoring:
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
313
6cf4bd6972c2 core, frontends: avatar refactoring:
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
314 post_treatment = metadata.get("get_post_treatment")
6cf4bd6972c2 core, frontends: avatar refactoring:
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
315 if post_treatment is not None:
6cf4bd6972c2 core, frontends: avatar refactoring:
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
316 data = await utils.asDeferred(post_treatment, client, entity, data)
6cf4bd6972c2 core, frontends: avatar refactoring:
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
317
6cf4bd6972c2 core, frontends: avatar refactoring:
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
318 self.host.memory.updateEntityData(
6cf4bd6972c2 core, frontends: avatar refactoring:
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
319 client, entity, metadata_name, data)
6cf4bd6972c2 core, frontends: avatar refactoring:
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
320
6cf4bd6972c2 core, frontends: avatar refactoring:
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
321 if metadata.get('store', False):
6cf4bd6972c2 core, frontends: avatar refactoring:
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
322 key = f"{entity}\n{metadata_name}"
6cf4bd6972c2 core, frontends: avatar refactoring:
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
323 await client._identity_storage.aset(key, data)
6cf4bd6972c2 core, frontends: avatar refactoring:
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
324
6cf4bd6972c2 core, frontends: avatar refactoring:
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
325 return data
6cf4bd6972c2 core, frontends: avatar refactoring:
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
326
6cf4bd6972c2 core, frontends: avatar refactoring:
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
327 async def set(self, client, metadata_name, data, entity=None):
6cf4bd6972c2 core, frontends: avatar refactoring:
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
328 """Set identity metadata for an entity
6cf4bd6972c2 core, frontends: avatar refactoring:
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
329
6cf4bd6972c2 core, frontends: avatar refactoring:
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
330 Registered callbacks will be tried in priority order (bigger to lower)
6cf4bd6972c2 core, frontends: avatar refactoring:
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
331 @param metadata_name(str): name of the metadata
6cf4bd6972c2 core, frontends: avatar refactoring:
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
332 must be one of self.metadata key
6cf4bd6972c2 core, frontends: avatar refactoring:
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
333 the name will also be used to set entity data in host.memory
6cf4bd6972c2 core, frontends: avatar refactoring:
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
334 @param data(object): value to set
6cf4bd6972c2 core, frontends: avatar refactoring:
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
335 @param entity(jid.JID, None): entity for which avatar is requested
6cf4bd6972c2 core, frontends: avatar refactoring:
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
336 None to use profile's jid
6cf4bd6972c2 core, frontends: avatar refactoring:
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
337 """
6cf4bd6972c2 core, frontends: avatar refactoring:
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
338 entity = self.getIdentityJid(client, entity)
6cf4bd6972c2 core, frontends: avatar refactoring:
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
339 metadata = self.metadata[metadata_name]
6cf4bd6972c2 core, frontends: avatar refactoring:
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
340 data_filter = metadata.get("set_data_filter")
6cf4bd6972c2 core, frontends: avatar refactoring:
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
341 if data_filter is not None:
6cf4bd6972c2 core, frontends: avatar refactoring:
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
342 data = await utils.asDeferred(data_filter, client, entity, data)
6cf4bd6972c2 core, frontends: avatar refactoring:
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
343 self.checkType(metadata_name, data)
6cf4bd6972c2 core, frontends: avatar refactoring:
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
344
6cf4bd6972c2 core, frontends: avatar refactoring:
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
345 try:
6cf4bd6972c2 core, frontends: avatar refactoring:
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
346 callbacks = metadata['callbacks']
6cf4bd6972c2 core, frontends: avatar refactoring:
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
347 except KeyError:
6cf4bd6972c2 core, frontends: avatar refactoring:
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
348 log.warning(_("No callback registered for {metadata_name}")
6cf4bd6972c2 core, frontends: avatar refactoring:
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
349 .format(metadata_name=metadata_name))
6cf4bd6972c2 core, frontends: avatar refactoring:
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
350 return exceptions.FeatureNotFound(f"Can't set {metadata_name} for {entity}")
6cf4bd6972c2 core, frontends: avatar refactoring:
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
351
6cf4bd6972c2 core, frontends: avatar refactoring:
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
352 for callback in callbacks:
6cf4bd6972c2 core, frontends: avatar refactoring:
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
353 try:
6cf4bd6972c2 core, frontends: avatar refactoring:
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
354 await defer.ensureDeferred(callback.set(client, data, entity))
6cf4bd6972c2 core, frontends: avatar refactoring:
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
355 except exceptions.CancelError:
6cf4bd6972c2 core, frontends: avatar refactoring:
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
356 continue
6cf4bd6972c2 core, frontends: avatar refactoring:
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
357 except Exception as e:
6cf4bd6972c2 core, frontends: avatar refactoring:
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
358 log.warning(
6cf4bd6972c2 core, frontends: avatar refactoring:
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
359 _("Error while trying to set {metadata_name} with {callback}: {e}")
6cf4bd6972c2 core, frontends: avatar refactoring:
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
360 .format(callback=callback.set, metadata_name=metadata_name, e=e))
6cf4bd6972c2 core, frontends: avatar refactoring:
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
361 else:
6cf4bd6972c2 core, frontends: avatar refactoring:
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
362 break
6cf4bd6972c2 core, frontends: avatar refactoring:
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
363 else:
6cf4bd6972c2 core, frontends: avatar refactoring:
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
364 raise exceptions.FeatureNotFound(f"Can't set {metadata_name} for {entity}")
6cf4bd6972c2 core, frontends: avatar refactoring:
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
365
6cf4bd6972c2 core, frontends: avatar refactoring:
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
366 post_treatment = metadata.get("set_post_treatment")
6cf4bd6972c2 core, frontends: avatar refactoring:
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
367 if post_treatment is not None:
6cf4bd6972c2 core, frontends: avatar refactoring:
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
368 await utils.asDeferred(post_treatment, client, entity, data)
6cf4bd6972c2 core, frontends: avatar refactoring:
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
369
3277
cf07641b764d plugin identity: fixed infinite loop on nicknames update
Goffi <goffi@goffi.org>
parents: 3262
diff changeset
370 async def update(
cf07641b764d plugin identity: fixed infinite loop on nicknames update
Goffi <goffi@goffi.org>
parents: 3262
diff changeset
371 self,
cf07641b764d plugin identity: fixed infinite loop on nicknames update
Goffi <goffi@goffi.org>
parents: 3262
diff changeset
372 client: SatXMPPEntity,
cf07641b764d plugin identity: fixed infinite loop on nicknames update
Goffi <goffi@goffi.org>
parents: 3262
diff changeset
373 origin: str,
cf07641b764d plugin identity: fixed infinite loop on nicknames update
Goffi <goffi@goffi.org>
parents: 3262
diff changeset
374 metadata_name: str,
cf07641b764d plugin identity: fixed infinite loop on nicknames update
Goffi <goffi@goffi.org>
parents: 3262
diff changeset
375 data: Any,
cf07641b764d plugin identity: fixed infinite loop on nicknames update
Goffi <goffi@goffi.org>
parents: 3262
diff changeset
376 entity: Optional[jid.JID]
cf07641b764d plugin identity: fixed infinite loop on nicknames update
Goffi <goffi@goffi.org>
parents: 3262
diff changeset
377 ):
3254
6cf4bd6972c2 core, frontends: avatar refactoring:
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
378 """Update a metadata in cache
6cf4bd6972c2 core, frontends: avatar refactoring:
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
379
6cf4bd6972c2 core, frontends: avatar refactoring:
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
380 This method may be called by plugins when an identity metadata is available.
3277
cf07641b764d plugin identity: fixed infinite loop on nicknames update
Goffi <goffi@goffi.org>
parents: 3262
diff changeset
381 @param origin: namespace of the plugin which is source of the metadata
3254
6cf4bd6972c2 core, frontends: avatar refactoring:
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
382 """
6cf4bd6972c2 core, frontends: avatar refactoring:
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
383 entity = self.getIdentityJid(client, entity)
3277
cf07641b764d plugin identity: fixed infinite loop on nicknames update
Goffi <goffi@goffi.org>
parents: 3262
diff changeset
384 if (entity, metadata_name) in client._identity_update_lock:
cf07641b764d plugin identity: fixed infinite loop on nicknames update
Goffi <goffi@goffi.org>
parents: 3262
diff changeset
385 log.debug(f"update is locked for {entity}'s {metadata_name}")
cf07641b764d plugin identity: fixed infinite loop on nicknames update
Goffi <goffi@goffi.org>
parents: 3262
diff changeset
386 return
3254
6cf4bd6972c2 core, frontends: avatar refactoring:
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
387 metadata = self.metadata[metadata_name]
6cf4bd6972c2 core, frontends: avatar refactoring:
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
388
6cf4bd6972c2 core, frontends: avatar refactoring:
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
389 try:
6cf4bd6972c2 core, frontends: avatar refactoring:
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
390 cached_data = self.host.memory.getEntityDatum(
6cf4bd6972c2 core, frontends: avatar refactoring:
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
391 client, entity, metadata_name)
6cf4bd6972c2 core, frontends: avatar refactoring:
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
392 except (KeyError, exceptions.UnknownEntityError):
6cf4bd6972c2 core, frontends: avatar refactoring:
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
393 # metadata is not cached, we do the update
6cf4bd6972c2 core, frontends: avatar refactoring:
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
394 pass
6cf4bd6972c2 core, frontends: avatar refactoring:
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
395 else:
6cf4bd6972c2 core, frontends: avatar refactoring:
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
396 # metadata is cached, we check if the new value differs from the cached one
6cf4bd6972c2 core, frontends: avatar refactoring:
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
397 try:
6cf4bd6972c2 core, frontends: avatar refactoring:
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
398 update_is_new_data = metadata["update_is_new_data"]
6cf4bd6972c2 core, frontends: avatar refactoring:
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
399 except KeyError:
6cf4bd6972c2 core, frontends: avatar refactoring:
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
400 update_is_new_data = self.defaultUpdateIsNewData
6cf4bd6972c2 core, frontends: avatar refactoring:
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
401
3257
704dada41df0 plugin identity: fixed update with `get_all` + better new data detection for avatar:
Goffi <goffi@goffi.org>
parents: 3254
diff changeset
402 if data is None:
3254
6cf4bd6972c2 core, frontends: avatar refactoring:
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
403 if cached_data is None:
6cf4bd6972c2 core, frontends: avatar refactoring:
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
404 log.debug(
6cf4bd6972c2 core, frontends: avatar refactoring:
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
405 f"{metadata_name} for {entity} is already disabled, nothing to "
6cf4bd6972c2 core, frontends: avatar refactoring:
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
406 f"do")
3257
704dada41df0 plugin identity: fixed update with `get_all` + better new data detection for avatar:
Goffi <goffi@goffi.org>
parents: 3254
diff changeset
407 return
3262
aa71f1d40300 plugin identity: fixed update when cached data is None
Goffi <goffi@goffi.org>
parents: 3257
diff changeset
408 elif cached_data is None:
aa71f1d40300 plugin identity: fixed update when cached data is None
Goffi <goffi@goffi.org>
parents: 3257
diff changeset
409 pass
3257
704dada41df0 plugin identity: fixed update with `get_all` + better new data detection for avatar:
Goffi <goffi@goffi.org>
parents: 3254
diff changeset
410 elif not update_is_new_data(client, entity, cached_data, data):
704dada41df0 plugin identity: fixed update with `get_all` + better new data detection for avatar:
Goffi <goffi@goffi.org>
parents: 3254
diff changeset
411 log.debug(
704dada41df0 plugin identity: fixed update with `get_all` + better new data detection for avatar:
Goffi <goffi@goffi.org>
parents: 3254
diff changeset
412 f"{metadata_name} for {entity} is already in cache, nothing to "
704dada41df0 plugin identity: fixed update with `get_all` + better new data detection for avatar:
Goffi <goffi@goffi.org>
parents: 3254
diff changeset
413 f"do")
3254
6cf4bd6972c2 core, frontends: avatar refactoring:
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
414 return
6cf4bd6972c2 core, frontends: avatar refactoring:
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
415
6cf4bd6972c2 core, frontends: avatar refactoring:
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
416 # we can't use the cache, so we do the update
6cf4bd6972c2 core, frontends: avatar refactoring:
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
417
6cf4bd6972c2 core, frontends: avatar refactoring:
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
418 log.debug(f"updating {metadata_name} for {entity}")
6cf4bd6972c2 core, frontends: avatar refactoring:
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
419
6cf4bd6972c2 core, frontends: avatar refactoring:
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
420 if metadata.get('get_all', False):
6cf4bd6972c2 core, frontends: avatar refactoring:
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
421 # get_all is set, meaning that we have to check all plugins
6cf4bd6972c2 core, frontends: avatar refactoring:
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
422 # so we first delete current cache
3257
704dada41df0 plugin identity: fixed update with `get_all` + better new data detection for avatar:
Goffi <goffi@goffi.org>
parents: 3254
diff changeset
423 try:
704dada41df0 plugin identity: fixed update with `get_all` + better new data detection for avatar:
Goffi <goffi@goffi.org>
parents: 3254
diff changeset
424 self.host.memory.delEntityDatum(client, entity, metadata_name)
3277
cf07641b764d plugin identity: fixed infinite loop on nicknames update
Goffi <goffi@goffi.org>
parents: 3262
diff changeset
425 except (KeyError, exceptions.UnknownEntityError):
3257
704dada41df0 plugin identity: fixed update with `get_all` + better new data detection for avatar:
Goffi <goffi@goffi.org>
parents: 3254
diff changeset
426 pass
3254
6cf4bd6972c2 core, frontends: avatar refactoring:
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
427 # then fill it again by calling get, which will retrieve all values
3277
cf07641b764d plugin identity: fixed infinite loop on nicknames update
Goffi <goffi@goffi.org>
parents: 3262
diff changeset
428 # we lock update to avoid infinite recursions (update can be called during
cf07641b764d plugin identity: fixed infinite loop on nicknames update
Goffi <goffi@goffi.org>
parents: 3262
diff changeset
429 # get callbacks)
cf07641b764d plugin identity: fixed infinite loop on nicknames update
Goffi <goffi@goffi.org>
parents: 3262
diff changeset
430 client._identity_update_lock.append((entity, metadata_name))
cf07641b764d plugin identity: fixed infinite loop on nicknames update
Goffi <goffi@goffi.org>
parents: 3262
diff changeset
431 await self.get(client, metadata_name, entity, prefilled_values={origin: data})
cf07641b764d plugin identity: fixed infinite loop on nicknames update
Goffi <goffi@goffi.org>
parents: 3262
diff changeset
432 client._identity_update_lock.remove((entity, metadata_name))
3254
6cf4bd6972c2 core, frontends: avatar refactoring:
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
433 return
6cf4bd6972c2 core, frontends: avatar refactoring:
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
434
6cf4bd6972c2 core, frontends: avatar refactoring:
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
435 if data is not None:
6cf4bd6972c2 core, frontends: avatar refactoring:
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
436 data_filter = metadata['update_data_filter']
6cf4bd6972c2 core, frontends: avatar refactoring:
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
437 if data_filter is not None:
6cf4bd6972c2 core, frontends: avatar refactoring:
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
438 data = await utils.asDeferred(data_filter, client, entity, data)
6cf4bd6972c2 core, frontends: avatar refactoring:
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
439 self.checkType(metadata_name, data)
6cf4bd6972c2 core, frontends: avatar refactoring:
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
440
6cf4bd6972c2 core, frontends: avatar refactoring:
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
441 self.host.memory.updateEntityData(client, entity, metadata_name, data)
6cf4bd6972c2 core, frontends: avatar refactoring:
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
442
6cf4bd6972c2 core, frontends: avatar refactoring:
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
443 if metadata.get('store', False):
6cf4bd6972c2 core, frontends: avatar refactoring:
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
444 key = f"{entity}\n{metadata_name}"
6cf4bd6972c2 core, frontends: avatar refactoring:
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
445 await client._identity_storage.aset(key, data)
6cf4bd6972c2 core, frontends: avatar refactoring:
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
446
6cf4bd6972c2 core, frontends: avatar refactoring:
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
447 def defaultUpdateIsNewData(self, client, entity, cached_data, new_data):
6cf4bd6972c2 core, frontends: avatar refactoring:
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
448 return new_data != cached_data
6cf4bd6972c2 core, frontends: avatar refactoring:
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
449
6cf4bd6972c2 core, frontends: avatar refactoring:
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
450 def _getAvatar(self, entity, use_cache, profile):
2253
db468f24b9fc plugin identity: plugin identity first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
451 client = self.host.getClient(profile)
3254
6cf4bd6972c2 core, frontends: avatar refactoring:
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
452 entity = jid.JID(entity) if entity else None
6cf4bd6972c2 core, frontends: avatar refactoring:
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
453 d = defer.ensureDeferred(self.get(client, "avatar", entity, use_cache))
6cf4bd6972c2 core, frontends: avatar refactoring:
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
454 d.addCallback(lambda data: data_format.serialise(data))
6cf4bd6972c2 core, frontends: avatar refactoring:
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
455 return d
6cf4bd6972c2 core, frontends: avatar refactoring:
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
456
6cf4bd6972c2 core, frontends: avatar refactoring:
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
457 def _setAvatar(self, file_path, entity, profile_key=C.PROF_KEY_NONE):
6cf4bd6972c2 core, frontends: avatar refactoring:
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
458 client = self.host.getClient(profile_key)
6cf4bd6972c2 core, frontends: avatar refactoring:
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
459 entity = jid.JID(entity) if entity else None
6cf4bd6972c2 core, frontends: avatar refactoring:
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
460 return defer.ensureDeferred(
6cf4bd6972c2 core, frontends: avatar refactoring:
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
461 self.set(client, "avatar", file_path, entity))
6cf4bd6972c2 core, frontends: avatar refactoring:
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
462
6cf4bd6972c2 core, frontends: avatar refactoring:
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
463 async def avatarSetDataFilter(self, client, entity, file_path):
6cf4bd6972c2 core, frontends: avatar refactoring:
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
464 """Convert avatar file path to dict data"""
6cf4bd6972c2 core, frontends: avatar refactoring:
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
465 file_path = Path(file_path)
6cf4bd6972c2 core, frontends: avatar refactoring:
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
466 if not file_path.is_file():
6cf4bd6972c2 core, frontends: avatar refactoring:
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
467 raise ValueError(f"There is no file at {file_path} to use as avatar")
6cf4bd6972c2 core, frontends: avatar refactoring:
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
468 avatar_data = {
6cf4bd6972c2 core, frontends: avatar refactoring:
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
469 'path': file_path,
3326
9e1ba1e1179f plugin identity: added "filename" metadata for avatar
Goffi <goffi@goffi.org>
parents: 3278
diff changeset
470 'filename': file_path.name,
3254
6cf4bd6972c2 core, frontends: avatar refactoring:
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
471 'media_type': image.guess_type(file_path),
6cf4bd6972c2 core, frontends: avatar refactoring:
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
472 }
6cf4bd6972c2 core, frontends: avatar refactoring:
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
473 media_type = avatar_data['media_type']
6cf4bd6972c2 core, frontends: avatar refactoring:
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
474 if media_type is None:
6cf4bd6972c2 core, frontends: avatar refactoring:
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
475 raise ValueError(f"Can't identify type of image at {file_path}")
6cf4bd6972c2 core, frontends: avatar refactoring:
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
476 if not media_type.startswith('image/'):
6cf4bd6972c2 core, frontends: avatar refactoring:
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
477 raise ValueError(f"File at {file_path} doesn't appear to be an image")
6cf4bd6972c2 core, frontends: avatar refactoring:
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
478 return avatar_data
6cf4bd6972c2 core, frontends: avatar refactoring:
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
479
6cf4bd6972c2 core, frontends: avatar refactoring:
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
480 async def avatarSetPostTreatment(self, client, entity, avatar_data):
6cf4bd6972c2 core, frontends: avatar refactoring:
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
481 """Update our own avatar"""
3278
27d4b71e264a plugin identity: fixed update in avatarSetPostTreatment
Goffi <goffi@goffi.org>
parents: 3277
diff changeset
482 await self.update(client, IMPORT_NAME, "avatar", avatar_data, entity)
3254
6cf4bd6972c2 core, frontends: avatar refactoring:
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
483
6cf4bd6972c2 core, frontends: avatar refactoring:
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
484 def avatarBuildMetadata(self, path, media_type=None, cache_uid=None):
6cf4bd6972c2 core, frontends: avatar refactoring:
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
485 """Helper method to generate avatar metadata
6cf4bd6972c2 core, frontends: avatar refactoring:
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
486
6cf4bd6972c2 core, frontends: avatar refactoring:
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
487 @param path(str, Path, None): path to avatar file
6cf4bd6972c2 core, frontends: avatar refactoring:
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
488 avatar file must be in cache
6cf4bd6972c2 core, frontends: avatar refactoring:
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
489 None if avatar is explicitely not set
6cf4bd6972c2 core, frontends: avatar refactoring:
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
490 @param media_type(str, None): type of the avatar file (MIME type)
6cf4bd6972c2 core, frontends: avatar refactoring:
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
491 @param cache_uid(str, None): UID of avatar in cache
6cf4bd6972c2 core, frontends: avatar refactoring:
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
492 @return (dict, None): avatar metadata
6cf4bd6972c2 core, frontends: avatar refactoring:
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
493 None if avatar is not set
6cf4bd6972c2 core, frontends: avatar refactoring:
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
494 """
6cf4bd6972c2 core, frontends: avatar refactoring:
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
495 if path is None:
6cf4bd6972c2 core, frontends: avatar refactoring:
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
496 return None
6cf4bd6972c2 core, frontends: avatar refactoring:
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
497 else:
6cf4bd6972c2 core, frontends: avatar refactoring:
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
498 if cache_uid is None:
6cf4bd6972c2 core, frontends: avatar refactoring:
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
499 raise ValueError("cache_uid must be set if path is set")
6cf4bd6972c2 core, frontends: avatar refactoring:
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
500 path = Path(path)
6cf4bd6972c2 core, frontends: avatar refactoring:
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
501 if media_type is None:
6cf4bd6972c2 core, frontends: avatar refactoring:
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
502 media_type = image.guess_type(path)
6cf4bd6972c2 core, frontends: avatar refactoring:
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
503
6cf4bd6972c2 core, frontends: avatar refactoring:
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
504 return {
6cf4bd6972c2 core, frontends: avatar refactoring:
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
505 "path": path,
3326
9e1ba1e1179f plugin identity: added "filename" metadata for avatar
Goffi <goffi@goffi.org>
parents: 3278
diff changeset
506 "filename": path.name,
3254
6cf4bd6972c2 core, frontends: avatar refactoring:
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
507 "media_type": media_type,
6cf4bd6972c2 core, frontends: avatar refactoring:
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
508 "cache_uid": cache_uid,
6cf4bd6972c2 core, frontends: avatar refactoring:
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
509 }
6cf4bd6972c2 core, frontends: avatar refactoring:
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
510
3257
704dada41df0 plugin identity: fixed update with `get_all` + better new data detection for avatar:
Goffi <goffi@goffi.org>
parents: 3254
diff changeset
511 def avatarUpdateIsNewData(self, client, entity, cached_data, new_data):
704dada41df0 plugin identity: fixed update with `get_all` + better new data detection for avatar:
Goffi <goffi@goffi.org>
parents: 3254
diff changeset
512 return new_data['path'] != cached_data['path']
3254
6cf4bd6972c2 core, frontends: avatar refactoring:
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
513
6cf4bd6972c2 core, frontends: avatar refactoring:
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
514 async def avatarUpdateDataFilter(self, client, entity, data):
6cf4bd6972c2 core, frontends: avatar refactoring:
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
515 if not isinstance(data, dict):
6cf4bd6972c2 core, frontends: avatar refactoring:
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
516 raise ValueError(f"Invalid data type ({type(data)}), a dict is expected")
3326
9e1ba1e1179f plugin identity: added "filename" metadata for avatar
Goffi <goffi@goffi.org>
parents: 3278
diff changeset
517 mandatory_keys = {'path', 'filename', 'cache_uid'}
3254
6cf4bd6972c2 core, frontends: avatar refactoring:
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
518 if not data.keys() >= mandatory_keys:
6cf4bd6972c2 core, frontends: avatar refactoring:
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
519 raise ValueError(f"missing avatar data keys: {mandatory_keys - data.keys()}")
6cf4bd6972c2 core, frontends: avatar refactoring:
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
520 return data
6cf4bd6972c2 core, frontends: avatar refactoring:
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
521
6cf4bd6972c2 core, frontends: avatar refactoring:
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
522 async def nicknamesGetPostTreatment(self, client, entity, plugin_nicknames):
6cf4bd6972c2 core, frontends: avatar refactoring:
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
523 """Prepend nicknames from core locations + set default nickname
6cf4bd6972c2 core, frontends: avatar refactoring:
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
524
6cf4bd6972c2 core, frontends: avatar refactoring:
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
525 nicknames are checked from many locations, there is always at least
6cf4bd6972c2 core, frontends: avatar refactoring:
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
526 one nickname. First nickname of the list can be used in priority.
6cf4bd6972c2 core, frontends: avatar refactoring:
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
527 Nicknames are appended in this order:
6cf4bd6972c2 core, frontends: avatar refactoring:
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
528 - roster, plugins set nicknames
6cf4bd6972c2 core, frontends: avatar refactoring:
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
529 - if no nickname is found, user part of jid is then used, or bare jid
6cf4bd6972c2 core, frontends: avatar refactoring:
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
530 if there is no user part.
6cf4bd6972c2 core, frontends: avatar refactoring:
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
531 For MUC, room nick is always put first
6cf4bd6972c2 core, frontends: avatar refactoring:
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
532 """
6cf4bd6972c2 core, frontends: avatar refactoring:
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
533 # we first check roster
6cf4bd6972c2 core, frontends: avatar refactoring:
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
534 nicknames = []
6cf4bd6972c2 core, frontends: avatar refactoring:
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
535 if entity.resource:
6cf4bd6972c2 core, frontends: avatar refactoring:
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
536 # getIdentityJid let the resource only if the entity is a MUC room
6cf4bd6972c2 core, frontends: avatar refactoring:
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
537 # occupant jid
6cf4bd6972c2 core, frontends: avatar refactoring:
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
538 nicknames.append(entity.resource)
6cf4bd6972c2 core, frontends: avatar refactoring:
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
539
6cf4bd6972c2 core, frontends: avatar refactoring:
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
540 roster_item = client.roster.getItem(entity.userhostJID())
6cf4bd6972c2 core, frontends: avatar refactoring:
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
541 if roster_item is not None and roster_item.name:
6cf4bd6972c2 core, frontends: avatar refactoring:
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
542 # user set name has priority over entity set name
6cf4bd6972c2 core, frontends: avatar refactoring:
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
543 nicknames.append(roster_item.name)
6cf4bd6972c2 core, frontends: avatar refactoring:
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
544
6cf4bd6972c2 core, frontends: avatar refactoring:
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
545 nicknames.extend(plugin_nicknames)
6cf4bd6972c2 core, frontends: avatar refactoring:
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
546
6cf4bd6972c2 core, frontends: avatar refactoring:
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
547 if not nicknames:
6cf4bd6972c2 core, frontends: avatar refactoring:
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
548 if entity.user:
6cf4bd6972c2 core, frontends: avatar refactoring:
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
549 nicknames.append(entity.user.capitalize())
6cf4bd6972c2 core, frontends: avatar refactoring:
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
550 else:
6cf4bd6972c2 core, frontends: avatar refactoring:
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
551 nicknames.append(entity.userhost())
6cf4bd6972c2 core, frontends: avatar refactoring:
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
552
6cf4bd6972c2 core, frontends: avatar refactoring:
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
553 # we remove duplicates while preserving order with dict
6cf4bd6972c2 core, frontends: avatar refactoring:
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
554 return list(dict.fromkeys(nicknames))
6cf4bd6972c2 core, frontends: avatar refactoring:
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
555
6cf4bd6972c2 core, frontends: avatar refactoring:
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
556 def nicknamesUpdateIsNewData(self, client, entity, cached_data, new_nicknames):
6cf4bd6972c2 core, frontends: avatar refactoring:
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
557 return not set(new_nicknames).issubset(cached_data)
6cf4bd6972c2 core, frontends: avatar refactoring:
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
558
6cf4bd6972c2 core, frontends: avatar refactoring:
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
559 def _getIdentity(self, entity_s, metadata_filter, use_cache, profile):
6cf4bd6972c2 core, frontends: avatar refactoring:
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
560 entity = jid.JID(entity_s)
6cf4bd6972c2 core, frontends: avatar refactoring:
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
561 client = self.host.getClient(profile)
6cf4bd6972c2 core, frontends: avatar refactoring:
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
562 d = defer.ensureDeferred(
6cf4bd6972c2 core, frontends: avatar refactoring:
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
563 self.getIdentity(client, entity, metadata_filter, use_cache))
6cf4bd6972c2 core, frontends: avatar refactoring:
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
564 d.addCallback(lambda data: data_format.serialise(data))
6cf4bd6972c2 core, frontends: avatar refactoring:
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
565 return d
6cf4bd6972c2 core, frontends: avatar refactoring:
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
566
6cf4bd6972c2 core, frontends: avatar refactoring:
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
567 async def getIdentity(
6cf4bd6972c2 core, frontends: avatar refactoring:
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
568 self, client, entity=None, metadata_filter=None, use_cache=True):
2253
db468f24b9fc plugin identity: plugin identity first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
569 """Retrieve identity of an entity
db468f24b9fc plugin identity: plugin identity first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
570
3254
6cf4bd6972c2 core, frontends: avatar refactoring:
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
571 @param entity(jid.JID, None): entity to check
6cf4bd6972c2 core, frontends: avatar refactoring:
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
572 @param metadata_filter(list[str], None): if not None or empty, only return
6cf4bd6972c2 core, frontends: avatar refactoring:
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
573 metadata in this filter
6cf4bd6972c2 core, frontends: avatar refactoring:
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
574 @param use_cache(bool): if False, cache won't be checked
6cf4bd6972c2 core, frontends: avatar refactoring:
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
575 should be True most of time, to avoid useless network requests
6cf4bd6972c2 core, frontends: avatar refactoring:
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
576 @return (dict): identity data
2253
db468f24b9fc plugin identity: plugin identity first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
577 """
db468f24b9fc plugin identity: plugin identity first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
578 id_data = {}
3254
6cf4bd6972c2 core, frontends: avatar refactoring:
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
579
6cf4bd6972c2 core, frontends: avatar refactoring:
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
580 if not metadata_filter:
6cf4bd6972c2 core, frontends: avatar refactoring:
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
581 metadata_names = self.metadata.keys()
2253
db468f24b9fc plugin identity: plugin identity first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
582 else:
3254
6cf4bd6972c2 core, frontends: avatar refactoring:
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
583 metadata_names = metadata_filter
2253
db468f24b9fc plugin identity: plugin identity first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
584
3254
6cf4bd6972c2 core, frontends: avatar refactoring:
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
585 for metadata_name in metadata_names:
6cf4bd6972c2 core, frontends: avatar refactoring:
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
586 id_data[metadata_name] = await self.get(
6cf4bd6972c2 core, frontends: avatar refactoring:
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
587 client, metadata_name, entity, use_cache)
6cf4bd6972c2 core, frontends: avatar refactoring:
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
588
6cf4bd6972c2 core, frontends: avatar refactoring:
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
589 return id_data
2463
65a6d2496504 plugin identity: return resource for MUC room occupants' nicks + added avatar and avatar_basename.
Goffi <goffi@goffi.org>
parents: 2427
diff changeset
590
3254
6cf4bd6972c2 core, frontends: avatar refactoring:
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
591 def _setIdentity(self, id_data_s, profile):
2253
db468f24b9fc plugin identity: plugin identity first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
592 client = self.host.getClient(profile)
3254
6cf4bd6972c2 core, frontends: avatar refactoring:
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
593 id_data = data_format.deserialise(id_data_s)
6cf4bd6972c2 core, frontends: avatar refactoring:
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
594 return defer.ensureDeferred(self.setIdentity(client, id_data))
2253
db468f24b9fc plugin identity: plugin identity first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
595
3254
6cf4bd6972c2 core, frontends: avatar refactoring:
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
596 async def setIdentity(self, client, id_data):
2253
db468f24b9fc plugin identity: plugin identity first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
597 """Update profile's identity
db468f24b9fc plugin identity: plugin identity first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
598
3254
6cf4bd6972c2 core, frontends: avatar refactoring:
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
599 @param id_data(dict): data to update, key can be on of self.metadata keys
2253
db468f24b9fc plugin identity: plugin identity first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
600 """
3254
6cf4bd6972c2 core, frontends: avatar refactoring:
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
601 if not id_data.keys() <= self.metadata.keys():
6cf4bd6972c2 core, frontends: avatar refactoring:
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
602 raise ValueError(
6cf4bd6972c2 core, frontends: avatar refactoring:
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
603 f"Invalid metadata names: {id_data.keys() - self.metadata.keys()}")
6cf4bd6972c2 core, frontends: avatar refactoring:
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
604 for metadata_name, data in id_data.items():
6cf4bd6972c2 core, frontends: avatar refactoring:
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
605 try:
6cf4bd6972c2 core, frontends: avatar refactoring:
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
606 await self.set(client, metadata_name, data)
6cf4bd6972c2 core, frontends: avatar refactoring:
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
607 except Exception as e:
6cf4bd6972c2 core, frontends: avatar refactoring:
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
608 log.warning(
6cf4bd6972c2 core, frontends: avatar refactoring:
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
609 _("Can't set metadata {metadata_name!r}: {reason}")
6cf4bd6972c2 core, frontends: avatar refactoring:
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
610 .format(metadata_name=metadata_name, reason=e))