From: Roger Braun Date: Thu, 20 Jul 2017 18:29:15 +0000 (+0200) Subject: Add followers endpoint. X-Git-Url: https://git.squeep.com/?a=commitdiff_plain;h=94a394e3a8949dde95e7e6cc65b46374dd9412a2;p=akkoma Add followers endpoint. --- diff --git a/lib/pleroma/web/router.ex b/lib/pleroma/web/router.ex index d92ee29ba..84a39d817 100644 --- a/lib/pleroma/web/router.ex +++ b/lib/pleroma/web/router.ex @@ -70,6 +70,8 @@ defmodule Pleroma.Web.Router do post "/favorites/destroy/:id", TwitterAPI.Controller, :unfavorite post "/qvitter/update_avatar", TwitterAPI.Controller, :update_avatar + + get "/statuses/followers", TwitterAPI.Controller, :followers end pipeline :ostatus do diff --git a/lib/pleroma/web/twitter_api/twitter_api_controller.ex b/lib/pleroma/web/twitter_api/twitter_api_controller.ex index 2b29b6ccf..ea3030676 100644 --- a/lib/pleroma/web/twitter_api/twitter_api_controller.ex +++ b/lib/pleroma/web/twitter_api/twitter_api_controller.ex @@ -210,6 +210,14 @@ defmodule Pleroma.Web.TwitterAPI.Controller do end end + def followers(%{assigns: %{user: user}} = conn, _params) do + with {:ok, followers} <- User.get_followers(user) do + render(conn, UserView, "index.json", %{users: followers, for: user}) + else + _e -> bad_request_reply(conn, "Can't get followers") + end + end + defp bad_request_reply(conn, error_message) do json = error_json(conn, error_message) json_reply(conn, 400, json) diff --git a/lib/pleroma/web/twitter_api/views/user_view.ex b/lib/pleroma/web/twitter_api/views/user_view.ex index fdab5be31..24d2981fa 100644 --- a/lib/pleroma/web/twitter_api/views/user_view.ex +++ b/lib/pleroma/web/twitter_api/views/user_view.ex @@ -4,6 +4,14 @@ defmodule Pleroma.Web.TwitterAPI.UserView do alias Pleroma.Web.TwitterAPI.Utils def render("show.json", %{user: user = %User{}} = assigns) do + render_one(user, Pleroma.Web.TwitterAPI.UserView, "user.json", assigns) + end + + def render("index.json", %{users: users, for: user}) do + render_many(users, Pleroma.Web.TwitterAPI.UserView, "user.json", for: user) + end + + def render("user.json", %{user: user = %User{}} = assigns) do image = User.avatar_url(user) following = if assigns[:for] do User.following?(assigns[:for], user) diff --git a/test/web/twitter_api/twitter_api_controller_test.exs b/test/web/twitter_api/twitter_api_controller_test.exs index 0281ea55b..4bf1c928b 100644 --- a/test/web/twitter_api/twitter_api_controller_test.exs +++ b/test/web/twitter_api/twitter_api_controller_test.exs @@ -419,6 +419,24 @@ defmodule Pleroma.Web.TwitterAPI.ControllerTest do end end + describe "GET /api/statuses/followers" do + test "it returns a user's followers", %{conn: conn} do + user = insert(:user) + follower_one = insert(:user) + follower_two = insert(:user) + not_follower = insert(:user) + + {:ok, follower_one} = User.follow(follower_one, user) + {:ok, follower_two} = User.follow(follower_two, user) + + conn = conn + |> assign(:user, user) + |> get("/api/statuses/followers") + + assert json_response(conn, 200) == UserView.render("index.json", %{users: [follower_one, follower_two], for: user}) + end + end + defp valid_user(_context) do user = insert(:user) [user: user]