2019-09-15 10:51:51 +00:00
|
|
|
import email.utils, urllib.parse
|
2019-09-15 09:43:46 +00:00
|
|
|
from src import utils
|
2019-09-15 10:53:01 +00:00
|
|
|
from . import ap_security, ap_utils
|
2019-09-15 09:43:46 +00:00
|
|
|
|
|
|
|
class Actor(object):
|
|
|
|
def __init__(self, url):
|
|
|
|
self.url = url
|
|
|
|
|
|
|
|
self.username = None
|
2019-12-10 11:45:29 +00:00
|
|
|
self.display_name = None
|
2019-09-15 09:43:46 +00:00
|
|
|
self.inbox = None
|
|
|
|
self.outbox = None
|
2019-10-15 14:24:57 +00:00
|
|
|
self.followers = None
|
2019-09-15 09:43:46 +00:00
|
|
|
|
|
|
|
def load(self):
|
2019-09-17 16:41:15 +00:00
|
|
|
response = ap_utils.activity_request(self.url)
|
|
|
|
if response.code == 200:
|
2019-11-25 18:17:30 +00:00
|
|
|
response = response.json()
|
|
|
|
self.username = response["preferredUsername"]
|
2019-12-10 11:45:29 +00:00
|
|
|
self.display_name = response.get("name", self.username)
|
2019-11-25 18:17:30 +00:00
|
|
|
self.inbox = Inbox(response["inbox"])
|
|
|
|
self.outbox = Outbox(response["outbox"])
|
|
|
|
self.followers = response["followers"]
|
2019-09-17 16:41:15 +00:00
|
|
|
return True
|
|
|
|
return False
|
2019-09-15 09:43:46 +00:00
|
|
|
|
|
|
|
class Outbox(object):
|
|
|
|
def __init__(self, url):
|
|
|
|
self._url = url
|
|
|
|
|
|
|
|
def load(self):
|
2019-11-25 18:17:30 +00:00
|
|
|
outbox = ap_utils.activity_request(self._url).json()
|
2019-09-15 09:43:46 +00:00
|
|
|
|
|
|
|
items = None
|
2019-11-25 18:17:30 +00:00
|
|
|
if "first" in outbox:
|
|
|
|
if type(outbox["first"]) == dict:
|
2019-09-15 09:43:46 +00:00
|
|
|
# pleroma
|
2019-11-25 18:17:30 +00:00
|
|
|
items = outbox["first"]["orderedItems"]
|
2019-09-15 09:43:46 +00:00
|
|
|
else:
|
|
|
|
# mastodon
|
2019-11-25 18:17:30 +00:00
|
|
|
first = ap_utils.activity_request(outbox["first"]).json()
|
|
|
|
items = first["orderedItems"]
|
2019-09-15 09:43:46 +00:00
|
|
|
else:
|
2019-11-25 18:17:30 +00:00
|
|
|
items = outbox["orderedItems"]
|
2019-09-15 09:43:46 +00:00
|
|
|
return items
|
|
|
|
|
|
|
|
class Inbox(object):
|
2019-09-15 11:15:58 +00:00
|
|
|
def __init__(self, url):
|
2019-09-15 09:43:46 +00:00
|
|
|
self._url = url
|
2019-09-15 11:15:58 +00:00
|
|
|
def send(self, sender, activity, private_key):
|
2019-09-15 09:43:46 +00:00
|
|
|
now = email.utils.formatdate(timeval=None, localtime=False, usegmt=True)
|
|
|
|
parts = urllib.parse.urlparse(self._url)
|
|
|
|
headers = [
|
2019-09-19 14:28:42 +00:00
|
|
|
["Host", parts.netloc],
|
|
|
|
["Date", now]
|
2019-09-15 09:43:46 +00:00
|
|
|
]
|
|
|
|
sign_headers = headers[:]
|
|
|
|
sign_headers.insert(0, ["(request-target)", "post %s" % parts.path])
|
2019-09-15 10:54:07 +00:00
|
|
|
signature = ap_security.signature(private_key, sign_headers)
|
2019-09-15 09:43:46 +00:00
|
|
|
|
2019-09-15 13:27:34 +00:00
|
|
|
headers.append(["signature", signature])
|
|
|
|
|
2019-09-15 11:15:58 +00:00
|
|
|
return ap_utils.activity_request(self._url, activity.format(sender),
|
2019-11-25 18:17:30 +00:00
|
|
|
method="POST", headers=dict(headers)).json()
|
2019-09-15 09:43:46 +00:00
|
|
|
|