From 68dc8de476446cc6f66c6ce26fb85d6be8f410ab Mon Sep 17 00:00:00 2001
From: Patrick Jentsch <p.jentsch@uni-bielefeld.de>
Date: Tue, 21 Feb 2023 16:23:10 +0100
Subject: [PATCH] Add function to dynamically add followers to
 CorpusFollowerList

---
 app/corpora/routes.py                         | 10 ++++++
 app/models.py                                 | 32 +++++++++----------
 .../js/ResourceLists/CorpusFollowerList.js    |  4 +--
 app/static/js/ResourceLists/ResourceList.js   |  3 +-
 4 files changed, 30 insertions(+), 19 deletions(-)

diff --git a/app/corpora/routes.py b/app/corpora/routes.py
index 17d203f1..0b2c788e 100644
--- a/app/corpora/routes.py
+++ b/app/corpora/routes.py
@@ -31,6 +31,16 @@ from .forms import (
 )
 
 
+@bp.route('/fake-add')
+@login_required
+def fake_add():
+    pjentsch = User.query.filter_by(username='pjentsch').first()
+    alice = Corpus.query.filter_by(title='Alice in Wonderland').first()
+    pjentsch.follow_corpus(alice)
+    db.session.commit()
+    return ''
+
+
 @bp.route('/<hashid:corpus_id>/is_public/enable', methods=['POST'])
 @login_required
 def enable_corpus_is_public(corpus_id):
diff --git a/app/models.py b/app/models.py
index 4e40793b..e2706eb5 100644
--- a/app/models.py
+++ b/app/models.py
@@ -1489,22 +1489,22 @@ def ressource_after_insert_handler(mapper, connection, ressource):
     socketio.emit('PATCH', jsonpatch, room=room)
 
 
-# @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)
-#     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)
+@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)
+    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)
 
 
 @db.event.listens_for(Corpus, 'after_update')
diff --git a/app/static/js/ResourceLists/CorpusFollowerList.js b/app/static/js/ResourceLists/CorpusFollowerList.js
index 616f3793..be7e46d9 100644
--- a/app/static/js/ResourceLists/CorpusFollowerList.js
+++ b/app/static/js/ResourceLists/CorpusFollowerList.js
@@ -175,8 +175,8 @@ class CorpusFollowerList extends ResourceList {
     for (let operation of filteredPatch) {
       switch(operation.op) {
         case 'add': {
-          // let re = new RegExp(`^/users/${this.userId}/corpora/${this.corpusId}/corpus_follower_associations/([A-Za-z0-9]*)$`);
-          // if (re.test(operation.path)) {this.add(operation.value);}
+          let re = new RegExp(`^/users/${this.userId}/corpora/${this.corpusId}/corpus_follower_associations/([A-Za-z0-9]*)$`);
+          if (re.test(operation.path)) {this.add(operation.value);}
           break;
         }
         case 'remove': {
diff --git a/app/static/js/ResourceLists/ResourceList.js b/app/static/js/ResourceLists/ResourceList.js
index b7445553..3251ef2b 100644
--- a/app/static/js/ResourceLists/ResourceList.js
+++ b/app/static/js/ResourceLists/ResourceList.js
@@ -43,7 +43,8 @@ class ResourceList {
   }
 
   add(resources, callback) {
-    let values = resources.map((resource) => {
+    let tmp = Array.isArray(resources) ? resources : [resources];
+    let values = tmp.map((resource) => {
       return this.mapResourceToValue(resource);
     });
     this.listjs.add(values, (items) => {
-- 
GitLab