TwApi ActivityView: Add Like rendering.

This commit is contained in:
lain 2018-03-30 13:49:09 +02:00
parent 361016349f
commit 1f1caab138
2 changed files with 66 additions and 2 deletions

View file

@ -5,8 +5,32 @@ defmodule Pleroma.Web.TwitterAPI.ActivityView do
alias Pleroma.Web.TwitterAPI.UserView
alias Pleroma.Web.TwitterAPI.TwitterAPI
alias Pleroma.Web.TwitterAPI.Representers.ObjectRepresenter
alias Pleroma.Activity
alias Pleroma.Formatter
def render("activity.json", %{activity: %{data: %{"type" => "Like"}} = activity} = opts) do
user = User.get_cached_by_ap_id(activity.data["actor"])
liked_activity = Activity.get_create_activity_by_object_ap_id(activity.data["object"])
created_at = activity.data["published"]
|> Utils.date_to_asctime
text = "#{user.nickname} favorited a status."
%{
"id" => activity.id,
"user" => UserView.render("show.json", %{user: user, for: opts[:for]}),
"statusnet_html" => text,
"text" => text,
"is_local" => activity.local,
"is_post_verb" => false,
"uri" => "tag:#{activity.data["id"]}:objectType=Favourite",
"created_at" => created_at,
"in_reply_to_status_id" => liked_activity.id,
"external_url" => activity.data["id"],
"activity_type" => "like"
}
end
def render("activity.json", %{activity: %{data: %{"type" => "Create", "object" => object}} = activity} = opts) do
actor = get_in(activity.data, ["actor"])
user = User.get_cached_by_ap_id(actor)

View file

@ -24,7 +24,7 @@ test "a create activity with a note" do
"attentions" => [
UserView.render("show.json", %{user: other_user})
],
"created_at" => activity.data["object"]["published"] |> Utils.date_to_asctime,
"created_at" => activity.data["object"]["published"] |> Utils.date_to_asctime(),
"external_url" => activity.data["object"]["id"],
"fave_num" => 0,
"favorited" => false,
@ -37,7 +37,7 @@ test "a create activity with a note" do
"repeated" => false,
"statusnet_conversation_id" => convo_id,
"statusnet_html" =>
"Hey <span><a href=\"http://localhost:4001/users/nick1\">@<span>shp</span></a></span>!",
"Hey <span><a href=\"#{other_user.ap_id}\">@<span>shp</span></a></span>!",
"tags" => [],
"text" => "Hey @shp!",
"uri" => activity.data["object"]["id"],
@ -46,4 +46,44 @@ test "a create activity with a note" do
assert result == expected
end
test "an activity that is a reply" do
user = insert(:user)
other_user = insert(:user, %{nickname: "shp"})
{:ok, activity} = CommonAPI.post(user, %{"status" => "Hey @shp!"})
{:ok, answer} =
CommonAPI.post(other_user, %{"status" => "Hi!", "in_reply_to_status_id" => activity.id})
result = ActivityView.render("activity.json", %{activity: answer})
assert result["in_reply_to_status_id"] == activity.id
end
test "a like activity" do
user = insert(:user)
other_user = insert(:user, %{nickname: "shp"})
{:ok, activity} = CommonAPI.post(user, %{"status" => "Hey @shp!"})
{:ok, like, _object} = CommonAPI.favorite(activity.id, other_user)
result = ActivityView.render("activity.json", activity: like)
expected = %{
"activity_type" => "like",
"created_at" => like.data["published"] |> Utils.date_to_asctime(),
"external_url" => like.data["id"],
"id" => like.id,
"in_reply_to_status_id" => activity.id,
"is_local" => true,
"is_post_verb" => false,
"statusnet_html" => "shp favorited a status.",
"text" => "shp favorited a status.",
"uri" => "tag:#{like.data["id"]}:objectType=Favourite",
"user" => UserView.render("show.json", user: other_user)
}
assert result == expected
end
end