Mercurial > libervia-backend
annotate sat/plugins/plugin_comp_ap_gateway/http_server.py @ 3869:c0bcbcf5b4b7
component AP gateway: handle `Like` and `Undo`/`Like` activities:
rel 370
author | Goffi <goffi@goffi.org> |
---|---|
date | Thu, 21 Jul 2022 18:07:35 +0200 |
parents | aaa4e7815ba8 |
children | bd84d289fc94 |
rev | line source |
---|---|
3682
7c990aaa49d3
comp AP Gateway: ActivityPub Component first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
1 #!/usr/bin/env python3 |
7c990aaa49d3
comp AP Gateway: ActivityPub Component first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
2 |
7c990aaa49d3
comp AP Gateway: ActivityPub Component first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
3 # Libervia ActivityPub Gateway |
7c990aaa49d3
comp AP Gateway: ActivityPub Component first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
4 # Copyright (C) 2009-2021 Jérôme Poisson (goffi@goffi.org) |
7c990aaa49d3
comp AP Gateway: ActivityPub Component first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
5 |
7c990aaa49d3
comp AP Gateway: ActivityPub Component first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
6 # This program is free software: you can redistribute it and/or modify |
7c990aaa49d3
comp AP Gateway: ActivityPub Component first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
7 # it under the terms of the GNU Affero General Public License as published by |
7c990aaa49d3
comp AP Gateway: ActivityPub Component first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
8 # the Free Software Foundation, either version 3 of the License, or |
7c990aaa49d3
comp AP Gateway: ActivityPub Component first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
9 # (at your option) any later version. |
7c990aaa49d3
comp AP Gateway: ActivityPub Component first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
10 |
7c990aaa49d3
comp AP Gateway: ActivityPub Component first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
11 # This program is distributed in the hope that it will be useful, |
7c990aaa49d3
comp AP Gateway: ActivityPub Component first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
12 # but WITHOUT ANY WARRANTY; without even the implied warranty of |
7c990aaa49d3
comp AP Gateway: ActivityPub Component first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
13 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
7c990aaa49d3
comp AP Gateway: ActivityPub Component first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
14 # GNU Affero General Public License for more details. |
7c990aaa49d3
comp AP Gateway: ActivityPub Component first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
15 |
7c990aaa49d3
comp AP Gateway: ActivityPub Component first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
16 # You should have received a copy of the GNU Affero General Public License |
7c990aaa49d3
comp AP Gateway: ActivityPub Component first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
17 # along with this program. If not, see <http://www.gnu.org/licenses/>. |
7c990aaa49d3
comp AP Gateway: ActivityPub Component first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
18 |
3745
a8c7e5cef0cb
comp AP gateway: signature checking, caching and threads management:
Goffi <goffi@goffi.org>
parents:
3729
diff
changeset
|
19 import time |
3821
0b1c30ff2cbb
component AP: XMPP identity => AP actor data converstion:
Goffi <goffi@goffi.org>
parents:
3807
diff
changeset
|
20 import html |
3764
125c7043b277
comp AP gateway: publish, (un)subscribe/(un)follow, public subscription/following/followers:
Goffi <goffi@goffi.org>
parents:
3745
diff
changeset
|
21 from typing import Optional, Dict, List, Any |
3728
b15644cae50d
component AP gateway: JID/node ⟺ AP outbox conversion:
Goffi <goffi@goffi.org>
parents:
3684
diff
changeset
|
22 import json |
b15644cae50d
component AP gateway: JID/node ⟺ AP outbox conversion:
Goffi <goffi@goffi.org>
parents:
3684
diff
changeset
|
23 from urllib import parse |
3745
a8c7e5cef0cb
comp AP gateway: signature checking, caching and threads management:
Goffi <goffi@goffi.org>
parents:
3729
diff
changeset
|
24 from collections import deque |
3728
b15644cae50d
component AP gateway: JID/node ⟺ AP outbox conversion:
Goffi <goffi@goffi.org>
parents:
3684
diff
changeset
|
25 import unicodedata |
b15644cae50d
component AP gateway: JID/node ⟺ AP outbox conversion:
Goffi <goffi@goffi.org>
parents:
3684
diff
changeset
|
26 |
b15644cae50d
component AP gateway: JID/node ⟺ AP outbox conversion:
Goffi <goffi@goffi.org>
parents:
3684
diff
changeset
|
27 from twisted.web import http, resource as web_resource, server |
3821
0b1c30ff2cbb
component AP: XMPP identity => AP actor data converstion:
Goffi <goffi@goffi.org>
parents:
3807
diff
changeset
|
28 from twisted.web import static |
3764
125c7043b277
comp AP gateway: publish, (un)subscribe/(un)follow, public subscription/following/followers:
Goffi <goffi@goffi.org>
parents:
3745
diff
changeset
|
29 from twisted.python import failure |
3833
381340b9a9ee
component AP gateway: convert XMPP mentions to AP:
Goffi <goffi@goffi.org>
parents:
3826
diff
changeset
|
30 from twisted.internet import defer |
3728
b15644cae50d
component AP gateway: JID/node ⟺ AP outbox conversion:
Goffi <goffi@goffi.org>
parents:
3684
diff
changeset
|
31 from twisted.words.protocols.jabber import jid, error |
b15644cae50d
component AP gateway: JID/node ⟺ AP outbox conversion:
Goffi <goffi@goffi.org>
parents:
3684
diff
changeset
|
32 from wokkel import pubsub, rsm |
b15644cae50d
component AP gateway: JID/node ⟺ AP outbox conversion:
Goffi <goffi@goffi.org>
parents:
3684
diff
changeset
|
33 |
b15644cae50d
component AP gateway: JID/node ⟺ AP outbox conversion:
Goffi <goffi@goffi.org>
parents:
3684
diff
changeset
|
34 from sat.core import exceptions |
3682
7c990aaa49d3
comp AP Gateway: ActivityPub Component first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
35 from sat.core.constants import Const as C |
3728
b15644cae50d
component AP gateway: JID/node ⟺ AP outbox conversion:
Goffi <goffi@goffi.org>
parents:
3684
diff
changeset
|
36 from sat.core.i18n import _ |
3869
c0bcbcf5b4b7
component AP gateway: handle `Like` and `Undo`/`Like` activities:
Goffi <goffi@goffi.org>
parents:
3847
diff
changeset
|
37 from sat.core.core_types import SatXMPPEntity |
3682
7c990aaa49d3
comp AP Gateway: ActivityPub Component first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
38 from sat.core.log import getLogger |
3844
65e5718e7710
component AP gateway: `Announce` activity implementation:
Goffi <goffi@goffi.org>
parents:
3833
diff
changeset
|
39 from sat.tools.common import date_utils, uri |
3745
a8c7e5cef0cb
comp AP gateway: signature checking, caching and threads management:
Goffi <goffi@goffi.org>
parents:
3729
diff
changeset
|
40 from sat.memory.sqla_mapping import SubscriptionState |
3729
86eea17cafa7
component AP gateway: split plugin in several files:
Goffi <goffi@goffi.org>
parents:
3728
diff
changeset
|
41 |
3745
a8c7e5cef0cb
comp AP gateway: signature checking, caching and threads management:
Goffi <goffi@goffi.org>
parents:
3729
diff
changeset
|
42 from .constants import ( |
3846
cc13efdd8360
component AP gateway: return item when `item` URL is used:
Goffi <goffi@goffi.org>
parents:
3844
diff
changeset
|
43 NS_AP, CONTENT_TYPE_AP, TYPE_ACTOR, TYPE_INBOX, TYPE_SHARED_INBOX, TYPE_OUTBOX, |
3745
a8c7e5cef0cb
comp AP gateway: signature checking, caching and threads management:
Goffi <goffi@goffi.org>
parents:
3729
diff
changeset
|
44 AP_REQUEST_TYPES, PAGE_SIZE, ACTIVITY_TYPES_LOWER, ACTIVIY_NO_ACCOUNT_ALLOWED, |
3869
c0bcbcf5b4b7
component AP gateway: handle `Like` and `Undo`/`Like` activities:
Goffi <goffi@goffi.org>
parents:
3847
diff
changeset
|
45 SIGN_HEADERS, HS2019, SIGN_EXP, TYPE_FOLLOWERS, TYPE_FOLLOWING, TYPE_ITEM, TYPE_LIKE |
3745
a8c7e5cef0cb
comp AP gateway: signature checking, caching and threads management:
Goffi <goffi@goffi.org>
parents:
3729
diff
changeset
|
46 ) |
a8c7e5cef0cb
comp AP gateway: signature checking, caching and threads management:
Goffi <goffi@goffi.org>
parents:
3729
diff
changeset
|
47 from .regex import RE_SIG_PARAM |
3682
7c990aaa49d3
comp AP Gateway: ActivityPub Component first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
48 |
7c990aaa49d3
comp AP Gateway: ActivityPub Component first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
49 |
7c990aaa49d3
comp AP Gateway: ActivityPub Component first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
50 log = getLogger(__name__) |
7c990aaa49d3
comp AP Gateway: ActivityPub Component first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
51 |
3728
b15644cae50d
component AP gateway: JID/node ⟺ AP outbox conversion:
Goffi <goffi@goffi.org>
parents:
3684
diff
changeset
|
52 VERSION = unicodedata.normalize( |
b15644cae50d
component AP gateway: JID/node ⟺ AP outbox conversion:
Goffi <goffi@goffi.org>
parents:
3684
diff
changeset
|
53 'NFKD', |
b15644cae50d
component AP gateway: JID/node ⟺ AP outbox conversion:
Goffi <goffi@goffi.org>
parents:
3684
diff
changeset
|
54 f"{C.APP_NAME} ActivityPub Gateway {C.APP_VERSION}" |
b15644cae50d
component AP gateway: JID/node ⟺ AP outbox conversion:
Goffi <goffi@goffi.org>
parents:
3684
diff
changeset
|
55 ) |
3682
7c990aaa49d3
comp AP Gateway: ActivityPub Component first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
56 |
7c990aaa49d3
comp AP Gateway: ActivityPub Component first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
57 |
7c990aaa49d3
comp AP Gateway: ActivityPub Component first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
58 class HTTPAPGServer(web_resource.Resource): |
7c990aaa49d3
comp AP Gateway: ActivityPub Component first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
59 """HTTP Server handling ActivityPub S2S protocol""" |
7c990aaa49d3
comp AP Gateway: ActivityPub Component first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
60 isLeaf = True |
7c990aaa49d3
comp AP Gateway: ActivityPub Component first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
61 |
7c990aaa49d3
comp AP Gateway: ActivityPub Component first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
62 def __init__(self, ap_gateway): |
7c990aaa49d3
comp AP Gateway: ActivityPub Component first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
63 self.apg = ap_gateway |
3745
a8c7e5cef0cb
comp AP gateway: signature checking, caching and threads management:
Goffi <goffi@goffi.org>
parents:
3729
diff
changeset
|
64 self._seen_digest = deque(maxlen=50) |
3682
7c990aaa49d3
comp AP Gateway: ActivityPub Component first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
65 super().__init__() |
7c990aaa49d3
comp AP Gateway: ActivityPub Component first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
66 |
3745
a8c7e5cef0cb
comp AP gateway: signature checking, caching and threads management:
Goffi <goffi@goffi.org>
parents:
3729
diff
changeset
|
67 def responseCode( |
a8c7e5cef0cb
comp AP gateway: signature checking, caching and threads management:
Goffi <goffi@goffi.org>
parents:
3729
diff
changeset
|
68 self, |
a8c7e5cef0cb
comp AP gateway: signature checking, caching and threads management:
Goffi <goffi@goffi.org>
parents:
3729
diff
changeset
|
69 request: "HTTPRequest", |
a8c7e5cef0cb
comp AP gateway: signature checking, caching and threads management:
Goffi <goffi@goffi.org>
parents:
3729
diff
changeset
|
70 http_code: int, |
a8c7e5cef0cb
comp AP gateway: signature checking, caching and threads management:
Goffi <goffi@goffi.org>
parents:
3729
diff
changeset
|
71 msg: Optional[str] = None |
a8c7e5cef0cb
comp AP gateway: signature checking, caching and threads management:
Goffi <goffi@goffi.org>
parents:
3729
diff
changeset
|
72 ) -> None: |
a8c7e5cef0cb
comp AP gateway: signature checking, caching and threads management:
Goffi <goffi@goffi.org>
parents:
3729
diff
changeset
|
73 """Log and set HTTP return code and associated message""" |
3833
381340b9a9ee
component AP gateway: convert XMPP mentions to AP:
Goffi <goffi@goffi.org>
parents:
3826
diff
changeset
|
74 if msg is not None: |
381340b9a9ee
component AP gateway: convert XMPP mentions to AP:
Goffi <goffi@goffi.org>
parents:
3826
diff
changeset
|
75 log.warning(msg) |
3745
a8c7e5cef0cb
comp AP gateway: signature checking, caching and threads management:
Goffi <goffi@goffi.org>
parents:
3729
diff
changeset
|
76 request.setResponseCode(http_code, None if msg is None else msg.encode()) |
a8c7e5cef0cb
comp AP gateway: signature checking, caching and threads management:
Goffi <goffi@goffi.org>
parents:
3729
diff
changeset
|
77 |
3764
125c7043b277
comp AP gateway: publish, (un)subscribe/(un)follow, public subscription/following/followers:
Goffi <goffi@goffi.org>
parents:
3745
diff
changeset
|
78 def _onRequestError(self, failure_: failure.Failure, request: "HTTPRequest") -> None: |
125c7043b277
comp AP gateway: publish, (un)subscribe/(un)follow, public subscription/following/followers:
Goffi <goffi@goffi.org>
parents:
3745
diff
changeset
|
79 log.error(f"Internal error: {failure_.value}") |
125c7043b277
comp AP gateway: publish, (un)subscribe/(un)follow, public subscription/following/followers:
Goffi <goffi@goffi.org>
parents:
3745
diff
changeset
|
80 self.responseCode( |
125c7043b277
comp AP gateway: publish, (un)subscribe/(un)follow, public subscription/following/followers:
Goffi <goffi@goffi.org>
parents:
3745
diff
changeset
|
81 request, |
125c7043b277
comp AP gateway: publish, (un)subscribe/(un)follow, public subscription/following/followers:
Goffi <goffi@goffi.org>
parents:
3745
diff
changeset
|
82 http.INTERNAL_SERVER_ERROR, |
125c7043b277
comp AP gateway: publish, (un)subscribe/(un)follow, public subscription/following/followers:
Goffi <goffi@goffi.org>
parents:
3745
diff
changeset
|
83 f"internal error: {failure_.value}" |
125c7043b277
comp AP gateway: publish, (un)subscribe/(un)follow, public subscription/following/followers:
Goffi <goffi@goffi.org>
parents:
3745
diff
changeset
|
84 ) |
125c7043b277
comp AP gateway: publish, (un)subscribe/(un)follow, public subscription/following/followers:
Goffi <goffi@goffi.org>
parents:
3745
diff
changeset
|
85 request.finish() |
125c7043b277
comp AP gateway: publish, (un)subscribe/(un)follow, public subscription/following/followers:
Goffi <goffi@goffi.org>
parents:
3745
diff
changeset
|
86 |
3728
b15644cae50d
component AP gateway: JID/node ⟺ AP outbox conversion:
Goffi <goffi@goffi.org>
parents:
3684
diff
changeset
|
87 async def webfinger(self, request): |
3682
7c990aaa49d3
comp AP Gateway: ActivityPub Component first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
88 url_parsed = parse.urlparse(request.uri.decode()) |
7c990aaa49d3
comp AP Gateway: ActivityPub Component first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
89 query = parse.parse_qs(url_parsed.query) |
7c990aaa49d3
comp AP Gateway: ActivityPub Component first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
90 resource = query.get("resource", [""])[0] |
7c990aaa49d3
comp AP Gateway: ActivityPub Component first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
91 account = resource[5:].strip() |
7c990aaa49d3
comp AP Gateway: ActivityPub Component first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
92 if not resource.startswith("acct:") or not account: |
7c990aaa49d3
comp AP Gateway: ActivityPub Component first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
93 return web_resource.ErrorPage( |
7c990aaa49d3
comp AP Gateway: ActivityPub Component first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
94 http.BAD_REQUEST, "Bad Request" , "Invalid webfinger resource" |
7c990aaa49d3
comp AP Gateway: ActivityPub Component first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
95 ).render(request) |
7c990aaa49d3
comp AP Gateway: ActivityPub Component first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
96 |
7c990aaa49d3
comp AP Gateway: ActivityPub Component first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
97 actor_url = self.apg.buildAPURL(TYPE_ACTOR, account) |
7c990aaa49d3
comp AP Gateway: ActivityPub Component first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
98 |
7c990aaa49d3
comp AP Gateway: ActivityPub Component first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
99 resp = { |
7c990aaa49d3
comp AP Gateway: ActivityPub Component first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
100 "subject": resource, |
7c990aaa49d3
comp AP Gateway: ActivityPub Component first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
101 "links": [ |
7c990aaa49d3
comp AP Gateway: ActivityPub Component first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
102 { |
7c990aaa49d3
comp AP Gateway: ActivityPub Component first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
103 "rel": "self", |
7c990aaa49d3
comp AP Gateway: ActivityPub Component first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
104 "type": "application/activity+json", |
7c990aaa49d3
comp AP Gateway: ActivityPub Component first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
105 "href": actor_url |
7c990aaa49d3
comp AP Gateway: ActivityPub Component first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
106 } |
7c990aaa49d3
comp AP Gateway: ActivityPub Component first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
107 ] |
7c990aaa49d3
comp AP Gateway: ActivityPub Component first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
108 } |
7c990aaa49d3
comp AP Gateway: ActivityPub Component first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
109 request.setHeader("content-type", CONTENT_TYPE_AP) |
3728
b15644cae50d
component AP gateway: JID/node ⟺ AP outbox conversion:
Goffi <goffi@goffi.org>
parents:
3684
diff
changeset
|
110 request.write(json.dumps(resp).encode()) |
b15644cae50d
component AP gateway: JID/node ⟺ AP outbox conversion:
Goffi <goffi@goffi.org>
parents:
3684
diff
changeset
|
111 request.finish() |
3682
7c990aaa49d3
comp AP Gateway: ActivityPub Component first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
112 |
3764
125c7043b277
comp AP gateway: publish, (un)subscribe/(un)follow, public subscription/following/followers:
Goffi <goffi@goffi.org>
parents:
3745
diff
changeset
|
113 async def handleUndoActivity( |
125c7043b277
comp AP gateway: publish, (un)subscribe/(un)follow, public subscription/following/followers:
Goffi <goffi@goffi.org>
parents:
3745
diff
changeset
|
114 self, |
125c7043b277
comp AP gateway: publish, (un)subscribe/(un)follow, public subscription/following/followers:
Goffi <goffi@goffi.org>
parents:
3745
diff
changeset
|
115 request: "HTTPRequest", |
125c7043b277
comp AP gateway: publish, (un)subscribe/(un)follow, public subscription/following/followers:
Goffi <goffi@goffi.org>
parents:
3745
diff
changeset
|
116 data: dict, |
125c7043b277
comp AP gateway: publish, (un)subscribe/(un)follow, public subscription/following/followers:
Goffi <goffi@goffi.org>
parents:
3745
diff
changeset
|
117 account_jid: jid.JID, |
125c7043b277
comp AP gateway: publish, (un)subscribe/(un)follow, public subscription/following/followers:
Goffi <goffi@goffi.org>
parents:
3745
diff
changeset
|
118 node: Optional[str], |
125c7043b277
comp AP gateway: publish, (un)subscribe/(un)follow, public subscription/following/followers:
Goffi <goffi@goffi.org>
parents:
3745
diff
changeset
|
119 ap_account: str, |
125c7043b277
comp AP gateway: publish, (un)subscribe/(un)follow, public subscription/following/followers:
Goffi <goffi@goffi.org>
parents:
3745
diff
changeset
|
120 ap_url: str, |
125c7043b277
comp AP gateway: publish, (un)subscribe/(un)follow, public subscription/following/followers:
Goffi <goffi@goffi.org>
parents:
3745
diff
changeset
|
121 signing_actor: str |
125c7043b277
comp AP gateway: publish, (un)subscribe/(un)follow, public subscription/following/followers:
Goffi <goffi@goffi.org>
parents:
3745
diff
changeset
|
122 ) -> None: |
125c7043b277
comp AP gateway: publish, (un)subscribe/(un)follow, public subscription/following/followers:
Goffi <goffi@goffi.org>
parents:
3745
diff
changeset
|
123 if node is None: |
125c7043b277
comp AP gateway: publish, (un)subscribe/(un)follow, public subscription/following/followers:
Goffi <goffi@goffi.org>
parents:
3745
diff
changeset
|
124 node = self.apg._m.namespace |
125c7043b277
comp AP gateway: publish, (un)subscribe/(un)follow, public subscription/following/followers:
Goffi <goffi@goffi.org>
parents:
3745
diff
changeset
|
125 client = await self.apg.getVirtualClient(signing_actor) |
125c7043b277
comp AP gateway: publish, (un)subscribe/(un)follow, public subscription/following/followers:
Goffi <goffi@goffi.org>
parents:
3745
diff
changeset
|
126 objects = await self.apg.apGetList(data, "object") |
125c7043b277
comp AP gateway: publish, (un)subscribe/(un)follow, public subscription/following/followers:
Goffi <goffi@goffi.org>
parents:
3745
diff
changeset
|
127 for obj in objects: |
125c7043b277
comp AP gateway: publish, (un)subscribe/(un)follow, public subscription/following/followers:
Goffi <goffi@goffi.org>
parents:
3745
diff
changeset
|
128 type_ = obj.get("type") |
125c7043b277
comp AP gateway: publish, (un)subscribe/(un)follow, public subscription/following/followers:
Goffi <goffi@goffi.org>
parents:
3745
diff
changeset
|
129 actor = await self.apg.apGetSenderActor(obj) |
125c7043b277
comp AP gateway: publish, (un)subscribe/(un)follow, public subscription/following/followers:
Goffi <goffi@goffi.org>
parents:
3745
diff
changeset
|
130 if actor != signing_actor: |
125c7043b277
comp AP gateway: publish, (un)subscribe/(un)follow, public subscription/following/followers:
Goffi <goffi@goffi.org>
parents:
3745
diff
changeset
|
131 log.warning(f"ignoring object not attributed to signing actor: {data}") |
125c7043b277
comp AP gateway: publish, (un)subscribe/(un)follow, public subscription/following/followers:
Goffi <goffi@goffi.org>
parents:
3745
diff
changeset
|
132 continue |
125c7043b277
comp AP gateway: publish, (un)subscribe/(un)follow, public subscription/following/followers:
Goffi <goffi@goffi.org>
parents:
3745
diff
changeset
|
133 |
125c7043b277
comp AP gateway: publish, (un)subscribe/(un)follow, public subscription/following/followers:
Goffi <goffi@goffi.org>
parents:
3745
diff
changeset
|
134 if type_ == "Follow": |
3844
65e5718e7710
component AP gateway: `Announce` activity implementation:
Goffi <goffi@goffi.org>
parents:
3833
diff
changeset
|
135 try: |
65e5718e7710
component AP gateway: `Announce` activity implementation:
Goffi <goffi@goffi.org>
parents:
3833
diff
changeset
|
136 target_account = obj["object"] |
65e5718e7710
component AP gateway: `Announce` activity implementation:
Goffi <goffi@goffi.org>
parents:
3833
diff
changeset
|
137 except KeyError: |
65e5718e7710
component AP gateway: `Announce` activity implementation:
Goffi <goffi@goffi.org>
parents:
3833
diff
changeset
|
138 log.warning(f'ignoring invalid object, missing "object" key: {data}') |
65e5718e7710
component AP gateway: `Announce` activity implementation:
Goffi <goffi@goffi.org>
parents:
3833
diff
changeset
|
139 continue |
65e5718e7710
component AP gateway: `Announce` activity implementation:
Goffi <goffi@goffi.org>
parents:
3833
diff
changeset
|
140 if not self.apg.isLocalURL(target_account): |
65e5718e7710
component AP gateway: `Announce` activity implementation:
Goffi <goffi@goffi.org>
parents:
3833
diff
changeset
|
141 log.warning(f"ignoring unfollow request to non local actor: {data}") |
65e5718e7710
component AP gateway: `Announce` activity implementation:
Goffi <goffi@goffi.org>
parents:
3833
diff
changeset
|
142 continue |
3764
125c7043b277
comp AP gateway: publish, (un)subscribe/(un)follow, public subscription/following/followers:
Goffi <goffi@goffi.org>
parents:
3745
diff
changeset
|
143 await self.apg._p.unsubscribe( |
125c7043b277
comp AP gateway: publish, (un)subscribe/(un)follow, public subscription/following/followers:
Goffi <goffi@goffi.org>
parents:
3745
diff
changeset
|
144 client, |
125c7043b277
comp AP gateway: publish, (un)subscribe/(un)follow, public subscription/following/followers:
Goffi <goffi@goffi.org>
parents:
3745
diff
changeset
|
145 account_jid, |
125c7043b277
comp AP gateway: publish, (un)subscribe/(un)follow, public subscription/following/followers:
Goffi <goffi@goffi.org>
parents:
3745
diff
changeset
|
146 node, |
125c7043b277
comp AP gateway: publish, (un)subscribe/(un)follow, public subscription/following/followers:
Goffi <goffi@goffi.org>
parents:
3745
diff
changeset
|
147 sender=client.jid, |
125c7043b277
comp AP gateway: publish, (un)subscribe/(un)follow, public subscription/following/followers:
Goffi <goffi@goffi.org>
parents:
3745
diff
changeset
|
148 ) |
3844
65e5718e7710
component AP gateway: `Announce` activity implementation:
Goffi <goffi@goffi.org>
parents:
3833
diff
changeset
|
149 elif type_ == "Announce": |
65e5718e7710
component AP gateway: `Announce` activity implementation:
Goffi <goffi@goffi.org>
parents:
3833
diff
changeset
|
150 # we can use directly the Announce object, as only the "id" field is |
65e5718e7710
component AP gateway: `Announce` activity implementation:
Goffi <goffi@goffi.org>
parents:
3833
diff
changeset
|
151 # needed |
65e5718e7710
component AP gateway: `Announce` activity implementation:
Goffi <goffi@goffi.org>
parents:
3833
diff
changeset
|
152 await self.apg.newAPDeleteItem(client, None, node, obj) |
3869
c0bcbcf5b4b7
component AP gateway: handle `Like` and `Undo`/`Like` activities:
Goffi <goffi@goffi.org>
parents:
3847
diff
changeset
|
153 elif type_ == TYPE_LIKE: |
c0bcbcf5b4b7
component AP gateway: handle `Like` and `Undo`/`Like` activities:
Goffi <goffi@goffi.org>
parents:
3847
diff
changeset
|
154 await self.handleNewLikeItem(client, obj, True) |
3764
125c7043b277
comp AP gateway: publish, (un)subscribe/(un)follow, public subscription/following/followers:
Goffi <goffi@goffi.org>
parents:
3745
diff
changeset
|
155 else: |
125c7043b277
comp AP gateway: publish, (un)subscribe/(un)follow, public subscription/following/followers:
Goffi <goffi@goffi.org>
parents:
3745
diff
changeset
|
156 log.warning(f"Unmanaged undo type: {type_!r}") |
125c7043b277
comp AP gateway: publish, (un)subscribe/(un)follow, public subscription/following/followers:
Goffi <goffi@goffi.org>
parents:
3745
diff
changeset
|
157 |
3745
a8c7e5cef0cb
comp AP gateway: signature checking, caching and threads management:
Goffi <goffi@goffi.org>
parents:
3729
diff
changeset
|
158 async def handleFollowActivity( |
a8c7e5cef0cb
comp AP gateway: signature checking, caching and threads management:
Goffi <goffi@goffi.org>
parents:
3729
diff
changeset
|
159 self, |
a8c7e5cef0cb
comp AP gateway: signature checking, caching and threads management:
Goffi <goffi@goffi.org>
parents:
3729
diff
changeset
|
160 request: "HTTPRequest", |
a8c7e5cef0cb
comp AP gateway: signature checking, caching and threads management:
Goffi <goffi@goffi.org>
parents:
3729
diff
changeset
|
161 data: dict, |
a8c7e5cef0cb
comp AP gateway: signature checking, caching and threads management:
Goffi <goffi@goffi.org>
parents:
3729
diff
changeset
|
162 account_jid: jid.JID, |
a8c7e5cef0cb
comp AP gateway: signature checking, caching and threads management:
Goffi <goffi@goffi.org>
parents:
3729
diff
changeset
|
163 node: Optional[str], |
a8c7e5cef0cb
comp AP gateway: signature checking, caching and threads management:
Goffi <goffi@goffi.org>
parents:
3729
diff
changeset
|
164 ap_account: str, |
a8c7e5cef0cb
comp AP gateway: signature checking, caching and threads management:
Goffi <goffi@goffi.org>
parents:
3729
diff
changeset
|
165 ap_url: str, |
a8c7e5cef0cb
comp AP gateway: signature checking, caching and threads management:
Goffi <goffi@goffi.org>
parents:
3729
diff
changeset
|
166 signing_actor: str |
a8c7e5cef0cb
comp AP gateway: signature checking, caching and threads management:
Goffi <goffi@goffi.org>
parents:
3729
diff
changeset
|
167 ) -> None: |
a8c7e5cef0cb
comp AP gateway: signature checking, caching and threads management:
Goffi <goffi@goffi.org>
parents:
3729
diff
changeset
|
168 if node is None: |
a8c7e5cef0cb
comp AP gateway: signature checking, caching and threads management:
Goffi <goffi@goffi.org>
parents:
3729
diff
changeset
|
169 node = self.apg._m.namespace |
a8c7e5cef0cb
comp AP gateway: signature checking, caching and threads management:
Goffi <goffi@goffi.org>
parents:
3729
diff
changeset
|
170 client = await self.apg.getVirtualClient(signing_actor) |
a8c7e5cef0cb
comp AP gateway: signature checking, caching and threads management:
Goffi <goffi@goffi.org>
parents:
3729
diff
changeset
|
171 try: |
a8c7e5cef0cb
comp AP gateway: signature checking, caching and threads management:
Goffi <goffi@goffi.org>
parents:
3729
diff
changeset
|
172 subscription = await self.apg._p.subscribe( |
a8c7e5cef0cb
comp AP gateway: signature checking, caching and threads management:
Goffi <goffi@goffi.org>
parents:
3729
diff
changeset
|
173 client, |
a8c7e5cef0cb
comp AP gateway: signature checking, caching and threads management:
Goffi <goffi@goffi.org>
parents:
3729
diff
changeset
|
174 account_jid, |
3764
125c7043b277
comp AP gateway: publish, (un)subscribe/(un)follow, public subscription/following/followers:
Goffi <goffi@goffi.org>
parents:
3745
diff
changeset
|
175 node, |
125c7043b277
comp AP gateway: publish, (un)subscribe/(un)follow, public subscription/following/followers:
Goffi <goffi@goffi.org>
parents:
3745
diff
changeset
|
176 # subscriptions from AP are always public |
125c7043b277
comp AP gateway: publish, (un)subscribe/(un)follow, public subscription/following/followers:
Goffi <goffi@goffi.org>
parents:
3745
diff
changeset
|
177 options=self.apg._pps.setPublicOpt() |
3745
a8c7e5cef0cb
comp AP gateway: signature checking, caching and threads management:
Goffi <goffi@goffi.org>
parents:
3729
diff
changeset
|
178 ) |
a8c7e5cef0cb
comp AP gateway: signature checking, caching and threads management:
Goffi <goffi@goffi.org>
parents:
3729
diff
changeset
|
179 except pubsub.SubscriptionPending: |
a8c7e5cef0cb
comp AP gateway: signature checking, caching and threads management:
Goffi <goffi@goffi.org>
parents:
3729
diff
changeset
|
180 log.info(f"subscription to node {node!r} of {account_jid} is pending") |
a8c7e5cef0cb
comp AP gateway: signature checking, caching and threads management:
Goffi <goffi@goffi.org>
parents:
3729
diff
changeset
|
181 # TODO: manage SubscriptionUnconfigured |
a8c7e5cef0cb
comp AP gateway: signature checking, caching and threads management:
Goffi <goffi@goffi.org>
parents:
3729
diff
changeset
|
182 else: |
a8c7e5cef0cb
comp AP gateway: signature checking, caching and threads management:
Goffi <goffi@goffi.org>
parents:
3729
diff
changeset
|
183 if subscription.state != "subscribed": |
a8c7e5cef0cb
comp AP gateway: signature checking, caching and threads management:
Goffi <goffi@goffi.org>
parents:
3729
diff
changeset
|
184 # other states should raise an Exception |
a8c7e5cef0cb
comp AP gateway: signature checking, caching and threads management:
Goffi <goffi@goffi.org>
parents:
3729
diff
changeset
|
185 raise exceptions.InternalError('"subscribed" state was expected') |
a8c7e5cef0cb
comp AP gateway: signature checking, caching and threads management:
Goffi <goffi@goffi.org>
parents:
3729
diff
changeset
|
186 inbox = await self.apg.getAPInboxFromId(signing_actor) |
a8c7e5cef0cb
comp AP gateway: signature checking, caching and threads management:
Goffi <goffi@goffi.org>
parents:
3729
diff
changeset
|
187 actor_id = self.apg.buildAPURL(TYPE_ACTOR, ap_account) |
a8c7e5cef0cb
comp AP gateway: signature checking, caching and threads management:
Goffi <goffi@goffi.org>
parents:
3729
diff
changeset
|
188 accept_data = self.apg.createActivity( |
a8c7e5cef0cb
comp AP gateway: signature checking, caching and threads management:
Goffi <goffi@goffi.org>
parents:
3729
diff
changeset
|
189 "Accept", actor_id, object_=data |
a8c7e5cef0cb
comp AP gateway: signature checking, caching and threads management:
Goffi <goffi@goffi.org>
parents:
3729
diff
changeset
|
190 ) |
a8c7e5cef0cb
comp AP gateway: signature checking, caching and threads management:
Goffi <goffi@goffi.org>
parents:
3729
diff
changeset
|
191 await self.apg.signAndPost(inbox, actor_id, accept_data) |
3764
125c7043b277
comp AP gateway: publish, (un)subscribe/(un)follow, public subscription/following/followers:
Goffi <goffi@goffi.org>
parents:
3745
diff
changeset
|
192 await self.apg._c.synchronise(client, account_jid, node, resync=False) |
3745
a8c7e5cef0cb
comp AP gateway: signature checking, caching and threads management:
Goffi <goffi@goffi.org>
parents:
3729
diff
changeset
|
193 |
a8c7e5cef0cb
comp AP gateway: signature checking, caching and threads management:
Goffi <goffi@goffi.org>
parents:
3729
diff
changeset
|
194 async def handleAcceptActivity( |
a8c7e5cef0cb
comp AP gateway: signature checking, caching and threads management:
Goffi <goffi@goffi.org>
parents:
3729
diff
changeset
|
195 self, |
a8c7e5cef0cb
comp AP gateway: signature checking, caching and threads management:
Goffi <goffi@goffi.org>
parents:
3729
diff
changeset
|
196 request: "HTTPRequest", |
a8c7e5cef0cb
comp AP gateway: signature checking, caching and threads management:
Goffi <goffi@goffi.org>
parents:
3729
diff
changeset
|
197 data: dict, |
a8c7e5cef0cb
comp AP gateway: signature checking, caching and threads management:
Goffi <goffi@goffi.org>
parents:
3729
diff
changeset
|
198 account_jid: jid.JID, |
a8c7e5cef0cb
comp AP gateway: signature checking, caching and threads management:
Goffi <goffi@goffi.org>
parents:
3729
diff
changeset
|
199 node: Optional[str], |
a8c7e5cef0cb
comp AP gateway: signature checking, caching and threads management:
Goffi <goffi@goffi.org>
parents:
3729
diff
changeset
|
200 ap_account: str, |
a8c7e5cef0cb
comp AP gateway: signature checking, caching and threads management:
Goffi <goffi@goffi.org>
parents:
3729
diff
changeset
|
201 ap_url: str, |
a8c7e5cef0cb
comp AP gateway: signature checking, caching and threads management:
Goffi <goffi@goffi.org>
parents:
3729
diff
changeset
|
202 signing_actor: str |
a8c7e5cef0cb
comp AP gateway: signature checking, caching and threads management:
Goffi <goffi@goffi.org>
parents:
3729
diff
changeset
|
203 ) -> None: |
a8c7e5cef0cb
comp AP gateway: signature checking, caching and threads management:
Goffi <goffi@goffi.org>
parents:
3729
diff
changeset
|
204 if node is None: |
a8c7e5cef0cb
comp AP gateway: signature checking, caching and threads management:
Goffi <goffi@goffi.org>
parents:
3729
diff
changeset
|
205 node = self.apg._m.namespace |
a8c7e5cef0cb
comp AP gateway: signature checking, caching and threads management:
Goffi <goffi@goffi.org>
parents:
3729
diff
changeset
|
206 client = await self.apg.getVirtualClient(signing_actor) |
a8c7e5cef0cb
comp AP gateway: signature checking, caching and threads management:
Goffi <goffi@goffi.org>
parents:
3729
diff
changeset
|
207 objects = await self.apg.apGetList(data, "object") |
a8c7e5cef0cb
comp AP gateway: signature checking, caching and threads management:
Goffi <goffi@goffi.org>
parents:
3729
diff
changeset
|
208 for obj in objects: |
a8c7e5cef0cb
comp AP gateway: signature checking, caching and threads management:
Goffi <goffi@goffi.org>
parents:
3729
diff
changeset
|
209 type_ = obj.get("type") |
a8c7e5cef0cb
comp AP gateway: signature checking, caching and threads management:
Goffi <goffi@goffi.org>
parents:
3729
diff
changeset
|
210 if type_ == "Follow": |
a8c7e5cef0cb
comp AP gateway: signature checking, caching and threads management:
Goffi <goffi@goffi.org>
parents:
3729
diff
changeset
|
211 follow_node = await self.apg.host.memory.storage.getPubsubNode( |
a8c7e5cef0cb
comp AP gateway: signature checking, caching and threads management:
Goffi <goffi@goffi.org>
parents:
3729
diff
changeset
|
212 client, client.jid, node, with_subscriptions=True |
a8c7e5cef0cb
comp AP gateway: signature checking, caching and threads management:
Goffi <goffi@goffi.org>
parents:
3729
diff
changeset
|
213 ) |
a8c7e5cef0cb
comp AP gateway: signature checking, caching and threads management:
Goffi <goffi@goffi.org>
parents:
3729
diff
changeset
|
214 if follow_node is None: |
a8c7e5cef0cb
comp AP gateway: signature checking, caching and threads management:
Goffi <goffi@goffi.org>
parents:
3729
diff
changeset
|
215 log.warning( |
a8c7e5cef0cb
comp AP gateway: signature checking, caching and threads management:
Goffi <goffi@goffi.org>
parents:
3729
diff
changeset
|
216 f"Received a follow accept on an unknown node: {node!r} at " |
a8c7e5cef0cb
comp AP gateway: signature checking, caching and threads management:
Goffi <goffi@goffi.org>
parents:
3729
diff
changeset
|
217 f"{client.jid}. Ignoring it" |
a8c7e5cef0cb
comp AP gateway: signature checking, caching and threads management:
Goffi <goffi@goffi.org>
parents:
3729
diff
changeset
|
218 ) |
a8c7e5cef0cb
comp AP gateway: signature checking, caching and threads management:
Goffi <goffi@goffi.org>
parents:
3729
diff
changeset
|
219 continue |
a8c7e5cef0cb
comp AP gateway: signature checking, caching and threads management:
Goffi <goffi@goffi.org>
parents:
3729
diff
changeset
|
220 try: |
a8c7e5cef0cb
comp AP gateway: signature checking, caching and threads management:
Goffi <goffi@goffi.org>
parents:
3729
diff
changeset
|
221 sub = next( |
a8c7e5cef0cb
comp AP gateway: signature checking, caching and threads management:
Goffi <goffi@goffi.org>
parents:
3729
diff
changeset
|
222 s for s in follow_node.subscriptions if s.subscriber==account_jid |
a8c7e5cef0cb
comp AP gateway: signature checking, caching and threads management:
Goffi <goffi@goffi.org>
parents:
3729
diff
changeset
|
223 ) |
a8c7e5cef0cb
comp AP gateway: signature checking, caching and threads management:
Goffi <goffi@goffi.org>
parents:
3729
diff
changeset
|
224 except StopIteration: |
a8c7e5cef0cb
comp AP gateway: signature checking, caching and threads management:
Goffi <goffi@goffi.org>
parents:
3729
diff
changeset
|
225 log.warning( |
a8c7e5cef0cb
comp AP gateway: signature checking, caching and threads management:
Goffi <goffi@goffi.org>
parents:
3729
diff
changeset
|
226 "Received a follow accept on a node without subscription: " |
a8c7e5cef0cb
comp AP gateway: signature checking, caching and threads management:
Goffi <goffi@goffi.org>
parents:
3729
diff
changeset
|
227 f"{node!r} at {client.jid}. Ignoring it" |
a8c7e5cef0cb
comp AP gateway: signature checking, caching and threads management:
Goffi <goffi@goffi.org>
parents:
3729
diff
changeset
|
228 ) |
a8c7e5cef0cb
comp AP gateway: signature checking, caching and threads management:
Goffi <goffi@goffi.org>
parents:
3729
diff
changeset
|
229 else: |
a8c7e5cef0cb
comp AP gateway: signature checking, caching and threads management:
Goffi <goffi@goffi.org>
parents:
3729
diff
changeset
|
230 if sub.state == SubscriptionState.SUBSCRIBED: |
a8c7e5cef0cb
comp AP gateway: signature checking, caching and threads management:
Goffi <goffi@goffi.org>
parents:
3729
diff
changeset
|
231 log.warning(f"Already subscribed to {node!r} at {client.jid}") |
a8c7e5cef0cb
comp AP gateway: signature checking, caching and threads management:
Goffi <goffi@goffi.org>
parents:
3729
diff
changeset
|
232 elif sub.state == SubscriptionState.PENDING: |
a8c7e5cef0cb
comp AP gateway: signature checking, caching and threads management:
Goffi <goffi@goffi.org>
parents:
3729
diff
changeset
|
233 follow_node.subscribed = True |
a8c7e5cef0cb
comp AP gateway: signature checking, caching and threads management:
Goffi <goffi@goffi.org>
parents:
3729
diff
changeset
|
234 sub.state = SubscriptionState.SUBSCRIBED |
a8c7e5cef0cb
comp AP gateway: signature checking, caching and threads management:
Goffi <goffi@goffi.org>
parents:
3729
diff
changeset
|
235 await self.apg.host.memory.storage.add(follow_node) |
a8c7e5cef0cb
comp AP gateway: signature checking, caching and threads management:
Goffi <goffi@goffi.org>
parents:
3729
diff
changeset
|
236 else: |
a8c7e5cef0cb
comp AP gateway: signature checking, caching and threads management:
Goffi <goffi@goffi.org>
parents:
3729
diff
changeset
|
237 raise exceptions.InternalError( |
a8c7e5cef0cb
comp AP gateway: signature checking, caching and threads management:
Goffi <goffi@goffi.org>
parents:
3729
diff
changeset
|
238 f"Unhandled subscription state {sub.state!r}" |
a8c7e5cef0cb
comp AP gateway: signature checking, caching and threads management:
Goffi <goffi@goffi.org>
parents:
3729
diff
changeset
|
239 ) |
a8c7e5cef0cb
comp AP gateway: signature checking, caching and threads management:
Goffi <goffi@goffi.org>
parents:
3729
diff
changeset
|
240 else: |
a8c7e5cef0cb
comp AP gateway: signature checking, caching and threads management:
Goffi <goffi@goffi.org>
parents:
3729
diff
changeset
|
241 log.warning(f"Unmanaged accept type: {type_!r}") |
a8c7e5cef0cb
comp AP gateway: signature checking, caching and threads management:
Goffi <goffi@goffi.org>
parents:
3729
diff
changeset
|
242 |
3793
b5c9021020df
component AP gateway: convert `Delete` AP activities to corresponding Pubsub `retract`:
Goffi <goffi@goffi.org>
parents:
3764
diff
changeset
|
243 async def handleDeleteActivity( |
b5c9021020df
component AP gateway: convert `Delete` AP activities to corresponding Pubsub `retract`:
Goffi <goffi@goffi.org>
parents:
3764
diff
changeset
|
244 self, |
b5c9021020df
component AP gateway: convert `Delete` AP activities to corresponding Pubsub `retract`:
Goffi <goffi@goffi.org>
parents:
3764
diff
changeset
|
245 request: "HTTPRequest", |
b5c9021020df
component AP gateway: convert `Delete` AP activities to corresponding Pubsub `retract`:
Goffi <goffi@goffi.org>
parents:
3764
diff
changeset
|
246 data: dict, |
b5c9021020df
component AP gateway: convert `Delete` AP activities to corresponding Pubsub `retract`:
Goffi <goffi@goffi.org>
parents:
3764
diff
changeset
|
247 account_jid: Optional[jid.JID], |
b5c9021020df
component AP gateway: convert `Delete` AP activities to corresponding Pubsub `retract`:
Goffi <goffi@goffi.org>
parents:
3764
diff
changeset
|
248 node: Optional[str], |
b5c9021020df
component AP gateway: convert `Delete` AP activities to corresponding Pubsub `retract`:
Goffi <goffi@goffi.org>
parents:
3764
diff
changeset
|
249 ap_account: Optional[str], |
b5c9021020df
component AP gateway: convert `Delete` AP activities to corresponding Pubsub `retract`:
Goffi <goffi@goffi.org>
parents:
3764
diff
changeset
|
250 ap_url: str, |
b5c9021020df
component AP gateway: convert `Delete` AP activities to corresponding Pubsub `retract`:
Goffi <goffi@goffi.org>
parents:
3764
diff
changeset
|
251 signing_actor: str |
b5c9021020df
component AP gateway: convert `Delete` AP activities to corresponding Pubsub `retract`:
Goffi <goffi@goffi.org>
parents:
3764
diff
changeset
|
252 ): |
b5c9021020df
component AP gateway: convert `Delete` AP activities to corresponding Pubsub `retract`:
Goffi <goffi@goffi.org>
parents:
3764
diff
changeset
|
253 if node is None: |
b5c9021020df
component AP gateway: convert `Delete` AP activities to corresponding Pubsub `retract`:
Goffi <goffi@goffi.org>
parents:
3764
diff
changeset
|
254 node = self.apg._m.namespace |
b5c9021020df
component AP gateway: convert `Delete` AP activities to corresponding Pubsub `retract`:
Goffi <goffi@goffi.org>
parents:
3764
diff
changeset
|
255 client = await self.apg.getVirtualClient(signing_actor) |
b5c9021020df
component AP gateway: convert `Delete` AP activities to corresponding Pubsub `retract`:
Goffi <goffi@goffi.org>
parents:
3764
diff
changeset
|
256 objects = await self.apg.apGetList(data, "object") |
b5c9021020df
component AP gateway: convert `Delete` AP activities to corresponding Pubsub `retract`:
Goffi <goffi@goffi.org>
parents:
3764
diff
changeset
|
257 for obj in objects: |
3807
2032826cfbcf
component AP gateway typing + remove unused `activity` arg from `newAPDeleteItem`
Goffi <goffi@goffi.org>
parents:
3804
diff
changeset
|
258 await self.apg.newAPDeleteItem(client, account_jid, node, obj) |
3793
b5c9021020df
component AP gateway: convert `Delete` AP activities to corresponding Pubsub `retract`:
Goffi <goffi@goffi.org>
parents:
3764
diff
changeset
|
259 |
3844
65e5718e7710
component AP gateway: `Announce` activity implementation:
Goffi <goffi@goffi.org>
parents:
3833
diff
changeset
|
260 async def handleNewAPItems( |
65e5718e7710
component AP gateway: `Announce` activity implementation:
Goffi <goffi@goffi.org>
parents:
3833
diff
changeset
|
261 self, |
65e5718e7710
component AP gateway: `Announce` activity implementation:
Goffi <goffi@goffi.org>
parents:
3833
diff
changeset
|
262 request: "HTTPRequest", |
65e5718e7710
component AP gateway: `Announce` activity implementation:
Goffi <goffi@goffi.org>
parents:
3833
diff
changeset
|
263 data: dict, |
65e5718e7710
component AP gateway: `Announce` activity implementation:
Goffi <goffi@goffi.org>
parents:
3833
diff
changeset
|
264 account_jid: Optional[jid.JID], |
65e5718e7710
component AP gateway: `Announce` activity implementation:
Goffi <goffi@goffi.org>
parents:
3833
diff
changeset
|
265 node: Optional[str], |
65e5718e7710
component AP gateway: `Announce` activity implementation:
Goffi <goffi@goffi.org>
parents:
3833
diff
changeset
|
266 signing_actor: str, |
65e5718e7710
component AP gateway: `Announce` activity implementation:
Goffi <goffi@goffi.org>
parents:
3833
diff
changeset
|
267 repeated: bool = False, |
65e5718e7710
component AP gateway: `Announce` activity implementation:
Goffi <goffi@goffi.org>
parents:
3833
diff
changeset
|
268 ): |
65e5718e7710
component AP gateway: `Announce` activity implementation:
Goffi <goffi@goffi.org>
parents:
3833
diff
changeset
|
269 """Helper method to handle workflow for new AP items |
65e5718e7710
component AP gateway: `Announce` activity implementation:
Goffi <goffi@goffi.org>
parents:
3833
diff
changeset
|
270 |
65e5718e7710
component AP gateway: `Announce` activity implementation:
Goffi <goffi@goffi.org>
parents:
3833
diff
changeset
|
271 accept globally the same parameter as for handleCreateActivity |
65e5718e7710
component AP gateway: `Announce` activity implementation:
Goffi <goffi@goffi.org>
parents:
3833
diff
changeset
|
272 @param repeated: if True, the item is an item republished from somewhere else |
65e5718e7710
component AP gateway: `Announce` activity implementation:
Goffi <goffi@goffi.org>
parents:
3833
diff
changeset
|
273 """ |
65e5718e7710
component AP gateway: `Announce` activity implementation:
Goffi <goffi@goffi.org>
parents:
3833
diff
changeset
|
274 if "_repeated" in data: |
65e5718e7710
component AP gateway: `Announce` activity implementation:
Goffi <goffi@goffi.org>
parents:
3833
diff
changeset
|
275 log.error( |
65e5718e7710
component AP gateway: `Announce` activity implementation:
Goffi <goffi@goffi.org>
parents:
3833
diff
changeset
|
276 '"_repeated" field already present in given AP item, this should not ' |
65e5718e7710
component AP gateway: `Announce` activity implementation:
Goffi <goffi@goffi.org>
parents:
3833
diff
changeset
|
277 f"happen. Ignoring object from {signing_actor}\n{data}" |
65e5718e7710
component AP gateway: `Announce` activity implementation:
Goffi <goffi@goffi.org>
parents:
3833
diff
changeset
|
278 ) |
65e5718e7710
component AP gateway: `Announce` activity implementation:
Goffi <goffi@goffi.org>
parents:
3833
diff
changeset
|
279 raise exceptions.DataError("unexpected field in item") |
65e5718e7710
component AP gateway: `Announce` activity implementation:
Goffi <goffi@goffi.org>
parents:
3833
diff
changeset
|
280 if node is None: |
65e5718e7710
component AP gateway: `Announce` activity implementation:
Goffi <goffi@goffi.org>
parents:
3833
diff
changeset
|
281 node = self.apg._m.namespace |
65e5718e7710
component AP gateway: `Announce` activity implementation:
Goffi <goffi@goffi.org>
parents:
3833
diff
changeset
|
282 client = await self.apg.getVirtualClient(signing_actor) |
65e5718e7710
component AP gateway: `Announce` activity implementation:
Goffi <goffi@goffi.org>
parents:
3833
diff
changeset
|
283 objects = await self.apg.apGetList(data, "object") |
65e5718e7710
component AP gateway: `Announce` activity implementation:
Goffi <goffi@goffi.org>
parents:
3833
diff
changeset
|
284 for obj in objects: |
65e5718e7710
component AP gateway: `Announce` activity implementation:
Goffi <goffi@goffi.org>
parents:
3833
diff
changeset
|
285 sender = await self.apg.apGetSenderActor(obj) |
65e5718e7710
component AP gateway: `Announce` activity implementation:
Goffi <goffi@goffi.org>
parents:
3833
diff
changeset
|
286 if repeated: |
65e5718e7710
component AP gateway: `Announce` activity implementation:
Goffi <goffi@goffi.org>
parents:
3833
diff
changeset
|
287 # we don't check sender when item is repeated, as it should be different |
65e5718e7710
component AP gateway: `Announce` activity implementation:
Goffi <goffi@goffi.org>
parents:
3833
diff
changeset
|
288 # from post author in this case |
65e5718e7710
component AP gateway: `Announce` activity implementation:
Goffi <goffi@goffi.org>
parents:
3833
diff
changeset
|
289 sender_jid = await self.apg.getJIDFromId(sender) |
65e5718e7710
component AP gateway: `Announce` activity implementation:
Goffi <goffi@goffi.org>
parents:
3833
diff
changeset
|
290 repeater_jid = await self.apg.getJIDFromId(signing_actor) |
65e5718e7710
component AP gateway: `Announce` activity implementation:
Goffi <goffi@goffi.org>
parents:
3833
diff
changeset
|
291 |
65e5718e7710
component AP gateway: `Announce` activity implementation:
Goffi <goffi@goffi.org>
parents:
3833
diff
changeset
|
292 obj["_repeated"] = { |
65e5718e7710
component AP gateway: `Announce` activity implementation:
Goffi <goffi@goffi.org>
parents:
3833
diff
changeset
|
293 "by": repeater_jid.full(), |
65e5718e7710
component AP gateway: `Announce` activity implementation:
Goffi <goffi@goffi.org>
parents:
3833
diff
changeset
|
294 "at": data.get("published"), |
65e5718e7710
component AP gateway: `Announce` activity implementation:
Goffi <goffi@goffi.org>
parents:
3833
diff
changeset
|
295 "uri": uri.buildXMPPUri( |
65e5718e7710
component AP gateway: `Announce` activity implementation:
Goffi <goffi@goffi.org>
parents:
3833
diff
changeset
|
296 "pubsub", |
65e5718e7710
component AP gateway: `Announce` activity implementation:
Goffi <goffi@goffi.org>
parents:
3833
diff
changeset
|
297 path=sender_jid.full(), |
65e5718e7710
component AP gateway: `Announce` activity implementation:
Goffi <goffi@goffi.org>
parents:
3833
diff
changeset
|
298 node=self.apg._m.namespace, |
65e5718e7710
component AP gateway: `Announce` activity implementation:
Goffi <goffi@goffi.org>
parents:
3833
diff
changeset
|
299 item=obj["id"] |
65e5718e7710
component AP gateway: `Announce` activity implementation:
Goffi <goffi@goffi.org>
parents:
3833
diff
changeset
|
300 ) |
65e5718e7710
component AP gateway: `Announce` activity implementation:
Goffi <goffi@goffi.org>
parents:
3833
diff
changeset
|
301 } |
65e5718e7710
component AP gateway: `Announce` activity implementation:
Goffi <goffi@goffi.org>
parents:
3833
diff
changeset
|
302 # we must use activity's id and targets, not the original item ones |
65e5718e7710
component AP gateway: `Announce` activity implementation:
Goffi <goffi@goffi.org>
parents:
3833
diff
changeset
|
303 for field in ("id", "to", "bto", "cc", "bcc"): |
65e5718e7710
component AP gateway: `Announce` activity implementation:
Goffi <goffi@goffi.org>
parents:
3833
diff
changeset
|
304 obj[field] = data.get(field) |
65e5718e7710
component AP gateway: `Announce` activity implementation:
Goffi <goffi@goffi.org>
parents:
3833
diff
changeset
|
305 else: |
65e5718e7710
component AP gateway: `Announce` activity implementation:
Goffi <goffi@goffi.org>
parents:
3833
diff
changeset
|
306 if sender != signing_actor: |
65e5718e7710
component AP gateway: `Announce` activity implementation:
Goffi <goffi@goffi.org>
parents:
3833
diff
changeset
|
307 log.warning( |
65e5718e7710
component AP gateway: `Announce` activity implementation:
Goffi <goffi@goffi.org>
parents:
3833
diff
changeset
|
308 "Ignoring object not attributed to signing actor: {obj}" |
65e5718e7710
component AP gateway: `Announce` activity implementation:
Goffi <goffi@goffi.org>
parents:
3833
diff
changeset
|
309 ) |
65e5718e7710
component AP gateway: `Announce` activity implementation:
Goffi <goffi@goffi.org>
parents:
3833
diff
changeset
|
310 continue |
65e5718e7710
component AP gateway: `Announce` activity implementation:
Goffi <goffi@goffi.org>
parents:
3833
diff
changeset
|
311 await self.apg.newAPItem(client, account_jid, node, obj) |
65e5718e7710
component AP gateway: `Announce` activity implementation:
Goffi <goffi@goffi.org>
parents:
3833
diff
changeset
|
312 |
3745
a8c7e5cef0cb
comp AP gateway: signature checking, caching and threads management:
Goffi <goffi@goffi.org>
parents:
3729
diff
changeset
|
313 async def handleCreateActivity( |
a8c7e5cef0cb
comp AP gateway: signature checking, caching and threads management:
Goffi <goffi@goffi.org>
parents:
3729
diff
changeset
|
314 self, |
a8c7e5cef0cb
comp AP gateway: signature checking, caching and threads management:
Goffi <goffi@goffi.org>
parents:
3729
diff
changeset
|
315 request: "HTTPRequest", |
a8c7e5cef0cb
comp AP gateway: signature checking, caching and threads management:
Goffi <goffi@goffi.org>
parents:
3729
diff
changeset
|
316 data: dict, |
a8c7e5cef0cb
comp AP gateway: signature checking, caching and threads management:
Goffi <goffi@goffi.org>
parents:
3729
diff
changeset
|
317 account_jid: Optional[jid.JID], |
a8c7e5cef0cb
comp AP gateway: signature checking, caching and threads management:
Goffi <goffi@goffi.org>
parents:
3729
diff
changeset
|
318 node: Optional[str], |
a8c7e5cef0cb
comp AP gateway: signature checking, caching and threads management:
Goffi <goffi@goffi.org>
parents:
3729
diff
changeset
|
319 ap_account: Optional[str], |
a8c7e5cef0cb
comp AP gateway: signature checking, caching and threads management:
Goffi <goffi@goffi.org>
parents:
3729
diff
changeset
|
320 ap_url: str, |
a8c7e5cef0cb
comp AP gateway: signature checking, caching and threads management:
Goffi <goffi@goffi.org>
parents:
3729
diff
changeset
|
321 signing_actor: str |
a8c7e5cef0cb
comp AP gateway: signature checking, caching and threads management:
Goffi <goffi@goffi.org>
parents:
3729
diff
changeset
|
322 ): |
3844
65e5718e7710
component AP gateway: `Announce` activity implementation:
Goffi <goffi@goffi.org>
parents:
3833
diff
changeset
|
323 await self.handleNewAPItems(request, data, account_jid, node, signing_actor) |
65e5718e7710
component AP gateway: `Announce` activity implementation:
Goffi <goffi@goffi.org>
parents:
3833
diff
changeset
|
324 |
65e5718e7710
component AP gateway: `Announce` activity implementation:
Goffi <goffi@goffi.org>
parents:
3833
diff
changeset
|
325 async def handleAnnounceActivity( |
65e5718e7710
component AP gateway: `Announce` activity implementation:
Goffi <goffi@goffi.org>
parents:
3833
diff
changeset
|
326 self, |
65e5718e7710
component AP gateway: `Announce` activity implementation:
Goffi <goffi@goffi.org>
parents:
3833
diff
changeset
|
327 request: "HTTPRequest", |
65e5718e7710
component AP gateway: `Announce` activity implementation:
Goffi <goffi@goffi.org>
parents:
3833
diff
changeset
|
328 data: dict, |
65e5718e7710
component AP gateway: `Announce` activity implementation:
Goffi <goffi@goffi.org>
parents:
3833
diff
changeset
|
329 account_jid: Optional[jid.JID], |
65e5718e7710
component AP gateway: `Announce` activity implementation:
Goffi <goffi@goffi.org>
parents:
3833
diff
changeset
|
330 node: Optional[str], |
65e5718e7710
component AP gateway: `Announce` activity implementation:
Goffi <goffi@goffi.org>
parents:
3833
diff
changeset
|
331 ap_account: Optional[str], |
65e5718e7710
component AP gateway: `Announce` activity implementation:
Goffi <goffi@goffi.org>
parents:
3833
diff
changeset
|
332 ap_url: str, |
65e5718e7710
component AP gateway: `Announce` activity implementation:
Goffi <goffi@goffi.org>
parents:
3833
diff
changeset
|
333 signing_actor: str |
65e5718e7710
component AP gateway: `Announce` activity implementation:
Goffi <goffi@goffi.org>
parents:
3833
diff
changeset
|
334 ): |
65e5718e7710
component AP gateway: `Announce` activity implementation:
Goffi <goffi@goffi.org>
parents:
3833
diff
changeset
|
335 # we create a new item |
65e5718e7710
component AP gateway: `Announce` activity implementation:
Goffi <goffi@goffi.org>
parents:
3833
diff
changeset
|
336 await self.handleNewAPItems( |
65e5718e7710
component AP gateway: `Announce` activity implementation:
Goffi <goffi@goffi.org>
parents:
3833
diff
changeset
|
337 request, |
65e5718e7710
component AP gateway: `Announce` activity implementation:
Goffi <goffi@goffi.org>
parents:
3833
diff
changeset
|
338 data, |
65e5718e7710
component AP gateway: `Announce` activity implementation:
Goffi <goffi@goffi.org>
parents:
3833
diff
changeset
|
339 account_jid, |
65e5718e7710
component AP gateway: `Announce` activity implementation:
Goffi <goffi@goffi.org>
parents:
3833
diff
changeset
|
340 node, |
65e5718e7710
component AP gateway: `Announce` activity implementation:
Goffi <goffi@goffi.org>
parents:
3833
diff
changeset
|
341 signing_actor, |
65e5718e7710
component AP gateway: `Announce` activity implementation:
Goffi <goffi@goffi.org>
parents:
3833
diff
changeset
|
342 repeated=True |
65e5718e7710
component AP gateway: `Announce` activity implementation:
Goffi <goffi@goffi.org>
parents:
3833
diff
changeset
|
343 ) |
3745
a8c7e5cef0cb
comp AP gateway: signature checking, caching and threads management:
Goffi <goffi@goffi.org>
parents:
3729
diff
changeset
|
344 |
3869
c0bcbcf5b4b7
component AP gateway: handle `Like` and `Undo`/`Like` activities:
Goffi <goffi@goffi.org>
parents:
3847
diff
changeset
|
345 async def handleNewLikeItem( |
c0bcbcf5b4b7
component AP gateway: handle `Like` and `Undo`/`Like` activities:
Goffi <goffi@goffi.org>
parents:
3847
diff
changeset
|
346 self, |
c0bcbcf5b4b7
component AP gateway: handle `Like` and `Undo`/`Like` activities:
Goffi <goffi@goffi.org>
parents:
3847
diff
changeset
|
347 client: SatXMPPEntity, |
c0bcbcf5b4b7
component AP gateway: handle `Like` and `Undo`/`Like` activities:
Goffi <goffi@goffi.org>
parents:
3847
diff
changeset
|
348 data: dict, |
c0bcbcf5b4b7
component AP gateway: handle `Like` and `Undo`/`Like` activities:
Goffi <goffi@goffi.org>
parents:
3847
diff
changeset
|
349 undo: bool = False, |
c0bcbcf5b4b7
component AP gateway: handle `Like` and `Undo`/`Like` activities:
Goffi <goffi@goffi.org>
parents:
3847
diff
changeset
|
350 ) -> None: |
c0bcbcf5b4b7
component AP gateway: handle `Like` and `Undo`/`Like` activities:
Goffi <goffi@goffi.org>
parents:
3847
diff
changeset
|
351 liked_ids = data.get("object") |
c0bcbcf5b4b7
component AP gateway: handle `Like` and `Undo`/`Like` activities:
Goffi <goffi@goffi.org>
parents:
3847
diff
changeset
|
352 if not liked_ids: |
c0bcbcf5b4b7
component AP gateway: handle `Like` and `Undo`/`Like` activities:
Goffi <goffi@goffi.org>
parents:
3847
diff
changeset
|
353 raise exceptions.DataError("object should be set") |
c0bcbcf5b4b7
component AP gateway: handle `Like` and `Undo`/`Like` activities:
Goffi <goffi@goffi.org>
parents:
3847
diff
changeset
|
354 elif isinstance(liked_ids, list): |
c0bcbcf5b4b7
component AP gateway: handle `Like` and `Undo`/`Like` activities:
Goffi <goffi@goffi.org>
parents:
3847
diff
changeset
|
355 try: |
c0bcbcf5b4b7
component AP gateway: handle `Like` and `Undo`/`Like` activities:
Goffi <goffi@goffi.org>
parents:
3847
diff
changeset
|
356 liked_ids = [o["id"] for o in liked_ids] |
c0bcbcf5b4b7
component AP gateway: handle `Like` and `Undo`/`Like` activities:
Goffi <goffi@goffi.org>
parents:
3847
diff
changeset
|
357 except (KeyError, TypeError): |
c0bcbcf5b4b7
component AP gateway: handle `Like` and `Undo`/`Like` activities:
Goffi <goffi@goffi.org>
parents:
3847
diff
changeset
|
358 raise exceptions.DataError(f"invalid object: {liked_ids!r}") |
c0bcbcf5b4b7
component AP gateway: handle `Like` and `Undo`/`Like` activities:
Goffi <goffi@goffi.org>
parents:
3847
diff
changeset
|
359 elif isinstance(liked_ids, dict): |
c0bcbcf5b4b7
component AP gateway: handle `Like` and `Undo`/`Like` activities:
Goffi <goffi@goffi.org>
parents:
3847
diff
changeset
|
360 obj_id = liked_ids.get("id") |
c0bcbcf5b4b7
component AP gateway: handle `Like` and `Undo`/`Like` activities:
Goffi <goffi@goffi.org>
parents:
3847
diff
changeset
|
361 if not obj_id or not isinstance(obj_id, str): |
c0bcbcf5b4b7
component AP gateway: handle `Like` and `Undo`/`Like` activities:
Goffi <goffi@goffi.org>
parents:
3847
diff
changeset
|
362 raise exceptions.DataError(f"invalid object: {liked_ids!r}") |
c0bcbcf5b4b7
component AP gateway: handle `Like` and `Undo`/`Like` activities:
Goffi <goffi@goffi.org>
parents:
3847
diff
changeset
|
363 liked_ids = [obj_id] |
c0bcbcf5b4b7
component AP gateway: handle `Like` and `Undo`/`Like` activities:
Goffi <goffi@goffi.org>
parents:
3847
diff
changeset
|
364 elif isinstance(liked_ids, str): |
c0bcbcf5b4b7
component AP gateway: handle `Like` and `Undo`/`Like` activities:
Goffi <goffi@goffi.org>
parents:
3847
diff
changeset
|
365 liked_ids = [liked_ids] |
c0bcbcf5b4b7
component AP gateway: handle `Like` and `Undo`/`Like` activities:
Goffi <goffi@goffi.org>
parents:
3847
diff
changeset
|
366 |
c0bcbcf5b4b7
component AP gateway: handle `Like` and `Undo`/`Like` activities:
Goffi <goffi@goffi.org>
parents:
3847
diff
changeset
|
367 for liked_id in liked_ids: |
c0bcbcf5b4b7
component AP gateway: handle `Like` and `Undo`/`Like` activities:
Goffi <goffi@goffi.org>
parents:
3847
diff
changeset
|
368 if not self.apg.isLocalURL(liked_id): |
c0bcbcf5b4b7
component AP gateway: handle `Like` and `Undo`/`Like` activities:
Goffi <goffi@goffi.org>
parents:
3847
diff
changeset
|
369 log.debug(f"ignoring non local liked ID: {liked_id}") |
c0bcbcf5b4b7
component AP gateway: handle `Like` and `Undo`/`Like` activities:
Goffi <goffi@goffi.org>
parents:
3847
diff
changeset
|
370 continue |
c0bcbcf5b4b7
component AP gateway: handle `Like` and `Undo`/`Like` activities:
Goffi <goffi@goffi.org>
parents:
3847
diff
changeset
|
371 url_type, url_args = self.apg.parseAPURL(liked_id) |
c0bcbcf5b4b7
component AP gateway: handle `Like` and `Undo`/`Like` activities:
Goffi <goffi@goffi.org>
parents:
3847
diff
changeset
|
372 if url_type != TYPE_ITEM: |
c0bcbcf5b4b7
component AP gateway: handle `Like` and `Undo`/`Like` activities:
Goffi <goffi@goffi.org>
parents:
3847
diff
changeset
|
373 log.warning(f"unexpected local URL for liked item: {liked_id}") |
c0bcbcf5b4b7
component AP gateway: handle `Like` and `Undo`/`Like` activities:
Goffi <goffi@goffi.org>
parents:
3847
diff
changeset
|
374 continue |
c0bcbcf5b4b7
component AP gateway: handle `Like` and `Undo`/`Like` activities:
Goffi <goffi@goffi.org>
parents:
3847
diff
changeset
|
375 try: |
c0bcbcf5b4b7
component AP gateway: handle `Like` and `Undo`/`Like` activities:
Goffi <goffi@goffi.org>
parents:
3847
diff
changeset
|
376 account, item_id = url_args |
c0bcbcf5b4b7
component AP gateway: handle `Like` and `Undo`/`Like` activities:
Goffi <goffi@goffi.org>
parents:
3847
diff
changeset
|
377 except ValueError: |
c0bcbcf5b4b7
component AP gateway: handle `Like` and `Undo`/`Like` activities:
Goffi <goffi@goffi.org>
parents:
3847
diff
changeset
|
378 raise ValueError(f"invalid URL: {liked_id}") |
c0bcbcf5b4b7
component AP gateway: handle `Like` and `Undo`/`Like` activities:
Goffi <goffi@goffi.org>
parents:
3847
diff
changeset
|
379 author_jid, item_node = await self.apg.getJIDAndNode(account) |
c0bcbcf5b4b7
component AP gateway: handle `Like` and `Undo`/`Like` activities:
Goffi <goffi@goffi.org>
parents:
3847
diff
changeset
|
380 if item_node is None: |
c0bcbcf5b4b7
component AP gateway: handle `Like` and `Undo`/`Like` activities:
Goffi <goffi@goffi.org>
parents:
3847
diff
changeset
|
381 item_node = self.apg._m.namespace |
c0bcbcf5b4b7
component AP gateway: handle `Like` and `Undo`/`Like` activities:
Goffi <goffi@goffi.org>
parents:
3847
diff
changeset
|
382 attachment_node = self.apg._pa.getAttachmentNodeName( |
c0bcbcf5b4b7
component AP gateway: handle `Like` and `Undo`/`Like` activities:
Goffi <goffi@goffi.org>
parents:
3847
diff
changeset
|
383 author_jid, item_node, item_id |
c0bcbcf5b4b7
component AP gateway: handle `Like` and `Undo`/`Like` activities:
Goffi <goffi@goffi.org>
parents:
3847
diff
changeset
|
384 ) |
c0bcbcf5b4b7
component AP gateway: handle `Like` and `Undo`/`Like` activities:
Goffi <goffi@goffi.org>
parents:
3847
diff
changeset
|
385 cached_node = await self.apg.host.memory.storage.getPubsubNode( |
c0bcbcf5b4b7
component AP gateway: handle `Like` and `Undo`/`Like` activities:
Goffi <goffi@goffi.org>
parents:
3847
diff
changeset
|
386 client, |
c0bcbcf5b4b7
component AP gateway: handle `Like` and `Undo`/`Like` activities:
Goffi <goffi@goffi.org>
parents:
3847
diff
changeset
|
387 author_jid, |
c0bcbcf5b4b7
component AP gateway: handle `Like` and `Undo`/`Like` activities:
Goffi <goffi@goffi.org>
parents:
3847
diff
changeset
|
388 attachment_node, |
c0bcbcf5b4b7
component AP gateway: handle `Like` and `Undo`/`Like` activities:
Goffi <goffi@goffi.org>
parents:
3847
diff
changeset
|
389 with_subscriptions=True, |
c0bcbcf5b4b7
component AP gateway: handle `Like` and `Undo`/`Like` activities:
Goffi <goffi@goffi.org>
parents:
3847
diff
changeset
|
390 create=True |
c0bcbcf5b4b7
component AP gateway: handle `Like` and `Undo`/`Like` activities:
Goffi <goffi@goffi.org>
parents:
3847
diff
changeset
|
391 ) |
c0bcbcf5b4b7
component AP gateway: handle `Like` and `Undo`/`Like` activities:
Goffi <goffi@goffi.org>
parents:
3847
diff
changeset
|
392 found_items, __ = await self.apg.host.memory.storage.getItems( |
c0bcbcf5b4b7
component AP gateway: handle `Like` and `Undo`/`Like` activities:
Goffi <goffi@goffi.org>
parents:
3847
diff
changeset
|
393 cached_node, item_ids=[item_id] |
c0bcbcf5b4b7
component AP gateway: handle `Like` and `Undo`/`Like` activities:
Goffi <goffi@goffi.org>
parents:
3847
diff
changeset
|
394 ) |
c0bcbcf5b4b7
component AP gateway: handle `Like` and `Undo`/`Like` activities:
Goffi <goffi@goffi.org>
parents:
3847
diff
changeset
|
395 if not found_items: |
c0bcbcf5b4b7
component AP gateway: handle `Like` and `Undo`/`Like` activities:
Goffi <goffi@goffi.org>
parents:
3847
diff
changeset
|
396 old_item_elt = None |
c0bcbcf5b4b7
component AP gateway: handle `Like` and `Undo`/`Like` activities:
Goffi <goffi@goffi.org>
parents:
3847
diff
changeset
|
397 else: |
c0bcbcf5b4b7
component AP gateway: handle `Like` and `Undo`/`Like` activities:
Goffi <goffi@goffi.org>
parents:
3847
diff
changeset
|
398 found_item = found_items[0] |
c0bcbcf5b4b7
component AP gateway: handle `Like` and `Undo`/`Like` activities:
Goffi <goffi@goffi.org>
parents:
3847
diff
changeset
|
399 old_item_elt = found_item.data |
c0bcbcf5b4b7
component AP gateway: handle `Like` and `Undo`/`Like` activities:
Goffi <goffi@goffi.org>
parents:
3847
diff
changeset
|
400 |
c0bcbcf5b4b7
component AP gateway: handle `Like` and `Undo`/`Like` activities:
Goffi <goffi@goffi.org>
parents:
3847
diff
changeset
|
401 item_elt = self.apg._pa.applySetHandler( |
c0bcbcf5b4b7
component AP gateway: handle `Like` and `Undo`/`Like` activities:
Goffi <goffi@goffi.org>
parents:
3847
diff
changeset
|
402 client, |
c0bcbcf5b4b7
component AP gateway: handle `Like` and `Undo`/`Like` activities:
Goffi <goffi@goffi.org>
parents:
3847
diff
changeset
|
403 {"extra": {"noticed": not undo}}, |
c0bcbcf5b4b7
component AP gateway: handle `Like` and `Undo`/`Like` activities:
Goffi <goffi@goffi.org>
parents:
3847
diff
changeset
|
404 old_item_elt, |
c0bcbcf5b4b7
component AP gateway: handle `Like` and `Undo`/`Like` activities:
Goffi <goffi@goffi.org>
parents:
3847
diff
changeset
|
405 [("noticed", self.apg._pa.namespace)] |
c0bcbcf5b4b7
component AP gateway: handle `Like` and `Undo`/`Like` activities:
Goffi <goffi@goffi.org>
parents:
3847
diff
changeset
|
406 ) |
c0bcbcf5b4b7
component AP gateway: handle `Like` and `Undo`/`Like` activities:
Goffi <goffi@goffi.org>
parents:
3847
diff
changeset
|
407 # we reparse the element, as there can be other attachments |
c0bcbcf5b4b7
component AP gateway: handle `Like` and `Undo`/`Like` activities:
Goffi <goffi@goffi.org>
parents:
3847
diff
changeset
|
408 attachments_data = self.apg._pa.items2attachmentData(client, [item_elt]) |
c0bcbcf5b4b7
component AP gateway: handle `Like` and `Undo`/`Like` activities:
Goffi <goffi@goffi.org>
parents:
3847
diff
changeset
|
409 # and we update the cache |
c0bcbcf5b4b7
component AP gateway: handle `Like` and `Undo`/`Like` activities:
Goffi <goffi@goffi.org>
parents:
3847
diff
changeset
|
410 await self.apg.host.memory.storage.cachePubsubItems( |
c0bcbcf5b4b7
component AP gateway: handle `Like` and `Undo`/`Like` activities:
Goffi <goffi@goffi.org>
parents:
3847
diff
changeset
|
411 client, |
c0bcbcf5b4b7
component AP gateway: handle `Like` and `Undo`/`Like` activities:
Goffi <goffi@goffi.org>
parents:
3847
diff
changeset
|
412 cached_node, |
c0bcbcf5b4b7
component AP gateway: handle `Like` and `Undo`/`Like` activities:
Goffi <goffi@goffi.org>
parents:
3847
diff
changeset
|
413 [item_elt], |
c0bcbcf5b4b7
component AP gateway: handle `Like` and `Undo`/`Like` activities:
Goffi <goffi@goffi.org>
parents:
3847
diff
changeset
|
414 attachments_data or [{}] |
c0bcbcf5b4b7
component AP gateway: handle `Like` and `Undo`/`Like` activities:
Goffi <goffi@goffi.org>
parents:
3847
diff
changeset
|
415 ) |
c0bcbcf5b4b7
component AP gateway: handle `Like` and `Undo`/`Like` activities:
Goffi <goffi@goffi.org>
parents:
3847
diff
changeset
|
416 |
c0bcbcf5b4b7
component AP gateway: handle `Like` and `Undo`/`Like` activities:
Goffi <goffi@goffi.org>
parents:
3847
diff
changeset
|
417 if self.apg.isVirtualJID(author_jid): |
c0bcbcf5b4b7
component AP gateway: handle `Like` and `Undo`/`Like` activities:
Goffi <goffi@goffi.org>
parents:
3847
diff
changeset
|
418 # the attachment is on t a virtual pubsub service (linking to an AP item), |
c0bcbcf5b4b7
component AP gateway: handle `Like` and `Undo`/`Like` activities:
Goffi <goffi@goffi.org>
parents:
3847
diff
changeset
|
419 # we notify all subscribers |
c0bcbcf5b4b7
component AP gateway: handle `Like` and `Undo`/`Like` activities:
Goffi <goffi@goffi.org>
parents:
3847
diff
changeset
|
420 for subscription in cached_node.subscriptions: |
c0bcbcf5b4b7
component AP gateway: handle `Like` and `Undo`/`Like` activities:
Goffi <goffi@goffi.org>
parents:
3847
diff
changeset
|
421 if subscription.state != SubscriptionState.SUBSCRIBED: |
c0bcbcf5b4b7
component AP gateway: handle `Like` and `Undo`/`Like` activities:
Goffi <goffi@goffi.org>
parents:
3847
diff
changeset
|
422 continue |
c0bcbcf5b4b7
component AP gateway: handle `Like` and `Undo`/`Like` activities:
Goffi <goffi@goffi.org>
parents:
3847
diff
changeset
|
423 self.apg.pubsub_service.notifyPublish( |
c0bcbcf5b4b7
component AP gateway: handle `Like` and `Undo`/`Like` activities:
Goffi <goffi@goffi.org>
parents:
3847
diff
changeset
|
424 author_jid, |
c0bcbcf5b4b7
component AP gateway: handle `Like` and `Undo`/`Like` activities:
Goffi <goffi@goffi.org>
parents:
3847
diff
changeset
|
425 attachment_node, |
c0bcbcf5b4b7
component AP gateway: handle `Like` and `Undo`/`Like` activities:
Goffi <goffi@goffi.org>
parents:
3847
diff
changeset
|
426 [(subscription.subscriber, None, [item_elt])] |
c0bcbcf5b4b7
component AP gateway: handle `Like` and `Undo`/`Like` activities:
Goffi <goffi@goffi.org>
parents:
3847
diff
changeset
|
427 ) |
c0bcbcf5b4b7
component AP gateway: handle `Like` and `Undo`/`Like` activities:
Goffi <goffi@goffi.org>
parents:
3847
diff
changeset
|
428 else: |
c0bcbcf5b4b7
component AP gateway: handle `Like` and `Undo`/`Like` activities:
Goffi <goffi@goffi.org>
parents:
3847
diff
changeset
|
429 # the attachment is on an XMPP item, we publish it to the attachment node |
c0bcbcf5b4b7
component AP gateway: handle `Like` and `Undo`/`Like` activities:
Goffi <goffi@goffi.org>
parents:
3847
diff
changeset
|
430 await self.apg._p.sendItems( |
c0bcbcf5b4b7
component AP gateway: handle `Like` and `Undo`/`Like` activities:
Goffi <goffi@goffi.org>
parents:
3847
diff
changeset
|
431 client, author_jid, attachment_node, [item_elt] |
c0bcbcf5b4b7
component AP gateway: handle `Like` and `Undo`/`Like` activities:
Goffi <goffi@goffi.org>
parents:
3847
diff
changeset
|
432 ) |
c0bcbcf5b4b7
component AP gateway: handle `Like` and `Undo`/`Like` activities:
Goffi <goffi@goffi.org>
parents:
3847
diff
changeset
|
433 |
c0bcbcf5b4b7
component AP gateway: handle `Like` and `Undo`/`Like` activities:
Goffi <goffi@goffi.org>
parents:
3847
diff
changeset
|
434 async def handleLikeActivity( |
c0bcbcf5b4b7
component AP gateway: handle `Like` and `Undo`/`Like` activities:
Goffi <goffi@goffi.org>
parents:
3847
diff
changeset
|
435 self, |
c0bcbcf5b4b7
component AP gateway: handle `Like` and `Undo`/`Like` activities:
Goffi <goffi@goffi.org>
parents:
3847
diff
changeset
|
436 request: "HTTPRequest", |
c0bcbcf5b4b7
component AP gateway: handle `Like` and `Undo`/`Like` activities:
Goffi <goffi@goffi.org>
parents:
3847
diff
changeset
|
437 data: dict, |
c0bcbcf5b4b7
component AP gateway: handle `Like` and `Undo`/`Like` activities:
Goffi <goffi@goffi.org>
parents:
3847
diff
changeset
|
438 account_jid: Optional[jid.JID], |
c0bcbcf5b4b7
component AP gateway: handle `Like` and `Undo`/`Like` activities:
Goffi <goffi@goffi.org>
parents:
3847
diff
changeset
|
439 node: Optional[str], |
c0bcbcf5b4b7
component AP gateway: handle `Like` and `Undo`/`Like` activities:
Goffi <goffi@goffi.org>
parents:
3847
diff
changeset
|
440 ap_account: Optional[str], |
c0bcbcf5b4b7
component AP gateway: handle `Like` and `Undo`/`Like` activities:
Goffi <goffi@goffi.org>
parents:
3847
diff
changeset
|
441 ap_url: str, |
c0bcbcf5b4b7
component AP gateway: handle `Like` and `Undo`/`Like` activities:
Goffi <goffi@goffi.org>
parents:
3847
diff
changeset
|
442 signing_actor: str |
c0bcbcf5b4b7
component AP gateway: handle `Like` and `Undo`/`Like` activities:
Goffi <goffi@goffi.org>
parents:
3847
diff
changeset
|
443 ): |
c0bcbcf5b4b7
component AP gateway: handle `Like` and `Undo`/`Like` activities:
Goffi <goffi@goffi.org>
parents:
3847
diff
changeset
|
444 client = await self.apg.getVirtualClient(signing_actor) |
c0bcbcf5b4b7
component AP gateway: handle `Like` and `Undo`/`Like` activities:
Goffi <goffi@goffi.org>
parents:
3847
diff
changeset
|
445 await self.handleNewLikeItem(client, data) |
c0bcbcf5b4b7
component AP gateway: handle `Like` and `Undo`/`Like` activities:
Goffi <goffi@goffi.org>
parents:
3847
diff
changeset
|
446 |
3728
b15644cae50d
component AP gateway: JID/node ⟺ AP outbox conversion:
Goffi <goffi@goffi.org>
parents:
3684
diff
changeset
|
447 async def APActorRequest( |
b15644cae50d
component AP gateway: JID/node ⟺ AP outbox conversion:
Goffi <goffi@goffi.org>
parents:
3684
diff
changeset
|
448 self, |
b15644cae50d
component AP gateway: JID/node ⟺ AP outbox conversion:
Goffi <goffi@goffi.org>
parents:
3684
diff
changeset
|
449 request: "HTTPRequest", |
b15644cae50d
component AP gateway: JID/node ⟺ AP outbox conversion:
Goffi <goffi@goffi.org>
parents:
3684
diff
changeset
|
450 account_jid: jid.JID, |
b15644cae50d
component AP gateway: JID/node ⟺ AP outbox conversion:
Goffi <goffi@goffi.org>
parents:
3684
diff
changeset
|
451 node: Optional[str], |
b15644cae50d
component AP gateway: JID/node ⟺ AP outbox conversion:
Goffi <goffi@goffi.org>
parents:
3684
diff
changeset
|
452 ap_account: str, |
3826
81c79b7cafa7
tests (unit/ap-gateway): tests for XMPP identity/vCard4 <=> AP actor data conversion:
Goffi <goffi@goffi.org>
parents:
3821
diff
changeset
|
453 ap_url: str, |
3745
a8c7e5cef0cb
comp AP gateway: signature checking, caching and threads management:
Goffi <goffi@goffi.org>
parents:
3729
diff
changeset
|
454 signing_actor: Optional[str] |
3728
b15644cae50d
component AP gateway: JID/node ⟺ AP outbox conversion:
Goffi <goffi@goffi.org>
parents:
3684
diff
changeset
|
455 ) -> dict: |
3764
125c7043b277
comp AP gateway: publish, (un)subscribe/(un)follow, public subscription/following/followers:
Goffi <goffi@goffi.org>
parents:
3745
diff
changeset
|
456 inbox = self.apg.buildAPURL(TYPE_INBOX, ap_account) |
3745
a8c7e5cef0cb
comp AP gateway: signature checking, caching and threads management:
Goffi <goffi@goffi.org>
parents:
3729
diff
changeset
|
457 shared_inbox = self.apg.buildAPURL(TYPE_SHARED_INBOX) |
3764
125c7043b277
comp AP gateway: publish, (un)subscribe/(un)follow, public subscription/following/followers:
Goffi <goffi@goffi.org>
parents:
3745
diff
changeset
|
458 outbox = self.apg.buildAPURL(TYPE_OUTBOX, ap_account) |
125c7043b277
comp AP gateway: publish, (un)subscribe/(un)follow, public subscription/following/followers:
Goffi <goffi@goffi.org>
parents:
3745
diff
changeset
|
459 followers = self.apg.buildAPURL(TYPE_FOLLOWERS, ap_account) |
125c7043b277
comp AP gateway: publish, (un)subscribe/(un)follow, public subscription/following/followers:
Goffi <goffi@goffi.org>
parents:
3745
diff
changeset
|
460 following = self.apg.buildAPURL(TYPE_FOLLOWING, ap_account) |
3728
b15644cae50d
component AP gateway: JID/node ⟺ AP outbox conversion:
Goffi <goffi@goffi.org>
parents:
3684
diff
changeset
|
461 |
b15644cae50d
component AP gateway: JID/node ⟺ AP outbox conversion:
Goffi <goffi@goffi.org>
parents:
3684
diff
changeset
|
462 # we have to use AP account as preferredUsername because it is used to retrieve |
b15644cae50d
component AP gateway: JID/node ⟺ AP outbox conversion:
Goffi <goffi@goffi.org>
parents:
3684
diff
changeset
|
463 # actor handle (see https://socialhub.activitypub.rocks/t/how-to-retrieve-user-server-tld-handle-from-actors-url/2196) |
b15644cae50d
component AP gateway: JID/node ⟺ AP outbox conversion:
Goffi <goffi@goffi.org>
parents:
3684
diff
changeset
|
464 preferred_username = ap_account.split("@", 1)[0] |
3821
0b1c30ff2cbb
component AP: XMPP identity => AP actor data converstion:
Goffi <goffi@goffi.org>
parents:
3807
diff
changeset
|
465 |
0b1c30ff2cbb
component AP: XMPP identity => AP actor data converstion:
Goffi <goffi@goffi.org>
parents:
3807
diff
changeset
|
466 identity_data = await self.apg._i.getIdentity(self.apg.client, account_jid) |
0b1c30ff2cbb
component AP: XMPP identity => AP actor data converstion:
Goffi <goffi@goffi.org>
parents:
3807
diff
changeset
|
467 |
0b1c30ff2cbb
component AP: XMPP identity => AP actor data converstion:
Goffi <goffi@goffi.org>
parents:
3807
diff
changeset
|
468 actor_data = { |
3682
7c990aaa49d3
comp AP Gateway: ActivityPub Component first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
469 "@context": [ |
7c990aaa49d3
comp AP Gateway: ActivityPub Component first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
470 "https://www.w3.org/ns/activitystreams", |
7c990aaa49d3
comp AP Gateway: ActivityPub Component first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
471 "https://w3id.org/security/v1" |
7c990aaa49d3
comp AP Gateway: ActivityPub Component first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
472 ], |
7c990aaa49d3
comp AP Gateway: ActivityPub Component first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
473 |
3826
81c79b7cafa7
tests (unit/ap-gateway): tests for XMPP identity/vCard4 <=> AP actor data conversion:
Goffi <goffi@goffi.org>
parents:
3821
diff
changeset
|
474 "id": ap_url, |
3682
7c990aaa49d3
comp AP Gateway: ActivityPub Component first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
475 "type": "Person", |
3728
b15644cae50d
component AP gateway: JID/node ⟺ AP outbox conversion:
Goffi <goffi@goffi.org>
parents:
3684
diff
changeset
|
476 "preferredUsername": preferred_username, |
3764
125c7043b277
comp AP gateway: publish, (un)subscribe/(un)follow, public subscription/following/followers:
Goffi <goffi@goffi.org>
parents:
3745
diff
changeset
|
477 "inbox": inbox, |
125c7043b277
comp AP gateway: publish, (un)subscribe/(un)follow, public subscription/following/followers:
Goffi <goffi@goffi.org>
parents:
3745
diff
changeset
|
478 "outbox": outbox, |
125c7043b277
comp AP gateway: publish, (un)subscribe/(un)follow, public subscription/following/followers:
Goffi <goffi@goffi.org>
parents:
3745
diff
changeset
|
479 "followers": followers, |
125c7043b277
comp AP gateway: publish, (un)subscribe/(un)follow, public subscription/following/followers:
Goffi <goffi@goffi.org>
parents:
3745
diff
changeset
|
480 "following": following, |
3682
7c990aaa49d3
comp AP Gateway: ActivityPub Component first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
481 "publicKey": { |
3826
81c79b7cafa7
tests (unit/ap-gateway): tests for XMPP identity/vCard4 <=> AP actor data conversion:
Goffi <goffi@goffi.org>
parents:
3821
diff
changeset
|
482 "id": f"{ap_url}#main-key", |
81c79b7cafa7
tests (unit/ap-gateway): tests for XMPP identity/vCard4 <=> AP actor data conversion:
Goffi <goffi@goffi.org>
parents:
3821
diff
changeset
|
483 "owner": ap_url, |
3682
7c990aaa49d3
comp AP Gateway: ActivityPub Component first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
484 "publicKeyPem": self.apg.public_key_pem |
3745
a8c7e5cef0cb
comp AP gateway: signature checking, caching and threads management:
Goffi <goffi@goffi.org>
parents:
3729
diff
changeset
|
485 }, |
a8c7e5cef0cb
comp AP gateway: signature checking, caching and threads management:
Goffi <goffi@goffi.org>
parents:
3729
diff
changeset
|
486 "endpoints": { |
a8c7e5cef0cb
comp AP gateway: signature checking, caching and threads management:
Goffi <goffi@goffi.org>
parents:
3729
diff
changeset
|
487 "sharedInbox": shared_inbox |
a8c7e5cef0cb
comp AP gateway: signature checking, caching and threads management:
Goffi <goffi@goffi.org>
parents:
3729
diff
changeset
|
488 }, |
3682
7c990aaa49d3
comp AP Gateway: ActivityPub Component first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
489 } |
3728
b15644cae50d
component AP gateway: JID/node ⟺ AP outbox conversion:
Goffi <goffi@goffi.org>
parents:
3684
diff
changeset
|
490 |
3821
0b1c30ff2cbb
component AP: XMPP identity => AP actor data converstion:
Goffi <goffi@goffi.org>
parents:
3807
diff
changeset
|
491 if identity_data.get("nicknames"): |
0b1c30ff2cbb
component AP: XMPP identity => AP actor data converstion:
Goffi <goffi@goffi.org>
parents:
3807
diff
changeset
|
492 actor_data["name"] = identity_data["nicknames"][0] |
0b1c30ff2cbb
component AP: XMPP identity => AP actor data converstion:
Goffi <goffi@goffi.org>
parents:
3807
diff
changeset
|
493 if identity_data.get("description"): |
0b1c30ff2cbb
component AP: XMPP identity => AP actor data converstion:
Goffi <goffi@goffi.org>
parents:
3807
diff
changeset
|
494 # description is plain text while summary expects HTML |
0b1c30ff2cbb
component AP: XMPP identity => AP actor data converstion:
Goffi <goffi@goffi.org>
parents:
3807
diff
changeset
|
495 actor_data["summary"] = html.escape(identity_data["description"]) |
0b1c30ff2cbb
component AP: XMPP identity => AP actor data converstion:
Goffi <goffi@goffi.org>
parents:
3807
diff
changeset
|
496 if identity_data.get("avatar"): |
0b1c30ff2cbb
component AP: XMPP identity => AP actor data converstion:
Goffi <goffi@goffi.org>
parents:
3807
diff
changeset
|
497 avatar_data = identity_data["avatar"] |
0b1c30ff2cbb
component AP: XMPP identity => AP actor data converstion:
Goffi <goffi@goffi.org>
parents:
3807
diff
changeset
|
498 try: |
0b1c30ff2cbb
component AP: XMPP identity => AP actor data converstion:
Goffi <goffi@goffi.org>
parents:
3807
diff
changeset
|
499 filename = avatar_data["filename"] |
0b1c30ff2cbb
component AP: XMPP identity => AP actor data converstion:
Goffi <goffi@goffi.org>
parents:
3807
diff
changeset
|
500 media_type = avatar_data["media_type"] |
0b1c30ff2cbb
component AP: XMPP identity => AP actor data converstion:
Goffi <goffi@goffi.org>
parents:
3807
diff
changeset
|
501 except KeyError: |
0b1c30ff2cbb
component AP: XMPP identity => AP actor data converstion:
Goffi <goffi@goffi.org>
parents:
3807
diff
changeset
|
502 log.error(f"incomplete avatar data: {identity_data!r}") |
0b1c30ff2cbb
component AP: XMPP identity => AP actor data converstion:
Goffi <goffi@goffi.org>
parents:
3807
diff
changeset
|
503 else: |
0b1c30ff2cbb
component AP: XMPP identity => AP actor data converstion:
Goffi <goffi@goffi.org>
parents:
3807
diff
changeset
|
504 avatar_url = self.apg.buildAPURL("avatar", filename) |
0b1c30ff2cbb
component AP: XMPP identity => AP actor data converstion:
Goffi <goffi@goffi.org>
parents:
3807
diff
changeset
|
505 actor_data["icon"] = { |
0b1c30ff2cbb
component AP: XMPP identity => AP actor data converstion:
Goffi <goffi@goffi.org>
parents:
3807
diff
changeset
|
506 "type": "Image", |
0b1c30ff2cbb
component AP: XMPP identity => AP actor data converstion:
Goffi <goffi@goffi.org>
parents:
3807
diff
changeset
|
507 "url": avatar_url, |
0b1c30ff2cbb
component AP: XMPP identity => AP actor data converstion:
Goffi <goffi@goffi.org>
parents:
3807
diff
changeset
|
508 "mediaType": media_type |
0b1c30ff2cbb
component AP: XMPP identity => AP actor data converstion:
Goffi <goffi@goffi.org>
parents:
3807
diff
changeset
|
509 } |
0b1c30ff2cbb
component AP: XMPP identity => AP actor data converstion:
Goffi <goffi@goffi.org>
parents:
3807
diff
changeset
|
510 |
0b1c30ff2cbb
component AP: XMPP identity => AP actor data converstion:
Goffi <goffi@goffi.org>
parents:
3807
diff
changeset
|
511 return actor_data |
0b1c30ff2cbb
component AP: XMPP identity => AP actor data converstion:
Goffi <goffi@goffi.org>
parents:
3807
diff
changeset
|
512 |
3728
b15644cae50d
component AP gateway: JID/node ⟺ AP outbox conversion:
Goffi <goffi@goffi.org>
parents:
3684
diff
changeset
|
513 def getCanonicalURL(self, request: "HTTPRequest") -> str: |
b15644cae50d
component AP gateway: JID/node ⟺ AP outbox conversion:
Goffi <goffi@goffi.org>
parents:
3684
diff
changeset
|
514 return parse.urljoin( |
b15644cae50d
component AP gateway: JID/node ⟺ AP outbox conversion:
Goffi <goffi@goffi.org>
parents:
3684
diff
changeset
|
515 f"https://{self.apg.public_url}", |
b15644cae50d
component AP gateway: JID/node ⟺ AP outbox conversion:
Goffi <goffi@goffi.org>
parents:
3684
diff
changeset
|
516 request.path.decode().rstrip("/") |
b15644cae50d
component AP gateway: JID/node ⟺ AP outbox conversion:
Goffi <goffi@goffi.org>
parents:
3684
diff
changeset
|
517 ) |
b15644cae50d
component AP gateway: JID/node ⟺ AP outbox conversion:
Goffi <goffi@goffi.org>
parents:
3684
diff
changeset
|
518 |
b15644cae50d
component AP gateway: JID/node ⟺ AP outbox conversion:
Goffi <goffi@goffi.org>
parents:
3684
diff
changeset
|
519 def queryData2RSMRequest( |
b15644cae50d
component AP gateway: JID/node ⟺ AP outbox conversion:
Goffi <goffi@goffi.org>
parents:
3684
diff
changeset
|
520 self, |
b15644cae50d
component AP gateway: JID/node ⟺ AP outbox conversion:
Goffi <goffi@goffi.org>
parents:
3684
diff
changeset
|
521 query_data: Dict[str, List[str]] |
b15644cae50d
component AP gateway: JID/node ⟺ AP outbox conversion:
Goffi <goffi@goffi.org>
parents:
3684
diff
changeset
|
522 ) -> rsm.RSMRequest: |
b15644cae50d
component AP gateway: JID/node ⟺ AP outbox conversion:
Goffi <goffi@goffi.org>
parents:
3684
diff
changeset
|
523 """Get RSM kwargs to use with RSMRequest from query data""" |
b15644cae50d
component AP gateway: JID/node ⟺ AP outbox conversion:
Goffi <goffi@goffi.org>
parents:
3684
diff
changeset
|
524 page = query_data.get("page") |
b15644cae50d
component AP gateway: JID/node ⟺ AP outbox conversion:
Goffi <goffi@goffi.org>
parents:
3684
diff
changeset
|
525 |
b15644cae50d
component AP gateway: JID/node ⟺ AP outbox conversion:
Goffi <goffi@goffi.org>
parents:
3684
diff
changeset
|
526 if page == ["first"]: |
b15644cae50d
component AP gateway: JID/node ⟺ AP outbox conversion:
Goffi <goffi@goffi.org>
parents:
3684
diff
changeset
|
527 return rsm.RSMRequest(max_=PAGE_SIZE, before="") |
b15644cae50d
component AP gateway: JID/node ⟺ AP outbox conversion:
Goffi <goffi@goffi.org>
parents:
3684
diff
changeset
|
528 elif page == ["last"]: |
b15644cae50d
component AP gateway: JID/node ⟺ AP outbox conversion:
Goffi <goffi@goffi.org>
parents:
3684
diff
changeset
|
529 return rsm.RSMRequest(max_=PAGE_SIZE) |
b15644cae50d
component AP gateway: JID/node ⟺ AP outbox conversion:
Goffi <goffi@goffi.org>
parents:
3684
diff
changeset
|
530 else: |
b15644cae50d
component AP gateway: JID/node ⟺ AP outbox conversion:
Goffi <goffi@goffi.org>
parents:
3684
diff
changeset
|
531 for query_key in ("index", "before", "after"): |
b15644cae50d
component AP gateway: JID/node ⟺ AP outbox conversion:
Goffi <goffi@goffi.org>
parents:
3684
diff
changeset
|
532 try: |
b15644cae50d
component AP gateway: JID/node ⟺ AP outbox conversion:
Goffi <goffi@goffi.org>
parents:
3684
diff
changeset
|
533 kwargs={query_key: query_data[query_key][0], "max_": PAGE_SIZE} |
b15644cae50d
component AP gateway: JID/node ⟺ AP outbox conversion:
Goffi <goffi@goffi.org>
parents:
3684
diff
changeset
|
534 except (KeyError, IndexError, ValueError): |
b15644cae50d
component AP gateway: JID/node ⟺ AP outbox conversion:
Goffi <goffi@goffi.org>
parents:
3684
diff
changeset
|
535 pass |
b15644cae50d
component AP gateway: JID/node ⟺ AP outbox conversion:
Goffi <goffi@goffi.org>
parents:
3684
diff
changeset
|
536 else: |
b15644cae50d
component AP gateway: JID/node ⟺ AP outbox conversion:
Goffi <goffi@goffi.org>
parents:
3684
diff
changeset
|
537 return rsm.RSMRequest(**kwargs) |
b15644cae50d
component AP gateway: JID/node ⟺ AP outbox conversion:
Goffi <goffi@goffi.org>
parents:
3684
diff
changeset
|
538 raise ValueError(f"Invalid query data: {query_data!r}") |
b15644cae50d
component AP gateway: JID/node ⟺ AP outbox conversion:
Goffi <goffi@goffi.org>
parents:
3684
diff
changeset
|
539 |
b15644cae50d
component AP gateway: JID/node ⟺ AP outbox conversion:
Goffi <goffi@goffi.org>
parents:
3684
diff
changeset
|
540 async def APOutboxPageRequest( |
b15644cae50d
component AP gateway: JID/node ⟺ AP outbox conversion:
Goffi <goffi@goffi.org>
parents:
3684
diff
changeset
|
541 self, |
b15644cae50d
component AP gateway: JID/node ⟺ AP outbox conversion:
Goffi <goffi@goffi.org>
parents:
3684
diff
changeset
|
542 request: "HTTPRequest", |
b15644cae50d
component AP gateway: JID/node ⟺ AP outbox conversion:
Goffi <goffi@goffi.org>
parents:
3684
diff
changeset
|
543 account_jid: jid.JID, |
b15644cae50d
component AP gateway: JID/node ⟺ AP outbox conversion:
Goffi <goffi@goffi.org>
parents:
3684
diff
changeset
|
544 node: Optional[str], |
b15644cae50d
component AP gateway: JID/node ⟺ AP outbox conversion:
Goffi <goffi@goffi.org>
parents:
3684
diff
changeset
|
545 ap_account: str, |
3729
86eea17cafa7
component AP gateway: split plugin in several files:
Goffi <goffi@goffi.org>
parents:
3728
diff
changeset
|
546 ap_url: str, |
3728
b15644cae50d
component AP gateway: JID/node ⟺ AP outbox conversion:
Goffi <goffi@goffi.org>
parents:
3684
diff
changeset
|
547 query_data: Dict[str, List[str]] |
b15644cae50d
component AP gateway: JID/node ⟺ AP outbox conversion:
Goffi <goffi@goffi.org>
parents:
3684
diff
changeset
|
548 ) -> dict: |
3764
125c7043b277
comp AP gateway: publish, (un)subscribe/(un)follow, public subscription/following/followers:
Goffi <goffi@goffi.org>
parents:
3745
diff
changeset
|
549 if node is None: |
125c7043b277
comp AP gateway: publish, (un)subscribe/(un)follow, public subscription/following/followers:
Goffi <goffi@goffi.org>
parents:
3745
diff
changeset
|
550 node = self.apg._m.namespace |
3728
b15644cae50d
component AP gateway: JID/node ⟺ AP outbox conversion:
Goffi <goffi@goffi.org>
parents:
3684
diff
changeset
|
551 # we only keep useful keys, and sort to have consistent URL which can |
b15644cae50d
component AP gateway: JID/node ⟺ AP outbox conversion:
Goffi <goffi@goffi.org>
parents:
3684
diff
changeset
|
552 # be used as ID |
b15644cae50d
component AP gateway: JID/node ⟺ AP outbox conversion:
Goffi <goffi@goffi.org>
parents:
3684
diff
changeset
|
553 url_keys = sorted(set(query_data) & {"page", "index", "before", "after"}) |
b15644cae50d
component AP gateway: JID/node ⟺ AP outbox conversion:
Goffi <goffi@goffi.org>
parents:
3684
diff
changeset
|
554 query_data = {k: query_data[k] for k in url_keys} |
b15644cae50d
component AP gateway: JID/node ⟺ AP outbox conversion:
Goffi <goffi@goffi.org>
parents:
3684
diff
changeset
|
555 try: |
b15644cae50d
component AP gateway: JID/node ⟺ AP outbox conversion:
Goffi <goffi@goffi.org>
parents:
3684
diff
changeset
|
556 items, metadata = await self.apg._p.getItems( |
b15644cae50d
component AP gateway: JID/node ⟺ AP outbox conversion:
Goffi <goffi@goffi.org>
parents:
3684
diff
changeset
|
557 client=self.apg.client, |
b15644cae50d
component AP gateway: JID/node ⟺ AP outbox conversion:
Goffi <goffi@goffi.org>
parents:
3684
diff
changeset
|
558 service=account_jid, |
b15644cae50d
component AP gateway: JID/node ⟺ AP outbox conversion:
Goffi <goffi@goffi.org>
parents:
3684
diff
changeset
|
559 node=node, |
b15644cae50d
component AP gateway: JID/node ⟺ AP outbox conversion:
Goffi <goffi@goffi.org>
parents:
3684
diff
changeset
|
560 rsm_request=self.queryData2RSMRequest(query_data), |
b15644cae50d
component AP gateway: JID/node ⟺ AP outbox conversion:
Goffi <goffi@goffi.org>
parents:
3684
diff
changeset
|
561 extra = {C.KEY_USE_CACHE: False} |
b15644cae50d
component AP gateway: JID/node ⟺ AP outbox conversion:
Goffi <goffi@goffi.org>
parents:
3684
diff
changeset
|
562 ) |
b15644cae50d
component AP gateway: JID/node ⟺ AP outbox conversion:
Goffi <goffi@goffi.org>
parents:
3684
diff
changeset
|
563 except error.StanzaError as e: |
b15644cae50d
component AP gateway: JID/node ⟺ AP outbox conversion:
Goffi <goffi@goffi.org>
parents:
3684
diff
changeset
|
564 log.warning(f"Can't get data from pubsub node {node} at {account_jid}: {e}") |
b15644cae50d
component AP gateway: JID/node ⟺ AP outbox conversion:
Goffi <goffi@goffi.org>
parents:
3684
diff
changeset
|
565 return {} |
b15644cae50d
component AP gateway: JID/node ⟺ AP outbox conversion:
Goffi <goffi@goffi.org>
parents:
3684
diff
changeset
|
566 |
b15644cae50d
component AP gateway: JID/node ⟺ AP outbox conversion:
Goffi <goffi@goffi.org>
parents:
3684
diff
changeset
|
567 base_url = self.getCanonicalURL(request) |
b15644cae50d
component AP gateway: JID/node ⟺ AP outbox conversion:
Goffi <goffi@goffi.org>
parents:
3684
diff
changeset
|
568 url = f"{base_url}?{parse.urlencode(query_data, True)}" |
b15644cae50d
component AP gateway: JID/node ⟺ AP outbox conversion:
Goffi <goffi@goffi.org>
parents:
3684
diff
changeset
|
569 data = { |
b15644cae50d
component AP gateway: JID/node ⟺ AP outbox conversion:
Goffi <goffi@goffi.org>
parents:
3684
diff
changeset
|
570 "@context": "https://www.w3.org/ns/activitystreams", |
b15644cae50d
component AP gateway: JID/node ⟺ AP outbox conversion:
Goffi <goffi@goffi.org>
parents:
3684
diff
changeset
|
571 "id": url, |
b15644cae50d
component AP gateway: JID/node ⟺ AP outbox conversion:
Goffi <goffi@goffi.org>
parents:
3684
diff
changeset
|
572 "type": "OrderedCollectionPage", |
b15644cae50d
component AP gateway: JID/node ⟺ AP outbox conversion:
Goffi <goffi@goffi.org>
parents:
3684
diff
changeset
|
573 "partOf": base_url, |
b15644cae50d
component AP gateway: JID/node ⟺ AP outbox conversion:
Goffi <goffi@goffi.org>
parents:
3684
diff
changeset
|
574 "orderedItems" : [ |
b15644cae50d
component AP gateway: JID/node ⟺ AP outbox conversion:
Goffi <goffi@goffi.org>
parents:
3684
diff
changeset
|
575 await self.apg.mbdata2APitem( |
b15644cae50d
component AP gateway: JID/node ⟺ AP outbox conversion:
Goffi <goffi@goffi.org>
parents:
3684
diff
changeset
|
576 self.apg.client, |
b15644cae50d
component AP gateway: JID/node ⟺ AP outbox conversion:
Goffi <goffi@goffi.org>
parents:
3684
diff
changeset
|
577 await self.apg._m.item2mbdata( |
b15644cae50d
component AP gateway: JID/node ⟺ AP outbox conversion:
Goffi <goffi@goffi.org>
parents:
3684
diff
changeset
|
578 self.apg.client, |
b15644cae50d
component AP gateway: JID/node ⟺ AP outbox conversion:
Goffi <goffi@goffi.org>
parents:
3684
diff
changeset
|
579 item, |
b15644cae50d
component AP gateway: JID/node ⟺ AP outbox conversion:
Goffi <goffi@goffi.org>
parents:
3684
diff
changeset
|
580 account_jid, |
b15644cae50d
component AP gateway: JID/node ⟺ AP outbox conversion:
Goffi <goffi@goffi.org>
parents:
3684
diff
changeset
|
581 node |
b15644cae50d
component AP gateway: JID/node ⟺ AP outbox conversion:
Goffi <goffi@goffi.org>
parents:
3684
diff
changeset
|
582 ) |
b15644cae50d
component AP gateway: JID/node ⟺ AP outbox conversion:
Goffi <goffi@goffi.org>
parents:
3684
diff
changeset
|
583 ) |
b15644cae50d
component AP gateway: JID/node ⟺ AP outbox conversion:
Goffi <goffi@goffi.org>
parents:
3684
diff
changeset
|
584 for item in reversed(items) |
b15644cae50d
component AP gateway: JID/node ⟺ AP outbox conversion:
Goffi <goffi@goffi.org>
parents:
3684
diff
changeset
|
585 ] |
b15644cae50d
component AP gateway: JID/node ⟺ AP outbox conversion:
Goffi <goffi@goffi.org>
parents:
3684
diff
changeset
|
586 } |
b15644cae50d
component AP gateway: JID/node ⟺ AP outbox conversion:
Goffi <goffi@goffi.org>
parents:
3684
diff
changeset
|
587 |
b15644cae50d
component AP gateway: JID/node ⟺ AP outbox conversion:
Goffi <goffi@goffi.org>
parents:
3684
diff
changeset
|
588 # AP OrderedCollection must be in reversed chronological order, thus the opposite |
b15644cae50d
component AP gateway: JID/node ⟺ AP outbox conversion:
Goffi <goffi@goffi.org>
parents:
3684
diff
changeset
|
589 # of what we get with RSM (at least with Libervia Pubsub) |
b15644cae50d
component AP gateway: JID/node ⟺ AP outbox conversion:
Goffi <goffi@goffi.org>
parents:
3684
diff
changeset
|
590 if not metadata["complete"]: |
b15644cae50d
component AP gateway: JID/node ⟺ AP outbox conversion:
Goffi <goffi@goffi.org>
parents:
3684
diff
changeset
|
591 try: |
b15644cae50d
component AP gateway: JID/node ⟺ AP outbox conversion:
Goffi <goffi@goffi.org>
parents:
3684
diff
changeset
|
592 last= metadata["rsm"]["last"] |
b15644cae50d
component AP gateway: JID/node ⟺ AP outbox conversion:
Goffi <goffi@goffi.org>
parents:
3684
diff
changeset
|
593 except KeyError: |
b15644cae50d
component AP gateway: JID/node ⟺ AP outbox conversion:
Goffi <goffi@goffi.org>
parents:
3684
diff
changeset
|
594 last = None |
b15644cae50d
component AP gateway: JID/node ⟺ AP outbox conversion:
Goffi <goffi@goffi.org>
parents:
3684
diff
changeset
|
595 data["prev"] = f"{base_url}?{parse.urlencode({'after': last})}" |
b15644cae50d
component AP gateway: JID/node ⟺ AP outbox conversion:
Goffi <goffi@goffi.org>
parents:
3684
diff
changeset
|
596 if metadata["rsm"]["index"] != 0: |
b15644cae50d
component AP gateway: JID/node ⟺ AP outbox conversion:
Goffi <goffi@goffi.org>
parents:
3684
diff
changeset
|
597 try: |
b15644cae50d
component AP gateway: JID/node ⟺ AP outbox conversion:
Goffi <goffi@goffi.org>
parents:
3684
diff
changeset
|
598 first= metadata["rsm"]["first"] |
b15644cae50d
component AP gateway: JID/node ⟺ AP outbox conversion:
Goffi <goffi@goffi.org>
parents:
3684
diff
changeset
|
599 except KeyError: |
b15644cae50d
component AP gateway: JID/node ⟺ AP outbox conversion:
Goffi <goffi@goffi.org>
parents:
3684
diff
changeset
|
600 first = None |
b15644cae50d
component AP gateway: JID/node ⟺ AP outbox conversion:
Goffi <goffi@goffi.org>
parents:
3684
diff
changeset
|
601 data["next"] = f"{base_url}?{parse.urlencode({'before': first})}" |
b15644cae50d
component AP gateway: JID/node ⟺ AP outbox conversion:
Goffi <goffi@goffi.org>
parents:
3684
diff
changeset
|
602 |
b15644cae50d
component AP gateway: JID/node ⟺ AP outbox conversion:
Goffi <goffi@goffi.org>
parents:
3684
diff
changeset
|
603 return data |
b15644cae50d
component AP gateway: JID/node ⟺ AP outbox conversion:
Goffi <goffi@goffi.org>
parents:
3684
diff
changeset
|
604 |
b15644cae50d
component AP gateway: JID/node ⟺ AP outbox conversion:
Goffi <goffi@goffi.org>
parents:
3684
diff
changeset
|
605 async def APOutboxRequest( |
b15644cae50d
component AP gateway: JID/node ⟺ AP outbox conversion:
Goffi <goffi@goffi.org>
parents:
3684
diff
changeset
|
606 self, |
b15644cae50d
component AP gateway: JID/node ⟺ AP outbox conversion:
Goffi <goffi@goffi.org>
parents:
3684
diff
changeset
|
607 request: "HTTPRequest", |
b15644cae50d
component AP gateway: JID/node ⟺ AP outbox conversion:
Goffi <goffi@goffi.org>
parents:
3684
diff
changeset
|
608 account_jid: jid.JID, |
b15644cae50d
component AP gateway: JID/node ⟺ AP outbox conversion:
Goffi <goffi@goffi.org>
parents:
3684
diff
changeset
|
609 node: Optional[str], |
b15644cae50d
component AP gateway: JID/node ⟺ AP outbox conversion:
Goffi <goffi@goffi.org>
parents:
3684
diff
changeset
|
610 ap_account: str, |
3745
a8c7e5cef0cb
comp AP gateway: signature checking, caching and threads management:
Goffi <goffi@goffi.org>
parents:
3729
diff
changeset
|
611 ap_url: str, |
a8c7e5cef0cb
comp AP gateway: signature checking, caching and threads management:
Goffi <goffi@goffi.org>
parents:
3729
diff
changeset
|
612 signing_actor: Optional[str] |
3728
b15644cae50d
component AP gateway: JID/node ⟺ AP outbox conversion:
Goffi <goffi@goffi.org>
parents:
3684
diff
changeset
|
613 ) -> dict: |
b15644cae50d
component AP gateway: JID/node ⟺ AP outbox conversion:
Goffi <goffi@goffi.org>
parents:
3684
diff
changeset
|
614 if node is None: |
b15644cae50d
component AP gateway: JID/node ⟺ AP outbox conversion:
Goffi <goffi@goffi.org>
parents:
3684
diff
changeset
|
615 node = self.apg._m.namespace |
b15644cae50d
component AP gateway: JID/node ⟺ AP outbox conversion:
Goffi <goffi@goffi.org>
parents:
3684
diff
changeset
|
616 |
b15644cae50d
component AP gateway: JID/node ⟺ AP outbox conversion:
Goffi <goffi@goffi.org>
parents:
3684
diff
changeset
|
617 parsed_url = parse.urlparse(request.uri.decode()) |
b15644cae50d
component AP gateway: JID/node ⟺ AP outbox conversion:
Goffi <goffi@goffi.org>
parents:
3684
diff
changeset
|
618 query_data = parse.parse_qs(parsed_url.query) |
b15644cae50d
component AP gateway: JID/node ⟺ AP outbox conversion:
Goffi <goffi@goffi.org>
parents:
3684
diff
changeset
|
619 if query_data: |
b15644cae50d
component AP gateway: JID/node ⟺ AP outbox conversion:
Goffi <goffi@goffi.org>
parents:
3684
diff
changeset
|
620 return await self.APOutboxPageRequest( |
3729
86eea17cafa7
component AP gateway: split plugin in several files:
Goffi <goffi@goffi.org>
parents:
3728
diff
changeset
|
621 request, account_jid, node, ap_account, ap_url, query_data |
3728
b15644cae50d
component AP gateway: JID/node ⟺ AP outbox conversion:
Goffi <goffi@goffi.org>
parents:
3684
diff
changeset
|
622 ) |
b15644cae50d
component AP gateway: JID/node ⟺ AP outbox conversion:
Goffi <goffi@goffi.org>
parents:
3684
diff
changeset
|
623 |
b15644cae50d
component AP gateway: JID/node ⟺ AP outbox conversion:
Goffi <goffi@goffi.org>
parents:
3684
diff
changeset
|
624 # XXX: we can't use disco#info here because this request won't work on a bare jid |
b15644cae50d
component AP gateway: JID/node ⟺ AP outbox conversion:
Goffi <goffi@goffi.org>
parents:
3684
diff
changeset
|
625 # due to security considerations of XEP-0030 (we don't have presence |
b15644cae50d
component AP gateway: JID/node ⟺ AP outbox conversion:
Goffi <goffi@goffi.org>
parents:
3684
diff
changeset
|
626 # subscription). |
b15644cae50d
component AP gateway: JID/node ⟺ AP outbox conversion:
Goffi <goffi@goffi.org>
parents:
3684
diff
changeset
|
627 # The current workaround is to do a request as if RSM was available, and actually |
b15644cae50d
component AP gateway: JID/node ⟺ AP outbox conversion:
Goffi <goffi@goffi.org>
parents:
3684
diff
changeset
|
628 # check its availability according to result. |
b15644cae50d
component AP gateway: JID/node ⟺ AP outbox conversion:
Goffi <goffi@goffi.org>
parents:
3684
diff
changeset
|
629 try: |
b15644cae50d
component AP gateway: JID/node ⟺ AP outbox conversion:
Goffi <goffi@goffi.org>
parents:
3684
diff
changeset
|
630 __, metadata = await self.apg._p.getItems( |
b15644cae50d
component AP gateway: JID/node ⟺ AP outbox conversion:
Goffi <goffi@goffi.org>
parents:
3684
diff
changeset
|
631 client=self.apg.client, |
b15644cae50d
component AP gateway: JID/node ⟺ AP outbox conversion:
Goffi <goffi@goffi.org>
parents:
3684
diff
changeset
|
632 service=account_jid, |
b15644cae50d
component AP gateway: JID/node ⟺ AP outbox conversion:
Goffi <goffi@goffi.org>
parents:
3684
diff
changeset
|
633 node=node, |
b15644cae50d
component AP gateway: JID/node ⟺ AP outbox conversion:
Goffi <goffi@goffi.org>
parents:
3684
diff
changeset
|
634 max_items=0, |
3745
a8c7e5cef0cb
comp AP gateway: signature checking, caching and threads management:
Goffi <goffi@goffi.org>
parents:
3729
diff
changeset
|
635 rsm_request=rsm.RSMRequest(max_=0), |
a8c7e5cef0cb
comp AP gateway: signature checking, caching and threads management:
Goffi <goffi@goffi.org>
parents:
3729
diff
changeset
|
636 extra = {C.KEY_USE_CACHE: False} |
3728
b15644cae50d
component AP gateway: JID/node ⟺ AP outbox conversion:
Goffi <goffi@goffi.org>
parents:
3684
diff
changeset
|
637 ) |
b15644cae50d
component AP gateway: JID/node ⟺ AP outbox conversion:
Goffi <goffi@goffi.org>
parents:
3684
diff
changeset
|
638 except error.StanzaError as e: |
b15644cae50d
component AP gateway: JID/node ⟺ AP outbox conversion:
Goffi <goffi@goffi.org>
parents:
3684
diff
changeset
|
639 log.warning(f"Can't get data from pubsub node {node} at {account_jid}: {e}") |
b15644cae50d
component AP gateway: JID/node ⟺ AP outbox conversion:
Goffi <goffi@goffi.org>
parents:
3684
diff
changeset
|
640 return {} |
b15644cae50d
component AP gateway: JID/node ⟺ AP outbox conversion:
Goffi <goffi@goffi.org>
parents:
3684
diff
changeset
|
641 try: |
b15644cae50d
component AP gateway: JID/node ⟺ AP outbox conversion:
Goffi <goffi@goffi.org>
parents:
3684
diff
changeset
|
642 items_count = metadata["rsm"]["count"] |
b15644cae50d
component AP gateway: JID/node ⟺ AP outbox conversion:
Goffi <goffi@goffi.org>
parents:
3684
diff
changeset
|
643 except KeyError: |
b15644cae50d
component AP gateway: JID/node ⟺ AP outbox conversion:
Goffi <goffi@goffi.org>
parents:
3684
diff
changeset
|
644 log.warning( |
b15644cae50d
component AP gateway: JID/node ⟺ AP outbox conversion:
Goffi <goffi@goffi.org>
parents:
3684
diff
changeset
|
645 f"No RSM metadata found when requesting pubsub node {node} at " |
b15644cae50d
component AP gateway: JID/node ⟺ AP outbox conversion:
Goffi <goffi@goffi.org>
parents:
3684
diff
changeset
|
646 f"{account_jid}, defaulting to items_count=20" |
b15644cae50d
component AP gateway: JID/node ⟺ AP outbox conversion:
Goffi <goffi@goffi.org>
parents:
3684
diff
changeset
|
647 ) |
b15644cae50d
component AP gateway: JID/node ⟺ AP outbox conversion:
Goffi <goffi@goffi.org>
parents:
3684
diff
changeset
|
648 items_count = 20 |
b15644cae50d
component AP gateway: JID/node ⟺ AP outbox conversion:
Goffi <goffi@goffi.org>
parents:
3684
diff
changeset
|
649 |
b15644cae50d
component AP gateway: JID/node ⟺ AP outbox conversion:
Goffi <goffi@goffi.org>
parents:
3684
diff
changeset
|
650 url = self.getCanonicalURL(request) |
b15644cae50d
component AP gateway: JID/node ⟺ AP outbox conversion:
Goffi <goffi@goffi.org>
parents:
3684
diff
changeset
|
651 url_first_page = f"{url}?{parse.urlencode({'page': 'first'})}" |
b15644cae50d
component AP gateway: JID/node ⟺ AP outbox conversion:
Goffi <goffi@goffi.org>
parents:
3684
diff
changeset
|
652 url_last_page = f"{url}?{parse.urlencode({'page': 'last'})}" |
b15644cae50d
component AP gateway: JID/node ⟺ AP outbox conversion:
Goffi <goffi@goffi.org>
parents:
3684
diff
changeset
|
653 return { |
b15644cae50d
component AP gateway: JID/node ⟺ AP outbox conversion:
Goffi <goffi@goffi.org>
parents:
3684
diff
changeset
|
654 "@context": "https://www.w3.org/ns/activitystreams", |
b15644cae50d
component AP gateway: JID/node ⟺ AP outbox conversion:
Goffi <goffi@goffi.org>
parents:
3684
diff
changeset
|
655 "id": url, |
b15644cae50d
component AP gateway: JID/node ⟺ AP outbox conversion:
Goffi <goffi@goffi.org>
parents:
3684
diff
changeset
|
656 "totalItems": items_count, |
b15644cae50d
component AP gateway: JID/node ⟺ AP outbox conversion:
Goffi <goffi@goffi.org>
parents:
3684
diff
changeset
|
657 "type": "OrderedCollection", |
b15644cae50d
component AP gateway: JID/node ⟺ AP outbox conversion:
Goffi <goffi@goffi.org>
parents:
3684
diff
changeset
|
658 "first": url_first_page, |
b15644cae50d
component AP gateway: JID/node ⟺ AP outbox conversion:
Goffi <goffi@goffi.org>
parents:
3684
diff
changeset
|
659 "last": url_last_page, |
b15644cae50d
component AP gateway: JID/node ⟺ AP outbox conversion:
Goffi <goffi@goffi.org>
parents:
3684
diff
changeset
|
660 } |
b15644cae50d
component AP gateway: JID/node ⟺ AP outbox conversion:
Goffi <goffi@goffi.org>
parents:
3684
diff
changeset
|
661 |
3745
a8c7e5cef0cb
comp AP gateway: signature checking, caching and threads management:
Goffi <goffi@goffi.org>
parents:
3729
diff
changeset
|
662 async def APInboxRequest( |
a8c7e5cef0cb
comp AP gateway: signature checking, caching and threads management:
Goffi <goffi@goffi.org>
parents:
3729
diff
changeset
|
663 self, |
a8c7e5cef0cb
comp AP gateway: signature checking, caching and threads management:
Goffi <goffi@goffi.org>
parents:
3729
diff
changeset
|
664 request: "HTTPRequest", |
a8c7e5cef0cb
comp AP gateway: signature checking, caching and threads management:
Goffi <goffi@goffi.org>
parents:
3729
diff
changeset
|
665 account_jid: Optional[jid.JID], |
a8c7e5cef0cb
comp AP gateway: signature checking, caching and threads management:
Goffi <goffi@goffi.org>
parents:
3729
diff
changeset
|
666 node: Optional[str], |
a8c7e5cef0cb
comp AP gateway: signature checking, caching and threads management:
Goffi <goffi@goffi.org>
parents:
3729
diff
changeset
|
667 ap_account: Optional[str], |
a8c7e5cef0cb
comp AP gateway: signature checking, caching and threads management:
Goffi <goffi@goffi.org>
parents:
3729
diff
changeset
|
668 ap_url: str, |
a8c7e5cef0cb
comp AP gateway: signature checking, caching and threads management:
Goffi <goffi@goffi.org>
parents:
3729
diff
changeset
|
669 signing_actor: Optional[str] |
a8c7e5cef0cb
comp AP gateway: signature checking, caching and threads management:
Goffi <goffi@goffi.org>
parents:
3729
diff
changeset
|
670 ) -> None: |
a8c7e5cef0cb
comp AP gateway: signature checking, caching and threads management:
Goffi <goffi@goffi.org>
parents:
3729
diff
changeset
|
671 if signing_actor is None: |
a8c7e5cef0cb
comp AP gateway: signature checking, caching and threads management:
Goffi <goffi@goffi.org>
parents:
3729
diff
changeset
|
672 raise exceptions.InternalError("signing_actor must be set for inbox requests") |
a8c7e5cef0cb
comp AP gateway: signature checking, caching and threads management:
Goffi <goffi@goffi.org>
parents:
3729
diff
changeset
|
673 if node is None: |
a8c7e5cef0cb
comp AP gateway: signature checking, caching and threads management:
Goffi <goffi@goffi.org>
parents:
3729
diff
changeset
|
674 node = self.apg._m.namespace |
a8c7e5cef0cb
comp AP gateway: signature checking, caching and threads management:
Goffi <goffi@goffi.org>
parents:
3729
diff
changeset
|
675 try: |
a8c7e5cef0cb
comp AP gateway: signature checking, caching and threads management:
Goffi <goffi@goffi.org>
parents:
3729
diff
changeset
|
676 data = json.load(request.content) |
a8c7e5cef0cb
comp AP gateway: signature checking, caching and threads management:
Goffi <goffi@goffi.org>
parents:
3729
diff
changeset
|
677 if not isinstance(data, dict): |
a8c7e5cef0cb
comp AP gateway: signature checking, caching and threads management:
Goffi <goffi@goffi.org>
parents:
3729
diff
changeset
|
678 raise ValueError("data should be an object") |
a8c7e5cef0cb
comp AP gateway: signature checking, caching and threads management:
Goffi <goffi@goffi.org>
parents:
3729
diff
changeset
|
679 except (json.JSONDecodeError, ValueError) as e: |
a8c7e5cef0cb
comp AP gateway: signature checking, caching and threads management:
Goffi <goffi@goffi.org>
parents:
3729
diff
changeset
|
680 return self.responseCode( |
a8c7e5cef0cb
comp AP gateway: signature checking, caching and threads management:
Goffi <goffi@goffi.org>
parents:
3729
diff
changeset
|
681 request, |
a8c7e5cef0cb
comp AP gateway: signature checking, caching and threads management:
Goffi <goffi@goffi.org>
parents:
3729
diff
changeset
|
682 http.BAD_REQUEST, |
a8c7e5cef0cb
comp AP gateway: signature checking, caching and threads management:
Goffi <goffi@goffi.org>
parents:
3729
diff
changeset
|
683 f"invalid json in inbox request: {e}" |
a8c7e5cef0cb
comp AP gateway: signature checking, caching and threads management:
Goffi <goffi@goffi.org>
parents:
3729
diff
changeset
|
684 ) |
a8c7e5cef0cb
comp AP gateway: signature checking, caching and threads management:
Goffi <goffi@goffi.org>
parents:
3729
diff
changeset
|
685 await self.checkSigningActor(data, signing_actor) |
a8c7e5cef0cb
comp AP gateway: signature checking, caching and threads management:
Goffi <goffi@goffi.org>
parents:
3729
diff
changeset
|
686 activity_type = (data.get("type") or "").lower() |
a8c7e5cef0cb
comp AP gateway: signature checking, caching and threads management:
Goffi <goffi@goffi.org>
parents:
3729
diff
changeset
|
687 if not activity_type in ACTIVITY_TYPES_LOWER: |
a8c7e5cef0cb
comp AP gateway: signature checking, caching and threads management:
Goffi <goffi@goffi.org>
parents:
3729
diff
changeset
|
688 return self.responseCode( |
a8c7e5cef0cb
comp AP gateway: signature checking, caching and threads management:
Goffi <goffi@goffi.org>
parents:
3729
diff
changeset
|
689 request, |
a8c7e5cef0cb
comp AP gateway: signature checking, caching and threads management:
Goffi <goffi@goffi.org>
parents:
3729
diff
changeset
|
690 http.UNSUPPORTED_MEDIA_TYPE, |
a8c7e5cef0cb
comp AP gateway: signature checking, caching and threads management:
Goffi <goffi@goffi.org>
parents:
3729
diff
changeset
|
691 f"request is not an activity, ignoring" |
a8c7e5cef0cb
comp AP gateway: signature checking, caching and threads management:
Goffi <goffi@goffi.org>
parents:
3729
diff
changeset
|
692 ) |
a8c7e5cef0cb
comp AP gateway: signature checking, caching and threads management:
Goffi <goffi@goffi.org>
parents:
3729
diff
changeset
|
693 |
a8c7e5cef0cb
comp AP gateway: signature checking, caching and threads management:
Goffi <goffi@goffi.org>
parents:
3729
diff
changeset
|
694 if account_jid is None and activity_type not in ACTIVIY_NO_ACCOUNT_ALLOWED: |
a8c7e5cef0cb
comp AP gateway: signature checking, caching and threads management:
Goffi <goffi@goffi.org>
parents:
3729
diff
changeset
|
695 return self.responseCode( |
a8c7e5cef0cb
comp AP gateway: signature checking, caching and threads management:
Goffi <goffi@goffi.org>
parents:
3729
diff
changeset
|
696 request, |
a8c7e5cef0cb
comp AP gateway: signature checking, caching and threads management:
Goffi <goffi@goffi.org>
parents:
3729
diff
changeset
|
697 http.UNSUPPORTED_MEDIA_TYPE, |
a8c7e5cef0cb
comp AP gateway: signature checking, caching and threads management:
Goffi <goffi@goffi.org>
parents:
3729
diff
changeset
|
698 f"{activity_type.title()!r} activity must target an account" |
a8c7e5cef0cb
comp AP gateway: signature checking, caching and threads management:
Goffi <goffi@goffi.org>
parents:
3729
diff
changeset
|
699 ) |
a8c7e5cef0cb
comp AP gateway: signature checking, caching and threads management:
Goffi <goffi@goffi.org>
parents:
3729
diff
changeset
|
700 |
a8c7e5cef0cb
comp AP gateway: signature checking, caching and threads management:
Goffi <goffi@goffi.org>
parents:
3729
diff
changeset
|
701 try: |
a8c7e5cef0cb
comp AP gateway: signature checking, caching and threads management:
Goffi <goffi@goffi.org>
parents:
3729
diff
changeset
|
702 method = getattr(self, f"handle{activity_type.title()}Activity") |
a8c7e5cef0cb
comp AP gateway: signature checking, caching and threads management:
Goffi <goffi@goffi.org>
parents:
3729
diff
changeset
|
703 except AttributeError: |
a8c7e5cef0cb
comp AP gateway: signature checking, caching and threads management:
Goffi <goffi@goffi.org>
parents:
3729
diff
changeset
|
704 return self.responseCode( |
a8c7e5cef0cb
comp AP gateway: signature checking, caching and threads management:
Goffi <goffi@goffi.org>
parents:
3729
diff
changeset
|
705 request, |
a8c7e5cef0cb
comp AP gateway: signature checking, caching and threads management:
Goffi <goffi@goffi.org>
parents:
3729
diff
changeset
|
706 http.UNSUPPORTED_MEDIA_TYPE, |
a8c7e5cef0cb
comp AP gateway: signature checking, caching and threads management:
Goffi <goffi@goffi.org>
parents:
3729
diff
changeset
|
707 f"{activity_type.title()} activity is not yet supported" |
a8c7e5cef0cb
comp AP gateway: signature checking, caching and threads management:
Goffi <goffi@goffi.org>
parents:
3729
diff
changeset
|
708 ) |
a8c7e5cef0cb
comp AP gateway: signature checking, caching and threads management:
Goffi <goffi@goffi.org>
parents:
3729
diff
changeset
|
709 else: |
a8c7e5cef0cb
comp AP gateway: signature checking, caching and threads management:
Goffi <goffi@goffi.org>
parents:
3729
diff
changeset
|
710 await method( |
a8c7e5cef0cb
comp AP gateway: signature checking, caching and threads management:
Goffi <goffi@goffi.org>
parents:
3729
diff
changeset
|
711 request, data, account_jid, node, ap_account, ap_url, signing_actor |
a8c7e5cef0cb
comp AP gateway: signature checking, caching and threads management:
Goffi <goffi@goffi.org>
parents:
3729
diff
changeset
|
712 ) |
a8c7e5cef0cb
comp AP gateway: signature checking, caching and threads management:
Goffi <goffi@goffi.org>
parents:
3729
diff
changeset
|
713 |
3764
125c7043b277
comp AP gateway: publish, (un)subscribe/(un)follow, public subscription/following/followers:
Goffi <goffi@goffi.org>
parents:
3745
diff
changeset
|
714 async def APFollowersRequest( |
125c7043b277
comp AP gateway: publish, (un)subscribe/(un)follow, public subscription/following/followers:
Goffi <goffi@goffi.org>
parents:
3745
diff
changeset
|
715 self, |
125c7043b277
comp AP gateway: publish, (un)subscribe/(un)follow, public subscription/following/followers:
Goffi <goffi@goffi.org>
parents:
3745
diff
changeset
|
716 request: "HTTPRequest", |
125c7043b277
comp AP gateway: publish, (un)subscribe/(un)follow, public subscription/following/followers:
Goffi <goffi@goffi.org>
parents:
3745
diff
changeset
|
717 account_jid: jid.JID, |
125c7043b277
comp AP gateway: publish, (un)subscribe/(un)follow, public subscription/following/followers:
Goffi <goffi@goffi.org>
parents:
3745
diff
changeset
|
718 node: Optional[str], |
125c7043b277
comp AP gateway: publish, (un)subscribe/(un)follow, public subscription/following/followers:
Goffi <goffi@goffi.org>
parents:
3745
diff
changeset
|
719 ap_account: Optional[str], |
125c7043b277
comp AP gateway: publish, (un)subscribe/(un)follow, public subscription/following/followers:
Goffi <goffi@goffi.org>
parents:
3745
diff
changeset
|
720 ap_url: str, |
125c7043b277
comp AP gateway: publish, (un)subscribe/(un)follow, public subscription/following/followers:
Goffi <goffi@goffi.org>
parents:
3745
diff
changeset
|
721 signing_actor: Optional[str] |
125c7043b277
comp AP gateway: publish, (un)subscribe/(un)follow, public subscription/following/followers:
Goffi <goffi@goffi.org>
parents:
3745
diff
changeset
|
722 ) -> dict: |
125c7043b277
comp AP gateway: publish, (un)subscribe/(un)follow, public subscription/following/followers:
Goffi <goffi@goffi.org>
parents:
3745
diff
changeset
|
723 if node is None: |
125c7043b277
comp AP gateway: publish, (un)subscribe/(un)follow, public subscription/following/followers:
Goffi <goffi@goffi.org>
parents:
3745
diff
changeset
|
724 node = self.apg._m.namespace |
125c7043b277
comp AP gateway: publish, (un)subscribe/(un)follow, public subscription/following/followers:
Goffi <goffi@goffi.org>
parents:
3745
diff
changeset
|
725 client = self.apg.client |
125c7043b277
comp AP gateway: publish, (un)subscribe/(un)follow, public subscription/following/followers:
Goffi <goffi@goffi.org>
parents:
3745
diff
changeset
|
726 subscribers = await self.apg._pps.getPublicNodeSubscriptions( |
125c7043b277
comp AP gateway: publish, (un)subscribe/(un)follow, public subscription/following/followers:
Goffi <goffi@goffi.org>
parents:
3745
diff
changeset
|
727 client, account_jid, node |
125c7043b277
comp AP gateway: publish, (un)subscribe/(un)follow, public subscription/following/followers:
Goffi <goffi@goffi.org>
parents:
3745
diff
changeset
|
728 ) |
125c7043b277
comp AP gateway: publish, (un)subscribe/(un)follow, public subscription/following/followers:
Goffi <goffi@goffi.org>
parents:
3745
diff
changeset
|
729 followers = [] |
125c7043b277
comp AP gateway: publish, (un)subscribe/(un)follow, public subscription/following/followers:
Goffi <goffi@goffi.org>
parents:
3745
diff
changeset
|
730 for subscriber in subscribers.keys(): |
3869
c0bcbcf5b4b7
component AP gateway: handle `Like` and `Undo`/`Like` activities:
Goffi <goffi@goffi.org>
parents:
3847
diff
changeset
|
731 if self.apg.isVirtualJID(subscriber): |
3764
125c7043b277
comp AP gateway: publish, (un)subscribe/(un)follow, public subscription/following/followers:
Goffi <goffi@goffi.org>
parents:
3745
diff
changeset
|
732 # the subscriber is an AP user subscribed with this gateway |
125c7043b277
comp AP gateway: publish, (un)subscribe/(un)follow, public subscription/following/followers:
Goffi <goffi@goffi.org>
parents:
3745
diff
changeset
|
733 ap_account = self.apg._e.unescape(subscriber.user) |
125c7043b277
comp AP gateway: publish, (un)subscribe/(un)follow, public subscription/following/followers:
Goffi <goffi@goffi.org>
parents:
3745
diff
changeset
|
734 else: |
125c7043b277
comp AP gateway: publish, (un)subscribe/(un)follow, public subscription/following/followers:
Goffi <goffi@goffi.org>
parents:
3745
diff
changeset
|
735 # regular XMPP user |
125c7043b277
comp AP gateway: publish, (un)subscribe/(un)follow, public subscription/following/followers:
Goffi <goffi@goffi.org>
parents:
3745
diff
changeset
|
736 ap_account = await self.apg.getAPAccountFromJidAndNode(subscriber, node) |
125c7043b277
comp AP gateway: publish, (un)subscribe/(un)follow, public subscription/following/followers:
Goffi <goffi@goffi.org>
parents:
3745
diff
changeset
|
737 followers.append(ap_account) |
125c7043b277
comp AP gateway: publish, (un)subscribe/(un)follow, public subscription/following/followers:
Goffi <goffi@goffi.org>
parents:
3745
diff
changeset
|
738 |
125c7043b277
comp AP gateway: publish, (un)subscribe/(un)follow, public subscription/following/followers:
Goffi <goffi@goffi.org>
parents:
3745
diff
changeset
|
739 url = self.getCanonicalURL(request) |
125c7043b277
comp AP gateway: publish, (un)subscribe/(un)follow, public subscription/following/followers:
Goffi <goffi@goffi.org>
parents:
3745
diff
changeset
|
740 return { |
125c7043b277
comp AP gateway: publish, (un)subscribe/(un)follow, public subscription/following/followers:
Goffi <goffi@goffi.org>
parents:
3745
diff
changeset
|
741 "@context": "https://www.w3.org/ns/activitystreams", |
125c7043b277
comp AP gateway: publish, (un)subscribe/(un)follow, public subscription/following/followers:
Goffi <goffi@goffi.org>
parents:
3745
diff
changeset
|
742 "type": "OrderedCollection", |
125c7043b277
comp AP gateway: publish, (un)subscribe/(un)follow, public subscription/following/followers:
Goffi <goffi@goffi.org>
parents:
3745
diff
changeset
|
743 "id": url, |
125c7043b277
comp AP gateway: publish, (un)subscribe/(un)follow, public subscription/following/followers:
Goffi <goffi@goffi.org>
parents:
3745
diff
changeset
|
744 "totalItems": len(subscribers), |
125c7043b277
comp AP gateway: publish, (un)subscribe/(un)follow, public subscription/following/followers:
Goffi <goffi@goffi.org>
parents:
3745
diff
changeset
|
745 "first": { |
125c7043b277
comp AP gateway: publish, (un)subscribe/(un)follow, public subscription/following/followers:
Goffi <goffi@goffi.org>
parents:
3745
diff
changeset
|
746 "type": "OrderedCollectionPage", |
125c7043b277
comp AP gateway: publish, (un)subscribe/(un)follow, public subscription/following/followers:
Goffi <goffi@goffi.org>
parents:
3745
diff
changeset
|
747 "id": url, |
125c7043b277
comp AP gateway: publish, (un)subscribe/(un)follow, public subscription/following/followers:
Goffi <goffi@goffi.org>
parents:
3745
diff
changeset
|
748 "orderedItems": followers |
125c7043b277
comp AP gateway: publish, (un)subscribe/(un)follow, public subscription/following/followers:
Goffi <goffi@goffi.org>
parents:
3745
diff
changeset
|
749 } |
125c7043b277
comp AP gateway: publish, (un)subscribe/(un)follow, public subscription/following/followers:
Goffi <goffi@goffi.org>
parents:
3745
diff
changeset
|
750 } |
125c7043b277
comp AP gateway: publish, (un)subscribe/(un)follow, public subscription/following/followers:
Goffi <goffi@goffi.org>
parents:
3745
diff
changeset
|
751 |
125c7043b277
comp AP gateway: publish, (un)subscribe/(un)follow, public subscription/following/followers:
Goffi <goffi@goffi.org>
parents:
3745
diff
changeset
|
752 async def APFollowingRequest( |
125c7043b277
comp AP gateway: publish, (un)subscribe/(un)follow, public subscription/following/followers:
Goffi <goffi@goffi.org>
parents:
3745
diff
changeset
|
753 self, |
125c7043b277
comp AP gateway: publish, (un)subscribe/(un)follow, public subscription/following/followers:
Goffi <goffi@goffi.org>
parents:
3745
diff
changeset
|
754 request: "HTTPRequest", |
125c7043b277
comp AP gateway: publish, (un)subscribe/(un)follow, public subscription/following/followers:
Goffi <goffi@goffi.org>
parents:
3745
diff
changeset
|
755 account_jid: jid.JID, |
125c7043b277
comp AP gateway: publish, (un)subscribe/(un)follow, public subscription/following/followers:
Goffi <goffi@goffi.org>
parents:
3745
diff
changeset
|
756 node: Optional[str], |
125c7043b277
comp AP gateway: publish, (un)subscribe/(un)follow, public subscription/following/followers:
Goffi <goffi@goffi.org>
parents:
3745
diff
changeset
|
757 ap_account: Optional[str], |
125c7043b277
comp AP gateway: publish, (un)subscribe/(un)follow, public subscription/following/followers:
Goffi <goffi@goffi.org>
parents:
3745
diff
changeset
|
758 ap_url: str, |
125c7043b277
comp AP gateway: publish, (un)subscribe/(un)follow, public subscription/following/followers:
Goffi <goffi@goffi.org>
parents:
3745
diff
changeset
|
759 signing_actor: Optional[str] |
125c7043b277
comp AP gateway: publish, (un)subscribe/(un)follow, public subscription/following/followers:
Goffi <goffi@goffi.org>
parents:
3745
diff
changeset
|
760 ) -> dict[str, Any]: |
125c7043b277
comp AP gateway: publish, (un)subscribe/(un)follow, public subscription/following/followers:
Goffi <goffi@goffi.org>
parents:
3745
diff
changeset
|
761 client = self.apg.client |
125c7043b277
comp AP gateway: publish, (un)subscribe/(un)follow, public subscription/following/followers:
Goffi <goffi@goffi.org>
parents:
3745
diff
changeset
|
762 subscriptions = await self.apg._pps.subscriptions( |
125c7043b277
comp AP gateway: publish, (un)subscribe/(un)follow, public subscription/following/followers:
Goffi <goffi@goffi.org>
parents:
3745
diff
changeset
|
763 client, account_jid, node |
125c7043b277
comp AP gateway: publish, (un)subscribe/(un)follow, public subscription/following/followers:
Goffi <goffi@goffi.org>
parents:
3745
diff
changeset
|
764 ) |
125c7043b277
comp AP gateway: publish, (un)subscribe/(un)follow, public subscription/following/followers:
Goffi <goffi@goffi.org>
parents:
3745
diff
changeset
|
765 following = [] |
125c7043b277
comp AP gateway: publish, (un)subscribe/(un)follow, public subscription/following/followers:
Goffi <goffi@goffi.org>
parents:
3745
diff
changeset
|
766 for sub_dict in subscriptions: |
125c7043b277
comp AP gateway: publish, (un)subscribe/(un)follow, public subscription/following/followers:
Goffi <goffi@goffi.org>
parents:
3745
diff
changeset
|
767 service = jid.JID(sub_dict["service"]) |
3869
c0bcbcf5b4b7
component AP gateway: handle `Like` and `Undo`/`Like` activities:
Goffi <goffi@goffi.org>
parents:
3847
diff
changeset
|
768 if self.apg.isVirtualJID(service): |
3764
125c7043b277
comp AP gateway: publish, (un)subscribe/(un)follow, public subscription/following/followers:
Goffi <goffi@goffi.org>
parents:
3745
diff
changeset
|
769 # the subscription is to an AP actor with this gateway |
125c7043b277
comp AP gateway: publish, (un)subscribe/(un)follow, public subscription/following/followers:
Goffi <goffi@goffi.org>
parents:
3745
diff
changeset
|
770 ap_account = self.apg._e.unescape(service.user) |
125c7043b277
comp AP gateway: publish, (un)subscribe/(un)follow, public subscription/following/followers:
Goffi <goffi@goffi.org>
parents:
3745
diff
changeset
|
771 else: |
125c7043b277
comp AP gateway: publish, (un)subscribe/(un)follow, public subscription/following/followers:
Goffi <goffi@goffi.org>
parents:
3745
diff
changeset
|
772 # regular XMPP user |
125c7043b277
comp AP gateway: publish, (un)subscribe/(un)follow, public subscription/following/followers:
Goffi <goffi@goffi.org>
parents:
3745
diff
changeset
|
773 ap_account = await self.apg.getAPAccountFromJidAndNode( |
125c7043b277
comp AP gateway: publish, (un)subscribe/(un)follow, public subscription/following/followers:
Goffi <goffi@goffi.org>
parents:
3745
diff
changeset
|
774 service, sub_dict["node"] |
125c7043b277
comp AP gateway: publish, (un)subscribe/(un)follow, public subscription/following/followers:
Goffi <goffi@goffi.org>
parents:
3745
diff
changeset
|
775 ) |
125c7043b277
comp AP gateway: publish, (un)subscribe/(un)follow, public subscription/following/followers:
Goffi <goffi@goffi.org>
parents:
3745
diff
changeset
|
776 following.append(ap_account) |
125c7043b277
comp AP gateway: publish, (un)subscribe/(un)follow, public subscription/following/followers:
Goffi <goffi@goffi.org>
parents:
3745
diff
changeset
|
777 |
125c7043b277
comp AP gateway: publish, (un)subscribe/(un)follow, public subscription/following/followers:
Goffi <goffi@goffi.org>
parents:
3745
diff
changeset
|
778 url = self.getCanonicalURL(request) |
125c7043b277
comp AP gateway: publish, (un)subscribe/(un)follow, public subscription/following/followers:
Goffi <goffi@goffi.org>
parents:
3745
diff
changeset
|
779 return { |
125c7043b277
comp AP gateway: publish, (un)subscribe/(un)follow, public subscription/following/followers:
Goffi <goffi@goffi.org>
parents:
3745
diff
changeset
|
780 "@context": "https://www.w3.org/ns/activitystreams", |
125c7043b277
comp AP gateway: publish, (un)subscribe/(un)follow, public subscription/following/followers:
Goffi <goffi@goffi.org>
parents:
3745
diff
changeset
|
781 "type": "OrderedCollection", |
125c7043b277
comp AP gateway: publish, (un)subscribe/(un)follow, public subscription/following/followers:
Goffi <goffi@goffi.org>
parents:
3745
diff
changeset
|
782 "id": url, |
125c7043b277
comp AP gateway: publish, (un)subscribe/(un)follow, public subscription/following/followers:
Goffi <goffi@goffi.org>
parents:
3745
diff
changeset
|
783 "totalItems": len(subscriptions), |
125c7043b277
comp AP gateway: publish, (un)subscribe/(un)follow, public subscription/following/followers:
Goffi <goffi@goffi.org>
parents:
3745
diff
changeset
|
784 "first": { |
125c7043b277
comp AP gateway: publish, (un)subscribe/(un)follow, public subscription/following/followers:
Goffi <goffi@goffi.org>
parents:
3745
diff
changeset
|
785 "type": "OrderedCollectionPage", |
125c7043b277
comp AP gateway: publish, (un)subscribe/(un)follow, public subscription/following/followers:
Goffi <goffi@goffi.org>
parents:
3745
diff
changeset
|
786 "id": url, |
125c7043b277
comp AP gateway: publish, (un)subscribe/(un)follow, public subscription/following/followers:
Goffi <goffi@goffi.org>
parents:
3745
diff
changeset
|
787 "orderedItems": following |
125c7043b277
comp AP gateway: publish, (un)subscribe/(un)follow, public subscription/following/followers:
Goffi <goffi@goffi.org>
parents:
3745
diff
changeset
|
788 } |
125c7043b277
comp AP gateway: publish, (un)subscribe/(un)follow, public subscription/following/followers:
Goffi <goffi@goffi.org>
parents:
3745
diff
changeset
|
789 } |
125c7043b277
comp AP gateway: publish, (un)subscribe/(un)follow, public subscription/following/followers:
Goffi <goffi@goffi.org>
parents:
3745
diff
changeset
|
790 |
3745
a8c7e5cef0cb
comp AP gateway: signature checking, caching and threads management:
Goffi <goffi@goffi.org>
parents:
3729
diff
changeset
|
791 async def APRequest( |
a8c7e5cef0cb
comp AP gateway: signature checking, caching and threads management:
Goffi <goffi@goffi.org>
parents:
3729
diff
changeset
|
792 self, |
a8c7e5cef0cb
comp AP gateway: signature checking, caching and threads management:
Goffi <goffi@goffi.org>
parents:
3729
diff
changeset
|
793 request: "HTTPRequest", |
a8c7e5cef0cb
comp AP gateway: signature checking, caching and threads management:
Goffi <goffi@goffi.org>
parents:
3729
diff
changeset
|
794 signing_actor: Optional[str] = None |
a8c7e5cef0cb
comp AP gateway: signature checking, caching and threads management:
Goffi <goffi@goffi.org>
parents:
3729
diff
changeset
|
795 ) -> None: |
3847
aaa4e7815ba8
component AP gateway: new `verbose` attribute in AP gateway to activate debug logs:
Goffi <goffi@goffi.org>
parents:
3846
diff
changeset
|
796 if self.apg.verbose: |
aaa4e7815ba8
component AP gateway: new `verbose` attribute in AP gateway to activate debug logs:
Goffi <goffi@goffi.org>
parents:
3846
diff
changeset
|
797 try: |
aaa4e7815ba8
component AP gateway: new `verbose` attribute in AP gateway to activate debug logs:
Goffi <goffi@goffi.org>
parents:
3846
diff
changeset
|
798 data = json.load(request.content) |
aaa4e7815ba8
component AP gateway: new `verbose` attribute in AP gateway to activate debug logs:
Goffi <goffi@goffi.org>
parents:
3846
diff
changeset
|
799 except (json.JSONDecodeError, ValueError) as e: |
aaa4e7815ba8
component AP gateway: new `verbose` attribute in AP gateway to activate debug logs:
Goffi <goffi@goffi.org>
parents:
3846
diff
changeset
|
800 pass |
aaa4e7815ba8
component AP gateway: new `verbose` attribute in AP gateway to activate debug logs:
Goffi <goffi@goffi.org>
parents:
3846
diff
changeset
|
801 else: |
aaa4e7815ba8
component AP gateway: new `verbose` attribute in AP gateway to activate debug logs:
Goffi <goffi@goffi.org>
parents:
3846
diff
changeset
|
802 from pprint import pformat |
aaa4e7815ba8
component AP gateway: new `verbose` attribute in AP gateway to activate debug logs:
Goffi <goffi@goffi.org>
parents:
3846
diff
changeset
|
803 log.info( |
aaa4e7815ba8
component AP gateway: new `verbose` attribute in AP gateway to activate debug logs:
Goffi <goffi@goffi.org>
parents:
3846
diff
changeset
|
804 f"==> got {request.method.decode()} request:\n{pformat(data)}\n---" |
aaa4e7815ba8
component AP gateway: new `verbose` attribute in AP gateway to activate debug logs:
Goffi <goffi@goffi.org>
parents:
3846
diff
changeset
|
805 ) |
aaa4e7815ba8
component AP gateway: new `verbose` attribute in AP gateway to activate debug logs:
Goffi <goffi@goffi.org>
parents:
3846
diff
changeset
|
806 |
aaa4e7815ba8
component AP gateway: new `verbose` attribute in AP gateway to activate debug logs:
Goffi <goffi@goffi.org>
parents:
3846
diff
changeset
|
807 finally: |
aaa4e7815ba8
component AP gateway: new `verbose` attribute in AP gateway to activate debug logs:
Goffi <goffi@goffi.org>
parents:
3846
diff
changeset
|
808 request.content.seek(0) |
aaa4e7815ba8
component AP gateway: new `verbose` attribute in AP gateway to activate debug logs:
Goffi <goffi@goffi.org>
parents:
3846
diff
changeset
|
809 |
3728
b15644cae50d
component AP gateway: JID/node ⟺ AP outbox conversion:
Goffi <goffi@goffi.org>
parents:
3684
diff
changeset
|
810 path = request.path.decode() |
3729
86eea17cafa7
component AP gateway: split plugin in several files:
Goffi <goffi@goffi.org>
parents:
3728
diff
changeset
|
811 ap_url = parse.urljoin( |
3728
b15644cae50d
component AP gateway: JID/node ⟺ AP outbox conversion:
Goffi <goffi@goffi.org>
parents:
3684
diff
changeset
|
812 f"https://{self.apg.public_url}", |
b15644cae50d
component AP gateway: JID/node ⟺ AP outbox conversion:
Goffi <goffi@goffi.org>
parents:
3684
diff
changeset
|
813 path |
b15644cae50d
component AP gateway: JID/node ⟺ AP outbox conversion:
Goffi <goffi@goffi.org>
parents:
3684
diff
changeset
|
814 ) |
3745
a8c7e5cef0cb
comp AP gateway: signature checking, caching and threads management:
Goffi <goffi@goffi.org>
parents:
3729
diff
changeset
|
815 request_type, extra_args = self.apg.parseAPURL(ap_url) |
a8c7e5cef0cb
comp AP gateway: signature checking, caching and threads management:
Goffi <goffi@goffi.org>
parents:
3729
diff
changeset
|
816 if len(extra_args) == 0: |
a8c7e5cef0cb
comp AP gateway: signature checking, caching and threads management:
Goffi <goffi@goffi.org>
parents:
3729
diff
changeset
|
817 if request_type != "shared_inbox": |
a8c7e5cef0cb
comp AP gateway: signature checking, caching and threads management:
Goffi <goffi@goffi.org>
parents:
3729
diff
changeset
|
818 raise exceptions.DataError(f"Invalid request type: {request_type!r}") |
a8c7e5cef0cb
comp AP gateway: signature checking, caching and threads management:
Goffi <goffi@goffi.org>
parents:
3729
diff
changeset
|
819 ret_data = await self.APInboxRequest( |
a8c7e5cef0cb
comp AP gateway: signature checking, caching and threads management:
Goffi <goffi@goffi.org>
parents:
3729
diff
changeset
|
820 request, None, None, None, ap_url, signing_actor |
a8c7e5cef0cb
comp AP gateway: signature checking, caching and threads management:
Goffi <goffi@goffi.org>
parents:
3729
diff
changeset
|
821 ) |
3821
0b1c30ff2cbb
component AP: XMPP identity => AP actor data converstion:
Goffi <goffi@goffi.org>
parents:
3807
diff
changeset
|
822 elif request_type == "avatar": |
0b1c30ff2cbb
component AP: XMPP identity => AP actor data converstion:
Goffi <goffi@goffi.org>
parents:
3807
diff
changeset
|
823 if len(extra_args) != 1: |
0b1c30ff2cbb
component AP: XMPP identity => AP actor data converstion:
Goffi <goffi@goffi.org>
parents:
3807
diff
changeset
|
824 raise exceptions.DataError("avatar argument expected in URL") |
0b1c30ff2cbb
component AP: XMPP identity => AP actor data converstion:
Goffi <goffi@goffi.org>
parents:
3807
diff
changeset
|
825 avatar_filename = extra_args[0] |
0b1c30ff2cbb
component AP: XMPP identity => AP actor data converstion:
Goffi <goffi@goffi.org>
parents:
3807
diff
changeset
|
826 avatar_path = self.apg.host.common_cache.getPath(avatar_filename) |
0b1c30ff2cbb
component AP: XMPP identity => AP actor data converstion:
Goffi <goffi@goffi.org>
parents:
3807
diff
changeset
|
827 return static.File(str(avatar_path)).render(request) |
3846
cc13efdd8360
component AP gateway: return item when `item` URL is used:
Goffi <goffi@goffi.org>
parents:
3844
diff
changeset
|
828 elif request_type == "item": |
cc13efdd8360
component AP gateway: return item when `item` URL is used:
Goffi <goffi@goffi.org>
parents:
3844
diff
changeset
|
829 ret_data = await self.apg.apGetLocalObject(ap_url) |
cc13efdd8360
component AP gateway: return item when `item` URL is used:
Goffi <goffi@goffi.org>
parents:
3844
diff
changeset
|
830 ret_data["@context"] = NS_AP |
3745
a8c7e5cef0cb
comp AP gateway: signature checking, caching and threads management:
Goffi <goffi@goffi.org>
parents:
3729
diff
changeset
|
831 else: |
a8c7e5cef0cb
comp AP gateway: signature checking, caching and threads management:
Goffi <goffi@goffi.org>
parents:
3729
diff
changeset
|
832 if len(extra_args) > 1: |
a8c7e5cef0cb
comp AP gateway: signature checking, caching and threads management:
Goffi <goffi@goffi.org>
parents:
3729
diff
changeset
|
833 log.warning(f"unexpected extra arguments: {extra_args!r}") |
a8c7e5cef0cb
comp AP gateway: signature checking, caching and threads management:
Goffi <goffi@goffi.org>
parents:
3729
diff
changeset
|
834 ap_account = extra_args[0] |
a8c7e5cef0cb
comp AP gateway: signature checking, caching and threads management:
Goffi <goffi@goffi.org>
parents:
3729
diff
changeset
|
835 account_jid, node = await self.apg.getJIDAndNode(ap_account) |
a8c7e5cef0cb
comp AP gateway: signature checking, caching and threads management:
Goffi <goffi@goffi.org>
parents:
3729
diff
changeset
|
836 if request_type not in AP_REQUEST_TYPES.get( |
a8c7e5cef0cb
comp AP gateway: signature checking, caching and threads management:
Goffi <goffi@goffi.org>
parents:
3729
diff
changeset
|
837 request.method.decode().upper(), [] |
a8c7e5cef0cb
comp AP gateway: signature checking, caching and threads management:
Goffi <goffi@goffi.org>
parents:
3729
diff
changeset
|
838 ): |
a8c7e5cef0cb
comp AP gateway: signature checking, caching and threads management:
Goffi <goffi@goffi.org>
parents:
3729
diff
changeset
|
839 raise exceptions.DataError(f"Invalid request type: {request_type!r}") |
a8c7e5cef0cb
comp AP gateway: signature checking, caching and threads management:
Goffi <goffi@goffi.org>
parents:
3729
diff
changeset
|
840 method = getattr(self, f"AP{request_type.title()}Request") |
a8c7e5cef0cb
comp AP gateway: signature checking, caching and threads management:
Goffi <goffi@goffi.org>
parents:
3729
diff
changeset
|
841 ret_data = await method( |
a8c7e5cef0cb
comp AP gateway: signature checking, caching and threads management:
Goffi <goffi@goffi.org>
parents:
3729
diff
changeset
|
842 request, account_jid, node, ap_account, ap_url, signing_actor |
a8c7e5cef0cb
comp AP gateway: signature checking, caching and threads management:
Goffi <goffi@goffi.org>
parents:
3729
diff
changeset
|
843 ) |
a8c7e5cef0cb
comp AP gateway: signature checking, caching and threads management:
Goffi <goffi@goffi.org>
parents:
3729
diff
changeset
|
844 if ret_data is not None: |
a8c7e5cef0cb
comp AP gateway: signature checking, caching and threads management:
Goffi <goffi@goffi.org>
parents:
3729
diff
changeset
|
845 request.setHeader("content-type", CONTENT_TYPE_AP) |
a8c7e5cef0cb
comp AP gateway: signature checking, caching and threads management:
Goffi <goffi@goffi.org>
parents:
3729
diff
changeset
|
846 request.write(json.dumps(ret_data).encode()) |
3847
aaa4e7815ba8
component AP gateway: new `verbose` attribute in AP gateway to activate debug logs:
Goffi <goffi@goffi.org>
parents:
3846
diff
changeset
|
847 if self.apg.verbose>=2: |
aaa4e7815ba8
component AP gateway: new `verbose` attribute in AP gateway to activate debug logs:
Goffi <goffi@goffi.org>
parents:
3846
diff
changeset
|
848 from pprint import pformat |
aaa4e7815ba8
component AP gateway: new `verbose` attribute in AP gateway to activate debug logs:
Goffi <goffi@goffi.org>
parents:
3846
diff
changeset
|
849 to_log = [f"==> ret (code: {request.code}):\n{pformat(ret_data)}"] |
aaa4e7815ba8
component AP gateway: new `verbose` attribute in AP gateway to activate debug logs:
Goffi <goffi@goffi.org>
parents:
3846
diff
changeset
|
850 log.info( |
aaa4e7815ba8
component AP gateway: new `verbose` attribute in AP gateway to activate debug logs:
Goffi <goffi@goffi.org>
parents:
3846
diff
changeset
|
851 |
aaa4e7815ba8
component AP gateway: new `verbose` attribute in AP gateway to activate debug logs:
Goffi <goffi@goffi.org>
parents:
3846
diff
changeset
|
852 ) |
aaa4e7815ba8
component AP gateway: new `verbose` attribute in AP gateway to activate debug logs:
Goffi <goffi@goffi.org>
parents:
3846
diff
changeset
|
853 if self.apg.verbose>=3: |
aaa4e7815ba8
component AP gateway: new `verbose` attribute in AP gateway to activate debug logs:
Goffi <goffi@goffi.org>
parents:
3846
diff
changeset
|
854 headers = "\n".join( |
aaa4e7815ba8
component AP gateway: new `verbose` attribute in AP gateway to activate debug logs:
Goffi <goffi@goffi.org>
parents:
3846
diff
changeset
|
855 f" {k.decode()}: {v.decode()}" |
aaa4e7815ba8
component AP gateway: new `verbose` attribute in AP gateway to activate debug logs:
Goffi <goffi@goffi.org>
parents:
3846
diff
changeset
|
856 for k,v in request.getAllHeaders().items() |
aaa4e7815ba8
component AP gateway: new `verbose` attribute in AP gateway to activate debug logs:
Goffi <goffi@goffi.org>
parents:
3846
diff
changeset
|
857 ) |
aaa4e7815ba8
component AP gateway: new `verbose` attribute in AP gateway to activate debug logs:
Goffi <goffi@goffi.org>
parents:
3846
diff
changeset
|
858 to_log.append(" headers:\n{headers}") |
aaa4e7815ba8
component AP gateway: new `verbose` attribute in AP gateway to activate debug logs:
Goffi <goffi@goffi.org>
parents:
3846
diff
changeset
|
859 to_log.append("---") |
aaa4e7815ba8
component AP gateway: new `verbose` attribute in AP gateway to activate debug logs:
Goffi <goffi@goffi.org>
parents:
3846
diff
changeset
|
860 log.info("\n".join(to_log)) |
3728
b15644cae50d
component AP gateway: JID/node ⟺ AP outbox conversion:
Goffi <goffi@goffi.org>
parents:
3684
diff
changeset
|
861 request.finish() |
b15644cae50d
component AP gateway: JID/node ⟺ AP outbox conversion:
Goffi <goffi@goffi.org>
parents:
3684
diff
changeset
|
862 |
3745
a8c7e5cef0cb
comp AP gateway: signature checking, caching and threads management:
Goffi <goffi@goffi.org>
parents:
3729
diff
changeset
|
863 async def APPostRequest(self, request: "HTTPRequest"): |
a8c7e5cef0cb
comp AP gateway: signature checking, caching and threads management:
Goffi <goffi@goffi.org>
parents:
3729
diff
changeset
|
864 try: |
a8c7e5cef0cb
comp AP gateway: signature checking, caching and threads management:
Goffi <goffi@goffi.org>
parents:
3729
diff
changeset
|
865 signing_actor = await self.checkSignature(request) |
a8c7e5cef0cb
comp AP gateway: signature checking, caching and threads management:
Goffi <goffi@goffi.org>
parents:
3729
diff
changeset
|
866 except exceptions.EncryptionError as e: |
a8c7e5cef0cb
comp AP gateway: signature checking, caching and threads management:
Goffi <goffi@goffi.org>
parents:
3729
diff
changeset
|
867 self.responseCode( |
a8c7e5cef0cb
comp AP gateway: signature checking, caching and threads management:
Goffi <goffi@goffi.org>
parents:
3729
diff
changeset
|
868 request, |
a8c7e5cef0cb
comp AP gateway: signature checking, caching and threads management:
Goffi <goffi@goffi.org>
parents:
3729
diff
changeset
|
869 http.FORBIDDEN, |
a8c7e5cef0cb
comp AP gateway: signature checking, caching and threads management:
Goffi <goffi@goffi.org>
parents:
3729
diff
changeset
|
870 f"invalid signature: {e}" |
a8c7e5cef0cb
comp AP gateway: signature checking, caching and threads management:
Goffi <goffi@goffi.org>
parents:
3729
diff
changeset
|
871 ) |
a8c7e5cef0cb
comp AP gateway: signature checking, caching and threads management:
Goffi <goffi@goffi.org>
parents:
3729
diff
changeset
|
872 request.finish() |
a8c7e5cef0cb
comp AP gateway: signature checking, caching and threads management:
Goffi <goffi@goffi.org>
parents:
3729
diff
changeset
|
873 return |
a8c7e5cef0cb
comp AP gateway: signature checking, caching and threads management:
Goffi <goffi@goffi.org>
parents:
3729
diff
changeset
|
874 |
3844
65e5718e7710
component AP gateway: `Announce` activity implementation:
Goffi <goffi@goffi.org>
parents:
3833
diff
changeset
|
875 request.setResponseCode(http.ACCEPTED) |
65e5718e7710
component AP gateway: `Announce` activity implementation:
Goffi <goffi@goffi.org>
parents:
3833
diff
changeset
|
876 |
65e5718e7710
component AP gateway: `Announce` activity implementation:
Goffi <goffi@goffi.org>
parents:
3833
diff
changeset
|
877 digest = request.getHeader("digest") |
65e5718e7710
component AP gateway: `Announce` activity implementation:
Goffi <goffi@goffi.org>
parents:
3833
diff
changeset
|
878 if digest in self._seen_digest: |
65e5718e7710
component AP gateway: `Announce` activity implementation:
Goffi <goffi@goffi.org>
parents:
3833
diff
changeset
|
879 log.debug(f"Ignoring duplicated request (digest: {digest!r})") |
65e5718e7710
component AP gateway: `Announce` activity implementation:
Goffi <goffi@goffi.org>
parents:
3833
diff
changeset
|
880 request.finish() |
65e5718e7710
component AP gateway: `Announce` activity implementation:
Goffi <goffi@goffi.org>
parents:
3833
diff
changeset
|
881 return |
65e5718e7710
component AP gateway: `Announce` activity implementation:
Goffi <goffi@goffi.org>
parents:
3833
diff
changeset
|
882 self._seen_digest.append(digest) |
65e5718e7710
component AP gateway: `Announce` activity implementation:
Goffi <goffi@goffi.org>
parents:
3833
diff
changeset
|
883 |
3802
983df907d456
component AP gateway: set POST default response code to 202 (accepted)
Goffi <goffi@goffi.org>
parents:
3793
diff
changeset
|
884 # default response code, may be changed, e.g. in case of exception |
3764
125c7043b277
comp AP gateway: publish, (un)subscribe/(un)follow, public subscription/following/followers:
Goffi <goffi@goffi.org>
parents:
3745
diff
changeset
|
885 try: |
125c7043b277
comp AP gateway: publish, (un)subscribe/(un)follow, public subscription/following/followers:
Goffi <goffi@goffi.org>
parents:
3745
diff
changeset
|
886 return await self.APRequest(request, signing_actor) |
125c7043b277
comp AP gateway: publish, (un)subscribe/(un)follow, public subscription/following/followers:
Goffi <goffi@goffi.org>
parents:
3745
diff
changeset
|
887 except Exception as e: |
125c7043b277
comp AP gateway: publish, (un)subscribe/(un)follow, public subscription/following/followers:
Goffi <goffi@goffi.org>
parents:
3745
diff
changeset
|
888 self._onRequestError(failure.Failure(e), request) |
3745
a8c7e5cef0cb
comp AP gateway: signature checking, caching and threads management:
Goffi <goffi@goffi.org>
parents:
3729
diff
changeset
|
889 |
a8c7e5cef0cb
comp AP gateway: signature checking, caching and threads management:
Goffi <goffi@goffi.org>
parents:
3729
diff
changeset
|
890 async def checkSigningActor(self, data: dict, signing_actor: str) -> None: |
a8c7e5cef0cb
comp AP gateway: signature checking, caching and threads management:
Goffi <goffi@goffi.org>
parents:
3729
diff
changeset
|
891 """That that signing actor correspond to actor declared in data |
a8c7e5cef0cb
comp AP gateway: signature checking, caching and threads management:
Goffi <goffi@goffi.org>
parents:
3729
diff
changeset
|
892 |
a8c7e5cef0cb
comp AP gateway: signature checking, caching and threads management:
Goffi <goffi@goffi.org>
parents:
3729
diff
changeset
|
893 @param data: request payload |
a8c7e5cef0cb
comp AP gateway: signature checking, caching and threads management:
Goffi <goffi@goffi.org>
parents:
3729
diff
changeset
|
894 @param signing_actor: actor ID of the signing entity, as returned by |
a8c7e5cef0cb
comp AP gateway: signature checking, caching and threads management:
Goffi <goffi@goffi.org>
parents:
3729
diff
changeset
|
895 checkSignature |
a8c7e5cef0cb
comp AP gateway: signature checking, caching and threads management:
Goffi <goffi@goffi.org>
parents:
3729
diff
changeset
|
896 @raise exceptions.NotFound: no actor found in data |
a8c7e5cef0cb
comp AP gateway: signature checking, caching and threads management:
Goffi <goffi@goffi.org>
parents:
3729
diff
changeset
|
897 @raise exceptions.EncryptionError: signing actor doesn't match actor in data |
a8c7e5cef0cb
comp AP gateway: signature checking, caching and threads management:
Goffi <goffi@goffi.org>
parents:
3729
diff
changeset
|
898 """ |
a8c7e5cef0cb
comp AP gateway: signature checking, caching and threads management:
Goffi <goffi@goffi.org>
parents:
3729
diff
changeset
|
899 actor = await self.apg.apGetSenderActor(data) |
a8c7e5cef0cb
comp AP gateway: signature checking, caching and threads management:
Goffi <goffi@goffi.org>
parents:
3729
diff
changeset
|
900 |
a8c7e5cef0cb
comp AP gateway: signature checking, caching and threads management:
Goffi <goffi@goffi.org>
parents:
3729
diff
changeset
|
901 if signing_actor != actor: |
a8c7e5cef0cb
comp AP gateway: signature checking, caching and threads management:
Goffi <goffi@goffi.org>
parents:
3729
diff
changeset
|
902 raise exceptions.EncryptionError( |
a8c7e5cef0cb
comp AP gateway: signature checking, caching and threads management:
Goffi <goffi@goffi.org>
parents:
3729
diff
changeset
|
903 f"signing actor ({signing_actor}) doesn't match actor in data ({actor})" |
a8c7e5cef0cb
comp AP gateway: signature checking, caching and threads management:
Goffi <goffi@goffi.org>
parents:
3729
diff
changeset
|
904 ) |
a8c7e5cef0cb
comp AP gateway: signature checking, caching and threads management:
Goffi <goffi@goffi.org>
parents:
3729
diff
changeset
|
905 |
a8c7e5cef0cb
comp AP gateway: signature checking, caching and threads management:
Goffi <goffi@goffi.org>
parents:
3729
diff
changeset
|
906 async def checkSignature(self, request: "HTTPRequest") -> str: |
a8c7e5cef0cb
comp AP gateway: signature checking, caching and threads management:
Goffi <goffi@goffi.org>
parents:
3729
diff
changeset
|
907 """Check and validate HTTP signature |
a8c7e5cef0cb
comp AP gateway: signature checking, caching and threads management:
Goffi <goffi@goffi.org>
parents:
3729
diff
changeset
|
908 |
a8c7e5cef0cb
comp AP gateway: signature checking, caching and threads management:
Goffi <goffi@goffi.org>
parents:
3729
diff
changeset
|
909 @return: id of the signing actor |
a8c7e5cef0cb
comp AP gateway: signature checking, caching and threads management:
Goffi <goffi@goffi.org>
parents:
3729
diff
changeset
|
910 |
a8c7e5cef0cb
comp AP gateway: signature checking, caching and threads management:
Goffi <goffi@goffi.org>
parents:
3729
diff
changeset
|
911 @raise exceptions.EncryptionError: signature is not present or doesn't match |
a8c7e5cef0cb
comp AP gateway: signature checking, caching and threads management:
Goffi <goffi@goffi.org>
parents:
3729
diff
changeset
|
912 """ |
a8c7e5cef0cb
comp AP gateway: signature checking, caching and threads management:
Goffi <goffi@goffi.org>
parents:
3729
diff
changeset
|
913 signature = request.getHeader("Signature") |
a8c7e5cef0cb
comp AP gateway: signature checking, caching and threads management:
Goffi <goffi@goffi.org>
parents:
3729
diff
changeset
|
914 if signature is None: |
a8c7e5cef0cb
comp AP gateway: signature checking, caching and threads management:
Goffi <goffi@goffi.org>
parents:
3729
diff
changeset
|
915 raise exceptions.EncryptionError("No signature found") |
a8c7e5cef0cb
comp AP gateway: signature checking, caching and threads management:
Goffi <goffi@goffi.org>
parents:
3729
diff
changeset
|
916 sign_data = { |
a8c7e5cef0cb
comp AP gateway: signature checking, caching and threads management:
Goffi <goffi@goffi.org>
parents:
3729
diff
changeset
|
917 m["key"]: m["uq_value"] or m["quoted_value"][1:-1] |
a8c7e5cef0cb
comp AP gateway: signature checking, caching and threads management:
Goffi <goffi@goffi.org>
parents:
3729
diff
changeset
|
918 for m in RE_SIG_PARAM.finditer(signature) |
a8c7e5cef0cb
comp AP gateway: signature checking, caching and threads management:
Goffi <goffi@goffi.org>
parents:
3729
diff
changeset
|
919 } |
a8c7e5cef0cb
comp AP gateway: signature checking, caching and threads management:
Goffi <goffi@goffi.org>
parents:
3729
diff
changeset
|
920 try: |
a8c7e5cef0cb
comp AP gateway: signature checking, caching and threads management:
Goffi <goffi@goffi.org>
parents:
3729
diff
changeset
|
921 key_id = sign_data["keyId"] |
a8c7e5cef0cb
comp AP gateway: signature checking, caching and threads management:
Goffi <goffi@goffi.org>
parents:
3729
diff
changeset
|
922 except KeyError: |
a8c7e5cef0cb
comp AP gateway: signature checking, caching and threads management:
Goffi <goffi@goffi.org>
parents:
3729
diff
changeset
|
923 raise exceptions.EncryptionError('"keyId" is missing from signature') |
a8c7e5cef0cb
comp AP gateway: signature checking, caching and threads management:
Goffi <goffi@goffi.org>
parents:
3729
diff
changeset
|
924 algorithm = sign_data.get("algorithm", HS2019) |
a8c7e5cef0cb
comp AP gateway: signature checking, caching and threads management:
Goffi <goffi@goffi.org>
parents:
3729
diff
changeset
|
925 signed_headers = sign_data.get( |
a8c7e5cef0cb
comp AP gateway: signature checking, caching and threads management:
Goffi <goffi@goffi.org>
parents:
3729
diff
changeset
|
926 "headers", |
a8c7e5cef0cb
comp AP gateway: signature checking, caching and threads management:
Goffi <goffi@goffi.org>
parents:
3729
diff
changeset
|
927 "(created)" if algorithm==HS2019 else "date" |
a8c7e5cef0cb
comp AP gateway: signature checking, caching and threads management:
Goffi <goffi@goffi.org>
parents:
3729
diff
changeset
|
928 ).lower().split() |
a8c7e5cef0cb
comp AP gateway: signature checking, caching and threads management:
Goffi <goffi@goffi.org>
parents:
3729
diff
changeset
|
929 try: |
a8c7e5cef0cb
comp AP gateway: signature checking, caching and threads management:
Goffi <goffi@goffi.org>
parents:
3729
diff
changeset
|
930 headers_to_check = SIGN_HEADERS[None] + SIGN_HEADERS[request.method] |
a8c7e5cef0cb
comp AP gateway: signature checking, caching and threads management:
Goffi <goffi@goffi.org>
parents:
3729
diff
changeset
|
931 except KeyError: |
a8c7e5cef0cb
comp AP gateway: signature checking, caching and threads management:
Goffi <goffi@goffi.org>
parents:
3729
diff
changeset
|
932 raise exceptions.InternalError( |
a8c7e5cef0cb
comp AP gateway: signature checking, caching and threads management:
Goffi <goffi@goffi.org>
parents:
3729
diff
changeset
|
933 f"there should be a list of headers for {request.method} method" |
a8c7e5cef0cb
comp AP gateway: signature checking, caching and threads management:
Goffi <goffi@goffi.org>
parents:
3729
diff
changeset
|
934 ) |
a8c7e5cef0cb
comp AP gateway: signature checking, caching and threads management:
Goffi <goffi@goffi.org>
parents:
3729
diff
changeset
|
935 if not headers_to_check: |
a8c7e5cef0cb
comp AP gateway: signature checking, caching and threads management:
Goffi <goffi@goffi.org>
parents:
3729
diff
changeset
|
936 raise exceptions.InternalError("headers_to_check must not be empty") |
a8c7e5cef0cb
comp AP gateway: signature checking, caching and threads management:
Goffi <goffi@goffi.org>
parents:
3729
diff
changeset
|
937 |
a8c7e5cef0cb
comp AP gateway: signature checking, caching and threads management:
Goffi <goffi@goffi.org>
parents:
3729
diff
changeset
|
938 for header in headers_to_check: |
a8c7e5cef0cb
comp AP gateway: signature checking, caching and threads management:
Goffi <goffi@goffi.org>
parents:
3729
diff
changeset
|
939 if isinstance(header, tuple): |
a8c7e5cef0cb
comp AP gateway: signature checking, caching and threads management:
Goffi <goffi@goffi.org>
parents:
3729
diff
changeset
|
940 if len(set(header).intersection(signed_headers)) == 0: |
a8c7e5cef0cb
comp AP gateway: signature checking, caching and threads management:
Goffi <goffi@goffi.org>
parents:
3729
diff
changeset
|
941 raise exceptions.EncryptionError( |
a8c7e5cef0cb
comp AP gateway: signature checking, caching and threads management:
Goffi <goffi@goffi.org>
parents:
3729
diff
changeset
|
942 f"at least one of following header must be signed: {header}" |
a8c7e5cef0cb
comp AP gateway: signature checking, caching and threads management:
Goffi <goffi@goffi.org>
parents:
3729
diff
changeset
|
943 ) |
a8c7e5cef0cb
comp AP gateway: signature checking, caching and threads management:
Goffi <goffi@goffi.org>
parents:
3729
diff
changeset
|
944 elif header not in signed_headers: |
a8c7e5cef0cb
comp AP gateway: signature checking, caching and threads management:
Goffi <goffi@goffi.org>
parents:
3729
diff
changeset
|
945 raise exceptions.EncryptionError( |
a8c7e5cef0cb
comp AP gateway: signature checking, caching and threads management:
Goffi <goffi@goffi.org>
parents:
3729
diff
changeset
|
946 f"the {header!r} header must be signed" |
a8c7e5cef0cb
comp AP gateway: signature checking, caching and threads management:
Goffi <goffi@goffi.org>
parents:
3729
diff
changeset
|
947 ) |
a8c7e5cef0cb
comp AP gateway: signature checking, caching and threads management:
Goffi <goffi@goffi.org>
parents:
3729
diff
changeset
|
948 |
a8c7e5cef0cb
comp AP gateway: signature checking, caching and threads management:
Goffi <goffi@goffi.org>
parents:
3729
diff
changeset
|
949 body = request.content.read() |
a8c7e5cef0cb
comp AP gateway: signature checking, caching and threads management:
Goffi <goffi@goffi.org>
parents:
3729
diff
changeset
|
950 request.content.seek(0) |
a8c7e5cef0cb
comp AP gateway: signature checking, caching and threads management:
Goffi <goffi@goffi.org>
parents:
3729
diff
changeset
|
951 headers = {} |
a8c7e5cef0cb
comp AP gateway: signature checking, caching and threads management:
Goffi <goffi@goffi.org>
parents:
3729
diff
changeset
|
952 for to_sign in signed_headers: |
a8c7e5cef0cb
comp AP gateway: signature checking, caching and threads management:
Goffi <goffi@goffi.org>
parents:
3729
diff
changeset
|
953 if to_sign == "(request-target)": |
a8c7e5cef0cb
comp AP gateway: signature checking, caching and threads management:
Goffi <goffi@goffi.org>
parents:
3729
diff
changeset
|
954 method = request.method.decode().lower() |
a8c7e5cef0cb
comp AP gateway: signature checking, caching and threads management:
Goffi <goffi@goffi.org>
parents:
3729
diff
changeset
|
955 uri = parse.unquote(request.uri.decode()) |
a8c7e5cef0cb
comp AP gateway: signature checking, caching and threads management:
Goffi <goffi@goffi.org>
parents:
3729
diff
changeset
|
956 headers[to_sign] = f"{method} /{uri.lstrip('/')}" |
a8c7e5cef0cb
comp AP gateway: signature checking, caching and threads management:
Goffi <goffi@goffi.org>
parents:
3729
diff
changeset
|
957 elif to_sign in ("(created)", "(expires)"): |
a8c7e5cef0cb
comp AP gateway: signature checking, caching and threads management:
Goffi <goffi@goffi.org>
parents:
3729
diff
changeset
|
958 if algorithm != HS2019: |
a8c7e5cef0cb
comp AP gateway: signature checking, caching and threads management:
Goffi <goffi@goffi.org>
parents:
3729
diff
changeset
|
959 raise exceptions.EncryptionError( |
a8c7e5cef0cb
comp AP gateway: signature checking, caching and threads management:
Goffi <goffi@goffi.org>
parents:
3729
diff
changeset
|
960 f"{to_sign!r} pseudo-header can only be used with {HS2019} " |
a8c7e5cef0cb
comp AP gateway: signature checking, caching and threads management:
Goffi <goffi@goffi.org>
parents:
3729
diff
changeset
|
961 "algorithm" |
a8c7e5cef0cb
comp AP gateway: signature checking, caching and threads management:
Goffi <goffi@goffi.org>
parents:
3729
diff
changeset
|
962 ) |
a8c7e5cef0cb
comp AP gateway: signature checking, caching and threads management:
Goffi <goffi@goffi.org>
parents:
3729
diff
changeset
|
963 key = to_sign[1:-1] |
a8c7e5cef0cb
comp AP gateway: signature checking, caching and threads management:
Goffi <goffi@goffi.org>
parents:
3729
diff
changeset
|
964 value = sign_data.get(key) |
a8c7e5cef0cb
comp AP gateway: signature checking, caching and threads management:
Goffi <goffi@goffi.org>
parents:
3729
diff
changeset
|
965 if not value: |
a8c7e5cef0cb
comp AP gateway: signature checking, caching and threads management:
Goffi <goffi@goffi.org>
parents:
3729
diff
changeset
|
966 raise exceptions.EncryptionError( |
a8c7e5cef0cb
comp AP gateway: signature checking, caching and threads management:
Goffi <goffi@goffi.org>
parents:
3729
diff
changeset
|
967 "{key!r} parameter is missing from signature" |
a8c7e5cef0cb
comp AP gateway: signature checking, caching and threads management:
Goffi <goffi@goffi.org>
parents:
3729
diff
changeset
|
968 ) |
a8c7e5cef0cb
comp AP gateway: signature checking, caching and threads management:
Goffi <goffi@goffi.org>
parents:
3729
diff
changeset
|
969 try: |
a8c7e5cef0cb
comp AP gateway: signature checking, caching and threads management:
Goffi <goffi@goffi.org>
parents:
3729
diff
changeset
|
970 if float(value) < 0: |
a8c7e5cef0cb
comp AP gateway: signature checking, caching and threads management:
Goffi <goffi@goffi.org>
parents:
3729
diff
changeset
|
971 raise ValueError |
a8c7e5cef0cb
comp AP gateway: signature checking, caching and threads management:
Goffi <goffi@goffi.org>
parents:
3729
diff
changeset
|
972 except ValueError: |
a8c7e5cef0cb
comp AP gateway: signature checking, caching and threads management:
Goffi <goffi@goffi.org>
parents:
3729
diff
changeset
|
973 raise exceptions.EncryptionError( |
a8c7e5cef0cb
comp AP gateway: signature checking, caching and threads management:
Goffi <goffi@goffi.org>
parents:
3729
diff
changeset
|
974 f"{to_sign} must be a Unix timestamp" |
a8c7e5cef0cb
comp AP gateway: signature checking, caching and threads management:
Goffi <goffi@goffi.org>
parents:
3729
diff
changeset
|
975 ) |
a8c7e5cef0cb
comp AP gateway: signature checking, caching and threads management:
Goffi <goffi@goffi.org>
parents:
3729
diff
changeset
|
976 headers[to_sign] = value |
a8c7e5cef0cb
comp AP gateway: signature checking, caching and threads management:
Goffi <goffi@goffi.org>
parents:
3729
diff
changeset
|
977 else: |
a8c7e5cef0cb
comp AP gateway: signature checking, caching and threads management:
Goffi <goffi@goffi.org>
parents:
3729
diff
changeset
|
978 value = request.getHeader(to_sign) |
a8c7e5cef0cb
comp AP gateway: signature checking, caching and threads management:
Goffi <goffi@goffi.org>
parents:
3729
diff
changeset
|
979 if not value: |
a8c7e5cef0cb
comp AP gateway: signature checking, caching and threads management:
Goffi <goffi@goffi.org>
parents:
3729
diff
changeset
|
980 raise exceptions.EncryptionError( |
a8c7e5cef0cb
comp AP gateway: signature checking, caching and threads management:
Goffi <goffi@goffi.org>
parents:
3729
diff
changeset
|
981 f"value of header {to_sign!r} is missing!" |
a8c7e5cef0cb
comp AP gateway: signature checking, caching and threads management:
Goffi <goffi@goffi.org>
parents:
3729
diff
changeset
|
982 ) |
a8c7e5cef0cb
comp AP gateway: signature checking, caching and threads management:
Goffi <goffi@goffi.org>
parents:
3729
diff
changeset
|
983 elif to_sign == "host": |
a8c7e5cef0cb
comp AP gateway: signature checking, caching and threads management:
Goffi <goffi@goffi.org>
parents:
3729
diff
changeset
|
984 # we check Forwarded/X-Forwarded-Host headers |
a8c7e5cef0cb
comp AP gateway: signature checking, caching and threads management:
Goffi <goffi@goffi.org>
parents:
3729
diff
changeset
|
985 # as we need original host if a proxy has modified the header |
a8c7e5cef0cb
comp AP gateway: signature checking, caching and threads management:
Goffi <goffi@goffi.org>
parents:
3729
diff
changeset
|
986 forwarded = request.getHeader("forwarded") |
a8c7e5cef0cb
comp AP gateway: signature checking, caching and threads management:
Goffi <goffi@goffi.org>
parents:
3729
diff
changeset
|
987 if forwarded is not None: |
a8c7e5cef0cb
comp AP gateway: signature checking, caching and threads management:
Goffi <goffi@goffi.org>
parents:
3729
diff
changeset
|
988 try: |
a8c7e5cef0cb
comp AP gateway: signature checking, caching and threads management:
Goffi <goffi@goffi.org>
parents:
3729
diff
changeset
|
989 host = [ |
a8c7e5cef0cb
comp AP gateway: signature checking, caching and threads management:
Goffi <goffi@goffi.org>
parents:
3729
diff
changeset
|
990 f[5:] for f in forwarded.split(";") |
a8c7e5cef0cb
comp AP gateway: signature checking, caching and threads management:
Goffi <goffi@goffi.org>
parents:
3729
diff
changeset
|
991 if f.startswith("host=") |
a8c7e5cef0cb
comp AP gateway: signature checking, caching and threads management:
Goffi <goffi@goffi.org>
parents:
3729
diff
changeset
|
992 ][0] or None |
a8c7e5cef0cb
comp AP gateway: signature checking, caching and threads management:
Goffi <goffi@goffi.org>
parents:
3729
diff
changeset
|
993 except IndexError: |
a8c7e5cef0cb
comp AP gateway: signature checking, caching and threads management:
Goffi <goffi@goffi.org>
parents:
3729
diff
changeset
|
994 host = None |
a8c7e5cef0cb
comp AP gateway: signature checking, caching and threads management:
Goffi <goffi@goffi.org>
parents:
3729
diff
changeset
|
995 else: |
a8c7e5cef0cb
comp AP gateway: signature checking, caching and threads management:
Goffi <goffi@goffi.org>
parents:
3729
diff
changeset
|
996 host = None |
a8c7e5cef0cb
comp AP gateway: signature checking, caching and threads management:
Goffi <goffi@goffi.org>
parents:
3729
diff
changeset
|
997 if host is None: |
a8c7e5cef0cb
comp AP gateway: signature checking, caching and threads management:
Goffi <goffi@goffi.org>
parents:
3729
diff
changeset
|
998 host = request.getHeader("x-forwarded-host") |
a8c7e5cef0cb
comp AP gateway: signature checking, caching and threads management:
Goffi <goffi@goffi.org>
parents:
3729
diff
changeset
|
999 if host: |
a8c7e5cef0cb
comp AP gateway: signature checking, caching and threads management:
Goffi <goffi@goffi.org>
parents:
3729
diff
changeset
|
1000 value = host |
a8c7e5cef0cb
comp AP gateway: signature checking, caching and threads management:
Goffi <goffi@goffi.org>
parents:
3729
diff
changeset
|
1001 elif to_sign == "digest": |
a8c7e5cef0cb
comp AP gateway: signature checking, caching and threads management:
Goffi <goffi@goffi.org>
parents:
3729
diff
changeset
|
1002 hashes = { |
a8c7e5cef0cb
comp AP gateway: signature checking, caching and threads management:
Goffi <goffi@goffi.org>
parents:
3729
diff
changeset
|
1003 algo.lower(): hash_ for algo, hash_ in ( |
a8c7e5cef0cb
comp AP gateway: signature checking, caching and threads management:
Goffi <goffi@goffi.org>
parents:
3729
diff
changeset
|
1004 digest.split("=", 1) for digest in value.split(",") |
a8c7e5cef0cb
comp AP gateway: signature checking, caching and threads management:
Goffi <goffi@goffi.org>
parents:
3729
diff
changeset
|
1005 ) |
a8c7e5cef0cb
comp AP gateway: signature checking, caching and threads management:
Goffi <goffi@goffi.org>
parents:
3729
diff
changeset
|
1006 } |
a8c7e5cef0cb
comp AP gateway: signature checking, caching and threads management:
Goffi <goffi@goffi.org>
parents:
3729
diff
changeset
|
1007 try: |
a8c7e5cef0cb
comp AP gateway: signature checking, caching and threads management:
Goffi <goffi@goffi.org>
parents:
3729
diff
changeset
|
1008 given_digest = hashes["sha-256"] |
a8c7e5cef0cb
comp AP gateway: signature checking, caching and threads management:
Goffi <goffi@goffi.org>
parents:
3729
diff
changeset
|
1009 except KeyError: |
a8c7e5cef0cb
comp AP gateway: signature checking, caching and threads management:
Goffi <goffi@goffi.org>
parents:
3729
diff
changeset
|
1010 raise exceptions.EncryptionError( |
a8c7e5cef0cb
comp AP gateway: signature checking, caching and threads management:
Goffi <goffi@goffi.org>
parents:
3729
diff
changeset
|
1011 "Only SHA-256 algorithm is currently supported for digest" |
a8c7e5cef0cb
comp AP gateway: signature checking, caching and threads management:
Goffi <goffi@goffi.org>
parents:
3729
diff
changeset
|
1012 ) |
a8c7e5cef0cb
comp AP gateway: signature checking, caching and threads management:
Goffi <goffi@goffi.org>
parents:
3729
diff
changeset
|
1013 __, computed_digest = self.apg.getDigest(body) |
a8c7e5cef0cb
comp AP gateway: signature checking, caching and threads management:
Goffi <goffi@goffi.org>
parents:
3729
diff
changeset
|
1014 if given_digest != computed_digest: |
a8c7e5cef0cb
comp AP gateway: signature checking, caching and threads management:
Goffi <goffi@goffi.org>
parents:
3729
diff
changeset
|
1015 raise exceptions.EncryptionError( |
a8c7e5cef0cb
comp AP gateway: signature checking, caching and threads management:
Goffi <goffi@goffi.org>
parents:
3729
diff
changeset
|
1016 f"SHA-256 given and computed digest differ:\n" |
a8c7e5cef0cb
comp AP gateway: signature checking, caching and threads management:
Goffi <goffi@goffi.org>
parents:
3729
diff
changeset
|
1017 f"given: {given_digest!r}\ncomputed: {computed_digest!r}" |
a8c7e5cef0cb
comp AP gateway: signature checking, caching and threads management:
Goffi <goffi@goffi.org>
parents:
3729
diff
changeset
|
1018 ) |
a8c7e5cef0cb
comp AP gateway: signature checking, caching and threads management:
Goffi <goffi@goffi.org>
parents:
3729
diff
changeset
|
1019 headers[to_sign] = value |
a8c7e5cef0cb
comp AP gateway: signature checking, caching and threads management:
Goffi <goffi@goffi.org>
parents:
3729
diff
changeset
|
1020 |
a8c7e5cef0cb
comp AP gateway: signature checking, caching and threads management:
Goffi <goffi@goffi.org>
parents:
3729
diff
changeset
|
1021 # date check |
a8c7e5cef0cb
comp AP gateway: signature checking, caching and threads management:
Goffi <goffi@goffi.org>
parents:
3729
diff
changeset
|
1022 limit_ts = time.time() + SIGN_EXP |
a8c7e5cef0cb
comp AP gateway: signature checking, caching and threads management:
Goffi <goffi@goffi.org>
parents:
3729
diff
changeset
|
1023 if "(created)" in headers: |
a8c7e5cef0cb
comp AP gateway: signature checking, caching and threads management:
Goffi <goffi@goffi.org>
parents:
3729
diff
changeset
|
1024 created = float(headers["created"]) |
a8c7e5cef0cb
comp AP gateway: signature checking, caching and threads management:
Goffi <goffi@goffi.org>
parents:
3729
diff
changeset
|
1025 else: |
a8c7e5cef0cb
comp AP gateway: signature checking, caching and threads management:
Goffi <goffi@goffi.org>
parents:
3729
diff
changeset
|
1026 created = date_utils.date_parse(headers["date"]) |
a8c7e5cef0cb
comp AP gateway: signature checking, caching and threads management:
Goffi <goffi@goffi.org>
parents:
3729
diff
changeset
|
1027 |
a8c7e5cef0cb
comp AP gateway: signature checking, caching and threads management:
Goffi <goffi@goffi.org>
parents:
3729
diff
changeset
|
1028 |
a8c7e5cef0cb
comp AP gateway: signature checking, caching and threads management:
Goffi <goffi@goffi.org>
parents:
3729
diff
changeset
|
1029 try: |
a8c7e5cef0cb
comp AP gateway: signature checking, caching and threads management:
Goffi <goffi@goffi.org>
parents:
3729
diff
changeset
|
1030 expires = float(headers["expires"]) |
a8c7e5cef0cb
comp AP gateway: signature checking, caching and threads management:
Goffi <goffi@goffi.org>
parents:
3729
diff
changeset
|
1031 except KeyError: |
a8c7e5cef0cb
comp AP gateway: signature checking, caching and threads management:
Goffi <goffi@goffi.org>
parents:
3729
diff
changeset
|
1032 pass |
a8c7e5cef0cb
comp AP gateway: signature checking, caching and threads management:
Goffi <goffi@goffi.org>
parents:
3729
diff
changeset
|
1033 else: |
a8c7e5cef0cb
comp AP gateway: signature checking, caching and threads management:
Goffi <goffi@goffi.org>
parents:
3729
diff
changeset
|
1034 if expires < created: |
a8c7e5cef0cb
comp AP gateway: signature checking, caching and threads management:
Goffi <goffi@goffi.org>
parents:
3729
diff
changeset
|
1035 log.warning( |
a8c7e5cef0cb
comp AP gateway: signature checking, caching and threads management:
Goffi <goffi@goffi.org>
parents:
3729
diff
changeset
|
1036 f"(expires) [{expires}] set in the past of (created) [{created}] " |
a8c7e5cef0cb
comp AP gateway: signature checking, caching and threads management:
Goffi <goffi@goffi.org>
parents:
3729
diff
changeset
|
1037 "ignoring it according to specs" |
a8c7e5cef0cb
comp AP gateway: signature checking, caching and threads management:
Goffi <goffi@goffi.org>
parents:
3729
diff
changeset
|
1038 ) |
a8c7e5cef0cb
comp AP gateway: signature checking, caching and threads management:
Goffi <goffi@goffi.org>
parents:
3729
diff
changeset
|
1039 else: |
a8c7e5cef0cb
comp AP gateway: signature checking, caching and threads management:
Goffi <goffi@goffi.org>
parents:
3729
diff
changeset
|
1040 limit_ts = min(limit_ts, expires) |
a8c7e5cef0cb
comp AP gateway: signature checking, caching and threads management:
Goffi <goffi@goffi.org>
parents:
3729
diff
changeset
|
1041 |
a8c7e5cef0cb
comp AP gateway: signature checking, caching and threads management:
Goffi <goffi@goffi.org>
parents:
3729
diff
changeset
|
1042 if created > limit_ts: |
a8c7e5cef0cb
comp AP gateway: signature checking, caching and threads management:
Goffi <goffi@goffi.org>
parents:
3729
diff
changeset
|
1043 raise exceptions.EncryptionError("Signature has expired") |
a8c7e5cef0cb
comp AP gateway: signature checking, caching and threads management:
Goffi <goffi@goffi.org>
parents:
3729
diff
changeset
|
1044 |
a8c7e5cef0cb
comp AP gateway: signature checking, caching and threads management:
Goffi <goffi@goffi.org>
parents:
3729
diff
changeset
|
1045 return await self.apg.checkSignature( |
a8c7e5cef0cb
comp AP gateway: signature checking, caching and threads management:
Goffi <goffi@goffi.org>
parents:
3729
diff
changeset
|
1046 sign_data["signature"], |
a8c7e5cef0cb
comp AP gateway: signature checking, caching and threads management:
Goffi <goffi@goffi.org>
parents:
3729
diff
changeset
|
1047 key_id, |
a8c7e5cef0cb
comp AP gateway: signature checking, caching and threads management:
Goffi <goffi@goffi.org>
parents:
3729
diff
changeset
|
1048 headers |
a8c7e5cef0cb
comp AP gateway: signature checking, caching and threads management:
Goffi <goffi@goffi.org>
parents:
3729
diff
changeset
|
1049 ) |
a8c7e5cef0cb
comp AP gateway: signature checking, caching and threads management:
Goffi <goffi@goffi.org>
parents:
3729
diff
changeset
|
1050 |
3728
b15644cae50d
component AP gateway: JID/node ⟺ AP outbox conversion:
Goffi <goffi@goffi.org>
parents:
3684
diff
changeset
|
1051 def render(self, request): |
b15644cae50d
component AP gateway: JID/node ⟺ AP outbox conversion:
Goffi <goffi@goffi.org>
parents:
3684
diff
changeset
|
1052 request.setHeader("server", VERSION) |
b15644cae50d
component AP gateway: JID/node ⟺ AP outbox conversion:
Goffi <goffi@goffi.org>
parents:
3684
diff
changeset
|
1053 return super().render(request) |
3682
7c990aaa49d3
comp AP Gateway: ActivityPub Component first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
1054 |
7c990aaa49d3
comp AP Gateway: ActivityPub Component first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
1055 def render_GET(self, request): |
7c990aaa49d3
comp AP Gateway: ActivityPub Component first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
1056 path = request.path.decode().lstrip("/") |
7c990aaa49d3
comp AP Gateway: ActivityPub Component first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
1057 if path.startswith(".well-known/webfinger"): |
3728
b15644cae50d
component AP gateway: JID/node ⟺ AP outbox conversion:
Goffi <goffi@goffi.org>
parents:
3684
diff
changeset
|
1058 defer.ensureDeferred(self.webfinger(request)) |
b15644cae50d
component AP gateway: JID/node ⟺ AP outbox conversion:
Goffi <goffi@goffi.org>
parents:
3684
diff
changeset
|
1059 return server.NOT_DONE_YET |
3682
7c990aaa49d3
comp AP Gateway: ActivityPub Component first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
1060 elif path.startswith(self.apg.ap_path): |
3764
125c7043b277
comp AP gateway: publish, (un)subscribe/(un)follow, public subscription/following/followers:
Goffi <goffi@goffi.org>
parents:
3745
diff
changeset
|
1061 d = defer.ensureDeferred(self.APRequest(request)) |
125c7043b277
comp AP gateway: publish, (un)subscribe/(un)follow, public subscription/following/followers:
Goffi <goffi@goffi.org>
parents:
3745
diff
changeset
|
1062 d.addErrback(self._onRequestError, request) |
3728
b15644cae50d
component AP gateway: JID/node ⟺ AP outbox conversion:
Goffi <goffi@goffi.org>
parents:
3684
diff
changeset
|
1063 return server.NOT_DONE_YET |
b15644cae50d
component AP gateway: JID/node ⟺ AP outbox conversion:
Goffi <goffi@goffi.org>
parents:
3684
diff
changeset
|
1064 |
3682
7c990aaa49d3
comp AP Gateway: ActivityPub Component first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
1065 return web_resource.NoResource().render(request) |
7c990aaa49d3
comp AP Gateway: ActivityPub Component first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
1066 |
3745
a8c7e5cef0cb
comp AP gateway: signature checking, caching and threads management:
Goffi <goffi@goffi.org>
parents:
3729
diff
changeset
|
1067 def render_POST(self, request): |
a8c7e5cef0cb
comp AP gateway: signature checking, caching and threads management:
Goffi <goffi@goffi.org>
parents:
3729
diff
changeset
|
1068 path = request.path.decode().lstrip("/") |
a8c7e5cef0cb
comp AP gateway: signature checking, caching and threads management:
Goffi <goffi@goffi.org>
parents:
3729
diff
changeset
|
1069 if not path.startswith(self.apg.ap_path): |
a8c7e5cef0cb
comp AP gateway: signature checking, caching and threads management:
Goffi <goffi@goffi.org>
parents:
3729
diff
changeset
|
1070 return web_resource.NoResource().render(request) |
a8c7e5cef0cb
comp AP gateway: signature checking, caching and threads management:
Goffi <goffi@goffi.org>
parents:
3729
diff
changeset
|
1071 defer.ensureDeferred(self.APPostRequest(request)) |
a8c7e5cef0cb
comp AP gateway: signature checking, caching and threads management:
Goffi <goffi@goffi.org>
parents:
3729
diff
changeset
|
1072 return server.NOT_DONE_YET |
a8c7e5cef0cb
comp AP gateway: signature checking, caching and threads management:
Goffi <goffi@goffi.org>
parents:
3729
diff
changeset
|
1073 |
3682
7c990aaa49d3
comp AP Gateway: ActivityPub Component first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
1074 |
7c990aaa49d3
comp AP Gateway: ActivityPub Component first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
1075 class HTTPRequest(server.Request): |
7c990aaa49d3
comp AP Gateway: ActivityPub Component first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
1076 pass |
7c990aaa49d3
comp AP Gateway: ActivityPub Component first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
1077 |
7c990aaa49d3
comp AP Gateway: ActivityPub Component first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
1078 |
7c990aaa49d3
comp AP Gateway: ActivityPub Component first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
1079 class HTTPServer(server.Site): |
7c990aaa49d3
comp AP Gateway: ActivityPub Component first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
1080 requestFactory = HTTPRequest |
7c990aaa49d3
comp AP Gateway: ActivityPub Component first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
1081 |
7c990aaa49d3
comp AP Gateway: ActivityPub Component first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
1082 def __init__(self, ap_gateway): |
7c990aaa49d3
comp AP Gateway: ActivityPub Component first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
1083 super().__init__(HTTPAPGServer(ap_gateway)) |