Don't fav things multiple times

Fixes #28
This commit is contained in:
Mouse Reeve 2020-02-19 00:22:55 -08:00
parent a664ad3328
commit 77841909fe
4 changed files with 21 additions and 6 deletions

View file

@ -1,4 +1,4 @@
# Generated by Django 3.0.3 on 2020-02-19 08:04
# Generated by Django 3.0.3 on 2020-02-19 08:16
from django.conf import settings
from django.db import migrations, models
@ -22,7 +22,7 @@ class Migration(migrations.Migration):
('user', models.ForeignKey(on_delete=django.db.models.deletion.PROTECT, to=settings.AUTH_USER_MODEL)),
],
options={
'abstract': False,
'unique_together': {('user', 'status')},
},
),
migrations.AddField(

View file

@ -50,3 +50,7 @@ class Favorite(FedireadsModel):
''' fav'ing a post '''
user = models.ForeignKey('User', on_delete=models.PROTECT)
status = models.ForeignKey('Status', on_delete=models.PROTECT)
class Meta:
unique_together = ('user', 'status')

View file

@ -1,4 +1,5 @@
''' handles all the activity coming out of the server '''
from django.db import IntegrityError
from django.http import HttpResponseNotFound, JsonResponse
from django.views.decorators.csrf import csrf_exempt
import requests
@ -172,10 +173,15 @@ def handle_comment(user, review, content):
def handle_outgoing_favorite(user, status):
''' a user likes a status '''
favorite = models.Favorite.objects.create(
status=status,
user=user
)
try:
favorite = models.Favorite.objects.create(
status=status,
user=user
)
except IntegrityError:
# you already fav'ed that
return
fav_activity = activitypub.get_favorite(favorite)
recipients = get_recipients(user, 'direct', [status.user])
broadcast(user, fav_activity, recipients)

View file

@ -67,6 +67,11 @@
<p>{{ activity.content | safe }}</p>
</div>
<div class="interaction">
{% if activity.favorites.all %}
<span>
{{ activity.favorites.count }} like(s)
</span>
{% endif %}
<form name="favorite" action="/favorite/{{ activity.id }}" method="post">
{% csrf_token %}
<button>⭐️ Like</button>