From d0da40dc81f5db2b3d335fb47d00d0ac52c2cba3 Mon Sep 17 00:00:00 2001 From: Roger Braun Date: Thu, 13 Apr 2017 17:05:53 +0200 Subject: [PATCH] Display like activities. --- .../representers/activity_representer.ex | 19 +++++++++++++++++++ lib/pleroma/web/twitter_api/twitter_api.ex | 9 +++++++++ .../activity_representer_test.exs | 14 ++++++++++++++ 3 files changed, 42 insertions(+) diff --git a/lib/pleroma/web/twitter_api/representers/activity_representer.ex b/lib/pleroma/web/twitter_api/representers/activity_representer.ex index 6a5304049..ed8655905 100644 --- a/lib/pleroma/web/twitter_api/representers/activity_representer.ex +++ b/lib/pleroma/web/twitter_api/representers/activity_representer.ex @@ -4,6 +4,25 @@ defmodule Pleroma.Web.TwitterAPI.Representers.ActivityRepresenter do alias Pleroma.Activity + 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 + + text = "#{user.nickname} favorited a status." + + %{ + "id" => activity.id, + "user" => UserRepresenter.to_map(user, opts), + "statusnet_html" => text, # TODO: add summary + "text" => text, + "is_local" => true, + "is_post_verb" => false, + "uri" => "tag:#{activity.data["id"]}:objectType=Favourite", + "created_at" => created_at, + "in_reply_to_status_id" => liked_activity.id, + } + end + def to_map(%Activity{data: %{"type" => "Follow"}} = activity, %{user: user} = opts) do created_at = get_in(activity.data, ["published"]) |> date_to_asctime diff --git a/lib/pleroma/web/twitter_api/twitter_api.ex b/lib/pleroma/web/twitter_api/twitter_api.ex index 2679397d9..33ddebbc1 100644 --- a/lib/pleroma/web/twitter_api/twitter_api.ex +++ b/lib/pleroma/web/twitter_api/twitter_api.ex @@ -192,6 +192,15 @@ defmodule Pleroma.Web.TwitterAPI.TwitterAPI do end) end + # For likes, fetch the liked activity, too. + defp activity_to_status(%Activity{data: %{"type" => "Like"}} = activity, opts) do + actor = get_in(activity.data, ["actor"]) + user = Repo.get_by!(User, ap_id: actor) + [liked_activity] = Activity.all_by_object_ap_id(activity.data["object"]) + + ActivityRepresenter.to_map(activity, Map.merge(opts, %{user: user, liked_activity: liked_activity})) + end + defp activity_to_status(activity, opts) do actor = get_in(activity.data, ["actor"]) user = Repo.get_by!(User, 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 70df79a77..5673c1f0d 100644 --- a/test/web/twitter_api/representers/activity_representer_test.exs +++ b/test/web/twitter_api/representers/activity_representer_test.exs @@ -2,7 +2,21 @@ defmodule Pleroma.Web.TwitterAPI.Representers.ActivityRepresenterTest do use Pleroma.DataCase alias Pleroma.{User, Activity, Object} alias Pleroma.Web.TwitterAPI.Representers.{UserRepresenter, ActivityRepresenter, ObjectRepresenter} + alias Pleroma.Web.ActivityPub.ActivityPub alias Pleroma.Builders.UserBuilder + import Pleroma.Factory + + test "a like activity" do + user = insert(:user) + note_activity = insert(:note_activity) + object = Object.get_by_ap_id(note_activity.data["object"]["id"]) + + {:ok, like_activity, object} = ActivityPub.like(user, object) + status = ActivityRepresenter.to_map(like_activity, %{user: user, liked_activity: note_activity}) + + assert status["id"] == like_activity.id + assert status["in_reply_to_status_id"] == note_activity.id + end test "an activity" do {:ok, user} = UserBuilder.insert -- 2.45.2