annotate sat/plugins/plugin_misc_identity.py @ 3836:e183f1fbfa8d

doc (cli/pubsub): add doc for ``reference`` subcommand: rel 369
author Goffi <goffi@goffi.org>
date Sun, 10 Jul 2022 16:15:09 +0200
parents 998c5318230f
children 524856bd7b19
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
3479
be6d91572633 date update
Goffi <goffi@goffi.org>
parents: 3338
diff changeset
3 # Copyright (C) 2009-2021 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
3254
6cf4bd6972c2 core, frontends: avatar refactoring:
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
18 from collections import namedtuple
3816
213e83a4ed10 plugin identity, XEP-0054: move avatar resizing and caching method to identity plugin:
Goffi <goffi@goffi.org>
parents: 3541
diff changeset
19 import io
3254
6cf4bd6972c2 core, frontends: avatar refactoring:
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
20 from pathlib import Path
3816
213e83a4ed10 plugin identity, XEP-0054: move avatar resizing and caching method to identity plugin:
Goffi <goffi@goffi.org>
parents: 3541
diff changeset
21 from base64 import b64encode
213e83a4ed10 plugin identity, XEP-0054: move avatar resizing and caching method to identity plugin:
Goffi <goffi@goffi.org>
parents: 3541
diff changeset
22 import hashlib
213e83a4ed10 plugin identity, XEP-0054: move avatar resizing and caching method to identity plugin:
Goffi <goffi@goffi.org>
parents: 3541
diff changeset
23 from typing import Any, Coroutine, Dict, List, Optional, Union
213e83a4ed10 plugin identity, XEP-0054: move avatar resizing and caching method to identity plugin:
Goffi <goffi@goffi.org>
parents: 3541
diff changeset
24
213e83a4ed10 plugin identity, XEP-0054: move avatar resizing and caching method to identity plugin:
Goffi <goffi@goffi.org>
parents: 3541
diff changeset
25 from twisted.internet import defer, threads
3254
6cf4bd6972c2 core, frontends: avatar refactoring:
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
26 from twisted.words.protocols.jabber import jid
3816
213e83a4ed10 plugin identity, XEP-0054: move avatar resizing and caching method to identity plugin:
Goffi <goffi@goffi.org>
parents: 3541
diff changeset
27
213e83a4ed10 plugin identity, XEP-0054: move avatar resizing and caching method to identity plugin:
Goffi <goffi@goffi.org>
parents: 3541
diff changeset
28 from sat.core import exceptions
2253
db468f24b9fc plugin identity: plugin identity first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
29 from sat.core.constants import Const as C
3816
213e83a4ed10 plugin identity, XEP-0054: move avatar resizing and caching method to identity plugin:
Goffi <goffi@goffi.org>
parents: 3541
diff changeset
30 from sat.core.i18n import _
2253
db468f24b9fc plugin identity: plugin identity first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
31 from sat.core.log import getLogger
3816
213e83a4ed10 plugin identity, XEP-0054: move avatar resizing and caching method to identity plugin:
Goffi <goffi@goffi.org>
parents: 3541
diff changeset
32 from sat.core.xmpp import SatXMPPEntity
3254
6cf4bd6972c2 core, frontends: avatar refactoring:
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
33 from sat.memory import persistent
6cf4bd6972c2 core, frontends: avatar refactoring:
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
34 from sat.tools import image
6cf4bd6972c2 core, frontends: avatar refactoring:
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
35 from sat.tools import utils
6cf4bd6972c2 core, frontends: avatar refactoring:
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
36 from sat.tools.common import data_format
6cf4bd6972c2 core, frontends: avatar refactoring:
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
37
3816
213e83a4ed10 plugin identity, XEP-0054: move avatar resizing and caching method to identity plugin:
Goffi <goffi@goffi.org>
parents: 3541
diff changeset
38 try:
213e83a4ed10 plugin identity, XEP-0054: move avatar resizing and caching method to identity plugin:
Goffi <goffi@goffi.org>
parents: 3541
diff changeset
39 from PIL import Image
213e83a4ed10 plugin identity, XEP-0054: move avatar resizing and caching method to identity plugin:
Goffi <goffi@goffi.org>
parents: 3541
diff changeset
40 except:
213e83a4ed10 plugin identity, XEP-0054: move avatar resizing and caching method to identity plugin:
Goffi <goffi@goffi.org>
parents: 3541
diff changeset
41 raise exceptions.MissingModule(
213e83a4ed10 plugin identity, XEP-0054: move avatar resizing and caching method to identity plugin:
Goffi <goffi@goffi.org>
parents: 3541
diff changeset
42 "Missing module pillow, please download/install it from https://python-pillow.github.io"
213e83a4ed10 plugin identity, XEP-0054: move avatar resizing and caching method to identity plugin:
Goffi <goffi@goffi.org>
parents: 3541
diff changeset
43 )
213e83a4ed10 plugin identity, XEP-0054: move avatar resizing and caching method to identity plugin:
Goffi <goffi@goffi.org>
parents: 3541
diff changeset
44
213e83a4ed10 plugin identity, XEP-0054: move avatar resizing and caching method to identity plugin:
Goffi <goffi@goffi.org>
parents: 3541
diff changeset
45
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
46
2253
db468f24b9fc plugin identity: plugin identity first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
47 log = getLogger(__name__)
db468f24b9fc plugin identity: plugin identity first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
48
db468f24b9fc plugin identity: plugin identity first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
49
3278
27d4b71e264a plugin identity: fixed update in avatarSetPostTreatment
Goffi <goffi@goffi.org>
parents: 3277
diff changeset
50 IMPORT_NAME = "IDENTITY"
27d4b71e264a plugin identity: fixed update in avatarSetPostTreatment
Goffi <goffi@goffi.org>
parents: 3277
diff changeset
51
27d4b71e264a plugin identity: fixed update in avatarSetPostTreatment
Goffi <goffi@goffi.org>
parents: 3277
diff changeset
52
2253
db468f24b9fc plugin identity: plugin identity first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
53 PLUGIN_INFO = {
db468f24b9fc plugin identity: plugin identity first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
54 C.PI_NAME: "Identity Plugin",
3278
27d4b71e264a plugin identity: fixed update in avatarSetPostTreatment
Goffi <goffi@goffi.org>
parents: 3277
diff changeset
55 C.PI_IMPORT_NAME: IMPORT_NAME,
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
56 C.PI_TYPE: C.PLUG_TYPE_MISC,
3817
998c5318230f plugin identity: make the plugin compatible with component + description:
Goffi <goffi@goffi.org>
parents: 3816
diff changeset
57 C.PI_MODES: C.PLUG_MODE_BOTH,
2253
db468f24b9fc plugin identity: plugin identity first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
58 C.PI_PROTOCOLS: [],
3254
6cf4bd6972c2 core, frontends: avatar refactoring:
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
59 C.PI_DEPENDENCIES: [],
6cf4bd6972c2 core, frontends: avatar refactoring:
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
60 C.PI_RECOMMENDATIONS: ["XEP-0045"],
2253
db468f24b9fc plugin identity: plugin identity first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
61 C.PI_MAIN: "Identity",
db468f24b9fc plugin identity: plugin identity first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
62 C.PI_HANDLER: "no",
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
63 C.PI_DESCRIPTION: _("""Identity manager"""),
2253
db468f24b9fc plugin identity: plugin identity first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
64 }
db468f24b9fc plugin identity: plugin identity first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
65
3277
cf07641b764d plugin identity: fixed infinite loop on nicknames update
Goffi <goffi@goffi.org>
parents: 3262
diff changeset
66 Callback = namedtuple("Callback", ("origin", "get", "set", "priority"))
3816
213e83a4ed10 plugin identity, XEP-0054: move avatar resizing and caching method to identity plugin:
Goffi <goffi@goffi.org>
parents: 3541
diff changeset
67 AVATAR_DIM = (128, 128)
2253
db468f24b9fc plugin identity: plugin identity first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
68
3254
6cf4bd6972c2 core, frontends: avatar refactoring:
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
69
6cf4bd6972c2 core, frontends: avatar refactoring:
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
70 class Identity:
6cf4bd6972c2 core, frontends: avatar refactoring:
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
71
2253
db468f24b9fc plugin identity: plugin identity first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
72 def __init__(self, host):
3028
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 2771
diff changeset
73 log.info(_("Plugin Identity initialization"))
2253
db468f24b9fc plugin identity: plugin identity first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
74 self.host = host
3254
6cf4bd6972c2 core, frontends: avatar refactoring:
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
75 self._m = host.plugins.get("XEP-0045")
6cf4bd6972c2 core, frontends: avatar refactoring:
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
76 self.metadata = {
6cf4bd6972c2 core, frontends: avatar refactoring:
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
77 "avatar": {
6cf4bd6972c2 core, frontends: avatar refactoring:
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
78 "type": dict,
6cf4bd6972c2 core, frontends: avatar refactoring:
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
79 # convert avatar path to avatar metadata (and check validity)
6cf4bd6972c2 core, frontends: avatar refactoring:
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
80 "set_data_filter": self.avatarSetDataFilter,
6cf4bd6972c2 core, frontends: avatar refactoring:
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
81 # update profile avatar, so all frontends are aware
6cf4bd6972c2 core, frontends: avatar refactoring:
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
82 "set_post_treatment": self.avatarSetPostTreatment,
6cf4bd6972c2 core, frontends: avatar refactoring:
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
83 "update_is_new_data": self.avatarUpdateIsNewData,
6cf4bd6972c2 core, frontends: avatar refactoring:
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
84 "update_data_filter": self.avatarUpdateDataFilter,
6cf4bd6972c2 core, frontends: avatar refactoring:
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
85 # 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
86 # (it is stored only for roster entities)
6cf4bd6972c2 core, frontends: avatar refactoring:
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
87 "store": True,
6cf4bd6972c2 core, frontends: avatar refactoring:
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
88 },
6cf4bd6972c2 core, frontends: avatar refactoring:
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
89 "nicknames": {
6cf4bd6972c2 core, frontends: avatar refactoring:
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
90 "type": list,
6cf4bd6972c2 core, frontends: avatar refactoring:
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
91 # accumulate all nicknames from all callbacks in a list instead
6cf4bd6972c2 core, frontends: avatar refactoring:
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
92 # of returning only the data from the first successful callback
6cf4bd6972c2 core, frontends: avatar refactoring:
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
93 "get_all": True,
6cf4bd6972c2 core, frontends: avatar refactoring:
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
94 # append nicknames from roster, resource, etc.
6cf4bd6972c2 core, frontends: avatar refactoring:
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
95 "get_post_treatment": self.nicknamesGetPostTreatment,
6cf4bd6972c2 core, frontends: avatar refactoring:
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
96 "update_is_new_data": self.nicknamesUpdateIsNewData,
6cf4bd6972c2 core, frontends: avatar refactoring:
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
97 "store": True,
6cf4bd6972c2 core, frontends: avatar refactoring:
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
98 },
3817
998c5318230f plugin identity: make the plugin compatible with component + description:
Goffi <goffi@goffi.org>
parents: 3816
diff changeset
99 "description": {
998c5318230f plugin identity: make the plugin compatible with component + description:
Goffi <goffi@goffi.org>
parents: 3816
diff changeset
100 "type": str,
998c5318230f plugin identity: make the plugin compatible with component + description:
Goffi <goffi@goffi.org>
parents: 3816
diff changeset
101 "get_all": True,
998c5318230f plugin identity: make the plugin compatible with component + description:
Goffi <goffi@goffi.org>
parents: 3816
diff changeset
102 "get_post_treatment": self.descriptionGetPostTreatment,
998c5318230f plugin identity: make the plugin compatible with component + description:
Goffi <goffi@goffi.org>
parents: 3816
diff changeset
103 "store": True,
998c5318230f plugin identity: make the plugin compatible with component + description:
Goffi <goffi@goffi.org>
parents: 3816
diff changeset
104 }
3254
6cf4bd6972c2 core, frontends: avatar refactoring:
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
105 }
6cf4bd6972c2 core, frontends: avatar refactoring:
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
106 host.trigger.add("roster_update", self._rosterUpdateTrigger)
6cf4bd6972c2 core, frontends: avatar refactoring:
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
107 host.memory.setSignalOnUpdate("avatar")
6cf4bd6972c2 core, frontends: avatar refactoring:
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
108 host.memory.setSignalOnUpdate("nicknames")
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
109 host.bridge.addMethod(
3028
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 2771
diff changeset
110 "identityGet",
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 2771
diff changeset
111 ".plugin",
3254
6cf4bd6972c2 core, frontends: avatar refactoring:
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
112 in_sign="sasbs",
6cf4bd6972c2 core, frontends: avatar refactoring:
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
113 out_sign="s",
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
114 method=self._getIdentity,
3028
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 2771
diff changeset
115 async_=True,
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
116 )
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
117 host.bridge.addMethod(
3338
203a491fcd86 plugin identity: new methods `identitiesGet` and `identitiesBaseGet`
Goffi <goffi@goffi.org>
parents: 3326
diff changeset
118 "identitiesGet",
203a491fcd86 plugin identity: new methods `identitiesGet` and `identitiesBaseGet`
Goffi <goffi@goffi.org>
parents: 3326
diff changeset
119 ".plugin",
203a491fcd86 plugin identity: new methods `identitiesGet` and `identitiesBaseGet`
Goffi <goffi@goffi.org>
parents: 3326
diff changeset
120 in_sign="asass",
203a491fcd86 plugin identity: new methods `identitiesGet` and `identitiesBaseGet`
Goffi <goffi@goffi.org>
parents: 3326
diff changeset
121 out_sign="s",
203a491fcd86 plugin identity: new methods `identitiesGet` and `identitiesBaseGet`
Goffi <goffi@goffi.org>
parents: 3326
diff changeset
122 method=self._getIdentities,
203a491fcd86 plugin identity: new methods `identitiesGet` and `identitiesBaseGet`
Goffi <goffi@goffi.org>
parents: 3326
diff changeset
123 async_=True,
203a491fcd86 plugin identity: new methods `identitiesGet` and `identitiesBaseGet`
Goffi <goffi@goffi.org>
parents: 3326
diff changeset
124 )
203a491fcd86 plugin identity: new methods `identitiesGet` and `identitiesBaseGet`
Goffi <goffi@goffi.org>
parents: 3326
diff changeset
125 host.bridge.addMethod(
203a491fcd86 plugin identity: new methods `identitiesGet` and `identitiesBaseGet`
Goffi <goffi@goffi.org>
parents: 3326
diff changeset
126 "identitiesBaseGet",
203a491fcd86 plugin identity: new methods `identitiesGet` and `identitiesBaseGet`
Goffi <goffi@goffi.org>
parents: 3326
diff changeset
127 ".plugin",
203a491fcd86 plugin identity: new methods `identitiesGet` and `identitiesBaseGet`
Goffi <goffi@goffi.org>
parents: 3326
diff changeset
128 in_sign="s",
203a491fcd86 plugin identity: new methods `identitiesGet` and `identitiesBaseGet`
Goffi <goffi@goffi.org>
parents: 3326
diff changeset
129 out_sign="s",
203a491fcd86 plugin identity: new methods `identitiesGet` and `identitiesBaseGet`
Goffi <goffi@goffi.org>
parents: 3326
diff changeset
130 method=self._getBaseIdentities,
203a491fcd86 plugin identity: new methods `identitiesGet` and `identitiesBaseGet`
Goffi <goffi@goffi.org>
parents: 3326
diff changeset
131 async_=True,
203a491fcd86 plugin identity: new methods `identitiesGet` and `identitiesBaseGet`
Goffi <goffi@goffi.org>
parents: 3326
diff changeset
132 )
203a491fcd86 plugin identity: new methods `identitiesGet` and `identitiesBaseGet`
Goffi <goffi@goffi.org>
parents: 3326
diff changeset
133 host.bridge.addMethod(
3028
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 2771
diff changeset
134 "identitySet",
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 2771
diff changeset
135 ".plugin",
3254
6cf4bd6972c2 core, frontends: avatar refactoring:
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
136 in_sign="ss",
3028
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 2771
diff changeset
137 out_sign="",
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
138 method=self._setIdentity,
3028
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 2771
diff changeset
139 async_=True,
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
140 )
3254
6cf4bd6972c2 core, frontends: avatar refactoring:
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
141 host.bridge.addMethod(
6cf4bd6972c2 core, frontends: avatar refactoring:
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
142 "avatarGet",
6cf4bd6972c2 core, frontends: avatar refactoring:
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
143 ".plugin",
6cf4bd6972c2 core, frontends: avatar refactoring:
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
144 in_sign="sbs",
6cf4bd6972c2 core, frontends: avatar refactoring:
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
145 out_sign="s",
6cf4bd6972c2 core, frontends: avatar refactoring:
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
146 method=self._getAvatar,
6cf4bd6972c2 core, frontends: avatar refactoring:
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
147 async_=True,
6cf4bd6972c2 core, frontends: avatar refactoring:
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
148 )
6cf4bd6972c2 core, frontends: avatar refactoring:
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
149 host.bridge.addMethod(
6cf4bd6972c2 core, frontends: avatar refactoring:
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
150 "avatarSet",
6cf4bd6972c2 core, frontends: avatar refactoring:
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
151 ".plugin",
6cf4bd6972c2 core, frontends: avatar refactoring:
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
152 in_sign="sss",
6cf4bd6972c2 core, frontends: avatar refactoring:
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
153 out_sign="",
6cf4bd6972c2 core, frontends: avatar refactoring:
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
154 method=self._setAvatar,
6cf4bd6972c2 core, frontends: avatar refactoring:
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
155 async_=True,
6cf4bd6972c2 core, frontends: avatar refactoring:
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
156 )
2253
db468f24b9fc plugin identity: plugin identity first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
157
3254
6cf4bd6972c2 core, frontends: avatar refactoring:
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
158 async def profileConnecting(self, client):
3277
cf07641b764d plugin identity: fixed infinite loop on nicknames update
Goffi <goffi@goffi.org>
parents: 3262
diff changeset
159 client._identity_update_lock = []
3254
6cf4bd6972c2 core, frontends: avatar refactoring:
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
160 # we restore known identities from database
6cf4bd6972c2 core, frontends: avatar refactoring:
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
161 client._identity_storage = persistent.LazyPersistentBinaryDict(
6cf4bd6972c2 core, frontends: avatar refactoring:
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
162 "identity", client.profile)
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 stored_data = await client._identity_storage.all()
6cf4bd6972c2 core, frontends: avatar refactoring:
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
165
6cf4bd6972c2 core, frontends: avatar refactoring:
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
166 to_delete = []
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, value in stored_data.items():
6cf4bd6972c2 core, frontends: avatar refactoring:
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
169 entity_s, name = key.split('\n')
6cf4bd6972c2 core, frontends: avatar refactoring:
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
170 if name not in self.metadata.keys():
6cf4bd6972c2 core, frontends: avatar refactoring:
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
171 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
172 to_delete.append(key)
6cf4bd6972c2 core, frontends: avatar refactoring:
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
173 continue
6cf4bd6972c2 core, frontends: avatar refactoring:
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
174 entity = jid.JID(entity_s)
6cf4bd6972c2 core, frontends: avatar refactoring:
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
175
6cf4bd6972c2 core, frontends: avatar refactoring:
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
176 if name == 'avatar':
6cf4bd6972c2 core, frontends: avatar refactoring:
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
177 if value is not None:
6cf4bd6972c2 core, frontends: avatar refactoring:
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
178 try:
6cf4bd6972c2 core, frontends: avatar refactoring:
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
179 cache_uid = value['cache_uid']
6cf4bd6972c2 core, frontends: avatar refactoring:
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
180 if not cache_uid:
6cf4bd6972c2 core, frontends: avatar refactoring:
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
181 raise ValueError
3326
9e1ba1e1179f plugin identity: added "filename" metadata for avatar
Goffi <goffi@goffi.org>
parents: 3278
diff changeset
182 filename = value['filename']
9e1ba1e1179f plugin identity: added "filename" metadata for avatar
Goffi <goffi@goffi.org>
parents: 3278
diff changeset
183 if not filename:
9e1ba1e1179f plugin identity: added "filename" metadata for avatar
Goffi <goffi@goffi.org>
parents: 3278
diff changeset
184 raise ValueError
3254
6cf4bd6972c2 core, frontends: avatar refactoring:
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
185 except (ValueError, KeyError):
6cf4bd6972c2 core, frontends: avatar refactoring:
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
186 log.warning(
6cf4bd6972c2 core, frontends: avatar refactoring:
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
187 f"invalid data for {entity} avatar, it will be deleted: "
6cf4bd6972c2 core, frontends: avatar refactoring:
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
188 f"{value}")
6cf4bd6972c2 core, frontends: avatar refactoring:
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
189 to_delete.append(key)
6cf4bd6972c2 core, frontends: avatar refactoring:
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
190 continue
6cf4bd6972c2 core, frontends: avatar refactoring:
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
191 cache = self.host.common_cache.getMetadata(cache_uid)
6cf4bd6972c2 core, frontends: avatar refactoring:
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
192 if cache is None:
6cf4bd6972c2 core, frontends: avatar refactoring:
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
193 log.debug(
6cf4bd6972c2 core, frontends: avatar refactoring:
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
194 f"purging avatar for {entity}: it is not in cache anymore")
6cf4bd6972c2 core, frontends: avatar refactoring:
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
195 to_delete.append(key)
6cf4bd6972c2 core, frontends: avatar refactoring:
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
196 continue
6cf4bd6972c2 core, frontends: avatar refactoring:
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
197
6cf4bd6972c2 core, frontends: avatar refactoring:
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
198 self.host.memory.updateEntityData(
6cf4bd6972c2 core, frontends: avatar refactoring:
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
199 client, entity, name, value, silent=True
6cf4bd6972c2 core, frontends: avatar refactoring:
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
200 )
6cf4bd6972c2 core, frontends: avatar refactoring:
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
201
6cf4bd6972c2 core, frontends: avatar refactoring:
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
202 for key in to_delete:
6cf4bd6972c2 core, frontends: avatar refactoring:
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
203 await client._identity_storage.adel(key)
6cf4bd6972c2 core, frontends: avatar refactoring:
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
204
6cf4bd6972c2 core, frontends: avatar refactoring:
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
205 def _rosterUpdateTrigger(self, client, roster_item):
6cf4bd6972c2 core, frontends: avatar refactoring:
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
206 old_item = client.roster.getItem(roster_item.jid)
6cf4bd6972c2 core, frontends: avatar refactoring:
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
207 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
208 log.debug(
6cf4bd6972c2 core, frontends: avatar refactoring:
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
209 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
210 f"{roster_item.jid}"
6cf4bd6972c2 core, frontends: avatar refactoring:
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
211 )
6cf4bd6972c2 core, frontends: avatar refactoring:
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
212 defer.ensureDeferred(
6cf4bd6972c2 core, frontends: avatar refactoring:
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
213 self.update(
6cf4bd6972c2 core, frontends: avatar refactoring:
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
214 client,
3338
203a491fcd86 plugin identity: new methods `identitiesGet` and `identitiesBaseGet`
Goffi <goffi@goffi.org>
parents: 3326
diff changeset
215 IMPORT_NAME,
3254
6cf4bd6972c2 core, frontends: avatar refactoring:
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
216 "nicknames",
6cf4bd6972c2 core, frontends: avatar refactoring:
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
217 [roster_item.name],
6cf4bd6972c2 core, frontends: avatar refactoring:
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
218 roster_item.jid
6cf4bd6972c2 core, frontends: avatar refactoring:
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
219 )
6cf4bd6972c2 core, frontends: avatar refactoring:
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
220 )
6cf4bd6972c2 core, frontends: avatar refactoring:
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
221 return True
6cf4bd6972c2 core, frontends: avatar refactoring:
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
222
3277
cf07641b764d plugin identity: fixed infinite loop on nicknames update
Goffi <goffi@goffi.org>
parents: 3262
diff changeset
223 def register(
cf07641b764d plugin identity: fixed infinite loop on nicknames update
Goffi <goffi@goffi.org>
parents: 3262
diff changeset
224 self,
cf07641b764d plugin identity: fixed infinite loop on nicknames update
Goffi <goffi@goffi.org>
parents: 3262
diff changeset
225 origin: str,
cf07641b764d plugin identity: fixed infinite loop on nicknames update
Goffi <goffi@goffi.org>
parents: 3262
diff changeset
226 metadata_name: str,
cf07641b764d plugin identity: fixed infinite loop on nicknames update
Goffi <goffi@goffi.org>
parents: 3262
diff changeset
227 cb_get: Union[Coroutine, defer.Deferred],
cf07641b764d plugin identity: fixed infinite loop on nicknames update
Goffi <goffi@goffi.org>
parents: 3262
diff changeset
228 cb_set: Union[Coroutine, defer.Deferred],
cf07641b764d plugin identity: fixed infinite loop on nicknames update
Goffi <goffi@goffi.org>
parents: 3262
diff changeset
229 priority: int=0):
3254
6cf4bd6972c2 core, frontends: avatar refactoring:
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
230 """Register callbacks to handle identity metadata
6cf4bd6972c2 core, frontends: avatar refactoring:
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
231
3277
cf07641b764d plugin identity: fixed infinite loop on nicknames update
Goffi <goffi@goffi.org>
parents: 3262
diff changeset
232 @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
233 @param metadata_name: name of metadata can be:
3254
6cf4bd6972c2 core, frontends: avatar refactoring:
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
234 - avatar
6cf4bd6972c2 core, frontends: avatar refactoring:
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
235 - nicknames
3277
cf07641b764d plugin identity: fixed infinite loop on nicknames update
Goffi <goffi@goffi.org>
parents: 3262
diff changeset
236 @param cb_get: method to retrieve a metadata
3254
6cf4bd6972c2 core, frontends: avatar refactoring:
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
237 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
238 @param cb_set: method to set a metadata
3254
6cf4bd6972c2 core, frontends: avatar refactoring:
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
239 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
240 @param priority: priority of this method for the given metadata.
3254
6cf4bd6972c2 core, frontends: avatar refactoring:
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
241 methods with bigger priorities will be called first
6cf4bd6972c2 core, frontends: avatar refactoring:
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
242 """
6cf4bd6972c2 core, frontends: avatar refactoring:
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
243 if not metadata_name in self.metadata.keys():
6cf4bd6972c2 core, frontends: avatar refactoring:
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
244 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
245 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
246 cb_list = self.metadata[metadata_name].setdefault('callbacks', [])
6cf4bd6972c2 core, frontends: avatar refactoring:
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
247 cb_list.append(callback)
6cf4bd6972c2 core, frontends: avatar refactoring:
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
248 cb_list.sort(key=lambda c: c.priority, reverse=True)
6cf4bd6972c2 core, frontends: avatar refactoring:
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
249
6cf4bd6972c2 core, frontends: avatar refactoring:
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
250 def getIdentityJid(self, client, peer_jid):
6cf4bd6972c2 core, frontends: avatar refactoring:
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
251 """Return jid to use to set identity metadata
6cf4bd6972c2 core, frontends: avatar refactoring:
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
252
6cf4bd6972c2 core, frontends: avatar refactoring:
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
253 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
254 otherwise bare jid will be used
6cf4bd6972c2 core, frontends: avatar refactoring:
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
255 if None, bare jid of profile will be used
6cf4bd6972c2 core, frontends: avatar refactoring:
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
256 @return (jid.JID): jid to use for avatar
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 if peer_jid is None:
6cf4bd6972c2 core, frontends: avatar refactoring:
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
259 return client.jid.userhostJID()
6cf4bd6972c2 core, frontends: avatar refactoring:
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
260 if self._m is None:
6cf4bd6972c2 core, frontends: avatar refactoring:
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
261 return peer_jid.userhostJID()
6cf4bd6972c2 core, frontends: avatar refactoring:
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
262 else:
6cf4bd6972c2 core, frontends: avatar refactoring:
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
263 return self._m.getBareOrFull(client, peer_jid)
6cf4bd6972c2 core, frontends: avatar refactoring:
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
264
6cf4bd6972c2 core, frontends: avatar refactoring:
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
265 def checkType(self, metadata_name, value):
6cf4bd6972c2 core, frontends: avatar refactoring:
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
266 """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
267 value_type = self.metadata[metadata_name]["type"]
6cf4bd6972c2 core, frontends: avatar refactoring:
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
268 if not isinstance(value, value_type):
6cf4bd6972c2 core, frontends: avatar refactoring:
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
269 raise ValueError(
6cf4bd6972c2 core, frontends: avatar refactoring:
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
270 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
271 f"expected")
6cf4bd6972c2 core, frontends: avatar refactoring:
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
272
3817
998c5318230f plugin identity: make the plugin compatible with component + description:
Goffi <goffi@goffi.org>
parents: 3816
diff changeset
273 def getFieldType(self, metadata_name: str) -> str:
998c5318230f plugin identity: make the plugin compatible with component + description:
Goffi <goffi@goffi.org>
parents: 3816
diff changeset
274 """Return the type the requested field
998c5318230f plugin identity: make the plugin compatible with component + description:
Goffi <goffi@goffi.org>
parents: 3816
diff changeset
275
998c5318230f plugin identity: make the plugin compatible with component + description:
Goffi <goffi@goffi.org>
parents: 3816
diff changeset
276 @param metadata_name: name of the field to check
998c5318230f plugin identity: make the plugin compatible with component + description:
Goffi <goffi@goffi.org>
parents: 3816
diff changeset
277 @raise KeyError: the request field doesn't exist
998c5318230f plugin identity: make the plugin compatible with component + description:
Goffi <goffi@goffi.org>
parents: 3816
diff changeset
278 """
998c5318230f plugin identity: make the plugin compatible with component + description:
Goffi <goffi@goffi.org>
parents: 3816
diff changeset
279 return self.metadata[metadata_name]["type"]
998c5318230f plugin identity: make the plugin compatible with component + description:
Goffi <goffi@goffi.org>
parents: 3816
diff changeset
280
3277
cf07641b764d plugin identity: fixed infinite loop on nicknames update
Goffi <goffi@goffi.org>
parents: 3262
diff changeset
281 async def get(
cf07641b764d plugin identity: fixed infinite loop on nicknames update
Goffi <goffi@goffi.org>
parents: 3262
diff changeset
282 self,
cf07641b764d plugin identity: fixed infinite loop on nicknames update
Goffi <goffi@goffi.org>
parents: 3262
diff changeset
283 client: SatXMPPEntity,
cf07641b764d plugin identity: fixed infinite loop on nicknames update
Goffi <goffi@goffi.org>
parents: 3262
diff changeset
284 metadata_name: str,
cf07641b764d plugin identity: fixed infinite loop on nicknames update
Goffi <goffi@goffi.org>
parents: 3262
diff changeset
285 entity: Optional[jid.JID],
cf07641b764d plugin identity: fixed infinite loop on nicknames update
Goffi <goffi@goffi.org>
parents: 3262
diff changeset
286 use_cache: bool=True,
cf07641b764d plugin identity: fixed infinite loop on nicknames update
Goffi <goffi@goffi.org>
parents: 3262
diff changeset
287 prefilled_values: Optional[Dict[str, Any]]=None
cf07641b764d plugin identity: fixed infinite loop on nicknames update
Goffi <goffi@goffi.org>
parents: 3262
diff changeset
288 ):
3254
6cf4bd6972c2 core, frontends: avatar refactoring:
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
289 """Retrieve identity metadata of an entity
6cf4bd6972c2 core, frontends: avatar refactoring:
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
290
6cf4bd6972c2 core, frontends: avatar refactoring:
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
291 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
292 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
293 @param metadata_name: name of the metadata
3254
6cf4bd6972c2 core, frontends: avatar refactoring:
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
294 must be one of self.metadata key
6cf4bd6972c2 core, frontends: avatar refactoring:
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
295 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
296 @param entity: entity for which avatar is requested
3254
6cf4bd6972c2 core, frontends: avatar refactoring:
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
297 None to use profile's jid
3277
cf07641b764d plugin identity: fixed infinite loop on nicknames update
Goffi <goffi@goffi.org>
parents: 3262
diff changeset
298 @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
299 @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
300 """
6cf4bd6972c2 core, frontends: avatar refactoring:
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
301 entity = self.getIdentityJid(client, entity)
6cf4bd6972c2 core, frontends: avatar refactoring:
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
302 try:
6cf4bd6972c2 core, frontends: avatar refactoring:
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
303 metadata = self.metadata[metadata_name]
6cf4bd6972c2 core, frontends: avatar refactoring:
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
304 except KeyError:
6cf4bd6972c2 core, frontends: avatar refactoring:
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
305 raise ValueError(f"Invalid metadata name: {metadata_name!r}")
6cf4bd6972c2 core, frontends: avatar refactoring:
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
306 get_all = metadata.get('get_all', False)
6cf4bd6972c2 core, frontends: avatar refactoring:
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
307 if use_cache:
6cf4bd6972c2 core, frontends: avatar refactoring:
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
308 try:
6cf4bd6972c2 core, frontends: avatar refactoring:
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
309 data = self.host.memory.getEntityDatum(
6cf4bd6972c2 core, frontends: avatar refactoring:
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
310 client, entity, metadata_name)
6cf4bd6972c2 core, frontends: avatar refactoring:
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
311 except (KeyError, exceptions.UnknownEntityError):
6cf4bd6972c2 core, frontends: avatar refactoring:
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
312 pass
6cf4bd6972c2 core, frontends: avatar refactoring:
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
313 else:
6cf4bd6972c2 core, frontends: avatar refactoring:
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
314 return data
6cf4bd6972c2 core, frontends: avatar refactoring:
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
315
6cf4bd6972c2 core, frontends: avatar refactoring:
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
316 try:
6cf4bd6972c2 core, frontends: avatar refactoring:
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
317 callbacks = metadata['callbacks']
6cf4bd6972c2 core, frontends: avatar refactoring:
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
318 except KeyError:
6cf4bd6972c2 core, frontends: avatar refactoring:
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
319 log.warning(_("No callback registered for {metadata_name}")
6cf4bd6972c2 core, frontends: avatar refactoring:
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
320 .format(metadata_name=metadata_name))
6cf4bd6972c2 core, frontends: avatar refactoring:
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
321 return [] if get_all else None
6cf4bd6972c2 core, frontends: avatar refactoring:
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
322
6cf4bd6972c2 core, frontends: avatar refactoring:
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
323 if get_all:
6cf4bd6972c2 core, frontends: avatar refactoring:
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
324 all_data = []
3277
cf07641b764d plugin identity: fixed infinite loop on nicknames update
Goffi <goffi@goffi.org>
parents: 3262
diff changeset
325 elif prefilled_values is not None:
cf07641b764d plugin identity: fixed infinite loop on nicknames update
Goffi <goffi@goffi.org>
parents: 3262
diff changeset
326 raise exceptions.InternalError(
cf07641b764d plugin identity: fixed infinite loop on nicknames update
Goffi <goffi@goffi.org>
parents: 3262
diff changeset
327 "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
328
6cf4bd6972c2 core, frontends: avatar refactoring:
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
329 for callback in callbacks:
6cf4bd6972c2 core, frontends: avatar refactoring:
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
330 try:
3277
cf07641b764d plugin identity: fixed infinite loop on nicknames update
Goffi <goffi@goffi.org>
parents: 3262
diff changeset
331 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
332 data = prefilled_values[callback.origin]
cf07641b764d plugin identity: fixed infinite loop on nicknames update
Goffi <goffi@goffi.org>
parents: 3262
diff changeset
333 log.debug(
cf07641b764d plugin identity: fixed infinite loop on nicknames update
Goffi <goffi@goffi.org>
parents: 3262
diff changeset
334 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
335 f"{callback.origin}")
cf07641b764d plugin identity: fixed infinite loop on nicknames update
Goffi <goffi@goffi.org>
parents: 3262
diff changeset
336 else:
cf07641b764d plugin identity: fixed infinite loop on nicknames update
Goffi <goffi@goffi.org>
parents: 3262
diff changeset
337 data = await defer.ensureDeferred(callback.get(client, entity))
3254
6cf4bd6972c2 core, frontends: avatar refactoring:
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
338 except exceptions.CancelError:
6cf4bd6972c2 core, frontends: avatar refactoring:
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
339 continue
6cf4bd6972c2 core, frontends: avatar refactoring:
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
340 except Exception as e:
6cf4bd6972c2 core, frontends: avatar refactoring:
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
341 log.warning(
6cf4bd6972c2 core, frontends: avatar refactoring:
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
342 _("Error while trying to get {metadata_name} with {callback}: {e}")
6cf4bd6972c2 core, frontends: avatar refactoring:
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
343 .format(callback=callback.get, metadata_name=metadata_name, e=e))
6cf4bd6972c2 core, frontends: avatar refactoring:
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
344 else:
6cf4bd6972c2 core, frontends: avatar refactoring:
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
345 if data:
6cf4bd6972c2 core, frontends: avatar refactoring:
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
346 self.checkType(metadata_name, data)
6cf4bd6972c2 core, frontends: avatar refactoring:
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
347 if get_all:
3817
998c5318230f plugin identity: make the plugin compatible with component + description:
Goffi <goffi@goffi.org>
parents: 3816
diff changeset
348 if isinstance(data, list):
998c5318230f plugin identity: make the plugin compatible with component + description:
Goffi <goffi@goffi.org>
parents: 3816
diff changeset
349 all_data.extend(data)
998c5318230f plugin identity: make the plugin compatible with component + description:
Goffi <goffi@goffi.org>
parents: 3816
diff changeset
350 else:
998c5318230f plugin identity: make the plugin compatible with component + description:
Goffi <goffi@goffi.org>
parents: 3816
diff changeset
351 all_data.append(data)
3254
6cf4bd6972c2 core, frontends: avatar refactoring:
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
352 else:
6cf4bd6972c2 core, frontends: avatar refactoring:
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
353 break
6cf4bd6972c2 core, frontends: avatar refactoring:
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
354 else:
6cf4bd6972c2 core, frontends: avatar refactoring:
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
355 data = None
6cf4bd6972c2 core, frontends: avatar refactoring:
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
356
6cf4bd6972c2 core, frontends: avatar refactoring:
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
357 if get_all:
6cf4bd6972c2 core, frontends: avatar refactoring:
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
358 data = all_data
6cf4bd6972c2 core, frontends: avatar refactoring:
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
359
6cf4bd6972c2 core, frontends: avatar refactoring:
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
360 post_treatment = metadata.get("get_post_treatment")
6cf4bd6972c2 core, frontends: avatar refactoring:
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
361 if post_treatment is not None:
6cf4bd6972c2 core, frontends: avatar refactoring:
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
362 data = await utils.asDeferred(post_treatment, client, entity, data)
6cf4bd6972c2 core, frontends: avatar refactoring:
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
363
6cf4bd6972c2 core, frontends: avatar refactoring:
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
364 self.host.memory.updateEntityData(
6cf4bd6972c2 core, frontends: avatar refactoring:
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
365 client, entity, metadata_name, data)
6cf4bd6972c2 core, frontends: avatar refactoring:
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
366
6cf4bd6972c2 core, frontends: avatar refactoring:
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
367 if metadata.get('store', False):
6cf4bd6972c2 core, frontends: avatar refactoring:
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
368 key = f"{entity}\n{metadata_name}"
6cf4bd6972c2 core, frontends: avatar refactoring:
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
369 await client._identity_storage.aset(key, data)
6cf4bd6972c2 core, frontends: avatar refactoring:
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
370
6cf4bd6972c2 core, frontends: avatar refactoring:
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
371 return data
6cf4bd6972c2 core, frontends: avatar refactoring:
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
372
6cf4bd6972c2 core, frontends: avatar refactoring:
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
373 async def set(self, client, metadata_name, data, entity=None):
6cf4bd6972c2 core, frontends: avatar refactoring:
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
374 """Set identity metadata for an entity
6cf4bd6972c2 core, frontends: avatar refactoring:
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
375
6cf4bd6972c2 core, frontends: avatar refactoring:
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
376 Registered callbacks will be tried in priority order (bigger to lower)
6cf4bd6972c2 core, frontends: avatar refactoring:
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
377 @param metadata_name(str): name of the metadata
6cf4bd6972c2 core, frontends: avatar refactoring:
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
378 must be one of self.metadata key
6cf4bd6972c2 core, frontends: avatar refactoring:
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
379 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
380 @param data(object): value to set
6cf4bd6972c2 core, frontends: avatar refactoring:
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
381 @param entity(jid.JID, None): entity for which avatar is requested
6cf4bd6972c2 core, frontends: avatar refactoring:
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
382 None to use profile's jid
6cf4bd6972c2 core, frontends: avatar refactoring:
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
383 """
6cf4bd6972c2 core, frontends: avatar refactoring:
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
384 entity = self.getIdentityJid(client, entity)
6cf4bd6972c2 core, frontends: avatar refactoring:
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
385 metadata = self.metadata[metadata_name]
6cf4bd6972c2 core, frontends: avatar refactoring:
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
386 data_filter = metadata.get("set_data_filter")
6cf4bd6972c2 core, frontends: avatar refactoring:
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
387 if data_filter is not None:
6cf4bd6972c2 core, frontends: avatar refactoring:
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
388 data = await utils.asDeferred(data_filter, client, entity, data)
6cf4bd6972c2 core, frontends: avatar refactoring:
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
389 self.checkType(metadata_name, data)
6cf4bd6972c2 core, frontends: avatar refactoring:
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
390
6cf4bd6972c2 core, frontends: avatar refactoring:
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
391 try:
6cf4bd6972c2 core, frontends: avatar refactoring:
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
392 callbacks = metadata['callbacks']
6cf4bd6972c2 core, frontends: avatar refactoring:
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
393 except KeyError:
6cf4bd6972c2 core, frontends: avatar refactoring:
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
394 log.warning(_("No callback registered for {metadata_name}")
6cf4bd6972c2 core, frontends: avatar refactoring:
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
395 .format(metadata_name=metadata_name))
6cf4bd6972c2 core, frontends: avatar refactoring:
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
396 return exceptions.FeatureNotFound(f"Can't set {metadata_name} for {entity}")
6cf4bd6972c2 core, frontends: avatar refactoring:
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
397
6cf4bd6972c2 core, frontends: avatar refactoring:
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
398 for callback in callbacks:
6cf4bd6972c2 core, frontends: avatar refactoring:
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
399 try:
6cf4bd6972c2 core, frontends: avatar refactoring:
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
400 await defer.ensureDeferred(callback.set(client, data, entity))
6cf4bd6972c2 core, frontends: avatar refactoring:
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
401 except exceptions.CancelError:
6cf4bd6972c2 core, frontends: avatar refactoring:
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
402 continue
6cf4bd6972c2 core, frontends: avatar refactoring:
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
403 except Exception as e:
6cf4bd6972c2 core, frontends: avatar refactoring:
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
404 log.warning(
6cf4bd6972c2 core, frontends: avatar refactoring:
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
405 _("Error while trying to set {metadata_name} with {callback}: {e}")
6cf4bd6972c2 core, frontends: avatar refactoring:
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
406 .format(callback=callback.set, metadata_name=metadata_name, e=e))
6cf4bd6972c2 core, frontends: avatar refactoring:
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
407 else:
6cf4bd6972c2 core, frontends: avatar refactoring:
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
408 break
6cf4bd6972c2 core, frontends: avatar refactoring:
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
409 else:
6cf4bd6972c2 core, frontends: avatar refactoring:
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
410 raise exceptions.FeatureNotFound(f"Can't set {metadata_name} for {entity}")
6cf4bd6972c2 core, frontends: avatar refactoring:
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
411
6cf4bd6972c2 core, frontends: avatar refactoring:
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
412 post_treatment = metadata.get("set_post_treatment")
6cf4bd6972c2 core, frontends: avatar refactoring:
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
413 if post_treatment is not None:
6cf4bd6972c2 core, frontends: avatar refactoring:
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
414 await utils.asDeferred(post_treatment, client, entity, data)
6cf4bd6972c2 core, frontends: avatar refactoring:
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
415
3277
cf07641b764d plugin identity: fixed infinite loop on nicknames update
Goffi <goffi@goffi.org>
parents: 3262
diff changeset
416 async def update(
3817
998c5318230f plugin identity: make the plugin compatible with component + description:
Goffi <goffi@goffi.org>
parents: 3816
diff changeset
417 self,
998c5318230f plugin identity: make the plugin compatible with component + description:
Goffi <goffi@goffi.org>
parents: 3816
diff changeset
418 client: SatXMPPEntity,
998c5318230f plugin identity: make the plugin compatible with component + description:
Goffi <goffi@goffi.org>
parents: 3816
diff changeset
419 origin: str,
998c5318230f plugin identity: make the plugin compatible with component + description:
Goffi <goffi@goffi.org>
parents: 3816
diff changeset
420 metadata_name: str,
998c5318230f plugin identity: make the plugin compatible with component + description:
Goffi <goffi@goffi.org>
parents: 3816
diff changeset
421 data: Any,
998c5318230f plugin identity: make the plugin compatible with component + description:
Goffi <goffi@goffi.org>
parents: 3816
diff changeset
422 entity: Optional[jid.JID]
998c5318230f plugin identity: make the plugin compatible with component + description:
Goffi <goffi@goffi.org>
parents: 3816
diff changeset
423 ):
3254
6cf4bd6972c2 core, frontends: avatar refactoring:
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
424 """Update a metadata in cache
6cf4bd6972c2 core, frontends: avatar refactoring:
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
425
6cf4bd6972c2 core, frontends: avatar refactoring:
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
426 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
427 @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
428 """
6cf4bd6972c2 core, frontends: avatar refactoring:
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
429 entity = self.getIdentityJid(client, entity)
3277
cf07641b764d plugin identity: fixed infinite loop on nicknames update
Goffi <goffi@goffi.org>
parents: 3262
diff changeset
430 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
431 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
432 return
3254
6cf4bd6972c2 core, frontends: avatar refactoring:
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
433 metadata = self.metadata[metadata_name]
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 try:
6cf4bd6972c2 core, frontends: avatar refactoring:
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
436 cached_data = self.host.memory.getEntityDatum(
6cf4bd6972c2 core, frontends: avatar refactoring:
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
437 client, entity, metadata_name)
6cf4bd6972c2 core, frontends: avatar refactoring:
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
438 except (KeyError, exceptions.UnknownEntityError):
6cf4bd6972c2 core, frontends: avatar refactoring:
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
439 # metadata is not cached, we do the update
6cf4bd6972c2 core, frontends: avatar refactoring:
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
440 pass
6cf4bd6972c2 core, frontends: avatar refactoring:
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
441 else:
6cf4bd6972c2 core, frontends: avatar refactoring:
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
442 # 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
443 try:
6cf4bd6972c2 core, frontends: avatar refactoring:
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
444 update_is_new_data = metadata["update_is_new_data"]
6cf4bd6972c2 core, frontends: avatar refactoring:
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
445 except KeyError:
6cf4bd6972c2 core, frontends: avatar refactoring:
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
446 update_is_new_data = self.defaultUpdateIsNewData
6cf4bd6972c2 core, frontends: avatar refactoring:
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
447
3257
704dada41df0 plugin identity: fixed update with `get_all` + better new data detection for avatar:
Goffi <goffi@goffi.org>
parents: 3254
diff changeset
448 if data is None:
3254
6cf4bd6972c2 core, frontends: avatar refactoring:
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
449 if cached_data is None:
6cf4bd6972c2 core, frontends: avatar refactoring:
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
450 log.debug(
6cf4bd6972c2 core, frontends: avatar refactoring:
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
451 f"{metadata_name} for {entity} is already disabled, nothing to "
6cf4bd6972c2 core, frontends: avatar refactoring:
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
452 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
453 return
3262
aa71f1d40300 plugin identity: fixed update when cached data is None
Goffi <goffi@goffi.org>
parents: 3257
diff changeset
454 elif cached_data is None:
aa71f1d40300 plugin identity: fixed update when cached data is None
Goffi <goffi@goffi.org>
parents: 3257
diff changeset
455 pass
3257
704dada41df0 plugin identity: fixed update with `get_all` + better new data detection for avatar:
Goffi <goffi@goffi.org>
parents: 3254
diff changeset
456 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
457 log.debug(
704dada41df0 plugin identity: fixed update with `get_all` + better new data detection for avatar:
Goffi <goffi@goffi.org>
parents: 3254
diff changeset
458 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
459 f"do")
3254
6cf4bd6972c2 core, frontends: avatar refactoring:
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
460 return
6cf4bd6972c2 core, frontends: avatar refactoring:
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
461
6cf4bd6972c2 core, frontends: avatar refactoring:
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
462 # we can't use the cache, so we do the update
6cf4bd6972c2 core, frontends: avatar refactoring:
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
463
6cf4bd6972c2 core, frontends: avatar refactoring:
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
464 log.debug(f"updating {metadata_name} for {entity}")
6cf4bd6972c2 core, frontends: avatar refactoring:
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
465
6cf4bd6972c2 core, frontends: avatar refactoring:
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
466 if metadata.get('get_all', False):
6cf4bd6972c2 core, frontends: avatar refactoring:
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
467 # 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
468 # 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
469 try:
704dada41df0 plugin identity: fixed update with `get_all` + better new data detection for avatar:
Goffi <goffi@goffi.org>
parents: 3254
diff changeset
470 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
471 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
472 pass
3254
6cf4bd6972c2 core, frontends: avatar refactoring:
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
473 # 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
474 # 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
475 # get callbacks)
cf07641b764d plugin identity: fixed infinite loop on nicknames update
Goffi <goffi@goffi.org>
parents: 3262
diff changeset
476 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
477 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
478 client._identity_update_lock.remove((entity, metadata_name))
3254
6cf4bd6972c2 core, frontends: avatar refactoring:
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
479 return
6cf4bd6972c2 core, frontends: avatar refactoring:
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
480
6cf4bd6972c2 core, frontends: avatar refactoring:
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
481 if data is not None:
6cf4bd6972c2 core, frontends: avatar refactoring:
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
482 data_filter = metadata['update_data_filter']
6cf4bd6972c2 core, frontends: avatar refactoring:
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
483 if data_filter is not None:
6cf4bd6972c2 core, frontends: avatar refactoring:
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
484 data = await utils.asDeferred(data_filter, client, entity, data)
6cf4bd6972c2 core, frontends: avatar refactoring:
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
485 self.checkType(metadata_name, data)
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 self.host.memory.updateEntityData(client, entity, metadata_name, data)
6cf4bd6972c2 core, frontends: avatar refactoring:
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
488
6cf4bd6972c2 core, frontends: avatar refactoring:
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
489 if metadata.get('store', False):
6cf4bd6972c2 core, frontends: avatar refactoring:
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
490 key = f"{entity}\n{metadata_name}"
6cf4bd6972c2 core, frontends: avatar refactoring:
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
491 await client._identity_storage.aset(key, data)
6cf4bd6972c2 core, frontends: avatar refactoring:
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
492
6cf4bd6972c2 core, frontends: avatar refactoring:
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
493 def defaultUpdateIsNewData(self, client, entity, cached_data, new_data):
6cf4bd6972c2 core, frontends: avatar refactoring:
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
494 return new_data != cached_data
6cf4bd6972c2 core, frontends: avatar refactoring:
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
495
6cf4bd6972c2 core, frontends: avatar refactoring:
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
496 def _getAvatar(self, entity, use_cache, profile):
2253
db468f24b9fc plugin identity: plugin identity first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
497 client = self.host.getClient(profile)
3254
6cf4bd6972c2 core, frontends: avatar refactoring:
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
498 entity = jid.JID(entity) if entity else None
6cf4bd6972c2 core, frontends: avatar refactoring:
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
499 d = defer.ensureDeferred(self.get(client, "avatar", entity, use_cache))
6cf4bd6972c2 core, frontends: avatar refactoring:
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
500 d.addCallback(lambda data: data_format.serialise(data))
6cf4bd6972c2 core, frontends: avatar refactoring:
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
501 return d
6cf4bd6972c2 core, frontends: avatar refactoring:
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
502
6cf4bd6972c2 core, frontends: avatar refactoring:
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
503 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
504 client = self.host.getClient(profile_key)
6cf4bd6972c2 core, frontends: avatar refactoring:
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
505 entity = jid.JID(entity) if entity else None
6cf4bd6972c2 core, frontends: avatar refactoring:
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
506 return defer.ensureDeferred(
6cf4bd6972c2 core, frontends: avatar refactoring:
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
507 self.set(client, "avatar", file_path, entity))
6cf4bd6972c2 core, frontends: avatar refactoring:
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
508
3816
213e83a4ed10 plugin identity, XEP-0054: move avatar resizing and caching method to identity plugin:
Goffi <goffi@goffi.org>
parents: 3541
diff changeset
509 def _blockingCacheAvatar(
213e83a4ed10 plugin identity, XEP-0054: move avatar resizing and caching method to identity plugin:
Goffi <goffi@goffi.org>
parents: 3541
diff changeset
510 self,
213e83a4ed10 plugin identity, XEP-0054: move avatar resizing and caching method to identity plugin:
Goffi <goffi@goffi.org>
parents: 3541
diff changeset
511 source: str,
213e83a4ed10 plugin identity, XEP-0054: move avatar resizing and caching method to identity plugin:
Goffi <goffi@goffi.org>
parents: 3541
diff changeset
512 avatar_data: dict[str, Any]
213e83a4ed10 plugin identity, XEP-0054: move avatar resizing and caching method to identity plugin:
Goffi <goffi@goffi.org>
parents: 3541
diff changeset
513 ):
213e83a4ed10 plugin identity, XEP-0054: move avatar resizing and caching method to identity plugin:
Goffi <goffi@goffi.org>
parents: 3541
diff changeset
514 """This method is executed in a separated thread"""
213e83a4ed10 plugin identity, XEP-0054: move avatar resizing and caching method to identity plugin:
Goffi <goffi@goffi.org>
parents: 3541
diff changeset
515 if avatar_data["media_type"] == "image/svg+xml":
213e83a4ed10 plugin identity, XEP-0054: move avatar resizing and caching method to identity plugin:
Goffi <goffi@goffi.org>
parents: 3541
diff changeset
516 # for vector image, we save directly
213e83a4ed10 plugin identity, XEP-0054: move avatar resizing and caching method to identity plugin:
Goffi <goffi@goffi.org>
parents: 3541
diff changeset
517 img_buf = open(avatar_data["path"], "rb")
213e83a4ed10 plugin identity, XEP-0054: move avatar resizing and caching method to identity plugin:
Goffi <goffi@goffi.org>
parents: 3541
diff changeset
518 else:
213e83a4ed10 plugin identity, XEP-0054: move avatar resizing and caching method to identity plugin:
Goffi <goffi@goffi.org>
parents: 3541
diff changeset
519 # for bitmap image, we check size and resize if necessary
213e83a4ed10 plugin identity, XEP-0054: move avatar resizing and caching method to identity plugin:
Goffi <goffi@goffi.org>
parents: 3541
diff changeset
520 try:
213e83a4ed10 plugin identity, XEP-0054: move avatar resizing and caching method to identity plugin:
Goffi <goffi@goffi.org>
parents: 3541
diff changeset
521 img = Image.open(avatar_data["path"])
213e83a4ed10 plugin identity, XEP-0054: move avatar resizing and caching method to identity plugin:
Goffi <goffi@goffi.org>
parents: 3541
diff changeset
522 except IOError as e:
213e83a4ed10 plugin identity, XEP-0054: move avatar resizing and caching method to identity plugin:
Goffi <goffi@goffi.org>
parents: 3541
diff changeset
523 raise exceptions.DataError(f"Can't open image: {e}")
213e83a4ed10 plugin identity, XEP-0054: move avatar resizing and caching method to identity plugin:
Goffi <goffi@goffi.org>
parents: 3541
diff changeset
524
213e83a4ed10 plugin identity, XEP-0054: move avatar resizing and caching method to identity plugin:
Goffi <goffi@goffi.org>
parents: 3541
diff changeset
525 if img.size != AVATAR_DIM:
213e83a4ed10 plugin identity, XEP-0054: move avatar resizing and caching method to identity plugin:
Goffi <goffi@goffi.org>
parents: 3541
diff changeset
526 img.thumbnail(AVATAR_DIM)
213e83a4ed10 plugin identity, XEP-0054: move avatar resizing and caching method to identity plugin:
Goffi <goffi@goffi.org>
parents: 3541
diff changeset
527 if img.size[0] != img.size[1]: # we need to crop first
213e83a4ed10 plugin identity, XEP-0054: move avatar resizing and caching method to identity plugin:
Goffi <goffi@goffi.org>
parents: 3541
diff changeset
528 left, upper = (0, 0)
213e83a4ed10 plugin identity, XEP-0054: move avatar resizing and caching method to identity plugin:
Goffi <goffi@goffi.org>
parents: 3541
diff changeset
529 right, lower = img.size
213e83a4ed10 plugin identity, XEP-0054: move avatar resizing and caching method to identity plugin:
Goffi <goffi@goffi.org>
parents: 3541
diff changeset
530 offset = abs(right - lower) / 2
213e83a4ed10 plugin identity, XEP-0054: move avatar resizing and caching method to identity plugin:
Goffi <goffi@goffi.org>
parents: 3541
diff changeset
531 if right == min(img.size):
213e83a4ed10 plugin identity, XEP-0054: move avatar resizing and caching method to identity plugin:
Goffi <goffi@goffi.org>
parents: 3541
diff changeset
532 upper += offset
213e83a4ed10 plugin identity, XEP-0054: move avatar resizing and caching method to identity plugin:
Goffi <goffi@goffi.org>
parents: 3541
diff changeset
533 lower -= offset
213e83a4ed10 plugin identity, XEP-0054: move avatar resizing and caching method to identity plugin:
Goffi <goffi@goffi.org>
parents: 3541
diff changeset
534 else:
213e83a4ed10 plugin identity, XEP-0054: move avatar resizing and caching method to identity plugin:
Goffi <goffi@goffi.org>
parents: 3541
diff changeset
535 left += offset
213e83a4ed10 plugin identity, XEP-0054: move avatar resizing and caching method to identity plugin:
Goffi <goffi@goffi.org>
parents: 3541
diff changeset
536 right -= offset
213e83a4ed10 plugin identity, XEP-0054: move avatar resizing and caching method to identity plugin:
Goffi <goffi@goffi.org>
parents: 3541
diff changeset
537 img = img.crop((left, upper, right, lower))
213e83a4ed10 plugin identity, XEP-0054: move avatar resizing and caching method to identity plugin:
Goffi <goffi@goffi.org>
parents: 3541
diff changeset
538 img_buf = io.BytesIO()
213e83a4ed10 plugin identity, XEP-0054: move avatar resizing and caching method to identity plugin:
Goffi <goffi@goffi.org>
parents: 3541
diff changeset
539 # PNG is well supported among clients, so we convert to this format
213e83a4ed10 plugin identity, XEP-0054: move avatar resizing and caching method to identity plugin:
Goffi <goffi@goffi.org>
parents: 3541
diff changeset
540 img.save(img_buf, "PNG")
213e83a4ed10 plugin identity, XEP-0054: move avatar resizing and caching method to identity plugin:
Goffi <goffi@goffi.org>
parents: 3541
diff changeset
541 img_buf.seek(0)
213e83a4ed10 plugin identity, XEP-0054: move avatar resizing and caching method to identity plugin:
Goffi <goffi@goffi.org>
parents: 3541
diff changeset
542 avatar_data["media_type"] = "image/png"
213e83a4ed10 plugin identity, XEP-0054: move avatar resizing and caching method to identity plugin:
Goffi <goffi@goffi.org>
parents: 3541
diff changeset
543
213e83a4ed10 plugin identity, XEP-0054: move avatar resizing and caching method to identity plugin:
Goffi <goffi@goffi.org>
parents: 3541
diff changeset
544 media_type = avatar_data["media_type"]
213e83a4ed10 plugin identity, XEP-0054: move avatar resizing and caching method to identity plugin:
Goffi <goffi@goffi.org>
parents: 3541
diff changeset
545 avatar_data["base64"] = image_b64 = b64encode(img_buf.read()).decode()
213e83a4ed10 plugin identity, XEP-0054: move avatar resizing and caching method to identity plugin:
Goffi <goffi@goffi.org>
parents: 3541
diff changeset
546 img_buf.seek(0)
213e83a4ed10 plugin identity, XEP-0054: move avatar resizing and caching method to identity plugin:
Goffi <goffi@goffi.org>
parents: 3541
diff changeset
547 image_hash = hashlib.sha1(img_buf.read()).hexdigest()
213e83a4ed10 plugin identity, XEP-0054: move avatar resizing and caching method to identity plugin:
Goffi <goffi@goffi.org>
parents: 3541
diff changeset
548 img_buf.seek(0)
213e83a4ed10 plugin identity, XEP-0054: move avatar resizing and caching method to identity plugin:
Goffi <goffi@goffi.org>
parents: 3541
diff changeset
549 with self.host.common_cache.cacheData(
213e83a4ed10 plugin identity, XEP-0054: move avatar resizing and caching method to identity plugin:
Goffi <goffi@goffi.org>
parents: 3541
diff changeset
550 source, image_hash, media_type
213e83a4ed10 plugin identity, XEP-0054: move avatar resizing and caching method to identity plugin:
Goffi <goffi@goffi.org>
parents: 3541
diff changeset
551 ) as f:
213e83a4ed10 plugin identity, XEP-0054: move avatar resizing and caching method to identity plugin:
Goffi <goffi@goffi.org>
parents: 3541
diff changeset
552 f.write(img_buf.read())
213e83a4ed10 plugin identity, XEP-0054: move avatar resizing and caching method to identity plugin:
Goffi <goffi@goffi.org>
parents: 3541
diff changeset
553 avatar_data['path'] = Path(f.name)
213e83a4ed10 plugin identity, XEP-0054: move avatar resizing and caching method to identity plugin:
Goffi <goffi@goffi.org>
parents: 3541
diff changeset
554 avatar_data['filename'] = avatar_data['path'].name
213e83a4ed10 plugin identity, XEP-0054: move avatar resizing and caching method to identity plugin:
Goffi <goffi@goffi.org>
parents: 3541
diff changeset
555 avatar_data['cache_uid'] = image_hash
213e83a4ed10 plugin identity, XEP-0054: move avatar resizing and caching method to identity plugin:
Goffi <goffi@goffi.org>
parents: 3541
diff changeset
556
213e83a4ed10 plugin identity, XEP-0054: move avatar resizing and caching method to identity plugin:
Goffi <goffi@goffi.org>
parents: 3541
diff changeset
557 async def cacheAvatar(self, source: str, avatar_data: Dict[str, Any]) -> None:
213e83a4ed10 plugin identity, XEP-0054: move avatar resizing and caching method to identity plugin:
Goffi <goffi@goffi.org>
parents: 3541
diff changeset
558 """Resize if necessary and cache avatar
213e83a4ed10 plugin identity, XEP-0054: move avatar resizing and caching method to identity plugin:
Goffi <goffi@goffi.org>
parents: 3541
diff changeset
559
213e83a4ed10 plugin identity, XEP-0054: move avatar resizing and caching method to identity plugin:
Goffi <goffi@goffi.org>
parents: 3541
diff changeset
560 @param source: source importing the avatar (usually it is plugin's import name),
213e83a4ed10 plugin identity, XEP-0054: move avatar resizing and caching method to identity plugin:
Goffi <goffi@goffi.org>
parents: 3541
diff changeset
561 will be used in cache metadata
213e83a4ed10 plugin identity, XEP-0054: move avatar resizing and caching method to identity plugin:
Goffi <goffi@goffi.org>
parents: 3541
diff changeset
562 @param avatar_data: avatar metadata as build by [avatarSetDataFilter]
213e83a4ed10 plugin identity, XEP-0054: move avatar resizing and caching method to identity plugin:
Goffi <goffi@goffi.org>
parents: 3541
diff changeset
563 will be updated with following keys:
213e83a4ed10 plugin identity, XEP-0054: move avatar resizing and caching method to identity plugin:
Goffi <goffi@goffi.org>
parents: 3541
diff changeset
564 path: updated path using cached file
213e83a4ed10 plugin identity, XEP-0054: move avatar resizing and caching method to identity plugin:
Goffi <goffi@goffi.org>
parents: 3541
diff changeset
565 filename: updated filename using cached file
213e83a4ed10 plugin identity, XEP-0054: move avatar resizing and caching method to identity plugin:
Goffi <goffi@goffi.org>
parents: 3541
diff changeset
566 base64: resized and base64 encoded avatar
213e83a4ed10 plugin identity, XEP-0054: move avatar resizing and caching method to identity plugin:
Goffi <goffi@goffi.org>
parents: 3541
diff changeset
567 cache_uid: SHA1 hash used as cache unique ID
213e83a4ed10 plugin identity, XEP-0054: move avatar resizing and caching method to identity plugin:
Goffi <goffi@goffi.org>
parents: 3541
diff changeset
568 """
213e83a4ed10 plugin identity, XEP-0054: move avatar resizing and caching method to identity plugin:
Goffi <goffi@goffi.org>
parents: 3541
diff changeset
569 await threads.deferToThread(self._blockingCacheAvatar, source, avatar_data)
213e83a4ed10 plugin identity, XEP-0054: move avatar resizing and caching method to identity plugin:
Goffi <goffi@goffi.org>
parents: 3541
diff changeset
570
3254
6cf4bd6972c2 core, frontends: avatar refactoring:
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
571 async def avatarSetDataFilter(self, client, entity, file_path):
6cf4bd6972c2 core, frontends: avatar refactoring:
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
572 """Convert avatar file path to dict data"""
6cf4bd6972c2 core, frontends: avatar refactoring:
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
573 file_path = Path(file_path)
6cf4bd6972c2 core, frontends: avatar refactoring:
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
574 if not file_path.is_file():
6cf4bd6972c2 core, frontends: avatar refactoring:
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
575 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
576 avatar_data = {
6cf4bd6972c2 core, frontends: avatar refactoring:
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
577 'path': file_path,
3326
9e1ba1e1179f plugin identity: added "filename" metadata for avatar
Goffi <goffi@goffi.org>
parents: 3278
diff changeset
578 'filename': file_path.name,
3254
6cf4bd6972c2 core, frontends: avatar refactoring:
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
579 'media_type': image.guess_type(file_path),
6cf4bd6972c2 core, frontends: avatar refactoring:
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
580 }
6cf4bd6972c2 core, frontends: avatar refactoring:
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
581 media_type = avatar_data['media_type']
6cf4bd6972c2 core, frontends: avatar refactoring:
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
582 if media_type is None:
6cf4bd6972c2 core, frontends: avatar refactoring:
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
583 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
584 if not media_type.startswith('image/'):
6cf4bd6972c2 core, frontends: avatar refactoring:
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
585 raise ValueError(f"File at {file_path} doesn't appear to be an image")
3816
213e83a4ed10 plugin identity, XEP-0054: move avatar resizing and caching method to identity plugin:
Goffi <goffi@goffi.org>
parents: 3541
diff changeset
586 await self.cacheAvatar(IMPORT_NAME, avatar_data)
3254
6cf4bd6972c2 core, frontends: avatar refactoring:
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
587 return avatar_data
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 async def avatarSetPostTreatment(self, client, entity, avatar_data):
6cf4bd6972c2 core, frontends: avatar refactoring:
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
590 """Update our own avatar"""
3278
27d4b71e264a plugin identity: fixed update in avatarSetPostTreatment
Goffi <goffi@goffi.org>
parents: 3277
diff changeset
591 await self.update(client, IMPORT_NAME, "avatar", avatar_data, entity)
3254
6cf4bd6972c2 core, frontends: avatar refactoring:
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
592
3816
213e83a4ed10 plugin identity, XEP-0054: move avatar resizing and caching method to identity plugin:
Goffi <goffi@goffi.org>
parents: 3541
diff changeset
593 def avatarBuildMetadata(
213e83a4ed10 plugin identity, XEP-0054: move avatar resizing and caching method to identity plugin:
Goffi <goffi@goffi.org>
parents: 3541
diff changeset
594 self,
213e83a4ed10 plugin identity, XEP-0054: move avatar resizing and caching method to identity plugin:
Goffi <goffi@goffi.org>
parents: 3541
diff changeset
595 path: Path,
213e83a4ed10 plugin identity, XEP-0054: move avatar resizing and caching method to identity plugin:
Goffi <goffi@goffi.org>
parents: 3541
diff changeset
596 media_type: Optional[str] = None,
213e83a4ed10 plugin identity, XEP-0054: move avatar resizing and caching method to identity plugin:
Goffi <goffi@goffi.org>
parents: 3541
diff changeset
597 cache_uid: Optional[str] = None
213e83a4ed10 plugin identity, XEP-0054: move avatar resizing and caching method to identity plugin:
Goffi <goffi@goffi.org>
parents: 3541
diff changeset
598 ) -> Optional[Dict[str, Union[str, Path, None]]]:
3254
6cf4bd6972c2 core, frontends: avatar refactoring:
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
599 """Helper method to generate avatar metadata
6cf4bd6972c2 core, frontends: avatar refactoring:
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
600
6cf4bd6972c2 core, frontends: avatar refactoring:
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
601 @param path(str, Path, None): path to avatar file
6cf4bd6972c2 core, frontends: avatar refactoring:
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
602 avatar file must be in cache
6cf4bd6972c2 core, frontends: avatar refactoring:
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
603 None if avatar is explicitely not set
6cf4bd6972c2 core, frontends: avatar refactoring:
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
604 @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
605 @param cache_uid(str, None): UID of avatar in cache
6cf4bd6972c2 core, frontends: avatar refactoring:
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
606 @return (dict, None): avatar metadata
6cf4bd6972c2 core, frontends: avatar refactoring:
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
607 None if avatar is not set
6cf4bd6972c2 core, frontends: avatar refactoring:
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
608 """
6cf4bd6972c2 core, frontends: avatar refactoring:
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
609 if path is None:
6cf4bd6972c2 core, frontends: avatar refactoring:
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
610 return None
6cf4bd6972c2 core, frontends: avatar refactoring:
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
611 else:
6cf4bd6972c2 core, frontends: avatar refactoring:
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
612 if cache_uid is None:
6cf4bd6972c2 core, frontends: avatar refactoring:
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
613 raise ValueError("cache_uid must be set if path is set")
6cf4bd6972c2 core, frontends: avatar refactoring:
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
614 path = Path(path)
6cf4bd6972c2 core, frontends: avatar refactoring:
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
615 if media_type is None:
6cf4bd6972c2 core, frontends: avatar refactoring:
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
616 media_type = image.guess_type(path)
6cf4bd6972c2 core, frontends: avatar refactoring:
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
617
6cf4bd6972c2 core, frontends: avatar refactoring:
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
618 return {
6cf4bd6972c2 core, frontends: avatar refactoring:
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
619 "path": path,
3326
9e1ba1e1179f plugin identity: added "filename" metadata for avatar
Goffi <goffi@goffi.org>
parents: 3278
diff changeset
620 "filename": path.name,
3254
6cf4bd6972c2 core, frontends: avatar refactoring:
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
621 "media_type": media_type,
6cf4bd6972c2 core, frontends: avatar refactoring:
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
622 "cache_uid": cache_uid,
6cf4bd6972c2 core, frontends: avatar refactoring:
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
623 }
6cf4bd6972c2 core, frontends: avatar refactoring:
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
624
3257
704dada41df0 plugin identity: fixed update with `get_all` + better new data detection for avatar:
Goffi <goffi@goffi.org>
parents: 3254
diff changeset
625 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
626 return new_data['path'] != cached_data['path']
3254
6cf4bd6972c2 core, frontends: avatar refactoring:
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
627
6cf4bd6972c2 core, frontends: avatar refactoring:
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
628 async def avatarUpdateDataFilter(self, client, entity, data):
6cf4bd6972c2 core, frontends: avatar refactoring:
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
629 if not isinstance(data, dict):
6cf4bd6972c2 core, frontends: avatar refactoring:
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
630 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
631 mandatory_keys = {'path', 'filename', 'cache_uid'}
3254
6cf4bd6972c2 core, frontends: avatar refactoring:
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
632 if not data.keys() >= mandatory_keys:
6cf4bd6972c2 core, frontends: avatar refactoring:
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
633 raise ValueError(f"missing avatar data keys: {mandatory_keys - data.keys()}")
6cf4bd6972c2 core, frontends: avatar refactoring:
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
634 return data
6cf4bd6972c2 core, frontends: avatar refactoring:
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
635
6cf4bd6972c2 core, frontends: avatar refactoring:
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
636 async def nicknamesGetPostTreatment(self, client, entity, plugin_nicknames):
6cf4bd6972c2 core, frontends: avatar refactoring:
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
637 """Prepend nicknames from core locations + set default nickname
6cf4bd6972c2 core, frontends: avatar refactoring:
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
638
6cf4bd6972c2 core, frontends: avatar refactoring:
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
639 nicknames are checked from many locations, there is always at least
6cf4bd6972c2 core, frontends: avatar refactoring:
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
640 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
641 Nicknames are appended in this order:
6cf4bd6972c2 core, frontends: avatar refactoring:
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
642 - roster, plugins set nicknames
6cf4bd6972c2 core, frontends: avatar refactoring:
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
643 - 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
644 if there is no user part.
6cf4bd6972c2 core, frontends: avatar refactoring:
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
645 For MUC, room nick is always put first
6cf4bd6972c2 core, frontends: avatar refactoring:
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
646 """
6cf4bd6972c2 core, frontends: avatar refactoring:
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
647 nicknames = []
3817
998c5318230f plugin identity: make the plugin compatible with component + description:
Goffi <goffi@goffi.org>
parents: 3816
diff changeset
648
998c5318230f plugin identity: make the plugin compatible with component + description:
Goffi <goffi@goffi.org>
parents: 3816
diff changeset
649 # for MUC we add resource
3254
6cf4bd6972c2 core, frontends: avatar refactoring:
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
650 if entity.resource:
6cf4bd6972c2 core, frontends: avatar refactoring:
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
651 # 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
652 # occupant jid
6cf4bd6972c2 core, frontends: avatar refactoring:
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
653 nicknames.append(entity.resource)
6cf4bd6972c2 core, frontends: avatar refactoring:
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
654
3817
998c5318230f plugin identity: make the plugin compatible with component + description:
Goffi <goffi@goffi.org>
parents: 3816
diff changeset
655 # we first check roster (if we are not in a component)
998c5318230f plugin identity: make the plugin compatible with component + description:
Goffi <goffi@goffi.org>
parents: 3816
diff changeset
656 if not client.is_component:
998c5318230f plugin identity: make the plugin compatible with component + description:
Goffi <goffi@goffi.org>
parents: 3816
diff changeset
657 roster_item = client.roster.getItem(entity.userhostJID())
998c5318230f plugin identity: make the plugin compatible with component + description:
Goffi <goffi@goffi.org>
parents: 3816
diff changeset
658 if roster_item is not None and roster_item.name:
998c5318230f plugin identity: make the plugin compatible with component + description:
Goffi <goffi@goffi.org>
parents: 3816
diff changeset
659 # user set name has priority over entity set name
998c5318230f plugin identity: make the plugin compatible with component + description:
Goffi <goffi@goffi.org>
parents: 3816
diff changeset
660 nicknames.append(roster_item.name)
3254
6cf4bd6972c2 core, frontends: avatar refactoring:
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
661
6cf4bd6972c2 core, frontends: avatar refactoring:
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
662 nicknames.extend(plugin_nicknames)
6cf4bd6972c2 core, frontends: avatar refactoring:
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
663
6cf4bd6972c2 core, frontends: avatar refactoring:
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
664 if not nicknames:
6cf4bd6972c2 core, frontends: avatar refactoring:
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
665 if entity.user:
6cf4bd6972c2 core, frontends: avatar refactoring:
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
666 nicknames.append(entity.user.capitalize())
6cf4bd6972c2 core, frontends: avatar refactoring:
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
667 else:
6cf4bd6972c2 core, frontends: avatar refactoring:
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
668 nicknames.append(entity.userhost())
6cf4bd6972c2 core, frontends: avatar refactoring:
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
669
6cf4bd6972c2 core, frontends: avatar refactoring:
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
670 # we remove duplicates while preserving order with dict
6cf4bd6972c2 core, frontends: avatar refactoring:
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
671 return list(dict.fromkeys(nicknames))
6cf4bd6972c2 core, frontends: avatar refactoring:
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
672
6cf4bd6972c2 core, frontends: avatar refactoring:
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
673 def nicknamesUpdateIsNewData(self, client, entity, cached_data, new_nicknames):
6cf4bd6972c2 core, frontends: avatar refactoring:
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
674 return not set(new_nicknames).issubset(cached_data)
6cf4bd6972c2 core, frontends: avatar refactoring:
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
675
3817
998c5318230f plugin identity: make the plugin compatible with component + description:
Goffi <goffi@goffi.org>
parents: 3816
diff changeset
676 async def descriptionGetPostTreatment(
998c5318230f plugin identity: make the plugin compatible with component + description:
Goffi <goffi@goffi.org>
parents: 3816
diff changeset
677 self,
998c5318230f plugin identity: make the plugin compatible with component + description:
Goffi <goffi@goffi.org>
parents: 3816
diff changeset
678 client: SatXMPPEntity,
998c5318230f plugin identity: make the plugin compatible with component + description:
Goffi <goffi@goffi.org>
parents: 3816
diff changeset
679 entity: jid.JID,
998c5318230f plugin identity: make the plugin compatible with component + description:
Goffi <goffi@goffi.org>
parents: 3816
diff changeset
680 plugin_description: List[str]
998c5318230f plugin identity: make the plugin compatible with component + description:
Goffi <goffi@goffi.org>
parents: 3816
diff changeset
681 ) -> str:
998c5318230f plugin identity: make the plugin compatible with component + description:
Goffi <goffi@goffi.org>
parents: 3816
diff changeset
682 """Join all descriptions in a unique string"""
998c5318230f plugin identity: make the plugin compatible with component + description:
Goffi <goffi@goffi.org>
parents: 3816
diff changeset
683 return '\n'.join(plugin_description)
998c5318230f plugin identity: make the plugin compatible with component + description:
Goffi <goffi@goffi.org>
parents: 3816
diff changeset
684
3254
6cf4bd6972c2 core, frontends: avatar refactoring:
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
685 def _getIdentity(self, entity_s, metadata_filter, use_cache, profile):
6cf4bd6972c2 core, frontends: avatar refactoring:
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
686 entity = jid.JID(entity_s)
6cf4bd6972c2 core, frontends: avatar refactoring:
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
687 client = self.host.getClient(profile)
6cf4bd6972c2 core, frontends: avatar refactoring:
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
688 d = defer.ensureDeferred(
6cf4bd6972c2 core, frontends: avatar refactoring:
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
689 self.getIdentity(client, entity, metadata_filter, use_cache))
3338
203a491fcd86 plugin identity: new methods `identitiesGet` and `identitiesBaseGet`
Goffi <goffi@goffi.org>
parents: 3326
diff changeset
690 d.addCallback(data_format.serialise)
3254
6cf4bd6972c2 core, frontends: avatar refactoring:
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
691 return d
6cf4bd6972c2 core, frontends: avatar refactoring:
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
692
6cf4bd6972c2 core, frontends: avatar refactoring:
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
693 async def getIdentity(
3817
998c5318230f plugin identity: make the plugin compatible with component + description:
Goffi <goffi@goffi.org>
parents: 3816
diff changeset
694 self,
998c5318230f plugin identity: make the plugin compatible with component + description:
Goffi <goffi@goffi.org>
parents: 3816
diff changeset
695 client: SatXMPPEntity,
998c5318230f plugin identity: make the plugin compatible with component + description:
Goffi <goffi@goffi.org>
parents: 3816
diff changeset
696 entity: Optional[jid.JID] = None,
998c5318230f plugin identity: make the plugin compatible with component + description:
Goffi <goffi@goffi.org>
parents: 3816
diff changeset
697 metadata_filter: Optional[List[str]] = None,
998c5318230f plugin identity: make the plugin compatible with component + description:
Goffi <goffi@goffi.org>
parents: 3816
diff changeset
698 use_cache: bool = True
998c5318230f plugin identity: make the plugin compatible with component + description:
Goffi <goffi@goffi.org>
parents: 3816
diff changeset
699 ) -> Dict[str, Any]:
2253
db468f24b9fc plugin identity: plugin identity first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
700 """Retrieve identity of an entity
db468f24b9fc plugin identity: plugin identity first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
701
3817
998c5318230f plugin identity: make the plugin compatible with component + description:
Goffi <goffi@goffi.org>
parents: 3816
diff changeset
702 @param entity: entity to check
998c5318230f plugin identity: make the plugin compatible with component + description:
Goffi <goffi@goffi.org>
parents: 3816
diff changeset
703 @param metadata_filter: if not None or empty, only return
3254
6cf4bd6972c2 core, frontends: avatar refactoring:
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
704 metadata in this filter
3817
998c5318230f plugin identity: make the plugin compatible with component + description:
Goffi <goffi@goffi.org>
parents: 3816
diff changeset
705 @param use_cache: if False, cache won't be checked
3254
6cf4bd6972c2 core, frontends: avatar refactoring:
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
706 should be True most of time, to avoid useless network requests
3817
998c5318230f plugin identity: make the plugin compatible with component + description:
Goffi <goffi@goffi.org>
parents: 3816
diff changeset
707 @return: identity data
2253
db468f24b9fc plugin identity: plugin identity first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
708 """
db468f24b9fc plugin identity: plugin identity first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
709 id_data = {}
3254
6cf4bd6972c2 core, frontends: avatar refactoring:
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
710
6cf4bd6972c2 core, frontends: avatar refactoring:
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
711 if not metadata_filter:
6cf4bd6972c2 core, frontends: avatar refactoring:
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
712 metadata_names = self.metadata.keys()
2253
db468f24b9fc plugin identity: plugin identity first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
713 else:
3254
6cf4bd6972c2 core, frontends: avatar refactoring:
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
714 metadata_names = metadata_filter
2253
db468f24b9fc plugin identity: plugin identity first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
715
3254
6cf4bd6972c2 core, frontends: avatar refactoring:
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
716 for metadata_name in metadata_names:
6cf4bd6972c2 core, frontends: avatar refactoring:
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
717 id_data[metadata_name] = await self.get(
6cf4bd6972c2 core, frontends: avatar refactoring:
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
718 client, metadata_name, entity, use_cache)
6cf4bd6972c2 core, frontends: avatar refactoring:
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
719
6cf4bd6972c2 core, frontends: avatar refactoring:
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
720 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
721
3338
203a491fcd86 plugin identity: new methods `identitiesGet` and `identitiesBaseGet`
Goffi <goffi@goffi.org>
parents: 3326
diff changeset
722 def _getIdentities(self, entities_s, metadata_filter, profile):
203a491fcd86 plugin identity: new methods `identitiesGet` and `identitiesBaseGet`
Goffi <goffi@goffi.org>
parents: 3326
diff changeset
723 entities = [jid.JID(e) for e in entities_s]
203a491fcd86 plugin identity: new methods `identitiesGet` and `identitiesBaseGet`
Goffi <goffi@goffi.org>
parents: 3326
diff changeset
724 client = self.host.getClient(profile)
203a491fcd86 plugin identity: new methods `identitiesGet` and `identitiesBaseGet`
Goffi <goffi@goffi.org>
parents: 3326
diff changeset
725 d = defer.ensureDeferred(self.getIdentities(client, entities, metadata_filter))
203a491fcd86 plugin identity: new methods `identitiesGet` and `identitiesBaseGet`
Goffi <goffi@goffi.org>
parents: 3326
diff changeset
726 d.addCallback(lambda d: data_format.serialise({str(j):i for j, i in d.items()}))
203a491fcd86 plugin identity: new methods `identitiesGet` and `identitiesBaseGet`
Goffi <goffi@goffi.org>
parents: 3326
diff changeset
727 return d
203a491fcd86 plugin identity: new methods `identitiesGet` and `identitiesBaseGet`
Goffi <goffi@goffi.org>
parents: 3326
diff changeset
728
203a491fcd86 plugin identity: new methods `identitiesGet` and `identitiesBaseGet`
Goffi <goffi@goffi.org>
parents: 3326
diff changeset
729 async def getIdentities(
203a491fcd86 plugin identity: new methods `identitiesGet` and `identitiesBaseGet`
Goffi <goffi@goffi.org>
parents: 3326
diff changeset
730 self,
203a491fcd86 plugin identity: new methods `identitiesGet` and `identitiesBaseGet`
Goffi <goffi@goffi.org>
parents: 3326
diff changeset
731 client: SatXMPPEntity,
203a491fcd86 plugin identity: new methods `identitiesGet` and `identitiesBaseGet`
Goffi <goffi@goffi.org>
parents: 3326
diff changeset
732 entities: List[jid.JID],
203a491fcd86 plugin identity: new methods `identitiesGet` and `identitiesBaseGet`
Goffi <goffi@goffi.org>
parents: 3326
diff changeset
733 metadata_filter: Optional[List[str]] = None,
203a491fcd86 plugin identity: new methods `identitiesGet` and `identitiesBaseGet`
Goffi <goffi@goffi.org>
parents: 3326
diff changeset
734 ) -> dict:
203a491fcd86 plugin identity: new methods `identitiesGet` and `identitiesBaseGet`
Goffi <goffi@goffi.org>
parents: 3326
diff changeset
735 """Retrieve several identities at once
203a491fcd86 plugin identity: new methods `identitiesGet` and `identitiesBaseGet`
Goffi <goffi@goffi.org>
parents: 3326
diff changeset
736
203a491fcd86 plugin identity: new methods `identitiesGet` and `identitiesBaseGet`
Goffi <goffi@goffi.org>
parents: 3326
diff changeset
737 @param entities: entities from which identities must be retrieved
203a491fcd86 plugin identity: new methods `identitiesGet` and `identitiesBaseGet`
Goffi <goffi@goffi.org>
parents: 3326
diff changeset
738 @param metadata_filter: same as for [getIdentity]
203a491fcd86 plugin identity: new methods `identitiesGet` and `identitiesBaseGet`
Goffi <goffi@goffi.org>
parents: 3326
diff changeset
739 @return: identities metadata where key is jid
203a491fcd86 plugin identity: new methods `identitiesGet` and `identitiesBaseGet`
Goffi <goffi@goffi.org>
parents: 3326
diff changeset
740 if an error happens while retrieve a jid entity, it won't be present in the
203a491fcd86 plugin identity: new methods `identitiesGet` and `identitiesBaseGet`
Goffi <goffi@goffi.org>
parents: 3326
diff changeset
741 result (and a warning will be logged)
203a491fcd86 plugin identity: new methods `identitiesGet` and `identitiesBaseGet`
Goffi <goffi@goffi.org>
parents: 3326
diff changeset
742 """
203a491fcd86 plugin identity: new methods `identitiesGet` and `identitiesBaseGet`
Goffi <goffi@goffi.org>
parents: 3326
diff changeset
743 identities = {}
203a491fcd86 plugin identity: new methods `identitiesGet` and `identitiesBaseGet`
Goffi <goffi@goffi.org>
parents: 3326
diff changeset
744 get_identity_list = []
203a491fcd86 plugin identity: new methods `identitiesGet` and `identitiesBaseGet`
Goffi <goffi@goffi.org>
parents: 3326
diff changeset
745 for entity_jid in entities:
203a491fcd86 plugin identity: new methods `identitiesGet` and `identitiesBaseGet`
Goffi <goffi@goffi.org>
parents: 3326
diff changeset
746 get_identity_list.append(
203a491fcd86 plugin identity: new methods `identitiesGet` and `identitiesBaseGet`
Goffi <goffi@goffi.org>
parents: 3326
diff changeset
747 defer.ensureDeferred(
203a491fcd86 plugin identity: new methods `identitiesGet` and `identitiesBaseGet`
Goffi <goffi@goffi.org>
parents: 3326
diff changeset
748 self.getIdentity(
203a491fcd86 plugin identity: new methods `identitiesGet` and `identitiesBaseGet`
Goffi <goffi@goffi.org>
parents: 3326
diff changeset
749 client,
203a491fcd86 plugin identity: new methods `identitiesGet` and `identitiesBaseGet`
Goffi <goffi@goffi.org>
parents: 3326
diff changeset
750 entity=entity_jid,
203a491fcd86 plugin identity: new methods `identitiesGet` and `identitiesBaseGet`
Goffi <goffi@goffi.org>
parents: 3326
diff changeset
751 metadata_filter=metadata_filter,
203a491fcd86 plugin identity: new methods `identitiesGet` and `identitiesBaseGet`
Goffi <goffi@goffi.org>
parents: 3326
diff changeset
752 )
203a491fcd86 plugin identity: new methods `identitiesGet` and `identitiesBaseGet`
Goffi <goffi@goffi.org>
parents: 3326
diff changeset
753 )
203a491fcd86 plugin identity: new methods `identitiesGet` and `identitiesBaseGet`
Goffi <goffi@goffi.org>
parents: 3326
diff changeset
754 )
203a491fcd86 plugin identity: new methods `identitiesGet` and `identitiesBaseGet`
Goffi <goffi@goffi.org>
parents: 3326
diff changeset
755 identities_result = await defer.DeferredList(get_identity_list)
203a491fcd86 plugin identity: new methods `identitiesGet` and `identitiesBaseGet`
Goffi <goffi@goffi.org>
parents: 3326
diff changeset
756 for idx, (success, identity) in enumerate(identities_result):
203a491fcd86 plugin identity: new methods `identitiesGet` and `identitiesBaseGet`
Goffi <goffi@goffi.org>
parents: 3326
diff changeset
757 entity_jid = entities[idx]
203a491fcd86 plugin identity: new methods `identitiesGet` and `identitiesBaseGet`
Goffi <goffi@goffi.org>
parents: 3326
diff changeset
758 if not success:
203a491fcd86 plugin identity: new methods `identitiesGet` and `identitiesBaseGet`
Goffi <goffi@goffi.org>
parents: 3326
diff changeset
759 log.warning(f"Can't get identity for {entity_jid}")
203a491fcd86 plugin identity: new methods `identitiesGet` and `identitiesBaseGet`
Goffi <goffi@goffi.org>
parents: 3326
diff changeset
760 else:
203a491fcd86 plugin identity: new methods `identitiesGet` and `identitiesBaseGet`
Goffi <goffi@goffi.org>
parents: 3326
diff changeset
761 identities[entity_jid] = identity
203a491fcd86 plugin identity: new methods `identitiesGet` and `identitiesBaseGet`
Goffi <goffi@goffi.org>
parents: 3326
diff changeset
762 return identities
203a491fcd86 plugin identity: new methods `identitiesGet` and `identitiesBaseGet`
Goffi <goffi@goffi.org>
parents: 3326
diff changeset
763
203a491fcd86 plugin identity: new methods `identitiesGet` and `identitiesBaseGet`
Goffi <goffi@goffi.org>
parents: 3326
diff changeset
764 def _getBaseIdentities(self, profile_key):
203a491fcd86 plugin identity: new methods `identitiesGet` and `identitiesBaseGet`
Goffi <goffi@goffi.org>
parents: 3326
diff changeset
765 client = self.host.getClient(profile_key)
203a491fcd86 plugin identity: new methods `identitiesGet` and `identitiesBaseGet`
Goffi <goffi@goffi.org>
parents: 3326
diff changeset
766 d = defer.ensureDeferred(self.getBaseIdentities(client))
203a491fcd86 plugin identity: new methods `identitiesGet` and `identitiesBaseGet`
Goffi <goffi@goffi.org>
parents: 3326
diff changeset
767 d.addCallback(lambda d: data_format.serialise({str(j):i for j, i in d.items()}))
203a491fcd86 plugin identity: new methods `identitiesGet` and `identitiesBaseGet`
Goffi <goffi@goffi.org>
parents: 3326
diff changeset
768 return d
203a491fcd86 plugin identity: new methods `identitiesGet` and `identitiesBaseGet`
Goffi <goffi@goffi.org>
parents: 3326
diff changeset
769
203a491fcd86 plugin identity: new methods `identitiesGet` and `identitiesBaseGet`
Goffi <goffi@goffi.org>
parents: 3326
diff changeset
770 async def getBaseIdentities(
203a491fcd86 plugin identity: new methods `identitiesGet` and `identitiesBaseGet`
Goffi <goffi@goffi.org>
parents: 3326
diff changeset
771 self,
203a491fcd86 plugin identity: new methods `identitiesGet` and `identitiesBaseGet`
Goffi <goffi@goffi.org>
parents: 3326
diff changeset
772 client: SatXMPPEntity,
203a491fcd86 plugin identity: new methods `identitiesGet` and `identitiesBaseGet`
Goffi <goffi@goffi.org>
parents: 3326
diff changeset
773 ) -> dict:
203a491fcd86 plugin identity: new methods `identitiesGet` and `identitiesBaseGet`
Goffi <goffi@goffi.org>
parents: 3326
diff changeset
774 """Retrieve identities for entities in roster + own identity + invitations
203a491fcd86 plugin identity: new methods `identitiesGet` and `identitiesBaseGet`
Goffi <goffi@goffi.org>
parents: 3326
diff changeset
775
203a491fcd86 plugin identity: new methods `identitiesGet` and `identitiesBaseGet`
Goffi <goffi@goffi.org>
parents: 3326
diff changeset
776 @param with_guests: if True, get affiliations of people invited by email
203a491fcd86 plugin identity: new methods `identitiesGet` and `identitiesBaseGet`
Goffi <goffi@goffi.org>
parents: 3326
diff changeset
777
203a491fcd86 plugin identity: new methods `identitiesGet` and `identitiesBaseGet`
Goffi <goffi@goffi.org>
parents: 3326
diff changeset
778 """
3817
998c5318230f plugin identity: make the plugin compatible with component + description:
Goffi <goffi@goffi.org>
parents: 3816
diff changeset
779 if client.is_component:
998c5318230f plugin identity: make the plugin compatible with component + description:
Goffi <goffi@goffi.org>
parents: 3816
diff changeset
780 entities = [client.jid.userhostJID()]
998c5318230f plugin identity: make the plugin compatible with component + description:
Goffi <goffi@goffi.org>
parents: 3816
diff changeset
781 else:
998c5318230f plugin identity: make the plugin compatible with component + description:
Goffi <goffi@goffi.org>
parents: 3816
diff changeset
782 entities = client.roster.getJids() + [client.jid.userhostJID()]
3338
203a491fcd86 plugin identity: new methods `identitiesGet` and `identitiesBaseGet`
Goffi <goffi@goffi.org>
parents: 3326
diff changeset
783
203a491fcd86 plugin identity: new methods `identitiesGet` and `identitiesBaseGet`
Goffi <goffi@goffi.org>
parents: 3326
diff changeset
784 return await self.getIdentities(
203a491fcd86 plugin identity: new methods `identitiesGet` and `identitiesBaseGet`
Goffi <goffi@goffi.org>
parents: 3326
diff changeset
785 client,
203a491fcd86 plugin identity: new methods `identitiesGet` and `identitiesBaseGet`
Goffi <goffi@goffi.org>
parents: 3326
diff changeset
786 entities,
203a491fcd86 plugin identity: new methods `identitiesGet` and `identitiesBaseGet`
Goffi <goffi@goffi.org>
parents: 3326
diff changeset
787 ['avatar', 'nicknames']
203a491fcd86 plugin identity: new methods `identitiesGet` and `identitiesBaseGet`
Goffi <goffi@goffi.org>
parents: 3326
diff changeset
788 )
203a491fcd86 plugin identity: new methods `identitiesGet` and `identitiesBaseGet`
Goffi <goffi@goffi.org>
parents: 3326
diff changeset
789
3254
6cf4bd6972c2 core, frontends: avatar refactoring:
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
790 def _setIdentity(self, id_data_s, profile):
2253
db468f24b9fc plugin identity: plugin identity first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
791 client = self.host.getClient(profile)
3254
6cf4bd6972c2 core, frontends: avatar refactoring:
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
792 id_data = data_format.deserialise(id_data_s)
6cf4bd6972c2 core, frontends: avatar refactoring:
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
793 return defer.ensureDeferred(self.setIdentity(client, id_data))
2253
db468f24b9fc plugin identity: plugin identity first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
794
3254
6cf4bd6972c2 core, frontends: avatar refactoring:
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
795 async def setIdentity(self, client, id_data):
2253
db468f24b9fc plugin identity: plugin identity first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
796 """Update profile's identity
db468f24b9fc plugin identity: plugin identity first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
797
3817
998c5318230f plugin identity: make the plugin compatible with component + description:
Goffi <goffi@goffi.org>
parents: 3816
diff changeset
798 @param id_data(dict): data to update, key can be one of self.metadata keys
2253
db468f24b9fc plugin identity: plugin identity first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
799 """
3254
6cf4bd6972c2 core, frontends: avatar refactoring:
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
800 if not id_data.keys() <= self.metadata.keys():
6cf4bd6972c2 core, frontends: avatar refactoring:
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
801 raise ValueError(
6cf4bd6972c2 core, frontends: avatar refactoring:
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
802 f"Invalid metadata names: {id_data.keys() - self.metadata.keys()}")
6cf4bd6972c2 core, frontends: avatar refactoring:
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
803 for metadata_name, data in id_data.items():
6cf4bd6972c2 core, frontends: avatar refactoring:
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
804 try:
6cf4bd6972c2 core, frontends: avatar refactoring:
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
805 await self.set(client, metadata_name, data)
6cf4bd6972c2 core, frontends: avatar refactoring:
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
806 except Exception as e:
6cf4bd6972c2 core, frontends: avatar refactoring:
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
807 log.warning(
6cf4bd6972c2 core, frontends: avatar refactoring:
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
808 _("Can't set metadata {metadata_name!r}: {reason}")
6cf4bd6972c2 core, frontends: avatar refactoring:
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
809 .format(metadata_name=metadata_name, reason=e))