From 60c60de330fe8fe03594da89831351099c8c9037 Mon Sep 17 00:00:00 2001 From: Roger Braun Date: Sat, 15 Apr 2017 13:54:46 +0200 Subject: [PATCH] Add announcements / retweets to TwAPI. --- lib/pleroma/web/activity_pub/activity_pub.ex | 2 +- .../representers/activity_representer.ex | 32 ++++++++++++++++++- lib/pleroma/web/twitter_api/twitter_api.ex | 23 +++++++++++++ .../activity_representer_test.exs | 27 ++++++++++++++-- test/web/twitter_api/twitter_api_test.exs | 11 +++++++ 5 files changed, 91 insertions(+), 4 deletions(-) diff --git a/lib/pleroma/web/activity_pub/activity_pub.ex b/lib/pleroma/web/activity_pub/activity_pub.ex index a38f63b03..b2508de68 100644 --- a/lib/pleroma/web/activity_pub/activity_pub.ex +++ b/lib/pleroma/web/activity_pub/activity_pub.ex @@ -63,7 +63,7 @@ defmodule Pleroma.Web.ActivityPub.ActivityPub do end) end - def unlike(%User{ap_id: ap_id} = user, %Object{data: %{ "id" => id}} = object) do + def unlike(%User{ap_id: ap_id}, %Object{data: %{ "id" => id}} = object) do query = from activity in Activity, where: fragment("? @> ?", activity.data, ^%{actor: ap_id, object: id, type: "Like"}) diff --git a/lib/pleroma/web/twitter_api/representers/activity_representer.ex b/lib/pleroma/web/twitter_api/representers/activity_representer.ex index e69a63e1d..f2bf93abb 100644 --- a/lib/pleroma/web/twitter_api/representers/activity_representer.ex +++ b/lib/pleroma/web/twitter_api/representers/activity_representer.ex @@ -4,6 +4,32 @@ defmodule Pleroma.Web.TwitterAPI.Representers.ActivityRepresenter do alias Pleroma.Activity + defp user_by_ap_id(user_list, ap_id) do + Enum.find(user_list, fn (%{ap_id: user_id}) -> ap_id == user_id end) + end + + def to_map(%Activity{data: %{"type" => "Announce", "actor" => actor}} = activity, %{users: users, announced_activity: announced_activity} = opts) do + user = user_by_ap_id(users, actor) + created_at = get_in(activity.data, ["published"]) + |> date_to_asctime + + text = "#{user.nickname} retweeted a status." + + announced_user = user_by_ap_id(users, announced_activity.data["actor"]) + retweeted_status = to_map(announced_activity, Map.merge(%{user: announced_user}, opts)) + %{ + "id" => activity.id, + "user" => UserRepresenter.to_map(user, opts), + "statusnet_html" => text, + "text" => text, + "is_local" => true, + "is_post_verb" => false, + "uri" => "tag:#{activity.data["id"]}:objectType=note", + "created_at" => created_at, + "retweeted_status" => retweeted_status + } + end + def to_map(%Activity{data: %{"type" => "Like"}} = activity, %{user: user, liked_activity: liked_activity} = opts) do created_at = get_in(activity.data, ["published"]) |> date_to_asctime @@ -45,7 +71,9 @@ defmodule Pleroma.Web.TwitterAPI.Representers.ActivityRepresenter do created_at = get_in(activity.data, ["object", "published"]) |> date_to_asctime like_count = get_in(activity.data, ["object", "like_count"]) || 0 + announcement_count = get_in(activity.data, ["object", "announcement_count"]) || 0 favorited = opts[:for] && opts[:for].ap_id in (activity.data["object"]["likes"] || []) + repeated = opts[:for] && opts[:for].ap_id in (activity.data["object"]["announcements"] || []) mentions = opts[:mentioned] || [] @@ -68,7 +96,9 @@ defmodule Pleroma.Web.TwitterAPI.Representers.ActivityRepresenter do "attachments" => (activity.data["object"]["attachment"] || []) |> ObjectRepresenter.enum_to_list(opts), "attentions" => attentions, "fave_num" => like_count, - "favorited" => !!favorited + "repeat_num" => announcement_count, + "favorited" => !!favorited, + "repeated" => !!repeated, } end diff --git a/lib/pleroma/web/twitter_api/twitter_api.ex b/lib/pleroma/web/twitter_api/twitter_api.ex index 76dbc77a1..58f8bc1d3 100644 --- a/lib/pleroma/web/twitter_api/twitter_api.ex +++ b/lib/pleroma/web/twitter_api/twitter_api.ex @@ -150,6 +150,19 @@ defmodule Pleroma.Web.TwitterAPI.TwitterAPI do {:ok, status} end + def retweet(%User{} = user, %Activity{data: %{"object" => object}} = activity) do + object = Object.get_by_ap_id(object["id"]) + + {:ok, _announce_activity, object} = ActivityPub.announce(user, object) + new_data = activity.data + |> Map.put("object", object.data) + + status = %{activity | data: new_data} + |> activity_to_status(%{for: user}) + + {:ok, status} + end + def upload(%Plug.Upload{} = file, format \\ "xml") do {:ok, object} = ActivityPub.upload(file) @@ -229,6 +242,16 @@ defmodule Pleroma.Web.TwitterAPI.TwitterAPI do ActivityRepresenter.to_map(activity, Map.merge(opts, %{user: user, liked_activity: liked_activity})) end + # For announces, fetch the announced activity and the user. + defp activity_to_status(%Activity{data: %{"type" => "Announce"}} = activity, opts) do + actor = get_in(activity.data, ["actor"]) + user = User.get_cached_by_ap_id(actor) + [announced_activity] = Activity.all_by_object_ap_id(activity.data["object"]) + announced_actor = User.get_cached_by_ap_id(announced_activity.data["actor"]) + + ActivityRepresenter.to_map(activity, Map.merge(opts, %{users: [user, announced_actor], announced_activity: announced_activity})) + end + defp activity_to_status(activity, opts) do actor = get_in(activity.data, ["actor"]) user = User.get_cached_by_ap_id(actor) diff --git a/test/web/twitter_api/representers/activity_representer_test.exs b/test/web/twitter_api/representers/activity_representer_test.exs index d1f9e00c8..a9129bccc 100644 --- a/test/web/twitter_api/representers/activity_representer_test.exs +++ b/test/web/twitter_api/representers/activity_representer_test.exs @@ -6,6 +6,26 @@ defmodule Pleroma.Web.TwitterAPI.Representers.ActivityRepresenterTest do alias Pleroma.Builders.UserBuilder import Pleroma.Factory + test "an announce activity" do + user = insert(:user) + note_activity = insert(:note_activity) + activity_actor = Repo.get_by(User, ap_id: note_activity.data["actor"]) + object = Object.get_by_ap_id(note_activity.data["object"]["id"]) + + {:ok, announce_activity, _object} = ActivityPub.announce(user, object) + note_activity = Activity.get_by_ap_id(note_activity.data["id"]) + + status = ActivityRepresenter.to_map(announce_activity, %{users: [user, activity_actor], announced_activity: note_activity, for: user}) + + assert status["id"] == announce_activity.id + assert status["user"] == UserRepresenter.to_map(user, %{for: user}) + + retweeted_status = ActivityRepresenter.to_map(note_activity, %{user: activity_actor, for: user}) + assert retweeted_status["repeated"] == true + + assert status["retweeted_status"] == retweeted_status + end + test "a like activity" do user = insert(:user) note_activity = insert(:note_activity) @@ -65,7 +85,8 @@ defmodule Pleroma.Web.TwitterAPI.Representers.ActivityRepresenterTest do "attachment" => [ object ], - "like_count" => 5 + "like_count" => 5, + "announcement_count" => 3 }, "published" => date } @@ -90,7 +111,9 @@ defmodule Pleroma.Web.TwitterAPI.Representers.ActivityRepresenterTest do UserRepresenter.to_map(mentioned_user, %{for: follower}) ], "fave_num" => 5, - "favorited" => false + "repeat_num" => 3, + "favorited" => false, + "repeated" => false } assert ActivityRepresenter.to_map(activity, %{user: user, for: follower, mentioned: [mentioned_user]}) == expected_status diff --git a/test/web/twitter_api/twitter_api_test.exs b/test/web/twitter_api/twitter_api_test.exs index fd9908b9f..71b0d8b12 100644 --- a/test/web/twitter_api/twitter_api_test.exs +++ b/test/web/twitter_api/twitter_api_test.exs @@ -209,6 +209,17 @@ defmodule Pleroma.Web.TwitterAPI.TwitterAPITest do assert status["fave_num"] == 0 end + test "it retweets a status and returns the retweet" do + user = insert(:user) + note_activity = insert(:note_activity) + activity_user = Repo.get_by!(User, ap_id: note_activity.data["actor"]) + + {:ok, status} = TwitterAPI.retweet(user, note_activity) + updated_activity = Activity.get_by_ap_id(note_activity.data["id"]) + + assert status == ActivityRepresenter.to_map(updated_activity, %{user: activity_user, for: user}) + end + setup do Supervisor.terminate_child(Pleroma.Supervisor, ConCache) Supervisor.restart_child(Pleroma.Supervisor, ConCache)