Use conversation mapping objects to get / retrieve context from TwAPI.

This commit is contained in:
Roger Braun 2017-04-30 13:53:26 +02:00
parent f9912599c4
commit 4c8111c334
6 changed files with 71 additions and 18 deletions

View File

@ -13,4 +13,8 @@ defmodule Pleroma.Object do
Repo.one(from object in Object, Repo.one(from object in Object,
where: fragment("? @> ?", object.data, ^%{id: ap_id})) where: fragment("? @> ?", object.data, ^%{id: ap_id}))
end end
def context_mapping(context) do
%Object{data: %{"id" => context}}
end
end end

View File

@ -1,9 +1,9 @@
defmodule Pleroma.Web.TwitterAPI.Representers.ActivityRepresenter do defmodule Pleroma.Web.TwitterAPI.Representers.ActivityRepresenter do
use Pleroma.Web.TwitterAPI.Representers.BaseRepresenter use Pleroma.Web.TwitterAPI.Representers.BaseRepresenter
alias Pleroma.Web.TwitterAPI.Representers.{UserRepresenter, ObjectRepresenter} alias Pleroma.Web.TwitterAPI.Representers.{UserRepresenter, ObjectRepresenter}
alias Pleroma.Web.TwitterAPI.TwitterAPI
alias Pleroma.Activity alias Pleroma.Activity
defp user_by_ap_id(user_list, ap_id) do defp user_by_ap_id(user_list, ap_id) do
Enum.find(user_list, fn (%{ap_id: user_id}) -> ap_id == user_id end) Enum.find(user_list, fn (%{ap_id: user_id}) -> ap_id == user_id end)
end end
@ -82,6 +82,12 @@ defmodule Pleroma.Web.TwitterAPI.Representers.ActivityRepresenter do
|> Enum.filter(&(&1)) |> Enum.filter(&(&1))
|> Enum.map(fn (user) -> UserRepresenter.to_map(user, opts) end) |> Enum.map(fn (user) -> UserRepresenter.to_map(user, opts) end)
conversation_id = with context when not is_nil(context) <- activity.data["context"] do
TwitterAPI.context_to_conversation_id(context)
else _e -> nil
end
%{ %{
"id" => activity.id, "id" => activity.id,
"user" => UserRepresenter.to_map(user, opts), "user" => UserRepresenter.to_map(user, opts),
@ -92,7 +98,7 @@ defmodule Pleroma.Web.TwitterAPI.Representers.ActivityRepresenter do
"is_post_verb" => true, "is_post_verb" => true,
"created_at" => created_at, "created_at" => created_at,
"in_reply_to_status_id" => activity.data["object"]["inReplyToStatusId"], "in_reply_to_status_id" => activity.data["object"]["inReplyToStatusId"],
"statusnet_conversation_id" => activity.data["object"]["statusnetConversationId"], "statusnet_conversation_id" => conversation_id,
"attachments" => (activity.data["object"]["attachment"] || []) |> ObjectRepresenter.enum_to_list(opts), "attachments" => (activity.data["object"]["attachment"] || []) |> ObjectRepresenter.enum_to_list(opts),
"attentions" => attentions, "attentions" => attentions,
"fave_num" => like_count, "fave_num" => like_count,

View File

@ -102,12 +102,7 @@ defmodule Pleroma.Web.TwitterAPI.TwitterAPI do
end end
def fetch_conversation(user, id) do def fetch_conversation(user, id) do
query = from activity in Activity, with context when is_binary(context) <- conversation_id_to_context(id),
where: fragment("? @> ?", activity.data, ^%{ statusnetConversationId: id}),
limit: 1
with %Activity{} = activity <- Repo.one(query),
context <- activity.data["context"],
activities <- ActivityPub.fetch_activities_for_context(context), activities <- ActivityPub.fetch_activities_for_context(context),
statuses <- activities |> activities_to_statuses(%{for: user}) statuses <- activities |> activities_to_statuses(%{for: user})
do do
@ -322,4 +317,22 @@ defmodule Pleroma.Web.TwitterAPI.TwitterAPI do
defp make_date do defp make_date do
DateTime.utc_now() |> DateTime.to_iso8601 DateTime.utc_now() |> DateTime.to_iso8601
end end
def context_to_conversation_id(context) do
with %Object{id: id} <- Object.get_by_ap_id(context) do
id
else _e ->
changeset = Object.context_mapping(context)
{:ok, %{id: id}} = Repo.insert(changeset)
id
end
end
def conversation_id_to_context(id) do
with %Object{data: %{"id" => context}} <- Repo.get(Object, id) do
context
else _e ->
{:error, "No such conversation"}
end
end
end end

View File

@ -69,6 +69,8 @@ defmodule Pleroma.Web.TwitterAPI.Representers.ActivityRepresenterTest do
content = HtmlSanitizeEx.strip_tags(content_html) content = HtmlSanitizeEx.strip_tags(content_html)
date = DateTime.from_naive!(~N[2016-05-24 13:26:08.003], "Etc/UTC") |> DateTime.to_iso8601 date = DateTime.from_naive!(~N[2016-05-24 13:26:08.003], "Etc/UTC") |> DateTime.to_iso8601
{:ok, convo_object} = Object.context_mapping("2hu") |> Repo.insert
activity = %Activity{ activity = %Activity{
id: 1, id: 1,
data: %{ data: %{
@ -84,14 +86,15 @@ defmodule Pleroma.Web.TwitterAPI.Representers.ActivityRepresenterTest do
"type" => "Note", "type" => "Note",
"content" => content_html, "content" => content_html,
"inReplyToStatusId" => 213123, "inReplyToStatusId" => 213123,
"statusnetConversationId" => 4711,
"attachment" => [ "attachment" => [
object object
], ],
"like_count" => 5, "like_count" => 5,
"announcement_count" => 3 "announcement_count" => 3,
"context" => "2hu"
}, },
"published" => date "published" => date,
"context" => "2hu"
} }
} }
@ -106,7 +109,7 @@ defmodule Pleroma.Web.TwitterAPI.Representers.ActivityRepresenterTest do
"is_post_verb" => true, "is_post_verb" => true,
"created_at" => "Tue May 24 13:26:08 +0000 2016", "created_at" => "Tue May 24 13:26:08 +0000 2016",
"in_reply_to_status_id" => 213123, "in_reply_to_status_id" => 213123,
"statusnet_conversation_id" => 4711, "statusnet_conversation_id" => convo_object.id,
"attachments" => [ "attachments" => [
ObjectRepresenter.to_map(object) ObjectRepresenter.to_map(object)
], ],

View File

@ -84,12 +84,13 @@ defmodule Pleroma.Web.TwitterAPI.ControllerTest do
describe "GET /statusnet/conversation/:id.json" do describe "GET /statusnet/conversation/:id.json" do
test "returns the statuses in the conversation", %{conn: conn} do test "returns the statuses in the conversation", %{conn: conn} do
{:ok, _user} = UserBuilder.insert {:ok, _user} = UserBuilder.insert
{:ok, _activity} = ActivityBuilder.insert(%{"statusnetConversationId" => 1, "context" => "2hu"}) {:ok, _activity} = ActivityBuilder.insert(%{"context" => "2hu"})
{:ok, _activity_two} = ActivityBuilder.insert(%{"statusnetConversationId" => 1,"context" => "2hu"}) {:ok, _activity_two} = ActivityBuilder.insert(%{"context" => "2hu"})
{:ok, _activity_three} = ActivityBuilder.insert(%{"context" => "3hu"}) {:ok, _activity_three} = ActivityBuilder.insert(%{"context" => "3hu"})
{:ok, object} = Object.context_mapping("2hu") |> Repo.insert
conn = conn conn = conn
|> get("/api/statusnet/conversation/1.json") |> get("/api/statusnet/conversation/#{object.id}.json")
response = json_response(conn, 200) response = json_response(conn, 200)

View File

@ -201,11 +201,13 @@ defmodule Pleroma.Web.TwitterAPI.TwitterAPITest do
test "fetch statuses in a context using the conversation id" do test "fetch statuses in a context using the conversation id" do
{:ok, user} = UserBuilder.insert() {:ok, user} = UserBuilder.insert()
{:ok, activity} = ActivityBuilder.insert(%{"statusnetConversationId" => 1, "context" => "2hu"}) {:ok, activity} = ActivityBuilder.insert(%{"context" => "2hu"})
{:ok, activity_two} = ActivityBuilder.insert(%{"statusnetConversationId" => 1,"context" => "2hu"}) {:ok, activity_two} = ActivityBuilder.insert(%{"context" => "2hu"})
{:ok, _activity_three} = ActivityBuilder.insert(%{"context" => "3hu"}) {:ok, _activity_three} = ActivityBuilder.insert(%{"context" => "3hu"})
statuses = TwitterAPI.fetch_conversation(user, 1) {:ok, object} = Object.context_mapping("2hu") |> Repo.insert
statuses = TwitterAPI.fetch_conversation(user, object.id)
assert length(statuses) == 2 assert length(statuses) == 2
assert Enum.at(statuses, 0)["id"] == activity.id assert Enum.at(statuses, 0)["id"] == activity.id
@ -314,9 +316,33 @@ defmodule Pleroma.Web.TwitterAPI.TwitterAPITest do
refute Repo.get_by(User, nickname: "lain") refute Repo.get_by(User, nickname: "lain")
end end
test "it assigns an integer conversation_id" do
note_activity = insert(:note_activity)
user = User.get_cached_by_ap_id(note_activity.data["actor"])
status = ActivityRepresenter.to_map(note_activity, %{user: user})
assert is_number(status["statusnet_conversation_id"])
end
setup do setup do
Supervisor.terminate_child(Pleroma.Supervisor, Cachex) Supervisor.terminate_child(Pleroma.Supervisor, Cachex)
Supervisor.restart_child(Pleroma.Supervisor, Cachex) Supervisor.restart_child(Pleroma.Supervisor, Cachex)
:ok :ok
end end
describe "context_to_conversation_id" do
test "creates a mapping object" do
conversation_id = TwitterAPI.context_to_conversation_id("random context")
object = Object.get_by_ap_id("random context")
assert conversation_id == object.id
end
test "returns an existing mapping for an existing object" do
{:ok, object} = Object.context_mapping("random context") |> Repo.insert
conversation_id = TwitterAPI.context_to_conversation_id("random context")
assert conversation_id == object.id
end
end
end end