mirror of
https://git.pleroma.social/pleroma/pleroma.git
synced 2024-12-23 16:40:29 +00:00
Extract filter actions from MastodonAPIController
to FilterController
This commit is contained in:
parent
91e2bcf218
commit
621377f378
6 changed files with 217 additions and 198 deletions
|
@ -0,0 +1,72 @@
|
||||||
|
# Pleroma: A lightweight social networking server
|
||||||
|
# Copyright © 2017-2019 Pleroma Authors <https://pleroma.social/>
|
||||||
|
# SPDX-License-Identifier: AGPL-3.0-only
|
||||||
|
|
||||||
|
defmodule Pleroma.Web.MastodonAPI.FilterController do
|
||||||
|
use Pleroma.Web, :controller
|
||||||
|
|
||||||
|
alias Pleroma.Filter
|
||||||
|
|
||||||
|
@doc "GET /api/v1/filters"
|
||||||
|
def index(%{assigns: %{user: user}} = conn, _) do
|
||||||
|
filters = Filter.get_filters(user)
|
||||||
|
|
||||||
|
render(conn, "filters.json", filters: filters)
|
||||||
|
end
|
||||||
|
|
||||||
|
@doc "POST /api/v1/filters"
|
||||||
|
def create(
|
||||||
|
%{assigns: %{user: user}} = conn,
|
||||||
|
%{"phrase" => phrase, "context" => context} = params
|
||||||
|
) do
|
||||||
|
query = %Filter{
|
||||||
|
user_id: user.id,
|
||||||
|
phrase: phrase,
|
||||||
|
context: context,
|
||||||
|
hide: Map.get(params, "irreversible", false),
|
||||||
|
whole_word: Map.get(params, "boolean", true)
|
||||||
|
# expires_at
|
||||||
|
}
|
||||||
|
|
||||||
|
{:ok, response} = Filter.create(query)
|
||||||
|
|
||||||
|
render(conn, "filter.json", filter: response)
|
||||||
|
end
|
||||||
|
|
||||||
|
@doc "GET /api/v1/filters/:id"
|
||||||
|
def show(%{assigns: %{user: user}} = conn, %{"id" => filter_id}) do
|
||||||
|
filter = Filter.get(filter_id, user)
|
||||||
|
|
||||||
|
render(conn, "filter.json", filter: filter)
|
||||||
|
end
|
||||||
|
|
||||||
|
@doc "PUT /api/v1/filters/:id"
|
||||||
|
def update(
|
||||||
|
%{assigns: %{user: user}} = conn,
|
||||||
|
%{"phrase" => phrase, "context" => context, "id" => filter_id} = params
|
||||||
|
) do
|
||||||
|
query = %Filter{
|
||||||
|
user_id: user.id,
|
||||||
|
filter_id: filter_id,
|
||||||
|
phrase: phrase,
|
||||||
|
context: context,
|
||||||
|
hide: Map.get(params, "irreversible", nil),
|
||||||
|
whole_word: Map.get(params, "boolean", true)
|
||||||
|
# expires_at
|
||||||
|
}
|
||||||
|
|
||||||
|
{:ok, response} = Filter.update(query)
|
||||||
|
render(conn, "filter.json", filter: response)
|
||||||
|
end
|
||||||
|
|
||||||
|
@doc "DELETE /api/v1/filters/:id"
|
||||||
|
def delete(%{assigns: %{user: user}} = conn, %{"id" => filter_id}) do
|
||||||
|
query = %Filter{
|
||||||
|
user_id: user.id,
|
||||||
|
filter_id: filter_id
|
||||||
|
}
|
||||||
|
|
||||||
|
{:ok, _} = Filter.delete(query)
|
||||||
|
json(conn, %{})
|
||||||
|
end
|
||||||
|
end
|
|
@ -14,7 +14,6 @@ defmodule Pleroma.Web.MastodonAPI.MastodonAPIController do
|
||||||
alias Pleroma.Config
|
alias Pleroma.Config
|
||||||
alias Pleroma.Conversation.Participation
|
alias Pleroma.Conversation.Participation
|
||||||
alias Pleroma.Emoji
|
alias Pleroma.Emoji
|
||||||
alias Pleroma.Filter
|
|
||||||
alias Pleroma.HTTP
|
alias Pleroma.HTTP
|
||||||
alias Pleroma.Object
|
alias Pleroma.Object
|
||||||
alias Pleroma.Pagination
|
alias Pleroma.Pagination
|
||||||
|
@ -30,7 +29,6 @@ defmodule Pleroma.Web.MastodonAPI.MastodonAPIController do
|
||||||
alias Pleroma.Web.MastodonAPI.AccountView
|
alias Pleroma.Web.MastodonAPI.AccountView
|
||||||
alias Pleroma.Web.MastodonAPI.AppView
|
alias Pleroma.Web.MastodonAPI.AppView
|
||||||
alias Pleroma.Web.MastodonAPI.ConversationView
|
alias Pleroma.Web.MastodonAPI.ConversationView
|
||||||
alias Pleroma.Web.MastodonAPI.FilterView
|
|
||||||
alias Pleroma.Web.MastodonAPI.ListView
|
alias Pleroma.Web.MastodonAPI.ListView
|
||||||
alias Pleroma.Web.MastodonAPI.MastodonAPI
|
alias Pleroma.Web.MastodonAPI.MastodonAPI
|
||||||
alias Pleroma.Web.MastodonAPI.MastodonView
|
alias Pleroma.Web.MastodonAPI.MastodonView
|
||||||
|
@ -1040,65 +1038,6 @@ defmodule Pleroma.Web.MastodonAPI.MastodonAPIController do
|
||||||
json(conn, %{})
|
json(conn, %{})
|
||||||
end
|
end
|
||||||
|
|
||||||
def get_filters(%{assigns: %{user: user}} = conn, _) do
|
|
||||||
filters = Filter.get_filters(user)
|
|
||||||
res = FilterView.render("filters.json", filters: filters)
|
|
||||||
json(conn, res)
|
|
||||||
end
|
|
||||||
|
|
||||||
def create_filter(
|
|
||||||
%{assigns: %{user: user}} = conn,
|
|
||||||
%{"phrase" => phrase, "context" => context} = params
|
|
||||||
) do
|
|
||||||
query = %Filter{
|
|
||||||
user_id: user.id,
|
|
||||||
phrase: phrase,
|
|
||||||
context: context,
|
|
||||||
hide: Map.get(params, "irreversible", false),
|
|
||||||
whole_word: Map.get(params, "boolean", true)
|
|
||||||
# expires_at
|
|
||||||
}
|
|
||||||
|
|
||||||
{:ok, response} = Filter.create(query)
|
|
||||||
res = FilterView.render("filter.json", filter: response)
|
|
||||||
json(conn, res)
|
|
||||||
end
|
|
||||||
|
|
||||||
def get_filter(%{assigns: %{user: user}} = conn, %{"id" => filter_id}) do
|
|
||||||
filter = Filter.get(filter_id, user)
|
|
||||||
res = FilterView.render("filter.json", filter: filter)
|
|
||||||
json(conn, res)
|
|
||||||
end
|
|
||||||
|
|
||||||
def update_filter(
|
|
||||||
%{assigns: %{user: user}} = conn,
|
|
||||||
%{"phrase" => phrase, "context" => context, "id" => filter_id} = params
|
|
||||||
) do
|
|
||||||
query = %Filter{
|
|
||||||
user_id: user.id,
|
|
||||||
filter_id: filter_id,
|
|
||||||
phrase: phrase,
|
|
||||||
context: context,
|
|
||||||
hide: Map.get(params, "irreversible", nil),
|
|
||||||
whole_word: Map.get(params, "boolean", true)
|
|
||||||
# expires_at
|
|
||||||
}
|
|
||||||
|
|
||||||
{:ok, response} = Filter.update(query)
|
|
||||||
res = FilterView.render("filter.json", filter: response)
|
|
||||||
json(conn, res)
|
|
||||||
end
|
|
||||||
|
|
||||||
def delete_filter(%{assigns: %{user: user}} = conn, %{"id" => filter_id}) do
|
|
||||||
query = %Filter{
|
|
||||||
user_id: user.id,
|
|
||||||
filter_id: filter_id
|
|
||||||
}
|
|
||||||
|
|
||||||
{:ok, _} = Filter.delete(query)
|
|
||||||
json(conn, %{})
|
|
||||||
end
|
|
||||||
|
|
||||||
def suggestions(%{assigns: %{user: user}} = conn, _) do
|
def suggestions(%{assigns: %{user: user}} = conn, _) do
|
||||||
suggestions = Config.get(:suggestions)
|
suggestions = Config.get(:suggestions)
|
||||||
|
|
||||||
|
|
|
@ -36,19 +36,19 @@ defmodule Pleroma.Web.PleromaAPI.SubscriptionNotificationView do
|
||||||
"mention" ->
|
"mention" ->
|
||||||
response
|
response
|
||||||
|> Map.merge(%{
|
|> Map.merge(%{
|
||||||
status: StatusView.render("status.json", %{activity: activity, for: user})
|
status: StatusView.render("show.json", %{activity: activity, for: user})
|
||||||
})
|
})
|
||||||
|
|
||||||
"favourite" ->
|
"favourite" ->
|
||||||
response
|
response
|
||||||
|> Map.merge(%{
|
|> Map.merge(%{
|
||||||
status: StatusView.render("status.json", %{activity: parent_activity, for: user})
|
status: StatusView.render("show.json", %{activity: parent_activity, for: user})
|
||||||
})
|
})
|
||||||
|
|
||||||
"reblog" ->
|
"reblog" ->
|
||||||
response
|
response
|
||||||
|> Map.merge(%{
|
|> Map.merge(%{
|
||||||
status: StatusView.render("status.json", %{activity: parent_activity, for: user})
|
status: StatusView.render("show.json", %{activity: parent_activity, for: user})
|
||||||
})
|
})
|
||||||
|
|
||||||
"follow" ->
|
"follow" ->
|
||||||
|
|
|
@ -348,7 +348,7 @@ defmodule Pleroma.Web.Router do
|
||||||
|
|
||||||
get("/domain_blocks", MastodonAPIController, :domain_blocks)
|
get("/domain_blocks", MastodonAPIController, :domain_blocks)
|
||||||
|
|
||||||
get("/filters", MastodonAPIController, :get_filters)
|
get("/filters", FilterController, :index)
|
||||||
|
|
||||||
get("/suggestions", MastodonAPIController, :suggestions)
|
get("/suggestions", MastodonAPIController, :suggestions)
|
||||||
|
|
||||||
|
@ -392,10 +392,10 @@ defmodule Pleroma.Web.Router do
|
||||||
post("/lists/:id/accounts", ListController, :add_to_list)
|
post("/lists/:id/accounts", ListController, :add_to_list)
|
||||||
delete("/lists/:id/accounts", ListController, :remove_from_list)
|
delete("/lists/:id/accounts", ListController, :remove_from_list)
|
||||||
|
|
||||||
post("/filters", MastodonAPIController, :create_filter)
|
post("/filters", FilterController, :create)
|
||||||
get("/filters/:id", MastodonAPIController, :get_filter)
|
get("/filters/:id", FilterController, :show)
|
||||||
put("/filters/:id", MastodonAPIController, :update_filter)
|
put("/filters/:id", FilterController, :update)
|
||||||
delete("/filters/:id", MastodonAPIController, :delete_filter)
|
delete("/filters/:id", FilterController, :delete)
|
||||||
|
|
||||||
patch("/pleroma/accounts/update_avatar", MastodonAPIController, :update_avatar)
|
patch("/pleroma/accounts/update_avatar", MastodonAPIController, :update_avatar)
|
||||||
patch("/pleroma/accounts/update_banner", MastodonAPIController, :update_banner)
|
patch("/pleroma/accounts/update_banner", MastodonAPIController, :update_banner)
|
||||||
|
|
137
test/web/mastodon_api/controllers/filter_controller_test.exs
Normal file
137
test/web/mastodon_api/controllers/filter_controller_test.exs
Normal file
|
@ -0,0 +1,137 @@
|
||||||
|
# Pleroma: A lightweight social networking server
|
||||||
|
# Copyright © 2017-2019 Pleroma Authors <https://pleroma.social/>
|
||||||
|
# SPDX-License-Identifier: AGPL-3.0-only
|
||||||
|
|
||||||
|
defmodule Pleroma.Web.MastodonAPI.FilterControllerTest do
|
||||||
|
use Pleroma.Web.ConnCase, async: true
|
||||||
|
|
||||||
|
alias Pleroma.Web.MastodonAPI.FilterView
|
||||||
|
|
||||||
|
import Pleroma.Factory
|
||||||
|
|
||||||
|
test "creating a filter", %{conn: conn} do
|
||||||
|
user = insert(:user)
|
||||||
|
|
||||||
|
filter = %Pleroma.Filter{
|
||||||
|
phrase: "knights",
|
||||||
|
context: ["home"]
|
||||||
|
}
|
||||||
|
|
||||||
|
conn =
|
||||||
|
conn
|
||||||
|
|> assign(:user, user)
|
||||||
|
|> post("/api/v1/filters", %{"phrase" => filter.phrase, context: filter.context})
|
||||||
|
|
||||||
|
assert response = json_response(conn, 200)
|
||||||
|
assert response["phrase"] == filter.phrase
|
||||||
|
assert response["context"] == filter.context
|
||||||
|
assert response["irreversible"] == false
|
||||||
|
assert response["id"] != nil
|
||||||
|
assert response["id"] != ""
|
||||||
|
end
|
||||||
|
|
||||||
|
test "fetching a list of filters", %{conn: conn} do
|
||||||
|
user = insert(:user)
|
||||||
|
|
||||||
|
query_one = %Pleroma.Filter{
|
||||||
|
user_id: user.id,
|
||||||
|
filter_id: 1,
|
||||||
|
phrase: "knights",
|
||||||
|
context: ["home"]
|
||||||
|
}
|
||||||
|
|
||||||
|
query_two = %Pleroma.Filter{
|
||||||
|
user_id: user.id,
|
||||||
|
filter_id: 2,
|
||||||
|
phrase: "who",
|
||||||
|
context: ["home"]
|
||||||
|
}
|
||||||
|
|
||||||
|
{:ok, filter_one} = Pleroma.Filter.create(query_one)
|
||||||
|
{:ok, filter_two} = Pleroma.Filter.create(query_two)
|
||||||
|
|
||||||
|
response =
|
||||||
|
conn
|
||||||
|
|> assign(:user, user)
|
||||||
|
|> get("/api/v1/filters")
|
||||||
|
|> json_response(200)
|
||||||
|
|
||||||
|
assert response ==
|
||||||
|
render_json(
|
||||||
|
FilterView,
|
||||||
|
"filters.json",
|
||||||
|
filters: [filter_two, filter_one]
|
||||||
|
)
|
||||||
|
end
|
||||||
|
|
||||||
|
test "get a filter", %{conn: conn} do
|
||||||
|
user = insert(:user)
|
||||||
|
|
||||||
|
query = %Pleroma.Filter{
|
||||||
|
user_id: user.id,
|
||||||
|
filter_id: 2,
|
||||||
|
phrase: "knight",
|
||||||
|
context: ["home"]
|
||||||
|
}
|
||||||
|
|
||||||
|
{:ok, filter} = Pleroma.Filter.create(query)
|
||||||
|
|
||||||
|
conn =
|
||||||
|
conn
|
||||||
|
|> assign(:user, user)
|
||||||
|
|> get("/api/v1/filters/#{filter.filter_id}")
|
||||||
|
|
||||||
|
assert _response = json_response(conn, 200)
|
||||||
|
end
|
||||||
|
|
||||||
|
test "update a filter", %{conn: conn} do
|
||||||
|
user = insert(:user)
|
||||||
|
|
||||||
|
query = %Pleroma.Filter{
|
||||||
|
user_id: user.id,
|
||||||
|
filter_id: 2,
|
||||||
|
phrase: "knight",
|
||||||
|
context: ["home"]
|
||||||
|
}
|
||||||
|
|
||||||
|
{:ok, _filter} = Pleroma.Filter.create(query)
|
||||||
|
|
||||||
|
new = %Pleroma.Filter{
|
||||||
|
phrase: "nii",
|
||||||
|
context: ["home"]
|
||||||
|
}
|
||||||
|
|
||||||
|
conn =
|
||||||
|
conn
|
||||||
|
|> assign(:user, user)
|
||||||
|
|> put("/api/v1/filters/#{query.filter_id}", %{
|
||||||
|
phrase: new.phrase,
|
||||||
|
context: new.context
|
||||||
|
})
|
||||||
|
|
||||||
|
assert response = json_response(conn, 200)
|
||||||
|
assert response["phrase"] == new.phrase
|
||||||
|
assert response["context"] == new.context
|
||||||
|
end
|
||||||
|
|
||||||
|
test "delete a filter", %{conn: conn} do
|
||||||
|
user = insert(:user)
|
||||||
|
|
||||||
|
query = %Pleroma.Filter{
|
||||||
|
user_id: user.id,
|
||||||
|
filter_id: 2,
|
||||||
|
phrase: "knight",
|
||||||
|
context: ["home"]
|
||||||
|
}
|
||||||
|
|
||||||
|
{:ok, filter} = Pleroma.Filter.create(query)
|
||||||
|
|
||||||
|
conn =
|
||||||
|
conn
|
||||||
|
|> assign(:user, user)
|
||||||
|
|> delete("/api/v1/filters/#{filter.filter_id}")
|
||||||
|
|
||||||
|
assert response = json_response(conn, 200)
|
||||||
|
assert response == %{}
|
||||||
|
end
|
||||||
|
end
|
|
@ -16,7 +16,6 @@ defmodule Pleroma.Web.MastodonAPI.MastodonAPIControllerTest do
|
||||||
alias Pleroma.User
|
alias Pleroma.User
|
||||||
alias Pleroma.Web.ActivityPub.ActivityPub
|
alias Pleroma.Web.ActivityPub.ActivityPub
|
||||||
alias Pleroma.Web.CommonAPI
|
alias Pleroma.Web.CommonAPI
|
||||||
alias Pleroma.Web.MastodonAPI.FilterView
|
|
||||||
alias Pleroma.Web.OAuth.App
|
alias Pleroma.Web.OAuth.App
|
||||||
alias Pleroma.Web.OAuth.Token
|
alias Pleroma.Web.OAuth.Token
|
||||||
alias Pleroma.Web.Push
|
alias Pleroma.Web.Push
|
||||||
|
@ -268,134 +267,6 @@ defmodule Pleroma.Web.MastodonAPI.MastodonAPIControllerTest do
|
||||||
assert expected == json_response(conn, 200)
|
assert expected == json_response(conn, 200)
|
||||||
end
|
end
|
||||||
|
|
||||||
describe "filters" do
|
|
||||||
test "creating a filter", %{conn: conn} do
|
|
||||||
user = insert(:user)
|
|
||||||
|
|
||||||
filter = %Pleroma.Filter{
|
|
||||||
phrase: "knights",
|
|
||||||
context: ["home"]
|
|
||||||
}
|
|
||||||
|
|
||||||
conn =
|
|
||||||
conn
|
|
||||||
|> assign(:user, user)
|
|
||||||
|> post("/api/v1/filters", %{"phrase" => filter.phrase, context: filter.context})
|
|
||||||
|
|
||||||
assert response = json_response(conn, 200)
|
|
||||||
assert response["phrase"] == filter.phrase
|
|
||||||
assert response["context"] == filter.context
|
|
||||||
assert response["irreversible"] == false
|
|
||||||
assert response["id"] != nil
|
|
||||||
assert response["id"] != ""
|
|
||||||
end
|
|
||||||
|
|
||||||
test "fetching a list of filters", %{conn: conn} do
|
|
||||||
user = insert(:user)
|
|
||||||
|
|
||||||
query_one = %Pleroma.Filter{
|
|
||||||
user_id: user.id,
|
|
||||||
filter_id: 1,
|
|
||||||
phrase: "knights",
|
|
||||||
context: ["home"]
|
|
||||||
}
|
|
||||||
|
|
||||||
query_two = %Pleroma.Filter{
|
|
||||||
user_id: user.id,
|
|
||||||
filter_id: 2,
|
|
||||||
phrase: "who",
|
|
||||||
context: ["home"]
|
|
||||||
}
|
|
||||||
|
|
||||||
{:ok, filter_one} = Pleroma.Filter.create(query_one)
|
|
||||||
{:ok, filter_two} = Pleroma.Filter.create(query_two)
|
|
||||||
|
|
||||||
response =
|
|
||||||
conn
|
|
||||||
|> assign(:user, user)
|
|
||||||
|> get("/api/v1/filters")
|
|
||||||
|> json_response(200)
|
|
||||||
|
|
||||||
assert response ==
|
|
||||||
render_json(
|
|
||||||
FilterView,
|
|
||||||
"filters.json",
|
|
||||||
filters: [filter_two, filter_one]
|
|
||||||
)
|
|
||||||
end
|
|
||||||
|
|
||||||
test "get a filter", %{conn: conn} do
|
|
||||||
user = insert(:user)
|
|
||||||
|
|
||||||
query = %Pleroma.Filter{
|
|
||||||
user_id: user.id,
|
|
||||||
filter_id: 2,
|
|
||||||
phrase: "knight",
|
|
||||||
context: ["home"]
|
|
||||||
}
|
|
||||||
|
|
||||||
{:ok, filter} = Pleroma.Filter.create(query)
|
|
||||||
|
|
||||||
conn =
|
|
||||||
conn
|
|
||||||
|> assign(:user, user)
|
|
||||||
|> get("/api/v1/filters/#{filter.filter_id}")
|
|
||||||
|
|
||||||
assert _response = json_response(conn, 200)
|
|
||||||
end
|
|
||||||
|
|
||||||
test "update a filter", %{conn: conn} do
|
|
||||||
user = insert(:user)
|
|
||||||
|
|
||||||
query = %Pleroma.Filter{
|
|
||||||
user_id: user.id,
|
|
||||||
filter_id: 2,
|
|
||||||
phrase: "knight",
|
|
||||||
context: ["home"]
|
|
||||||
}
|
|
||||||
|
|
||||||
{:ok, _filter} = Pleroma.Filter.create(query)
|
|
||||||
|
|
||||||
new = %Pleroma.Filter{
|
|
||||||
phrase: "nii",
|
|
||||||
context: ["home"]
|
|
||||||
}
|
|
||||||
|
|
||||||
conn =
|
|
||||||
conn
|
|
||||||
|> assign(:user, user)
|
|
||||||
|> put("/api/v1/filters/#{query.filter_id}", %{
|
|
||||||
phrase: new.phrase,
|
|
||||||
context: new.context
|
|
||||||
})
|
|
||||||
|
|
||||||
assert response = json_response(conn, 200)
|
|
||||||
assert response["phrase"] == new.phrase
|
|
||||||
assert response["context"] == new.context
|
|
||||||
end
|
|
||||||
|
|
||||||
test "delete a filter", %{conn: conn} do
|
|
||||||
user = insert(:user)
|
|
||||||
|
|
||||||
query = %Pleroma.Filter{
|
|
||||||
user_id: user.id,
|
|
||||||
filter_id: 2,
|
|
||||||
phrase: "knight",
|
|
||||||
context: ["home"]
|
|
||||||
}
|
|
||||||
|
|
||||||
{:ok, filter} = Pleroma.Filter.create(query)
|
|
||||||
|
|
||||||
conn =
|
|
||||||
conn
|
|
||||||
|> assign(:user, user)
|
|
||||||
|> delete("/api/v1/filters/#{filter.filter_id}")
|
|
||||||
|
|
||||||
assert response = json_response(conn, 200)
|
|
||||||
assert response == %{}
|
|
||||||
end
|
|
||||||
end
|
|
||||||
|
|
||||||
describe "user timelines" do
|
describe "user timelines" do
|
||||||
test "gets a users statuses", %{conn: conn} do
|
test "gets a users statuses", %{conn: conn} do
|
||||||
user_one = insert(:user)
|
user_one = insert(:user)
|
||||||
|
|
Loading…
Reference in a new issue