Fixing posts option

main
Bob Mottram 2021-08-01 15:47:31 +01:00
parent b265dbe0b8
commit 9b7add2f24
1 changed files with 110 additions and 90 deletions

200
posts.py
View File

@ -184,7 +184,7 @@ def getUserUrl(wfRequest: {}, sourceId: int = 0, debug: bool = False) -> str:
def parseUserFeed(session, feedUrl: str, asHeader: {}, def parseUserFeed(session, feedUrl: str, asHeader: {},
projectVersion: str, httpPrefix: str, projectVersion: str, httpPrefix: str,
domain: str, debug: bool, depth: int = 0) -> {}: domain: str, debug: bool, depth: int = 0) -> []:
if depth > 10: if depth > 10:
if debug: if debug:
print('Maximum search depth reached') print('Maximum search depth reached')
@ -205,8 +205,9 @@ def parseUserFeed(session, feedUrl: str, asHeader: {},
pprint(feedJson) pprint(feedJson)
if 'orderedItems' in feedJson: if 'orderedItems' in feedJson:
for item in feedJson['orderedItems']: #for item in feedJson['orderedItems']:
yield item # yield item
return feedJson['orderedItems']
nextUrl = None nextUrl = None
if 'first' in feedJson: if 'first' in feedJson:
@ -224,14 +225,17 @@ def parseUserFeed(session, feedUrl: str, asHeader: {},
parseUserFeed(session, nextUrl, asHeader, parseUserFeed(session, nextUrl, asHeader,
projectVersion, httpPrefix, projectVersion, httpPrefix,
domain, debug, depth + 1) domain, debug, depth + 1)
if userFeed: if userFeed:
for item in userFeed: #for item in userFeed:
yield item # yield item
return userFeed
elif isinstance(nextUrl, dict): elif isinstance(nextUrl, dict):
userFeed = nextUrl userFeed = nextUrl
if userFeed.get('orderedItems'): if userFeed.get('orderedItems'):
for item in userFeed['orderedItems']: #for item in userFeed['orderedItems']:
yield item # yield item
return userFeed['orderedItems']
return None
def _getPersonBoxActor(session, baseDir: str, actor: str, def _getPersonBoxActor(session, baseDir: str, actor: str,
@ -411,108 +415,124 @@ def _getPosts(session, outboxUrl: str, maxPosts: int,
if debug: if debug:
print('No type') print('No type')
continue continue
if item['type'] != 'Create': if item['type'] != 'Create' and item['type'] != 'Announce':
if debug: if debug:
print('Not Create type') print('Not Create type')
continue continue
if not hasObjectDict(item): if not isinstance(item, dict):
if debug: if debug:
print('item object is not a dict') print('item object is not a dict')
pprint(item)
continue continue
if not item['object'].get('published'): if item.get('object'):
if debug: if isinstance(item['object'], dict):
print('No published attribute') if not item['object'].get('published'):
continue if debug:
print('No published attribute')
continue
elif isinstance(item['object'], str):
if not item.get('published'):
if debug:
print('No published attribute')
continue
else:
if debug:
print('object is not a dict or string')
continue
if not personPosts.get(item['id']): if not personPosts.get(item['id']):
# check that this is a public post # check that this is a public post
# #Public should appear in the "to" list # #Public should appear in the "to" list
if item['object'].get('to'): if isinstance(item['object'], dict):
isPublic = False if item['object'].get('to'):
for recipient in item['object']['to']: isPublic = False
if recipient.endswith('#Public'): for recipient in item['object']['to']:
isPublic = True if recipient.endswith('#Public'):
break isPublic = True
if not isPublic: break
continue if not isPublic:
continue
elif isinstance(item['object'], str):
if item.get('to'):
isPublic = False
for recipient in item['to']:
if recipient.endswith('#Public'):
isPublic = True
break
if not isPublic:
continue
content = getBaseContentFromPost(item, systemLanguage) content = getBaseContentFromPost(item, systemLanguage)
content = content.replace(''', "'") content = content.replace(''', "'")
mentions = [] mentions = []
emoji = {} emoji = {}
if item['object'].get('tag'): summary = ''
for tagItem in item['object']['tag']: inReplyTo = ''
tagType = tagItem['type'].lower() attachment = []
if tagType == 'emoji': sensitive = False
if tagItem.get('name') and tagItem.get('icon'): if isinstance(item['object'], dict):
if tagItem['icon'].get('url'): if item['object'].get('tag'):
# No emoji from non-permitted domains for tagItem in item['object']['tag']:
if urlPermitted(tagItem['icon']['url'], tagType = tagItem['type'].lower()
if tagType == 'emoji':
if tagItem.get('name') and tagItem.get('icon'):
if tagItem['icon'].get('url'):
# No emoji from non-permitted domains
if urlPermitted(tagItem['icon']['url'],
federationList):
emojiName = tagItem['name']
emojiIcon = tagItem['icon']['url']
emoji[emojiName] = emojiIcon
else:
if debug:
print('url not permitted ' +
tagItem['icon']['url'])
if tagType == 'mention':
if tagItem.get('name'):
if tagItem['name'] not in mentions:
mentions.append(tagItem['name'])
if len(mentions) > maxMentions:
if debug:
print('max mentions reached')
continue
if len(emoji) > maxEmoji:
if debug:
print('max emojis reached')
continue
if item['object'].get('summary'):
if item['object']['summary']:
summary = item['object']['summary']
if item['object'].get('inReplyTo'):
if item['object']['inReplyTo']:
if isinstance(item['object']['inReplyTo'], str):
# No replies to non-permitted domains
if not urlPermitted(item['object']['inReplyTo'],
federationList): federationList):
emojiName = tagItem['name'] if debug:
emojiIcon = tagItem['icon']['url'] print('url not permitted ' +
emoji[emojiName] = emojiIcon item['object']['inReplyTo'])
continue
inReplyTo = item['object']['inReplyTo']
if item['object'].get('attachment'):
if item['object']['attachment']:
for attach in item['object']['attachment']:
if attach.get('name') and attach.get('url'):
# no attachments from non-permitted domains
if urlPermitted(attach['url'],
federationList):
attachment.append([attach['name'],
attach['url']])
else: else:
if debug: if debug:
print('url not permitted ' + print('url not permitted ' +
tagItem['icon']['url']) attach['url'])
if tagType == 'mention':
if tagItem.get('name'):
if tagItem['name'] not in mentions:
mentions.append(tagItem['name'])
if len(mentions) > maxMentions:
if debug:
print('max mentions reached')
continue
if len(emoji) > maxEmoji:
if debug:
print('max emojis reached')
continue
summary = '' sensitive = False
if item['object'].get('summary'): if item['object'].get('sensitive'):
if item['object']['summary']: sensitive = item['object']['sensitive']
summary = item['object']['summary']
inReplyTo = ''
if item['object'].get('inReplyTo'):
if item['object']['inReplyTo']:
if isinstance(item['object']['inReplyTo'], str):
# No replies to non-permitted domains
if not urlPermitted(item['object']['inReplyTo'],
federationList):
if debug:
print('url not permitted ' +
item['object']['inReplyTo'])
continue
inReplyTo = item['object']['inReplyTo']
conversation = ''
if item['object'].get('conversation'):
if item['object']['conversation']:
# no conversations originated in non-permitted domains
if urlPermitted(item['object']['conversation'],
federationList):
conversation = item['object']['conversation']
attachment = []
if item['object'].get('attachment'):
if item['object']['attachment']:
for attach in item['object']['attachment']:
if attach.get('name') and attach.get('url'):
# no attachments from non-permitted domains
if urlPermitted(attach['url'],
federationList):
attachment.append([attach['name'],
attach['url']])
else:
if debug:
print('url not permitted ' +
attach['url'])
sensitive = False
if item['object'].get('sensitive'):
sensitive = item['object']['sensitive']
if simple: if simple:
print(_cleanHtml(content) + '\n') print(_cleanHtml(content) + '\n')