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 @@ def get_by_ap_id(ap_id) do
Repo.one(from object in Object,
where: fragment("? @> ?", object.data, ^%{id: ap_id}))
end
def context_mapping(context) do
%Object{data: %{"id" => context}}
end
end

View file

@ -1,9 +1,9 @@
defmodule Pleroma.Web.TwitterAPI.Representers.ActivityRepresenter do
use Pleroma.Web.TwitterAPI.Representers.BaseRepresenter
alias Pleroma.Web.TwitterAPI.Representers.{UserRepresenter, ObjectRepresenter}
alias Pleroma.Web.TwitterAPI.TwitterAPI
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
@ -82,6 +82,12 @@ def to_map(%Activity{} = activity, %{user: user} = opts) do
|> Enum.filter(&(&1))
|> 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,
"user" => UserRepresenter.to_map(user, opts),
@ -92,7 +98,7 @@ def to_map(%Activity{} = activity, %{user: user} = opts) do
"is_post_verb" => true,
"created_at" => created_at,
"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),
"attentions" => attentions,
"fave_num" => like_count,

View file

@ -102,12 +102,7 @@ def fetch_mentions(user, opts \\ %{}) do
end
def fetch_conversation(user, id) do
query = from activity in Activity,
where: fragment("? @> ?", activity.data, ^%{ statusnetConversationId: id}),
limit: 1
with %Activity{} = activity <- Repo.one(query),
context <- activity.data["context"],
with context when is_binary(context) <- conversation_id_to_context(id),
activities <- ActivityPub.fetch_activities_for_context(context),
statuses <- activities |> activities_to_statuses(%{for: user})
do
@ -322,4 +317,22 @@ defp activity_to_status(activity, opts) do
defp make_date do
DateTime.utc_now() |> DateTime.to_iso8601
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

View file

@ -69,6 +69,8 @@ test "an activity" do
content = HtmlSanitizeEx.strip_tags(content_html)
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{
id: 1,
data: %{
@ -84,14 +86,15 @@ test "an activity" do
"type" => "Note",
"content" => content_html,
"inReplyToStatusId" => 213123,
"statusnetConversationId" => 4711,
"attachment" => [
object
],
"like_count" => 5,
"announcement_count" => 3
"announcement_count" => 3,
"context" => "2hu"
},
"published" => date
"published" => date,
"context" => "2hu"
}
}
@ -106,7 +109,7 @@ test "an activity" do
"is_post_verb" => true,
"created_at" => "Tue May 24 13:26:08 +0000 2016",
"in_reply_to_status_id" => 213123,
"statusnet_conversation_id" => 4711,
"statusnet_conversation_id" => convo_object.id,
"attachments" => [
ObjectRepresenter.to_map(object)
],

View file

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

View file

@ -201,11 +201,13 @@ test "Unfollow another user using screen_name" do
test "fetch statuses in a context using the conversation id" do
{:ok, user} = UserBuilder.insert()
{:ok, activity} = ActivityBuilder.insert(%{"statusnetConversationId" => 1, "context" => "2hu"})
{:ok, activity_two} = ActivityBuilder.insert(%{"statusnetConversationId" => 1,"context" => "2hu"})
{:ok, activity} = ActivityBuilder.insert(%{"context" => "2hu"})
{:ok, activity_two} = ActivityBuilder.insert(%{"context" => "2hu"})
{: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 Enum.at(statuses, 0)["id"] == activity.id
@ -314,9 +316,33 @@ test "it returns the error on registration problems" do
refute Repo.get_by(User, nickname: "lain")
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
Supervisor.terminate_child(Pleroma.Supervisor, Cachex)
Supervisor.restart_child(Pleroma.Supervisor, Cachex)
:ok
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