From 8c935820e8091398202852dd657de059243670d0 Mon Sep 17 00:00:00 2001
From: Patrick Jentsch <p.jentsch@uni-bielefeld.de>
Date: Mon, 8 May 2023 15:20:42 +0200
Subject: [PATCH] Better and more live updated on corpus follower actions

---
 app/models.py | 39 +++++++++++++++++++++++++--------------
 1 file changed, 25 insertions(+), 14 deletions(-)

diff --git a/app/models.py b/app/models.py
index 2ee313ec..61587dd7 100644
--- a/app/models.py
+++ b/app/models.py
@@ -1687,18 +1687,24 @@ def ressource_after_delete(mapper, connection, ressource):
 @db.event.listens_for(CorpusFollowerAssociation, 'after_delete')
 def corpus_follower_association_after_delete_handler(mapper, connection, ressource):
     corpus_owner_hashid = ressource.corpus.user.hashid
-    corpus_hashid = hashids.encode(ressource.corpus_id)
-    follower_hashid = hashids.encode(ressource.follower_id)
+    corpus_hashid = ressource.corpus.hashid
     # Send a PATCH to the corpus owner
     jsonpatch_path = f'/users/{corpus_owner_hashid}/corpora/{corpus_hashid}/corpus_follower_associations/{ressource.hashid}'
     jsonpatch = [{'op': 'remove', 'path': jsonpatch_path}]
     room = f'/users/{corpus_owner_hashid}'
     socketio.emit('PATCH', jsonpatch, room=room)
-    # Send a PATCH to the follower
-    jsonpatch_path = f'/users/{follower_hashid}/corpus_follower_associations/{ressource.hashid}'
-    jsonpatch = [{'op': 'remove', 'path': jsonpatch_path}]
-    room = f'/users/{follower_hashid}'
-    socketio.emit('PATCH', jsonpatch, room=room)
+    # Send a PATCH to the followers (the deleted follower and others with permission "MANAGE_FOLLOWERS")
+    followers = [
+        x for x in ressource.corpus.corpus_follower_associations
+        if x.follower_id == ressource.follower_id
+            or x.role.has_permission('MANAGE_FOLLOWERS')
+    ]
+    for follower in followers:
+        jsonpatch_path = f'/users/{follower.hashid}/corpus_follower_associations/{ressource.hashid}'
+        jsonpatch = [{'op': 'remove', 'path': jsonpatch_path}]
+        room = f'/users/{follower.hashid}'
+        socketio.emit('PATCH', jsonpatch, room=room)
+
 
 
 @db.event.listens_for(Corpus, 'after_insert')
@@ -1722,19 +1728,24 @@ def ressource_after_insert_handler(mapper, connection, ressource):
 @db.event.listens_for(CorpusFollowerAssociation, 'after_insert')
 def corpus_follower_association_after_insert_handler(mapper, connection, ressource):
     corpus_owner_hashid = ressource.corpus.user.hashid
-    corpus_hashid = hashids.encode(ressource.corpus_id)
-    follower_hashid = hashids.encode(ressource.follower_id)
+    corpus_hashid = ressource.corpus.hashid
     value = ressource.to_json_serializeable()
     # Send a PATCH to the corpus owner
     jsonpatch_path = f'/users/{corpus_owner_hashid}/corpora/{corpus_hashid}/corpus_follower_associations/{ressource.hashid}'
     jsonpatch = [{'op': 'add', 'path': jsonpatch_path, 'value': value}]
     room = f'/users/{corpus_owner_hashid}'
     socketio.emit('PATCH', jsonpatch, room=room)
-    # Send a PATCH to the follower
-    jsonpatch_path = f'/users/{follower_hashid}/corpus_follower_associations/{ressource.hashid}'
-    jsonpatch = [{'op': 'add', 'path': jsonpatch_path, 'value': value}]
-    room = f'/users/{follower_hashid}'
-    socketio.emit('PATCH', jsonpatch, room=room)
+    # Send a PATCH to the followers (the deleted follower and others with permission "MANAGE_FOLLOWERS")
+    followers = [
+        x for x in ressource.corpus.corpus_follower_associations
+        if x.follower_id == ressource.follower_id
+            or x.role.has_permission('MANAGE_FOLLOWERS')
+    ]
+    for follower in followers:
+        jsonpatch_path = f'/users/{follower.hashid}/corpus_follower_associations/{ressource.hashid}'
+        jsonpatch = [{'op': 'add', 'path': jsonpatch_path, 'value': value}]
+        room = f'/users/{follower.hashid}'
+        socketio.emit('PATCH', jsonpatch, room=room)
 
 
 @db.event.listens_for(Corpus, 'after_update')
-- 
GitLab