MastoAPI: add lists.
This commit is contained in:
parent
a8369db4f2
commit
3dbd9809d4
|
@ -35,7 +35,7 @@ def for_user(user, opts) do
|
|||
Repo.all(query)
|
||||
end
|
||||
|
||||
def get(%{id: user_id} = _user, id) do
|
||||
def get(id, %{id: user_id} = _user) do
|
||||
query =
|
||||
from(
|
||||
l in Pleroma.List,
|
||||
|
@ -47,10 +47,12 @@ def get(%{id: user_id} = _user, id) do
|
|||
end
|
||||
|
||||
def get_following(%Pleroma.List{following: following} = list) do
|
||||
q = from(
|
||||
q =
|
||||
from(
|
||||
u in User,
|
||||
where: u.follower_address in ^following
|
||||
)
|
||||
|
||||
{:ok, Repo.all(q)}
|
||||
end
|
||||
|
||||
|
@ -65,7 +67,6 @@ def create(title, %User{} = creator) do
|
|||
Repo.insert(list)
|
||||
end
|
||||
|
||||
# TODO check that user is following followed
|
||||
def follow(%Pleroma.List{following: following} = list, %User{} = followed) do
|
||||
update_follows(list, %{following: Enum.uniq([followed.follower_address | following])})
|
||||
end
|
||||
|
|
|
@ -572,7 +572,7 @@ def get_lists(%{assigns: %{user: user}} = conn, opts) do
|
|||
end
|
||||
|
||||
def get_list(%{assigns: %{user: user}} = conn, %{"id" => id}) do
|
||||
with %Pleroma.List{} = list <- Pleroma.List.get(user, id) do
|
||||
with %Pleroma.List{} = list <- Pleroma.List.get(id, user) do
|
||||
res = ListView.render("list.json", list: list)
|
||||
json(conn, res)
|
||||
else
|
||||
|
@ -581,7 +581,7 @@ def get_list(%{assigns: %{user: user}} = conn, %{"id" => id}) do
|
|||
end
|
||||
|
||||
def delete_list(%{assigns: %{user: user}} = conn, %{"id" => id}) do
|
||||
with %Pleroma.List{} = list <- Pleroma.List.get(user, id),
|
||||
with %Pleroma.List{} = list <- Pleroma.List.get(id, user),
|
||||
{:ok, _list} <- Pleroma.List.delete(list) do
|
||||
json(conn, %{})
|
||||
else
|
||||
|
@ -600,9 +600,9 @@ def create_list(%{assigns: %{user: user}} = conn, %{"title" => title}) do
|
|||
def add_to_list(%{assigns: %{user: user}} = conn, %{"id" => id, "account_ids" => accounts}) do
|
||||
accounts
|
||||
|> Enum.each(fn account_id ->
|
||||
with %Pleroma.List{} = list <- Pleroma.List.get(user, id),
|
||||
with %Pleroma.List{} = list <- Pleroma.List.get(id, user),
|
||||
%User{} = followed <- Repo.get(User, account_id) do
|
||||
ret = Pleroma.List.follow(list, followed)
|
||||
Pleroma.List.follow(list, followed)
|
||||
end
|
||||
end)
|
||||
|
||||
|
@ -612,7 +612,7 @@ def add_to_list(%{assigns: %{user: user}} = conn, %{"id" => id, "account_ids" =>
|
|||
def remove_from_list(%{assigns: %{user: user}} = conn, %{"id" => id, "account_ids" => accounts}) do
|
||||
accounts
|
||||
|> Enum.each(fn account_id ->
|
||||
with %Pleroma.List{} = list <- Pleroma.List.get(user, id),
|
||||
with %Pleroma.List{} = list <- Pleroma.List.get(id, user),
|
||||
%User{} = followed <- Repo.get(Pleroma.User, account_id) do
|
||||
Pleroma.List.unfollow(list, followed)
|
||||
end
|
||||
|
@ -622,14 +622,14 @@ def remove_from_list(%{assigns: %{user: user}} = conn, %{"id" => id, "account_id
|
|||
end
|
||||
|
||||
def list_accounts(%{assigns: %{user: user}} = conn, %{"id" => id}) do
|
||||
with %Pleroma.List{} = list <- Pleroma.List.get(user, id),
|
||||
with %Pleroma.List{} = list <- Pleroma.List.get(id, user),
|
||||
{:ok, users} = Pleroma.List.get_following(list) do
|
||||
render(conn, AccountView, "accounts.json", %{users: users, as: :user})
|
||||
end
|
||||
end
|
||||
|
||||
def rename_list(%{assigns: %{user: user}} = conn, %{"id" => id, "title" => title}) do
|
||||
with %Pleroma.List{} = list <- Pleroma.List.get(user, id),
|
||||
with %Pleroma.List{} = list <- Pleroma.List.get(id, user),
|
||||
{:ok, list} <- Pleroma.List.rename(list, title) do
|
||||
res = ListView.render("list.json", list: list)
|
||||
json(conn, res)
|
||||
|
@ -640,7 +640,7 @@ def rename_list(%{assigns: %{user: user}} = conn, %{"id" => id, "title" => title
|
|||
end
|
||||
|
||||
def list_timeline(%{assigns: %{user: user}} = conn, %{"list_id" => id} = params) do
|
||||
with %Pleroma.List{title: title, following: following} <- Pleroma.List.get(user, id) do
|
||||
with %Pleroma.List{title: title, following: following} <- Pleroma.List.get(id, user) do
|
||||
params =
|
||||
params
|
||||
|> Map.put("type", "Create")
|
||||
|
|
|
@ -0,0 +1,77 @@
|
|||
defmodule Pleroma.ListTest do
|
||||
alias Pleroma.{User, Repo}
|
||||
use Pleroma.DataCase
|
||||
|
||||
import Pleroma.Factory
|
||||
import Ecto.Query
|
||||
|
||||
test "creating a list" do
|
||||
user = insert(:user)
|
||||
{:ok, %Pleroma.List{} = list} = Pleroma.List.create("title", user)
|
||||
%Pleroma.List{title: title} = Pleroma.List.get(list.id, user)
|
||||
assert title == "title"
|
||||
end
|
||||
|
||||
test "getting a list not belonging to the user" do
|
||||
user = insert(:user)
|
||||
other_user = insert(:user)
|
||||
{:ok, %Pleroma.List{} = list} = Pleroma.List.create("title", user)
|
||||
ret = Pleroma.List.get(list.id, other_user)
|
||||
assert is_nil(ret)
|
||||
end
|
||||
|
||||
test "adding an user to a list" do
|
||||
user = insert(:user)
|
||||
other_user = insert(:user)
|
||||
{:ok, list} = Pleroma.List.create("title", user)
|
||||
{:ok, %{following: following}} = Pleroma.List.follow(list, other_user)
|
||||
assert [other_user.follower_address] == following
|
||||
end
|
||||
|
||||
test "removing an user from a list" do
|
||||
user = insert(:user)
|
||||
other_user = insert(:user)
|
||||
{:ok, list} = Pleroma.List.create("title", user)
|
||||
{:ok, %{following: following}} = Pleroma.List.follow(list, other_user)
|
||||
{:ok, %{following: following}} = Pleroma.List.unfollow(list, other_user)
|
||||
assert [] == following
|
||||
end
|
||||
|
||||
test "renaming a list" do
|
||||
user = insert(:user)
|
||||
{:ok, list} = Pleroma.List.create("title", user)
|
||||
{:ok, %{title: title}} = Pleroma.List.rename(list, "new")
|
||||
assert "new" == title
|
||||
end
|
||||
|
||||
test "deleting a list" do
|
||||
user = insert(:user)
|
||||
{:ok, list} = Pleroma.List.create("title", user)
|
||||
{:ok, list} = Pleroma.List.delete(list)
|
||||
assert is_nil(Repo.get(Pleroma.List, list.id))
|
||||
end
|
||||
|
||||
test "getting users in a list" do
|
||||
user = insert(:user)
|
||||
other_user = insert(:user)
|
||||
third_user = insert(:user)
|
||||
{:ok, list} = Pleroma.List.create("title", user)
|
||||
{:ok, list} = Pleroma.List.follow(list, other_user)
|
||||
{:ok, list} = Pleroma.List.follow(list, third_user)
|
||||
{:ok, following} = Pleroma.List.get_following(list)
|
||||
assert other_user in following
|
||||
assert third_user in following
|
||||
end
|
||||
|
||||
test "getting all lists by an user" do
|
||||
user = insert(:user)
|
||||
other_user = insert(:user)
|
||||
{:ok, list_one} = Pleroma.List.create("title", user)
|
||||
{:ok, list_two} = Pleroma.List.create("other title", user)
|
||||
{:ok, list_three} = Pleroma.List.create("third title", other_user)
|
||||
lists = Pleroma.List.for_user(user, %{})
|
||||
assert list_one in lists
|
||||
assert list_two in lists
|
||||
refute list_three in lists
|
||||
end
|
||||
end
|
|
@ -0,0 +1,19 @@
|
|||
defmodule Pleroma.Web.MastodonAPI.ListViewTest do
|
||||
use Pleroma.DataCase
|
||||
import Pleroma.Factory
|
||||
alias Pleroma.Web.MastodonAPI.ListView
|
||||
alias Pleroma.List
|
||||
|
||||
test "Represent a list" do
|
||||
user = insert(:user)
|
||||
title = "mortal enemies"
|
||||
{:ok, list} = Pleroma.List.create(title, user)
|
||||
|
||||
expected = %{
|
||||
id: to_string(list.id),
|
||||
title: title
|
||||
}
|
||||
|
||||
assert expected == ListView.render("list.json", %{list: list})
|
||||
end
|
||||
end
|
|
@ -161,6 +161,125 @@ test "when you didn't create it", %{conn: conn} do
|
|||
end
|
||||
end
|
||||
|
||||
describe "lists" do
|
||||
test "creating a list", %{conn: conn} do
|
||||
user = insert(:user)
|
||||
|
||||
conn =
|
||||
conn
|
||||
|> assign(:user, user)
|
||||
|> post("/api/v1/lists", %{"title" => "cuties"})
|
||||
|
||||
assert %{"title" => title} = json_response(conn, 200)
|
||||
assert title == "cuties"
|
||||
end
|
||||
|
||||
test "adding users to a list", %{conn: conn} do
|
||||
user = insert(:user)
|
||||
other_user = insert(:user)
|
||||
{:ok, list} = Pleroma.List.create("name", user)
|
||||
|
||||
conn =
|
||||
conn
|
||||
|> assign(:user, user)
|
||||
|> post("/api/v1/lists/#{list.id}/accounts", %{"account_ids" => [other_user.id]})
|
||||
|
||||
assert %{} == json_response(conn, 200)
|
||||
%Pleroma.List{following: following} = Pleroma.List.get(list.id, user)
|
||||
assert following == [other_user.follower_address]
|
||||
end
|
||||
|
||||
test "removing users from a list", %{conn: conn} do
|
||||
user = insert(:user)
|
||||
other_user = insert(:user)
|
||||
third_user = insert(:user)
|
||||
{:ok, list} = Pleroma.List.create("name", user)
|
||||
{:ok, list} = Pleroma.List.follow(list, other_user)
|
||||
{:ok, list} = Pleroma.List.follow(list, third_user)
|
||||
|
||||
conn =
|
||||
conn
|
||||
|> assign(:user, user)
|
||||
|> delete("/api/v1/lists/#{list.id}/accounts", %{"account_ids" => [other_user.id]})
|
||||
|
||||
assert %{} == json_response(conn, 200)
|
||||
%Pleroma.List{following: following} = Pleroma.List.get(list.id, user)
|
||||
assert following == [third_user.follower_address]
|
||||
end
|
||||
|
||||
test "listing users in a list", %{conn: conn} do
|
||||
user = insert(:user)
|
||||
other_user = insert(:user)
|
||||
{:ok, list} = Pleroma.List.create("name", user)
|
||||
{:ok, list} = Pleroma.List.follow(list, other_user)
|
||||
|
||||
conn =
|
||||
conn
|
||||
|> assign(:user, user)
|
||||
|> get("/api/v1/lists/#{list.id}/accounts", %{"account_ids" => [other_user.id]})
|
||||
|
||||
assert [%{"id" => id}] = json_response(conn, 200)
|
||||
assert id == to_string(other_user.id)
|
||||
end
|
||||
|
||||
test "retrieving a list", %{conn: conn} do
|
||||
user = insert(:user)
|
||||
{:ok, list} = Pleroma.List.create("name", user)
|
||||
|
||||
conn =
|
||||
conn
|
||||
|> assign(:user, user)
|
||||
|> get("/api/v1/lists/#{list.id}")
|
||||
|
||||
assert %{"id" => id} = json_response(conn, 200)
|
||||
assert id == to_string(list.id)
|
||||
end
|
||||
|
||||
test "renaming a list", %{conn: conn} do
|
||||
user = insert(:user)
|
||||
{:ok, list} = Pleroma.List.create("name", user)
|
||||
|
||||
conn =
|
||||
conn
|
||||
|> assign(:user, user)
|
||||
|> put("/api/v1/lists/#{list.id}", %{"title" => "newname"})
|
||||
|
||||
assert %{"title" => name} = json_response(conn, 200)
|
||||
assert name == "newname"
|
||||
end
|
||||
|
||||
test "deleting a list", %{conn: conn} do
|
||||
user = insert(:user)
|
||||
{:ok, list} = Pleroma.List.create("name", user)
|
||||
|
||||
conn =
|
||||
conn
|
||||
|> assign(:user, user)
|
||||
|> delete("/api/v1/lists/#{list.id}")
|
||||
|
||||
assert %{} = json_response(conn, 200)
|
||||
assert is_nil(Repo.get(Pleroma.List, list.id))
|
||||
end
|
||||
|
||||
test "list timeline", %{conn: conn} do
|
||||
user = insert(:user)
|
||||
other_user = insert(:user)
|
||||
{:ok, activity_one} = TwitterAPI.create_status(user, %{"status" => "Marisa is cute."})
|
||||
{:ok, activity_two} = TwitterAPI.create_status(other_user, %{"status" => "Marisa is cute."})
|
||||
{:ok, list} = Pleroma.List.create("name", user)
|
||||
{:ok, list} = Pleroma.List.follow(list, other_user)
|
||||
|
||||
conn =
|
||||
conn
|
||||
|> assign(:user, user)
|
||||
|> get("/api/v1/timelines/list/#{list.id}")
|
||||
|
||||
assert [%{"id" => id}] = json_response(conn, 200)
|
||||
|
||||
assert id == to_string(activity_two.id)
|
||||
end
|
||||
end
|
||||
|
||||
describe "notifications" do
|
||||
test "list of notifications", %{conn: conn} do
|
||||
user = insert(:user)
|
||||
|
|
Loading…
Reference in New Issue