2021-09-13 11:34:56 +00:00
|
|
|
__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-09-13 11:34:56 +00:00
|
|
|
from utils import getNicknameFromActor
|
|
|
|
from utils import getDomainFromActor
|
2021-12-27 11:20:57 +00:00
|
|
|
from utils import remove_id_ending
|
2021-09-13 11:34:56 +00:00
|
|
|
from blocking import isBlocked
|
|
|
|
from filters import isFiltered
|
|
|
|
|
|
|
|
|
2021-12-25 16:17:53 +00:00
|
|
|
def convertVideoToNote(base_dir: str, nickname: str, domain: str,
|
2021-12-25 23:03:28 +00:00
|
|
|
system_language: str,
|
2021-12-25 22:09:19 +00:00
|
|
|
post_json_object: {}, blockedCache: {}) -> {}:
|
2021-09-13 11:34:56 +00:00
|
|
|
"""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):
|
2021-09-13 11:34:56 +00:00
|
|
|
return None
|
|
|
|
|
2021-12-25 22:09:19 +00:00
|
|
|
if post_json_object['type'] != 'Video':
|
2021-09-13 11:34:56 +00:00
|
|
|
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']:
|
2021-09-13 11:34:56 +00:00
|
|
|
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']
|
2021-09-13 11:34:56 +00:00
|
|
|
|
|
|
|
# check that the attributed actor is not blocked
|
|
|
|
postNickname = getNicknameFromActor(attributedTo)
|
|
|
|
if not postNickname:
|
|
|
|
return None
|
|
|
|
postDomain, postDomainPort = getDomainFromActor(attributedTo)
|
|
|
|
if not postDomain:
|
|
|
|
return None
|
2021-12-26 12:45:03 +00:00
|
|
|
postDomainFull = get_full_domain(postDomain, postDomainPort)
|
2021-12-25 16:17:53 +00:00
|
|
|
if isBlocked(base_dir, nickname, domain,
|
2021-09-13 11:34:56 +00:00
|
|
|
postNickname, postDomainFull, blockedCache):
|
|
|
|
return None
|
|
|
|
|
|
|
|
# check that the content is valid
|
2021-12-25 22:09:19 +00:00
|
|
|
if isFiltered(base_dir, nickname, domain, post_json_object['name']):
|
2021-09-13 11:34:56 +00:00
|
|
|
return None
|
2021-12-25 22:09:19 +00:00
|
|
|
if isFiltered(base_dir, nickname, domain, post_json_object['content']):
|
2021-09-13 11:34:56 +00:00
|
|
|
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-25 16:17:53 +00:00
|
|
|
if isFiltered(base_dir, nickname, domain,
|
2021-12-25 22:09:19 +00:00
|
|
|
post_json_object['license']['name']):
|
2021-09-13 11:34:56 +00:00
|
|
|
return None
|
2021-12-25 22:09:19 +00:00
|
|
|
content += '<p>' + post_json_object['license']['name'] + '</p>'
|
|
|
|
content += post_json_object['content']
|
2021-09-13 11:34:56 +00:00
|
|
|
|
2021-12-27 11:20:57 +00:00
|
|
|
conversationId = remove_id_ending(post_json_object['id'])
|
2021-09-13 11:34:56 +00:00
|
|
|
|
|
|
|
mediaType = None
|
|
|
|
mediaUrl = None
|
|
|
|
mediaTorrent = None
|
|
|
|
mediaMagnet = None
|
2021-12-25 22:09:19 +00:00
|
|
|
for mediaLink in post_json_object['url']:
|
2021-09-13 11:34:56 +00:00
|
|
|
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'],
|
2021-09-13 11:34:56 +00:00
|
|
|
'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'])
|
2021-09-13 11:34:56 +00:00
|
|
|
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',
|
2021-09-13 11:34:56 +00:00
|
|
|
'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'],
|
2021-09-13 11:34:56 +00:00
|
|
|
'object': {
|
2021-09-28 10:28:42 +00:00
|
|
|
'id': newPostId,
|
2021-09-13 11:34:56 +00:00
|
|
|
'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,
|
2021-09-13 11:34:56 +00:00
|
|
|
'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,
|
2021-09-13 11:34:56 +00:00
|
|
|
'inReplyToAtomUri': None,
|
2021-12-25 22:09:19 +00:00
|
|
|
'commentsEnabled': post_json_object['commentsEnabled'],
|
|
|
|
'rejectReplies': not post_json_object['commentsEnabled'],
|
2021-09-13 11:34:56 +00:00
|
|
|
'mediaType': 'text/html',
|
|
|
|
'content': content,
|
|
|
|
'contentMap': {
|
|
|
|
postLanguage: content
|
|
|
|
},
|
|
|
|
'attachment': attachment,
|
|
|
|
'tag': [],
|
|
|
|
'replies': {
|
2021-09-28 10:28:42 +00:00
|
|
|
'id': newPostId + '/replies',
|
2021-09-13 11:34:56 +00:00
|
|
|
'type': 'Collection',
|
|
|
|
'first': {
|
|
|
|
'type': 'CollectionPage',
|
2021-09-28 10:28:42 +00:00
|
|
|
'partOf': newPostId + '/replies',
|
2021-09-13 11:34:56 +00:00
|
|
|
'items': []
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return newPost
|