annotate libervia/backend/plugins/plugin_misc_identity.py @ 4240:79c8a70e1813

backend, frontend: prepare remote control: This is a series of changes necessary to prepare the implementation of remote control feature: - XEP-0166: add a `priority` attribute to `ApplicationData`: this is needed when several applications are working in a same session, to know which one must be handled first. Will be used to make Remote Control have precedence over Call content. - XEP-0166: `_call_plugins` is now async and is not used with `DeferredList` anymore: the benefit to have methods called in parallels is very low, and it cause a lot of trouble as we can't predict order. Methods are now called sequentially so workflow can be predicted. - XEP-0167: fix `senders` XMPP attribute <=> SDP mapping - XEP-0234: preflight acceptance key is now `pre-accepted` instead of `file-accepted`, so the same key can be used with other jingle applications. - XEP-0167, XEP-0343: move some method to XEP-0167 - XEP-0353: use new `priority` feature to call preflight methods of applications according to it. - frontend (webrtc): refactor the sources/sink handling with a more flexible mechanism based on Pydantic models. It is now possible to have has many Data Channel as necessary, to have them in addition to A/V streams, to specify manually GStreamer sources and sinks, etc. - frontend (webrtc): rework of the pipeline to reduce latency. - frontend: new `portal_desktop` method. Screenshare portal handling has been moved there, and RemoteDesktop portal has been added. - frontend (webrtc): fix `extract_ufrag_pwd` method. rel 436
author Goffi <goffi@goffi.org>
date Sat, 11 May 2024 13:52:41 +0200
parents 5f2d496c633f
children 0d7bb4df2343
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
4071
4b842c1fb686 refactoring: renamed `sat` package to `libervia.backend`
Goffi <goffi@goffi.org>
parents: 4037
diff changeset
28 from libervia.backend.core import exceptions
4b842c1fb686 refactoring: renamed `sat` package to `libervia.backend`
Goffi <goffi@goffi.org>
parents: 4037
diff changeset
29 from libervia.backend.core.constants import Const as C
4b842c1fb686 refactoring: renamed `sat` package to `libervia.backend`
Goffi <goffi@goffi.org>
parents: 4037
diff changeset
30 from libervia.backend.core.i18n import _
4b842c1fb686 refactoring: renamed `sat` package to `libervia.backend`
Goffi <goffi@goffi.org>
parents: 4037
diff changeset
31 from libervia.backend.core.log import getLogger
4b842c1fb686 refactoring: renamed `sat` package to `libervia.backend`
Goffi <goffi@goffi.org>
parents: 4037
diff changeset
32 from libervia.backend.core.xmpp import SatXMPPEntity
4b842c1fb686 refactoring: renamed `sat` package to `libervia.backend`
Goffi <goffi@goffi.org>
parents: 4037
diff changeset
33 from libervia.backend.memory import persistent
4b842c1fb686 refactoring: renamed `sat` package to `libervia.backend`
Goffi <goffi@goffi.org>
parents: 4037
diff changeset
34 from libervia.backend.tools import image
4b842c1fb686 refactoring: renamed `sat` package to `libervia.backend`
Goffi <goffi@goffi.org>
parents: 4037
diff changeset
35 from libervia.backend.tools import utils
4b842c1fb686 refactoring: renamed `sat` package to `libervia.backend`
Goffi <goffi@goffi.org>
parents: 4037
diff changeset
36 from libervia.backend.tools.common import data_format
3254
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)
4037
524856bd7b19 massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents: 3817
diff changeset
80 "set_data_filter": self.avatar_set_data_filter,
3254
6cf4bd6972c2 core, frontends: avatar refactoring:
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
81 # update profile avatar, so all frontends are aware
4037
524856bd7b19 massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents: 3817
diff changeset
82 "set_post_treatment": self.avatar_set_post_treatment,
524856bd7b19 massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents: 3817
diff changeset
83 "update_is_new_data": self.avatar_update_is_new_data,
524856bd7b19 massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents: 3817
diff changeset
84 "update_data_filter": self.avatar_update_data_filter,
3254
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,
4212
5f2d496c633f core: get rid of `pickle`:
Goffi <goffi@goffi.org>
parents: 4071
diff changeset
88 "store_serialisation": self._avatar_ser,
5f2d496c633f core: get rid of `pickle`:
Goffi <goffi@goffi.org>
parents: 4071
diff changeset
89 "store_deserialisation": self._avatar_deser
3254
6cf4bd6972c2 core, frontends: avatar refactoring:
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
90 },
6cf4bd6972c2 core, frontends: avatar refactoring:
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
91 "nicknames": {
6cf4bd6972c2 core, frontends: avatar refactoring:
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
92 "type": list,
6cf4bd6972c2 core, frontends: avatar refactoring:
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
93 # accumulate all nicknames from all callbacks in a list instead
6cf4bd6972c2 core, frontends: avatar refactoring:
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
94 # of returning only the data from the first successful callback
6cf4bd6972c2 core, frontends: avatar refactoring:
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
95 "get_all": True,
6cf4bd6972c2 core, frontends: avatar refactoring:
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
96 # append nicknames from roster, resource, etc.
4037
524856bd7b19 massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents: 3817
diff changeset
97 "get_post_treatment": self.nicknames_get_post_treatment,
524856bd7b19 massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents: 3817
diff changeset
98 "update_is_new_data": self.nicknames_update_is_new_data,
3254
6cf4bd6972c2 core, frontends: avatar refactoring:
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
99 "store": True,
6cf4bd6972c2 core, frontends: avatar refactoring:
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
100 },
3817
998c5318230f plugin identity: make the plugin compatible with component + description:
Goffi <goffi@goffi.org>
parents: 3816
diff changeset
101 "description": {
998c5318230f plugin identity: make the plugin compatible with component + description:
Goffi <goffi@goffi.org>
parents: 3816
diff changeset
102 "type": str,
998c5318230f plugin identity: make the plugin compatible with component + description:
Goffi <goffi@goffi.org>
parents: 3816
diff changeset
103 "get_all": True,
4037
524856bd7b19 massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents: 3817
diff changeset
104 "get_post_treatment": self.description_get_post_treatment,
3817
998c5318230f plugin identity: make the plugin compatible with component + description:
Goffi <goffi@goffi.org>
parents: 3816
diff changeset
105 "store": True,
998c5318230f plugin identity: make the plugin compatible with component + description:
Goffi <goffi@goffi.org>
parents: 3816
diff changeset
106 }
3254
6cf4bd6972c2 core, frontends: avatar refactoring:
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
107 }
4037
524856bd7b19 massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents: 3817
diff changeset
108 host.trigger.add("roster_update", self._roster_update_trigger)
524856bd7b19 massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents: 3817
diff changeset
109 host.memory.set_signal_on_update("avatar")
524856bd7b19 massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents: 3817
diff changeset
110 host.memory.set_signal_on_update("nicknames")
524856bd7b19 massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents: 3817
diff changeset
111 host.bridge.add_method(
524856bd7b19 massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents: 3817
diff changeset
112 "identity_get",
3028
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 2771
diff changeset
113 ".plugin",
3254
6cf4bd6972c2 core, frontends: avatar refactoring:
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
114 in_sign="sasbs",
6cf4bd6972c2 core, frontends: avatar refactoring:
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
115 out_sign="s",
4037
524856bd7b19 massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents: 3817
diff changeset
116 method=self._get_identity,
3028
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 2771
diff changeset
117 async_=True,
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
118 )
4037
524856bd7b19 massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents: 3817
diff changeset
119 host.bridge.add_method(
524856bd7b19 massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents: 3817
diff changeset
120 "identities_get",
3338
203a491fcd86 plugin identity: new methods `identitiesGet` and `identitiesBaseGet`
Goffi <goffi@goffi.org>
parents: 3326
diff changeset
121 ".plugin",
203a491fcd86 plugin identity: new methods `identitiesGet` and `identitiesBaseGet`
Goffi <goffi@goffi.org>
parents: 3326
diff changeset
122 in_sign="asass",
203a491fcd86 plugin identity: new methods `identitiesGet` and `identitiesBaseGet`
Goffi <goffi@goffi.org>
parents: 3326
diff changeset
123 out_sign="s",
4037
524856bd7b19 massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents: 3817
diff changeset
124 method=self._get_identities,
3338
203a491fcd86 plugin identity: new methods `identitiesGet` and `identitiesBaseGet`
Goffi <goffi@goffi.org>
parents: 3326
diff changeset
125 async_=True,
203a491fcd86 plugin identity: new methods `identitiesGet` and `identitiesBaseGet`
Goffi <goffi@goffi.org>
parents: 3326
diff changeset
126 )
4037
524856bd7b19 massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents: 3817
diff changeset
127 host.bridge.add_method(
524856bd7b19 massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents: 3817
diff changeset
128 "identities_base_get",
3338
203a491fcd86 plugin identity: new methods `identitiesGet` and `identitiesBaseGet`
Goffi <goffi@goffi.org>
parents: 3326
diff changeset
129 ".plugin",
203a491fcd86 plugin identity: new methods `identitiesGet` and `identitiesBaseGet`
Goffi <goffi@goffi.org>
parents: 3326
diff changeset
130 in_sign="s",
203a491fcd86 plugin identity: new methods `identitiesGet` and `identitiesBaseGet`
Goffi <goffi@goffi.org>
parents: 3326
diff changeset
131 out_sign="s",
4037
524856bd7b19 massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents: 3817
diff changeset
132 method=self._get_base_identities,
3338
203a491fcd86 plugin identity: new methods `identitiesGet` and `identitiesBaseGet`
Goffi <goffi@goffi.org>
parents: 3326
diff changeset
133 async_=True,
203a491fcd86 plugin identity: new methods `identitiesGet` and `identitiesBaseGet`
Goffi <goffi@goffi.org>
parents: 3326
diff changeset
134 )
4037
524856bd7b19 massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents: 3817
diff changeset
135 host.bridge.add_method(
524856bd7b19 massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents: 3817
diff changeset
136 "identity_set",
3028
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 2771
diff changeset
137 ".plugin",
3254
6cf4bd6972c2 core, frontends: avatar refactoring:
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
138 in_sign="ss",
3028
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 2771
diff changeset
139 out_sign="",
4037
524856bd7b19 massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents: 3817
diff changeset
140 method=self._set_identity,
3028
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 2771
diff changeset
141 async_=True,
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
142 )
4037
524856bd7b19 massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents: 3817
diff changeset
143 host.bridge.add_method(
524856bd7b19 massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents: 3817
diff changeset
144 "avatar_get",
3254
6cf4bd6972c2 core, frontends: avatar refactoring:
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
145 ".plugin",
6cf4bd6972c2 core, frontends: avatar refactoring:
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
146 in_sign="sbs",
6cf4bd6972c2 core, frontends: avatar refactoring:
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
147 out_sign="s",
6cf4bd6972c2 core, frontends: avatar refactoring:
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
148 method=self._getAvatar,
6cf4bd6972c2 core, frontends: avatar refactoring:
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
149 async_=True,
6cf4bd6972c2 core, frontends: avatar refactoring:
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
150 )
4037
524856bd7b19 massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents: 3817
diff changeset
151 host.bridge.add_method(
524856bd7b19 massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents: 3817
diff changeset
152 "avatar_set",
3254
6cf4bd6972c2 core, frontends: avatar refactoring:
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
153 ".plugin",
6cf4bd6972c2 core, frontends: avatar refactoring:
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
154 in_sign="sss",
6cf4bd6972c2 core, frontends: avatar refactoring:
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
155 out_sign="",
4037
524856bd7b19 massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents: 3817
diff changeset
156 method=self._set_avatar,
3254
6cf4bd6972c2 core, frontends: avatar refactoring:
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
157 async_=True,
6cf4bd6972c2 core, frontends: avatar refactoring:
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
158 )
2253
db468f24b9fc plugin identity: plugin identity first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
159
4037
524856bd7b19 massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents: 3817
diff changeset
160 async def profile_connecting(self, client):
3277
cf07641b764d plugin identity: fixed infinite loop on nicknames update
Goffi <goffi@goffi.org>
parents: 3262
diff changeset
161 client._identity_update_lock = []
3254
6cf4bd6972c2 core, frontends: avatar refactoring:
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
162 # we restore known identities from database
6cf4bd6972c2 core, frontends: avatar refactoring:
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
163 client._identity_storage = persistent.LazyPersistentBinaryDict(
6cf4bd6972c2 core, frontends: avatar refactoring:
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
164 "identity", client.profile)
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 stored_data = await client._identity_storage.all()
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 to_delete = []
6cf4bd6972c2 core, frontends: avatar refactoring:
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
169
6cf4bd6972c2 core, frontends: avatar refactoring:
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
170 for key, value in stored_data.items():
6cf4bd6972c2 core, frontends: avatar refactoring:
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
171 entity_s, name = key.split('\n')
4212
5f2d496c633f core: get rid of `pickle`:
Goffi <goffi@goffi.org>
parents: 4071
diff changeset
172 try:
5f2d496c633f core: get rid of `pickle`:
Goffi <goffi@goffi.org>
parents: 4071
diff changeset
173 metadata = self.metadata[name]
5f2d496c633f core: get rid of `pickle`:
Goffi <goffi@goffi.org>
parents: 4071
diff changeset
174 except KeyError:
3254
6cf4bd6972c2 core, frontends: avatar refactoring:
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
175 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
176 to_delete.append(key)
6cf4bd6972c2 core, frontends: avatar refactoring:
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
177 continue
4212
5f2d496c633f core: get rid of `pickle`:
Goffi <goffi@goffi.org>
parents: 4071
diff changeset
178 if value is not None:
5f2d496c633f core: get rid of `pickle`:
Goffi <goffi@goffi.org>
parents: 4071
diff changeset
179 deser_method = metadata.get("store_deserialisation")
5f2d496c633f core: get rid of `pickle`:
Goffi <goffi@goffi.org>
parents: 4071
diff changeset
180 if deser_method is not None:
5f2d496c633f core: get rid of `pickle`:
Goffi <goffi@goffi.org>
parents: 4071
diff changeset
181 value = deser_method(value)
3254
6cf4bd6972c2 core, frontends: avatar refactoring:
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
182 entity = jid.JID(entity_s)
6cf4bd6972c2 core, frontends: avatar refactoring:
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
183
6cf4bd6972c2 core, frontends: avatar refactoring:
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
184 if name == 'avatar':
6cf4bd6972c2 core, frontends: avatar refactoring:
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
185 if value is not None:
6cf4bd6972c2 core, frontends: avatar refactoring:
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
186 try:
6cf4bd6972c2 core, frontends: avatar refactoring:
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
187 cache_uid = value['cache_uid']
6cf4bd6972c2 core, frontends: avatar refactoring:
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
188 if not cache_uid:
6cf4bd6972c2 core, frontends: avatar refactoring:
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
189 raise ValueError
3326
9e1ba1e1179f plugin identity: added "filename" metadata for avatar
Goffi <goffi@goffi.org>
parents: 3278
diff changeset
190 filename = value['filename']
9e1ba1e1179f plugin identity: added "filename" metadata for avatar
Goffi <goffi@goffi.org>
parents: 3278
diff changeset
191 if not filename:
9e1ba1e1179f plugin identity: added "filename" metadata for avatar
Goffi <goffi@goffi.org>
parents: 3278
diff changeset
192 raise ValueError
3254
6cf4bd6972c2 core, frontends: avatar refactoring:
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
193 except (ValueError, KeyError):
6cf4bd6972c2 core, frontends: avatar refactoring:
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
194 log.warning(
6cf4bd6972c2 core, frontends: avatar refactoring:
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
195 f"invalid data for {entity} avatar, it will be deleted: "
6cf4bd6972c2 core, frontends: avatar refactoring:
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
196 f"{value}")
6cf4bd6972c2 core, frontends: avatar refactoring:
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
197 to_delete.append(key)
6cf4bd6972c2 core, frontends: avatar refactoring:
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
198 continue
4037
524856bd7b19 massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents: 3817
diff changeset
199 cache = self.host.common_cache.get_metadata(cache_uid)
3254
6cf4bd6972c2 core, frontends: avatar refactoring:
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
200 if cache is None:
6cf4bd6972c2 core, frontends: avatar refactoring:
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
201 log.debug(
6cf4bd6972c2 core, frontends: avatar refactoring:
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
202 f"purging avatar for {entity}: it is not in cache anymore")
6cf4bd6972c2 core, frontends: avatar refactoring:
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
203 to_delete.append(key)
6cf4bd6972c2 core, frontends: avatar refactoring:
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
204 continue
6cf4bd6972c2 core, frontends: avatar refactoring:
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
205
4037
524856bd7b19 massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents: 3817
diff changeset
206 self.host.memory.update_entity_data(
3254
6cf4bd6972c2 core, frontends: avatar refactoring:
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
207 client, entity, name, value, silent=True
6cf4bd6972c2 core, frontends: avatar refactoring:
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
208 )
6cf4bd6972c2 core, frontends: avatar refactoring:
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
209
6cf4bd6972c2 core, frontends: avatar refactoring:
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
210 for key in to_delete:
6cf4bd6972c2 core, frontends: avatar refactoring:
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
211 await client._identity_storage.adel(key)
6cf4bd6972c2 core, frontends: avatar refactoring:
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
212
4037
524856bd7b19 massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents: 3817
diff changeset
213 def _roster_update_trigger(self, client, roster_item):
524856bd7b19 massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents: 3817
diff changeset
214 old_item = client.roster.get_item(roster_item.jid)
3254
6cf4bd6972c2 core, frontends: avatar refactoring:
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
215 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
216 log.debug(
6cf4bd6972c2 core, frontends: avatar refactoring:
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
217 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
218 f"{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 defer.ensureDeferred(
6cf4bd6972c2 core, frontends: avatar refactoring:
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
221 self.update(
6cf4bd6972c2 core, frontends: avatar refactoring:
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
222 client,
3338
203a491fcd86 plugin identity: new methods `identitiesGet` and `identitiesBaseGet`
Goffi <goffi@goffi.org>
parents: 3326
diff changeset
223 IMPORT_NAME,
3254
6cf4bd6972c2 core, frontends: avatar refactoring:
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
224 "nicknames",
6cf4bd6972c2 core, frontends: avatar refactoring:
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
225 [roster_item.name],
6cf4bd6972c2 core, frontends: avatar refactoring:
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
226 roster_item.jid
6cf4bd6972c2 core, frontends: avatar refactoring:
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
227 )
6cf4bd6972c2 core, frontends: avatar refactoring:
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
228 )
6cf4bd6972c2 core, frontends: avatar refactoring:
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
229 return True
6cf4bd6972c2 core, frontends: avatar refactoring:
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
230
3277
cf07641b764d plugin identity: fixed infinite loop on nicknames update
Goffi <goffi@goffi.org>
parents: 3262
diff changeset
231 def register(
cf07641b764d plugin identity: fixed infinite loop on nicknames update
Goffi <goffi@goffi.org>
parents: 3262
diff changeset
232 self,
cf07641b764d plugin identity: fixed infinite loop on nicknames update
Goffi <goffi@goffi.org>
parents: 3262
diff changeset
233 origin: str,
cf07641b764d plugin identity: fixed infinite loop on nicknames update
Goffi <goffi@goffi.org>
parents: 3262
diff changeset
234 metadata_name: str,
cf07641b764d plugin identity: fixed infinite loop on nicknames update
Goffi <goffi@goffi.org>
parents: 3262
diff changeset
235 cb_get: Union[Coroutine, defer.Deferred],
cf07641b764d plugin identity: fixed infinite loop on nicknames update
Goffi <goffi@goffi.org>
parents: 3262
diff changeset
236 cb_set: Union[Coroutine, defer.Deferred],
cf07641b764d plugin identity: fixed infinite loop on nicknames update
Goffi <goffi@goffi.org>
parents: 3262
diff changeset
237 priority: int=0):
3254
6cf4bd6972c2 core, frontends: avatar refactoring:
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
238 """Register callbacks to handle identity metadata
6cf4bd6972c2 core, frontends: avatar refactoring:
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
239
3277
cf07641b764d plugin identity: fixed infinite loop on nicknames update
Goffi <goffi@goffi.org>
parents: 3262
diff changeset
240 @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
241 @param metadata_name: name of metadata can be:
3254
6cf4bd6972c2 core, frontends: avatar refactoring:
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
242 - avatar
6cf4bd6972c2 core, frontends: avatar refactoring:
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
243 - nicknames
3277
cf07641b764d plugin identity: fixed infinite loop on nicknames update
Goffi <goffi@goffi.org>
parents: 3262
diff changeset
244 @param cb_get: method to retrieve a metadata
3254
6cf4bd6972c2 core, frontends: avatar refactoring:
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
245 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
246 @param cb_set: method to set a metadata
3254
6cf4bd6972c2 core, frontends: avatar refactoring:
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
247 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
248 @param priority: priority of this method for the given metadata.
3254
6cf4bd6972c2 core, frontends: avatar refactoring:
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
249 methods with bigger priorities will be called first
6cf4bd6972c2 core, frontends: avatar refactoring:
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
250 """
6cf4bd6972c2 core, frontends: avatar refactoring:
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
251 if not metadata_name in self.metadata.keys():
6cf4bd6972c2 core, frontends: avatar refactoring:
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
252 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
253 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
254 cb_list = self.metadata[metadata_name].setdefault('callbacks', [])
6cf4bd6972c2 core, frontends: avatar refactoring:
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
255 cb_list.append(callback)
6cf4bd6972c2 core, frontends: avatar refactoring:
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
256 cb_list.sort(key=lambda c: c.priority, reverse=True)
6cf4bd6972c2 core, frontends: avatar refactoring:
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
257
4037
524856bd7b19 massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents: 3817
diff changeset
258 def get_identity_jid(self, client, peer_jid):
3254
6cf4bd6972c2 core, frontends: avatar refactoring:
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
259 """Return jid to use to set identity metadata
6cf4bd6972c2 core, frontends: avatar refactoring:
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
260
6cf4bd6972c2 core, frontends: avatar refactoring:
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
261 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
262 otherwise bare jid will be used
6cf4bd6972c2 core, frontends: avatar refactoring:
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
263 if None, bare jid of profile will be used
6cf4bd6972c2 core, frontends: avatar refactoring:
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
264 @return (jid.JID): jid to use for avatar
6cf4bd6972c2 core, frontends: avatar refactoring:
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
265 """
6cf4bd6972c2 core, frontends: avatar refactoring:
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
266 if peer_jid is None:
6cf4bd6972c2 core, frontends: avatar refactoring:
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
267 return client.jid.userhostJID()
6cf4bd6972c2 core, frontends: avatar refactoring:
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
268 if self._m is None:
6cf4bd6972c2 core, frontends: avatar refactoring:
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
269 return peer_jid.userhostJID()
6cf4bd6972c2 core, frontends: avatar refactoring:
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
270 else:
4037
524856bd7b19 massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents: 3817
diff changeset
271 return self._m.get_bare_or_full(client, peer_jid)
3254
6cf4bd6972c2 core, frontends: avatar refactoring:
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
272
4037
524856bd7b19 massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents: 3817
diff changeset
273 def check_type(self, metadata_name, value):
3254
6cf4bd6972c2 core, frontends: avatar refactoring:
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
274 """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
275 value_type = self.metadata[metadata_name]["type"]
6cf4bd6972c2 core, frontends: avatar refactoring:
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
276 if not isinstance(value, value_type):
6cf4bd6972c2 core, frontends: avatar refactoring:
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
277 raise ValueError(
6cf4bd6972c2 core, frontends: avatar refactoring:
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
278 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
279 f"expected")
6cf4bd6972c2 core, frontends: avatar refactoring:
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
280
4037
524856bd7b19 massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents: 3817
diff changeset
281 def get_field_type(self, metadata_name: str) -> str:
3817
998c5318230f plugin identity: make the plugin compatible with component + description:
Goffi <goffi@goffi.org>
parents: 3816
diff changeset
282 """Return the type the requested field
998c5318230f plugin identity: make the plugin compatible with component + description:
Goffi <goffi@goffi.org>
parents: 3816
diff changeset
283
998c5318230f plugin identity: make the plugin compatible with component + description:
Goffi <goffi@goffi.org>
parents: 3816
diff changeset
284 @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
285 @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
286 """
998c5318230f plugin identity: make the plugin compatible with component + description:
Goffi <goffi@goffi.org>
parents: 3816
diff changeset
287 return self.metadata[metadata_name]["type"]
998c5318230f plugin identity: make the plugin compatible with component + description:
Goffi <goffi@goffi.org>
parents: 3816
diff changeset
288
3277
cf07641b764d plugin identity: fixed infinite loop on nicknames update
Goffi <goffi@goffi.org>
parents: 3262
diff changeset
289 async def get(
cf07641b764d plugin identity: fixed infinite loop on nicknames update
Goffi <goffi@goffi.org>
parents: 3262
diff changeset
290 self,
cf07641b764d plugin identity: fixed infinite loop on nicknames update
Goffi <goffi@goffi.org>
parents: 3262
diff changeset
291 client: SatXMPPEntity,
cf07641b764d plugin identity: fixed infinite loop on nicknames update
Goffi <goffi@goffi.org>
parents: 3262
diff changeset
292 metadata_name: str,
cf07641b764d plugin identity: fixed infinite loop on nicknames update
Goffi <goffi@goffi.org>
parents: 3262
diff changeset
293 entity: Optional[jid.JID],
cf07641b764d plugin identity: fixed infinite loop on nicknames update
Goffi <goffi@goffi.org>
parents: 3262
diff changeset
294 use_cache: bool=True,
cf07641b764d plugin identity: fixed infinite loop on nicknames update
Goffi <goffi@goffi.org>
parents: 3262
diff changeset
295 prefilled_values: Optional[Dict[str, Any]]=None
cf07641b764d plugin identity: fixed infinite loop on nicknames update
Goffi <goffi@goffi.org>
parents: 3262
diff changeset
296 ):
3254
6cf4bd6972c2 core, frontends: avatar refactoring:
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
297 """Retrieve identity metadata of an entity
6cf4bd6972c2 core, frontends: avatar refactoring:
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
298
6cf4bd6972c2 core, frontends: avatar refactoring:
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
299 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
300 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
301 @param metadata_name: name of the metadata
3254
6cf4bd6972c2 core, frontends: avatar refactoring:
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
302 must be one of self.metadata key
6cf4bd6972c2 core, frontends: avatar refactoring:
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
303 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
304 @param entity: entity for which avatar is requested
3254
6cf4bd6972c2 core, frontends: avatar refactoring:
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
305 None to use profile's jid
3277
cf07641b764d plugin identity: fixed infinite loop on nicknames update
Goffi <goffi@goffi.org>
parents: 3262
diff changeset
306 @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
307 @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
308 """
4037
524856bd7b19 massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents: 3817
diff changeset
309 entity = self.get_identity_jid(client, entity)
3254
6cf4bd6972c2 core, frontends: avatar refactoring:
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
310 try:
6cf4bd6972c2 core, frontends: avatar refactoring:
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
311 metadata = self.metadata[metadata_name]
6cf4bd6972c2 core, frontends: avatar refactoring:
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
312 except KeyError:
6cf4bd6972c2 core, frontends: avatar refactoring:
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
313 raise ValueError(f"Invalid metadata name: {metadata_name!r}")
6cf4bd6972c2 core, frontends: avatar refactoring:
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
314 get_all = metadata.get('get_all', False)
6cf4bd6972c2 core, frontends: avatar refactoring:
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
315 if use_cache:
6cf4bd6972c2 core, frontends: avatar refactoring:
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
316 try:
4037
524856bd7b19 massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents: 3817
diff changeset
317 data = self.host.memory.get_entity_datum(
3254
6cf4bd6972c2 core, frontends: avatar refactoring:
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
318 client, entity, metadata_name)
6cf4bd6972c2 core, frontends: avatar refactoring:
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
319 except (KeyError, exceptions.UnknownEntityError):
6cf4bd6972c2 core, frontends: avatar refactoring:
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
320 pass
6cf4bd6972c2 core, frontends: avatar refactoring:
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
321 else:
6cf4bd6972c2 core, frontends: avatar refactoring:
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
322 return data
6cf4bd6972c2 core, frontends: avatar refactoring:
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
323
6cf4bd6972c2 core, frontends: avatar refactoring:
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
324 try:
6cf4bd6972c2 core, frontends: avatar refactoring:
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
325 callbacks = metadata['callbacks']
6cf4bd6972c2 core, frontends: avatar refactoring:
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
326 except KeyError:
6cf4bd6972c2 core, frontends: avatar refactoring:
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
327 log.warning(_("No callback registered for {metadata_name}")
6cf4bd6972c2 core, frontends: avatar refactoring:
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
328 .format(metadata_name=metadata_name))
6cf4bd6972c2 core, frontends: avatar refactoring:
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
329 return [] if get_all else None
6cf4bd6972c2 core, frontends: avatar refactoring:
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
330
6cf4bd6972c2 core, frontends: avatar refactoring:
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
331 if get_all:
6cf4bd6972c2 core, frontends: avatar refactoring:
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
332 all_data = []
3277
cf07641b764d plugin identity: fixed infinite loop on nicknames update
Goffi <goffi@goffi.org>
parents: 3262
diff changeset
333 elif prefilled_values is not None:
cf07641b764d plugin identity: fixed infinite loop on nicknames update
Goffi <goffi@goffi.org>
parents: 3262
diff changeset
334 raise exceptions.InternalError(
cf07641b764d plugin identity: fixed infinite loop on nicknames update
Goffi <goffi@goffi.org>
parents: 3262
diff changeset
335 "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
336
6cf4bd6972c2 core, frontends: avatar refactoring:
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
337 for callback in callbacks:
6cf4bd6972c2 core, frontends: avatar refactoring:
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
338 try:
3277
cf07641b764d plugin identity: fixed infinite loop on nicknames update
Goffi <goffi@goffi.org>
parents: 3262
diff changeset
339 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
340 data = prefilled_values[callback.origin]
cf07641b764d plugin identity: fixed infinite loop on nicknames update
Goffi <goffi@goffi.org>
parents: 3262
diff changeset
341 log.debug(
cf07641b764d plugin identity: fixed infinite loop on nicknames update
Goffi <goffi@goffi.org>
parents: 3262
diff changeset
342 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
343 f"{callback.origin}")
cf07641b764d plugin identity: fixed infinite loop on nicknames update
Goffi <goffi@goffi.org>
parents: 3262
diff changeset
344 else:
cf07641b764d plugin identity: fixed infinite loop on nicknames update
Goffi <goffi@goffi.org>
parents: 3262
diff changeset
345 data = await defer.ensureDeferred(callback.get(client, entity))
3254
6cf4bd6972c2 core, frontends: avatar refactoring:
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
346 except exceptions.CancelError:
6cf4bd6972c2 core, frontends: avatar refactoring:
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
347 continue
6cf4bd6972c2 core, frontends: avatar refactoring:
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
348 except Exception as e:
6cf4bd6972c2 core, frontends: avatar refactoring:
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
349 log.warning(
6cf4bd6972c2 core, frontends: avatar refactoring:
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
350 _("Error while trying to get {metadata_name} with {callback}: {e}")
6cf4bd6972c2 core, frontends: avatar refactoring:
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
351 .format(callback=callback.get, metadata_name=metadata_name, e=e))
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 if data:
4037
524856bd7b19 massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents: 3817
diff changeset
354 self.check_type(metadata_name, data)
3254
6cf4bd6972c2 core, frontends: avatar refactoring:
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
355 if get_all:
3817
998c5318230f plugin identity: make the plugin compatible with component + description:
Goffi <goffi@goffi.org>
parents: 3816
diff changeset
356 if isinstance(data, list):
998c5318230f plugin identity: make the plugin compatible with component + description:
Goffi <goffi@goffi.org>
parents: 3816
diff changeset
357 all_data.extend(data)
998c5318230f plugin identity: make the plugin compatible with component + description:
Goffi <goffi@goffi.org>
parents: 3816
diff changeset
358 else:
998c5318230f plugin identity: make the plugin compatible with component + description:
Goffi <goffi@goffi.org>
parents: 3816
diff changeset
359 all_data.append(data)
3254
6cf4bd6972c2 core, frontends: avatar refactoring:
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
360 else:
6cf4bd6972c2 core, frontends: avatar refactoring:
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
361 break
6cf4bd6972c2 core, frontends: avatar refactoring:
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
362 else:
6cf4bd6972c2 core, frontends: avatar refactoring:
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
363 data = None
6cf4bd6972c2 core, frontends: avatar refactoring:
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
364
6cf4bd6972c2 core, frontends: avatar refactoring:
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
365 if get_all:
6cf4bd6972c2 core, frontends: avatar refactoring:
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
366 data = all_data
6cf4bd6972c2 core, frontends: avatar refactoring:
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
367
6cf4bd6972c2 core, frontends: avatar refactoring:
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
368 post_treatment = metadata.get("get_post_treatment")
6cf4bd6972c2 core, frontends: avatar refactoring:
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
369 if post_treatment is not None:
4037
524856bd7b19 massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents: 3817
diff changeset
370 data = await utils.as_deferred(post_treatment, client, entity, data)
3254
6cf4bd6972c2 core, frontends: avatar refactoring:
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
371
4037
524856bd7b19 massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents: 3817
diff changeset
372 self.host.memory.update_entity_data(
3254
6cf4bd6972c2 core, frontends: avatar refactoring:
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
373 client, entity, metadata_name, data)
6cf4bd6972c2 core, frontends: avatar refactoring:
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
374
6cf4bd6972c2 core, frontends: avatar refactoring:
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
375 if metadata.get('store', False):
4212
5f2d496c633f core: get rid of `pickle`:
Goffi <goffi@goffi.org>
parents: 4071
diff changeset
376 if data is not None:
5f2d496c633f core: get rid of `pickle`:
Goffi <goffi@goffi.org>
parents: 4071
diff changeset
377 ser_method = metadata.get("store_serialisation")
5f2d496c633f core: get rid of `pickle`:
Goffi <goffi@goffi.org>
parents: 4071
diff changeset
378 if ser_method is not None:
5f2d496c633f core: get rid of `pickle`:
Goffi <goffi@goffi.org>
parents: 4071
diff changeset
379 data = ser_method(data)
3254
6cf4bd6972c2 core, frontends: avatar refactoring:
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
380 key = f"{entity}\n{metadata_name}"
6cf4bd6972c2 core, frontends: avatar refactoring:
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
381 await client._identity_storage.aset(key, data)
6cf4bd6972c2 core, frontends: avatar refactoring:
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
382
6cf4bd6972c2 core, frontends: avatar refactoring:
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
383 return data
6cf4bd6972c2 core, frontends: avatar refactoring:
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
384
6cf4bd6972c2 core, frontends: avatar refactoring:
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
385 async def set(self, client, metadata_name, data, entity=None):
6cf4bd6972c2 core, frontends: avatar refactoring:
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
386 """Set identity metadata for an entity
6cf4bd6972c2 core, frontends: avatar refactoring:
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
387
6cf4bd6972c2 core, frontends: avatar refactoring:
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
388 Registered callbacks will be tried in priority order (bigger to lower)
6cf4bd6972c2 core, frontends: avatar refactoring:
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
389 @param metadata_name(str): name of the metadata
6cf4bd6972c2 core, frontends: avatar refactoring:
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
390 must be one of self.metadata key
6cf4bd6972c2 core, frontends: avatar refactoring:
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
391 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
392 @param data(object): value to set
6cf4bd6972c2 core, frontends: avatar refactoring:
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
393 @param entity(jid.JID, None): entity for which avatar is requested
6cf4bd6972c2 core, frontends: avatar refactoring:
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
394 None to use profile's jid
6cf4bd6972c2 core, frontends: avatar refactoring:
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
395 """
4037
524856bd7b19 massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents: 3817
diff changeset
396 entity = self.get_identity_jid(client, entity)
3254
6cf4bd6972c2 core, frontends: avatar refactoring:
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
397 metadata = self.metadata[metadata_name]
6cf4bd6972c2 core, frontends: avatar refactoring:
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
398 data_filter = metadata.get("set_data_filter")
6cf4bd6972c2 core, frontends: avatar refactoring:
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
399 if data_filter is not None:
4037
524856bd7b19 massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents: 3817
diff changeset
400 data = await utils.as_deferred(data_filter, client, entity, data)
524856bd7b19 massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents: 3817
diff changeset
401 self.check_type(metadata_name, data)
3254
6cf4bd6972c2 core, frontends: avatar refactoring:
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
402
6cf4bd6972c2 core, frontends: avatar refactoring:
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
403 try:
6cf4bd6972c2 core, frontends: avatar refactoring:
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
404 callbacks = metadata['callbacks']
6cf4bd6972c2 core, frontends: avatar refactoring:
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
405 except KeyError:
6cf4bd6972c2 core, frontends: avatar refactoring:
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
406 log.warning(_("No callback registered for {metadata_name}")
6cf4bd6972c2 core, frontends: avatar refactoring:
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
407 .format(metadata_name=metadata_name))
6cf4bd6972c2 core, frontends: avatar refactoring:
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
408 return exceptions.FeatureNotFound(f"Can't set {metadata_name} for {entity}")
6cf4bd6972c2 core, frontends: avatar refactoring:
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
409
6cf4bd6972c2 core, frontends: avatar refactoring:
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
410 for callback in callbacks:
6cf4bd6972c2 core, frontends: avatar refactoring:
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
411 try:
6cf4bd6972c2 core, frontends: avatar refactoring:
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
412 await defer.ensureDeferred(callback.set(client, data, entity))
6cf4bd6972c2 core, frontends: avatar refactoring:
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
413 except exceptions.CancelError:
6cf4bd6972c2 core, frontends: avatar refactoring:
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
414 continue
6cf4bd6972c2 core, frontends: avatar refactoring:
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
415 except Exception as e:
6cf4bd6972c2 core, frontends: avatar refactoring:
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
416 log.warning(
6cf4bd6972c2 core, frontends: avatar refactoring:
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
417 _("Error while trying to set {metadata_name} with {callback}: {e}")
6cf4bd6972c2 core, frontends: avatar refactoring:
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
418 .format(callback=callback.set, metadata_name=metadata_name, e=e))
6cf4bd6972c2 core, frontends: avatar refactoring:
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
419 else:
6cf4bd6972c2 core, frontends: avatar refactoring:
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
420 break
6cf4bd6972c2 core, frontends: avatar refactoring:
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
421 else:
6cf4bd6972c2 core, frontends: avatar refactoring:
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
422 raise exceptions.FeatureNotFound(f"Can't set {metadata_name} for {entity}")
6cf4bd6972c2 core, frontends: avatar refactoring:
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
423
6cf4bd6972c2 core, frontends: avatar refactoring:
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
424 post_treatment = metadata.get("set_post_treatment")
6cf4bd6972c2 core, frontends: avatar refactoring:
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
425 if post_treatment is not None:
4037
524856bd7b19 massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents: 3817
diff changeset
426 await utils.as_deferred(post_treatment, client, entity, data)
3254
6cf4bd6972c2 core, frontends: avatar refactoring:
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
427
3277
cf07641b764d plugin identity: fixed infinite loop on nicknames update
Goffi <goffi@goffi.org>
parents: 3262
diff changeset
428 async def update(
3817
998c5318230f plugin identity: make the plugin compatible with component + description:
Goffi <goffi@goffi.org>
parents: 3816
diff changeset
429 self,
998c5318230f plugin identity: make the plugin compatible with component + description:
Goffi <goffi@goffi.org>
parents: 3816
diff changeset
430 client: SatXMPPEntity,
998c5318230f plugin identity: make the plugin compatible with component + description:
Goffi <goffi@goffi.org>
parents: 3816
diff changeset
431 origin: str,
998c5318230f plugin identity: make the plugin compatible with component + description:
Goffi <goffi@goffi.org>
parents: 3816
diff changeset
432 metadata_name: str,
998c5318230f plugin identity: make the plugin compatible with component + description:
Goffi <goffi@goffi.org>
parents: 3816
diff changeset
433 data: Any,
998c5318230f plugin identity: make the plugin compatible with component + description:
Goffi <goffi@goffi.org>
parents: 3816
diff changeset
434 entity: Optional[jid.JID]
998c5318230f plugin identity: make the plugin compatible with component + description:
Goffi <goffi@goffi.org>
parents: 3816
diff changeset
435 ):
3254
6cf4bd6972c2 core, frontends: avatar refactoring:
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
436 """Update a metadata in cache
6cf4bd6972c2 core, frontends: avatar refactoring:
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
437
6cf4bd6972c2 core, frontends: avatar refactoring:
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
438 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
439 @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
440 """
4037
524856bd7b19 massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents: 3817
diff changeset
441 entity = self.get_identity_jid(client, entity)
3277
cf07641b764d plugin identity: fixed infinite loop on nicknames update
Goffi <goffi@goffi.org>
parents: 3262
diff changeset
442 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
443 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
444 return
3254
6cf4bd6972c2 core, frontends: avatar refactoring:
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
445 metadata = self.metadata[metadata_name]
6cf4bd6972c2 core, frontends: avatar refactoring:
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
446
6cf4bd6972c2 core, frontends: avatar refactoring:
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
447 try:
4037
524856bd7b19 massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents: 3817
diff changeset
448 cached_data = self.host.memory.get_entity_datum(
3254
6cf4bd6972c2 core, frontends: avatar refactoring:
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
449 client, entity, metadata_name)
6cf4bd6972c2 core, frontends: avatar refactoring:
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
450 except (KeyError, exceptions.UnknownEntityError):
6cf4bd6972c2 core, frontends: avatar refactoring:
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
451 # metadata is not cached, we do the update
6cf4bd6972c2 core, frontends: avatar refactoring:
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
452 pass
6cf4bd6972c2 core, frontends: avatar refactoring:
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
453 else:
6cf4bd6972c2 core, frontends: avatar refactoring:
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
454 # 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
455 try:
6cf4bd6972c2 core, frontends: avatar refactoring:
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
456 update_is_new_data = metadata["update_is_new_data"]
6cf4bd6972c2 core, frontends: avatar refactoring:
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
457 except KeyError:
4037
524856bd7b19 massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents: 3817
diff changeset
458 update_is_new_data = self.default_update_is_new_data
3254
6cf4bd6972c2 core, frontends: avatar refactoring:
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
459
3257
704dada41df0 plugin identity: fixed update with `get_all` + better new data detection for avatar:
Goffi <goffi@goffi.org>
parents: 3254
diff changeset
460 if data is None:
3254
6cf4bd6972c2 core, frontends: avatar refactoring:
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
461 if cached_data is None:
6cf4bd6972c2 core, frontends: avatar refactoring:
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
462 log.debug(
6cf4bd6972c2 core, frontends: avatar refactoring:
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
463 f"{metadata_name} for {entity} is already disabled, nothing to "
6cf4bd6972c2 core, frontends: avatar refactoring:
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
464 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
465 return
3262
aa71f1d40300 plugin identity: fixed update when cached data is None
Goffi <goffi@goffi.org>
parents: 3257
diff changeset
466 elif cached_data is None:
aa71f1d40300 plugin identity: fixed update when cached data is None
Goffi <goffi@goffi.org>
parents: 3257
diff changeset
467 pass
3257
704dada41df0 plugin identity: fixed update with `get_all` + better new data detection for avatar:
Goffi <goffi@goffi.org>
parents: 3254
diff changeset
468 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
469 log.debug(
704dada41df0 plugin identity: fixed update with `get_all` + better new data detection for avatar:
Goffi <goffi@goffi.org>
parents: 3254
diff changeset
470 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
471 f"do")
3254
6cf4bd6972c2 core, frontends: avatar refactoring:
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
472 return
6cf4bd6972c2 core, frontends: avatar refactoring:
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
473
6cf4bd6972c2 core, frontends: avatar refactoring:
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
474 # we can't use the cache, so we do the update
6cf4bd6972c2 core, frontends: avatar refactoring:
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
475
6cf4bd6972c2 core, frontends: avatar refactoring:
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
476 log.debug(f"updating {metadata_name} for {entity}")
6cf4bd6972c2 core, frontends: avatar refactoring:
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
477
6cf4bd6972c2 core, frontends: avatar refactoring:
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
478 if metadata.get('get_all', False):
6cf4bd6972c2 core, frontends: avatar refactoring:
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
479 # 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
480 # 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
481 try:
4037
524856bd7b19 massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents: 3817
diff changeset
482 self.host.memory.del_entity_datum(client, entity, metadata_name)
3277
cf07641b764d plugin identity: fixed infinite loop on nicknames update
Goffi <goffi@goffi.org>
parents: 3262
diff changeset
483 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
484 pass
3254
6cf4bd6972c2 core, frontends: avatar refactoring:
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
485 # 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
486 # 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
487 # get callbacks)
cf07641b764d plugin identity: fixed infinite loop on nicknames update
Goffi <goffi@goffi.org>
parents: 3262
diff changeset
488 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
489 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
490 client._identity_update_lock.remove((entity, metadata_name))
3254
6cf4bd6972c2 core, frontends: avatar refactoring:
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
491 return
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 if data is not None:
6cf4bd6972c2 core, frontends: avatar refactoring:
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
494 data_filter = metadata['update_data_filter']
6cf4bd6972c2 core, frontends: avatar refactoring:
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
495 if data_filter is not None:
4037
524856bd7b19 massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents: 3817
diff changeset
496 data = await utils.as_deferred(data_filter, client, entity, data)
524856bd7b19 massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents: 3817
diff changeset
497 self.check_type(metadata_name, data)
3254
6cf4bd6972c2 core, frontends: avatar refactoring:
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
498
4037
524856bd7b19 massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents: 3817
diff changeset
499 self.host.memory.update_entity_data(client, entity, metadata_name, data)
3254
6cf4bd6972c2 core, frontends: avatar refactoring:
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
500
6cf4bd6972c2 core, frontends: avatar refactoring:
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
501 if metadata.get('store', False):
6cf4bd6972c2 core, frontends: avatar refactoring:
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
502 key = f"{entity}\n{metadata_name}"
4212
5f2d496c633f core: get rid of `pickle`:
Goffi <goffi@goffi.org>
parents: 4071
diff changeset
503 if data is not None:
5f2d496c633f core: get rid of `pickle`:
Goffi <goffi@goffi.org>
parents: 4071
diff changeset
504 ser_method = metadata.get("store_serialisation")
5f2d496c633f core: get rid of `pickle`:
Goffi <goffi@goffi.org>
parents: 4071
diff changeset
505 if ser_method is not None:
5f2d496c633f core: get rid of `pickle`:
Goffi <goffi@goffi.org>
parents: 4071
diff changeset
506 data = ser_method(data)
3254
6cf4bd6972c2 core, frontends: avatar refactoring:
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
507 await client._identity_storage.aset(key, data)
6cf4bd6972c2 core, frontends: avatar refactoring:
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
508
4037
524856bd7b19 massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents: 3817
diff changeset
509 def default_update_is_new_data(self, client, entity, cached_data, new_data):
3254
6cf4bd6972c2 core, frontends: avatar refactoring:
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
510 return new_data != cached_data
6cf4bd6972c2 core, frontends: avatar refactoring:
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
511
6cf4bd6972c2 core, frontends: avatar refactoring:
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
512 def _getAvatar(self, entity, use_cache, profile):
4037
524856bd7b19 massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents: 3817
diff changeset
513 client = self.host.get_client(profile)
3254
6cf4bd6972c2 core, frontends: avatar refactoring:
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
514 entity = jid.JID(entity) if entity else None
6cf4bd6972c2 core, frontends: avatar refactoring:
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
515 d = defer.ensureDeferred(self.get(client, "avatar", entity, use_cache))
6cf4bd6972c2 core, frontends: avatar refactoring:
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
516 d.addCallback(lambda data: data_format.serialise(data))
6cf4bd6972c2 core, frontends: avatar refactoring:
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
517 return d
6cf4bd6972c2 core, frontends: avatar refactoring:
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
518
4037
524856bd7b19 massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents: 3817
diff changeset
519 def _set_avatar(self, file_path, entity, profile_key=C.PROF_KEY_NONE):
524856bd7b19 massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents: 3817
diff changeset
520 client = self.host.get_client(profile_key)
3254
6cf4bd6972c2 core, frontends: avatar refactoring:
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
521 entity = jid.JID(entity) if entity else None
6cf4bd6972c2 core, frontends: avatar refactoring:
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
522 return defer.ensureDeferred(
6cf4bd6972c2 core, frontends: avatar refactoring:
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
523 self.set(client, "avatar", file_path, entity))
6cf4bd6972c2 core, frontends: avatar refactoring:
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
524
4037
524856bd7b19 massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents: 3817
diff changeset
525 def _blocking_cache_avatar(
3816
213e83a4ed10 plugin identity, XEP-0054: move avatar resizing and caching method to identity plugin:
Goffi <goffi@goffi.org>
parents: 3541
diff changeset
526 self,
213e83a4ed10 plugin identity, XEP-0054: move avatar resizing and caching method to identity plugin:
Goffi <goffi@goffi.org>
parents: 3541
diff changeset
527 source: str,
213e83a4ed10 plugin identity, XEP-0054: move avatar resizing and caching method to identity plugin:
Goffi <goffi@goffi.org>
parents: 3541
diff changeset
528 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
529 ):
213e83a4ed10 plugin identity, XEP-0054: move avatar resizing and caching method to identity plugin:
Goffi <goffi@goffi.org>
parents: 3541
diff changeset
530 """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
531 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
532 # 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
533 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
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 # 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
536 try:
213e83a4ed10 plugin identity, XEP-0054: move avatar resizing and caching method to identity plugin:
Goffi <goffi@goffi.org>
parents: 3541
diff changeset
537 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
538 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
539 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
540
213e83a4ed10 plugin identity, XEP-0054: move avatar resizing and caching method to identity plugin:
Goffi <goffi@goffi.org>
parents: 3541
diff changeset
541 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
542 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
543 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
544 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
545 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
546 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
547 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
548 upper += offset
213e83a4ed10 plugin identity, XEP-0054: move avatar resizing and caching method to identity plugin:
Goffi <goffi@goffi.org>
parents: 3541
diff changeset
549 lower -= offset
213e83a4ed10 plugin identity, XEP-0054: move avatar resizing and caching method to identity plugin:
Goffi <goffi@goffi.org>
parents: 3541
diff changeset
550 else:
213e83a4ed10 plugin identity, XEP-0054: move avatar resizing and caching method to identity plugin:
Goffi <goffi@goffi.org>
parents: 3541
diff changeset
551 left += offset
213e83a4ed10 plugin identity, XEP-0054: move avatar resizing and caching method to identity plugin:
Goffi <goffi@goffi.org>
parents: 3541
diff changeset
552 right -= offset
213e83a4ed10 plugin identity, XEP-0054: move avatar resizing and caching method to identity plugin:
Goffi <goffi@goffi.org>
parents: 3541
diff changeset
553 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
554 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
555 # 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
556 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
557 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
558 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
559
213e83a4ed10 plugin identity, XEP-0054: move avatar resizing and caching method to identity plugin:
Goffi <goffi@goffi.org>
parents: 3541
diff changeset
560 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
561 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
562 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
563 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
564 img_buf.seek(0)
4037
524856bd7b19 massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents: 3817
diff changeset
565 with self.host.common_cache.cache_data(
3816
213e83a4ed10 plugin identity, XEP-0054: move avatar resizing and caching method to identity plugin:
Goffi <goffi@goffi.org>
parents: 3541
diff changeset
566 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
567 ) as f:
213e83a4ed10 plugin identity, XEP-0054: move avatar resizing and caching method to identity plugin:
Goffi <goffi@goffi.org>
parents: 3541
diff changeset
568 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
569 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
570 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
571 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
572
4037
524856bd7b19 massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents: 3817
diff changeset
573 async def cache_avatar(self, source: str, avatar_data: Dict[str, Any]) -> None:
3816
213e83a4ed10 plugin identity, XEP-0054: move avatar resizing and caching method to identity plugin:
Goffi <goffi@goffi.org>
parents: 3541
diff changeset
574 """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
575
213e83a4ed10 plugin identity, XEP-0054: move avatar resizing and caching method to identity plugin:
Goffi <goffi@goffi.org>
parents: 3541
diff changeset
576 @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
577 will be used in cache metadata
4037
524856bd7b19 massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents: 3817
diff changeset
578 @param avatar_data: avatar metadata as build by [avatar_set_data_filter]
3816
213e83a4ed10 plugin identity, XEP-0054: move avatar resizing and caching method to identity plugin:
Goffi <goffi@goffi.org>
parents: 3541
diff changeset
579 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
580 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
581 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
582 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
583 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
584 """
4037
524856bd7b19 massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents: 3817
diff changeset
585 await threads.deferToThread(self._blocking_cache_avatar, source, avatar_data)
3816
213e83a4ed10 plugin identity, XEP-0054: move avatar resizing and caching method to identity plugin:
Goffi <goffi@goffi.org>
parents: 3541
diff changeset
586
4037
524856bd7b19 massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents: 3817
diff changeset
587 async def avatar_set_data_filter(self, client, entity, file_path):
3254
6cf4bd6972c2 core, frontends: avatar refactoring:
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
588 """Convert avatar file path to dict data"""
6cf4bd6972c2 core, frontends: avatar refactoring:
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
589 file_path = Path(file_path)
6cf4bd6972c2 core, frontends: avatar refactoring:
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
590 if not file_path.is_file():
6cf4bd6972c2 core, frontends: avatar refactoring:
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
591 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
592 avatar_data = {
6cf4bd6972c2 core, frontends: avatar refactoring:
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
593 'path': file_path,
3326
9e1ba1e1179f plugin identity: added "filename" metadata for avatar
Goffi <goffi@goffi.org>
parents: 3278
diff changeset
594 'filename': file_path.name,
3254
6cf4bd6972c2 core, frontends: avatar refactoring:
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
595 'media_type': image.guess_type(file_path),
6cf4bd6972c2 core, frontends: avatar refactoring:
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
596 }
6cf4bd6972c2 core, frontends: avatar refactoring:
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
597 media_type = avatar_data['media_type']
6cf4bd6972c2 core, frontends: avatar refactoring:
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
598 if media_type is None:
6cf4bd6972c2 core, frontends: avatar refactoring:
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
599 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
600 if not media_type.startswith('image/'):
6cf4bd6972c2 core, frontends: avatar refactoring:
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
601 raise ValueError(f"File at {file_path} doesn't appear to be an image")
4037
524856bd7b19 massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents: 3817
diff changeset
602 await self.cache_avatar(IMPORT_NAME, avatar_data)
3254
6cf4bd6972c2 core, frontends: avatar refactoring:
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
603 return avatar_data
6cf4bd6972c2 core, frontends: avatar refactoring:
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
604
4037
524856bd7b19 massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents: 3817
diff changeset
605 async def avatar_set_post_treatment(self, client, entity, avatar_data):
3254
6cf4bd6972c2 core, frontends: avatar refactoring:
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
606 """Update our own avatar"""
3278
27d4b71e264a plugin identity: fixed update in avatarSetPostTreatment
Goffi <goffi@goffi.org>
parents: 3277
diff changeset
607 await self.update(client, IMPORT_NAME, "avatar", avatar_data, entity)
3254
6cf4bd6972c2 core, frontends: avatar refactoring:
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
608
4037
524856bd7b19 massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents: 3817
diff changeset
609 def avatar_build_metadata(
3816
213e83a4ed10 plugin identity, XEP-0054: move avatar resizing and caching method to identity plugin:
Goffi <goffi@goffi.org>
parents: 3541
diff changeset
610 self,
213e83a4ed10 plugin identity, XEP-0054: move avatar resizing and caching method to identity plugin:
Goffi <goffi@goffi.org>
parents: 3541
diff changeset
611 path: Path,
213e83a4ed10 plugin identity, XEP-0054: move avatar resizing and caching method to identity plugin:
Goffi <goffi@goffi.org>
parents: 3541
diff changeset
612 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
613 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
614 ) -> Optional[Dict[str, Union[str, Path, None]]]:
3254
6cf4bd6972c2 core, frontends: avatar refactoring:
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
615 """Helper method to generate avatar metadata
6cf4bd6972c2 core, frontends: avatar refactoring:
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
616
6cf4bd6972c2 core, frontends: avatar refactoring:
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
617 @param path(str, Path, None): path to avatar file
6cf4bd6972c2 core, frontends: avatar refactoring:
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
618 avatar file must be in cache
6cf4bd6972c2 core, frontends: avatar refactoring:
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
619 None if avatar is explicitely not set
6cf4bd6972c2 core, frontends: avatar refactoring:
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
620 @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
621 @param cache_uid(str, None): UID of avatar in cache
6cf4bd6972c2 core, frontends: avatar refactoring:
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
622 @return (dict, None): avatar metadata
6cf4bd6972c2 core, frontends: avatar refactoring:
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
623 None if avatar is not set
6cf4bd6972c2 core, frontends: avatar refactoring:
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
624 """
6cf4bd6972c2 core, frontends: avatar refactoring:
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
625 if path is None:
6cf4bd6972c2 core, frontends: avatar refactoring:
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
626 return None
6cf4bd6972c2 core, frontends: avatar refactoring:
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
627 else:
6cf4bd6972c2 core, frontends: avatar refactoring:
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
628 if cache_uid is None:
6cf4bd6972c2 core, frontends: avatar refactoring:
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
629 raise ValueError("cache_uid must be set if path is set")
6cf4bd6972c2 core, frontends: avatar refactoring:
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
630 path = Path(path)
6cf4bd6972c2 core, frontends: avatar refactoring:
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
631 if media_type is None:
6cf4bd6972c2 core, frontends: avatar refactoring:
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
632 media_type = image.guess_type(path)
6cf4bd6972c2 core, frontends: avatar refactoring:
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
633
6cf4bd6972c2 core, frontends: avatar refactoring:
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
634 return {
6cf4bd6972c2 core, frontends: avatar refactoring:
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
635 "path": path,
3326
9e1ba1e1179f plugin identity: added "filename" metadata for avatar
Goffi <goffi@goffi.org>
parents: 3278
diff changeset
636 "filename": path.name,
3254
6cf4bd6972c2 core, frontends: avatar refactoring:
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
637 "media_type": media_type,
6cf4bd6972c2 core, frontends: avatar refactoring:
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
638 "cache_uid": cache_uid,
6cf4bd6972c2 core, frontends: avatar refactoring:
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
639 }
6cf4bd6972c2 core, frontends: avatar refactoring:
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
640
4037
524856bd7b19 massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents: 3817
diff changeset
641 def avatar_update_is_new_data(self, client, entity, cached_data, new_data):
3257
704dada41df0 plugin identity: fixed update with `get_all` + better new data detection for avatar:
Goffi <goffi@goffi.org>
parents: 3254
diff changeset
642 return new_data['path'] != cached_data['path']
3254
6cf4bd6972c2 core, frontends: avatar refactoring:
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
643
4037
524856bd7b19 massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents: 3817
diff changeset
644 async def avatar_update_data_filter(self, client, entity, data):
3254
6cf4bd6972c2 core, frontends: avatar refactoring:
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
645 if not isinstance(data, dict):
6cf4bd6972c2 core, frontends: avatar refactoring:
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
646 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
647 mandatory_keys = {'path', 'filename', 'cache_uid'}
3254
6cf4bd6972c2 core, frontends: avatar refactoring:
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
648 if not data.keys() >= mandatory_keys:
6cf4bd6972c2 core, frontends: avatar refactoring:
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
649 raise ValueError(f"missing avatar data keys: {mandatory_keys - data.keys()}")
6cf4bd6972c2 core, frontends: avatar refactoring:
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
650 return data
6cf4bd6972c2 core, frontends: avatar refactoring:
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
651
4212
5f2d496c633f core: get rid of `pickle`:
Goffi <goffi@goffi.org>
parents: 4071
diff changeset
652 def _avatar_ser(self, data: dict) -> dict:
5f2d496c633f core: get rid of `pickle`:
Goffi <goffi@goffi.org>
parents: 4071
diff changeset
653 if data.get("path"):
5f2d496c633f core: get rid of `pickle`:
Goffi <goffi@goffi.org>
parents: 4071
diff changeset
654 # Path instance can't be stored
5f2d496c633f core: get rid of `pickle`:
Goffi <goffi@goffi.org>
parents: 4071
diff changeset
655 data = data.copy()
5f2d496c633f core: get rid of `pickle`:
Goffi <goffi@goffi.org>
parents: 4071
diff changeset
656 data["path"] = str(data["path"])
5f2d496c633f core: get rid of `pickle`:
Goffi <goffi@goffi.org>
parents: 4071
diff changeset
657 return data
5f2d496c633f core: get rid of `pickle`:
Goffi <goffi@goffi.org>
parents: 4071
diff changeset
658
5f2d496c633f core: get rid of `pickle`:
Goffi <goffi@goffi.org>
parents: 4071
diff changeset
659 def _avatar_deser(self, data: dict) -> dict:
5f2d496c633f core: get rid of `pickle`:
Goffi <goffi@goffi.org>
parents: 4071
diff changeset
660 if data.get("path"):
5f2d496c633f core: get rid of `pickle`:
Goffi <goffi@goffi.org>
parents: 4071
diff changeset
661 data["path"] = Path(data["path"])
5f2d496c633f core: get rid of `pickle`:
Goffi <goffi@goffi.org>
parents: 4071
diff changeset
662 return data
5f2d496c633f core: get rid of `pickle`:
Goffi <goffi@goffi.org>
parents: 4071
diff changeset
663
4037
524856bd7b19 massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents: 3817
diff changeset
664 async def nicknames_get_post_treatment(self, client, entity, plugin_nicknames):
3254
6cf4bd6972c2 core, frontends: avatar refactoring:
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
665 """Prepend nicknames from core locations + set default nickname
6cf4bd6972c2 core, frontends: avatar refactoring:
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
666
6cf4bd6972c2 core, frontends: avatar refactoring:
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
667 nicknames are checked from many locations, there is always at least
6cf4bd6972c2 core, frontends: avatar refactoring:
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
668 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
669 Nicknames are appended in this order:
6cf4bd6972c2 core, frontends: avatar refactoring:
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
670 - roster, plugins set nicknames
6cf4bd6972c2 core, frontends: avatar refactoring:
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
671 - 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
672 if there is no user part.
6cf4bd6972c2 core, frontends: avatar refactoring:
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
673 For MUC, room nick is always put first
6cf4bd6972c2 core, frontends: avatar refactoring:
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
674 """
6cf4bd6972c2 core, frontends: avatar refactoring:
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
675 nicknames = []
3817
998c5318230f plugin identity: make the plugin compatible with component + description:
Goffi <goffi@goffi.org>
parents: 3816
diff changeset
676
998c5318230f plugin identity: make the plugin compatible with component + description:
Goffi <goffi@goffi.org>
parents: 3816
diff changeset
677 # for MUC we add resource
3254
6cf4bd6972c2 core, frontends: avatar refactoring:
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
678 if entity.resource:
4037
524856bd7b19 massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents: 3817
diff changeset
679 # get_identity_jid let the resource only if the entity is a MUC room
3254
6cf4bd6972c2 core, frontends: avatar refactoring:
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
680 # occupant jid
6cf4bd6972c2 core, frontends: avatar refactoring:
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
681 nicknames.append(entity.resource)
6cf4bd6972c2 core, frontends: avatar refactoring:
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
682
3817
998c5318230f plugin identity: make the plugin compatible with component + description:
Goffi <goffi@goffi.org>
parents: 3816
diff changeset
683 # 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
684 if not client.is_component:
4037
524856bd7b19 massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents: 3817
diff changeset
685 roster_item = client.roster.get_item(entity.userhostJID())
3817
998c5318230f plugin identity: make the plugin compatible with component + description:
Goffi <goffi@goffi.org>
parents: 3816
diff changeset
686 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
687 # 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
688 nicknames.append(roster_item.name)
3254
6cf4bd6972c2 core, frontends: avatar refactoring:
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
689
6cf4bd6972c2 core, frontends: avatar refactoring:
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
690 nicknames.extend(plugin_nicknames)
6cf4bd6972c2 core, frontends: avatar refactoring:
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
691
6cf4bd6972c2 core, frontends: avatar refactoring:
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
692 if not nicknames:
6cf4bd6972c2 core, frontends: avatar refactoring:
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
693 if entity.user:
6cf4bd6972c2 core, frontends: avatar refactoring:
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
694 nicknames.append(entity.user.capitalize())
6cf4bd6972c2 core, frontends: avatar refactoring:
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
695 else:
6cf4bd6972c2 core, frontends: avatar refactoring:
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
696 nicknames.append(entity.userhost())
6cf4bd6972c2 core, frontends: avatar refactoring:
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
697
6cf4bd6972c2 core, frontends: avatar refactoring:
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
698 # we remove duplicates while preserving order with dict
6cf4bd6972c2 core, frontends: avatar refactoring:
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
699 return list(dict.fromkeys(nicknames))
6cf4bd6972c2 core, frontends: avatar refactoring:
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
700
4037
524856bd7b19 massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents: 3817
diff changeset
701 def nicknames_update_is_new_data(self, client, entity, cached_data, new_nicknames):
3254
6cf4bd6972c2 core, frontends: avatar refactoring:
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
702 return not set(new_nicknames).issubset(cached_data)
6cf4bd6972c2 core, frontends: avatar refactoring:
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
703
4037
524856bd7b19 massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents: 3817
diff changeset
704 async def description_get_post_treatment(
3817
998c5318230f plugin identity: make the plugin compatible with component + description:
Goffi <goffi@goffi.org>
parents: 3816
diff changeset
705 self,
998c5318230f plugin identity: make the plugin compatible with component + description:
Goffi <goffi@goffi.org>
parents: 3816
diff changeset
706 client: SatXMPPEntity,
998c5318230f plugin identity: make the plugin compatible with component + description:
Goffi <goffi@goffi.org>
parents: 3816
diff changeset
707 entity: jid.JID,
998c5318230f plugin identity: make the plugin compatible with component + description:
Goffi <goffi@goffi.org>
parents: 3816
diff changeset
708 plugin_description: List[str]
998c5318230f plugin identity: make the plugin compatible with component + description:
Goffi <goffi@goffi.org>
parents: 3816
diff changeset
709 ) -> str:
998c5318230f plugin identity: make the plugin compatible with component + description:
Goffi <goffi@goffi.org>
parents: 3816
diff changeset
710 """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
711 return '\n'.join(plugin_description)
998c5318230f plugin identity: make the plugin compatible with component + description:
Goffi <goffi@goffi.org>
parents: 3816
diff changeset
712
4037
524856bd7b19 massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents: 3817
diff changeset
713 def _get_identity(self, entity_s, metadata_filter, use_cache, profile):
3254
6cf4bd6972c2 core, frontends: avatar refactoring:
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
714 entity = jid.JID(entity_s)
4037
524856bd7b19 massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents: 3817
diff changeset
715 client = self.host.get_client(profile)
3254
6cf4bd6972c2 core, frontends: avatar refactoring:
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
716 d = defer.ensureDeferred(
4037
524856bd7b19 massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents: 3817
diff changeset
717 self.get_identity(client, entity, metadata_filter, use_cache))
3338
203a491fcd86 plugin identity: new methods `identitiesGet` and `identitiesBaseGet`
Goffi <goffi@goffi.org>
parents: 3326
diff changeset
718 d.addCallback(data_format.serialise)
3254
6cf4bd6972c2 core, frontends: avatar refactoring:
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
719 return d
6cf4bd6972c2 core, frontends: avatar refactoring:
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
720
4037
524856bd7b19 massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents: 3817
diff changeset
721 async def get_identity(
3817
998c5318230f plugin identity: make the plugin compatible with component + description:
Goffi <goffi@goffi.org>
parents: 3816
diff changeset
722 self,
998c5318230f plugin identity: make the plugin compatible with component + description:
Goffi <goffi@goffi.org>
parents: 3816
diff changeset
723 client: SatXMPPEntity,
998c5318230f plugin identity: make the plugin compatible with component + description:
Goffi <goffi@goffi.org>
parents: 3816
diff changeset
724 entity: Optional[jid.JID] = None,
998c5318230f plugin identity: make the plugin compatible with component + description:
Goffi <goffi@goffi.org>
parents: 3816
diff changeset
725 metadata_filter: Optional[List[str]] = None,
998c5318230f plugin identity: make the plugin compatible with component + description:
Goffi <goffi@goffi.org>
parents: 3816
diff changeset
726 use_cache: bool = True
998c5318230f plugin identity: make the plugin compatible with component + description:
Goffi <goffi@goffi.org>
parents: 3816
diff changeset
727 ) -> Dict[str, Any]:
2253
db468f24b9fc plugin identity: plugin identity first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
728 """Retrieve identity of an entity
db468f24b9fc plugin identity: plugin identity first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
729
3817
998c5318230f plugin identity: make the plugin compatible with component + description:
Goffi <goffi@goffi.org>
parents: 3816
diff changeset
730 @param entity: entity to check
998c5318230f plugin identity: make the plugin compatible with component + description:
Goffi <goffi@goffi.org>
parents: 3816
diff changeset
731 @param metadata_filter: if not None or empty, only return
3254
6cf4bd6972c2 core, frontends: avatar refactoring:
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
732 metadata in this filter
3817
998c5318230f plugin identity: make the plugin compatible with component + description:
Goffi <goffi@goffi.org>
parents: 3816
diff changeset
733 @param use_cache: if False, cache won't be checked
3254
6cf4bd6972c2 core, frontends: avatar refactoring:
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
734 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
735 @return: identity data
2253
db468f24b9fc plugin identity: plugin identity first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
736 """
db468f24b9fc plugin identity: plugin identity first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
737 id_data = {}
3254
6cf4bd6972c2 core, frontends: avatar refactoring:
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
738
6cf4bd6972c2 core, frontends: avatar refactoring:
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
739 if not metadata_filter:
6cf4bd6972c2 core, frontends: avatar refactoring:
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
740 metadata_names = self.metadata.keys()
2253
db468f24b9fc plugin identity: plugin identity first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
741 else:
3254
6cf4bd6972c2 core, frontends: avatar refactoring:
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
742 metadata_names = metadata_filter
2253
db468f24b9fc plugin identity: plugin identity first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
743
3254
6cf4bd6972c2 core, frontends: avatar refactoring:
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
744 for metadata_name in metadata_names:
6cf4bd6972c2 core, frontends: avatar refactoring:
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
745 id_data[metadata_name] = await self.get(
6cf4bd6972c2 core, frontends: avatar refactoring:
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
746 client, metadata_name, entity, use_cache)
6cf4bd6972c2 core, frontends: avatar refactoring:
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
747
6cf4bd6972c2 core, frontends: avatar refactoring:
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
748 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
749
4037
524856bd7b19 massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents: 3817
diff changeset
750 def _get_identities(self, entities_s, metadata_filter, profile):
3338
203a491fcd86 plugin identity: new methods `identitiesGet` and `identitiesBaseGet`
Goffi <goffi@goffi.org>
parents: 3326
diff changeset
751 entities = [jid.JID(e) for e in entities_s]
4037
524856bd7b19 massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents: 3817
diff changeset
752 client = self.host.get_client(profile)
524856bd7b19 massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents: 3817
diff changeset
753 d = defer.ensureDeferred(self.get_identities(client, entities, metadata_filter))
3338
203a491fcd86 plugin identity: new methods `identitiesGet` and `identitiesBaseGet`
Goffi <goffi@goffi.org>
parents: 3326
diff changeset
754 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
755 return d
203a491fcd86 plugin identity: new methods `identitiesGet` and `identitiesBaseGet`
Goffi <goffi@goffi.org>
parents: 3326
diff changeset
756
4037
524856bd7b19 massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents: 3817
diff changeset
757 async def get_identities(
3338
203a491fcd86 plugin identity: new methods `identitiesGet` and `identitiesBaseGet`
Goffi <goffi@goffi.org>
parents: 3326
diff changeset
758 self,
203a491fcd86 plugin identity: new methods `identitiesGet` and `identitiesBaseGet`
Goffi <goffi@goffi.org>
parents: 3326
diff changeset
759 client: SatXMPPEntity,
203a491fcd86 plugin identity: new methods `identitiesGet` and `identitiesBaseGet`
Goffi <goffi@goffi.org>
parents: 3326
diff changeset
760 entities: List[jid.JID],
203a491fcd86 plugin identity: new methods `identitiesGet` and `identitiesBaseGet`
Goffi <goffi@goffi.org>
parents: 3326
diff changeset
761 metadata_filter: Optional[List[str]] = None,
203a491fcd86 plugin identity: new methods `identitiesGet` and `identitiesBaseGet`
Goffi <goffi@goffi.org>
parents: 3326
diff changeset
762 ) -> dict:
203a491fcd86 plugin identity: new methods `identitiesGet` and `identitiesBaseGet`
Goffi <goffi@goffi.org>
parents: 3326
diff changeset
763 """Retrieve several identities at once
203a491fcd86 plugin identity: new methods `identitiesGet` and `identitiesBaseGet`
Goffi <goffi@goffi.org>
parents: 3326
diff changeset
764
203a491fcd86 plugin identity: new methods `identitiesGet` and `identitiesBaseGet`
Goffi <goffi@goffi.org>
parents: 3326
diff changeset
765 @param entities: entities from which identities must be retrieved
4037
524856bd7b19 massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents: 3817
diff changeset
766 @param metadata_filter: same as for [get_identity]
3338
203a491fcd86 plugin identity: new methods `identitiesGet` and `identitiesBaseGet`
Goffi <goffi@goffi.org>
parents: 3326
diff changeset
767 @return: identities metadata where key is jid
203a491fcd86 plugin identity: new methods `identitiesGet` and `identitiesBaseGet`
Goffi <goffi@goffi.org>
parents: 3326
diff changeset
768 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
769 result (and a warning will be logged)
203a491fcd86 plugin identity: new methods `identitiesGet` and `identitiesBaseGet`
Goffi <goffi@goffi.org>
parents: 3326
diff changeset
770 """
203a491fcd86 plugin identity: new methods `identitiesGet` and `identitiesBaseGet`
Goffi <goffi@goffi.org>
parents: 3326
diff changeset
771 identities = {}
203a491fcd86 plugin identity: new methods `identitiesGet` and `identitiesBaseGet`
Goffi <goffi@goffi.org>
parents: 3326
diff changeset
772 get_identity_list = []
203a491fcd86 plugin identity: new methods `identitiesGet` and `identitiesBaseGet`
Goffi <goffi@goffi.org>
parents: 3326
diff changeset
773 for entity_jid in entities:
203a491fcd86 plugin identity: new methods `identitiesGet` and `identitiesBaseGet`
Goffi <goffi@goffi.org>
parents: 3326
diff changeset
774 get_identity_list.append(
203a491fcd86 plugin identity: new methods `identitiesGet` and `identitiesBaseGet`
Goffi <goffi@goffi.org>
parents: 3326
diff changeset
775 defer.ensureDeferred(
4037
524856bd7b19 massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents: 3817
diff changeset
776 self.get_identity(
3338
203a491fcd86 plugin identity: new methods `identitiesGet` and `identitiesBaseGet`
Goffi <goffi@goffi.org>
parents: 3326
diff changeset
777 client,
203a491fcd86 plugin identity: new methods `identitiesGet` and `identitiesBaseGet`
Goffi <goffi@goffi.org>
parents: 3326
diff changeset
778 entity=entity_jid,
203a491fcd86 plugin identity: new methods `identitiesGet` and `identitiesBaseGet`
Goffi <goffi@goffi.org>
parents: 3326
diff changeset
779 metadata_filter=metadata_filter,
203a491fcd86 plugin identity: new methods `identitiesGet` and `identitiesBaseGet`
Goffi <goffi@goffi.org>
parents: 3326
diff changeset
780 )
203a491fcd86 plugin identity: new methods `identitiesGet` and `identitiesBaseGet`
Goffi <goffi@goffi.org>
parents: 3326
diff changeset
781 )
203a491fcd86 plugin identity: new methods `identitiesGet` and `identitiesBaseGet`
Goffi <goffi@goffi.org>
parents: 3326
diff changeset
782 )
203a491fcd86 plugin identity: new methods `identitiesGet` and `identitiesBaseGet`
Goffi <goffi@goffi.org>
parents: 3326
diff changeset
783 identities_result = await defer.DeferredList(get_identity_list)
203a491fcd86 plugin identity: new methods `identitiesGet` and `identitiesBaseGet`
Goffi <goffi@goffi.org>
parents: 3326
diff changeset
784 for idx, (success, identity) in enumerate(identities_result):
203a491fcd86 plugin identity: new methods `identitiesGet` and `identitiesBaseGet`
Goffi <goffi@goffi.org>
parents: 3326
diff changeset
785 entity_jid = entities[idx]
203a491fcd86 plugin identity: new methods `identitiesGet` and `identitiesBaseGet`
Goffi <goffi@goffi.org>
parents: 3326
diff changeset
786 if not success:
203a491fcd86 plugin identity: new methods `identitiesGet` and `identitiesBaseGet`
Goffi <goffi@goffi.org>
parents: 3326
diff changeset
787 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
788 else:
203a491fcd86 plugin identity: new methods `identitiesGet` and `identitiesBaseGet`
Goffi <goffi@goffi.org>
parents: 3326
diff changeset
789 identities[entity_jid] = identity
203a491fcd86 plugin identity: new methods `identitiesGet` and `identitiesBaseGet`
Goffi <goffi@goffi.org>
parents: 3326
diff changeset
790 return identities
203a491fcd86 plugin identity: new methods `identitiesGet` and `identitiesBaseGet`
Goffi <goffi@goffi.org>
parents: 3326
diff changeset
791
4037
524856bd7b19 massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents: 3817
diff changeset
792 def _get_base_identities(self, profile_key):
524856bd7b19 massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents: 3817
diff changeset
793 client = self.host.get_client(profile_key)
524856bd7b19 massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents: 3817
diff changeset
794 d = defer.ensureDeferred(self.get_base_identities(client))
3338
203a491fcd86 plugin identity: new methods `identitiesGet` and `identitiesBaseGet`
Goffi <goffi@goffi.org>
parents: 3326
diff changeset
795 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
796 return d
203a491fcd86 plugin identity: new methods `identitiesGet` and `identitiesBaseGet`
Goffi <goffi@goffi.org>
parents: 3326
diff changeset
797
4037
524856bd7b19 massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents: 3817
diff changeset
798 async def get_base_identities(
3338
203a491fcd86 plugin identity: new methods `identitiesGet` and `identitiesBaseGet`
Goffi <goffi@goffi.org>
parents: 3326
diff changeset
799 self,
203a491fcd86 plugin identity: new methods `identitiesGet` and `identitiesBaseGet`
Goffi <goffi@goffi.org>
parents: 3326
diff changeset
800 client: SatXMPPEntity,
203a491fcd86 plugin identity: new methods `identitiesGet` and `identitiesBaseGet`
Goffi <goffi@goffi.org>
parents: 3326
diff changeset
801 ) -> dict:
203a491fcd86 plugin identity: new methods `identitiesGet` and `identitiesBaseGet`
Goffi <goffi@goffi.org>
parents: 3326
diff changeset
802 """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
803
203a491fcd86 plugin identity: new methods `identitiesGet` and `identitiesBaseGet`
Goffi <goffi@goffi.org>
parents: 3326
diff changeset
804 @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
805
203a491fcd86 plugin identity: new methods `identitiesGet` and `identitiesBaseGet`
Goffi <goffi@goffi.org>
parents: 3326
diff changeset
806 """
3817
998c5318230f plugin identity: make the plugin compatible with component + description:
Goffi <goffi@goffi.org>
parents: 3816
diff changeset
807 if client.is_component:
998c5318230f plugin identity: make the plugin compatible with component + description:
Goffi <goffi@goffi.org>
parents: 3816
diff changeset
808 entities = [client.jid.userhostJID()]
998c5318230f plugin identity: make the plugin compatible with component + description:
Goffi <goffi@goffi.org>
parents: 3816
diff changeset
809 else:
4037
524856bd7b19 massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents: 3817
diff changeset
810 entities = client.roster.get_jids() + [client.jid.userhostJID()]
3338
203a491fcd86 plugin identity: new methods `identitiesGet` and `identitiesBaseGet`
Goffi <goffi@goffi.org>
parents: 3326
diff changeset
811
4037
524856bd7b19 massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents: 3817
diff changeset
812 return await self.get_identities(
3338
203a491fcd86 plugin identity: new methods `identitiesGet` and `identitiesBaseGet`
Goffi <goffi@goffi.org>
parents: 3326
diff changeset
813 client,
203a491fcd86 plugin identity: new methods `identitiesGet` and `identitiesBaseGet`
Goffi <goffi@goffi.org>
parents: 3326
diff changeset
814 entities,
203a491fcd86 plugin identity: new methods `identitiesGet` and `identitiesBaseGet`
Goffi <goffi@goffi.org>
parents: 3326
diff changeset
815 ['avatar', 'nicknames']
203a491fcd86 plugin identity: new methods `identitiesGet` and `identitiesBaseGet`
Goffi <goffi@goffi.org>
parents: 3326
diff changeset
816 )
203a491fcd86 plugin identity: new methods `identitiesGet` and `identitiesBaseGet`
Goffi <goffi@goffi.org>
parents: 3326
diff changeset
817
4037
524856bd7b19 massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents: 3817
diff changeset
818 def _set_identity(self, id_data_s, profile):
524856bd7b19 massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents: 3817
diff changeset
819 client = self.host.get_client(profile)
3254
6cf4bd6972c2 core, frontends: avatar refactoring:
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
820 id_data = data_format.deserialise(id_data_s)
4037
524856bd7b19 massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents: 3817
diff changeset
821 return defer.ensureDeferred(self.set_identity(client, id_data))
2253
db468f24b9fc plugin identity: plugin identity first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
822
4037
524856bd7b19 massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents: 3817
diff changeset
823 async def set_identity(self, client, id_data):
2253
db468f24b9fc plugin identity: plugin identity first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
824 """Update profile's identity
db468f24b9fc plugin identity: plugin identity first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
825
3817
998c5318230f plugin identity: make the plugin compatible with component + description:
Goffi <goffi@goffi.org>
parents: 3816
diff changeset
826 @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
827 """
3254
6cf4bd6972c2 core, frontends: avatar refactoring:
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
828 if not id_data.keys() <= self.metadata.keys():
6cf4bd6972c2 core, frontends: avatar refactoring:
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
829 raise ValueError(
6cf4bd6972c2 core, frontends: avatar refactoring:
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
830 f"Invalid metadata names: {id_data.keys() - self.metadata.keys()}")
6cf4bd6972c2 core, frontends: avatar refactoring:
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
831 for metadata_name, data in id_data.items():
6cf4bd6972c2 core, frontends: avatar refactoring:
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
832 try:
6cf4bd6972c2 core, frontends: avatar refactoring:
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
833 await self.set(client, metadata_name, data)
6cf4bd6972c2 core, frontends: avatar refactoring:
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
834 except Exception as e:
6cf4bd6972c2 core, frontends: avatar refactoring:
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
835 log.warning(
6cf4bd6972c2 core, frontends: avatar refactoring:
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
836 _("Can't set metadata {metadata_name!r}: {reason}")
6cf4bd6972c2 core, frontends: avatar refactoring:
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
837 .format(metadata_name=metadata_name, reason=e))