From 394666357fd2e21a1b99c1b3df1e42c8a6aa4ee8 Mon Sep 17 00:00:00 2001 From: Mouse Reeve Date: Mon, 18 Jan 2021 16:32:02 -0800 Subject: [PATCH 01/11] Poll for notifications --- bookwyrm/static/js/shared.js | 21 +++++++++++++++++++++ bookwyrm/templates/layout.html | 6 +++--- bookwyrm/urls.py | 5 +++-- bookwyrm/views/__init__.py | 1 + bookwyrm/views/updates.py | 17 +++++++++++++++++ 5 files changed, 45 insertions(+), 5 deletions(-) create mode 100644 bookwyrm/views/updates.py diff --git a/bookwyrm/static/js/shared.js b/bookwyrm/static/js/shared.js index 55a5c1b20..8bac038bc 100644 --- a/bookwyrm/static/js/shared.js +++ b/bookwyrm/static/js/shared.js @@ -31,8 +31,29 @@ window.onload = function() { // hidden submit button in a form document.querySelectorAll('.hidden-form input') .forEach(t => t.onchange = revealForm); + + // polling + document.querySelectorAll('[data-poll]') + .forEach(t => setInterval(function () { polling(t) }, 10000)); }; + +function polling(el) { + // poll the endpoint + fetch('/api/updates/' + el.getAttribute('data-poll'), { + method : "GET", + }).then(response => response.json()) + .then(data => updateCountElement(el, data)); +} +function updateCountElement(el, data) { + const count = data[el.getAttribute('data-poll')]; + if (count) { + removeClass(el, 'hidden'); + el.innerHTML = count; + } +} + + function revealForm(e) { var hidden = e.currentTarget.closest('.hidden-form').getElementsByClassName('hidden')[0]; if (hidden) { diff --git a/bookwyrm/templates/layout.html b/bookwyrm/templates/layout.html index 30c8bf7a4..f3e378614 100644 --- a/bookwyrm/templates/layout.html +++ b/bookwyrm/templates/layout.html @@ -113,9 +113,9 @@ Notifications - {% if request.user|notification_count %} - {{ request.user | notification_count }} - {% endif %} + + {{ request.user | notification_count }} + diff --git a/bookwyrm/urls.py b/bookwyrm/urls.py index f014d44d8..ab134b1c5 100644 --- a/bookwyrm/urls.py +++ b/bookwyrm/urls.py @@ -31,14 +31,15 @@ urlpatterns = [ re_path(r'^inbox/?$', incoming.shared_inbox), re_path(r'%s/inbox/?$' % local_user_path, incoming.inbox), re_path(r'%s/outbox/?$' % local_user_path, views.Outbox.as_view()), - - # .well-known endpoints re_path(r'^.well-known/webfinger/?$', wellknown.webfinger), re_path(r'^.well-known/nodeinfo/?$', wellknown.nodeinfo_pointer), re_path(r'^nodeinfo/2\.0/?$', wellknown.nodeinfo), re_path(r'^api/v1/instance/?$', wellknown.instance_info), re_path(r'^api/v1/instance/peers/?$', wellknown.peers), + # polling updates + re_path('^api/updates/notifications/?$', views.Updates.as_view()), + # authentication re_path(r'^login/?$', views.Login.as_view()), re_path(r'^register/?$', views.Register.as_view()), diff --git a/bookwyrm/views/__init__.py b/bookwyrm/views/__init__.py index e1ffeda42..d37851aa4 100644 --- a/bookwyrm/views/__init__.py +++ b/bookwyrm/views/__init__.py @@ -23,4 +23,5 @@ from .shelf import Shelf from .shelf import user_shelves_page, create_shelf, delete_shelf from .shelf import shelve, unshelve from .status import Status, Replies, CreateStatus, DeleteStatus +from .updates import Updates from .user import User, EditUser, Followers, Following diff --git a/bookwyrm/views/updates.py b/bookwyrm/views/updates.py new file mode 100644 index 000000000..233e51917 --- /dev/null +++ b/bookwyrm/views/updates.py @@ -0,0 +1,17 @@ +''' endpoints for getting updates about activity ''' +from django.contrib.auth.decorators import login_required +from django.http import JsonResponse +from django.utils.decorators import method_decorator +from django.views import View + +# pylint: disable= no-self-use +@method_decorator(login_required, name='dispatch') +class Updates(View): + ''' so the app can poll ''' + def get(self, request): + ''' any notifications waiting? ''' + return JsonResponse({ + 'notifications': request.user.notification_set.filter( + read=False + ).count(), + }) From 128dc3be44b457ed2ba6f4fb1b6f240b5419c669 Mon Sep 17 00:00:00 2001 From: Mouse Reeve Date: Mon, 18 Jan 2021 16:52:51 -0800 Subject: [PATCH 02/11] Remove a div --- bookwyrm/templates/layout.html | 18 ++++++++---------- 1 file changed, 8 insertions(+), 10 deletions(-) diff --git a/bookwyrm/templates/layout.html b/bookwyrm/templates/layout.html index f3e378614..ddbafbb42 100644 --- a/bookwyrm/templates/layout.html +++ b/bookwyrm/templates/layout.html @@ -106,17 +106,15 @@ {% else %} From 1048688284107594fbb89a3f51631bb4065a214f Mon Sep 17 00:00:00 2001 From: Mouse Reeve Date: Tue, 19 Jan 2021 07:15:08 -0800 Subject: [PATCH 03/11] Fixes import paths --- bookwyrm/views/import_data.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/bookwyrm/views/import_data.py b/bookwyrm/views/import_data.py index a8bfc8659..8c7cee889 100644 --- a/bookwyrm/views/import_data.py +++ b/bookwyrm/views/import_data.py @@ -43,7 +43,7 @@ class Import(View): except (UnicodeDecodeError, ValueError): return HttpResponseBadRequest('Not a valid csv file') goodreads_import.start_import(job) - return redirect('/import-status/%d' % job.id) + return redirect('/import/%d' % job.id) return HttpResponseBadRequest() @@ -80,4 +80,4 @@ class ImportStatus(View): items, ) goodreads_import.start_import(job) - return redirect('/import-status/%d' % job.id) + return redirect('/import/%d' % job.id) From 6efe4d54f008ae2051fba642a0aac431c2f2e7e1 Mon Sep 17 00:00:00 2001 From: Mouse Reeve Date: Tue, 19 Jan 2021 07:30:35 -0800 Subject: [PATCH 04/11] Sets minimum goal --- bookwyrm/models/user.py | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/bookwyrm/models/user.py b/bookwyrm/models/user.py index 1e9db4011..79d112062 100644 --- a/bookwyrm/models/user.py +++ b/bookwyrm/models/user.py @@ -4,6 +4,7 @@ from urllib.parse import urlparse from django.apps import apps from django.contrib.auth.models import AbstractUser +from django.core.validators import MinValueValidator from django.db import models from django.dispatch import receiver from django.utils import timezone @@ -226,7 +227,9 @@ class KeyPair(ActivitypubMixin, BookWyrmModel): class AnnualGoal(BookWyrmModel): ''' set a goal for how many books you read in a year ''' user = models.ForeignKey('User', on_delete=models.PROTECT) - goal = models.IntegerField() + goal = models.IntegerField( + validators=[MinValueValidator(1)] + ) year = models.IntegerField(default=timezone.now().year) privacy = models.CharField( max_length=255, From 328ebc39c1407abf7a0924c8e242a806cd81d7e5 Mon Sep 17 00:00:00 2001 From: Mouse Reeve Date: Tue, 19 Jan 2021 07:38:12 -0800 Subject: [PATCH 05/11] Adds migration --- .../migrations/0038_auto_20210119_1534.py | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) create mode 100644 bookwyrm/migrations/0038_auto_20210119_1534.py diff --git a/bookwyrm/migrations/0038_auto_20210119_1534.py b/bookwyrm/migrations/0038_auto_20210119_1534.py new file mode 100644 index 000000000..ac7a0d68f --- /dev/null +++ b/bookwyrm/migrations/0038_auto_20210119_1534.py @@ -0,0 +1,19 @@ +# Generated by Django 3.0.7 on 2021-01-19 15:34 + +import django.core.validators +from django.db import migrations, models + + +class Migration(migrations.Migration): + + dependencies = [ + ('bookwyrm', '0037_auto_20210118_1954'), + ] + + operations = [ + migrations.AlterField( + model_name='annualgoal', + name='goal', + field=models.IntegerField(validators=[django.core.validators.MinValueValidator(1)]), + ), + ] From 589c128793547836d92e31f76750d01caf461228 Mon Sep 17 00:00:00 2001 From: Mouse Reeve Date: Tue, 19 Jan 2021 07:38:18 -0800 Subject: [PATCH 06/11] Sets min in html --- bookwyrm/templates/snippets/goal_form.html | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/bookwyrm/templates/snippets/goal_form.html b/bookwyrm/templates/snippets/goal_form.html index bd36f6090..475c5da5c 100644 --- a/bookwyrm/templates/snippets/goal_form.html +++ b/bookwyrm/templates/snippets/goal_form.html @@ -8,7 +8,7 @@
- +
From c1c7b68fc1ac9f1c15fbe106526c81741a528513 Mon Sep 17 00:00:00 2001 From: Mouse Reeve Date: Tue, 19 Jan 2021 07:40:37 -0800 Subject: [PATCH 07/11] Fixes error state for goal --- bookwyrm/views/goal.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/bookwyrm/views/goal.py b/bookwyrm/views/goal.py index 45fe614b7..f1954672f 100644 --- a/bookwyrm/views/goal.py +++ b/bookwyrm/views/goal.py @@ -52,7 +52,7 @@ class Goal(View): form = forms.GoalForm(request.POST, instance=goal) if not form.is_valid(): data = { - 'title': '%s\'s %d Reading' % (goal.user.display_name, year), + 'title': '%s\'s %d Reading' % (request.user.display_name, year), 'goal_form': form, 'goal': goal, 'year': year, From f049c7c3d93e8e6139a080c095d558ddddc2fd8a Mon Sep 17 00:00:00 2001 From: Mouse Reeve Date: Tue, 19 Jan 2021 09:31:01 -0800 Subject: [PATCH 08/11] Fixes clashing form ids in rate/review --- bookwyrm/templates/snippets/rate_action.html | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/bookwyrm/templates/snippets/rate_action.html b/bookwyrm/templates/snippets/rate_action.html index 2a376f50d..7a82501ab 100644 --- a/bookwyrm/templates/snippets/rate_action.html +++ b/bookwyrm/templates/snippets/rate_action.html @@ -10,11 +10,11 @@
- - + + {% for i in '12345'|make_list %} - -