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