Merge branch 'main' of ssh://code.freedombone.net:2222/bashrc/epicyon

main
Bob Mottram 2021-05-07 23:53:48 +01:00
commit b2e796addb
3 changed files with 126 additions and 117 deletions

View File

@ -40,7 +40,7 @@ def outboxAnnounce(recentPostsCache: {},
return False return False
nickname = getNicknameFromActor(messageJson['actor']) nickname = getNicknameFromActor(messageJson['actor'])
if not nickname: if not nickname:
print('WARN: no nickname found in '+messageJson['actor']) print('WARN: no nickname found in ' + messageJson['actor'])
return False return False
domain, port = getDomainFromActor(messageJson['actor']) domain, port = getDomainFromActor(messageJson['actor'])
postFilename = locatePost(baseDir, nickname, domain, postFilename = locatePost(baseDir, nickname, domain,
@ -49,7 +49,7 @@ def outboxAnnounce(recentPostsCache: {},
updateAnnounceCollection(recentPostsCache, baseDir, postFilename, updateAnnounceCollection(recentPostsCache, baseDir, postFilename,
messageJson['actor'], domain, debug) messageJson['actor'], domain, debug)
return True return True
if messageJson['type'] == 'Undo': elif messageJson['type'] == 'Undo':
if not isinstance(messageJson['object'], dict): if not isinstance(messageJson['object'], dict):
return False return False
if not messageJson['object'].get('type'): if not messageJson['object'].get('type'):
@ -73,26 +73,15 @@ def outboxAnnounce(recentPostsCache: {},
return False return False
def announcedByPerson(postJsonObject: {}, nickname: str, domain: str) -> bool: def announcedByPerson(isAnnounced: bool, postActor: str,
nickname: str, domainFull: str) -> bool:
"""Returns True if the given post is announced by the given person """Returns True if the given post is announced by the given person
""" """
if not postJsonObject.get('object'): if not postActor:
return False return False
if not isinstance(postJsonObject['object'], dict): if isAnnounced and \
return False postActor.endswith(domainFull + '/users/' + nickname):
# not to be confused with shared items return True
if not postJsonObject['object'].get('shares'):
return False
if not isinstance(postJsonObject['object']['shares'], dict):
return False
if not postJsonObject['object']['shares'].get('items'):
return False
if not isinstance(postJsonObject['object']['shares']['items'], list):
return False
actorMatch = domain + '/users/' + nickname
for item in postJsonObject['object']['shares']['items']:
if item['actor'].endswith(actorMatch):
return True
return False return False
@ -124,7 +113,7 @@ def createAnnounce(session, baseDir: str, federationList: [],
'/statuses/' + statusNumber '/statuses/' + statusNumber
newAnnounce = { newAnnounce = {
"@context": "https://www.w3.org/ns/activitystreams", "@context": "https://www.w3.org/ns/activitystreams",
'actor': httpPrefix+'://'+fullDomain+'/users/'+nickname, 'actor': httpPrefix + '://' + fullDomain + '/users/' + nickname,
'atomUri': atomUriStr, 'atomUri': atomUriStr,
'cc': [], 'cc': [],
'id': newAnnounceId + '/activity', 'id': newAnnounceId + '/activity',

109
utils.py
View File

@ -1985,63 +1985,64 @@ def updateAnnounceCollection(recentPostsCache: {},
It's shares of posts, not shares of physical objects. It's shares of posts, not shares of physical objects.
""" """
postJsonObject = loadJson(postFilename) postJsonObject = loadJson(postFilename)
if postJsonObject: if not postJsonObject:
# remove any cached version of this announce so that the announce return
# icon is changed # remove any cached version of this announce so that the announce
nickname = getNicknameFromActor(actor) # icon is changed
cachedPostFilename = getCachedPostFilename(baseDir, nickname, domain, nickname = getNicknameFromActor(actor)
postJsonObject) cachedPostFilename = getCachedPostFilename(baseDir, nickname, domain,
if cachedPostFilename: postJsonObject)
if os.path.isfile(cachedPostFilename): if cachedPostFilename:
os.remove(cachedPostFilename) if os.path.isfile(cachedPostFilename):
removePostFromCache(postJsonObject, recentPostsCache) os.remove(cachedPostFilename)
removePostFromCache(postJsonObject, recentPostsCache)
if not postJsonObject.get('object'):
if debug:
pprint(postJsonObject)
print('DEBUG: post ' + postFilename + ' has no object')
return
if not isinstance(postJsonObject['object'], dict):
return
postUrl = removeIdEnding(postJsonObject['id']) + '/shares'
if not postJsonObject['object'].get('shares'):
if debug:
print('DEBUG: Adding initial shares (announcements) to ' +
postUrl)
announcementsJson = {
"@context": "https://www.w3.org/ns/activitystreams",
'id': postUrl,
'type': 'Collection',
"totalItems": 1,
'items': [{
'type': 'Announce',
'actor': actor
}]
}
postJsonObject['object']['shares'] = announcementsJson
else:
if postJsonObject['object']['shares'].get('items'):
sharesItems = postJsonObject['object']['shares']['items']
for announceItem in sharesItems:
if announceItem.get('actor'):
if announceItem['actor'] == actor:
return
newAnnounce = {
'type': 'Announce',
'actor': actor
}
postJsonObject['object']['shares']['items'].append(newAnnounce)
itlen = len(postJsonObject['object']['shares']['items'])
postJsonObject['object']['shares']['totalItems'] = itlen
else:
if debug:
print('DEBUG: shares (announcements) section of post ' +
'has no items list')
if not postJsonObject.get('object'):
if debug: if debug:
print('DEBUG: saving post with shares (announcements) added')
pprint(postJsonObject) pprint(postJsonObject)
saveJson(postJsonObject, postFilename) print('DEBUG: post ' + postFilename + ' has no object')
return
if not isinstance(postJsonObject['object'], dict):
return
postUrl = removeIdEnding(postJsonObject['id']) + '/shares'
if not postJsonObject['object'].get('shares'):
if debug:
print('DEBUG: Adding initial shares (announcements) to ' +
postUrl)
announcementsJson = {
"@context": "https://www.w3.org/ns/activitystreams",
'id': postUrl,
'type': 'Collection',
"totalItems": 1,
'items': [{
'type': 'Announce',
'actor': actor
}]
}
postJsonObject['object']['shares'] = announcementsJson
else:
if postJsonObject['object']['shares'].get('items'):
sharesItems = postJsonObject['object']['shares']['items']
for announceItem in sharesItems:
if announceItem.get('actor'):
if announceItem['actor'] == actor:
return
newAnnounce = {
'type': 'Announce',
'actor': actor
}
postJsonObject['object']['shares']['items'].append(newAnnounce)
itlen = len(postJsonObject['object']['shares']['items'])
postJsonObject['object']['shares']['totalItems'] = itlen
else:
if debug:
print('DEBUG: shares (announcements) section of post ' +
'has no items list')
if debug:
print('DEBUG: saving post with shares (announcements) added')
pprint(postJsonObject)
saveJson(postJsonObject, postFilename)
def weekDayOfMonthStart(monthNumber: int, year: int) -> int: def weekDayOfMonthStart(monthNumber: int, year: int) -> int:

View File

@ -21,6 +21,7 @@ from posts import postIsMuted
from posts import getPersonBox from posts import getPersonBox
from posts import downloadAnnounce from posts import downloadAnnounce
from posts import populateRepliesJson from posts import populateRepliesJson
from utils import updateAnnounceCollection
from utils import isPGPEncrypted from utils import isPGPEncrypted
from utils import isDM from utils import isDM
from utils import rejectPostId from utils import rejectPostId
@ -384,7 +385,9 @@ def _getEditIconHtml(baseDir: str, nickname: str, domainFull: str,
return editStr return editStr
def _getAnnounceIconHtml(nickname: str, domainFull: str, def _getAnnounceIconHtml(isAnnounced: bool,
postActor: str,
nickname: str, domainFull: str,
postJsonObject: {}, postJsonObject: {},
isPublicRepeat: bool, isPublicRepeat: bool,
isModerationPost: bool, isModerationPost: bool,
@ -396,35 +399,43 @@ def _getAnnounceIconHtml(nickname: str, domainFull: str,
"""Returns html for announce icon/button """Returns html for announce icon/button
""" """
announceStr = '' announceStr = ''
if not isModerationPost and showRepeatIcon:
# don't allow announce/repeat of your own posts if not showRepeatIcon:
announceIcon = 'repeat_inactive.png' return announceStr
announceLink = 'repeat'
announceEmoji = '' if isModerationPost:
return announceStr
# don't allow announce/repeat of your own posts
announceIcon = 'repeat_inactive.png'
announceLink = 'repeat'
announceEmoji = ''
if not isPublicRepeat:
announceLink = 'repeatprivate'
announceTitle = translate['Repeat this post']
if announcedByPerson(isAnnounced,
postActor, nickname, domainFull):
announceIcon = 'repeat.png'
announceEmoji = '🔁 '
announceLink = 'unrepeat'
if not isPublicRepeat: if not isPublicRepeat:
announceLink = 'repeatprivate' announceLink = 'unrepeatprivate'
announceTitle = translate['Repeat this post'] announceTitle = translate['Undo the repeat']
if announcedByPerson(postJsonObject, nickname, domainFull): announceStr = \
announceIcon = 'repeat.png' ' <a class="imageAnchor" href="/users/' + \
announceEmoji = '🔁 ' nickname + '?' + announceLink + \
if not isPublicRepeat: '=' + postJsonObject['object']['id'] + pageNumberParam + \
announceLink = 'unrepeatprivate' '?actor=' + postJsonObject['actor'] + \
announceTitle = translate['Undo the repeat'] '?bm=' + timelinePostBookmark + \
'?tl=' + boxName + '" title="' + announceTitle + '">\n'
announceStr = \ announceStr += \
' <a class="imageAnchor" href="/users/' + \ ' ' + \
nickname + '?' + announceLink + \ '<img loading="lazy" title="' + announceTitle + \
'=' + postJsonObject['object']['id'] + pageNumberParam + \ '" alt="' + announceEmoji + announceTitle + \
'?actor=' + postJsonObject['actor'] + \ ' |" src="/icons/' + announceIcon + '"/></a>\n'
'?bm=' + timelinePostBookmark + \
'?tl=' + boxName + '" title="' + announceTitle + '">\n'
announceStr += \
' ' + \
'<img loading="lazy" title="' + translate['Repeat this post'] + \
'" alt="' + announceEmoji + translate['Repeat this post'] + \
' |" src="/icons/' + announceIcon + '"/></a>\n'
return announceStr return announceStr
@ -1295,21 +1306,27 @@ def individualPostAsHtml(allowDownloads: bool,
return '' return ''
postJsonObject = postJsonAnnounce postJsonObject = postJsonAnnounce
if isRecentPost(postJsonObject): announceFilename = \
announceFilename = \ locatePost(baseDir, nickname, domain,
locatePost(baseDir, nickname, domain, postJsonObject['id'])
postJsonObject['id']) if announceFilename:
if announceFilename and postJsonObject.get('actor'): updateAnnounceCollection(recentPostsCache,
if not os.path.isfile(announceFilename + '.tts'): baseDir, announceFilename,
updateSpeaker(baseDir, httpPrefix, postActor, domainFull, False)
nickname, domain, domainFull,
postJsonObject, personCache, # create a file for use by text-to-speech
translate, postJsonObject['actor'], if isRecentPost(postJsonObject):
themeName) if postJsonObject.get('actor'):
ttsFile = open(announceFilename + '.tts', "w+") if not os.path.isfile(announceFilename + '.tts'):
if ttsFile: updateSpeaker(baseDir, httpPrefix,
ttsFile.write('\n') nickname, domain, domainFull,
ttsFile.close() postJsonObject, personCache,
translate, postJsonObject['actor'],
themeName)
ttsFile = open(announceFilename + '.tts', "w+")
if ttsFile:
ttsFile.write('\n')
ttsFile.close()
isAnnounced = True isAnnounced = True
@ -1394,7 +1411,9 @@ def individualPostAsHtml(allowDownloads: bool,
translate, isEvent) translate, isEvent)
announceStr = \ announceStr = \
_getAnnounceIconHtml(nickname, domainFull, _getAnnounceIconHtml(isAnnounced,
postActor,
nickname, domainFull,
postJsonObject, postJsonObject,
isPublicRepeat, isPublicRepeat,
isModerationPost, isModerationPost,