Merge pull request #607 from mouse-reeve/notifications

Notifications for list additions
This commit is contained in:
Mouse Reeve 2021-02-10 14:08:46 -08:00 committed by GitHub
commit ed56398667
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
6 changed files with 137 additions and 50 deletions

View file

@ -0,0 +1,58 @@
# Generated by Django 3.0.7 on 2021-02-10 21:14
from django.conf import settings
from django.db import migrations, models
import django.db.models.deletion
class Migration(migrations.Migration):
dependencies = [
('bookwyrm', '0044_auto_20210207_1924'),
]
operations = [
migrations.RemoveConstraint(
model_name='notification',
name='notification_type_valid',
),
migrations.AddField(
model_name='notification',
name='related_list_item',
field=models.ForeignKey(null=True, on_delete=django.db.models.deletion.CASCADE, to='bookwyrm.ListItem'),
),
migrations.AlterField(
model_name='notification',
name='notification_type',
field=models.CharField(choices=[('FAVORITE', 'Favorite'), ('REPLY', 'Reply'), ('MENTION', 'Mention'), ('TAG', 'Tag'), ('FOLLOW', 'Follow'), ('FOLLOW_REQUEST', 'Follow Request'), ('BOOST', 'Boost'), ('IMPORT', 'Import'), ('ADD', 'Add')], max_length=255),
),
migrations.AlterField(
model_name='notification',
name='related_book',
field=models.ForeignKey(null=True, on_delete=django.db.models.deletion.CASCADE, to='bookwyrm.Edition'),
),
migrations.AlterField(
model_name='notification',
name='related_import',
field=models.ForeignKey(null=True, on_delete=django.db.models.deletion.CASCADE, to='bookwyrm.ImportJob'),
),
migrations.AlterField(
model_name='notification',
name='related_status',
field=models.ForeignKey(null=True, on_delete=django.db.models.deletion.CASCADE, to='bookwyrm.Status'),
),
migrations.AlterField(
model_name='notification',
name='related_user',
field=models.ForeignKey(null=True, on_delete=django.db.models.deletion.CASCADE, related_name='related_user', to=settings.AUTH_USER_MODEL),
),
migrations.AlterField(
model_name='notification',
name='user',
field=models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, to=settings.AUTH_USER_MODEL),
),
migrations.AddConstraint(
model_name='notification',
constraint=models.CheckConstraint(check=models.Q(notification_type__in=['FAVORITE', 'REPLY', 'MENTION', 'TAG', 'FOLLOW', 'FOLLOW_REQUEST', 'BOOST', 'IMPORT', 'ADD']), name='notification_type_valid'),
),
]

View file

@ -1,4 +1,5 @@
''' make a list of books!! ''' ''' make a list of books!! '''
from django.apps import apps
from django.db import models from django.db import models
from bookwyrm import activitypub from bookwyrm import activitypub
@ -71,6 +72,22 @@ class ListItem(CollectionItemMixin, BookWyrmModel):
object_field = 'book' object_field = 'book'
collection_field = 'book_list' collection_field = 'book_list'
def save(self, *args, **kwargs):
''' create a notification too '''
created = not bool(self.id)
super().save(*args, **kwargs)
list_owner = self.book_list.user
# create a notification if somoene ELSE added to a local user's list
if created and list_owner.local and list_owner != self.user:
model = apps.get_model('bookwyrm.Notification', require_ready=True)
model.objects.create(
user=list_owner,
related_user=self.user,
related_list_item=self,
notification_type='ADD',
)
class Meta: class Meta:
''' an opinionated constraint! you can't put a book on a list twice ''' ''' an opinionated constraint! you can't put a book on a list twice '''
unique_together = ('book', 'book_list') unique_together = ('book', 'book_list')

View file

@ -5,20 +5,22 @@ from .base_model import BookWyrmModel
NotificationType = models.TextChoices( NotificationType = models.TextChoices(
'NotificationType', 'NotificationType',
'FAVORITE REPLY MENTION TAG FOLLOW FOLLOW_REQUEST BOOST IMPORT') 'FAVORITE REPLY MENTION TAG FOLLOW FOLLOW_REQUEST BOOST IMPORT ADD')
class Notification(BookWyrmModel): class Notification(BookWyrmModel):
''' you've been tagged, liked, followed, etc ''' ''' you've been tagged, liked, followed, etc '''
user = models.ForeignKey('User', on_delete=models.PROTECT) user = models.ForeignKey('User', on_delete=models.CASCADE)
related_book = models.ForeignKey( related_book = models.ForeignKey(
'Edition', on_delete=models.PROTECT, null=True) 'Edition', on_delete=models.CASCADE, null=True)
related_user = models.ForeignKey( related_user = models.ForeignKey(
'User', 'User',
on_delete=models.PROTECT, null=True, related_name='related_user') on_delete=models.CASCADE, null=True, related_name='related_user')
related_status = models.ForeignKey( related_status = models.ForeignKey(
'Status', on_delete=models.PROTECT, null=True) 'Status', on_delete=models.CASCADE, null=True)
related_import = models.ForeignKey( related_import = models.ForeignKey(
'ImportJob', on_delete=models.PROTECT, null=True) 'ImportJob', on_delete=models.CASCADE, null=True)
related_list_item = models.ForeignKey(
'ListItem', on_delete=models.CASCADE, null=True)
read = models.BooleanField(default=False) read = models.BooleanField(default=False)
notification_type = models.CharField( notification_type = models.CharField(
max_length=255, choices=NotificationType.choices) max_length=255, choices=NotificationType.choices)

View file

@ -26,7 +26,7 @@
<div class="column is-flex-direction-column is-align-items-self-start"> <div class="column is-flex-direction-column is-align-items-self-start">
<span>{% include 'snippets/book_titleby.html' with book=item.book %}</span> <span>{% include 'snippets/book_titleby.html' with book=item.book %}</span>
{% include 'snippets/stars.html' with rating=item.book|rating:request.user %} {% include 'snippets/stars.html' with rating=item.book|rating:request.user %}
{% include 'snippets/shelve_button.html' with book=item.book %} {% include 'snippets/shelve_button/shelve_button.html' with book=item.book %}
</div> </div>
</div> </div>
<div class="card-footer has-background-white-bis"> <div class="card-footer has-background-white-bis">
@ -72,6 +72,7 @@
<p>No books found{% if query %} matching the query "{{ query }}"{% endif %}</p> <p>No books found{% if query %} matching the query "{{ query }}"{% endif %}</p>
{% endif %} {% endif %}
{% for book in suggested_books %} {% for book in suggested_books %}
{% if book %}
<div class="block columns"> <div class="block columns">
<div class="column is-narrow"> <div class="column is-narrow">
<a href="{{ book.local_path }}">{% include 'snippets/book_cover.html' with book=book size="small" %}</a> <a href="{{ book.local_path }}">{% include 'snippets/book_cover.html' with book=book size="small" %}</a>
@ -85,6 +86,7 @@
</form> </form>
</div> </div>
</div> </div>
{% endif %}
{% endfor %} {% endfor %}
</section> </section>
{% endif %} {% endif %}

View file

@ -29,6 +29,8 @@
<span class="icon icon-heart"></span> <span class="icon icon-heart"></span>
{% elif notification.notification_type == 'IMPORT' %} {% elif notification.notification_type == 'IMPORT' %}
<span class="icon icon-list"></span> <span class="icon icon-list"></span>
{% elif notification.notification_type == 'ADD' %}
<span class="icon icon-plus"></span>
{% endif %} {% endif %}
</div> </div>
<div class="column"> <div class="column">
@ -36,33 +38,34 @@
<p> <p>
{# DESCRIPTION #} {# DESCRIPTION #}
{% if notification.related_user %} {% if notification.related_user %}
{% include 'snippets/avatar.html' with user=notification.related_user %} {% include 'snippets/avatar.html' with user=notification.related_user %}
{% include 'snippets/username.html' with user=notification.related_user %} {% include 'snippets/username.html' with user=notification.related_user %}
{% if notification.notification_type == 'FAVORITE' %} {% if notification.notification_type == 'FAVORITE' %}
favorited your favorited your
<a href="{{ related_status.local_path }}">{{ related_status | status_preview_name|safe }}</a> <a href="{{ related_status.local_path }}">{{ related_status | status_preview_name|safe }}</a>
{% elif notification.notification_type == 'MENTION' %} {% elif notification.notification_type == 'MENTION' %}
mentioned you in a mentioned you in a
<a href="{{ related_status.local_path }}">{{ related_status | status_preview_name|safe }}</a> <a href="{{ related_status.local_path }}">{{ related_status | status_preview_name|safe }}</a>
{% elif notification.notification_type == 'REPLY' %} {% elif notification.notification_type == 'REPLY' %}
<a href="{{ related_status.local_path }}">replied</a> <a href="{{ related_status.local_path }}">replied</a>
to your to your
<a href="{{ related_status.reply_parent.local_path }}">{{ related_status | status_preview_name|safe }}</a> <a href="{{ related_status.reply_parent.local_path }}">{{ related_status | status_preview_name|safe }}</a>
{% elif notification.notification_type == 'FOLLOW' %} {% elif notification.notification_type == 'FOLLOW' %}
followed you followed you
{% include 'snippets/follow_button.html' with user=notification.related_user %} {% include 'snippets/follow_button.html' with user=notification.related_user %}
{% elif notification.notification_type == 'FOLLOW_REQUEST' %} {% elif notification.notification_type == 'FOLLOW_REQUEST' %}
sent you a follow request sent you a follow request
<div class="row shrink"> <div class="row shrink">
{% include 'snippets/follow_request_buttons.html' with user=notification.related_user %} {% include 'snippets/follow_request_buttons.html' with user=notification.related_user %}
</div> </div>
{% elif notification.notification_type == 'BOOST' %}
{% elif notification.notification_type == 'BOOST' %} boosted your <a href="{{ related_status.local_path }}">{{ related_status | status_preview_name|safe }}</a>
boosted your <a href="{{ related_status.local_path }}">{{ related_status | status_preview_name|safe }}</a> {% elif notification.notification_type == 'ADD' %}
{% endif %} {% if notification.related_list_item.approved %}added{% else %}suggested adding{% endif %} {% include 'snippets/book_titleby.html' with book=notification.related_list_item.book %} to your list "<a href="{{ notification.related_list_item.book_list.local_path }}{% if not notification.related_list_item.approved %}/curate{% endif %}">{{ notification.related_list_item.book_list.name }}</a>"
{% else %} {% endif %}
{% elif notification.related_import %}
your <a href="/import/{{ notification.related_import.id }}">import</a> completed. your <a href="/import/{{ notification.related_import.id }}">import</a> completed.
{% endif %} {% endif %}
</p> </p>

View file

@ -1,6 +1,7 @@
''' book list views''' ''' book list views'''
from django.contrib.auth.decorators import login_required from django.contrib.auth.decorators import login_required
from django.core.paginator import Paginator from django.core.paginator import Paginator
from django.db import IntegrityError
from django.db.models import Count, Q from django.db.models import Count, Q
from django.http import HttpResponseNotFound, HttpResponseBadRequest from django.http import HttpResponseNotFound, HttpResponseBadRequest
from django.shortcuts import get_object_or_404, redirect from django.shortcuts import get_object_or_404, redirect
@ -181,24 +182,28 @@ def add_book(request, list_id):
book = get_object_or_404(models.Edition, id=request.POST.get('book')) book = get_object_or_404(models.Edition, id=request.POST.get('book'))
# do you have permission to add to the list? # do you have permission to add to the list?
if request.user == book_list.user or book_list.curation == 'open': try:
# go ahead and add it if request.user == book_list.user or book_list.curation == 'open':
models.ListItem.objects.create( # go ahead and add it
book=book, models.ListItem.objects.create(
book_list=book_list, book=book,
user=request.user, book_list=book_list,
) user=request.user,
elif book_list.curation == 'curated': )
# make a pending entry elif book_list.curation == 'curated':
models.ListItem.objects.create( # make a pending entry
approved=False, models.ListItem.objects.create(
book=book, approved=False,
book_list=book_list, book=book,
user=request.user, book_list=book_list,
) user=request.user,
else: )
# you can't add to this list, what were you THINKING else:
return HttpResponseBadRequest() # you can't add to this list, what were you THINKING
return HttpResponseBadRequest()
except IntegrityError:
# if the book is already on the list, don't flip out
pass
return redirect('list', list_id) return redirect('list', list_id)