epicyon/announce.py

422 lines
16 KiB
Python
Raw Normal View History

2020-04-01 19:11:39 +00:00
__filename__ = "announce.py"
__author__ = "Bob Mottram"
__license__ = "AGPL3+"
2021-01-26 10:07:42 +00:00
__version__ = "1.2.0"
2020-04-01 19:11:39 +00:00
__maintainer__ = "Bob Mottram"
2021-09-10 16:14:50 +00:00
__email__ = "bob@libreserver.org"
2020-04-01 19:11:39 +00:00
__status__ = "Production"
2021-06-15 15:08:12 +00:00
__module_group__ = "ActivityPub"
2019-07-02 09:25:29 +00:00
2021-10-13 10:37:52 +00:00
from utils import hasObjectStringObject
from utils import hasGroupType
2021-06-26 14:21:24 +00:00
from utils import removeDomainPort
from utils import removeIdEnding
2021-12-26 12:19:00 +00:00
from utils import has_users_path
2021-12-26 12:45:03 +00:00
from utils import get_full_domain
2019-07-02 09:25:29 +00:00
from utils import getStatusNumber
from utils import createOutboxDir
2019-07-02 10:43:54 +00:00
from utils import urlPermitted
2019-07-11 17:55:10 +00:00
from utils import getNicknameFromActor
from utils import getDomainFromActor
from utils import locatePost
2019-10-22 11:55:06 +00:00
from utils import saveJson
from utils import undoAnnounceCollectionEntry
from utils import updateAnnounceCollection
2021-12-26 10:19:59 +00:00
from utils import local_actor_url
from utils import replaceUsersWithAt
from utils import hasActor
2021-10-13 10:11:02 +00:00
from utils import hasObjectStringType
2019-07-11 17:55:10 +00:00
from posts import sendSignedJson
2019-07-16 19:07:45 +00:00
from posts import getPersonBox
from session import postJson
from webfinger import webfingerHandle
from auth import createBasicAuthHeader
2019-07-02 09:25:29 +00:00
2020-04-01 19:11:39 +00:00
2021-12-25 22:09:19 +00:00
def isSelfAnnounce(post_json_object: {}) -> bool:
2021-06-22 19:43:03 +00:00
"""Is the given post a self announce?
"""
2021-12-25 22:09:19 +00:00
if not post_json_object.get('actor'):
2021-06-22 19:43:03 +00:00
return False
2021-12-25 22:09:19 +00:00
if not post_json_object.get('type'):
2021-06-22 19:43:03 +00:00
return False
2021-12-25 22:09:19 +00:00
if post_json_object['type'] != 'Announce':
2021-06-22 19:43:03 +00:00
return False
2021-12-25 22:09:19 +00:00
if not post_json_object.get('object'):
2021-06-22 19:43:03 +00:00
return False
2021-12-25 22:09:19 +00:00
if not isinstance(post_json_object['actor'], str):
2021-06-22 19:43:03 +00:00
return False
2021-12-25 22:09:19 +00:00
if not isinstance(post_json_object['object'], str):
2021-06-22 19:43:03 +00:00
return False
2021-12-25 22:09:19 +00:00
return post_json_object['actor'] in post_json_object['object']
2021-06-22 19:43:03 +00:00
2020-04-01 19:11:39 +00:00
def outboxAnnounce(recentPostsCache: {},
2021-12-25 23:51:19 +00:00
base_dir: str, message_json: {}, debug: bool) -> bool:
""" Adds or removes announce entries from the shares collection
within a given post
"""
2021-12-25 23:51:19 +00:00
if not hasActor(message_json, debug):
return False
2021-12-25 23:51:19 +00:00
if not isinstance(message_json['actor'], str):
return False
2021-12-25 23:51:19 +00:00
if not message_json.get('type'):
return False
2021-12-25 23:51:19 +00:00
if not message_json.get('object'):
return False
2021-12-25 23:51:19 +00:00
if message_json['type'] == 'Announce':
if not isinstance(message_json['object'], str):
2019-09-02 09:43:43 +00:00
return False
2021-12-25 23:51:19 +00:00
if isSelfAnnounce(message_json):
return False
2021-12-25 23:51:19 +00:00
nickname = getNicknameFromActor(message_json['actor'])
2019-09-02 09:43:43 +00:00
if not nickname:
2021-12-25 23:51:19 +00:00
print('WARN: no nickname found in ' + message_json['actor'])
2019-09-02 09:43:43 +00:00
return False
2021-12-25 23:51:19 +00:00
domain, port = getDomainFromActor(message_json['actor'])
2021-12-25 16:17:53 +00:00
postFilename = locatePost(base_dir, nickname, domain,
2021-12-25 23:51:19 +00:00
message_json['object'])
if postFilename:
2021-12-25 16:17:53 +00:00
updateAnnounceCollection(recentPostsCache, base_dir, postFilename,
2021-12-25 23:51:19 +00:00
message_json['actor'],
nickname, domain, debug)
return True
2021-12-25 23:51:19 +00:00
elif message_json['type'] == 'Undo':
if not hasObjectStringType(message_json, debug):
return False
2021-12-25 23:51:19 +00:00
if message_json['object']['type'] == 'Announce':
if not isinstance(message_json['object']['object'], str):
2019-09-02 09:43:43 +00:00
return False
2021-12-25 23:51:19 +00:00
nickname = getNicknameFromActor(message_json['actor'])
2019-09-02 09:43:43 +00:00
if not nickname:
2021-12-25 23:51:19 +00:00
print('WARN: no nickname found in ' + message_json['actor'])
2019-09-02 09:43:43 +00:00
return False
2021-12-25 23:51:19 +00:00
domain, port = getDomainFromActor(message_json['actor'])
2021-12-25 16:17:53 +00:00
postFilename = locatePost(base_dir, nickname, domain,
2021-12-25 23:51:19 +00:00
message_json['object']['object'])
if postFilename:
2020-04-01 19:11:39 +00:00
undoAnnounceCollectionEntry(recentPostsCache,
2021-12-25 16:17:53 +00:00
base_dir, postFilename,
2021-12-25 23:51:19 +00:00
message_json['actor'],
2020-04-01 19:11:39 +00:00
domain, debug)
return True
return False
2020-04-01 19:11:39 +00:00
2021-05-07 22:49:04 +00:00
def announcedByPerson(isAnnounced: bool, postActor: str,
2021-12-26 10:00:46 +00:00
nickname: str, domain_full: str) -> bool:
"""Returns True if the given post is announced by the given person
"""
2021-05-07 22:49:04 +00:00
if not postActor:
2021-05-07 13:26:00 +00:00
return False
2021-05-07 21:10:55 +00:00
if isAnnounced and \
2021-12-26 10:00:46 +00:00
postActor.endswith(domain_full + '/users/' + nickname):
2021-05-07 21:10:55 +00:00
return True
return False
2020-04-01 19:11:39 +00:00
2021-12-25 23:45:30 +00:00
def createAnnounce(session, base_dir: str, federation_list: [],
2020-04-01 19:11:39 +00:00
nickname: str, domain: str, port: int,
2021-12-25 17:09:22 +00:00
toUrl: str, ccUrl: str, http_prefix: str,
2020-04-01 19:11:39 +00:00
objectUrl: str, saveToFile: bool,
2021-12-25 20:39:35 +00:00
client_to_server: bool,
2021-12-25 21:37:41 +00:00
send_threads: [], postLog: [],
2021-12-25 22:28:18 +00:00
person_cache: {}, cached_webfingers: {},
2021-12-25 20:34:38 +00:00
debug: bool, project_version: str,
2021-12-25 23:03:28 +00:00
signing_priv_key_pem: str) -> {}:
2019-07-02 09:25:29 +00:00
"""Creates an announce message
Typically toUrl will be https://www.w3.org/ns/activitystreams#Public
2019-07-06 17:00:22 +00:00
and ccUrl might be a specific person favorited or repeated and the
followers url objectUrl is typically the url of the message,
corresponding to url or atomUri in createPostBase
2019-07-02 09:25:29 +00:00
"""
2021-12-25 23:45:30 +00:00
if not urlPermitted(objectUrl, federation_list):
2019-07-02 10:43:54 +00:00
return None
domain = removeDomainPort(domain)
2021-12-26 12:45:03 +00:00
fullDomain = get_full_domain(domain, port)
2020-04-01 19:11:39 +00:00
statusNumber, published = getStatusNumber()
2021-12-25 17:09:22 +00:00
newAnnounceId = http_prefix + '://' + fullDomain + \
2020-04-01 19:11:39 +00:00
'/users/' + nickname + '/statuses/' + statusNumber
2021-12-26 10:19:59 +00:00
atomUriStr = local_actor_url(http_prefix, nickname, fullDomain) + \
2020-04-01 19:11:39 +00:00
'/statuses/' + statusNumber
newAnnounce = {
2019-08-18 11:07:06 +00:00
"@context": "https://www.w3.org/ns/activitystreams",
2021-12-26 10:19:59 +00:00
'actor': local_actor_url(http_prefix, nickname, fullDomain),
2020-04-01 19:11:39 +00:00
'atomUri': atomUriStr,
2019-07-02 09:25:29 +00:00
'cc': [],
2020-08-23 11:13:35 +00:00
'id': newAnnounceId + '/activity',
2019-07-02 09:25:29 +00:00
'object': objectUrl,
'published': published,
'to': [toUrl],
'type': 'Announce'
}
if ccUrl:
2020-04-01 19:11:39 +00:00
if len(ccUrl) > 0:
newAnnounce['cc'] = [ccUrl]
2019-07-02 09:25:29 +00:00
if saveToFile:
2021-12-25 16:17:53 +00:00
outboxDir = createOutboxDir(nickname, domain, base_dir)
2020-04-01 19:11:39 +00:00
filename = outboxDir + '/' + newAnnounceId.replace('/', '#') + '.json'
saveJson(newAnnounce, filename)
2019-07-11 17:55:10 +00:00
2020-04-01 19:11:39 +00:00
announceNickname = None
announceDomain = None
announcePort = None
2021-12-26 00:07:44 +00:00
group_account = False
2021-12-26 12:19:00 +00:00
if has_users_path(objectUrl):
2020-04-01 19:11:39 +00:00
announceNickname = getNicknameFromActor(objectUrl)
announceDomain, announcePort = getDomainFromActor(objectUrl)
if '/' + str(announceNickname) + '/' in objectUrl:
announceActor = \
objectUrl.split('/' + announceNickname + '/')[0] + \
'/' + announceNickname
2021-12-25 22:17:49 +00:00
if hasGroupType(base_dir, announceActor, person_cache):
2021-12-26 00:07:44 +00:00
group_account = True
2019-07-11 17:55:10 +00:00
if announceNickname and announceDomain:
2021-12-25 16:17:53 +00:00
sendSignedJson(newAnnounce, session, base_dir,
2020-04-01 19:11:39 +00:00
nickname, domain, port,
announceNickname, announceDomain, announcePort, None,
2021-12-25 23:45:30 +00:00
http_prefix, True, client_to_server, federation_list,
2021-12-25 22:28:18 +00:00
send_threads, postLog, cached_webfingers, person_cache,
2021-12-26 00:07:44 +00:00
debug, project_version, None, group_account,
2021-12-25 23:03:28 +00:00
signing_priv_key_pem, 639633)
2020-03-22 21:16:02 +00:00
2019-07-02 09:25:29 +00:00
return newAnnounce
2020-04-01 19:11:39 +00:00
2021-12-25 23:45:30 +00:00
def announcePublic(session, base_dir: str, federation_list: [],
2021-12-25 17:09:22 +00:00
nickname: str, domain: str, port: int, http_prefix: str,
2021-12-25 20:39:35 +00:00
objectUrl: str, client_to_server: bool,
2021-12-25 21:37:41 +00:00
send_threads: [], postLog: [],
2021-12-25 22:28:18 +00:00
person_cache: {}, cached_webfingers: {},
2021-12-25 20:34:38 +00:00
debug: bool, project_version: str,
2021-12-25 23:03:28 +00:00
signing_priv_key_pem: str) -> {}:
2019-07-02 09:25:29 +00:00
"""Makes a public announcement
"""
2021-12-26 12:45:03 +00:00
fromDomain = get_full_domain(domain, port)
2020-04-01 19:11:39 +00:00
toUrl = 'https://www.w3.org/ns/activitystreams#Public'
2021-12-26 10:19:59 +00:00
ccUrl = local_actor_url(http_prefix, nickname, fromDomain) + '/followers'
2021-12-25 23:45:30 +00:00
return createAnnounce(session, base_dir, federation_list,
2020-04-01 19:11:39 +00:00
nickname, domain, port,
2021-12-25 17:09:22 +00:00
toUrl, ccUrl, http_prefix,
2021-12-25 20:39:35 +00:00
objectUrl, True, client_to_server,
2021-12-25 21:37:41 +00:00
send_threads, postLog,
2021-12-25 22:28:18 +00:00
person_cache, cached_webfingers,
2021-12-25 20:34:38 +00:00
debug, project_version,
2021-12-25 23:03:28 +00:00
signing_priv_key_pem)
2020-04-01 19:11:39 +00:00
2021-12-25 16:17:53 +00:00
def sendAnnounceViaServer(base_dir: str, session,
2020-04-01 19:11:39 +00:00
fromNickname: str, password: str,
fromDomain: str, fromPort: int,
2021-12-25 17:09:22 +00:00
http_prefix: str, repeatObjectUrl: str,
2021-12-25 22:28:18 +00:00
cached_webfingers: {}, person_cache: {},
2021-12-25 20:34:38 +00:00
debug: bool, project_version: str,
2021-12-25 23:03:28 +00:00
signing_priv_key_pem: str) -> {}:
2019-07-16 19:07:45 +00:00
"""Creates an announce message via c2s
"""
if not session:
print('WARN: No session for sendAnnounceViaServer')
return 6
2021-12-26 12:45:03 +00:00
fromDomainFull = get_full_domain(fromDomain, fromPort)
2019-07-16 19:07:45 +00:00
2020-04-01 19:11:39 +00:00
toUrl = 'https://www.w3.org/ns/activitystreams#Public'
2021-12-26 10:19:59 +00:00
actorStr = local_actor_url(http_prefix, fromNickname, fromDomainFull)
2021-08-14 11:13:39 +00:00
ccUrl = actorStr + '/followers'
2019-07-16 19:07:45 +00:00
2020-04-01 19:11:39 +00:00
statusNumber, published = getStatusNumber()
2021-08-14 11:13:39 +00:00
newAnnounceId = actorStr + '/statuses/' + statusNumber
2020-04-01 19:11:39 +00:00
newAnnounceJson = {
2019-08-18 11:07:06 +00:00
"@context": "https://www.w3.org/ns/activitystreams",
2021-06-22 12:42:52 +00:00
'actor': actorStr,
2019-07-16 19:07:45 +00:00
'atomUri': newAnnounceId,
'cc': [ccUrl],
2020-08-23 11:13:35 +00:00
'id': newAnnounceId + '/activity',
2019-07-16 19:07:45 +00:00
'object': repeatObjectUrl,
'published': published,
'to': [toUrl],
'type': 'Announce'
}
2021-12-25 17:09:22 +00:00
handle = http_prefix + '://' + fromDomainFull + '/@' + fromNickname
2019-07-16 19:07:45 +00:00
# lookup the inbox for the To handle
2021-12-25 17:09:22 +00:00
wfRequest = webfingerHandle(session, handle, http_prefix,
2021-12-25 22:28:18 +00:00
cached_webfingers,
2021-12-25 20:34:38 +00:00
fromDomain, project_version, debug, False,
2021-12-25 23:03:28 +00:00
signing_priv_key_pem)
2019-07-16 19:07:45 +00:00
if not wfRequest:
if debug:
2020-04-01 19:11:39 +00:00
print('DEBUG: announce webfinger failed for ' + handle)
2019-07-16 19:07:45 +00:00
return 1
2020-06-23 10:41:12 +00:00
if not isinstance(wfRequest, dict):
2021-03-18 10:01:01 +00:00
print('WARN: announce webfinger for ' + handle +
' did not return a dict. ' + str(wfRequest))
2020-06-23 10:41:12 +00:00
return 1
2019-07-16 19:07:45 +00:00
2020-04-01 19:11:39 +00:00
postToBox = 'outbox'
2019-07-16 19:07:45 +00:00
# get the actor inbox for the To handle
2021-09-15 14:05:08 +00:00
originDomain = fromDomain
2020-04-01 19:11:39 +00:00
(inboxUrl, pubKeyId, pubKey, fromPersonId,
2020-09-27 19:27:24 +00:00
sharedInbox, avatarUrl,
2021-12-25 23:03:28 +00:00
displayName, _) = getPersonBox(signing_priv_key_pem,
originDomain,
2021-12-25 16:17:53 +00:00
base_dir, session, wfRequest,
2021-12-25 22:17:49 +00:00
person_cache,
2021-12-25 20:34:38 +00:00
project_version, http_prefix,
fromNickname, fromDomain,
postToBox, 73528)
2020-03-22 21:16:02 +00:00
2019-07-16 19:07:45 +00:00
if not inboxUrl:
if debug:
2021-03-18 10:01:01 +00:00
print('DEBUG: announce no ' + postToBox +
' was found for ' + handle)
2019-07-16 19:07:45 +00:00
return 3
if not fromPersonId:
if debug:
2021-03-18 10:01:01 +00:00
print('DEBUG: announce no actor was found for ' + handle)
2019-07-16 19:07:45 +00:00
return 4
2020-03-22 21:16:02 +00:00
2020-04-01 19:11:39 +00:00
authHeader = createBasicAuthHeader(fromNickname, password)
2020-03-22 21:16:02 +00:00
2020-04-01 19:11:39 +00:00
headers = {
'host': fromDomain,
'Content-type': 'application/json',
2020-03-22 20:36:19 +00:00
'Authorization': authHeader
}
2021-12-25 17:09:22 +00:00
postResult = postJson(http_prefix, fromDomainFull,
2021-06-20 13:39:53 +00:00
session, newAnnounceJson, [], inboxUrl,
2021-03-21 13:35:15 +00:00
headers, 3, True)
2020-04-01 19:11:39 +00:00
if not postResult:
2021-03-18 10:01:01 +00:00
print('WARN: announce not posted')
2019-07-16 19:07:45 +00:00
if debug:
print('DEBUG: c2s POST announce success')
return newAnnounceJson
2021-03-18 20:56:08 +00:00
2021-12-25 16:17:53 +00:00
def sendUndoAnnounceViaServer(base_dir: str, session,
2021-03-18 20:56:08 +00:00
undoPostJsonObject: {},
nickname: str, password: str,
domain: str, port: int,
2021-12-25 17:09:22 +00:00
http_prefix: str, repeatObjectUrl: str,
2021-12-25 22:28:18 +00:00
cached_webfingers: {}, person_cache: {},
2021-12-25 20:34:38 +00:00
debug: bool, project_version: str,
2021-12-25 23:03:28 +00:00
signing_priv_key_pem: str) -> {}:
2021-03-18 20:56:08 +00:00
"""Undo an announce message via c2s
"""
if not session:
print('WARN: No session for sendUndoAnnounceViaServer')
return 6
2021-12-26 12:45:03 +00:00
domain_full = get_full_domain(domain, port)
2021-03-18 20:56:08 +00:00
2021-12-26 10:19:59 +00:00
actor = local_actor_url(http_prefix, nickname, domain_full)
handle = replaceUsersWithAt(actor)
2021-03-18 20:56:08 +00:00
statusNumber, published = getStatusNumber()
unAnnounceJson = {
'@context': 'https://www.w3.org/ns/activitystreams',
'id': actor + '/statuses/' + str(statusNumber) + '/undo',
'type': 'Undo',
'actor': actor,
'object': undoPostJsonObject['object']
}
# lookup the inbox for the To handle
2021-12-25 17:09:22 +00:00
wfRequest = webfingerHandle(session, handle, http_prefix,
2021-12-25 22:28:18 +00:00
cached_webfingers,
2021-12-25 20:34:38 +00:00
domain, project_version, debug, False,
2021-12-25 23:03:28 +00:00
signing_priv_key_pem)
2021-03-18 20:56:08 +00:00
if not wfRequest:
if debug:
print('DEBUG: undo announce webfinger failed for ' + handle)
return 1
if not isinstance(wfRequest, dict):
print('WARN: undo announce webfinger for ' + handle +
' did not return a dict. ' + str(wfRequest))
return 1
postToBox = 'outbox'
# get the actor inbox for the To handle
2021-09-15 14:05:08 +00:00
originDomain = domain
2021-03-18 20:56:08 +00:00
(inboxUrl, pubKeyId, pubKey, fromPersonId,
sharedInbox, avatarUrl,
2021-12-25 23:03:28 +00:00
displayName, _) = getPersonBox(signing_priv_key_pem,
originDomain,
2021-12-25 16:17:53 +00:00
base_dir, session, wfRequest,
2021-12-25 22:17:49 +00:00
person_cache,
2021-12-25 20:34:38 +00:00
project_version, http_prefix,
nickname, domain,
postToBox, 73528)
2021-03-18 20:56:08 +00:00
if not inboxUrl:
if debug:
print('DEBUG: undo announce no ' + postToBox +
' was found for ' + handle)
return 3
if not fromPersonId:
if debug:
print('DEBUG: undo announce no actor was found for ' + handle)
return 4
authHeader = createBasicAuthHeader(nickname, password)
headers = {
'host': domain,
'Content-type': 'application/json',
'Authorization': authHeader
}
2021-12-26 10:00:46 +00:00
postResult = postJson(http_prefix, domain_full,
2021-06-20 13:39:53 +00:00
session, unAnnounceJson, [], inboxUrl,
2021-03-21 13:35:15 +00:00
headers, 3, True)
2021-03-18 20:56:08 +00:00
if not postResult:
print('WARN: undo announce not posted')
if debug:
print('DEBUG: c2s POST undo announce success')
return unAnnounceJson
def outboxUndoAnnounce(recentPostsCache: {},
2021-12-25 17:09:22 +00:00
base_dir: str, http_prefix: str,
nickname: str, domain: str, port: int,
2021-12-25 23:51:19 +00:00
message_json: {}, debug: bool) -> None:
""" When an undo announce is received by the outbox from c2s
"""
2021-12-25 23:51:19 +00:00
if not message_json.get('type'):
return
2021-12-25 23:51:19 +00:00
if not message_json['type'] == 'Undo':
return
2021-12-25 23:51:19 +00:00
if not hasObjectStringType(message_json, debug):
return
2021-12-25 23:51:19 +00:00
if not message_json['object']['type'] == 'Announce':
if debug:
print('DEBUG: not a undo announce')
return
2021-12-25 23:51:19 +00:00
if not hasObjectStringObject(message_json, debug):
return
if debug:
print('DEBUG: c2s undo announce request arrived in outbox')
2021-12-25 23:51:19 +00:00
messageId = removeIdEnding(message_json['object']['object'])
domain = removeDomainPort(domain)
2021-12-25 16:17:53 +00:00
postFilename = locatePost(base_dir, nickname, domain, messageId)
if not postFilename:
if debug:
print('DEBUG: c2s undo announce post not found in inbox or outbox')
print(messageId)
return True
2021-12-25 16:17:53 +00:00
undoAnnounceCollectionEntry(recentPostsCache, base_dir, postFilename,
2021-12-25 23:51:19 +00:00
message_json['actor'], domain, debug)
if debug:
print('DEBUG: post undo announce via c2s - ' + postFilename)