epicyon/outbox.py

339 lines
14 KiB
Python
Raw Normal View History

2020-04-03 17:15:33 +00:00
__filename__ = "outbox.py"
__author__ = "Bob Mottram"
__license__ = "AGPL3+"
__version__ = "1.1.0"
__maintainer__ = "Bob Mottram"
__email__ = "bob@freedombone.net"
__status__ = "Production"
2020-01-13 10:35:17 +00:00
import os
from session import createSession
2020-02-04 20:25:00 +00:00
from auth import createPassword
2020-01-13 10:35:17 +00:00
from posts import outboxMessageCreateWrap
from posts import savePostToBox
from posts import sendToFollowersThread
from posts import sendToNamedAddresses
from utils import getDomainFromActor
from blocking import isBlockedDomain
from blocking import outboxBlock
from blocking import outboxUndoBlock
2020-01-15 11:06:40 +00:00
from media import replaceYouTube
2020-01-13 10:35:17 +00:00
from media import getMediaPath
from media import createMediaDirs
from inbox import inboxUpdateIndex
from announce import outboxAnnounce
from follow import outboxUndoFollow
from roles import outboxDelegate
from skills import outboxSkills
from availability import outboxAvailability
from like import outboxLike
from like import outboxUndoLike
from bookmarks import outboxBookmark
from bookmarks import outboxUndoBookmark
from delete import outboxDelete
from shares import outboxShareUpload
from shares import outboxUndoShareUpload
2020-04-03 17:15:33 +00:00
def postMessageToOutbox(messageJson: {}, postToNickname: str,
server, baseDir: str, httpPrefix: str,
domain: str, domainFull: str, onionDomain: str,
port: int,
recentPostsCache: {}, followersThreads: [],
federationList: [], sendThreads: [],
postLog: [], cachedWebfingers: {},
personCache: {}, allowDeletion: bool,
useTor: bool, version: str, debug: bool) -> bool:
2020-01-13 10:35:17 +00:00
"""post is received by the outbox
Client to server message post
https://www.w3.org/TR/activitypub/#client-to-server-outbox-delivery
"""
if not messageJson.get('type'):
if debug:
print('DEBUG: POST to outbox has no "type" parameter')
return False
if not messageJson.get('object') and messageJson.get('content'):
2020-04-03 17:15:33 +00:00
if messageJson['type'] != 'Create':
2020-01-13 10:35:17 +00:00
# https://www.w3.org/TR/activitypub/#object-without-create
if debug:
print('DEBUG: POST to outbox - adding Create wrapper')
2020-04-03 17:15:33 +00:00
messageJson = \
outboxMessageCreateWrap(httpPrefix,
postToNickname,
domain, port,
2020-01-13 10:35:17 +00:00
messageJson)
2020-04-03 17:15:33 +00:00
if messageJson['type'] == 'Create':
if not (messageJson.get('id') and
messageJson.get('type') and
messageJson.get('actor') and
messageJson.get('object') and
2020-01-13 10:35:17 +00:00
messageJson.get('to')):
2020-01-13 12:45:27 +00:00
if not messageJson.get('id'):
if debug:
2020-04-03 17:15:33 +00:00
print('DEBUG: POST to outbox - ' +
'Create does not have the id parameter ' +
str(messageJson))
2020-01-13 12:45:27 +00:00
elif not messageJson.get('id'):
if debug:
2020-04-03 17:15:33 +00:00
print('DEBUG: POST to outbox - ' +
'Create does not have the type parameter ' +
str(messageJson))
2020-01-13 12:45:27 +00:00
elif not messageJson.get('id'):
if debug:
2020-04-03 17:15:33 +00:00
print('DEBUG: POST to outbox - ' +
'Create does not have the actor parameter ' +
str(messageJson))
2020-01-13 12:45:27 +00:00
elif not messageJson.get('id'):
if debug:
2020-04-03 17:15:33 +00:00
print('DEBUG: POST to outbox - ' +
'Create does not have the object parameter ' +
str(messageJson))
2020-01-13 12:45:27 +00:00
else:
if debug:
2020-04-03 17:15:33 +00:00
print('DEBUG: POST to outbox - ' +
'Create does not have the "to" parameter ' +
str(messageJson))
2020-01-13 10:35:17 +00:00
return False
2020-04-03 17:15:33 +00:00
testDomain, testPort = getDomainFromActor(messageJson['actor'])
2020-01-13 10:35:17 +00:00
if testPort:
2020-04-03 17:15:33 +00:00
if testPort != 80 and testPort != 443:
testDomain = testDomain + ':' + str(testPort)
if isBlockedDomain(baseDir, testDomain):
2020-01-13 10:35:17 +00:00
if debug:
2020-04-03 17:15:33 +00:00
print('DEBUG: domain is blocked: ' + messageJson['actor'])
2020-01-13 10:35:17 +00:00
return False
2020-01-15 11:06:40 +00:00
# replace youtube, so that google gets less tracking data
replaceYouTube(messageJson)
2020-01-13 10:35:17 +00:00
# https://www.w3.org/TR/activitypub/#create-activity-outbox
2020-04-03 17:15:33 +00:00
messageJson['object']['attributedTo'] = messageJson['actor']
2020-01-13 10:35:17 +00:00
if messageJson['object'].get('attachment'):
2020-04-03 17:15:33 +00:00
attachmentIndex = 0
attach = messageJson['object']['attachment'][attachmentIndex]
if attach.get('mediaType'):
fileExtension = 'png'
mediaTypeStr = \
attach['mediaType']
2020-01-13 10:35:17 +00:00
if mediaTypeStr.endswith('jpeg'):
2020-04-03 17:15:33 +00:00
fileExtension = 'jpg'
2020-01-13 10:35:17 +00:00
elif mediaTypeStr.endswith('gif'):
2020-04-03 17:15:33 +00:00
fileExtension = 'gif'
2020-01-13 10:35:17 +00:00
elif mediaTypeStr.endswith('webp'):
2020-04-03 17:15:33 +00:00
fileExtension = 'webp'
2020-01-13 10:35:17 +00:00
elif mediaTypeStr.endswith('audio/mpeg'):
2020-04-03 17:15:33 +00:00
fileExtension = 'mp3'
2020-01-13 10:35:17 +00:00
elif mediaTypeStr.endswith('ogg'):
2020-04-03 17:15:33 +00:00
fileExtension = 'ogg'
2020-01-13 10:35:17 +00:00
elif mediaTypeStr.endswith('mp4'):
2020-04-03 17:15:33 +00:00
fileExtension = 'mp4'
2020-01-13 10:35:17 +00:00
elif mediaTypeStr.endswith('webm'):
2020-04-03 17:15:33 +00:00
fileExtension = 'webm'
2020-01-13 10:35:17 +00:00
elif mediaTypeStr.endswith('ogv'):
2020-04-03 17:15:33 +00:00
fileExtension = 'ogv'
mediaDir = \
baseDir + '/accounts/' + \
postToNickname + '@' + domain
uploadMediaFilename = mediaDir + '/upload.' + fileExtension
2020-01-13 10:35:17 +00:00
if not os.path.isfile(uploadMediaFilename):
del messageJson['object']['attachment']
else:
# generate a path for the uploaded image
2020-04-03 17:15:33 +00:00
mPath = getMediaPath()
mediaPath = mPath + '/' + \
createPassword(32) + '.' + fileExtension
createMediaDirs(baseDir, mPath)
mediaFilename = baseDir + '/' + mediaPath
2020-01-13 10:35:17 +00:00
# move the uploaded image to its new path
2020-04-03 17:15:33 +00:00
os.rename(uploadMediaFilename, mediaFilename)
2020-01-13 10:35:17 +00:00
# change the url of the attachment
2020-04-03 17:15:33 +00:00
attach['url'] = \
httpPrefix + '://' + domainFull + '/' + mediaPath
2020-01-13 10:35:17 +00:00
2020-04-03 17:15:33 +00:00
permittedOutboxTypes = ('Create', 'Announce', 'Like', 'Follow', 'Undo',
'Update', 'Add', 'Remove', 'Block', 'Delete',
'Delegate', 'Skill', 'Bookmark')
2020-01-13 10:35:17 +00:00
if messageJson['type'] not in permittedOutboxTypes:
if debug:
2020-04-03 17:15:33 +00:00
print('DEBUG: POST to outbox - ' + messageJson['type'] +
2020-01-13 10:35:17 +00:00
' is not a permitted activity type')
return False
if messageJson.get('id'):
2020-04-03 17:15:33 +00:00
postId = \
messageJson['id'].replace('/activity', '').replace('/undo', '')
2020-01-13 10:35:17 +00:00
if debug:
print('DEBUG: id attribute exists within POST to outbox')
else:
if debug:
print('DEBUG: No id attribute within POST to outbox')
2020-04-03 17:15:33 +00:00
postId = None
2020-01-13 10:35:17 +00:00
if debug:
print('DEBUG: savePostToBox')
2020-04-03 17:15:33 +00:00
if messageJson['type'] != 'Upgrade':
outboxName = 'outbox'
2020-02-24 22:50:55 +00:00
# if this is a blog post then save to its own box
2020-04-03 17:15:33 +00:00
if messageJson['type'] == 'Create':
2020-02-24 22:50:55 +00:00
if messageJson.get('object'):
if isinstance(messageJson['object'], dict):
if messageJson['object'].get('type'):
2020-04-03 17:15:33 +00:00
if messageJson['object']['type'] == 'Article':
outboxName = 'tlblogs'
2020-04-03 17:15:33 +00:00
savedFilename = \
savePostToBox(baseDir,
httpPrefix,
postId,
postToNickname,
domainFull, messageJson, outboxName)
if messageJson['type'] == 'Create' or \
messageJson['type'] == 'Question' or \
messageJson['type'] == 'Note' or \
messageJson['type'] == 'Article' or \
messageJson['type'] == 'Announce':
indexes = [outboxName, "inbox"]
for boxNameIndex in indexes:
2020-04-03 17:15:33 +00:00
if boxNameIndex == 'inbox' and outboxName == 'tlblogs':
continue
2020-04-03 17:15:33 +00:00
inboxUpdateIndex(boxNameIndex, baseDir,
postToNickname + '@' + domain,
savedFilename, debug)
if outboxAnnounce(recentPostsCache,
baseDir, messageJson, debug):
2020-01-13 10:35:17 +00:00
if debug:
2020-04-03 17:15:33 +00:00
print('DEBUG: Updated announcements (shares) collection ' +
'for the post associated with the Announce activity')
2020-01-13 10:35:17 +00:00
if not server.session:
if debug:
print('DEBUG: creating new session for c2s')
2020-04-03 17:15:33 +00:00
server.session = \
2020-01-13 10:35:17 +00:00
createSession(useTor)
if debug:
print('DEBUG: sending c2s post to followers')
# remove inactive threads
2020-04-03 17:15:33 +00:00
inactiveFollowerThreads = []
2020-01-13 10:35:17 +00:00
for th in followersThreads:
if not th.is_alive():
inactiveFollowerThreads.append(th)
for th in inactiveFollowerThreads:
followersThreads.remove(th)
if debug:
2020-04-03 17:15:33 +00:00
print('DEBUG: ' + str(len(followersThreads)) +
' followers threads active')
2020-01-20 12:43:34 +00:00
# retain up to 200 threads
2020-04-03 17:15:33 +00:00
if len(followersThreads) > 200:
2020-01-13 10:35:17 +00:00
# kill the thread if it is still alive
if followersThreads[0].is_alive():
followersThreads[0].kill()
# remove it from the list
followersThreads.pop(0)
# create a thread to send the post to followers
2020-04-03 17:15:33 +00:00
followersThread = \
sendToFollowersThread(server.session,
baseDir,
postToNickname,
domain, onionDomain,
port, httpPrefix,
federationList,
sendThreads,
postLog,
cachedWebfingers,
personCache,
messageJson, debug,
2020-01-13 10:35:17 +00:00
version)
followersThreads.append(followersThread)
2020-02-04 20:11:19 +00:00
2020-01-13 10:35:17 +00:00
if debug:
print('DEBUG: handle any unfollow requests')
2020-04-03 17:15:33 +00:00
outboxUndoFollow(baseDir, messageJson, debug)
2020-02-04 20:11:19 +00:00
2020-01-13 10:35:17 +00:00
if debug:
print('DEBUG: handle delegation requests')
2020-04-03 17:15:33 +00:00
outboxDelegate(baseDir, postToNickname, messageJson, debug)
2020-02-04 20:11:19 +00:00
2020-01-13 10:35:17 +00:00
if debug:
print('DEBUG: handle skills changes requests')
2020-04-03 17:15:33 +00:00
outboxSkills(baseDir, postToNickname, messageJson, debug)
2020-02-04 20:11:19 +00:00
2020-01-13 10:35:17 +00:00
if debug:
print('DEBUG: handle availability changes requests')
2020-04-03 17:15:33 +00:00
outboxAvailability(baseDir, postToNickname, messageJson, debug)
2020-01-13 10:35:17 +00:00
if debug:
print('DEBUG: handle any like requests')
2020-04-03 17:15:33 +00:00
outboxLike(recentPostsCache,
baseDir, httpPrefix,
postToNickname, domain, port,
messageJson, debug)
2020-01-13 10:35:17 +00:00
if debug:
print('DEBUG: handle any undo like requests')
2020-04-03 17:15:33 +00:00
outboxUndoLike(recentPostsCache,
baseDir, httpPrefix,
postToNickname, domain, port,
messageJson, debug)
2020-01-13 10:35:17 +00:00
if debug:
print('DEBUG: handle any bookmark requests')
2020-04-03 17:15:33 +00:00
outboxBookmark(recentPostsCache,
baseDir, httpPrefix,
postToNickname, domain, port,
messageJson, debug)
2020-01-13 10:35:17 +00:00
if debug:
print('DEBUG: handle any undo bookmark requests')
2020-04-03 17:15:33 +00:00
outboxUndoBookmark(recentPostsCache,
baseDir, httpPrefix,
postToNickname, domain, port,
messageJson, debug)
2020-01-13 10:35:17 +00:00
if debug:
2020-03-22 21:16:02 +00:00
print('DEBUG: handle delete requests')
2020-04-03 17:15:33 +00:00
outboxDelete(baseDir, httpPrefix,
postToNickname, domain,
messageJson, debug,
2020-01-13 10:35:17 +00:00
allowDeletion)
2020-02-04 20:11:19 +00:00
2020-01-13 10:35:17 +00:00
if debug:
print('DEBUG: handle block requests')
2020-04-03 17:15:33 +00:00
outboxBlock(baseDir, httpPrefix,
postToNickname, domain,
2020-01-13 10:35:17 +00:00
port,
2020-04-03 17:15:33 +00:00
messageJson, debug)
2020-02-04 20:11:19 +00:00
2020-01-13 10:35:17 +00:00
if debug:
print('DEBUG: handle undo block requests')
2020-04-03 17:15:33 +00:00
outboxUndoBlock(baseDir, httpPrefix,
postToNickname, domain,
port, messageJson, debug)
2020-02-04 20:11:19 +00:00
2020-01-13 10:35:17 +00:00
if debug:
print('DEBUG: handle share uploads')
2020-04-03 17:15:33 +00:00
outboxShareUpload(baseDir, httpPrefix,
postToNickname, domain,
port, messageJson, debug)
2020-02-04 20:11:19 +00:00
2020-01-13 10:35:17 +00:00
if debug:
print('DEBUG: handle undo share uploads')
2020-04-03 17:15:33 +00:00
outboxUndoShareUpload(baseDir, httpPrefix,
postToNickname, domain,
port, messageJson, debug)
2020-02-04 20:11:19 +00:00
2020-01-13 10:35:17 +00:00
if debug:
print('DEBUG: sending c2s post to named addresses')
2020-02-04 20:11:19 +00:00
if messageJson.get('to'):
2020-04-03 17:15:33 +00:00
print('c2s sender: ' +
postToNickname + '@' + domain + ':' + str(port) +
' recipient: ' + str(messageJson['to']))
2020-02-04 20:11:19 +00:00
else:
2020-04-03 17:15:33 +00:00
print('c2s sender: ' +
postToNickname + '@' + domain + ':' + str(port))
sendToNamedAddresses(server.session, baseDir,
postToNickname,
domain, onionDomain, port,
httpPrefix,
federationList,
sendThreads,
postLog,
cachedWebfingers,
personCache,
messageJson, debug,
2020-02-21 13:18:14 +00:00
version)
2020-01-13 10:35:17 +00:00
return True