epicyon/announce.py

421 lines
15 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"
__email__ = "bob@freedombone.net"
__status__ = "Production"
2021-06-15 15:08:12 +00:00
__module_group__ = "ActivityPub"
2019-07-02 09:25:29 +00:00
from utils import hasGroupType
2021-06-26 14:21:24 +00:00
from utils import removeDomainPort
from utils import hasObjectDict
from utils import removeIdEnding
2020-12-23 10:57:44 +00:00
from utils import hasUsersPath
2020-12-16 10:30:54 +00:00
from utils import getFullDomain
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
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-06-22 19:43:03 +00:00
def isSelfAnnounce(postJsonObject: {}) -> bool:
"""Is the given post a self announce?
"""
if not postJsonObject.get('actor'):
return False
if not postJsonObject.get('type'):
return False
if postJsonObject['type'] != 'Announce':
return False
if not postJsonObject.get('object'):
return False
if not isinstance(postJsonObject['actor'], str):
return False
if not isinstance(postJsonObject['object'], str):
return False
return postJsonObject['actor'] in postJsonObject['object']
2020-04-01 19:11:39 +00:00
def outboxAnnounce(recentPostsCache: {},
baseDir: str, messageJson: {}, debug: bool) -> bool:
""" Adds or removes announce entries from the shares collection
within a given post
"""
if not messageJson.get('actor'):
return False
if not isinstance(messageJson['actor'], str):
return False
if not messageJson.get('type'):
return False
if not messageJson.get('object'):
return False
2020-04-01 19:11:39 +00:00
if messageJson['type'] == 'Announce':
if not isinstance(messageJson['object'], str):
2019-09-02 09:43:43 +00:00
return False
if isSelfAnnounce(messageJson):
return False
2020-04-01 19:11:39 +00:00
nickname = getNicknameFromActor(messageJson['actor'])
2019-09-02 09:43:43 +00:00
if not nickname:
2021-05-07 16:56:21 +00:00
print('WARN: no nickname found in ' + messageJson['actor'])
2019-09-02 09:43:43 +00:00
return False
2020-04-01 19:11:39 +00:00
domain, port = getDomainFromActor(messageJson['actor'])
postFilename = locatePost(baseDir, nickname, domain,
messageJson['object'])
if postFilename:
2020-04-01 19:11:39 +00:00
updateAnnounceCollection(recentPostsCache, baseDir, postFilename,
messageJson['actor'],
nickname, domain, debug)
return True
2021-05-07 16:56:21 +00:00
elif messageJson['type'] == 'Undo':
2021-07-06 10:00:19 +00:00
if not hasObjectDict(messageJson):
2019-09-02 09:43:43 +00:00
return False
if not messageJson['object'].get('type'):
return False
2020-04-01 19:11:39 +00:00
if messageJson['object']['type'] == 'Announce':
if not isinstance(messageJson['object']['object'], str):
2019-09-02 09:43:43 +00:00
return False
2020-04-01 19:11:39 +00:00
nickname = getNicknameFromActor(messageJson['actor'])
2019-09-02 09:43:43 +00:00
if not nickname:
2020-04-01 19:11:39 +00:00
print('WARN: no nickname found in ' + messageJson['actor'])
2019-09-02 09:43:43 +00:00
return False
2020-04-01 19:11:39 +00:00
domain, port = getDomainFromActor(messageJson['actor'])
postFilename = locatePost(baseDir, nickname, domain,
messageJson['object']['object'])
if postFilename:
2020-04-01 19:11:39 +00:00
undoAnnounceCollectionEntry(recentPostsCache,
baseDir, postFilename,
messageJson['actor'],
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-05-07 21:10:55 +00:00
nickname: str, domainFull: 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-05-07 22:49:04 +00:00
postActor.endswith(domainFull + '/users/' + nickname):
2021-05-07 21:10:55 +00:00
return True
return False
2020-04-01 19:11:39 +00:00
def createAnnounce(session, baseDir: str, federationList: [],
nickname: str, domain: str, port: int,
toUrl: str, ccUrl: str, httpPrefix: str,
objectUrl: str, saveToFile: bool,
clientToServer: bool,
sendThreads: [], postLog: [],
personCache: {}, cachedWebfingers: {},
debug: bool, projectVersion: 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
"""
2020-09-27 19:27:24 +00:00
if not urlPermitted(objectUrl, federationList):
2019-07-02 10:43:54 +00:00
return None
domain = removeDomainPort(domain)
2020-12-16 10:30:54 +00:00
fullDomain = getFullDomain(domain, port)
2020-04-01 19:11:39 +00:00
statusNumber, published = getStatusNumber()
newAnnounceId = httpPrefix + '://' + fullDomain + \
'/users/' + nickname + '/statuses/' + statusNumber
atomUriStr = httpPrefix + '://' + fullDomain + '/users/' + nickname + \
'/statuses/' + statusNumber
newAnnounce = {
2019-08-18 11:07:06 +00:00
"@context": "https://www.w3.org/ns/activitystreams",
2021-05-07 16:56:21 +00:00
'actor': httpPrefix + '://' + fullDomain + '/users/' + nickname,
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:
2020-04-01 19:11:39 +00:00
outboxDir = createOutboxDir(nickname, domain, baseDir)
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
groupAccount = False
2020-12-23 10:57:44 +00:00
if hasUsersPath(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
if hasGroupType(baseDir, announceActor, personCache):
groupAccount = True
2019-07-11 17:55:10 +00:00
if announceNickname and announceDomain:
2020-04-01 19:11:39 +00:00
sendSignedJson(newAnnounce, session, baseDir,
nickname, domain, port,
announceNickname, announceDomain, announcePort, None,
httpPrefix, True, clientToServer, federationList,
sendThreads, postLog, cachedWebfingers, personCache,
debug, projectVersion, None, groupAccount)
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
def announcePublic(session, baseDir: str, federationList: [],
nickname: str, domain: str, port: int, httpPrefix: str,
objectUrl: str, clientToServer: bool,
sendThreads: [], postLog: [],
personCache: {}, cachedWebfingers: {},
debug: bool, projectVersion: str) -> {}:
2019-07-02 09:25:29 +00:00
"""Makes a public announcement
"""
2020-12-16 10:30:54 +00:00
fromDomain = getFullDomain(domain, port)
2020-04-01 19:11:39 +00:00
toUrl = 'https://www.w3.org/ns/activitystreams#Public'
ccUrl = httpPrefix + '://' + fromDomain + '/users/' + nickname + \
'/followers'
return createAnnounce(session, baseDir, federationList,
nickname, domain, port,
toUrl, ccUrl, httpPrefix,
objectUrl, True, clientToServer,
sendThreads, postLog,
personCache, cachedWebfingers,
debug, projectVersion)
def sendAnnounceViaServer(baseDir: str, session,
fromNickname: str, password: str,
fromDomain: str, fromPort: int,
httpPrefix: str, repeatObjectUrl: str,
cachedWebfingers: {}, personCache: {},
debug: bool, projectVersion: 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
2020-12-16 10:30:54 +00:00
fromDomainFull = getFullDomain(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'
ccUrl = httpPrefix + '://' + fromDomainFull + '/users/' + fromNickname + \
'/followers'
2019-07-16 19:07:45 +00:00
2020-04-01 19:11:39 +00:00
statusNumber, published = getStatusNumber()
newAnnounceId = httpPrefix + '://' + fromDomainFull + '/users/' + \
fromNickname + '/statuses/' + statusNumber
2021-06-22 12:42:52 +00:00
actorStr = httpPrefix + '://' + fromDomainFull + '/users/' + fromNickname
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'
}
2020-04-01 19:11:39 +00:00
handle = httpPrefix + '://' + fromDomainFull + '/@' + fromNickname
2019-07-16 19:07:45 +00:00
# lookup the inbox for the To handle
2020-04-01 19:11:39 +00:00
wfRequest = webfingerHandle(session, handle, httpPrefix,
cachedWebfingers,
2021-07-30 13:00:23 +00:00
fromDomain, projectVersion, debug, False)
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
2020-04-01 19:11:39 +00:00
(inboxUrl, pubKeyId, pubKey, fromPersonId,
2020-09-27 19:27:24 +00:00
sharedInbox, avatarUrl,
displayName) = getPersonBox(baseDir, session, wfRequest,
personCache,
projectVersion, httpPrefix,
fromNickname, fromDomain,
2020-12-18 17:49:17 +00:00
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-06-20 13:39:53 +00:00
postResult = postJson(httpPrefix, fromDomainFull,
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
def sendUndoAnnounceViaServer(baseDir: str, session,
undoPostJsonObject: {},
nickname: str, password: str,
domain: str, port: int,
httpPrefix: str, repeatObjectUrl: str,
cachedWebfingers: {}, personCache: {},
debug: bool, projectVersion: str) -> {}:
"""Undo an announce message via c2s
"""
if not session:
print('WARN: No session for sendUndoAnnounceViaServer')
return 6
domainFull = getFullDomain(domain, port)
actor = httpPrefix + '://' + domainFull + '/users/' + nickname
handle = actor.replace('/users/', '/@')
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
wfRequest = webfingerHandle(session, handle, httpPrefix,
cachedWebfingers,
2021-07-30 13:00:23 +00:00
domain, projectVersion, debug, False)
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
(inboxUrl, pubKeyId, pubKey, fromPersonId,
sharedInbox, avatarUrl,
displayName) = getPersonBox(baseDir, session, wfRequest,
personCache,
projectVersion, httpPrefix,
nickname, domain,
postToBox, 73528)
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-06-20 13:39:53 +00:00
postResult = postJson(httpPrefix, domainFull,
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: {},
baseDir: str, httpPrefix: str,
nickname: str, domain: str, port: int,
messageJson: {}, debug: bool) -> None:
""" When an undo announce is received by the outbox from c2s
"""
if not messageJson.get('type'):
return
if not messageJson['type'] == 'Undo':
return
if not hasObjectDict(messageJson):
if debug:
print('DEBUG: undo like object is not dict')
return
if not messageJson['object'].get('type'):
if debug:
print('DEBUG: undo like - no type')
return
if not messageJson['object']['type'] == 'Announce':
if debug:
print('DEBUG: not a undo announce')
return
if not messageJson['object'].get('object'):
if debug:
print('DEBUG: no object in undo announce')
return
if not isinstance(messageJson['object']['object'], str):
if debug:
print('DEBUG: undo announce object is not string')
return
if debug:
print('DEBUG: c2s undo announce request arrived in outbox')
messageId = removeIdEnding(messageJson['object']['object'])
domain = removeDomainPort(domain)
postFilename = locatePost(baseDir, nickname, domain, messageId)
if not postFilename:
if debug:
print('DEBUG: c2s undo announce post not found in inbox or outbox')
print(messageId)
return True
undoAnnounceCollectionEntry(recentPostsCache, baseDir, postFilename,
messageJson['actor'], domain, debug)
if debug:
print('DEBUG: post undo announce via c2s - ' + postFilename)