mirror of
https://git.pleroma.social/pleroma/pleroma.git
synced 2024-12-23 00:26:30 +00:00
Favorite changes.
- Add 'likes' to activity, collection of ids of people who liked it. - show if you favorited something or not. - Don't allow double favorites - Address favorites to the followers of the liked activity's author.
This commit is contained in:
parent
5cb446a148
commit
fa0c279139
5 changed files with 54 additions and 30 deletions
7
TODO.txt
7
TODO.txt
|
@ -1,8 +1 @@
|
||||||
- Add cache for user fetching / representing. (mostly in TwitterAPI.activity_to_status)
|
- Add cache for user fetching / representing. (mostly in TwitterAPI.activity_to_status)
|
||||||
|
|
||||||
Favorites:
|
|
||||||
- Add 'likes' to activity, collection of ids of people who liked it.
|
|
||||||
- show if you favorited something or not.
|
|
||||||
- Don't allow double favorites
|
|
||||||
- Address favorites to the followers of the liked activity's author.
|
|
||||||
|
|
||||||
|
|
|
@ -19,28 +19,43 @@ defmodule Pleroma.Web.ActivityPub.ActivityPub do
|
||||||
Repo.insert(%Activity{data: map})
|
Repo.insert(%Activity{data: map})
|
||||||
end
|
end
|
||||||
|
|
||||||
def like(%User{ap_id: ap_id}, object = %Object{data: %{ "id" => id}}) do
|
def like(%User{ap_id: ap_id} = user, %Object{data: %{ "id" => id}} = object) do
|
||||||
data = %{
|
cond do
|
||||||
"type" => "Like",
|
# There's already a like here, so return the original activity.
|
||||||
"actor" => ap_id,
|
ap_id in (object.data["likes"] || []) ->
|
||||||
"object" => id
|
query = from activity in Activity,
|
||||||
}
|
where: fragment("? @> ?", activity.data, ^%{actor: ap_id, object: id})
|
||||||
|
|
||||||
{:ok, activity} = insert(data)
|
activity = Repo.one(query)
|
||||||
like_count = (object.data["like_count"] || 0) + 1
|
{:ok, activity, object}
|
||||||
new_data = object.data |> Map.put("like_count", like_count)
|
true ->
|
||||||
changeset = Ecto.Changeset.change(object, data: new_data)
|
data = %{
|
||||||
{:ok, object} = Repo.update(changeset)
|
"type" => "Like",
|
||||||
|
"actor" => ap_id,
|
||||||
|
"object" => id,
|
||||||
|
"to" => [User.ap_followers(user)]
|
||||||
|
}
|
||||||
|
|
||||||
# Update activities that already had this. Could be done in a seperate process.
|
{:ok, activity} = insert(data)
|
||||||
relevant_activities = Activity.all_by_object_ap_id(id)
|
|
||||||
Enum.map(relevant_activities, fn (activity) ->
|
|
||||||
new_activity_data = activity.data |> Map.put("object", new_data)
|
|
||||||
changeset = Ecto.Changeset.change(activity, data: new_activity_data)
|
|
||||||
Repo.update(changeset)
|
|
||||||
end)
|
|
||||||
|
|
||||||
{:ok, activity, object}
|
likes = [ap_id | (object.data["likes"] || [])] |> Enum.uniq
|
||||||
|
|
||||||
|
new_data = object.data
|
||||||
|
|> Map.put("like_count", length(likes))
|
||||||
|
|> Map.put("likes", likes)
|
||||||
|
|
||||||
|
changeset = Ecto.Changeset.change(object, data: new_data)
|
||||||
|
{:ok, object} = Repo.update(changeset)
|
||||||
|
|
||||||
|
# Update activities that already had this. Could be done in a seperate process.
|
||||||
|
relevant_activities = Activity.all_by_object_ap_id(id)
|
||||||
|
Enum.map(relevant_activities, fn (activity) ->
|
||||||
|
new_activity_data = activity.data |> Map.put("object", new_data)
|
||||||
|
changeset = Ecto.Changeset.change(activity, data: new_activity_data)
|
||||||
|
Repo.update(changeset)
|
||||||
|
end)
|
||||||
|
{:ok, activity, object}
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
def generate_activity_id do
|
def generate_activity_id do
|
||||||
|
|
|
@ -45,6 +45,7 @@ defmodule Pleroma.Web.TwitterAPI.Representers.ActivityRepresenter do
|
||||||
created_at = get_in(activity.data, ["object", "published"])
|
created_at = get_in(activity.data, ["object", "published"])
|
||||||
|> date_to_asctime
|
|> date_to_asctime
|
||||||
like_count = get_in(activity.data, ["object", "like_count"]) || 0
|
like_count = get_in(activity.data, ["object", "like_count"]) || 0
|
||||||
|
favorited = opts[:for] && opts[:for].ap_id in (activity.data["object"]["likes"] || [])
|
||||||
|
|
||||||
mentions = opts[:mentioned] || []
|
mentions = opts[:mentioned] || []
|
||||||
|
|
||||||
|
@ -66,7 +67,8 @@ defmodule Pleroma.Web.TwitterAPI.Representers.ActivityRepresenter do
|
||||||
"statusnet_conversation_id" => activity.data["object"]["statusnetConversationId"],
|
"statusnet_conversation_id" => activity.data["object"]["statusnetConversationId"],
|
||||||
"attachments" => (activity.data["object"]["attachment"] || []) |> ObjectRepresenter.enum_to_list(opts),
|
"attachments" => (activity.data["object"]["attachment"] || []) |> ObjectRepresenter.enum_to_list(opts),
|
||||||
"attentions" => attentions,
|
"attentions" => attentions,
|
||||||
"fave_num" => like_count
|
"fave_num" => like_count,
|
||||||
|
"favorited" => !!favorited
|
||||||
}
|
}
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
|
@ -1,7 +1,7 @@
|
||||||
defmodule Pleroma.Web.ActivityPub.ActivityPubTest do
|
defmodule Pleroma.Web.ActivityPub.ActivityPubTest do
|
||||||
use Pleroma.DataCase
|
use Pleroma.DataCase
|
||||||
alias Pleroma.Web.ActivityPub.ActivityPub
|
alias Pleroma.Web.ActivityPub.ActivityPub
|
||||||
alias Pleroma.{Activity, Object}
|
alias Pleroma.{Activity, Object, User}
|
||||||
alias Pleroma.Builders.ActivityBuilder
|
alias Pleroma.Builders.ActivityBuilder
|
||||||
|
|
||||||
import Pleroma.Factory
|
import Pleroma.Factory
|
||||||
|
@ -124,7 +124,15 @@ defmodule Pleroma.Web.ActivityPub.ActivityPubTest do
|
||||||
assert like_activity.data["actor"] == user.ap_id
|
assert like_activity.data["actor"] == user.ap_id
|
||||||
assert like_activity.data["type"] == "Like"
|
assert like_activity.data["type"] == "Like"
|
||||||
assert like_activity.data["object"] == object.data["id"]
|
assert like_activity.data["object"] == object.data["id"]
|
||||||
|
assert like_activity.data["to"] == [User.ap_followers(user)]
|
||||||
assert object.data["like_count"] == 1
|
assert object.data["like_count"] == 1
|
||||||
|
assert object.data["likes"] == [user.ap_id]
|
||||||
|
|
||||||
|
# Just return the original activity if the user already liked it.
|
||||||
|
{:ok, same_like_activity, object} = ActivityPub.like(user, object)
|
||||||
|
|
||||||
|
assert like_activity == same_like_activity
|
||||||
|
assert object.data["likes"] == [user.ap_id]
|
||||||
|
|
||||||
[note_activity] = Activity.all_by_object_ap_id(object.data["id"])
|
[note_activity] = Activity.all_by_object_ap_id(object.data["id"])
|
||||||
assert note_activity.data["object"]["like_count"] == 1
|
assert note_activity.data["object"]["like_count"] == 1
|
||||||
|
|
|
@ -11,11 +11,16 @@ defmodule Pleroma.Web.TwitterAPI.Representers.ActivityRepresenterTest do
|
||||||
note_activity = insert(:note_activity)
|
note_activity = insert(:note_activity)
|
||||||
object = Object.get_by_ap_id(note_activity.data["object"]["id"])
|
object = Object.get_by_ap_id(note_activity.data["object"]["id"])
|
||||||
|
|
||||||
{:ok, like_activity, object} = ActivityPub.like(user, object)
|
{:ok, like_activity, _object} = ActivityPub.like(user, object)
|
||||||
status = ActivityRepresenter.to_map(like_activity, %{user: user, liked_activity: note_activity})
|
status = ActivityRepresenter.to_map(like_activity, %{user: user, liked_activity: note_activity})
|
||||||
|
|
||||||
assert status["id"] == like_activity.id
|
assert status["id"] == like_activity.id
|
||||||
assert status["in_reply_to_status_id"] == note_activity.id
|
assert status["in_reply_to_status_id"] == note_activity.id
|
||||||
|
|
||||||
|
note_activity = Activity.get_by_ap_id(note_activity.data["id"])
|
||||||
|
activity_actor = Repo.get_by(User, ap_id: note_activity.data["actor"])
|
||||||
|
liked_status = ActivityRepresenter.to_map(note_activity, %{user: activity_actor, for: user})
|
||||||
|
assert liked_status["favorited"] == true
|
||||||
end
|
end
|
||||||
|
|
||||||
test "an activity" do
|
test "an activity" do
|
||||||
|
@ -84,7 +89,8 @@ defmodule Pleroma.Web.TwitterAPI.Representers.ActivityRepresenterTest do
|
||||||
"attentions" => [
|
"attentions" => [
|
||||||
UserRepresenter.to_map(mentioned_user, %{for: follower})
|
UserRepresenter.to_map(mentioned_user, %{for: follower})
|
||||||
],
|
],
|
||||||
"fave_num" => 5
|
"fave_num" => 5,
|
||||||
|
"favorited" => false
|
||||||
}
|
}
|
||||||
|
|
||||||
assert ActivityRepresenter.to_map(activity, %{user: user, for: follower, mentioned: [mentioned_user]}) == expected_status
|
assert ActivityRepresenter.to_map(activity, %{user: user, for: follower, mentioned: [mentioned_user]}) == expected_status
|
||||||
|
|
Loading…
Reference in a new issue