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-12-26 15:54:46 +00:00
|
|
|
from utils import has_object_string_object
|
2021-12-26 17:53:07 +00:00
|
|
|
from utils import has_group_type
|
2021-12-26 18:17:37 +00:00
|
|
|
from utils import remove_domain_port
|
2021-05-08 13:14:52 +00:00
|
|
|
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
|
2021-12-26 20:36:08 +00:00
|
|
|
from utils import locate_post
|
2021-12-26 14:47:21 +00:00
|
|
|
from utils import save_json
|
2021-12-27 10:55:48 +00:00
|
|
|
from utils import undo_announce_collection_entry
|
2021-12-26 23:41:34 +00:00
|
|
|
from utils import update_announce_collection
|
2021-12-26 10:19:59 +00:00
|
|
|
from utils import local_actor_url
|
2021-12-26 17:21:37 +00:00
|
|
|
from utils import replace_users_with_at
|
2021-12-26 17:15:04 +00:00
|
|
|
from utils import has_actor
|
2021-12-26 17:12:07 +00:00
|
|
|
from utils import has_object_stringType
|
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
|
|
|
|
|
|
|
|
2021-12-26 20:01:37 +00:00
|
|
|
def outboxAnnounce(recent_posts_cache: {},
|
2021-12-25 23:51:19 +00:00
|
|
|
base_dir: str, message_json: {}, debug: bool) -> bool:
|
2019-08-01 11:43:22 +00:00
|
|
|
""" Adds or removes announce entries from the shares collection
|
|
|
|
within a given post
|
|
|
|
"""
|
2021-12-26 17:15:04 +00:00
|
|
|
if not has_actor(message_json, debug):
|
2019-08-01 11:43:22 +00:00
|
|
|
return False
|
2021-12-25 23:51:19 +00:00
|
|
|
if not isinstance(message_json['actor'], str):
|
2021-06-03 08:33:50 +00:00
|
|
|
return False
|
2021-12-25 23:51:19 +00:00
|
|
|
if not message_json.get('type'):
|
2019-08-01 11:43:22 +00:00
|
|
|
return False
|
2021-12-25 23:51:19 +00:00
|
|
|
if not message_json.get('object'):
|
2019-08-01 11:43:22 +00:00
|
|
|
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):
|
2021-06-03 08:33:50 +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-26 20:09:52 +00:00
|
|
|
domain, _ = getDomainFromActor(message_json['actor'])
|
2021-12-26 23:41:34 +00:00
|
|
|
post_filename = locate_post(base_dir, nickname, domain,
|
|
|
|
message_json['object'])
|
|
|
|
if post_filename:
|
|
|
|
update_announce_collection(recent_posts_cache,
|
|
|
|
base_dir, post_filename,
|
|
|
|
message_json['actor'],
|
|
|
|
nickname, domain, debug)
|
2019-08-01 11:43:22 +00:00
|
|
|
return True
|
2021-12-25 23:51:19 +00:00
|
|
|
elif message_json['type'] == 'Undo':
|
2021-12-26 17:12:07 +00:00
|
|
|
if not has_object_stringType(message_json, debug):
|
2019-08-01 11:43:22 +00:00
|
|
|
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-26 20:09:52 +00:00
|
|
|
domain, _ = getDomainFromActor(message_json['actor'])
|
2021-12-26 23:41:34 +00:00
|
|
|
post_filename = locate_post(base_dir, nickname, domain,
|
|
|
|
message_json['object']['object'])
|
|
|
|
if post_filename:
|
2021-12-27 10:55:48 +00:00
|
|
|
undo_announce_collection_entry(recent_posts_cache,
|
|
|
|
base_dir, post_filename,
|
|
|
|
message_json['actor'],
|
|
|
|
domain, debug)
|
2019-08-01 11:43:22 +00:00
|
|
|
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:
|
2019-08-01 09:31:38 +00:00
|
|
|
"""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
|
2019-08-01 09:31:38 +00:00
|
|
|
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
|
|
|
|
|
2021-12-26 18:17:37 +00:00
|
|
|
domain = remove_domain_port(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'
|
2021-12-26 14:47:21 +00:00
|
|
|
save_json(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)
|
2021-07-31 13:19:45 +00:00
|
|
|
if '/' + str(announceNickname) + '/' in objectUrl:
|
|
|
|
announceActor = \
|
|
|
|
objectUrl.split('/' + announceNickname + '/')[0] + \
|
|
|
|
'/' + announceNickname
|
2021-12-26 17:53:07 +00:00
|
|
|
if has_group_type(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,
|
2021-09-22 09:29:48 +00:00
|
|
|
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,
|
2021-09-22 09:29:48 +00:00
|
|
|
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)
|
2021-12-26 17:21:37 +00:00
|
|
|
handle = replace_users_with_at(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,
|
2021-09-22 09:29:48 +00:00
|
|
|
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,
|
2021-09-22 09:29:48 +00:00
|
|
|
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
|
2021-05-08 13:14:52 +00:00
|
|
|
|
|
|
|
|
2021-12-26 20:01:37 +00:00
|
|
|
def outboxUndoAnnounce(recent_posts_cache: {},
|
2021-12-25 17:09:22 +00:00
|
|
|
base_dir: str, http_prefix: str,
|
2021-05-08 13:14:52 +00:00
|
|
|
nickname: str, domain: str, port: int,
|
2021-12-25 23:51:19 +00:00
|
|
|
message_json: {}, debug: bool) -> None:
|
2021-05-08 13:14:52 +00:00
|
|
|
""" 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'):
|
2021-05-08 13:14:52 +00:00
|
|
|
return
|
2021-12-25 23:51:19 +00:00
|
|
|
if not message_json['type'] == 'Undo':
|
2021-05-08 13:14:52 +00:00
|
|
|
return
|
2021-12-26 17:12:07 +00:00
|
|
|
if not has_object_stringType(message_json, debug):
|
2021-05-08 13:14:52 +00:00
|
|
|
return
|
2021-12-25 23:51:19 +00:00
|
|
|
if not message_json['object']['type'] == 'Announce':
|
2021-05-08 13:14:52 +00:00
|
|
|
if debug:
|
|
|
|
print('DEBUG: not a undo announce')
|
|
|
|
return
|
2021-12-26 15:54:46 +00:00
|
|
|
if not has_object_string_object(message_json, debug):
|
2021-05-08 13:14:52 +00:00
|
|
|
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'])
|
2021-12-26 18:17:37 +00:00
|
|
|
domain = remove_domain_port(domain)
|
2021-12-26 23:41:34 +00:00
|
|
|
post_filename = locate_post(base_dir, nickname, domain, messageId)
|
|
|
|
if not post_filename:
|
2021-05-08 13:14:52 +00:00
|
|
|
if debug:
|
|
|
|
print('DEBUG: c2s undo announce post not found in inbox or outbox')
|
|
|
|
print(messageId)
|
|
|
|
return True
|
2021-12-27 10:55:48 +00:00
|
|
|
undo_announce_collection_entry(recent_posts_cache, base_dir, post_filename,
|
|
|
|
message_json['actor'], domain, debug)
|
2021-05-08 13:14:52 +00:00
|
|
|
if debug:
|
2021-12-26 23:41:34 +00:00
|
|
|
print('DEBUG: post undo announce via c2s - ' + post_filename)
|