From e25aea3fa70279a51595206b486f0ed9174690e7 Mon Sep 17 00:00:00 2001 From: Roger Braun Date: Tue, 28 Mar 2017 14:40:09 +0200 Subject: [PATCH] Add conversation id to activities. This is for compatibility with the TwAPI. --- .../representers/user_representer.ex | 3 ++- lib/pleroma/web/twitter_api/twitter_api.ex | 19 ++++++++++++++++++- test/web/twitter_api/twitter_api_test.exs | 7 +++++-- 3 files changed, 25 insertions(+), 4 deletions(-) diff --git a/lib/pleroma/web/twitter_api/representers/user_representer.ex b/lib/pleroma/web/twitter_api/representers/user_representer.ex index 8875d26ff..d8f98488e 100644 --- a/lib/pleroma/web/twitter_api/representers/user_representer.ex +++ b/lib/pleroma/web/twitter_api/representers/user_representer.ex @@ -1,8 +1,9 @@ defmodule Pleroma.Web.TwitterAPI.Representers.UserRepresenter do use Pleroma.Web.TwitterAPI.Representers.BaseRepresenter + alias Pleroma.User - def to_map(user, opts \\ %{}) do + def to_map(user, opts) do image = "https://placehold.it/48x48" diff --git a/lib/pleroma/web/twitter_api/twitter_api.ex b/lib/pleroma/web/twitter_api/twitter_api.ex index f777ba848..7bbab14c5 100644 --- a/lib/pleroma/web/twitter_api/twitter_api.ex +++ b/lib/pleroma/web/twitter_api/twitter_api.ex @@ -34,11 +34,15 @@ defmodule Pleroma.Web.TwitterAPI.TwitterAPI do |> put_in(["object", "context"], context) |> put_in(["object", "inReplyTo"], inReplyTo.data["object"]["id"]) |> put_in(["object", "inReplyToStatusId"], inReplyToId) + |> put_in(["statusnet_conversation_id"], inReplyTo.data["statusnet_conversation_id"]) + |> put_in(["object", "statusnet_conversation_id"], inReplyTo.data["statusnet_conversation_id"]) else _e -> activity end - ActivityPub.insert(activity) + with {:ok, activity} <- ActivityPub.insert(activity) do + add_conversation_id(activity) + end end def fetch_friend_statuses(user, opts \\ %{}) do @@ -67,6 +71,19 @@ defmodule Pleroma.Web.TwitterAPI.TwitterAPI do end end + defp add_conversation_id(activity) do + if is_integer(activity.data["statusnet_conversation_id"]) do + {:ok, activity} + else + data = activity.data + |> put_in(["object", "statusnet_conversation_id"], activity.id) + |> put_in(["statusnet_conversation_id"], activity.id) + + changeset = Ecto.Changeset.change(activity, data: data) + Repo.update(changeset) + end + end + defp activities_to_statuses(activities, opts) do Enum.map(activities, fn(activity) -> activity_to_status(activity, opts) diff --git a/test/web/twitter_api/twitter_api_test.exs b/test/web/twitter_api/twitter_api_test.exs index 2d289ffcb..e7c180a9c 100644 --- a/test/web/twitter_api/twitter_api_test.exs +++ b/test/web/twitter_api/twitter_api_test.exs @@ -19,9 +19,11 @@ defmodule Pleroma.Web.TwitterAPI.TwitterAPITest do assert Enum.member?(get_in(activity.data, ["to"]), User.ap_followers(user)) assert Enum.member?(get_in(activity.data, ["to"]), "https://www.w3.org/ns/activitystreams#Public") - # Add a context + # Add a context + 'statusnet_conversation_id' assert is_binary(get_in(activity.data, ["context"])) assert is_binary(get_in(activity.data, ["object", "context"])) + assert get_in(activity.data, ["object", "statusnet_conversation_id"]) == activity.id + assert get_in(activity.data, ["statusnet_conversation_id"]) == activity.id end test "create a status that is a reply" do @@ -41,7 +43,8 @@ defmodule Pleroma.Web.TwitterAPI.TwitterAPITest do assert get_in(reply.data, ["context"]) == get_in(activity.data, ["context"]) assert get_in(reply.data, ["object", "context"]) == get_in(activity.data, ["object", "context"]) - assert get_in(reply.data, ["object", "context"]) == get_in(activity.data, ["object", "context"]) + assert get_in(reply.data, ["statusnet_conversation_id"]) == get_in(activity.data, ["statusnet_conversation_id"]) + assert get_in(reply.data, ["object", "statusnet_conversation_id"]) == get_in(activity.data, ["object", "statusnet_conversation_id"]) assert get_in(reply.data, ["object", "inReplyTo"]) == get_in(activity.data, ["object", "id"]) assert get_in(reply.data, ["object", "inReplyToStatusId"]) == activity.id end