Add conversation fetching to TwAPI controller.
This commit is contained in:
parent
0dd3373304
commit
d2cdcc097b
@ -24,6 +24,7 @@ defmodule Pleroma.Web.Router do
|
|||||||
get "/statuses/public_timeline", TwitterAPI.Controller, :public_timeline
|
get "/statuses/public_timeline", TwitterAPI.Controller, :public_timeline
|
||||||
get "/statuses/public_and_external_timeline", TwitterAPI.Controller, :public_timeline
|
get "/statuses/public_and_external_timeline", TwitterAPI.Controller, :public_timeline
|
||||||
get "/statuses/show/:id", TwitterAPI.Controller, :fetch_status
|
get "/statuses/show/:id", TwitterAPI.Controller, :fetch_status
|
||||||
|
get "/statusnet/conversation/:id", TwitterAPI.Controller, :fetch_conversation
|
||||||
end
|
end
|
||||||
|
|
||||||
scope "/api", Pleroma.Web do
|
scope "/api", Pleroma.Web do
|
||||||
|
@ -3,6 +3,8 @@ defmodule Pleroma.Web.TwitterAPI.TwitterAPI do
|
|||||||
alias Pleroma.Web.ActivityPub.ActivityPub
|
alias Pleroma.Web.ActivityPub.ActivityPub
|
||||||
alias Pleroma.Web.TwitterAPI.Representers.ActivityRepresenter
|
alias Pleroma.Web.TwitterAPI.Representers.ActivityRepresenter
|
||||||
|
|
||||||
|
import Ecto.Query
|
||||||
|
|
||||||
def create_status(user = %User{}, data = %{}) do
|
def create_status(user = %User{}, data = %{}) do
|
||||||
date = DateTime.utc_now() |> DateTime.to_iso8601
|
date = DateTime.utc_now() |> DateTime.to_iso8601
|
||||||
|
|
||||||
@ -55,6 +57,29 @@ defmodule Pleroma.Web.TwitterAPI.TwitterAPI do
|
|||||||
|> activities_to_statuses(%{for: user})
|
|> activities_to_statuses(%{for: user})
|
||||||
end
|
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"],
|
||||||
|
activities <- ActivityPub.fetch_activities_for_context(context),
|
||||||
|
statuses <- activities |> activities_to_statuses(%{for: user})
|
||||||
|
do
|
||||||
|
statuses
|
||||||
|
else e ->
|
||||||
|
IO.inspect(e)
|
||||||
|
[]
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
def fetch_status(user, id) do
|
||||||
|
with %Activity{} = activity <- Repo.get(Activity, id) do
|
||||||
|
activity_to_status(activity, %{for: user})
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
def follow(%User{} = follower, followed_id) do
|
def follow(%User{} = follower, followed_id) do
|
||||||
with %User{} = followed <- Repo.get(User, followed_id),
|
with %User{} = followed <- Repo.get(User, followed_id),
|
||||||
{ :ok, follower } <- User.follow(follower, followed)
|
{ :ok, follower } <- User.follow(follower, followed)
|
||||||
@ -95,10 +120,4 @@ defmodule Pleroma.Web.TwitterAPI.TwitterAPI do
|
|||||||
user = Repo.get_by!(User, ap_id: actor)
|
user = Repo.get_by!(User, ap_id: actor)
|
||||||
ActivityRepresenter.to_map(activity, Map.merge(opts, %{user: user}))
|
ActivityRepresenter.to_map(activity, Map.merge(opts, %{user: user}))
|
||||||
end
|
end
|
||||||
|
|
||||||
def fetch_status(user, id) do
|
|
||||||
with %Activity{} = activity <- Repo.get(Activity, id) do
|
|
||||||
activity_to_status(activity, %{for: user})
|
|
||||||
end
|
|
||||||
end
|
|
||||||
end
|
end
|
||||||
|
@ -57,6 +57,15 @@ defmodule Pleroma.Web.TwitterAPI.Controller do
|
|||||||
|> json_reply(200, response)
|
|> json_reply(200, response)
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def fetch_conversation(%{assigns: %{user: user}} = conn, %{ "id" => id }) do
|
||||||
|
id = String.to_integer(id)
|
||||||
|
response = TwitterAPI.fetch_conversation(user, id) |> Poison.encode!
|
||||||
|
|
||||||
|
conn
|
||||||
|
|> json_reply(200, response)
|
||||||
|
end
|
||||||
|
|
||||||
|
|
||||||
defp json_reply(conn, status, json) do
|
defp json_reply(conn, status, json) do
|
||||||
conn
|
conn
|
||||||
|> put_resp_content_type("application/json")
|
|> put_resp_content_type("application/json")
|
||||||
|
@ -67,6 +67,22 @@ defmodule Pleroma.Web.TwitterAPI.ControllerTest do
|
|||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
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_three} = ActivityBuilder.insert(%{"context" => "3hu"})
|
||||||
|
|
||||||
|
conn = conn
|
||||||
|
|> get("/api/statusnet/conversation/1.json")
|
||||||
|
|
||||||
|
response = json_response(conn, 200)
|
||||||
|
|
||||||
|
assert length(response) == 2
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
describe "GET /statuses/friends_timeline.json" do
|
describe "GET /statuses/friends_timeline.json" do
|
||||||
setup [:valid_user]
|
setup [:valid_user]
|
||||||
test "without valid credentials", %{conn: conn} do
|
test "without valid credentials", %{conn: conn} do
|
||||||
|
@ -103,4 +103,17 @@ defmodule Pleroma.Web.TwitterAPI.TwitterAPITest do
|
|||||||
|
|
||||||
assert user.following == []
|
assert user.following == []
|
||||||
end
|
end
|
||||||
|
|
||||||
|
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_three} = ActivityBuilder.insert(%{"context" => "3hu"})
|
||||||
|
|
||||||
|
statuses = TwitterAPI.fetch_conversation(user, 1)
|
||||||
|
|
||||||
|
assert length(statuses) == 2
|
||||||
|
assert Enum.at(statuses, 0)["id"] == activity.id
|
||||||
|
assert Enum.at(statuses, 1)["id"] == activity_two.id
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
Loading…
Reference in New Issue
Block a user