Simplify the main new post construction function

main
Bob Mottram 2021-06-27 15:58:54 +01:00
parent 29cb994b31
commit 810f4986c9
1 changed files with 145 additions and 156 deletions

301
posts.py
View File

@ -762,50 +762,6 @@ def _addSchedulePost(baseDir: str, nickname: str, domain: str,
scheduleFile.write(indexStr + '\n') scheduleFile.write(indexStr + '\n')
def _appendEventFields(newPost: {},
eventUUID: str, eventStatus: str,
anonymousParticipationEnabled: bool,
repliesModerationOption: str,
category: str,
joinMode: str,
eventDateStr: str,
endDateStr: str,
location: str,
maximumAttendeeCapacity: int,
ticketUrl: str,
subject: str) -> None:
"""Appends Mobilizon-type event fields to a post
"""
if not eventUUID:
return
# add attributes for Mobilizon-type events
newPost['uuid'] = eventUUID
if eventStatus:
newPost['ical:status'] = eventStatus
if anonymousParticipationEnabled:
newPost['anonymousParticipationEnabled'] = \
anonymousParticipationEnabled
if repliesModerationOption:
newPost['repliesModerationOption'] = repliesModerationOption
if category:
newPost['category'] = category
if joinMode:
newPost['joinMode'] = joinMode
newPost['startTime'] = eventDateStr
newPost['endTime'] = endDateStr
if location:
newPost['location'] = location
if maximumAttendeeCapacity:
newPost['maximumAttendeeCapacity'] = maximumAttendeeCapacity
if ticketUrl:
newPost['ticketUrl'] = ticketUrl
if subject:
newPost['name'] = subject
newPost['summary'] = None
newPost['sensitive'] = False
def validContentWarning(cw: str) -> str: def validContentWarning(cw: str) -> str:
"""Returns a validated content warning """Returns a validated content warning
""" """
@ -876,6 +832,131 @@ def _createPostCWFromReply(baseDir: str, nickname: str, domain: str,
return sensitive, summary return sensitive, summary
def _createPostS2S(baseDir: str, nickname: str, domain: str, port: int,
httpPrefix: str, content: str, statusNumber: str,
published: str, newPostId: str, postContext: {},
toRecipients: [], toCC: [], inReplyTo: str,
sensitive: bool, commentsEnabled: bool,
tags: {}, attachImageFilename: str,
mediaType: str, imageDescription: str, city: str,
postObjectType: str, summary: str,
inReplyToAtomUri: str) -> {}:
"""Creates a new server-to-server post
"""
actorUrl = httpPrefix + '://' + domain + '/users/' + nickname
idStr = \
httpPrefix + '://' + domain + '/users/' + nickname + \
'/statuses/' + statusNumber + '/replies'
newPostUrl = \
httpPrefix + '://' + domain + '/@' + nickname + '/' + statusNumber
newPostAttributedTo = \
httpPrefix + '://' + domain + '/users/' + nickname
newPost = {
'@context': postContext,
'id': newPostId + '/activity',
'type': 'Create',
'actor': actorUrl,
'published': published,
'to': toRecipients,
'cc': toCC,
'object': {
'id': newPostId,
'type': postObjectType,
'summary': summary,
'inReplyTo': inReplyTo,
'published': published,
'url': newPostUrl,
'attributedTo': newPostAttributedTo,
'to': toRecipients,
'cc': toCC,
'sensitive': sensitive,
'atomUri': newPostId,
'inReplyToAtomUri': inReplyToAtomUri,
'commentsEnabled': commentsEnabled,
'rejectReplies': not commentsEnabled,
'mediaType': 'text/html',
'content': content,
'contentMap': {
'en': content
},
'attachment': [],
'tag': tags,
'replies': {
'id': idStr,
'type': 'Collection',
'first': {
'type': 'CollectionPage',
'partOf': idStr,
'items': []
}
}
}
}
if attachImageFilename:
newPost['object'] = \
attachMedia(baseDir, httpPrefix, nickname, domain, port,
newPost['object'], attachImageFilename,
mediaType, imageDescription, city)
return newPost
def _createPostC2S(baseDir: str, nickname: str, domain: str, port: int,
httpPrefix: str, content: str, statusNumber: str,
published: str, newPostId: str, postContext: {},
toRecipients: [], toCC: [], inReplyTo: str,
sensitive: bool, commentsEnabled: bool,
tags: {}, attachImageFilename: str,
mediaType: str, imageDescription: str, city: str,
postObjectType: str, summary: str,
inReplyToAtomUri: str) -> {}:
"""Creates a new client-to-server post
"""
idStr = \
httpPrefix + '://' + domain + '/users/' + nickname + \
'/statuses/' + statusNumber + '/replies'
newPostUrl = \
httpPrefix + '://' + domain + '/@' + nickname + '/' + statusNumber
newPost = {
"@context": postContext,
'id': newPostId,
'type': postObjectType,
'summary': summary,
'inReplyTo': inReplyTo,
'published': published,
'url': newPostUrl,
'attributedTo': httpPrefix + '://' + domain + '/users/' + nickname,
'to': toRecipients,
'cc': toCC,
'sensitive': sensitive,
'atomUri': newPostId,
'inReplyToAtomUri': inReplyToAtomUri,
'commentsEnabled': commentsEnabled,
'rejectReplies': not commentsEnabled,
'mediaType': 'text/html',
'content': content,
'contentMap': {
'en': content
},
'attachment': [],
'tag': tags,
'replies': {
'id': idStr,
'type': 'Collection',
'first': {
'type': 'CollectionPage',
'partOf': idStr,
'items': []
}
}
}
if attachImageFilename:
newPost = \
attachMedia(baseDir, httpPrefix, nickname, domain, port,
newPost, attachImageFilename,
mediaType, imageDescription, city)
return newPost
def _createPostBase(baseDir: str, nickname: str, domain: str, port: int, def _createPostBase(baseDir: str, nickname: str, domain: str, port: int,
toUrl: str, ccUrl: str, httpPrefix: str, content: str, toUrl: str, ccUrl: str, httpPrefix: str, content: str,
followersOnly: bool, saveToFile: bool, followersOnly: bool, saveToFile: bool,
@ -1059,119 +1140,27 @@ def _createPostBase(baseDir: str, nickname: str, domain: str, port: int,
postObjectType = 'Article' postObjectType = 'Article'
if not clientToServer: if not clientToServer:
actorUrl = httpPrefix + '://' + domain + '/users/' + nickname newPost = \
_createPostS2S(baseDir, nickname, domain, port,
idStr = \ httpPrefix, content, statusNumber,
httpPrefix + '://' + domain + '/users/' + nickname + \ published, newPostId, postContext,
'/statuses/' + statusNumber + '/replies' toRecipients, toCC, inReplyTo,
newPostUrl = \ sensitive, commentsEnabled,
httpPrefix + '://' + domain + '/@' + nickname + '/' + statusNumber tags, attachImageFilename,
newPostAttributedTo = \ mediaType, imageDescription, city,
httpPrefix + '://' + domain + '/users/' + nickname postObjectType, summary,
newPost = { inReplyToAtomUri)
'@context': postContext,
'id': newPostId + '/activity',
'type': 'Create',
'actor': actorUrl,
'published': published,
'to': toRecipients,
'cc': toCC,
'object': {
'id': newPostId,
'type': postObjectType,
'summary': summary,
'inReplyTo': inReplyTo,
'published': published,
'url': newPostUrl,
'attributedTo': newPostAttributedTo,
'to': toRecipients,
'cc': toCC,
'sensitive': sensitive,
'atomUri': newPostId,
'inReplyToAtomUri': inReplyToAtomUri,
'commentsEnabled': commentsEnabled,
'rejectReplies': not commentsEnabled,
'mediaType': 'text/html',
'content': content,
'contentMap': {
'en': content
},
'attachment': [],
'tag': tags,
'replies': {
'id': idStr,
'type': 'Collection',
'first': {
'type': 'CollectionPage',
'partOf': idStr,
'items': []
}
}
}
}
if attachImageFilename:
newPost['object'] = \
attachMedia(baseDir, httpPrefix, nickname, domain, port,
newPost['object'], attachImageFilename,
mediaType, imageDescription, city)
_appendEventFields(newPost['object'], eventUUID, eventStatus,
anonymousParticipationEnabled,
repliesModerationOption,
category, joinMode,
eventDateStr, endDateStr,
location, maximumAttendeeCapacity,
ticketUrl, subject)
else: else:
idStr = \ newPost = \
httpPrefix + '://' + domain + '/users/' + nickname + \ _createPostC2S(baseDir, nickname, domain, port,
'/statuses/' + statusNumber + '/replies' httpPrefix, content, statusNumber,
newPostUrl = \ published, newPostId, postContext,
httpPrefix + '://' + domain + '/@' + nickname + '/' + statusNumber toRecipients, toCC, inReplyTo,
newPost = { sensitive, commentsEnabled,
"@context": postContext, tags, attachImageFilename,
'id': newPostId, mediaType, imageDescription, city,
'type': postObjectType, postObjectType, summary,
'summary': summary, inReplyToAtomUri)
'inReplyTo': inReplyTo,
'published': published,
'url': newPostUrl,
'attributedTo': httpPrefix + '://' + domain + '/users/' + nickname,
'to': toRecipients,
'cc': toCC,
'sensitive': sensitive,
'atomUri': newPostId,
'inReplyToAtomUri': inReplyToAtomUri,
'commentsEnabled': commentsEnabled,
'rejectReplies': not commentsEnabled,
'mediaType': 'text/html',
'content': content,
'contentMap': {
'en': content
},
'attachment': [],
'tag': tags,
'replies': {
'id': idStr,
'type': 'Collection',
'first': {
'type': 'CollectionPage',
'partOf': idStr,
'items': []
}
}
}
if attachImageFilename:
newPost = \
attachMedia(baseDir, httpPrefix, nickname, domain, port,
newPost, attachImageFilename,
mediaType, imageDescription, city)
_appendEventFields(newPost, eventUUID, eventStatus,
anonymousParticipationEnabled,
repliesModerationOption,
category, joinMode,
eventDateStr, endDateStr,
location, maximumAttendeeCapacity,
ticketUrl, subject)
if ccUrl: if ccUrl:
if len(ccUrl) > 0: if len(ccUrl) > 0:
newPost['cc'] = [ccUrl] newPost['cc'] = [ccUrl]