diff --git a/app/corpora/routes.py b/app/corpora/routes.py index be3e7814..b6bedc24 100644 --- a/app/corpora/routes.py +++ b/app/corpora/routes.py @@ -57,17 +57,27 @@ def disable_corpus_is_public(corpus_id): @bp.route('//follow/') @login_required def follow_corpus(corpus_id, token): - try: + corpus = Corpus.query.get_or_404(corpus_id) + try: payload = jwt.decode( - token, - current_app.config['SECRET_KEY'], - algorithms=['HS256'], - issuer=current_app.config['SERVER_NAME'], - options={'require': ['iat', 'iss', 'sub']} - ) + token, + current_app.config['SECRET_KEY'], + algorithms=['HS256'], + issuer=current_app.config['SERVER_NAME'], + # options={'require': ['exp', 'iat', 'iss', 'sub']} + options={'require': ['exp', 'iat', 'iss']} + ) except jwt.PyJWTError: - return False - return redirect(url_for('.corpus', corpus_id=corpus_id)) + abort(403) + # permission = payload.get('sub') + expiration = payload.get('exp') + if expiration < int(datetime.utcnow().timestamp()): + abort(403) + if not current_user.is_following_corpus(corpus): + current_user.follow_corpus(corpus) + db.session.commit() + flash(f'You are following {corpus.title} now', category='corpus') + return redirect(url_for('corpora.corpus', corpus_id=corpus_id)) @bp.route('//followers//unfollow', methods=['POST']) @@ -170,6 +180,9 @@ def create_corpus(): def corpus(corpus_id): corpus = Corpus.query.get_or_404(corpus_id) exp_date = (datetime.utcnow() + timedelta(days=7)).strftime('%b %d, %Y') + print(corpus.user) + print(current_user) + print(current_user.is_following_corpus(corpus)) if corpus.user == current_user or current_user.is_administrator(): return render_template( 'corpora/corpus.html.j2', @@ -191,15 +204,15 @@ def corpus(corpus_id): @login_required def generate_corpus_share_link(corpus_id): data = request.get_json('data') - permission = data['permission'] - expiration = data['expiration'] - corpus = Corpus.query.get_or_404(corpus_id) + # permission = data['permission'] + exp_data = data['expiration'] + expiration = datetime.strptime(exp_data, '%b %d, %Y') now = datetime.utcnow() payload = { 'exp': expiration, 'iat': now, - 'iss': current_app.config['SERVER_NAME'], - 'sub': permission + 'iss': current_app.config['SERVER_NAME'] + # 'sub': permission } token = jwt.encode( payload, diff --git a/app/static/js/RessourceDisplays/CorpusDisplay.js b/app/static/js/RessourceDisplays/CorpusDisplay.js index fd342dd4..aab9e470 100644 --- a/app/static/js/RessourceDisplays/CorpusDisplay.js +++ b/app/static/js/RessourceDisplays/CorpusDisplay.js @@ -31,6 +31,7 @@ class CorpusDisplay extends RessourceDisplay { this.setStatus(corpus.status); this.setTitle(corpus.title); this.setNumTokens(corpus.num_tokens); + this.setShareLink(); } onPatch(patch) { @@ -117,4 +118,28 @@ class CorpusDisplay extends RessourceDisplay { new Date(creationDate).toLocaleString("en-US") ); } + + setShareLink() { + let generateShareLinkButton = this.displayElement.querySelector('#generate-share-link-button'); + let copyShareLinkButton = this.displayElement.querySelector('#copy-share-link-button'); + let shareLinkInput = this.displayElement.querySelector('#share-link-input'); + // let permissionSelect = this.displayElement.querySelector('#permission-select'); + let expirationDate = this.displayElement.querySelector('#expiration'); + + + generateShareLinkButton.addEventListener('click', () => { + // Utils.generateCorpusShareLinkRequest(`${this.corpusId}`, permissionSelect.value, expirationDate.value) + Utils.generateCorpusShareLinkRequest(`${this.corpusId}`, expirationDate.value) + .then((shareLink) => { + shareLinkInput.parentElement.classList.remove('hide'); + shareLinkInput.value = shareLink; + }); + }); + + copyShareLinkButton.addEventListener('click', () => { + shareLinkInput.select(); + navigator.clipboard.writeText(shareLinkInput.value); + app.flash(`Copied!`, 'success'); + }); + } } diff --git a/app/static/js/Utils.js b/app/static/js/Utils.js index 80340d6c..354f0f63 100644 --- a/app/static/js/Utils.js +++ b/app/static/js/Utils.js @@ -778,9 +778,11 @@ class Utils { }); } - static generateCorpusShareLinkRequest(corpusId, permission, expiration) { + // static generateCorpusShareLinkRequest(corpusId, permission, expiration) { + static generateCorpusShareLinkRequest(corpusId, expiration) { return new Promise((resolve, reject) => { - const data = {permission: permission, expiration: expiration}; + // const data = {permission: permission, expiration: expiration}; + const data = {expiration: expiration}; fetch(`/corpora/${corpusId}/generate-corpus-share-link`, {method: 'POST', headers: {Accept: 'text/plain'}, body: JSON.stringify(data)}) .then( (response) => { diff --git a/app/templates/corpora/corpus.html.j2 b/app/templates/corpora/corpus.html.j2 index 0e982d70..8a4976e2 100644 --- a/app/templates/corpora/corpus.html.j2 +++ b/app/templates/corpora/corpus.html.j2 @@ -96,7 +96,7 @@

-
+ {#
+ Copy +
@@ -142,30 +144,5 @@ {{ super() }} {% endblock scripts %} diff --git a/app/templates/corpora/public_corpus.html.j2 b/app/templates/corpora/public_corpus.html.j2 index 94b3524c..63744441 100644 --- a/app/templates/corpora/public_corpus.html.j2 +++ b/app/templates/corpora/public_corpus.html.j2 @@ -61,13 +61,13 @@ let unfollowRequestElement = document.querySelector('.action-button[data-action="unfollow-request"]'); unfollowRequestElement.addEventListener('click', () => { return new Promise((resolve, reject) => { - fetch('{{ url_for("corpora.unfollow_corpus", corpus_id=corpus.id) }}', {method: 'POST', headers: {Accept: 'application/json'}}) + fetch('{{ url_for("corpora.current_user_unfollow_corpus", corpus_id=corpus.id) }}', {method: 'POST', headers: {Accept: 'application/json'}}) .then( (response) => { if (response.status === 403) {app.flash('Forbidden', 'error'); reject(response);} if (response.status === 404) {app.flash('Not Found', 'error'); reject(response);} resolve(response); - window.location.href = '{{ url_for("corpora.corpus", corpus_id=corpus.id) }}'; + window.location.href = '{{ url_for("main.dashboard") }}'; }, (response) => { app.flash('Something went wrong', 'error');