Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement Reactions #650

Draft
wants to merge 5 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 14 additions & 1 deletion activities/models/post.py
Original file line number Diff line number Diff line change
Expand Up @@ -471,6 +471,7 @@ def stats_with_defaults(self):
"likes": self.stats.get("likes", 0) if self.stats else 0,
"boosts": self.stats.get("boosts", 0) if self.stats else 0,
"replies": self.stats.get("replies", 0) if self.stats else 0,
"reactions": self.stats.get("reactions", {}) if self.stats else {},
}

### Local creation/editing ###
Expand Down Expand Up @@ -610,12 +611,24 @@ def calculate_stats(self, save=True):
"likes": self.interactions.filter(
type=PostInteraction.Types.like,
state__in=PostInteractionStates.group_active(),
).count(),
)
.values("identity")
.distinct()
.count(), # This counts each user that's had any likes/reactions
"boosts": self.interactions.filter(
type=PostInteraction.Types.boost,
state__in=PostInteractionStates.group_active(),
).count(),
"replies": Post.objects.filter(in_reply_to=self.object_uri).count(),
"reactions": {
row["value"] or "": row["count"]
for row in self.interactions.filter(
type=PostInteraction.Types.like,
state__in=PostInteractionStates.group_active(),
)
.values("value")
.annotate(count=models.Count("identity"))
},
}
if save:
self.save()
Expand Down
3 changes: 2 additions & 1 deletion activities/models/post_interaction.py
Original file line number Diff line number Diff line change
Expand Up @@ -154,7 +154,7 @@ class Types(models.TextChoices):
)

# Used to store any interaction extra text value like the vote
# in the question/poll case
# in the question/poll case, or the reaction
value = models.CharField(max_length=50, blank=True, null=True)

# When the activity was originally created (as opposed to when we received it)
Expand Down Expand Up @@ -392,6 +392,7 @@ def by_ap(cls, data, create=False) -> "PostInteraction":
# Get the right type
if data["type"].lower() == "like":
type = cls.Types.like
value = data.get("content") or data.get("_misskey_reaction")
elif data["type"].lower() == "announce":
type = cls.Types.boost
elif (
Expand Down
24 changes: 18 additions & 6 deletions activities/services/post.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import logging
from types import EllipsisType

from activities.models import (
Post,
Expand Down Expand Up @@ -38,36 +39,47 @@ def queryset(cls):
def __init__(self, post: Post):
self.post = post

def interact_as(self, identity: Identity, type: str):
def interact_as(self, identity: Identity, type: str, value: str | None = None):
"""
Performs an interaction on this Post
"""
interaction = PostInteraction.objects.get_or_create(
type=type,
identity=identity,
post=self.post,
value=value,
)[0]
if interaction.state not in PostInteractionStates.group_active():
interaction.transition_perform(PostInteractionStates.new)
self.post.calculate_stats()

def uninteract_as(self, identity, type):
def uninteract_as(self, identity, type, value: str | None | EllipsisType = ...):
"""
Undoes an interaction on this Post
"""
# Only search by value if it was actually given
additional_fields = {}
if value is not ...:
additional_fields["value"] = value

for interaction in PostInteraction.objects.filter(
type=type,
identity=identity,
post=self.post,
**additional_fields,
):
interaction.transition_perform(PostInteractionStates.undone)

self.post.calculate_stats()

def like_as(self, identity: Identity):
self.interact_as(identity, PostInteraction.Types.like)
def like_as(self, identity: Identity, reaction: str | None = None):
"""
Add a Like to the post, including reactions.
"""
self.interact_as(identity, PostInteraction.Types.like, value=reaction)

def unlike_as(self, identity: Identity):
self.uninteract_as(identity, PostInteraction.Types.like)
def unlike_as(self, identity: Identity, reaction: str | None = None):
self.uninteract_as(identity, PostInteraction.Types.like, value=reaction)

def boost_as(self, identity: Identity):
self.interact_as(identity, PostInteraction.Types.boost)
Expand Down
14 changes: 10 additions & 4 deletions templates/activities/_post.html
Original file line number Diff line number Diff line change
Expand Up @@ -78,10 +78,16 @@
<i class="fa-solid fa-reply"></i>
<span class="like-count">{{ post.stats_with_defaults.replies|default:"0" }}</span>
</a>
<a title="Likes" class="no-action">
<i class="fa-solid fa-star"></i>
<span class="like-count">{{ post.stats_with_defaults.likes|default:"0" }}</span>
</a>
{% for reaction, count in post.stats_with_defaults.reactions.items %}
<a title="Reaction {{reaction}}" class="no-action">
{% if reaction %}
<span>{{reaction}}</span>
{% else %}
<i class="fa-solid fa-star"></i>
{% endif %}
<span class="like-count">{{count}}</span>
</a>
{% endfor %}
<a title="Boosts" class="no-action">
<i class="fa-solid fa-retweet"></i>
<span class="like-count">{{ post.stats_with_defaults.boosts|default:"0" }}</span>
Expand Down
Loading