comparison sat/plugins/plugin_comp_ap_gateway/http_server.py @ 3833:381340b9a9ee

component AP gateway: convert XMPP mentions to AP: When a XEP-0372 mention is received, the linked pubsub item is looked after in cache, and if found, it is send to mentioned entity with `mention` tag added. However, this doesn't work in some cases (see incoming doc for details). To work around that, `@user@server.tld` type mention are also scanned in body, and mentions are added when found (this can be disabled with `auto_mentions` setting). Mention are only scanned in "public" messages, i.e. for pubsub items, and not direct messages. rel 369
author Goffi <goffi@goffi.org>
date Sun, 10 Jul 2022 16:15:06 +0200
parents 81c79b7cafa7
children 65e5718e7710
comparison
equal deleted inserted replaced
3832:201a22bfbb74 3833:381340b9a9ee
21 from typing import Optional, Dict, List, Any 21 from typing import Optional, Dict, List, Any
22 import json 22 import json
23 from urllib import parse 23 from urllib import parse
24 from collections import deque 24 from collections import deque
25 import unicodedata 25 import unicodedata
26 from pathlib import Path
27 from pprint import pformat
28 26
29 from twisted.web import http, resource as web_resource, server 27 from twisted.web import http, resource as web_resource, server
30 from twisted.web import static 28 from twisted.web import static
31 from twisted.python import failure 29 from twisted.python import failure
32 from twisted.internet import reactor, defer 30 from twisted.internet import defer
33 from twisted.words.protocols.jabber import jid, error 31 from twisted.words.protocols.jabber import jid, error
34 from wokkel import pubsub, rsm 32 from wokkel import pubsub, rsm
35 33
36 from sat.core import exceptions 34 from sat.core import exceptions
37 from sat.core.constants import Const as C 35 from sat.core.constants import Const as C
38 from sat.core.i18n import _ 36 from sat.core.i18n import _
39 from sat.core.log import getLogger 37 from sat.core.log import getLogger
40 from sat.tools import utils
41 from sat.tools.common import date_utils 38 from sat.tools.common import date_utils
42 from sat.memory.sqla_mapping import SubscriptionState 39 from sat.memory.sqla_mapping import SubscriptionState
43 40
44 from .constants import ( 41 from .constants import (
45 CONTENT_TYPE_AP, TYPE_ACTOR, TYPE_INBOX, TYPE_SHARED_INBOX, TYPE_OUTBOX, 42 CONTENT_TYPE_AP, TYPE_ACTOR, TYPE_INBOX, TYPE_SHARED_INBOX, TYPE_OUTBOX,
71 request: "HTTPRequest", 68 request: "HTTPRequest",
72 http_code: int, 69 http_code: int,
73 msg: Optional[str] = None 70 msg: Optional[str] = None
74 ) -> None: 71 ) -> None:
75 """Log and set HTTP return code and associated message""" 72 """Log and set HTTP return code and associated message"""
76 log.warning(msg) 73 if msg is not None:
74 log.warning(msg)
77 request.setResponseCode(http_code, None if msg is None else msg.encode()) 75 request.setResponseCode(http_code, None if msg is None else msg.encode())
78 76
79 def _onRequestError(self, failure_: failure.Failure, request: "HTTPRequest") -> None: 77 def _onRequestError(self, failure_: failure.Failure, request: "HTTPRequest") -> None:
80 log.error(f"Internal error: {failure_.value}") 78 log.error(f"Internal error: {failure_.value}")
81 self.responseCode( 79 self.responseCode(
681 ) 679 )
682 request.finish() 680 request.finish()
683 return 681 return
684 682
685 # default response code, may be changed, e.g. in case of exception 683 # default response code, may be changed, e.g. in case of exception
686 self.responseCode(request, http.ACCEPTED) 684 request.setResponseCode(http.ACCEPTED)
687 try: 685 try:
688 return await self.APRequest(request, signing_actor) 686 return await self.APRequest(request, signing_actor)
689 except Exception as e: 687 except Exception as e:
690 self._onRequestError(failure.Failure(e), request) 688 self._onRequestError(failure.Failure(e), request)
691 689