annotate libervia/backend/plugins/plugin_misc_identity.py @ 4340:ea72364131d5 default tip @

doc (components): Update Email Gateway documentation: A section has been added to explain how attachments are handled. fix 453
author Goffi <goffi@goffi.org>
date Tue, 03 Dec 2024 00:53:18 +0100
parents ff88a807852d
children
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
2253
db468f24b9fc plugin identity: plugin identity first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
46 log = getLogger(__name__)
db468f24b9fc plugin identity: plugin identity first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
47
db468f24b9fc plugin identity: plugin identity first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
48
3278
27d4b71e264a plugin identity: fixed update in avatarSetPostTreatment
Goffi <goffi@goffi.org>
parents: 3277
diff changeset
49 IMPORT_NAME = "IDENTITY"
27d4b71e264a plugin identity: fixed update in avatarSetPostTreatment
Goffi <goffi@goffi.org>
parents: 3277
diff changeset
50
27d4b71e264a plugin identity: fixed update in avatarSetPostTreatment
Goffi <goffi@goffi.org>
parents: 3277
diff changeset
51
2253
db468f24b9fc plugin identity: plugin identity first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
52 PLUGIN_INFO = {
db468f24b9fc plugin identity: plugin identity first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
53 C.PI_NAME: "Identity Plugin",
3278
27d4b71e264a plugin identity: fixed update in avatarSetPostTreatment
Goffi <goffi@goffi.org>
parents: 3277
diff changeset
54 C.PI_IMPORT_NAME: IMPORT_NAME,
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
55 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
56 C.PI_MODES: C.PLUG_MODE_BOTH,
2253
db468f24b9fc plugin identity: plugin identity first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
57 C.PI_PROTOCOLS: [],
3254
6cf4bd6972c2 core, frontends: avatar refactoring:
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
58 C.PI_DEPENDENCIES: [],
6cf4bd6972c2 core, frontends: avatar refactoring:
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
59 C.PI_RECOMMENDATIONS: ["XEP-0045"],
2253
db468f24b9fc plugin identity: plugin identity first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
60 C.PI_MAIN: "Identity",
db468f24b9fc plugin identity: plugin identity first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
61 C.PI_HANDLER: "no",
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
62 C.PI_DESCRIPTION: _("""Identity manager"""),
2253
db468f24b9fc plugin identity: plugin identity first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
63 }
db468f24b9fc plugin identity: plugin identity first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
64
3277
cf07641b764d plugin identity: fixed infinite loop on nicknames update
Goffi <goffi@goffi.org>
parents: 3262
diff changeset
65 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
66 AVATAR_DIM = (128, 128)
2253
db468f24b9fc plugin identity: plugin identity first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
67
3254
6cf4bd6972c2 core, frontends: avatar refactoring:
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
68
6cf4bd6972c2 core, frontends: avatar refactoring:
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
69 class Identity:
6cf4bd6972c2 core, frontends: avatar refactoring:
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
70
2253
db468f24b9fc plugin identity: plugin identity first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
71 def __init__(self, host):
3028
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 2771
diff changeset
72 log.info(_("Plugin Identity initialization"))
2253
db468f24b9fc plugin identity: plugin identity first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
73 self.host = host
3254
6cf4bd6972c2 core, frontends: avatar refactoring:
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
74 self._m = host.plugins.get("XEP-0045")
6cf4bd6972c2 core, frontends: avatar refactoring:
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
75 self.metadata = {
6cf4bd6972c2 core, frontends: avatar refactoring:
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
76 "avatar": {
6cf4bd6972c2 core, frontends: avatar refactoring:
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
77 "type": dict,
6cf4bd6972c2 core, frontends: avatar refactoring:
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
78 # 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
79 "set_data_filter": self.avatar_set_data_filter,
3254
6cf4bd6972c2 core, frontends: avatar refactoring:
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
80 # 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
81 "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
82 "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
83 "update_data_filter": self.avatar_update_data_filter,
3254
6cf4bd6972c2 core, frontends: avatar refactoring:
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
84 # 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
85 # (it is stored only for roster entities)
6cf4bd6972c2 core, frontends: avatar refactoring:
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
86 "store": True,
4212
5f2d496c633f core: get rid of `pickle`:
Goffi <goffi@goffi.org>
parents: 4071
diff changeset
87 "store_serialisation": self._avatar_ser,
4270
0d7bb4df2343 Reformatted code base using black.
Goffi <goffi@goffi.org>
parents: 4212
diff changeset
88 "store_deserialisation": self._avatar_deser,
3254
6cf4bd6972c2 core, frontends: avatar refactoring:
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
89 },
6cf4bd6972c2 core, frontends: avatar refactoring:
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
90 "nicknames": {
6cf4bd6972c2 core, frontends: avatar refactoring:
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
91 "type": list,
6cf4bd6972c2 core, frontends: avatar refactoring:
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
92 # accumulate all nicknames from all callbacks in a list instead
6cf4bd6972c2 core, frontends: avatar refactoring:
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
93 # of returning only the data from the first successful callback
6cf4bd6972c2 core, frontends: avatar refactoring:
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
94 "get_all": True,
6cf4bd6972c2 core, frontends: avatar refactoring:
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
95 # 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
96 "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
97 "update_is_new_data": self.nicknames_update_is_new_data,
3254
6cf4bd6972c2 core, frontends: avatar refactoring:
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
98 "store": True,
6cf4bd6972c2 core, frontends: avatar refactoring:
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
99 },
3817
998c5318230f plugin identity: make the plugin compatible with component + description:
Goffi <goffi@goffi.org>
parents: 3816
diff changeset
100 "description": {
998c5318230f plugin identity: make the plugin compatible with component + description:
Goffi <goffi@goffi.org>
parents: 3816
diff changeset
101 "type": str,
998c5318230f plugin identity: make the plugin compatible with component + description:
Goffi <goffi@goffi.org>
parents: 3816
diff changeset
102 "get_all": True,
4037
524856bd7b19 massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents: 3817
diff changeset
103 "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
104 "store": True,
4270
0d7bb4df2343 Reformatted code base using black.
Goffi <goffi@goffi.org>
parents: 4212
diff changeset
105 },
3254
6cf4bd6972c2 core, frontends: avatar refactoring:
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
106 }
4037
524856bd7b19 massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents: 3817
diff changeset
107 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
108 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
109 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
110 host.bridge.add_method(
524856bd7b19 massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents: 3817
diff changeset
111 "identity_get",
3028
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 2771
diff changeset
112 ".plugin",
3254
6cf4bd6972c2 core, frontends: avatar refactoring:
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
113 in_sign="sasbs",
6cf4bd6972c2 core, frontends: avatar refactoring:
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
114 out_sign="s",
4037
524856bd7b19 massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents: 3817
diff changeset
115 method=self._get_identity,
3028
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 2771
diff changeset
116 async_=True,
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
117 )
4037
524856bd7b19 massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents: 3817
diff changeset
118 host.bridge.add_method(
524856bd7b19 massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents: 3817
diff changeset
119 "identities_get",
3338
203a491fcd86 plugin identity: new methods `identitiesGet` and `identitiesBaseGet`
Goffi <goffi@goffi.org>
parents: 3326
diff changeset
120 ".plugin",
203a491fcd86 plugin identity: new methods `identitiesGet` and `identitiesBaseGet`
Goffi <goffi@goffi.org>
parents: 3326
diff changeset
121 in_sign="asass",
203a491fcd86 plugin identity: new methods `identitiesGet` and `identitiesBaseGet`
Goffi <goffi@goffi.org>
parents: 3326
diff changeset
122 out_sign="s",
4037
524856bd7b19 massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents: 3817
diff changeset
123 method=self._get_identities,
3338
203a491fcd86 plugin identity: new methods `identitiesGet` and `identitiesBaseGet`
Goffi <goffi@goffi.org>
parents: 3326
diff changeset
124 async_=True,
203a491fcd86 plugin identity: new methods `identitiesGet` and `identitiesBaseGet`
Goffi <goffi@goffi.org>
parents: 3326
diff changeset
125 )
4037
524856bd7b19 massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents: 3817
diff changeset
126 host.bridge.add_method(
524856bd7b19 massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents: 3817
diff changeset
127 "identities_base_get",
3338
203a491fcd86 plugin identity: new methods `identitiesGet` and `identitiesBaseGet`
Goffi <goffi@goffi.org>
parents: 3326
diff changeset
128 ".plugin",
203a491fcd86 plugin identity: new methods `identitiesGet` and `identitiesBaseGet`
Goffi <goffi@goffi.org>
parents: 3326
diff changeset
129 in_sign="s",
203a491fcd86 plugin identity: new methods `identitiesGet` and `identitiesBaseGet`
Goffi <goffi@goffi.org>
parents: 3326
diff changeset
130 out_sign="s",
4037
524856bd7b19 massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents: 3817
diff changeset
131 method=self._get_base_identities,
3338
203a491fcd86 plugin identity: new methods `identitiesGet` and `identitiesBaseGet`
Goffi <goffi@goffi.org>
parents: 3326
diff changeset
132 async_=True,
203a491fcd86 plugin identity: new methods `identitiesGet` and `identitiesBaseGet`
Goffi <goffi@goffi.org>
parents: 3326
diff changeset
133 )
4037
524856bd7b19 massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents: 3817
diff changeset
134 host.bridge.add_method(
524856bd7b19 massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents: 3817
diff changeset
135 "identity_set",
3028
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 2771
diff changeset
136 ".plugin",
3254
6cf4bd6972c2 core, frontends: avatar refactoring:
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
137 in_sign="ss",
3028
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 2771
diff changeset
138 out_sign="",
4037
524856bd7b19 massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents: 3817
diff changeset
139 method=self._set_identity,
3028
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 2771
diff changeset
140 async_=True,
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
141 )
4037
524856bd7b19 massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents: 3817
diff changeset
142 host.bridge.add_method(
524856bd7b19 massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents: 3817
diff changeset
143 "avatar_get",
3254
6cf4bd6972c2 core, frontends: avatar refactoring:
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
144 ".plugin",
6cf4bd6972c2 core, frontends: avatar refactoring:
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
145 in_sign="sbs",
6cf4bd6972c2 core, frontends: avatar refactoring:
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
146 out_sign="s",
6cf4bd6972c2 core, frontends: avatar refactoring:
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
147 method=self._getAvatar,
6cf4bd6972c2 core, frontends: avatar refactoring:
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
148 async_=True,
6cf4bd6972c2 core, frontends: avatar refactoring:
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
149 )
4037
524856bd7b19 massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents: 3817
diff changeset
150 host.bridge.add_method(
524856bd7b19 massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents: 3817
diff changeset
151 "avatar_set",
3254
6cf4bd6972c2 core, frontends: avatar refactoring:
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
152 ".plugin",
6cf4bd6972c2 core, frontends: avatar refactoring:
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
153 in_sign="sss",
6cf4bd6972c2 core, frontends: avatar refactoring:
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
154 out_sign="",
4037
524856bd7b19 massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents: 3817
diff changeset
155 method=self._set_avatar,
3254
6cf4bd6972c2 core, frontends: avatar refactoring:
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
156 async_=True,
6cf4bd6972c2 core, frontends: avatar refactoring:
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
157 )
2253
db468f24b9fc plugin identity: plugin identity first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
158
4037
524856bd7b19 massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents: 3817
diff changeset
159 async def profile_connecting(self, client):
3277
cf07641b764d plugin identity: fixed infinite loop on nicknames update
Goffi <goffi@goffi.org>
parents: 3262
diff changeset
160 client._identity_update_lock = []
3254
6cf4bd6972c2 core, frontends: avatar refactoring:
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
161 # we restore known identities from database
6cf4bd6972c2 core, frontends: avatar refactoring:
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
162 client._identity_storage = persistent.LazyPersistentBinaryDict(
4270
0d7bb4df2343 Reformatted code base using black.
Goffi <goffi@goffi.org>
parents: 4212
diff changeset
163 "identity", client.profile
0d7bb4df2343 Reformatted code base using black.
Goffi <goffi@goffi.org>
parents: 4212
diff changeset
164 )
3254
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():
4270
0d7bb4df2343 Reformatted code base using black.
Goffi <goffi@goffi.org>
parents: 4212
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
4270
0d7bb4df2343 Reformatted code base using black.
Goffi <goffi@goffi.org>
parents: 4212
diff changeset
184 if name == "avatar":
3254
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:
4270
0d7bb4df2343 Reformatted code base using black.
Goffi <goffi@goffi.org>
parents: 4212
diff changeset
187 cache_uid = value["cache_uid"]
3254
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
4270
0d7bb4df2343 Reformatted code base using black.
Goffi <goffi@goffi.org>
parents: 4212
diff changeset
190 filename = value["filename"]
3326
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: "
4270
0d7bb4df2343 Reformatted code base using black.
Goffi <goffi@goffi.org>
parents: 4212
diff changeset
196 f"{value}"
0d7bb4df2343 Reformatted code base using black.
Goffi <goffi@goffi.org>
parents: 4212
diff changeset
197 )
3254
6cf4bd6972c2 core, frontends: avatar refactoring:
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
198 to_delete.append(key)
6cf4bd6972c2 core, frontends: avatar refactoring:
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
199 continue
4037
524856bd7b19 massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents: 3817
diff changeset
200 cache = self.host.common_cache.get_metadata(cache_uid)
3254
6cf4bd6972c2 core, frontends: avatar refactoring:
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
201 if cache is None:
6cf4bd6972c2 core, frontends: avatar refactoring:
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
202 log.debug(
4270
0d7bb4df2343 Reformatted code base using black.
Goffi <goffi@goffi.org>
parents: 4212
diff changeset
203 f"purging avatar for {entity}: it is not in cache anymore"
0d7bb4df2343 Reformatted code base using black.
Goffi <goffi@goffi.org>
parents: 4212
diff changeset
204 )
3254
6cf4bd6972c2 core, frontends: avatar refactoring:
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
205 to_delete.append(key)
6cf4bd6972c2 core, frontends: avatar refactoring:
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
206 continue
6cf4bd6972c2 core, frontends: avatar refactoring:
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
207
4270
0d7bb4df2343 Reformatted code base using black.
Goffi <goffi@goffi.org>
parents: 4212
diff changeset
208 self.host.memory.update_entity_data(client, entity, name, value, silent=True)
3254
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(
4270
0d7bb4df2343 Reformatted code base using black.
Goffi <goffi@goffi.org>
parents: 4212
diff changeset
222 client, IMPORT_NAME, "nicknames", [roster_item.name], roster_item.jid
3254
6cf4bd6972c2 core, frontends: avatar refactoring:
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
223 )
6cf4bd6972c2 core, frontends: avatar refactoring:
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
224 )
6cf4bd6972c2 core, frontends: avatar refactoring:
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
225 return True
6cf4bd6972c2 core, frontends: avatar refactoring:
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
226
3277
cf07641b764d plugin identity: fixed infinite loop on nicknames update
Goffi <goffi@goffi.org>
parents: 3262
diff changeset
227 def register(
4270
0d7bb4df2343 Reformatted code base using black.
Goffi <goffi@goffi.org>
parents: 4212
diff changeset
228 self,
0d7bb4df2343 Reformatted code base using black.
Goffi <goffi@goffi.org>
parents: 4212
diff changeset
229 origin: str,
0d7bb4df2343 Reformatted code base using black.
Goffi <goffi@goffi.org>
parents: 4212
diff changeset
230 metadata_name: str,
0d7bb4df2343 Reformatted code base using black.
Goffi <goffi@goffi.org>
parents: 4212
diff changeset
231 cb_get: Union[Coroutine, defer.Deferred],
0d7bb4df2343 Reformatted code base using black.
Goffi <goffi@goffi.org>
parents: 4212
diff changeset
232 cb_set: Union[Coroutine, defer.Deferred],
0d7bb4df2343 Reformatted code base using black.
Goffi <goffi@goffi.org>
parents: 4212
diff changeset
233 priority: int = 0,
0d7bb4df2343 Reformatted code base using black.
Goffi <goffi@goffi.org>
parents: 4212
diff changeset
234 ):
3254
6cf4bd6972c2 core, frontends: avatar refactoring:
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
235 """Register callbacks to handle identity metadata
6cf4bd6972c2 core, frontends: avatar refactoring:
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
236
3277
cf07641b764d plugin identity: fixed infinite loop on nicknames update
Goffi <goffi@goffi.org>
parents: 3262
diff changeset
237 @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
238 @param metadata_name: name of metadata can be:
3254
6cf4bd6972c2 core, frontends: avatar refactoring:
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
239 - avatar
6cf4bd6972c2 core, frontends: avatar refactoring:
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
240 - nicknames
3277
cf07641b764d plugin identity: fixed infinite loop on nicknames update
Goffi <goffi@goffi.org>
parents: 3262
diff changeset
241 @param cb_get: method to retrieve a metadata
3254
6cf4bd6972c2 core, frontends: avatar refactoring:
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
242 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
243 @param cb_set: method to set a metadata
3254
6cf4bd6972c2 core, frontends: avatar refactoring:
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
244 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
245 @param priority: priority of this method for the given metadata.
3254
6cf4bd6972c2 core, frontends: avatar refactoring:
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
246 methods with bigger priorities will be called first
6cf4bd6972c2 core, frontends: avatar refactoring:
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
247 """
6cf4bd6972c2 core, frontends: avatar refactoring:
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
248 if not metadata_name in self.metadata.keys():
6cf4bd6972c2 core, frontends: avatar refactoring:
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
249 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
250 callback = Callback(origin=origin, get=cb_get, set=cb_set, priority=priority)
4270
0d7bb4df2343 Reformatted code base using black.
Goffi <goffi@goffi.org>
parents: 4212
diff changeset
251 cb_list = self.metadata[metadata_name].setdefault("callbacks", [])
3254
6cf4bd6972c2 core, frontends: avatar refactoring:
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
252 cb_list.append(callback)
6cf4bd6972c2 core, frontends: avatar refactoring:
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
253 cb_list.sort(key=lambda c: c.priority, reverse=True)
6cf4bd6972c2 core, frontends: avatar refactoring:
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
254
4037
524856bd7b19 massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents: 3817
diff changeset
255 def get_identity_jid(self, client, peer_jid):
3254
6cf4bd6972c2 core, frontends: avatar refactoring:
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
256 """Return jid to use to set identity metadata
6cf4bd6972c2 core, frontends: avatar refactoring:
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
257
6cf4bd6972c2 core, frontends: avatar refactoring:
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
258 if 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
259 otherwise bare jid will be used
6cf4bd6972c2 core, frontends: avatar refactoring:
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
260 if None, bare jid of profile will be used
6cf4bd6972c2 core, frontends: avatar refactoring:
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
261 @return (jid.JID): jid to use for avatar
6cf4bd6972c2 core, frontends: avatar refactoring:
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
262 """
6cf4bd6972c2 core, frontends: avatar refactoring:
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
263 if peer_jid is None:
6cf4bd6972c2 core, frontends: avatar refactoring:
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
264 return client.jid.userhostJID()
6cf4bd6972c2 core, frontends: avatar refactoring:
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
265 if self._m is None:
6cf4bd6972c2 core, frontends: avatar refactoring:
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
266 return peer_jid.userhostJID()
6cf4bd6972c2 core, frontends: avatar refactoring:
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
267 else:
4037
524856bd7b19 massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents: 3817
diff changeset
268 return self._m.get_bare_or_full(client, peer_jid)
3254
6cf4bd6972c2 core, frontends: avatar refactoring:
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
269
4037
524856bd7b19 massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents: 3817
diff changeset
270 def check_type(self, metadata_name, value):
3254
6cf4bd6972c2 core, frontends: avatar refactoring:
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
271 """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
272 value_type = self.metadata[metadata_name]["type"]
6cf4bd6972c2 core, frontends: avatar refactoring:
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
273 if not isinstance(value, value_type):
6cf4bd6972c2 core, frontends: avatar refactoring:
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
274 raise ValueError(
6cf4bd6972c2 core, frontends: avatar refactoring:
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
275 f"{value} has wrong type: it is {type(value)} while {value_type} was "
4270
0d7bb4df2343 Reformatted code base using black.
Goffi <goffi@goffi.org>
parents: 4212
diff changeset
276 f"expected"
0d7bb4df2343 Reformatted code base using black.
Goffi <goffi@goffi.org>
parents: 4212
diff changeset
277 )
3254
6cf4bd6972c2 core, frontends: avatar refactoring:
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
278
4037
524856bd7b19 massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents: 3817
diff changeset
279 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
280 """Return the type the requested field
998c5318230f plugin identity: make the plugin compatible with component + description:
Goffi <goffi@goffi.org>
parents: 3816
diff changeset
281
998c5318230f plugin identity: make the plugin compatible with component + description:
Goffi <goffi@goffi.org>
parents: 3816
diff changeset
282 @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
283 @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
284 """
998c5318230f plugin identity: make the plugin compatible with component + description:
Goffi <goffi@goffi.org>
parents: 3816
diff changeset
285 return self.metadata[metadata_name]["type"]
998c5318230f plugin identity: make the plugin compatible with component + description:
Goffi <goffi@goffi.org>
parents: 3816
diff changeset
286
3277
cf07641b764d plugin identity: fixed infinite loop on nicknames update
Goffi <goffi@goffi.org>
parents: 3262
diff changeset
287 async def get(
4270
0d7bb4df2343 Reformatted code base using black.
Goffi <goffi@goffi.org>
parents: 4212
diff changeset
288 self,
0d7bb4df2343 Reformatted code base using black.
Goffi <goffi@goffi.org>
parents: 4212
diff changeset
289 client: SatXMPPEntity,
0d7bb4df2343 Reformatted code base using black.
Goffi <goffi@goffi.org>
parents: 4212
diff changeset
290 metadata_name: str,
0d7bb4df2343 Reformatted code base using black.
Goffi <goffi@goffi.org>
parents: 4212
diff changeset
291 entity: Optional[jid.JID],
0d7bb4df2343 Reformatted code base using black.
Goffi <goffi@goffi.org>
parents: 4212
diff changeset
292 use_cache: bool = True,
0d7bb4df2343 Reformatted code base using black.
Goffi <goffi@goffi.org>
parents: 4212
diff changeset
293 prefilled_values: Optional[Dict[str, Any]] = None,
0d7bb4df2343 Reformatted code base using black.
Goffi <goffi@goffi.org>
parents: 4212
diff changeset
294 ):
3254
6cf4bd6972c2 core, frontends: avatar refactoring:
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
295 """Retrieve identity metadata of an entity
6cf4bd6972c2 core, frontends: avatar refactoring:
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
296
6cf4bd6972c2 core, frontends: avatar refactoring:
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
297 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
298 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
299 @param metadata_name: name of the metadata
3254
6cf4bd6972c2 core, frontends: avatar refactoring:
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
300 must be one of self.metadata key
6cf4bd6972c2 core, frontends: avatar refactoring:
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
301 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
302 @param entity: entity for which avatar is requested
3254
6cf4bd6972c2 core, frontends: avatar refactoring:
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
303 None to use profile's jid
3277
cf07641b764d plugin identity: fixed infinite loop on nicknames update
Goffi <goffi@goffi.org>
parents: 3262
diff changeset
304 @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
305 @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
306 """
4037
524856bd7b19 massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents: 3817
diff changeset
307 entity = self.get_identity_jid(client, entity)
3254
6cf4bd6972c2 core, frontends: avatar refactoring:
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
308 try:
6cf4bd6972c2 core, frontends: avatar refactoring:
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
309 metadata = self.metadata[metadata_name]
6cf4bd6972c2 core, frontends: avatar refactoring:
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
310 except KeyError:
6cf4bd6972c2 core, frontends: avatar refactoring:
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
311 raise ValueError(f"Invalid metadata name: {metadata_name!r}")
4270
0d7bb4df2343 Reformatted code base using black.
Goffi <goffi@goffi.org>
parents: 4212
diff changeset
312 get_all = metadata.get("get_all", False)
3254
6cf4bd6972c2 core, frontends: avatar refactoring:
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
313 if use_cache:
6cf4bd6972c2 core, frontends: avatar refactoring:
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
314 try:
4270
0d7bb4df2343 Reformatted code base using black.
Goffi <goffi@goffi.org>
parents: 4212
diff changeset
315 data = self.host.memory.get_entity_datum(client, entity, metadata_name)
3254
6cf4bd6972c2 core, frontends: avatar refactoring:
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
316 except (KeyError, exceptions.UnknownEntityError):
6cf4bd6972c2 core, frontends: avatar refactoring:
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
317 pass
6cf4bd6972c2 core, frontends: avatar refactoring:
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
318 else:
6cf4bd6972c2 core, frontends: avatar refactoring:
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
319 return data
6cf4bd6972c2 core, frontends: avatar refactoring:
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
320
6cf4bd6972c2 core, frontends: avatar refactoring:
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
321 try:
4270
0d7bb4df2343 Reformatted code base using black.
Goffi <goffi@goffi.org>
parents: 4212
diff changeset
322 callbacks = metadata["callbacks"]
3254
6cf4bd6972c2 core, frontends: avatar refactoring:
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
323 except KeyError:
4270
0d7bb4df2343 Reformatted code base using black.
Goffi <goffi@goffi.org>
parents: 4212
diff changeset
324 log.warning(
0d7bb4df2343 Reformatted code base using black.
Goffi <goffi@goffi.org>
parents: 4212
diff changeset
325 _("No callback registered for {metadata_name}").format(
0d7bb4df2343 Reformatted code base using black.
Goffi <goffi@goffi.org>
parents: 4212
diff changeset
326 metadata_name=metadata_name
0d7bb4df2343 Reformatted code base using black.
Goffi <goffi@goffi.org>
parents: 4212
diff changeset
327 )
0d7bb4df2343 Reformatted code base using black.
Goffi <goffi@goffi.org>
parents: 4212
diff changeset
328 )
3254
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(
4270
0d7bb4df2343 Reformatted code base using black.
Goffi <goffi@goffi.org>
parents: 4212
diff changeset
335 "prefilled_values can only be used when `get_all` is set"
0d7bb4df2343 Reformatted code base using black.
Goffi <goffi@goffi.org>
parents: 4212
diff changeset
336 )
3254
6cf4bd6972c2 core, frontends: avatar refactoring:
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
337
6cf4bd6972c2 core, frontends: avatar refactoring:
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
338 for callback in callbacks:
6cf4bd6972c2 core, frontends: avatar refactoring:
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
339 try:
3277
cf07641b764d plugin identity: fixed infinite loop on nicknames update
Goffi <goffi@goffi.org>
parents: 3262
diff changeset
340 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
341 data = prefilled_values[callback.origin]
cf07641b764d plugin identity: fixed infinite loop on nicknames update
Goffi <goffi@goffi.org>
parents: 3262
diff changeset
342 log.debug(
cf07641b764d plugin identity: fixed infinite loop on nicknames update
Goffi <goffi@goffi.org>
parents: 3262
diff changeset
343 f"using prefilled values {data!r} for {metadata_name} with "
4270
0d7bb4df2343 Reformatted code base using black.
Goffi <goffi@goffi.org>
parents: 4212
diff changeset
344 f"{callback.origin}"
0d7bb4df2343 Reformatted code base using black.
Goffi <goffi@goffi.org>
parents: 4212
diff changeset
345 )
3277
cf07641b764d plugin identity: fixed infinite loop on nicknames update
Goffi <goffi@goffi.org>
parents: 3262
diff changeset
346 else:
cf07641b764d plugin identity: fixed infinite loop on nicknames update
Goffi <goffi@goffi.org>
parents: 3262
diff changeset
347 data = await defer.ensureDeferred(callback.get(client, entity))
3254
6cf4bd6972c2 core, frontends: avatar refactoring:
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
348 except exceptions.CancelError:
6cf4bd6972c2 core, frontends: avatar refactoring:
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
349 continue
6cf4bd6972c2 core, frontends: avatar refactoring:
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
350 except Exception as e:
6cf4bd6972c2 core, frontends: avatar refactoring:
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
351 log.warning(
4270
0d7bb4df2343 Reformatted code base using black.
Goffi <goffi@goffi.org>
parents: 4212
diff changeset
352 _(
0d7bb4df2343 Reformatted code base using black.
Goffi <goffi@goffi.org>
parents: 4212
diff changeset
353 "Error while trying to get {metadata_name} with {callback}: {e}"
0d7bb4df2343 Reformatted code base using black.
Goffi <goffi@goffi.org>
parents: 4212
diff changeset
354 ).format(callback=callback.get, metadata_name=metadata_name, e=e)
0d7bb4df2343 Reformatted code base using black.
Goffi <goffi@goffi.org>
parents: 4212
diff changeset
355 )
3254
6cf4bd6972c2 core, frontends: avatar refactoring:
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
356 else:
6cf4bd6972c2 core, frontends: avatar refactoring:
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
357 if data:
4037
524856bd7b19 massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents: 3817
diff changeset
358 self.check_type(metadata_name, data)
3254
6cf4bd6972c2 core, frontends: avatar refactoring:
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
359 if get_all:
3817
998c5318230f plugin identity: make the plugin compatible with component + description:
Goffi <goffi@goffi.org>
parents: 3816
diff changeset
360 if isinstance(data, list):
998c5318230f plugin identity: make the plugin compatible with component + description:
Goffi <goffi@goffi.org>
parents: 3816
diff changeset
361 all_data.extend(data)
998c5318230f plugin identity: make the plugin compatible with component + description:
Goffi <goffi@goffi.org>
parents: 3816
diff changeset
362 else:
998c5318230f plugin identity: make the plugin compatible with component + description:
Goffi <goffi@goffi.org>
parents: 3816
diff changeset
363 all_data.append(data)
3254
6cf4bd6972c2 core, frontends: avatar refactoring:
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
364 else:
6cf4bd6972c2 core, frontends: avatar refactoring:
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
365 break
6cf4bd6972c2 core, frontends: avatar refactoring:
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
366 else:
6cf4bd6972c2 core, frontends: avatar refactoring:
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
367 data = None
6cf4bd6972c2 core, frontends: avatar refactoring:
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
368
6cf4bd6972c2 core, frontends: avatar refactoring:
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
369 if get_all:
6cf4bd6972c2 core, frontends: avatar refactoring:
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
370 data = all_data
6cf4bd6972c2 core, frontends: avatar refactoring:
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
371
6cf4bd6972c2 core, frontends: avatar refactoring:
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
372 post_treatment = metadata.get("get_post_treatment")
6cf4bd6972c2 core, frontends: avatar refactoring:
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
373 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
374 data = await utils.as_deferred(post_treatment, client, entity, data)
3254
6cf4bd6972c2 core, frontends: avatar refactoring:
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
375
4270
0d7bb4df2343 Reformatted code base using black.
Goffi <goffi@goffi.org>
parents: 4212
diff changeset
376 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
377
4270
0d7bb4df2343 Reformatted code base using black.
Goffi <goffi@goffi.org>
parents: 4212
diff changeset
378 if metadata.get("store", False):
4212
5f2d496c633f core: get rid of `pickle`:
Goffi <goffi@goffi.org>
parents: 4071
diff changeset
379 if data is not None:
5f2d496c633f core: get rid of `pickle`:
Goffi <goffi@goffi.org>
parents: 4071
diff changeset
380 ser_method = metadata.get("store_serialisation")
5f2d496c633f core: get rid of `pickle`:
Goffi <goffi@goffi.org>
parents: 4071
diff changeset
381 if ser_method is not None:
5f2d496c633f core: get rid of `pickle`:
Goffi <goffi@goffi.org>
parents: 4071
diff changeset
382 data = ser_method(data)
3254
6cf4bd6972c2 core, frontends: avatar refactoring:
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
383 key = f"{entity}\n{metadata_name}"
6cf4bd6972c2 core, frontends: avatar refactoring:
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
384 await client._identity_storage.aset(key, data)
6cf4bd6972c2 core, frontends: avatar refactoring:
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
385
6cf4bd6972c2 core, frontends: avatar refactoring:
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
386 return data
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 async def set(self, client, metadata_name, data, entity=None):
6cf4bd6972c2 core, frontends: avatar refactoring:
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
389 """Set identity metadata for an entity
6cf4bd6972c2 core, frontends: avatar refactoring:
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
390
6cf4bd6972c2 core, frontends: avatar refactoring:
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
391 Registered callbacks will be tried in priority order (bigger to lower)
6cf4bd6972c2 core, frontends: avatar refactoring:
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
392 @param metadata_name(str): name of the metadata
6cf4bd6972c2 core, frontends: avatar refactoring:
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
393 must be one of self.metadata key
6cf4bd6972c2 core, frontends: avatar refactoring:
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
394 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
395 @param data(object): value to set
6cf4bd6972c2 core, frontends: avatar refactoring:
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
396 @param entity(jid.JID, None): entity for which avatar is requested
6cf4bd6972c2 core, frontends: avatar refactoring:
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
397 None to use profile's jid
6cf4bd6972c2 core, frontends: avatar refactoring:
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
398 """
4037
524856bd7b19 massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents: 3817
diff changeset
399 entity = self.get_identity_jid(client, entity)
3254
6cf4bd6972c2 core, frontends: avatar refactoring:
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
400 metadata = self.metadata[metadata_name]
6cf4bd6972c2 core, frontends: avatar refactoring:
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
401 data_filter = metadata.get("set_data_filter")
6cf4bd6972c2 core, frontends: avatar refactoring:
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
402 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
403 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
404 self.check_type(metadata_name, data)
3254
6cf4bd6972c2 core, frontends: avatar refactoring:
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
405
6cf4bd6972c2 core, frontends: avatar refactoring:
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
406 try:
4270
0d7bb4df2343 Reformatted code base using black.
Goffi <goffi@goffi.org>
parents: 4212
diff changeset
407 callbacks = metadata["callbacks"]
3254
6cf4bd6972c2 core, frontends: avatar refactoring:
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
408 except KeyError:
4270
0d7bb4df2343 Reformatted code base using black.
Goffi <goffi@goffi.org>
parents: 4212
diff changeset
409 log.warning(
0d7bb4df2343 Reformatted code base using black.
Goffi <goffi@goffi.org>
parents: 4212
diff changeset
410 _("No callback registered for {metadata_name}").format(
0d7bb4df2343 Reformatted code base using black.
Goffi <goffi@goffi.org>
parents: 4212
diff changeset
411 metadata_name=metadata_name
0d7bb4df2343 Reformatted code base using black.
Goffi <goffi@goffi.org>
parents: 4212
diff changeset
412 )
0d7bb4df2343 Reformatted code base using black.
Goffi <goffi@goffi.org>
parents: 4212
diff changeset
413 )
3254
6cf4bd6972c2 core, frontends: avatar refactoring:
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
414 return exceptions.FeatureNotFound(f"Can't set {metadata_name} for {entity}")
6cf4bd6972c2 core, frontends: avatar refactoring:
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
415
6cf4bd6972c2 core, frontends: avatar refactoring:
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
416 for callback in callbacks:
6cf4bd6972c2 core, frontends: avatar refactoring:
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
417 try:
6cf4bd6972c2 core, frontends: avatar refactoring:
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
418 await defer.ensureDeferred(callback.set(client, data, entity))
6cf4bd6972c2 core, frontends: avatar refactoring:
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
419 except exceptions.CancelError:
6cf4bd6972c2 core, frontends: avatar refactoring:
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
420 continue
6cf4bd6972c2 core, frontends: avatar refactoring:
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
421 except Exception as e:
6cf4bd6972c2 core, frontends: avatar refactoring:
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
422 log.warning(
4270
0d7bb4df2343 Reformatted code base using black.
Goffi <goffi@goffi.org>
parents: 4212
diff changeset
423 _(
0d7bb4df2343 Reformatted code base using black.
Goffi <goffi@goffi.org>
parents: 4212
diff changeset
424 "Error while trying to set {metadata_name} with {callback}: {e}"
0d7bb4df2343 Reformatted code base using black.
Goffi <goffi@goffi.org>
parents: 4212
diff changeset
425 ).format(callback=callback.set, metadata_name=metadata_name, e=e)
0d7bb4df2343 Reformatted code base using black.
Goffi <goffi@goffi.org>
parents: 4212
diff changeset
426 )
3254
6cf4bd6972c2 core, frontends: avatar refactoring:
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
427 else:
6cf4bd6972c2 core, frontends: avatar refactoring:
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
428 break
6cf4bd6972c2 core, frontends: avatar refactoring:
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
429 else:
6cf4bd6972c2 core, frontends: avatar refactoring:
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
430 raise exceptions.FeatureNotFound(f"Can't set {metadata_name} for {entity}")
6cf4bd6972c2 core, frontends: avatar refactoring:
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
431
6cf4bd6972c2 core, frontends: avatar refactoring:
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
432 post_treatment = metadata.get("set_post_treatment")
6cf4bd6972c2 core, frontends: avatar refactoring:
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
433 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
434 await utils.as_deferred(post_treatment, client, entity, data)
3254
6cf4bd6972c2 core, frontends: avatar refactoring:
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
435
3277
cf07641b764d plugin identity: fixed infinite loop on nicknames update
Goffi <goffi@goffi.org>
parents: 3262
diff changeset
436 async def update(
3817
998c5318230f plugin identity: make the plugin compatible with component + description:
Goffi <goffi@goffi.org>
parents: 3816
diff changeset
437 self,
998c5318230f plugin identity: make the plugin compatible with component + description:
Goffi <goffi@goffi.org>
parents: 3816
diff changeset
438 client: SatXMPPEntity,
998c5318230f plugin identity: make the plugin compatible with component + description:
Goffi <goffi@goffi.org>
parents: 3816
diff changeset
439 origin: str,
998c5318230f plugin identity: make the plugin compatible with component + description:
Goffi <goffi@goffi.org>
parents: 3816
diff changeset
440 metadata_name: str,
998c5318230f plugin identity: make the plugin compatible with component + description:
Goffi <goffi@goffi.org>
parents: 3816
diff changeset
441 data: Any,
4270
0d7bb4df2343 Reformatted code base using black.
Goffi <goffi@goffi.org>
parents: 4212
diff changeset
442 entity: Optional[jid.JID],
3817
998c5318230f plugin identity: make the plugin compatible with component + description:
Goffi <goffi@goffi.org>
parents: 3816
diff changeset
443 ):
3254
6cf4bd6972c2 core, frontends: avatar refactoring:
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
444 """Update a metadata in cache
6cf4bd6972c2 core, frontends: avatar refactoring:
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
445
6cf4bd6972c2 core, frontends: avatar refactoring:
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
446 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
447 @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
448 """
4037
524856bd7b19 massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents: 3817
diff changeset
449 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
450 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
451 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
452 return
3254
6cf4bd6972c2 core, frontends: avatar refactoring:
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
453 metadata = self.metadata[metadata_name]
6cf4bd6972c2 core, frontends: avatar refactoring:
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
454
6cf4bd6972c2 core, frontends: avatar refactoring:
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
455 try:
4270
0d7bb4df2343 Reformatted code base using black.
Goffi <goffi@goffi.org>
parents: 4212
diff changeset
456 cached_data = self.host.memory.get_entity_datum(client, entity, metadata_name)
3254
6cf4bd6972c2 core, frontends: avatar refactoring:
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
457 except (KeyError, exceptions.UnknownEntityError):
6cf4bd6972c2 core, frontends: avatar refactoring:
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
458 # metadata is not cached, we do the update
6cf4bd6972c2 core, frontends: avatar refactoring:
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
459 pass
6cf4bd6972c2 core, frontends: avatar refactoring:
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
460 else:
6cf4bd6972c2 core, frontends: avatar refactoring:
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
461 # 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
462 try:
6cf4bd6972c2 core, frontends: avatar refactoring:
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
463 update_is_new_data = metadata["update_is_new_data"]
6cf4bd6972c2 core, frontends: avatar refactoring:
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
464 except KeyError:
4037
524856bd7b19 massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents: 3817
diff changeset
465 update_is_new_data = self.default_update_is_new_data
3254
6cf4bd6972c2 core, frontends: avatar refactoring:
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
466
3257
704dada41df0 plugin identity: fixed update with `get_all` + better new data detection for avatar:
Goffi <goffi@goffi.org>
parents: 3254
diff changeset
467 if data is None:
3254
6cf4bd6972c2 core, frontends: avatar refactoring:
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
468 if cached_data is None:
6cf4bd6972c2 core, frontends: avatar refactoring:
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
469 log.debug(
6cf4bd6972c2 core, frontends: avatar refactoring:
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
470 f"{metadata_name} for {entity} is already disabled, nothing to "
4270
0d7bb4df2343 Reformatted code base using black.
Goffi <goffi@goffi.org>
parents: 4212
diff changeset
471 f"do"
0d7bb4df2343 Reformatted code base using black.
Goffi <goffi@goffi.org>
parents: 4212
diff changeset
472 )
3257
704dada41df0 plugin identity: fixed update with `get_all` + better new data detection for avatar:
Goffi <goffi@goffi.org>
parents: 3254
diff changeset
473 return
3262
aa71f1d40300 plugin identity: fixed update when cached data is None
Goffi <goffi@goffi.org>
parents: 3257
diff changeset
474 elif cached_data is None:
aa71f1d40300 plugin identity: fixed update when cached data is None
Goffi <goffi@goffi.org>
parents: 3257
diff changeset
475 pass
3257
704dada41df0 plugin identity: fixed update with `get_all` + better new data detection for avatar:
Goffi <goffi@goffi.org>
parents: 3254
diff changeset
476 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
477 log.debug(
4270
0d7bb4df2343 Reformatted code base using black.
Goffi <goffi@goffi.org>
parents: 4212
diff changeset
478 f"{metadata_name} for {entity} is already in cache, nothing to " f"do"
0d7bb4df2343 Reformatted code base using black.
Goffi <goffi@goffi.org>
parents: 4212
diff changeset
479 )
3254
6cf4bd6972c2 core, frontends: avatar refactoring:
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
480 return
6cf4bd6972c2 core, frontends: avatar refactoring:
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
481
6cf4bd6972c2 core, frontends: avatar refactoring:
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
482 # we can't use the cache, so we do the update
6cf4bd6972c2 core, frontends: avatar refactoring:
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
483
6cf4bd6972c2 core, frontends: avatar refactoring:
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
484 log.debug(f"updating {metadata_name} for {entity}")
6cf4bd6972c2 core, frontends: avatar refactoring:
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
485
4270
0d7bb4df2343 Reformatted code base using black.
Goffi <goffi@goffi.org>
parents: 4212
diff changeset
486 if metadata.get("get_all", False):
3254
6cf4bd6972c2 core, frontends: avatar refactoring:
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
487 # 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
488 # 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
489 try:
4037
524856bd7b19 massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents: 3817
diff changeset
490 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
491 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
492 pass
3254
6cf4bd6972c2 core, frontends: avatar refactoring:
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
493 # 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
494 # 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
495 # get callbacks)
cf07641b764d plugin identity: fixed infinite loop on nicknames update
Goffi <goffi@goffi.org>
parents: 3262
diff changeset
496 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
497 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
498 client._identity_update_lock.remove((entity, metadata_name))
3254
6cf4bd6972c2 core, frontends: avatar refactoring:
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
499 return
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 data is not None:
4270
0d7bb4df2343 Reformatted code base using black.
Goffi <goffi@goffi.org>
parents: 4212
diff changeset
502 data_filter = metadata["update_data_filter"]
3254
6cf4bd6972c2 core, frontends: avatar refactoring:
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
503 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
504 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
505 self.check_type(metadata_name, data)
3254
6cf4bd6972c2 core, frontends: avatar refactoring:
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
506
4037
524856bd7b19 massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents: 3817
diff changeset
507 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
508
4270
0d7bb4df2343 Reformatted code base using black.
Goffi <goffi@goffi.org>
parents: 4212
diff changeset
509 if metadata.get("store", False):
3254
6cf4bd6972c2 core, frontends: avatar refactoring:
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
510 key = f"{entity}\n{metadata_name}"
4212
5f2d496c633f core: get rid of `pickle`:
Goffi <goffi@goffi.org>
parents: 4071
diff changeset
511 if data is not None:
5f2d496c633f core: get rid of `pickle`:
Goffi <goffi@goffi.org>
parents: 4071
diff changeset
512 ser_method = metadata.get("store_serialisation")
5f2d496c633f core: get rid of `pickle`:
Goffi <goffi@goffi.org>
parents: 4071
diff changeset
513 if ser_method is not None:
5f2d496c633f core: get rid of `pickle`:
Goffi <goffi@goffi.org>
parents: 4071
diff changeset
514 data = ser_method(data)
3254
6cf4bd6972c2 core, frontends: avatar refactoring:
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
515 await client._identity_storage.aset(key, data)
6cf4bd6972c2 core, frontends: avatar refactoring:
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
516
4037
524856bd7b19 massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents: 3817
diff changeset
517 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
518 return new_data != cached_data
6cf4bd6972c2 core, frontends: avatar refactoring:
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
519
6cf4bd6972c2 core, frontends: avatar refactoring:
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
520 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
521 client = self.host.get_client(profile)
3254
6cf4bd6972c2 core, frontends: avatar refactoring:
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
522 entity = jid.JID(entity) if entity else None
6cf4bd6972c2 core, frontends: avatar refactoring:
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
523 d = defer.ensureDeferred(self.get(client, "avatar", entity, use_cache))
6cf4bd6972c2 core, frontends: avatar refactoring:
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
524 d.addCallback(lambda data: data_format.serialise(data))
6cf4bd6972c2 core, frontends: avatar refactoring:
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
525 return d
6cf4bd6972c2 core, frontends: avatar refactoring:
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
526
4037
524856bd7b19 massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents: 3817
diff changeset
527 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
528 client = self.host.get_client(profile_key)
3254
6cf4bd6972c2 core, frontends: avatar refactoring:
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
529 entity = jid.JID(entity) if entity else None
4270
0d7bb4df2343 Reformatted code base using black.
Goffi <goffi@goffi.org>
parents: 4212
diff changeset
530 return defer.ensureDeferred(self.set(client, "avatar", file_path, entity))
3254
6cf4bd6972c2 core, frontends: avatar refactoring:
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
531
4270
0d7bb4df2343 Reformatted code base using black.
Goffi <goffi@goffi.org>
parents: 4212
diff changeset
532 def _blocking_cache_avatar(self, source: str, avatar_data: dict[str, Any]):
3816
213e83a4ed10 plugin identity, XEP-0054: move avatar resizing and caching method to identity plugin:
Goffi <goffi@goffi.org>
parents: 3541
diff changeset
533 """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
534 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
535 # 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
536 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
537 else:
213e83a4ed10 plugin identity, XEP-0054: move avatar resizing and caching method to identity plugin:
Goffi <goffi@goffi.org>
parents: 3541
diff changeset
538 # 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
539 try:
213e83a4ed10 plugin identity, XEP-0054: move avatar resizing and caching method to identity plugin:
Goffi <goffi@goffi.org>
parents: 3541
diff changeset
540 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
541 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
542 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
543
213e83a4ed10 plugin identity, XEP-0054: move avatar resizing and caching method to identity plugin:
Goffi <goffi@goffi.org>
parents: 3541
diff changeset
544 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
545 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
546 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
547 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
548 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
549 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
550 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
551 upper += offset
213e83a4ed10 plugin identity, XEP-0054: move avatar resizing and caching method to identity plugin:
Goffi <goffi@goffi.org>
parents: 3541
diff changeset
552 lower -= offset
213e83a4ed10 plugin identity, XEP-0054: move avatar resizing and caching method to identity plugin:
Goffi <goffi@goffi.org>
parents: 3541
diff changeset
553 else:
213e83a4ed10 plugin identity, XEP-0054: move avatar resizing and caching method to identity plugin:
Goffi <goffi@goffi.org>
parents: 3541
diff changeset
554 left += offset
213e83a4ed10 plugin identity, XEP-0054: move avatar resizing and caching method to identity plugin:
Goffi <goffi@goffi.org>
parents: 3541
diff changeset
555 right -= offset
213e83a4ed10 plugin identity, XEP-0054: move avatar resizing and caching method to identity plugin:
Goffi <goffi@goffi.org>
parents: 3541
diff changeset
556 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
557 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
558 # 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
559 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
560 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
561 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
562
213e83a4ed10 plugin identity, XEP-0054: move avatar resizing and caching method to identity plugin:
Goffi <goffi@goffi.org>
parents: 3541
diff changeset
563 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
564 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
565 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
566 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
567 img_buf.seek(0)
4270
0d7bb4df2343 Reformatted code base using black.
Goffi <goffi@goffi.org>
parents: 4212
diff changeset
568 with self.host.common_cache.cache_data(source, image_hash, media_type) as f:
3816
213e83a4ed10 plugin identity, XEP-0054: move avatar resizing and caching method to identity plugin:
Goffi <goffi@goffi.org>
parents: 3541
diff changeset
569 f.write(img_buf.read())
4270
0d7bb4df2343 Reformatted code base using black.
Goffi <goffi@goffi.org>
parents: 4212
diff changeset
570 avatar_data["path"] = Path(f.name)
0d7bb4df2343 Reformatted code base using black.
Goffi <goffi@goffi.org>
parents: 4212
diff changeset
571 avatar_data["filename"] = avatar_data["path"].name
0d7bb4df2343 Reformatted code base using black.
Goffi <goffi@goffi.org>
parents: 4212
diff changeset
572 avatar_data["cache_uid"] = image_hash
3816
213e83a4ed10 plugin identity, XEP-0054: move avatar resizing and caching method to identity plugin:
Goffi <goffi@goffi.org>
parents: 3541
diff changeset
573
4037
524856bd7b19 massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents: 3817
diff changeset
574 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
575 """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
576
213e83a4ed10 plugin identity, XEP-0054: move avatar resizing and caching method to identity plugin:
Goffi <goffi@goffi.org>
parents: 3541
diff changeset
577 @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
578 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
579 @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
580 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
581 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
582 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
583 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
584 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
585 """
4037
524856bd7b19 massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents: 3817
diff changeset
586 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
587
4037
524856bd7b19 massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents: 3817
diff changeset
588 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
589 """Convert avatar file path to dict data"""
6cf4bd6972c2 core, frontends: avatar refactoring:
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
590 file_path = Path(file_path)
6cf4bd6972c2 core, frontends: avatar refactoring:
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
591 if not file_path.is_file():
6cf4bd6972c2 core, frontends: avatar refactoring:
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
592 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
593 avatar_data = {
4270
0d7bb4df2343 Reformatted code base using black.
Goffi <goffi@goffi.org>
parents: 4212
diff changeset
594 "path": file_path,
0d7bb4df2343 Reformatted code base using black.
Goffi <goffi@goffi.org>
parents: 4212
diff changeset
595 "filename": file_path.name,
0d7bb4df2343 Reformatted code base using black.
Goffi <goffi@goffi.org>
parents: 4212
diff changeset
596 "media_type": image.guess_type(file_path),
3254
6cf4bd6972c2 core, frontends: avatar refactoring:
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
597 }
4270
0d7bb4df2343 Reformatted code base using black.
Goffi <goffi@goffi.org>
parents: 4212
diff changeset
598 media_type = avatar_data["media_type"]
3254
6cf4bd6972c2 core, frontends: avatar refactoring:
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
599 if media_type is None:
6cf4bd6972c2 core, frontends: avatar refactoring:
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
600 raise ValueError(f"Can't identify type of image at {file_path}")
4270
0d7bb4df2343 Reformatted code base using black.
Goffi <goffi@goffi.org>
parents: 4212
diff changeset
601 if not media_type.startswith("image/"):
3254
6cf4bd6972c2 core, frontends: avatar refactoring:
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
602 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
603 await self.cache_avatar(IMPORT_NAME, avatar_data)
3254
6cf4bd6972c2 core, frontends: avatar refactoring:
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
604 return avatar_data
6cf4bd6972c2 core, frontends: avatar refactoring:
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
605
4037
524856bd7b19 massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents: 3817
diff changeset
606 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
607 """Update our own avatar"""
3278
27d4b71e264a plugin identity: fixed update in avatarSetPostTreatment
Goffi <goffi@goffi.org>
parents: 3277
diff changeset
608 await self.update(client, IMPORT_NAME, "avatar", avatar_data, entity)
3254
6cf4bd6972c2 core, frontends: avatar refactoring:
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
609
4037
524856bd7b19 massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents: 3817
diff changeset
610 def avatar_build_metadata(
4270
0d7bb4df2343 Reformatted code base using black.
Goffi <goffi@goffi.org>
parents: 4212
diff changeset
611 self,
0d7bb4df2343 Reformatted code base using black.
Goffi <goffi@goffi.org>
parents: 4212
diff changeset
612 path: Path,
0d7bb4df2343 Reformatted code base using black.
Goffi <goffi@goffi.org>
parents: 4212
diff changeset
613 media_type: Optional[str] = None,
0d7bb4df2343 Reformatted code base using black.
Goffi <goffi@goffi.org>
parents: 4212
diff changeset
614 cache_uid: Optional[str] = None,
3816
213e83a4ed10 plugin identity, XEP-0054: move avatar resizing and caching method to identity plugin:
Goffi <goffi@goffi.org>
parents: 3541
diff changeset
615 ) -> Optional[Dict[str, Union[str, Path, None]]]:
3254
6cf4bd6972c2 core, frontends: avatar refactoring:
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
616 """Helper method to generate avatar metadata
6cf4bd6972c2 core, frontends: avatar refactoring:
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
617
6cf4bd6972c2 core, frontends: avatar refactoring:
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
618 @param path(str, Path, None): path to avatar file
6cf4bd6972c2 core, frontends: avatar refactoring:
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
619 avatar file must be in cache
6cf4bd6972c2 core, frontends: avatar refactoring:
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
620 None if avatar is explicitely not set
6cf4bd6972c2 core, frontends: avatar refactoring:
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
621 @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
622 @param cache_uid(str, None): UID of avatar in cache
6cf4bd6972c2 core, frontends: avatar refactoring:
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
623 @return (dict, None): avatar metadata
6cf4bd6972c2 core, frontends: avatar refactoring:
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
624 None if avatar is not set
6cf4bd6972c2 core, frontends: avatar refactoring:
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
625 """
6cf4bd6972c2 core, frontends: avatar refactoring:
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
626 if path is None:
6cf4bd6972c2 core, frontends: avatar refactoring:
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
627 return None
6cf4bd6972c2 core, frontends: avatar refactoring:
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
628 else:
6cf4bd6972c2 core, frontends: avatar refactoring:
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
629 if cache_uid is None:
6cf4bd6972c2 core, frontends: avatar refactoring:
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
630 raise ValueError("cache_uid must be set if path is set")
6cf4bd6972c2 core, frontends: avatar refactoring:
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
631 path = Path(path)
6cf4bd6972c2 core, frontends: avatar refactoring:
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
632 if media_type is None:
6cf4bd6972c2 core, frontends: avatar refactoring:
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
633 media_type = image.guess_type(path)
6cf4bd6972c2 core, frontends: avatar refactoring:
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
634
6cf4bd6972c2 core, frontends: avatar refactoring:
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
635 return {
6cf4bd6972c2 core, frontends: avatar refactoring:
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
636 "path": path,
3326
9e1ba1e1179f plugin identity: added "filename" metadata for avatar
Goffi <goffi@goffi.org>
parents: 3278
diff changeset
637 "filename": path.name,
3254
6cf4bd6972c2 core, frontends: avatar refactoring:
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
638 "media_type": media_type,
6cf4bd6972c2 core, frontends: avatar refactoring:
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
639 "cache_uid": cache_uid,
6cf4bd6972c2 core, frontends: avatar refactoring:
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
640 }
6cf4bd6972c2 core, frontends: avatar refactoring:
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
641
4037
524856bd7b19 massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents: 3817
diff changeset
642 def avatar_update_is_new_data(self, client, entity, cached_data, new_data):
4270
0d7bb4df2343 Reformatted code base using black.
Goffi <goffi@goffi.org>
parents: 4212
diff changeset
643 return new_data["path"] != cached_data["path"]
3254
6cf4bd6972c2 core, frontends: avatar refactoring:
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
644
4037
524856bd7b19 massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents: 3817
diff changeset
645 async def avatar_update_data_filter(self, client, entity, data):
3254
6cf4bd6972c2 core, frontends: avatar refactoring:
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
646 if not isinstance(data, dict):
6cf4bd6972c2 core, frontends: avatar refactoring:
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
647 raise ValueError(f"Invalid data type ({type(data)}), a dict is expected")
4270
0d7bb4df2343 Reformatted code base using black.
Goffi <goffi@goffi.org>
parents: 4212
diff changeset
648 mandatory_keys = {"path", "filename", "cache_uid"}
3254
6cf4bd6972c2 core, frontends: avatar refactoring:
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
649 if not data.keys() >= mandatory_keys:
6cf4bd6972c2 core, frontends: avatar refactoring:
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
650 raise ValueError(f"missing avatar data keys: {mandatory_keys - data.keys()}")
6cf4bd6972c2 core, frontends: avatar refactoring:
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
651 return data
6cf4bd6972c2 core, frontends: avatar refactoring:
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
652
4212
5f2d496c633f core: get rid of `pickle`:
Goffi <goffi@goffi.org>
parents: 4071
diff changeset
653 def _avatar_ser(self, data: dict) -> dict:
5f2d496c633f core: get rid of `pickle`:
Goffi <goffi@goffi.org>
parents: 4071
diff changeset
654 if data.get("path"):
5f2d496c633f core: get rid of `pickle`:
Goffi <goffi@goffi.org>
parents: 4071
diff changeset
655 # Path instance can't be stored
5f2d496c633f core: get rid of `pickle`:
Goffi <goffi@goffi.org>
parents: 4071
diff changeset
656 data = data.copy()
5f2d496c633f core: get rid of `pickle`:
Goffi <goffi@goffi.org>
parents: 4071
diff changeset
657 data["path"] = str(data["path"])
5f2d496c633f core: get rid of `pickle`:
Goffi <goffi@goffi.org>
parents: 4071
diff changeset
658 return data
5f2d496c633f core: get rid of `pickle`:
Goffi <goffi@goffi.org>
parents: 4071
diff changeset
659
5f2d496c633f core: get rid of `pickle`:
Goffi <goffi@goffi.org>
parents: 4071
diff changeset
660 def _avatar_deser(self, data: dict) -> dict:
5f2d496c633f core: get rid of `pickle`:
Goffi <goffi@goffi.org>
parents: 4071
diff changeset
661 if data.get("path"):
5f2d496c633f core: get rid of `pickle`:
Goffi <goffi@goffi.org>
parents: 4071
diff changeset
662 data["path"] = Path(data["path"])
5f2d496c633f core: get rid of `pickle`:
Goffi <goffi@goffi.org>
parents: 4071
diff changeset
663 return data
5f2d496c633f core: get rid of `pickle`:
Goffi <goffi@goffi.org>
parents: 4071
diff changeset
664
4037
524856bd7b19 massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents: 3817
diff changeset
665 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
666 """Prepend nicknames from core locations + set default nickname
6cf4bd6972c2 core, frontends: avatar refactoring:
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
667
6cf4bd6972c2 core, frontends: avatar refactoring:
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
668 nicknames are checked from many locations, there is always at least
6cf4bd6972c2 core, frontends: avatar refactoring:
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
669 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
670 Nicknames are appended in this order:
6cf4bd6972c2 core, frontends: avatar refactoring:
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
671 - roster, plugins set nicknames
6cf4bd6972c2 core, frontends: avatar refactoring:
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
672 - 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
673 if there is no user part.
6cf4bd6972c2 core, frontends: avatar refactoring:
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
674 For MUC, room nick is always put first
6cf4bd6972c2 core, frontends: avatar refactoring:
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
675 """
6cf4bd6972c2 core, frontends: avatar refactoring:
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
676 nicknames = []
3817
998c5318230f plugin identity: make the plugin compatible with component + description:
Goffi <goffi@goffi.org>
parents: 3816
diff changeset
677
998c5318230f plugin identity: make the plugin compatible with component + description:
Goffi <goffi@goffi.org>
parents: 3816
diff changeset
678 # for MUC we add resource
3254
6cf4bd6972c2 core, frontends: avatar refactoring:
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
679 if entity.resource:
4037
524856bd7b19 massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents: 3817
diff changeset
680 # 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
681 # occupant jid
6cf4bd6972c2 core, frontends: avatar refactoring:
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
682 nicknames.append(entity.resource)
6cf4bd6972c2 core, frontends: avatar refactoring:
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
683
3817
998c5318230f plugin identity: make the plugin compatible with component + description:
Goffi <goffi@goffi.org>
parents: 3816
diff changeset
684 # 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
685 if not client.is_component:
4037
524856bd7b19 massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents: 3817
diff changeset
686 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
687 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
688 # 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
689 nicknames.append(roster_item.name)
3254
6cf4bd6972c2 core, frontends: avatar refactoring:
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
690
6cf4bd6972c2 core, frontends: avatar refactoring:
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
691 nicknames.extend(plugin_nicknames)
6cf4bd6972c2 core, frontends: avatar refactoring:
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
692
6cf4bd6972c2 core, frontends: avatar refactoring:
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
693 if not nicknames:
6cf4bd6972c2 core, frontends: avatar refactoring:
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
694 if entity.user:
6cf4bd6972c2 core, frontends: avatar refactoring:
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
695 nicknames.append(entity.user.capitalize())
6cf4bd6972c2 core, frontends: avatar refactoring:
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
696 else:
6cf4bd6972c2 core, frontends: avatar refactoring:
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
697 nicknames.append(entity.userhost())
6cf4bd6972c2 core, frontends: avatar refactoring:
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
698
6cf4bd6972c2 core, frontends: avatar refactoring:
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
699 # we remove duplicates while preserving order with dict
6cf4bd6972c2 core, frontends: avatar refactoring:
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
700 return list(dict.fromkeys(nicknames))
6cf4bd6972c2 core, frontends: avatar refactoring:
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
701
4037
524856bd7b19 massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents: 3817
diff changeset
702 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
703 return not set(new_nicknames).issubset(cached_data)
6cf4bd6972c2 core, frontends: avatar refactoring:
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
704
4037
524856bd7b19 massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents: 3817
diff changeset
705 async def description_get_post_treatment(
4270
0d7bb4df2343 Reformatted code base using black.
Goffi <goffi@goffi.org>
parents: 4212
diff changeset
706 self, client: SatXMPPEntity, entity: jid.JID, plugin_description: List[str]
3817
998c5318230f plugin identity: make the plugin compatible with component + description:
Goffi <goffi@goffi.org>
parents: 3816
diff changeset
707 ) -> str:
998c5318230f plugin identity: make the plugin compatible with component + description:
Goffi <goffi@goffi.org>
parents: 3816
diff changeset
708 """Join all descriptions in a unique string"""
4270
0d7bb4df2343 Reformatted code base using black.
Goffi <goffi@goffi.org>
parents: 4212
diff changeset
709 return "\n".join(plugin_description)
3817
998c5318230f plugin identity: make the plugin compatible with component + description:
Goffi <goffi@goffi.org>
parents: 3816
diff changeset
710
4037
524856bd7b19 massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents: 3817
diff changeset
711 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
712 entity = jid.JID(entity_s)
4037
524856bd7b19 massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents: 3817
diff changeset
713 client = self.host.get_client(profile)
3254
6cf4bd6972c2 core, frontends: avatar refactoring:
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
714 d = defer.ensureDeferred(
4270
0d7bb4df2343 Reformatted code base using black.
Goffi <goffi@goffi.org>
parents: 4212
diff changeset
715 self.get_identity(client, entity, metadata_filter, use_cache)
0d7bb4df2343 Reformatted code base using black.
Goffi <goffi@goffi.org>
parents: 4212
diff changeset
716 )
3338
203a491fcd86 plugin identity: new methods `identitiesGet` and `identitiesBaseGet`
Goffi <goffi@goffi.org>
parents: 3326
diff changeset
717 d.addCallback(data_format.serialise)
3254
6cf4bd6972c2 core, frontends: avatar refactoring:
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
718 return d
6cf4bd6972c2 core, frontends: avatar refactoring:
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
719
4037
524856bd7b19 massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents: 3817
diff changeset
720 async def get_identity(
3817
998c5318230f plugin identity: make the plugin compatible with component + description:
Goffi <goffi@goffi.org>
parents: 3816
diff changeset
721 self,
998c5318230f plugin identity: make the plugin compatible with component + description:
Goffi <goffi@goffi.org>
parents: 3816
diff changeset
722 client: SatXMPPEntity,
998c5318230f plugin identity: make the plugin compatible with component + description:
Goffi <goffi@goffi.org>
parents: 3816
diff changeset
723 entity: Optional[jid.JID] = None,
998c5318230f plugin identity: make the plugin compatible with component + description:
Goffi <goffi@goffi.org>
parents: 3816
diff changeset
724 metadata_filter: Optional[List[str]] = None,
4270
0d7bb4df2343 Reformatted code base using black.
Goffi <goffi@goffi.org>
parents: 4212
diff changeset
725 use_cache: bool = True,
3817
998c5318230f plugin identity: make the plugin compatible with component + description:
Goffi <goffi@goffi.org>
parents: 3816
diff changeset
726 ) -> Dict[str, Any]:
2253
db468f24b9fc plugin identity: plugin identity first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
727 """Retrieve identity of an entity
db468f24b9fc plugin identity: plugin identity first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
728
3817
998c5318230f plugin identity: make the plugin compatible with component + description:
Goffi <goffi@goffi.org>
parents: 3816
diff changeset
729 @param entity: entity to check
998c5318230f plugin identity: make the plugin compatible with component + description:
Goffi <goffi@goffi.org>
parents: 3816
diff changeset
730 @param metadata_filter: if not None or empty, only return
3254
6cf4bd6972c2 core, frontends: avatar refactoring:
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
731 metadata in this filter
3817
998c5318230f plugin identity: make the plugin compatible with component + description:
Goffi <goffi@goffi.org>
parents: 3816
diff changeset
732 @param use_cache: if False, cache won't be checked
3254
6cf4bd6972c2 core, frontends: avatar refactoring:
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
733 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
734 @return: identity data
2253
db468f24b9fc plugin identity: plugin identity first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
735 """
4287
ff88a807852d plugin identity: disco identity is now added in identity metadata:
Goffi <goffi@goffi.org>
parents: 4270
diff changeset
736 identities = []
ff88a807852d plugin identity: disco identity is now added in identity metadata:
Goffi <goffi@goffi.org>
parents: 4270
diff changeset
737 id_data = {"identities": identities}
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(
4270
0d7bb4df2343 Reformatted code base using black.
Goffi <goffi@goffi.org>
parents: 4212
diff changeset
746 client, metadata_name, entity, use_cache
0d7bb4df2343 Reformatted code base using black.
Goffi <goffi@goffi.org>
parents: 4212
diff changeset
747 )
3254
6cf4bd6972c2 core, frontends: avatar refactoring:
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
748
4287
ff88a807852d plugin identity: disco identity is now added in identity metadata:
Goffi <goffi@goffi.org>
parents: 4270
diff changeset
749 if entity is not None:
ff88a807852d plugin identity: disco identity is now added in identity metadata:
Goffi <goffi@goffi.org>
parents: 4270
diff changeset
750 try:
ff88a807852d plugin identity: disco identity is now added in identity metadata:
Goffi <goffi@goffi.org>
parents: 4270
diff changeset
751 disco_infos = await self.host.memory.disco.get_infos(client, entity)
ff88a807852d plugin identity: disco identity is now added in identity metadata:
Goffi <goffi@goffi.org>
parents: 4270
diff changeset
752 except Exception as e:
ff88a807852d plugin identity: disco identity is now added in identity metadata:
Goffi <goffi@goffi.org>
parents: 4270
diff changeset
753 log.exception(f"Can't get disco infos for {entity}.")
ff88a807852d plugin identity: disco identity is now added in identity metadata:
Goffi <goffi@goffi.org>
parents: 4270
diff changeset
754 else:
ff88a807852d plugin identity: disco identity is now added in identity metadata:
Goffi <goffi@goffi.org>
parents: 4270
diff changeset
755 for (category, type_), name in disco_infos.identities.items():
ff88a807852d plugin identity: disco identity is now added in identity metadata:
Goffi <goffi@goffi.org>
parents: 4270
diff changeset
756 identities.append({"category": category, "type": type_, "name": name})
ff88a807852d plugin identity: disco identity is now added in identity metadata:
Goffi <goffi@goffi.org>
parents: 4270
diff changeset
757
3254
6cf4bd6972c2 core, frontends: avatar refactoring:
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
758 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
759
4037
524856bd7b19 massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents: 3817
diff changeset
760 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
761 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
762 client = self.host.get_client(profile)
524856bd7b19 massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents: 3817
diff changeset
763 d = defer.ensureDeferred(self.get_identities(client, entities, metadata_filter))
4270
0d7bb4df2343 Reformatted code base using black.
Goffi <goffi@goffi.org>
parents: 4212
diff changeset
764 d.addCallback(lambda d: data_format.serialise({str(j): i for j, i in d.items()}))
3338
203a491fcd86 plugin identity: new methods `identitiesGet` and `identitiesBaseGet`
Goffi <goffi@goffi.org>
parents: 3326
diff changeset
765 return d
203a491fcd86 plugin identity: new methods `identitiesGet` and `identitiesBaseGet`
Goffi <goffi@goffi.org>
parents: 3326
diff changeset
766
4037
524856bd7b19 massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents: 3817
diff changeset
767 async def get_identities(
3338
203a491fcd86 plugin identity: new methods `identitiesGet` and `identitiesBaseGet`
Goffi <goffi@goffi.org>
parents: 3326
diff changeset
768 self,
203a491fcd86 plugin identity: new methods `identitiesGet` and `identitiesBaseGet`
Goffi <goffi@goffi.org>
parents: 3326
diff changeset
769 client: SatXMPPEntity,
203a491fcd86 plugin identity: new methods `identitiesGet` and `identitiesBaseGet`
Goffi <goffi@goffi.org>
parents: 3326
diff changeset
770 entities: List[jid.JID],
203a491fcd86 plugin identity: new methods `identitiesGet` and `identitiesBaseGet`
Goffi <goffi@goffi.org>
parents: 3326
diff changeset
771 metadata_filter: Optional[List[str]] = None,
203a491fcd86 plugin identity: new methods `identitiesGet` and `identitiesBaseGet`
Goffi <goffi@goffi.org>
parents: 3326
diff changeset
772 ) -> dict:
203a491fcd86 plugin identity: new methods `identitiesGet` and `identitiesBaseGet`
Goffi <goffi@goffi.org>
parents: 3326
diff changeset
773 """Retrieve several identities at once
203a491fcd86 plugin identity: new methods `identitiesGet` and `identitiesBaseGet`
Goffi <goffi@goffi.org>
parents: 3326
diff changeset
774
203a491fcd86 plugin identity: new methods `identitiesGet` and `identitiesBaseGet`
Goffi <goffi@goffi.org>
parents: 3326
diff changeset
775 @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
776 @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
777 @return: identities metadata where key is jid
203a491fcd86 plugin identity: new methods `identitiesGet` and `identitiesBaseGet`
Goffi <goffi@goffi.org>
parents: 3326
diff changeset
778 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
779 result (and a warning will be logged)
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 identities = {}
203a491fcd86 plugin identity: new methods `identitiesGet` and `identitiesBaseGet`
Goffi <goffi@goffi.org>
parents: 3326
diff changeset
782 get_identity_list = []
203a491fcd86 plugin identity: new methods `identitiesGet` and `identitiesBaseGet`
Goffi <goffi@goffi.org>
parents: 3326
diff changeset
783 for entity_jid in entities:
203a491fcd86 plugin identity: new methods `identitiesGet` and `identitiesBaseGet`
Goffi <goffi@goffi.org>
parents: 3326
diff changeset
784 get_identity_list.append(
203a491fcd86 plugin identity: new methods `identitiesGet` and `identitiesBaseGet`
Goffi <goffi@goffi.org>
parents: 3326
diff changeset
785 defer.ensureDeferred(
4037
524856bd7b19 massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents: 3817
diff changeset
786 self.get_identity(
3338
203a491fcd86 plugin identity: new methods `identitiesGet` and `identitiesBaseGet`
Goffi <goffi@goffi.org>
parents: 3326
diff changeset
787 client,
203a491fcd86 plugin identity: new methods `identitiesGet` and `identitiesBaseGet`
Goffi <goffi@goffi.org>
parents: 3326
diff changeset
788 entity=entity_jid,
203a491fcd86 plugin identity: new methods `identitiesGet` and `identitiesBaseGet`
Goffi <goffi@goffi.org>
parents: 3326
diff changeset
789 metadata_filter=metadata_filter,
203a491fcd86 plugin identity: new methods `identitiesGet` and `identitiesBaseGet`
Goffi <goffi@goffi.org>
parents: 3326
diff changeset
790 )
203a491fcd86 plugin identity: new methods `identitiesGet` and `identitiesBaseGet`
Goffi <goffi@goffi.org>
parents: 3326
diff changeset
791 )
203a491fcd86 plugin identity: new methods `identitiesGet` and `identitiesBaseGet`
Goffi <goffi@goffi.org>
parents: 3326
diff changeset
792 )
203a491fcd86 plugin identity: new methods `identitiesGet` and `identitiesBaseGet`
Goffi <goffi@goffi.org>
parents: 3326
diff changeset
793 identities_result = await defer.DeferredList(get_identity_list)
203a491fcd86 plugin identity: new methods `identitiesGet` and `identitiesBaseGet`
Goffi <goffi@goffi.org>
parents: 3326
diff changeset
794 for idx, (success, identity) in enumerate(identities_result):
203a491fcd86 plugin identity: new methods `identitiesGet` and `identitiesBaseGet`
Goffi <goffi@goffi.org>
parents: 3326
diff changeset
795 entity_jid = entities[idx]
203a491fcd86 plugin identity: new methods `identitiesGet` and `identitiesBaseGet`
Goffi <goffi@goffi.org>
parents: 3326
diff changeset
796 if not success:
203a491fcd86 plugin identity: new methods `identitiesGet` and `identitiesBaseGet`
Goffi <goffi@goffi.org>
parents: 3326
diff changeset
797 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
798 else:
203a491fcd86 plugin identity: new methods `identitiesGet` and `identitiesBaseGet`
Goffi <goffi@goffi.org>
parents: 3326
diff changeset
799 identities[entity_jid] = identity
203a491fcd86 plugin identity: new methods `identitiesGet` and `identitiesBaseGet`
Goffi <goffi@goffi.org>
parents: 3326
diff changeset
800 return identities
203a491fcd86 plugin identity: new methods `identitiesGet` and `identitiesBaseGet`
Goffi <goffi@goffi.org>
parents: 3326
diff changeset
801
4037
524856bd7b19 massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents: 3817
diff changeset
802 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
803 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
804 d = defer.ensureDeferred(self.get_base_identities(client))
4270
0d7bb4df2343 Reformatted code base using black.
Goffi <goffi@goffi.org>
parents: 4212
diff changeset
805 d.addCallback(lambda d: data_format.serialise({str(j): i for j, i in d.items()}))
3338
203a491fcd86 plugin identity: new methods `identitiesGet` and `identitiesBaseGet`
Goffi <goffi@goffi.org>
parents: 3326
diff changeset
806 return d
203a491fcd86 plugin identity: new methods `identitiesGet` and `identitiesBaseGet`
Goffi <goffi@goffi.org>
parents: 3326
diff changeset
807
4037
524856bd7b19 massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents: 3817
diff changeset
808 async def get_base_identities(
3338
203a491fcd86 plugin identity: new methods `identitiesGet` and `identitiesBaseGet`
Goffi <goffi@goffi.org>
parents: 3326
diff changeset
809 self,
203a491fcd86 plugin identity: new methods `identitiesGet` and `identitiesBaseGet`
Goffi <goffi@goffi.org>
parents: 3326
diff changeset
810 client: SatXMPPEntity,
203a491fcd86 plugin identity: new methods `identitiesGet` and `identitiesBaseGet`
Goffi <goffi@goffi.org>
parents: 3326
diff changeset
811 ) -> dict:
203a491fcd86 plugin identity: new methods `identitiesGet` and `identitiesBaseGet`
Goffi <goffi@goffi.org>
parents: 3326
diff changeset
812 """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
813
203a491fcd86 plugin identity: new methods `identitiesGet` and `identitiesBaseGet`
Goffi <goffi@goffi.org>
parents: 3326
diff changeset
814 @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
815
203a491fcd86 plugin identity: new methods `identitiesGet` and `identitiesBaseGet`
Goffi <goffi@goffi.org>
parents: 3326
diff changeset
816 """
3817
998c5318230f plugin identity: make the plugin compatible with component + description:
Goffi <goffi@goffi.org>
parents: 3816
diff changeset
817 if client.is_component:
998c5318230f plugin identity: make the plugin compatible with component + description:
Goffi <goffi@goffi.org>
parents: 3816
diff changeset
818 entities = [client.jid.userhostJID()]
998c5318230f plugin identity: make the plugin compatible with component + description:
Goffi <goffi@goffi.org>
parents: 3816
diff changeset
819 else:
4037
524856bd7b19 massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents: 3817
diff changeset
820 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
821
4270
0d7bb4df2343 Reformatted code base using black.
Goffi <goffi@goffi.org>
parents: 4212
diff changeset
822 return await self.get_identities(client, entities, ["avatar", "nicknames"])
3338
203a491fcd86 plugin identity: new methods `identitiesGet` and `identitiesBaseGet`
Goffi <goffi@goffi.org>
parents: 3326
diff changeset
823
4037
524856bd7b19 massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents: 3817
diff changeset
824 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
825 client = self.host.get_client(profile)
3254
6cf4bd6972c2 core, frontends: avatar refactoring:
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
826 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
827 return defer.ensureDeferred(self.set_identity(client, id_data))
2253
db468f24b9fc plugin identity: plugin identity first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
828
4037
524856bd7b19 massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents: 3817
diff changeset
829 async def set_identity(self, client, id_data):
2253
db468f24b9fc plugin identity: plugin identity first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
830 """Update profile's identity
db468f24b9fc plugin identity: plugin identity first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
831
3817
998c5318230f plugin identity: make the plugin compatible with component + description:
Goffi <goffi@goffi.org>
parents: 3816
diff changeset
832 @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
833 """
3254
6cf4bd6972c2 core, frontends: avatar refactoring:
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
834 if not id_data.keys() <= self.metadata.keys():
6cf4bd6972c2 core, frontends: avatar refactoring:
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
835 raise ValueError(
4270
0d7bb4df2343 Reformatted code base using black.
Goffi <goffi@goffi.org>
parents: 4212
diff changeset
836 f"Invalid metadata names: {id_data.keys() - self.metadata.keys()}"
0d7bb4df2343 Reformatted code base using black.
Goffi <goffi@goffi.org>
parents: 4212
diff changeset
837 )
3254
6cf4bd6972c2 core, frontends: avatar refactoring:
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
838 for metadata_name, data in id_data.items():
6cf4bd6972c2 core, frontends: avatar refactoring:
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
839 try:
6cf4bd6972c2 core, frontends: avatar refactoring:
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
840 await self.set(client, metadata_name, data)
6cf4bd6972c2 core, frontends: avatar refactoring:
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
841 except Exception as e:
6cf4bd6972c2 core, frontends: avatar refactoring:
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
842 log.warning(
4270
0d7bb4df2343 Reformatted code base using black.
Goffi <goffi@goffi.org>
parents: 4212
diff changeset
843 _("Can't set metadata {metadata_name!r}: {reason}").format(
0d7bb4df2343 Reformatted code base using black.
Goffi <goffi@goffi.org>
parents: 4212
diff changeset
844 metadata_name=metadata_name, reason=e
0d7bb4df2343 Reformatted code base using black.
Goffi <goffi@goffi.org>
parents: 4212
diff changeset
845 )
0d7bb4df2343 Reformatted code base using black.
Goffi <goffi@goffi.org>
parents: 4212
diff changeset
846 )