Better and more live updated on corpus follower actions

This commit is contained in:
Patrick Jentsch 2023-05-08 15:20:42 +02:00
parent e4593d5922
commit 8c935820e8

View File

@ -1687,18 +1687,24 @@ def ressource_after_delete(mapper, connection, ressource):
@db.event.listens_for(CorpusFollowerAssociation, 'after_delete') @db.event.listens_for(CorpusFollowerAssociation, 'after_delete')
def corpus_follower_association_after_delete_handler(mapper, connection, ressource): def corpus_follower_association_after_delete_handler(mapper, connection, ressource):
corpus_owner_hashid = ressource.corpus.user.hashid corpus_owner_hashid = ressource.corpus.user.hashid
corpus_hashid = hashids.encode(ressource.corpus_id) corpus_hashid = ressource.corpus.hashid
follower_hashid = hashids.encode(ressource.follower_id)
# Send a PATCH to the corpus owner # Send a PATCH to the corpus owner
jsonpatch_path = f'/users/{corpus_owner_hashid}/corpora/{corpus_hashid}/corpus_follower_associations/{ressource.hashid}' jsonpatch_path = f'/users/{corpus_owner_hashid}/corpora/{corpus_hashid}/corpus_follower_associations/{ressource.hashid}'
jsonpatch = [{'op': 'remove', 'path': jsonpatch_path}] jsonpatch = [{'op': 'remove', 'path': jsonpatch_path}]
room = f'/users/{corpus_owner_hashid}' room = f'/users/{corpus_owner_hashid}'
socketio.emit('PATCH', jsonpatch, room=room) socketio.emit('PATCH', jsonpatch, room=room)
# Send a PATCH to the follower # Send a PATCH to the followers (the deleted follower and others with permission "MANAGE_FOLLOWERS")
jsonpatch_path = f'/users/{follower_hashid}/corpus_follower_associations/{ressource.hashid}' followers = [
jsonpatch = [{'op': 'remove', 'path': jsonpatch_path}] x for x in ressource.corpus.corpus_follower_associations
room = f'/users/{follower_hashid}' if x.follower_id == ressource.follower_id
socketio.emit('PATCH', jsonpatch, room=room) 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') @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') @db.event.listens_for(CorpusFollowerAssociation, 'after_insert')
def corpus_follower_association_after_insert_handler(mapper, connection, ressource): def corpus_follower_association_after_insert_handler(mapper, connection, ressource):
corpus_owner_hashid = ressource.corpus.user.hashid corpus_owner_hashid = ressource.corpus.user.hashid
corpus_hashid = hashids.encode(ressource.corpus_id) corpus_hashid = ressource.corpus.hashid
follower_hashid = hashids.encode(ressource.follower_id)
value = ressource.to_json_serializeable() value = ressource.to_json_serializeable()
# Send a PATCH to the corpus owner # Send a PATCH to the corpus owner
jsonpatch_path = f'/users/{corpus_owner_hashid}/corpora/{corpus_hashid}/corpus_follower_associations/{ressource.hashid}' jsonpatch_path = f'/users/{corpus_owner_hashid}/corpora/{corpus_hashid}/corpus_follower_associations/{ressource.hashid}'
jsonpatch = [{'op': 'add', 'path': jsonpatch_path, 'value': value}] jsonpatch = [{'op': 'add', 'path': jsonpatch_path, 'value': value}]
room = f'/users/{corpus_owner_hashid}' room = f'/users/{corpus_owner_hashid}'
socketio.emit('PATCH', jsonpatch, room=room) socketio.emit('PATCH', jsonpatch, room=room)
# Send a PATCH to the follower # Send a PATCH to the followers (the deleted follower and others with permission "MANAGE_FOLLOWERS")
jsonpatch_path = f'/users/{follower_hashid}/corpus_follower_associations/{ressource.hashid}' followers = [
jsonpatch = [{'op': 'add', 'path': jsonpatch_path, 'value': value}] x for x in ressource.corpus.corpus_follower_associations
room = f'/users/{follower_hashid}' if x.follower_id == ressource.follower_id
socketio.emit('PATCH', jsonpatch, room=room) 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') @db.event.listens_for(Corpus, 'after_update')