diff options
Diffstat (limited to 'lib')
-rw-r--r-- | lib/pleroma/web/activity_pub/activity_pub.ex | 2 | ||||
-rw-r--r-- | lib/pleroma/web/twitter_api/representers/activity_representer.ex | 32 | ||||
-rw-r--r-- | lib/pleroma/web/twitter_api/twitter_api.ex | 23 |
3 files changed, 55 insertions, 2 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) |