epicyon/video.py

179 lines
6.1 KiB
Python
Raw Normal View History

__filename__ = "video.py"
__author__ = "Bob Mottram"
__license__ = "AGPL3+"
__version__ = "1.2.0"
__maintainer__ = "Bob Mottram"
__email__ = "bob@libreserver.org"
__status__ = "Production"
__module_group__ = "Timeline"
2021-12-26 12:45:03 +00:00
from utils import get_full_domain
2021-12-27 22:19:18 +00:00
from utils import get_nickname_from_actor
2021-12-27 19:05:25 +00:00
from utils import get_domain_from_actor
2021-12-27 11:20:57 +00:00
from utils import remove_id_ending
2021-12-29 21:55:09 +00:00
from blocking import is_blocked
from filters import is_filtered
2021-12-29 21:55:09 +00:00
def convert_video_to_note(base_dir: str, nickname: str, domain: str,
system_language: str,
post_json_object: {}, blockedCache: {}) -> {}:
"""Converts a PeerTube Video ActivityPub(ish) object into
a Note, so that it can then be displayed in a timeline
"""
# check that the required fields are present
requiredFields = (
'type', '@context', 'id', 'published', 'to', 'cc',
'attributedTo', 'commentsEnabled', 'content', 'sensitive',
'name', 'url'
)
for fieldName in requiredFields:
2021-12-25 22:09:19 +00:00
if not post_json_object.get(fieldName):
return None
2021-12-25 22:09:19 +00:00
if post_json_object['type'] != 'Video':
return None
# who is this attributed to ?
attributedTo = None
2021-12-25 22:09:19 +00:00
if isinstance(post_json_object['attributedTo'], str):
attributedTo = post_json_object['attributedTo']
elif isinstance(post_json_object['attributedTo'], list):
for entity in post_json_object['attributedTo']:
if not isinstance(entity, dict):
continue
if not entity.get('type'):
continue
if entity['type'] != 'Person':
continue
if not entity.get('id'):
continue
attributedTo = entity['id']
break
if not attributedTo:
return None
# get the language of the video
2021-12-25 23:03:28 +00:00
postLanguage = system_language
2021-12-25 22:09:19 +00:00
if post_json_object.get('language'):
if isinstance(post_json_object['language'], dict):
if post_json_object['language'].get('identifier'):
postLanguage = post_json_object['language']['identifier']
# check that the attributed actor is not blocked
2021-12-27 22:19:18 +00:00
postNickname = get_nickname_from_actor(attributedTo)
if not postNickname:
return None
2021-12-27 19:05:25 +00:00
postDomain, postDomainPort = get_domain_from_actor(attributedTo)
if not postDomain:
return None
2021-12-26 12:45:03 +00:00
postDomainFull = get_full_domain(postDomain, postDomainPort)
2021-12-29 21:55:09 +00:00
if is_blocked(base_dir, nickname, domain,
postNickname, postDomainFull, blockedCache):
return None
# check that the content is valid
2021-12-29 21:55:09 +00:00
if is_filtered(base_dir, nickname, domain, post_json_object['name']):
return None
2021-12-29 21:55:09 +00:00
if is_filtered(base_dir, nickname, domain, post_json_object['content']):
return None
# get the content
2021-12-25 22:09:19 +00:00
content = '<p><b>' + post_json_object['name'] + '</b></p>'
if post_json_object.get('license'):
if isinstance(post_json_object['license'], dict):
if post_json_object['license'].get('name'):
2021-12-29 21:55:09 +00:00
if is_filtered(base_dir, nickname, domain,
post_json_object['license']['name']):
return None
2021-12-25 22:09:19 +00:00
content += '<p>' + post_json_object['license']['name'] + '</p>'
content += post_json_object['content']
2021-12-27 11:20:57 +00:00
conversationId = remove_id_ending(post_json_object['id'])
mediaType = None
mediaUrl = None
mediaTorrent = None
mediaMagnet = None
2021-12-25 22:09:19 +00:00
for mediaLink in post_json_object['url']:
if not isinstance(mediaLink, dict):
continue
if not mediaLink.get('mediaType'):
continue
if not mediaLink.get('href'):
continue
if mediaLink['mediaType'] == 'application/x-bittorrent':
mediaTorrent = mediaLink['href']
if mediaLink['href'].startswith('magnet:'):
mediaMagnet = mediaLink['href']
if mediaLink['mediaType'] != 'video/mp4' and \
mediaLink['mediaType'] != 'video/ogv':
continue
if not mediaUrl:
mediaType = mediaLink['mediaType']
mediaUrl = mediaLink['href']
if not mediaUrl:
return None
attachment = [{
'mediaType': mediaType,
2021-12-25 22:09:19 +00:00
'name': post_json_object['content'],
'type': 'Document',
'url': mediaUrl
}]
if mediaTorrent or mediaMagnet:
content += '<p>'
if mediaTorrent:
content += '<a href="' + mediaTorrent + '">⇓</a> '
if mediaMagnet:
content += '<a href="' + mediaMagnet + '">🧲</a>'
content += '</p>'
2021-12-27 11:20:57 +00:00
newPostId = remove_id_ending(post_json_object['id'])
newPost = {
2021-12-25 22:09:19 +00:00
'@context': post_json_object['@context'],
2021-09-28 10:28:42 +00:00
'id': newPostId + '/activity',
'type': 'Create',
'actor': attributedTo,
2021-12-25 22:09:19 +00:00
'published': post_json_object['published'],
'to': post_json_object['to'],
'cc': post_json_object['cc'],
'object': {
2021-09-28 10:28:42 +00:00
'id': newPostId,
'conversation': conversationId,
'type': 'Note',
'summary': None,
'inReplyTo': None,
2021-12-25 22:09:19 +00:00
'published': post_json_object['published'],
2021-09-28 10:28:42 +00:00
'url': newPostId,
'attributedTo': attributedTo,
2021-12-25 22:09:19 +00:00
'to': post_json_object['to'],
'cc': post_json_object['cc'],
'sensitive': post_json_object['sensitive'],
2021-09-28 10:28:42 +00:00
'atomUri': newPostId,
'inReplyToAtomUri': None,
2021-12-25 22:09:19 +00:00
'commentsEnabled': post_json_object['commentsEnabled'],
'rejectReplies': not post_json_object['commentsEnabled'],
'mediaType': 'text/html',
'content': content,
'contentMap': {
postLanguage: content
},
'attachment': attachment,
'tag': [],
'replies': {
2021-09-28 10:28:42 +00:00
'id': newPostId + '/replies',
'type': 'Collection',
'first': {
'type': 'CollectionPage',
2021-09-28 10:28:42 +00:00
'partOf': newPostId + '/replies',
'items': []
}
}
}
}
return newPost