From 468e4b8e625a94bb494d5be7525a63286bacb60e Mon Sep 17 00:00:00 2001 From: Your Name Date: Sat, 11 Jun 2022 14:46:08 -0400 Subject: [PATCH] share collection --- collection/templates/detail.html | 18 ++++++- collection/templates/share_collection.html | 56 ++++++++++++++++++++++ collection/urls.py | 1 + collection/views.py | 20 +++++++- common/static/lib/css/neo.css | 6 ++- mastodon/api.py | 25 ++++++++++ 6 files changed, 121 insertions(+), 5 deletions(-) create mode 100644 collection/templates/share_collection.html diff --git a/collection/templates/detail.html b/collection/templates/detail.html index 7cbe183a..335e22a4 100644 --- a/collection/templates/detail.html +++ b/collection/templates/detail.html @@ -27,6 +27,7 @@ + @@ -95,7 +96,10 @@ - {% if request.user != collection.owner %} + + + {% if request.user != collection.owner %} +
{% if following %} @@ -111,7 +115,17 @@ {% endif %}
- {% endif %} +
+ {% endif %} + +
+
+
+
+ +
+
+
diff --git a/collection/templates/share_collection.html b/collection/templates/share_collection.html new file mode 100644 index 00000000..dc8e3beb --- /dev/null +++ b/collection/templates/share_collection.html @@ -0,0 +1,56 @@ +{% load static %} +{% load i18n %} +{% load l10n %} +{% load humanize %} +{% load admin_url %} +{% load mastodon %} +{% load oauth_token %} +{% load truncate %} +{% load highlight %} +{% load thumb %} + + diff --git a/collection/urls.py b/collection/urls.py index 35fd3f7f..6264abeb 100644 --- a/collection/urls.py +++ b/collection/urls.py @@ -20,6 +20,7 @@ urlpatterns = [ path('/show_item_comment/', show_item_comment, name='show_item_comment'), path('with///', list_with, name='list_with'), path('add_to_list///', add_to_list, name='add_to_list'), + path('share//', share, name='share'), # TODO: tag ] diff --git a/collection/views.py b/collection/views.py index c2150008..622c81cd 100644 --- a/collection/views.py +++ b/collection/views.py @@ -10,9 +10,9 @@ from django.utils import timezone from django.core.paginator import Paginator from mastodon import mastodon_request_included from mastodon.models import MastodonApplication -from mastodon.api import post_toot, TootVisibilityEnum +from mastodon.api import post_toot, TootVisibilityEnum, share_collection from common.utils import PageLinksGenerator -from common.views import PAGE_LINK_NUMBER, jump_or_scrape +from common.views import PAGE_LINK_NUMBER, jump_or_scrape, go_relogin from common.models import SourceSiteEnum from .models import * from .forms import * @@ -413,3 +413,19 @@ def add_to_list(request, type, id): collection = Collection.objects.filter(owner=request.user, id=cid).first() collection.append_item(item, request.POST.get('comment')) return HttpResponseRedirect(request.META.get('HTTP_REFERER')) + + +@login_required +def share(request, id): + collection = Collection.objects.filter(id=id).first() + if not collection: + return HttpResponseBadRequest() + if request.method == 'GET': + return render(request, 'share_collection.html', {'id': id, 'visibility': request.user.get_preference().default_visibility}) + else: + visibility = int(request.POST.get('visibility', default=0)) + comment = request.POST.get('comment') + if share_collection(collection, comment, request.user, visibility): + return HttpResponseRedirect(request.META.get('HTTP_REFERER')) + else: + return go_relogin(request) diff --git a/common/static/lib/css/neo.css b/common/static/lib/css/neo.css index f5f9e8d8..2f2fcbce 100644 --- a/common/static/lib/css/neo.css +++ b/common/static/lib/css/neo.css @@ -159,4 +159,8 @@ #modal .add-to-list-modal__confirm-button { float: right; -} \ No newline at end of file +} + +#modal li, #modal ul, #modal label { + display: inline; +} diff --git a/mastodon/api.py b/mastodon/api.py index ed4f183d..fc35bc96 100644 --- a/mastodon/api.py +++ b/mastodon/api.py @@ -433,3 +433,28 @@ def share_review(review): return True else: return False + + +def share_collection(collection, comment, user, visibility_no): + if visibility_no == 2: + visibility = TootVisibilityEnum.DIRECT + elif visibility_no == 1: + visibility = TootVisibilityEnum.PRIVATE + elif user.get_preference().mastodon_publish_public: + visibility = TootVisibilityEnum.PUBLIC + else: + visibility = TootVisibilityEnum.UNLISTED + tags = '\n' + user.get_preference().mastodon_append_tag.replace('[category]', '收藏单') if user.get_preference().mastodon_append_tag else '' + content = f"分享收藏单《{collection.title}》\n{collection.url}\n{comment}{tags}" + response = post_toot(user.mastodon_site, content, visibility, user.mastodon_token) + if response and response.status_code in [200, 201]: + j = response.json() + if 'url' in j: + shared_link = j['url'] + elif 'data' in j: + shared_link = f"https://twitter.com/{user.username}/status/{j['data']['id']}" + if shared_link: + pass + return True + else: + return False