diff options
Diffstat (limited to 'lib/pleroma/web/twitter_api')
-rw-r--r-- | lib/pleroma/web/twitter_api/twitter_api.ex | 34 | ||||
-rw-r--r-- | lib/pleroma/web/twitter_api/twitter_api_controller.ex | 17 |
2 files changed, 49 insertions, 2 deletions
diff --git a/lib/pleroma/web/twitter_api/twitter_api.ex b/lib/pleroma/web/twitter_api/twitter_api.ex index 7984b2e63..9b9ee0bd1 100644 --- a/lib/pleroma/web/twitter_api/twitter_api.ex +++ b/lib/pleroma/web/twitter_api/twitter_api.ex @@ -80,6 +80,11 @@ defmodule Pleroma.Web.TwitterAPI.TwitterAPI do |> activities_to_statuses(%{for: user}) end + def fetch_user_statuses(user, opts \\ %{}) do + ActivityPub.fetch_activities([], opts) + |> activities_to_statuses(%{for: user}) + end + def fetch_conversation(user, id) do query = from activity in Activity, where: fragment("? @> ?", activity.data, ^%{ statusnetConversationId: id}), @@ -248,12 +253,37 @@ defmodule Pleroma.Web.TwitterAPI.TwitterAPI do {:ok, UserRepresenter.to_map(user)} else {:error, changeset} -> - errors = Ecto.Changeset.traverse_errors(changeset, fn {msg, opts} -> msg end) - |> Poison.encode! + errors = Ecto.Changeset.traverse_errors(changeset, fn {msg, _opts} -> msg end) + |> Poison.encode! {:error, %{error: errors}} end end + def get_user(user, params) do + case params do + %{ "user_id" => user_id } -> + case target = Repo.get(User, user_id) do + nil -> + {:error, "No user with such user_id"} + _ -> + {:ok, target} + end + %{ "screen_name" => nickname } -> + case target = Repo.get_by(User, nickname: nickname) do + nil -> + {:error, "No user with such screen_name"} + _ -> + {:ok, target} + end + _ -> + if user do + {:ok, user} + else + {:error, "You need to specify screen_name or user_id"} + end + end + end + defp activities_to_statuses(activities, opts) do Enum.map(activities, fn(activity) -> activity_to_status(activity, opts) diff --git a/lib/pleroma/web/twitter_api/twitter_api_controller.ex b/lib/pleroma/web/twitter_api/twitter_api_controller.ex index 32d352d79..8b5e6270b 100644 --- a/lib/pleroma/web/twitter_api/twitter_api_controller.ex +++ b/lib/pleroma/web/twitter_api/twitter_api_controller.ex @@ -45,6 +45,18 @@ defmodule Pleroma.Web.TwitterAPI.Controller do |> json_reply(200, json) end + def user_timeline(%{assigns: %{user: user}} = conn, params) do + case TwitterAPI.get_user(user, params) do + {:ok, target_user} -> + params = Map.merge(params, %{"actor_id" => target_user.ap_id}) + statuses = TwitterAPI.fetch_user_statuses(user, params) + conn + |> json_reply(200, statuses |> Poison.encode!) + {:error, msg} -> + bad_request_reply(conn, msg) + end + end + def follow(%{assigns: %{user: user}} = conn, %{ "user_id" => followed_id }) do case TwitterAPI.follow(user, followed_id) do { :ok, user, followed, _activity } -> @@ -162,6 +174,11 @@ defmodule Pleroma.Web.TwitterAPI.Controller do |> json_reply(200, response) end + defp bad_request_reply(conn, error_message) do + json = Poison.encode!(%{"error" => error_message}) + json_reply(conn, 400, json) + end + defp json_reply(conn, status, json) do conn |> put_resp_content_type("application/json") |