forked from AkkomaGang/akkoma
Add relationships to masto api.
This commit is contained in:
parent
f03524805f
commit
49929321c7
5 changed files with 65 additions and 0 deletions
|
@ -7,6 +7,7 @@ defmodule Pleroma.Web.MastodonAPI.MastodonAPIController do
|
||||||
alias Pleroma.Web.ActivityPub.ActivityPub
|
alias Pleroma.Web.ActivityPub.ActivityPub
|
||||||
alias Pleroma.Web.TwitterAPI.TwitterAPI
|
alias Pleroma.Web.TwitterAPI.TwitterAPI
|
||||||
alias Pleroma.Web.CommonAPI
|
alias Pleroma.Web.CommonAPI
|
||||||
|
import Ecto.Query
|
||||||
import Logger
|
import Logger
|
||||||
|
|
||||||
def create_app(conn, params) do
|
def create_app(conn, params) do
|
||||||
|
@ -177,6 +178,14 @@ def notifications(%{assigns: %{user: user}} = conn, params) do
|
||||||
|> json(result)
|
|> json(result)
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def relationships(%{assigns: %{user: user}} = conn, %{"id" => id}) do
|
||||||
|
id = List.wrap(id)
|
||||||
|
q = from u in User,
|
||||||
|
where: u.id in ^id
|
||||||
|
targets = Repo.all(q)
|
||||||
|
render conn, AccountView, "relationships.json", %{user: user, targets: targets}
|
||||||
|
end
|
||||||
|
|
||||||
def empty_array(conn, _) do
|
def empty_array(conn, _) do
|
||||||
Logger.debug("Unimplemented, returning an empty array")
|
Logger.debug("Unimplemented, returning an empty array")
|
||||||
json(conn, [])
|
json(conn, [])
|
||||||
|
|
|
@ -1,6 +1,7 @@
|
||||||
defmodule Pleroma.Web.MastodonAPI.AccountView do
|
defmodule Pleroma.Web.MastodonAPI.AccountView do
|
||||||
use Pleroma.Web, :view
|
use Pleroma.Web, :view
|
||||||
alias Pleroma.User
|
alias Pleroma.User
|
||||||
|
alias Pleroma.Web.MastodonAPI.AccountView
|
||||||
|
|
||||||
defp image_url(%{"url" => [ %{ "href" => href } | t ]}), do: href
|
defp image_url(%{"url" => [ %{ "href" => href } | t ]}), do: href
|
||||||
defp image_url(_), do: nil
|
defp image_url(_), do: nil
|
||||||
|
@ -38,4 +39,20 @@ def render("mention.json", %{user: user}) do
|
||||||
url: user.ap_id
|
url: user.ap_id
|
||||||
}
|
}
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def render("relationship.json", %{user: user, target: target}) do
|
||||||
|
%{
|
||||||
|
id: target.id,
|
||||||
|
following: User.following?(target, user),
|
||||||
|
followed_by: User.following?(user, target),
|
||||||
|
blocking: false,
|
||||||
|
muting: false,
|
||||||
|
requested: false,
|
||||||
|
domain_blocking: false
|
||||||
|
}
|
||||||
|
end
|
||||||
|
|
||||||
|
def render("relationships.json", %{user: user, targets: targets}) do
|
||||||
|
render_many(targets, AccountView, "relationship.json", user: user, as: :target)
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|
|
@ -56,6 +56,8 @@ def user_fetcher(username) do
|
||||||
pipe_through :authenticated_api
|
pipe_through :authenticated_api
|
||||||
|
|
||||||
get "/accounts/verify_credentials", MastodonAPIController, :verify_credentials
|
get "/accounts/verify_credentials", MastodonAPIController, :verify_credentials
|
||||||
|
get "/accounts/relationships", MastodonAPIController, :relationships
|
||||||
|
|
||||||
get "/timelines/home", MastodonAPIController, :home_timeline
|
get "/timelines/home", MastodonAPIController, :home_timeline
|
||||||
|
|
||||||
post "/statuses", MastodonAPIController, :post_status
|
post "/statuses", MastodonAPIController, :post_status
|
||||||
|
|
|
@ -2,6 +2,7 @@ defmodule Pleroma.Web.MastodonAPI.AccountViewTest do
|
||||||
use Pleroma.DataCase
|
use Pleroma.DataCase
|
||||||
import Pleroma.Factory
|
import Pleroma.Factory
|
||||||
alias Pleroma.Web.MastodonAPI.AccountView
|
alias Pleroma.Web.MastodonAPI.AccountView
|
||||||
|
alias Pleroma.User
|
||||||
|
|
||||||
test "Represent a user account" do
|
test "Represent a user account" do
|
||||||
user = insert(:user, %{info: %{"note_count" => 5, "follower_count" => 3}, nickname: "shp@shitposter.club"})
|
user = insert(:user, %{info: %{"note_count" => 5, "follower_count" => 3}, nickname: "shp@shitposter.club"})
|
||||||
|
@ -39,4 +40,23 @@ test "Represent a smaller mention" do
|
||||||
|
|
||||||
assert expected == AccountView.render("mention.json", %{user: user})
|
assert expected == AccountView.render("mention.json", %{user: user})
|
||||||
end
|
end
|
||||||
|
|
||||||
|
test "represent a relationship" do
|
||||||
|
user = insert(:user)
|
||||||
|
other_user = insert(:user)
|
||||||
|
|
||||||
|
{:ok, user} = User.follow(user, other_user)
|
||||||
|
|
||||||
|
expected = %{
|
||||||
|
id: other_user.id,
|
||||||
|
following: false,
|
||||||
|
followed_by: true,
|
||||||
|
blocking: false,
|
||||||
|
muting: false,
|
||||||
|
requested: false,
|
||||||
|
domain_blocking: false
|
||||||
|
}
|
||||||
|
|
||||||
|
assert expected == AccountView.render("relationship.json", %{user: user, target: other_user})
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|
|
@ -181,4 +181,21 @@ test "gets a users statuses", %{conn: conn} do
|
||||||
assert id == note_two.id
|
assert id == note_two.id
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
describe "user relationships" do
|
||||||
|
test "returns the relationships for the current user", %{conn: conn} do
|
||||||
|
user = insert(:user)
|
||||||
|
other_user = insert(:user)
|
||||||
|
|
||||||
|
{:ok, user} = User.follow(user, other_user)
|
||||||
|
|
||||||
|
conn = conn
|
||||||
|
|> assign(:user, user)
|
||||||
|
|> get("/api/v1/accounts/relationships", %{"id" => [other_user.id]})
|
||||||
|
|
||||||
|
assert [relationship] = json_response(conn, 200)
|
||||||
|
|
||||||
|
assert other_user.id == relationship["id"]
|
||||||
|
end
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|
Loading…
Reference in a new issue