Add ignores collection to make mutes visible to c2s

merge-requests/20/merge
Bob Mottram 2021-03-21 10:45:24 +00:00
parent 44df9eabaf
commit d98dc388d8
5 changed files with 97 additions and 22 deletions

View File

@ -11,6 +11,7 @@ import json
from datetime import datetime
from utils import getCachedPostFilename
from utils import loadJson
from utils import saveJson
from utils import fileLastModified
from utils import setConfigParam
from utils import hasUsersPath
@ -364,8 +365,9 @@ def outboxUndoBlock(baseDir: str, httpPrefix: str,
print('DEBUG: post undo blocked via c2s - ' + postFilename)
def mutePost(baseDir: str, nickname: str, domain: str, postId: str,
recentPostsCache: {}) -> None:
def mutePost(baseDir: str, nickname: str, domain: str, port: int,
httpPrefix: str, postId: str, recentPostsCache: {},
debug: bool) -> None:
""" Mutes the given post
"""
postFilename = locatePost(baseDir, nickname, domain, postId)
@ -375,6 +377,42 @@ def mutePost(baseDir: str, nickname: str, domain: str, postId: str,
if not postJsonObject:
return
if postJsonObject.get('object'):
if isinstance(postJsonObject['object'], dict):
domainFull = getFullDomain(domain, port)
actor = httpPrefix + '://' + domainFull + '/users/' + nickname
# does this post have ignores on it from differenent actors?
if not postJsonObject['object'].get('ignores'):
if debug:
print('DEBUG: Adding initial mute to ' + postId)
ignoresJson = {
"@context": "https://www.w3.org/ns/activitystreams",
'id': postId,
'type': 'Collection',
"totalItems": 1,
'items': [{
'type': 'Ignore',
'actor': actor
}]
}
postJsonObject['object']['ignores'] = ignoresJson
else:
if not postJsonObject['object']['ignores'].get('items'):
postJsonObject['object']['ignores']['items'] = []
itemsList = postJsonObject['object']['ignores']['items']
for ignoresItem in itemsList:
if ignoresItem.get('actor'):
if ignoresItem['actor'] == actor:
return
newIgnore = {
'type': 'Ignore',
'actor': actor
}
igIt = len(itemsList)
itemsList.append(newIgnore)
postJsonObject['object']['ignores']['totalItems'] = igIt
saveJson(postJsonObject, postFilename)
# remove cached post so that the muted version gets recreated
# without its content text and/or image
cachedPostFilename = \
@ -405,8 +443,9 @@ def mutePost(baseDir: str, nickname: str, domain: str, postId: str,
' marked as muted in recent posts memory cache')
def unmutePost(baseDir: str, nickname: str, domain: str, postId: str,
recentPostsCache: {}) -> None:
def unmutePost(baseDir: str, nickname: str, domain: str, port: int,
httpPrefix: str, postId: str, recentPostsCache: {},
debug: bool) -> None:
""" Unmutes the given post
"""
postFilename = locatePost(baseDir, nickname, domain, postId)
@ -421,6 +460,32 @@ def unmutePost(baseDir: str, nickname: str, domain: str, postId: str,
os.remove(muteFilename)
print('UNMUTE: ' + muteFilename + ' file removed')
if postJsonObject.get('object'):
if isinstance(postJsonObject['object'], dict):
if postJsonObject['object'].get('ignores'):
domainFull = getFullDomain(domain, port)
actor = httpPrefix + '://' + domainFull + '/users/' + nickname
totalItems = 0
if postJsonObject['object']['ignores'].get('totalItems'):
totalItems = \
postJsonObject['object']['ignores']['totalItems']
itemsList = postJsonObject['object']['ignores']['items']
for ignoresItem in itemsList:
if ignoresItem.get('actor'):
if ignoresItem['actor'] == actor:
if debug:
print('DEBUG: mute was removed for ' + actor)
itemsList.remove(ignoresItem)
break
if totalItems == 1:
if debug:
print('DEBUG: mute was removed from post')
del postJsonObject['object']['ignores']
else:
igItLen = len(postJsonObject['object']['ignores']['items'])
postJsonObject['object']['ignores']['totalItems'] = igItLen
saveJson(postJsonObject, postFilename)
# remove cached post so that the muted version gets recreated
# with its content text and/or image
cachedPostFilename = \
@ -493,8 +558,9 @@ def outboxMute(baseDir: str, httpPrefix: str,
print('WARN: unable to find nickname in ' + messageJson['object'])
return
mutePost(baseDir, nickname, domain,
messageJson['object'], recentPostsCache)
mutePost(baseDir, nickname, domain, port,
httpPrefix, messageJson['object'], recentPostsCache,
debug)
if debug:
print('DEBUG: post muted via c2s - ' + postFilename)
@ -553,9 +619,9 @@ def outboxUndoMute(baseDir: str, httpPrefix: str,
messageJson['object']['object'])
return
unmutePost(baseDir, nickname, domain,
messageJson['object']['object'],
recentPostsCache)
unmutePost(baseDir, nickname, domain, port,
httpPrefix, messageJson['object']['object'],
recentPostsCache, debug)
if debug:
print('DEBUG: post undo mute via c2s - ' + postFilename)

View File

@ -185,14 +185,14 @@ def updateBookmarksCollection(recentPostsCache: {},
if bookmarkItem.get('actor'):
if bookmarkItem['actor'] == actor:
return
newBookmark = {
'type': 'Bookmark',
'actor': actor
}
nb = newBookmark
bmIt = len(postJsonObject['object']['bookmarks']['items'])
postJsonObject['object']['bookmarks']['items'].append(nb)
postJsonObject['object']['bookmarks']['totalItems'] = bmIt
newBookmark = {
'type': 'Bookmark',
'actor': actor
}
nb = newBookmark
bmIt = len(postJsonObject['object']['bookmarks']['items'])
postJsonObject['object']['bookmarks']['items'].append(nb)
postJsonObject['object']['bookmarks']['totalItems'] = bmIt
if debug:
print('DEBUG: saving post with bookmarks added')

View File

@ -471,6 +471,8 @@ class PubServer(BaseHTTPRequestHandler):
postJsonObject['replies'] = {}
if postJsonObject.get('bookmarks'):
postJsonObject['bookmarks'] = {}
if postJsonObject.get('ignores'):
postJsonObject['ignores'] = {}
if not postJsonObject.get('object'):
return
if not isinstance(postJsonObject['object'], dict):
@ -483,6 +485,8 @@ class PubServer(BaseHTTPRequestHandler):
postJsonObject['object']['replies'] = {}
if postJsonObject['object'].get('bookmarks'):
postJsonObject['object']['bookmarks'] = {}
if postJsonObject['object'].get('ignores'):
postJsonObject['object']['ignores'] = {}
def _requestHTTP(self) -> bool:
"""Should a http response be given?
@ -7010,8 +7014,9 @@ class PubServer(BaseHTTPRequestHandler):
actor = \
httpPrefix + '://' + domainFull + path.split('?mute=')[0]
nickname = getNicknameFromActor(actor)
mutePost(baseDir, nickname, domain,
muteUrl, self.server.recentPostsCache)
mutePost(baseDir, nickname, domain, port,
httpPrefix, muteUrl,
self.server.recentPostsCache, debug)
self.server.GETbusy = False
if callingDomain.endswith('.onion') and onionDomain:
actor = \
@ -7054,8 +7059,9 @@ class PubServer(BaseHTTPRequestHandler):
actor = \
httpPrefix + '://' + domainFull + path.split('?unmute=')[0]
nickname = getNicknameFromActor(actor)
unmutePost(baseDir, nickname, domain,
muteUrl, self.server.recentPostsCache)
unmutePost(baseDir, nickname, domain, port,
httpPrefix, muteUrl,
self.server.recentPostsCache, debug)
self.server.GETbusy = False
if callingDomain.endswith('.onion') and onionDomain:
actor = \

View File

@ -3273,6 +3273,8 @@ def _createBoxIndexed(recentPostsCache: {},
p['shares'] = {}
if p['object'].get('bookmarks'):
p['bookmarks'] = {}
if p['object'].get('ignores'):
p['ignores'] = {}
boxItems['orderedItems'].append(p)

View File

@ -1359,7 +1359,8 @@ def _isReservedName(nickname: str) -> bool:
'updates', 'repeat', 'announce',
'shares', 'fonts', 'icons', 'avatars',
'welcome', 'helpimages',
'bookmark', 'bookmarks', 'tlbookmarks')
'bookmark', 'bookmarks', 'tlbookmarks',
'ignores')
if nickname in reservedNames:
return True
return False