get "/statuses/public_timeline", TwitterAPI.Controller, :public_timeline
get "/statuses/public_and_external_timeline", TwitterAPI.Controller, :public_timeline
get "/statuses/show/:id", TwitterAPI.Controller, :fetch_status
+ get "/statusnet/conversation/:id", TwitterAPI.Controller, :fetch_conversation
end
scope "/api", Pleroma.Web do
alias Pleroma.Web.ActivityPub.ActivityPub
alias Pleroma.Web.TwitterAPI.Representers.ActivityRepresenter
+ import Ecto.Query
+
def create_status(user = %User{}, data = %{}) do
date = DateTime.utc_now() |> DateTime.to_iso8601
|> activities_to_statuses(%{for: user})
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
with %User{} = followed <- Repo.get(User, followed_id),
{ :ok, follower } <- User.follow(follower, followed)
user = Repo.get_by!(User, ap_id: actor)
ActivityRepresenter.to_map(activity, Map.merge(opts, %{user: user}))
end
-
- def fetch_status(user, id) do
- with %Activity{} = activity <- Repo.get(Activity, id) do
- activity_to_status(activity, %{for: user})
- end
- end
end
|> json_reply(200, response)
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
conn
|> put_resp_content_type("application/json")
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
setup [:valid_user]
test "without valid credentials", %{conn: conn} do
assert user.following == []
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