1
0
Fork 0

Merge pull request #2082 from bookwyrm-social/notifications

Notifications refactor
This commit is contained in:
Mouse Reeve 2022-07-07 09:34:19 -07:00 committed by GitHub
commit 0b7c8e8dc0
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
29 changed files with 1168 additions and 296 deletions

View file

@ -7,6 +7,7 @@ from bookwyrm import models, settings
@patch("bookwyrm.models.activitypub_mixin.broadcast_task.apply_async")
@patch("bookwyrm.lists_stream.remove_list_task.delay")
class List(TestCase):
"""some activitypub oddness ahead"""
@ -21,25 +22,15 @@ class List(TestCase):
work = models.Work.objects.create(title="hello")
self.book = models.Edition.objects.create(title="hi", parent_work=work)
def test_remote_id(self, _):
def test_remote_id(self, *_):
"""shelves use custom remote ids"""
with patch(
"bookwyrm.models.activitypub_mixin.broadcast_task.apply_async"
), patch("bookwyrm.lists_stream.remove_list_task.delay"):
book_list = models.List.objects.create(
name="Test List", user=self.local_user
)
book_list = models.List.objects.create(name="Test List", user=self.local_user)
expected_id = f"https://{settings.DOMAIN}/list/{book_list.id}"
self.assertEqual(book_list.get_remote_id(), expected_id)
def test_to_activity(self, _):
def test_to_activity(self, *_):
"""jsonify it"""
with patch(
"bookwyrm.models.activitypub_mixin.broadcast_task.apply_async"
), patch("bookwyrm.lists_stream.remove_list_task.delay"):
book_list = models.List.objects.create(
name="Test List", user=self.local_user
)
book_list = models.List.objects.create(name="Test List", user=self.local_user)
activity_json = book_list.to_activity()
self.assertIsInstance(activity_json, dict)
self.assertEqual(activity_json["id"], book_list.remote_id)
@ -48,14 +39,11 @@ class List(TestCase):
self.assertEqual(activity_json["name"], "Test List")
self.assertEqual(activity_json["owner"], self.local_user.remote_id)
def test_list_item(self, _):
def test_list_item(self, *_):
"""a list entry"""
with patch(
"bookwyrm.models.activitypub_mixin.broadcast_task.apply_async"
), patch("bookwyrm.lists_stream.remove_list_task.delay"):
book_list = models.List.objects.create(
name="Test List", user=self.local_user, privacy="unlisted"
)
book_list = models.List.objects.create(
name="Test List", user=self.local_user, privacy="unlisted"
)
item = models.ListItem.objects.create(
book_list=book_list,
@ -68,14 +56,9 @@ class List(TestCase):
self.assertEqual(item.privacy, "unlisted")
self.assertEqual(item.recipients, [])
def test_list_item_pending(self, _):
def test_list_item_pending(self, *_):
"""a list entry"""
with patch(
"bookwyrm.models.activitypub_mixin.broadcast_task.apply_async"
), patch("bookwyrm.lists_stream.remove_list_task.delay"):
book_list = models.List.objects.create(
name="Test List", user=self.local_user
)
book_list = models.List.objects.create(name="Test List", user=self.local_user)
item = models.ListItem.objects.create(
book_list=book_list,
@ -90,13 +73,8 @@ class List(TestCase):
self.assertEqual(item.privacy, "direct")
self.assertEqual(item.recipients, [])
def test_embed_key(self, _):
def test_embed_key(self, *_):
"""embed_key should never be empty"""
with patch(
"bookwyrm.models.activitypub_mixin.broadcast_task.apply_async"
), patch("bookwyrm.lists_stream.remove_list_task.delay"):
book_list = models.List.objects.create(
name="Test List", user=self.local_user
)
book_list = models.List.objects.create(name="Test List", user=self.local_user)
self.assertIsInstance(book_list.embed_key, UUID)

View file

@ -0,0 +1,183 @@
""" testing models """
from unittest.mock import patch
from django.test import TestCase
from bookwyrm import models
class Notification(TestCase):
"""let people know things"""
def setUp(self): # pylint: disable=invalid-name
"""useful things for creating a notification"""
with patch("bookwyrm.suggested_users.rerank_suggestions_task.delay"), patch(
"bookwyrm.activitystreams.populate_stream_task.delay"
), patch("bookwyrm.lists_stream.populate_lists_task.delay"):
self.local_user = models.User.objects.create_user(
"mouse", "mouse@mouse.mouse", "mouseword", local=True, localname="mouse"
)
self.another_user = models.User.objects.create_user(
"rat", "rat@rat.rat", "ratword", local=True, localname="rat"
)
with patch("bookwyrm.models.user.set_remote_server.delay"):
self.remote_user = models.User.objects.create_user(
"rat",
"rat@rat.com",
"ratword",
local=False,
remote_id="https://example.com/users/rat",
inbox="https://example.com/users/rat/inbox",
outbox="https://example.com/users/rat/outbox",
)
self.work = models.Work.objects.create(title="Test Work")
self.book = models.Edition.objects.create(
title="Test Book",
isbn_13="1234567890123",
remote_id="https://example.com/book/1",
parent_work=self.work,
)
self.another_book = models.Edition.objects.create(
title="Second Test Book",
parent_work=models.Work.objects.create(title="Test Work"),
)
def test_notification(self):
"""New notifications are unread"""
notification = models.Notification.objects.create(
user=self.local_user, notification_type=models.Notification.FAVORITE
)
self.assertFalse(notification.read)
def test_notify(self):
"""Create a notification"""
models.Notification.notify(
self.local_user,
self.remote_user,
notification_type=models.Notification.FAVORITE,
)
self.assertTrue(models.Notification.objects.exists())
def test_notify_grouping(self):
"""Bundle notifications"""
models.Notification.notify(
self.local_user,
self.remote_user,
notification_type=models.Notification.FAVORITE,
)
self.assertEqual(models.Notification.objects.count(), 1)
notification = models.Notification.objects.get()
self.assertEqual(notification.related_users.count(), 1)
models.Notification.notify(
self.local_user,
self.another_user,
notification_type=models.Notification.FAVORITE,
)
self.assertEqual(models.Notification.objects.count(), 1)
notification.refresh_from_db()
self.assertEqual(notification.related_users.count(), 2)
def test_notify_remote(self):
"""Don't create notifications for remote users"""
models.Notification.notify(
self.remote_user,
self.local_user,
notification_type=models.Notification.FAVORITE,
)
self.assertFalse(models.Notification.objects.exists())
def test_notify_self(self):
"""Don't create notifications for yourself"""
models.Notification.notify(
self.local_user,
self.local_user,
notification_type=models.Notification.FAVORITE,
)
self.assertFalse(models.Notification.objects.exists())
@patch("bookwyrm.models.activitypub_mixin.broadcast_task.apply_async")
@patch("bookwyrm.lists_stream.remove_list_task.delay")
def test_notify_list_item_own_list(self, *_):
"""Don't add list item notification for your own list"""
test_list = models.List.objects.create(user=self.local_user, name="hi")
models.ListItem.objects.create(
user=self.local_user, book=self.book, book_list=test_list, order=1
)
self.assertFalse(models.Notification.objects.exists())
@patch("bookwyrm.models.activitypub_mixin.broadcast_task.apply_async")
@patch("bookwyrm.lists_stream.remove_list_task.delay")
def test_notify_list_item_remote(self, *_):
"""Don't add list item notification for a remote user"""
test_list = models.List.objects.create(user=self.remote_user, name="hi")
models.ListItem.objects.create(
user=self.local_user, book=self.book, book_list=test_list, order=1
)
self.assertFalse(models.Notification.objects.exists())
@patch("bookwyrm.models.activitypub_mixin.broadcast_task.apply_async")
@patch("bookwyrm.lists_stream.remove_list_task.delay")
def test_notify_list_item(self, *_):
"""Add list item notification"""
test_list = models.List.objects.create(user=self.local_user, name="hi")
list_item = models.ListItem.objects.create(
user=self.remote_user, book=self.book, book_list=test_list, order=2
)
notification = models.Notification.objects.get()
self.assertEqual(notification.related_users.count(), 1)
self.assertEqual(notification.related_users.first(), self.remote_user)
self.assertEqual(notification.related_list_items.count(), 1)
self.assertEqual(notification.related_list_items.first(), list_item)
models.ListItem.objects.create(
user=self.remote_user, book=self.another_book, book_list=test_list, order=3
)
notification = models.Notification.objects.get()
self.assertEqual(notification.related_users.count(), 1)
self.assertEqual(notification.related_users.first(), self.remote_user)
self.assertEqual(notification.related_list_items.count(), 2)
def test_unnotify(self):
"""Remove a notification"""
models.Notification.notify(
self.local_user,
self.remote_user,
notification_type=models.Notification.FAVORITE,
)
self.assertTrue(models.Notification.objects.exists())
models.Notification.unnotify(
self.local_user,
self.remote_user,
notification_type=models.Notification.FAVORITE,
)
self.assertFalse(models.Notification.objects.exists())
def test_unnotify_multiple_users(self):
"""Remove a notification"""
models.Notification.notify(
self.local_user,
self.remote_user,
notification_type=models.Notification.FAVORITE,
)
models.Notification.notify(
self.local_user,
self.another_user,
notification_type=models.Notification.FAVORITE,
)
self.assertTrue(models.Notification.objects.exists())
models.Notification.unnotify(
self.local_user,
self.remote_user,
notification_type=models.Notification.FAVORITE,
)
self.assertTrue(models.Notification.objects.exists())
models.Notification.unnotify(
self.local_user,
self.another_user,
notification_type=models.Notification.FAVORITE,
)
self.assertFalse(models.Notification.objects.exists())

View file

@ -394,18 +394,6 @@ class Status(TestCase):
self.assertEqual(activity["type"], "Announce")
self.assertEqual(activity, boost.to_activity(pure=True))
def test_notification(self, *_):
"""a simple model"""
notification = models.Notification.objects.create(
user=self.local_user, notification_type="FAVORITE"
)
self.assertFalse(notification.read)
with self.assertRaises(IntegrityError):
models.Notification.objects.create(
user=self.local_user, notification_type="GLORB"
)
# pylint: disable=unused-argument
def test_create_broadcast(self, one, two, broadcast_mock, *_):
"""should send out two verions of a status on create"""

View file

@ -257,6 +257,29 @@ class GroupViews(TestCase):
self.assertEqual(notification.related_group, self.testgroup)
self.assertEqual(notification.notification_type, "REMOVE")
def test_remove_member_remove_self(self, _):
"""Leave a group"""
models.GroupMember.objects.create(
user=self.rat,
group=self.testgroup,
)
request = self.factory.post(
"",
{
"group": self.testgroup.id,
"user": self.rat.localname,
},
)
request.user = self.rat
result = views.remove_member(request)
self.assertEqual(result.status_code, 302)
self.assertEqual(models.GroupMember.objects.count(), 1)
self.assertEqual(models.GroupMember.objects.first().user, self.local_user)
notification = models.Notification.objects.get()
self.assertEqual(notification.user, self.local_user)
self.assertEqual(notification.related_group, self.testgroup)
self.assertEqual(notification.notification_type, "LEAVE")
def test_accept_membership(self, _):
"""accept an invite"""
models.GroupMemberInvitation.objects.create(

View file

@ -60,7 +60,7 @@ class InteractionViews(TestCase):
notification = models.Notification.objects.get()
self.assertEqual(notification.notification_type, "FAVORITE")
self.assertEqual(notification.user, self.local_user)
self.assertEqual(notification.related_user, self.remote_user)
self.assertEqual(notification.related_users.first(), self.remote_user)
def test_unfavorite(self, *_):
"""unfav a status"""
@ -98,7 +98,7 @@ class InteractionViews(TestCase):
notification = models.Notification.objects.get()
self.assertEqual(notification.notification_type, "BOOST")
self.assertEqual(notification.user, self.local_user)
self.assertEqual(notification.related_user, self.remote_user)
self.assertEqual(notification.related_users.first(), self.remote_user)
self.assertEqual(notification.related_status, status)
def test_self_boost(self, *_):

View file

@ -25,10 +25,12 @@ class NotificationViews(TestCase):
local=True,
localname="mouse",
)
self.another_user = models.User.objects.create_user(
"rat", "rat@rat.rat", "ratword", local=True, localname="rat"
)
with patch("bookwyrm.models.activitypub_mixin.broadcast_task.apply_async"):
self.status = models.Status.objects.create(
content="hi",
user=self.local_user,
content="hi", user=self.local_user
)
models.SiteSettings.objects.create()
@ -42,27 +44,31 @@ class NotificationViews(TestCase):
validate_html(result.render())
self.assertEqual(result.status_code, 200)
def test_notifications_page_notifications(self):
def test_notifications_page_status_notifications(self):
"""there are so many views, this just makes sure it LOADS"""
models.Notification.objects.create(
user=self.local_user,
models.Notification.notify(
self.local_user,
self.another_user,
notification_type="FAVORITE",
related_status=self.status,
)
models.Notification.objects.create(
user=self.local_user,
models.Notification.notify(
self.local_user,
self.another_user,
notification_type="BOOST",
related_status=self.status,
)
models.Notification.objects.create(
user=self.local_user,
models.Notification.notify(
self.local_user,
self.another_user,
notification_type="MENTION",
related_status=self.status,
)
self.status.reply_parent = self.status
self.status.save(broadcast=False)
models.Notification.objects.create(
user=self.local_user,
models.Notification.notify(
self.local_user,
self.another_user,
notification_type="REPLY",
related_status=self.status,
)
@ -74,6 +80,200 @@ class NotificationViews(TestCase):
validate_html(result.render())
self.assertEqual(result.status_code, 200)
def test_notifications_page_follow_request(self):
"""import completed notification"""
models.Notification.notify(
self.local_user,
self.another_user,
notification_type="FOLLOW_REQUEST",
)
view = views.Notifications.as_view()
request = self.factory.get("")
request.user = self.local_user
result = view(request)
self.assertIsInstance(result, TemplateResponse)
validate_html(result.render())
def test_notifications_page_follows(self):
"""import completed notification"""
models.Notification.notify(
self.local_user,
self.another_user,
notification_type="FOLLOW",
)
view = views.Notifications.as_view()
request = self.factory.get("")
request.user = self.local_user
result = view(request)
self.assertIsInstance(result, TemplateResponse)
validate_html(result.render())
def test_notifications_page_report(self):
"""import completed notification"""
report = models.Report.objects.create(
user=self.another_user,
reporter=self.local_user,
)
notification = models.Notification.objects.create(
user=self.local_user,
notification_type="REPORT",
)
notification.related_reports.add(report)
view = views.Notifications.as_view()
request = self.factory.get("")
request.user = self.local_user
result = view(request)
self.assertIsInstance(result, TemplateResponse)
validate_html(result.render())
def test_notifications_page_import(self):
"""import completed notification"""
import_job = models.ImportJob.objects.create(user=self.local_user, mappings={})
models.Notification.objects.create(
user=self.local_user, notification_type="IMPORT", related_import=import_job
)
view = views.Notifications.as_view()
request = self.factory.get("")
request.user = self.local_user
result = view(request)
self.assertIsInstance(result, TemplateResponse)
validate_html(result.render())
self.assertEqual(result.status_code, 200)
def test_notifications_page_list(self):
"""Adding books to lists"""
book = models.Edition.objects.create(title="shape")
with patch(
"bookwyrm.models.activitypub_mixin.broadcast_task.apply_async"
), patch("bookwyrm.lists_stream.remove_list_task.delay"):
book_list = models.List.objects.create(user=self.local_user, name="hi")
item = models.ListItem.objects.create(
book=book, user=self.another_user, book_list=book_list, order=1
)
models.Notification.notify_list_item(self.local_user, item)
view = views.Notifications.as_view()
request = self.factory.get("")
request.user = self.local_user
result = view(request)
self.assertIsInstance(result, TemplateResponse)
validate_html(result.render())
self.assertEqual(result.status_code, 200)
def test_notifications_page_group_invite(self):
"""group related notifications"""
group = models.Group.objects.create(user=self.another_user, name="group")
models.Notification.notify(
self.local_user,
self.another_user,
notification_type="INVITE",
related_group=group,
)
view = views.Notifications.as_view()
request = self.factory.get("")
request.user = self.local_user
result = view(request)
self.assertIsInstance(result, TemplateResponse)
validate_html(result.render())
self.assertEqual(result.status_code, 200)
def test_notifications_page_group_accept(self):
"""group related notifications"""
group = models.Group.objects.create(user=self.another_user, name="group")
models.Notification.notify(
self.local_user,
self.another_user,
notification_type="ACCEPT",
related_group=group,
)
view = views.Notifications.as_view()
request = self.factory.get("")
request.user = self.local_user
result = view(request)
self.assertIsInstance(result, TemplateResponse)
validate_html(result.render())
self.assertEqual(result.status_code, 200)
def test_notifications_page_group_join(self):
"""group related notifications"""
group = models.Group.objects.create(user=self.another_user, name="group")
models.Notification.notify(
self.local_user,
self.another_user,
notification_type="JOIN",
related_group=group,
)
view = views.Notifications.as_view()
request = self.factory.get("")
request.user = self.local_user
result = view(request)
self.assertIsInstance(result, TemplateResponse)
validate_html(result.render())
self.assertEqual(result.status_code, 200)
def test_notifications_page_group_leave(self):
"""group related notifications"""
group = models.Group.objects.create(user=self.another_user, name="group")
models.Notification.notify(
self.local_user,
self.another_user,
notification_type="LEAVE",
related_group=group,
)
view = views.Notifications.as_view()
request = self.factory.get("")
request.user = self.local_user
result = view(request)
self.assertIsInstance(result, TemplateResponse)
validate_html(result.render())
self.assertEqual(result.status_code, 200)
def test_notifications_page_group_remove(self):
"""group related notifications"""
group = models.Group.objects.create(user=self.another_user, name="group")
models.Notification.notify(
self.local_user,
self.another_user,
notification_type="REMOVE",
related_group=group,
)
view = views.Notifications.as_view()
request = self.factory.get("")
request.user = self.local_user
result = view(request)
self.assertIsInstance(result, TemplateResponse)
validate_html(result.render())
self.assertEqual(result.status_code, 200)
def test_notifications_page_group_changes(self):
"""group related notifications"""
group = models.Group.objects.create(user=self.another_user, name="group")
models.Notification.notify(
self.local_user,
self.another_user,
notification_type="GROUP_PRIVACY",
related_group=group,
)
models.Notification.notify(
self.local_user,
self.another_user,
notification_type="GROUP_NAME",
related_group=group,
)
models.Notification.notify(
self.local_user,
self.another_user,
notification_type="GROUP_DESCRIPTION",
related_group=group,
)
view = views.Notifications.as_view()
request = self.factory.get("")
request.user = self.local_user
result = view(request)
self.assertIsInstance(result, TemplateResponse)
validate_html(result.render())
self.assertEqual(result.status_code, 200)
def test_clear_notifications(self):
"""erase notifications"""
models.Notification.objects.create(