Browse Source

Add ignores collection to make mutes visible to c2s

merge-requests/20/merge
Bob Mottram 1 month ago
parent
commit
d98dc388d8
5 changed files with 97 additions and 22 deletions
  1. 75
    9
      blocking.py
  2. 8
    8
      bookmarks.py
  3. 10
    4
      daemon.py
  4. 2
    0
      posts.py
  5. 2
    1
      utils.py

+ 75
- 9
blocking.py View File

@@ -11,6 +11,7 @@ import json
11 11
 from datetime import datetime
12 12
 from utils import getCachedPostFilename
13 13
 from utils import loadJson
14
+from utils import saveJson
14 15
 from utils import fileLastModified
15 16
 from utils import setConfigParam
16 17
 from utils import hasUsersPath
@@ -364,8 +365,9 @@ def outboxUndoBlock(baseDir: str, httpPrefix: str,
364 365
         print('DEBUG: post undo blocked via c2s - ' + postFilename)
365 366
 
366 367
 
367
-def mutePost(baseDir: str, nickname: str, domain: str, postId: str,
368
-             recentPostsCache: {}) -> None:
368
+def mutePost(baseDir: str, nickname: str, domain: str, port: int,
369
+             httpPrefix: str, postId: str, recentPostsCache: {},
370
+             debug: bool) -> None:
369 371
     """ Mutes the given post
370 372
     """
371 373
     postFilename = locatePost(baseDir, nickname, domain, postId)
@@ -375,6 +377,42 @@ def mutePost(baseDir: str, nickname: str, domain: str, postId: str,
375 377
     if not postJsonObject:
376 378
         return
377 379
 
380
+    if postJsonObject.get('object'):
381
+        if isinstance(postJsonObject['object'], dict):
382
+            domainFull = getFullDomain(domain, port)
383
+            actor = httpPrefix + '://' + domainFull + '/users/' + nickname
384
+            # does this post have ignores on it from differenent actors?
385
+            if not postJsonObject['object'].get('ignores'):
386
+                if debug:
387
+                    print('DEBUG: Adding initial mute to ' + postId)
388
+                ignoresJson = {
389
+                    "@context": "https://www.w3.org/ns/activitystreams",
390
+                    'id': postId,
391
+                    'type': 'Collection',
392
+                    "totalItems": 1,
393
+                    'items': [{
394
+                        'type': 'Ignore',
395
+                        'actor': actor
396
+                    }]
397
+                }
398
+                postJsonObject['object']['ignores'] = ignoresJson
399
+            else:
400
+                if not postJsonObject['object']['ignores'].get('items'):
401
+                    postJsonObject['object']['ignores']['items'] = []
402
+                itemsList = postJsonObject['object']['ignores']['items']
403
+                for ignoresItem in itemsList:
404
+                    if ignoresItem.get('actor'):
405
+                        if ignoresItem['actor'] == actor:
406
+                            return
407
+                newIgnore = {
408
+                    'type': 'Ignore',
409
+                    'actor': actor
410
+                }
411
+                igIt = len(itemsList)
412
+                itemsList.append(newIgnore)
413
+                postJsonObject['object']['ignores']['totalItems'] = igIt
414
+                saveJson(postJsonObject, postFilename)
415
+
378 416
     # remove cached post so that the muted version gets recreated
379 417
     # without its content text and/or image
380 418
     cachedPostFilename = \
@@ -405,8 +443,9 @@ def mutePost(baseDir: str, nickname: str, domain: str, postId: str,
405 443
                       ' marked as muted in recent posts memory cache')
406 444
 
407 445
 
408
-def unmutePost(baseDir: str, nickname: str, domain: str, postId: str,
409
-               recentPostsCache: {}) -> None:
446
+def unmutePost(baseDir: str, nickname: str, domain: str, port: int,
447
+               httpPrefix: str, postId: str, recentPostsCache: {},
448
+               debug: bool) -> None:
410 449
     """ Unmutes the given post
411 450
     """
412 451
     postFilename = locatePost(baseDir, nickname, domain, postId)
@@ -421,6 +460,32 @@ def unmutePost(baseDir: str, nickname: str, domain: str, postId: str,
421 460
         os.remove(muteFilename)
422 461
         print('UNMUTE: ' + muteFilename + ' file removed')
423 462
 
463
+    if postJsonObject.get('object'):
464
+        if isinstance(postJsonObject['object'], dict):
465
+            if postJsonObject['object'].get('ignores'):
466
+                domainFull = getFullDomain(domain, port)
467
+                actor = httpPrefix + '://' + domainFull + '/users/' + nickname
468
+                totalItems = 0
469
+                if postJsonObject['object']['ignores'].get('totalItems'):
470
+                    totalItems = \
471
+                        postJsonObject['object']['ignores']['totalItems']
472
+                itemsList = postJsonObject['object']['ignores']['items']
473
+                for ignoresItem in itemsList:
474
+                    if ignoresItem.get('actor'):
475
+                        if ignoresItem['actor'] == actor:
476
+                            if debug:
477
+                                print('DEBUG: mute was removed for ' + actor)
478
+                            itemsList.remove(ignoresItem)
479
+                            break
480
+                if totalItems == 1:
481
+                    if debug:
482
+                        print('DEBUG: mute was removed from post')
483
+                    del postJsonObject['object']['ignores']
484
+                else:
485
+                    igItLen = len(postJsonObject['object']['ignores']['items'])
486
+                    postJsonObject['object']['ignores']['totalItems'] = igItLen
487
+                saveJson(postJsonObject, postFilename)
488
+
424 489
     # remove cached post so that the muted version gets recreated
425 490
     # with its content text and/or image
426 491
     cachedPostFilename = \
@@ -493,8 +558,9 @@ def outboxMute(baseDir: str, httpPrefix: str,
493 558
         print('WARN: unable to find nickname in ' + messageJson['object'])
494 559
         return
495 560
 
496
-    mutePost(baseDir, nickname, domain,
497
-             messageJson['object'], recentPostsCache)
561
+    mutePost(baseDir, nickname, domain, port,
562
+             httpPrefix, messageJson['object'], recentPostsCache,
563
+             debug)
498 564
 
499 565
     if debug:
500 566
         print('DEBUG: post muted via c2s - ' + postFilename)
@@ -553,9 +619,9 @@ def outboxUndoMute(baseDir: str, httpPrefix: str,
553 619
               messageJson['object']['object'])
554 620
         return
555 621
 
556
-    unmutePost(baseDir, nickname, domain,
557
-               messageJson['object']['object'],
558
-               recentPostsCache)
622
+    unmutePost(baseDir, nickname, domain, port,
623
+               httpPrefix, messageJson['object']['object'],
624
+               recentPostsCache, debug)
559 625
 
560 626
     if debug:
561 627
         print('DEBUG: post undo mute via c2s - ' + postFilename)

+ 8
- 8
bookmarks.py View File

@@ -185,14 +185,14 @@ def updateBookmarksCollection(recentPostsCache: {},
185 185
                 if bookmarkItem.get('actor'):
186 186
                     if bookmarkItem['actor'] == actor:
187 187
                         return
188
-                    newBookmark = {
189
-                        'type': 'Bookmark',
190
-                        'actor': actor
191
-                    }
192
-                    nb = newBookmark
193
-                    bmIt = len(postJsonObject['object']['bookmarks']['items'])
194
-                    postJsonObject['object']['bookmarks']['items'].append(nb)
195
-                    postJsonObject['object']['bookmarks']['totalItems'] = bmIt
188
+            newBookmark = {
189
+                'type': 'Bookmark',
190
+                'actor': actor
191
+            }
192
+            nb = newBookmark
193
+            bmIt = len(postJsonObject['object']['bookmarks']['items'])
194
+            postJsonObject['object']['bookmarks']['items'].append(nb)
195
+            postJsonObject['object']['bookmarks']['totalItems'] = bmIt
196 196
 
197 197
         if debug:
198 198
             print('DEBUG: saving post with bookmarks added')

+ 10
- 4
daemon.py View File

@@ -471,6 +471,8 @@ class PubServer(BaseHTTPRequestHandler):
471 471
             postJsonObject['replies'] = {}
472 472
         if postJsonObject.get('bookmarks'):
473 473
             postJsonObject['bookmarks'] = {}
474
+        if postJsonObject.get('ignores'):
475
+            postJsonObject['ignores'] = {}
474 476
         if not postJsonObject.get('object'):
475 477
             return
476 478
         if not isinstance(postJsonObject['object'], dict):
@@ -483,6 +485,8 @@ class PubServer(BaseHTTPRequestHandler):
483 485
             postJsonObject['object']['replies'] = {}
484 486
         if postJsonObject['object'].get('bookmarks'):
485 487
             postJsonObject['object']['bookmarks'] = {}
488
+        if postJsonObject['object'].get('ignores'):
489
+            postJsonObject['object']['ignores'] = {}
486 490
 
487 491
     def _requestHTTP(self) -> bool:
488 492
         """Should a http response be given?
@@ -7010,8 +7014,9 @@ class PubServer(BaseHTTPRequestHandler):
7010 7014
         actor = \
7011 7015
             httpPrefix + '://' + domainFull + path.split('?mute=')[0]
7012 7016
         nickname = getNicknameFromActor(actor)
7013
-        mutePost(baseDir, nickname, domain,
7014
-                 muteUrl, self.server.recentPostsCache)
7017
+        mutePost(baseDir, nickname, domain, port,
7018
+                 httpPrefix, muteUrl,
7019
+                 self.server.recentPostsCache, debug)
7015 7020
         self.server.GETbusy = False
7016 7021
         if callingDomain.endswith('.onion') and onionDomain:
7017 7022
             actor = \
@@ -7054,8 +7059,9 @@ class PubServer(BaseHTTPRequestHandler):
7054 7059
         actor = \
7055 7060
             httpPrefix + '://' + domainFull + path.split('?unmute=')[0]
7056 7061
         nickname = getNicknameFromActor(actor)
7057
-        unmutePost(baseDir, nickname, domain,
7058
-                   muteUrl, self.server.recentPostsCache)
7062
+        unmutePost(baseDir, nickname, domain, port,
7063
+                   httpPrefix, muteUrl,
7064
+                   self.server.recentPostsCache, debug)
7059 7065
         self.server.GETbusy = False
7060 7066
         if callingDomain.endswith('.onion') and onionDomain:
7061 7067
             actor = \

+ 2
- 0
posts.py View File

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

+ 2
- 1
utils.py View File

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

Loading…
Cancel
Save