mirror of
https://gitlab.ub.uni-bielefeld.de/sfb1288inf/nopaque.git
synced 2024-12-24 10:34:17 +00:00
Add unfollow and view function to corpusfollowerlist
This commit is contained in:
parent
2dc41fd387
commit
8168a2384f
2
.gitignore
vendored
2
.gitignore
vendored
@ -6,6 +6,8 @@ logs/
|
|||||||
!logs/dummy
|
!logs/dummy
|
||||||
*.env
|
*.env
|
||||||
|
|
||||||
|
*.pjentsch-testing
|
||||||
|
|
||||||
# Byte-compiled / optimized / DLL files
|
# Byte-compiled / optimized / DLL files
|
||||||
__pycache__/
|
__pycache__/
|
||||||
*.py[cod]
|
*.py[cod]
|
||||||
|
@ -3,6 +3,7 @@ from flask import (
|
|||||||
abort,
|
abort,
|
||||||
current_app,
|
current_app,
|
||||||
flash,
|
flash,
|
||||||
|
make_response,
|
||||||
Markup,
|
Markup,
|
||||||
redirect,
|
redirect,
|
||||||
render_template,
|
render_template,
|
||||||
@ -70,49 +71,59 @@ def disable_corpus_is_public(corpus_id):
|
|||||||
# return redirect(url_for('.corpus', corpus_id=corpus_id))
|
# return redirect(url_for('.corpus', corpus_id=corpus_id))
|
||||||
|
|
||||||
|
|
||||||
@bp.route('/<hashid:corpus_id>/unfollow', methods=['GET', 'POST'])
|
@bp.route('/<hashid:corpus_id>/followers/<hashid:follower_id>/unfollow', methods=['POST'])
|
||||||
@login_required
|
@login_required
|
||||||
def unfollow_corpus(corpus_id):
|
def unfollow_corpus(corpus_id, follower_id):
|
||||||
corpus = Corpus.query.get_or_404(corpus_id)
|
corpus_follower_association = CorpusFollowerAssociation.query.filter_by(followed_corpus_id=corpus_id, following_user_id=follower_id).first_or_404()
|
||||||
user_hashid = request.args.get('user_id')
|
if not (corpus_follower_association.followed_corpus.user == current_user
|
||||||
if user_hashid is None:
|
or corpus_follower_association.following_user == current_user
|
||||||
user = current_user
|
or current_user.is_administrator()):
|
||||||
elif current_user.is_administrator():
|
|
||||||
user_id = hashids.decode(user_hashid)
|
|
||||||
user = User.query.get_or_404(user_id)
|
|
||||||
else:
|
|
||||||
abort(403)
|
abort(403)
|
||||||
if user.is_following_corpus(corpus):
|
if not corpus_follower_association.following_user.is_following_corpus(corpus_follower_association.followed_corpus):
|
||||||
user.unfollow_corpus(corpus)
|
abort(409) # 'User is not following the corpus'
|
||||||
|
corpus_follower_association.following_user.unfollow_corpus(corpus_follower_association.followed_corpus)
|
||||||
|
db.session.commit()
|
||||||
|
return '', 204
|
||||||
|
|
||||||
|
|
||||||
|
@bp.route('/<hashid:corpus_id>/unfollow', methods=['POST'])
|
||||||
|
@login_required
|
||||||
|
def current_user_unfollow_corpus(corpus_id):
|
||||||
|
corpus = Corpus.query.get_or_404(corpus_id)
|
||||||
|
if not current_user.is_following_corpus(corpus):
|
||||||
|
abort(409) # 'You are not following the corpus'
|
||||||
|
current_user.unfollow_corpus(corpus)
|
||||||
db.session.commit()
|
db.session.commit()
|
||||||
flash(f'You are not following {corpus.title} anymore', category='corpus')
|
flash(f'You are not following {corpus.title} anymore', category='corpus')
|
||||||
return '', 204
|
return '', 204
|
||||||
|
|
||||||
|
|
||||||
@bp.route('/<hashid:corpus_id>/followers/<hashid:corpus_follower_id>/permissions/<permission_name>/add', methods=['POST'])
|
@bp.route('/<hashid:corpus_id>/followers/<hashid:follower_id>/permissions/<permission_name>/add', methods=['POST'])
|
||||||
def add_permission(corpus_id, corpus_follower_id, permission_name):
|
def add_permission(corpus_id, follower_id, permission_name):
|
||||||
if permission_name not in [x.name for x in CorpusFollowerPermission]:
|
try:
|
||||||
abort(400)
|
permission = CorpusFollowerPermission[permission_name]
|
||||||
corpus_follower_association = CorpusFollowerAssociation.query.filter_by(followed_corpus_id=corpus_id, id=corpus_follower_id).first_or_404()
|
except KeyError:
|
||||||
corpus = corpus_follower_association.followed_corpus
|
abort(409) # 'Permission "{permission_name}" does not exist'
|
||||||
if not (corpus.user == current_user or current_user.is_administrator()):
|
corpus_follower_association = CorpusFollowerAssociation.query.filter_by(followed_corpus_id=corpus_id, following_user_id=follower_id).first_or_404()
|
||||||
|
if not (corpus_follower_association.followed_corpus.user == current_user
|
||||||
|
or current_user.is_administrator()):
|
||||||
abort(403)
|
abort(403)
|
||||||
permission_value = CorpusFollowerPermission[permission_name].value
|
corpus_follower_association.add_permission(permission)
|
||||||
corpus_follower_association.add_permission(permission_value)
|
|
||||||
db.session.commit()
|
db.session.commit()
|
||||||
return '', 204
|
return '', 204
|
||||||
|
|
||||||
|
|
||||||
@bp.route('/<hashid:corpus_id>/followers/<hashid:corpus_follower_id>/permissions/<permission_name>/remove', methods=['POST'])
|
@bp.route('/<hashid:corpus_id>/followers/<hashid:follower_id>/permissions/<permission_name>/remove', methods=['POST'])
|
||||||
def remove_permission(corpus_id, corpus_follower_id, permission_name):
|
def remove_permission(corpus_id, follower_id, permission_name):
|
||||||
if permission_name not in [x.name for x in CorpusFollowerPermission]:
|
try:
|
||||||
abort(400)
|
permission = CorpusFollowerPermission[permission_name]
|
||||||
corpus_follower_association = CorpusFollowerAssociation.query.filter_by(followed_corpus_id=corpus_id, id=corpus_follower_id).first_or_404()
|
except KeyError:
|
||||||
corpus = corpus_follower_association.followed_corpus
|
return make_response(f'Permission "{permission_name}" does not exist', 409)
|
||||||
if not (corpus.user == current_user or current_user.is_administrator()):
|
corpus_follower_association = CorpusFollowerAssociation.query.filter_by(followed_corpus_id=corpus_id, following_user_id=follower_id).first_or_404()
|
||||||
|
if not (corpus_follower_association.followed_corpus.user == current_user
|
||||||
|
or current_user.is_administrator()):
|
||||||
abort(403)
|
abort(403)
|
||||||
permission_value = CorpusFollowerPermission[permission_name].value
|
corpus_follower_association.remove_permission(permission)
|
||||||
corpus_follower_association.remove_permission(permission_value)
|
|
||||||
db.session.commit()
|
db.session.commit()
|
||||||
return '', 204
|
return '', 204
|
||||||
|
|
||||||
|
@ -307,23 +307,23 @@ class CorpusFollowerAssociation(HashidMixin, db.Model):
|
|||||||
def __repr__(self):
|
def __repr__(self):
|
||||||
return f'<CorpusFollowerAssociation {self.following_user.__repr__()} ~ {self.followed_corpus.__repr__()}>'
|
return f'<CorpusFollowerAssociation {self.following_user.__repr__()} ~ {self.followed_corpus.__repr__()}>'
|
||||||
|
|
||||||
def has_permission(self, permission):
|
def has_permission(self, permission: CorpusFollowerPermission):
|
||||||
return self.permissions & permission == permission
|
return self.permissions & permission.value == permission.value
|
||||||
|
|
||||||
def add_permission(self, permission):
|
def add_permission(self, permission: CorpusFollowerPermission):
|
||||||
if not self.has_permission(permission):
|
if not self.has_permission(permission):
|
||||||
self.permissions += permission
|
self.permissions += permission.value
|
||||||
|
|
||||||
def remove_permission(self, permission):
|
def remove_permission(self, permission: CorpusFollowerPermission):
|
||||||
if self.has_permission(permission):
|
if self.has_permission(permission):
|
||||||
self.permissions -= permission
|
self.permissions -= permission.value
|
||||||
|
|
||||||
def to_json_serializeable(self, backrefs=False, relationships=False):
|
def to_json_serializeable(self, backrefs=False, relationships=False):
|
||||||
json_serializeable = {
|
json_serializeable = {
|
||||||
'id': self.hashid,
|
'id': self.hashid,
|
||||||
'permissions': [
|
'permissions': [
|
||||||
x.name for x in CorpusFollowerPermission
|
x.name for x in CorpusFollowerPermission
|
||||||
if self.has_permission(x.value)
|
if self.has_permission(x)
|
||||||
],
|
],
|
||||||
'followed_corpus': self.followed_corpus.to_json_serializeable(),
|
'followed_corpus': self.followed_corpus.to_json_serializeable(),
|
||||||
'following_user': self.following_user.to_json_serializeable()
|
'following_user': self.following_user.to_json_serializeable()
|
||||||
@ -370,7 +370,8 @@ class User(HashidMixin, UserMixin, db.Model):
|
|||||||
)
|
)
|
||||||
followed_corpus_associations = db.relationship(
|
followed_corpus_associations = db.relationship(
|
||||||
'CorpusFollowerAssociation',
|
'CorpusFollowerAssociation',
|
||||||
back_populates='following_user'
|
back_populates='following_user',
|
||||||
|
cascade='all, delete-orphan'
|
||||||
)
|
)
|
||||||
followed_corpora = association_proxy(
|
followed_corpora = association_proxy(
|
||||||
'followed_corpus_associations',
|
'followed_corpus_associations',
|
||||||
@ -1320,7 +1321,8 @@ class Corpus(HashidMixin, db.Model):
|
|||||||
)
|
)
|
||||||
following_user_associations = db.relationship(
|
following_user_associations = db.relationship(
|
||||||
'CorpusFollowerAssociation',
|
'CorpusFollowerAssociation',
|
||||||
back_populates='followed_corpus'
|
back_populates='followed_corpus',
|
||||||
|
cascade='all, delete-orphan'
|
||||||
)
|
)
|
||||||
following_users = association_proxy(
|
following_users = association_proxy(
|
||||||
'following_user_associations',
|
'following_user_associations',
|
||||||
|
@ -33,22 +33,22 @@ class CorpusFollowerList extends ResourceList {
|
|||||||
<td><span class="full-name"></span><br><i class="about-me"></i></td>
|
<td><span class="full-name"></span><br><i class="about-me"></i></td>
|
||||||
<td>
|
<td>
|
||||||
<label>
|
<label>
|
||||||
<input ${values['permissions-can-VIEW'] ? 'checked' : ''} class="list-action-trigger" data-list-action="toggle-permission-view" type="checkbox">
|
<input ${values['permission-can-VIEW'] ? 'checked' : ''} class="list-action-trigger" data-list-action="toggle-permission" data-permission="VIEW" type="checkbox">
|
||||||
<span>View</span>
|
<span>View</span>
|
||||||
</label>
|
</label>
|
||||||
<br>
|
<br>
|
||||||
<label>
|
<label>
|
||||||
<input ${values['permissions-can-CONTRIBUTE'] ? 'checked' : ''} class="list-action-trigger" data-list-action="toggle-permission-contribute" type="checkbox">
|
<input ${values['permission-can-CONTRIBUTE'] ? 'checked' : ''} class="list-action-trigger" data-list-action="toggle-permission" data-permission="CONTRIBUTE" type="checkbox">
|
||||||
<span>Contribute</span>
|
<span>Contribute</span>
|
||||||
</label>
|
</label>
|
||||||
<br>
|
<br>
|
||||||
<label>
|
<label>
|
||||||
<input ${values['permissions-can-ADMINISTRATE'] ? 'checked' : ''} class="list-action-trigger" data-list-action="toggle-permission-administrate" type="checkbox">
|
<input ${values['permission-can-ADMINISTRATE'] ? 'checked' : ''} class="list-action-trigger" data-list-action="toggle-permission" data-permission="ADMINISTRATE" type="checkbox">
|
||||||
<span>Administrate</span>
|
<span>Administrate</span>
|
||||||
</label>
|
</label>
|
||||||
</td>
|
</td>
|
||||||
<td class="right-align">
|
<td class="right-align">
|
||||||
<a class="list-action-trigger btn-floating red waves-effect waves-light" data-list-action="delete-request"><i class="material-icons">delete</i></a>
|
<a class="list-action-trigger btn-floating red waves-effect waves-light" data-list-action="unfollow-request"><i class="material-icons">delete</i></a>
|
||||||
<a class="list-action-trigger btn-floating darken waves-effect waves-light" data-list-action="view"><i class="material-icons">send</i></a>
|
<a class="list-action-trigger btn-floating darken waves-effect waves-light" data-list-action="view"><i class="material-icons">send</i></a>
|
||||||
</td>
|
</td>
|
||||||
</tr>
|
</tr>
|
||||||
@ -59,6 +59,7 @@ class CorpusFollowerList extends ResourceList {
|
|||||||
get valueNames() {
|
get valueNames() {
|
||||||
return [
|
return [
|
||||||
{data: ['id']},
|
{data: ['id']},
|
||||||
|
{data: ['follower-id']},
|
||||||
{name: 'avatar', attr: 'src'},
|
{name: 'avatar', attr: 'src'},
|
||||||
'username',
|
'username',
|
||||||
'about-me',
|
'about-me',
|
||||||
@ -97,13 +98,14 @@ class CorpusFollowerList extends ResourceList {
|
|||||||
let user = corpusFollowerAssociation.following_user;
|
let user = corpusFollowerAssociation.following_user;
|
||||||
return {
|
return {
|
||||||
'id': corpusFollowerAssociation.id,
|
'id': corpusFollowerAssociation.id,
|
||||||
|
'follower-id': user.id,
|
||||||
'avatar': user.avatar ? `/users/${user.id}/avatar` : '/static/images/user_avatar.png',
|
'avatar': user.avatar ? `/users/${user.id}/avatar` : '/static/images/user_avatar.png',
|
||||||
'username': user.username,
|
'username': user.username,
|
||||||
'full-name': user.full_name ? user.full_name : '',
|
'full-name': user.full_name ? user.full_name : '',
|
||||||
'about-me': user.about_me ? user.about_me : '',
|
'about-me': user.about_me ? user.about_me : '',
|
||||||
'permissions-can-VIEW': corpusFollowerAssociation.permissions.includes('VIEW'),
|
'permission-can-VIEW': corpusFollowerAssociation.permissions.includes('VIEW'),
|
||||||
'permissions-can-CONTRIBUTE': corpusFollowerAssociation.permissions.includes('CONTRIBUTE'),
|
'permission-can-CONTRIBUTE': corpusFollowerAssociation.permissions.includes('CONTRIBUTE'),
|
||||||
'permissions-can-ADMINISTRATE': corpusFollowerAssociation.permissions.includes('ADMINISTRATE')
|
'permission-can-ADMINISTRATE': corpusFollowerAssociation.permissions.includes('ADMINISTRATE')
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -120,27 +122,15 @@ class CorpusFollowerList extends ResourceList {
|
|||||||
if (listActionElement === null) {return;}
|
if (listActionElement === null) {return;}
|
||||||
let listAction = listActionElement.dataset.listAction;
|
let listAction = listActionElement.dataset.listAction;
|
||||||
switch (listAction) {
|
switch (listAction) {
|
||||||
case 'toggle-permission-view': {
|
case 'toggle-permission': {
|
||||||
|
let followerId = listItemElement.dataset.followerId;
|
||||||
|
let permission = listActionElement.dataset.permission;
|
||||||
if (event.target.checked) {
|
if (event.target.checked) {
|
||||||
Utils.addCorpusFollowerPermissionRequest(this.userId, this.corpusId, itemId, 'VIEW');
|
Utils.addCorpusFollowerPermissionRequest(this.corpusId, followerId, permission)
|
||||||
|
.catch((error) => {event.target.checked = !event.target.checked;});
|
||||||
} else {
|
} else {
|
||||||
Utils.removeCorpusFollowerPermissionRequest(this.userId, this.corpusId, itemId, 'VIEW');
|
Utils.removeCorpusFollowerPermissionRequest(this.corpusId, followerId, permission)
|
||||||
}
|
.catch((error) => {event.target.checked = !event.target.checked;});
|
||||||
break;
|
|
||||||
}
|
|
||||||
case 'toggle-permission-contribute': {
|
|
||||||
if (event.target.checked) {
|
|
||||||
Utils.addCorpusFollowerPermissionRequest(this.userId, this.corpusId, itemId, 'CONTRIBUTE');
|
|
||||||
} else {
|
|
||||||
Utils.removeCorpusFollowerPermissionRequest(this.userId, this.corpusId, itemId, 'CONTRIBUTE');
|
|
||||||
}
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
case 'toggle-permission-administrate': {
|
|
||||||
if (event.target.checked) {
|
|
||||||
Utils.addCorpusFollowerPermissionRequest(this.userId, this.corpusId, itemId, 'ADMINISTRATE');
|
|
||||||
} else {
|
|
||||||
Utils.removeCorpusFollowerPermissionRequest(this.userId, this.corpusId, itemId, 'ADMINISTRATE');
|
|
||||||
}
|
}
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
@ -151,7 +141,26 @@ class CorpusFollowerList extends ResourceList {
|
|||||||
}
|
}
|
||||||
|
|
||||||
onClick(event) {
|
onClick(event) {
|
||||||
|
let listItemElement = event.target.closest('.list-item[data-id]');
|
||||||
|
if (listItemElement === null) {return;}
|
||||||
|
let itemId = listItemElement.dataset.id;
|
||||||
|
let listActionElement = event.target.closest('.list-action-trigger[data-list-action]');
|
||||||
|
let listAction = listActionElement === null ? 'view' : listActionElement.dataset.listAction;
|
||||||
|
switch (listAction) {
|
||||||
|
case 'unfollow-request': {
|
||||||
|
let followerId = listItemElement.dataset.followerId;
|
||||||
|
Utils.unfollowCorpusRequest(this.corpusId, followerId);
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
case 'view': {
|
||||||
|
let followerId = listItemElement.dataset.followerId;
|
||||||
|
window.location.href = `/users/${followerId}`;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
default: {
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
onPatch(patch) {}
|
onPatch(patch) {}
|
||||||
|
@ -69,16 +69,19 @@ class Utils {
|
|||||||
return Utils.mergeObjectsDeep(mergedObject, ...objects.slice(2));
|
return Utils.mergeObjectsDeep(mergedObject, ...objects.slice(2));
|
||||||
}
|
}
|
||||||
|
|
||||||
static addCorpusFollowerPermissionRequest(userId, corpusId, corpusFollowerAssociationId, permission) {
|
static addCorpusFollowerPermissionRequest(corpusId, followerId, permission) {
|
||||||
return new Promise((resolve, reject) => {
|
return new Promise((resolve, reject) => {
|
||||||
fetch(`/corpora/${corpusId}/followers/${corpusFollowerAssociationId}/permissions/${permission}/add`, {method: 'POST', headers: {Accept: 'application/json'}})
|
fetch(`/corpora/${corpusId}/followers/${followerId}/permissions/${permission}/add`, {method: 'POST', headers: {Accept: 'application/json'}})
|
||||||
.then(
|
.then(
|
||||||
(response) => {
|
(response) => {
|
||||||
if (response.status === 400) {app.flash('Bad Request', 'error'); reject(response);}
|
if (response.ok) {
|
||||||
if (response.status === 403) {app.flash('Forbidden', 'error'); reject(response);}
|
app.flash(`Permission added`, 'corpus');
|
||||||
if (response.status === 404) {app.flash('Not Found', 'error'); reject(response);}
|
resolve(response);
|
||||||
app.flash(`Permission added`, 'corpus');
|
return;
|
||||||
resolve(response);
|
} else {
|
||||||
|
app.flash(`${response.statusText}`, 'error');
|
||||||
|
reject(response);
|
||||||
|
}
|
||||||
},
|
},
|
||||||
(response) => {
|
(response) => {
|
||||||
app.flash('Something went wrong', 'error');
|
app.flash('Something went wrong', 'error');
|
||||||
@ -88,16 +91,18 @@ class Utils {
|
|||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
static removeCorpusFollowerPermissionRequest(userId, corpusId, corpusFollowerAssociationId, permission) {
|
static removeCorpusFollowerPermissionRequest(corpusId, followerId, permission) {
|
||||||
return new Promise((resolve, reject) => {
|
return new Promise((resolve, reject) => {
|
||||||
fetch(`/corpora/${corpusId}/followers/${corpusFollowerAssociationId}/permissions/${permission}/remove`, {method: 'POST', headers: {Accept: 'application/json'}})
|
fetch(`/corpora/${corpusId}/followers/${followerId}/permissions/${permission}/remove`, {method: 'POST', headers: {Accept: 'application/json'}})
|
||||||
.then(
|
.then(
|
||||||
(response) => {
|
(response) => {
|
||||||
if (response.status === 400) {app.flash('Bad Request', 'error'); reject(response);}
|
if (response.ok) {
|
||||||
if (response.status === 403) {app.flash('Forbidden', 'error'); reject(response);}
|
app.flash(`Permission removed`, 'corpus');
|
||||||
if (response.status === 404) {app.flash('Not Found', 'error'); reject(response);}
|
resolve(response);
|
||||||
app.flash(`Permission removed`, 'corpus');
|
} else {
|
||||||
resolve(response);
|
app.flash(`${response.statusText}`, 'error');
|
||||||
|
reject(response);
|
||||||
|
}
|
||||||
},
|
},
|
||||||
(response) => {
|
(response) => {
|
||||||
app.flash('Something went wrong', 'error');
|
app.flash('Something went wrong', 'error');
|
||||||
@ -215,6 +220,27 @@ class Utils {
|
|||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
static unfollowCorpusRequest(corpusId, followerId) {
|
||||||
|
return new Promise((resolve, reject) => {
|
||||||
|
fetch(`/corpora/${corpusId}/followers/${followerId}/unfollow`, {method: 'POST', headers: {Accept: 'application/json'}})
|
||||||
|
.then(
|
||||||
|
(response) => {
|
||||||
|
if (response.ok) {
|
||||||
|
app.flash(`User unfollowed from Corpus`, 'corpus');
|
||||||
|
resolve(response);
|
||||||
|
} else {
|
||||||
|
app.flash(`${response.statusText}`, 'error');
|
||||||
|
reject(response);
|
||||||
|
}
|
||||||
|
},
|
||||||
|
(response) => {
|
||||||
|
app.flash('Something went wrong', 'error');
|
||||||
|
reject(response);
|
||||||
|
}
|
||||||
|
);
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
static deleteCorpusRequest(userId, corpusId) {
|
static deleteCorpusRequest(userId, corpusId) {
|
||||||
return new Promise((resolve, reject) => {
|
return new Promise((resolve, reject) => {
|
||||||
let corpus;
|
let corpus;
|
||||||
|
@ -1,10 +0,0 @@
|
|||||||
import DataStore from './DataStore';
|
|
||||||
import EventBroker from './EventBroker';
|
|
||||||
|
|
||||||
|
|
||||||
const dataStore = new DataStore();
|
|
||||||
const eventBroker = new EventBroker();
|
|
||||||
const socket = io({transports: ['websocket'], upgrade: false});
|
|
||||||
|
|
||||||
|
|
||||||
export {eventBroker, socket};
|
|
Loading…
Reference in New Issue
Block a user