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 datetime import datetime
from utils import getCachedPostFilename from utils import getCachedPostFilename
from utils import loadJson from utils import loadJson
from utils import saveJson
from utils import fileLastModified from utils import fileLastModified
from utils import setConfigParam from utils import setConfigParam
from utils import hasUsersPath from utils import hasUsersPath
@ -364,8 +365,9 @@ def outboxUndoBlock(baseDir: str, httpPrefix: str,
print('DEBUG: post undo blocked via c2s - ' + postFilename) print('DEBUG: post undo blocked via c2s - ' + postFilename)
def mutePost(baseDir: str, nickname: str, domain: str, postId: str, def mutePost(baseDir: str, nickname: str, domain: str, port: int,
recentPostsCache: {}) -> None: httpPrefix: str, postId: str, recentPostsCache: {},
debug: bool) -> None:
""" Mutes the given post """ Mutes the given post
""" """
postFilename = locatePost(baseDir, nickname, domain, postId) postFilename = locatePost(baseDir, nickname, domain, postId)
@ -375,6 +377,42 @@ def mutePost(baseDir: str, nickname: str, domain: str, postId: str,
if not postJsonObject: if not postJsonObject:
return 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 # remove cached post so that the muted version gets recreated
# without its content text and/or image # without its content text and/or image
cachedPostFilename = \ cachedPostFilename = \
@ -405,8 +443,9 @@ def mutePost(baseDir: str, nickname: str, domain: str, postId: str,
' marked as muted in recent posts memory cache') ' marked as muted in recent posts memory cache')
def unmutePost(baseDir: str, nickname: str, domain: str, postId: str, def unmutePost(baseDir: str, nickname: str, domain: str, port: int,
recentPostsCache: {}) -> None: httpPrefix: str, postId: str, recentPostsCache: {},
debug: bool) -> None:
""" Unmutes the given post """ Unmutes the given post
""" """
postFilename = locatePost(baseDir, nickname, domain, postId) postFilename = locatePost(baseDir, nickname, domain, postId)
@ -421,6 +460,32 @@ def unmutePost(baseDir: str, nickname: str, domain: str, postId: str,
os.remove(muteFilename) os.remove(muteFilename)
print('UNMUTE: ' + muteFilename + ' file removed') 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 # remove cached post so that the muted version gets recreated
# with its content text and/or image # with its content text and/or image
cachedPostFilename = \ cachedPostFilename = \
@ -493,8 +558,9 @@ def outboxMute(baseDir: str, httpPrefix: str,
print('WARN: unable to find nickname in ' + messageJson['object']) print('WARN: unable to find nickname in ' + messageJson['object'])
return return
mutePost(baseDir, nickname, domain, mutePost(baseDir, nickname, domain, port,
messageJson['object'], recentPostsCache) httpPrefix, messageJson['object'], recentPostsCache,
debug)
if debug: if debug:
print('DEBUG: post muted via c2s - ' + postFilename) print('DEBUG: post muted via c2s - ' + postFilename)
@ -553,9 +619,9 @@ def outboxUndoMute(baseDir: str, httpPrefix: str,
messageJson['object']['object']) messageJson['object']['object'])
return return
unmutePost(baseDir, nickname, domain, unmutePost(baseDir, nickname, domain, port,
messageJson['object']['object'], httpPrefix, messageJson['object']['object'],
recentPostsCache) recentPostsCache, debug)
if debug: if debug:
print('DEBUG: post undo mute via c2s - ' + postFilename) print('DEBUG: post undo mute via c2s - ' + postFilename)

View File

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

View File

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

View File

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

View File

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