renaming back and reject nil on create
This commit is contained in:
parent
52ff75413a
commit
20c27bef40
|
@ -110,7 +110,7 @@ def compose_regex(%User{} = user, format) do
|
||||||
__MODULE__
|
__MODULE__
|
||||||
|> get_active()
|
|> get_active()
|
||||||
|> get_irreversible()
|
|> get_irreversible()
|
||||||
|> get_by_user(user)
|
|> get_filters(user)
|
||||||
|> compose_regex(format)
|
|> compose_regex(format)
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
|
@ -352,6 +352,7 @@ def dismiss(%{id: user_id} = _user, id) do
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
@spec create_notifications(Activity.t(), keyword()) :: {:ok, [Notification.t()] | []}
|
||||||
def create_notifications(activity, options \\ [])
|
def create_notifications(activity, options \\ [])
|
||||||
|
|
||||||
def create_notifications(%Activity{data: %{"to" => _, "type" => "Create"}} = activity, options) do
|
def create_notifications(%Activity{data: %{"to" => _, "type" => "Create"}} = activity, options) do
|
||||||
|
|
|
@ -22,7 +22,7 @@ defmodule Pleroma.Web.MastodonAPI.FilterController do
|
||||||
|
|
||||||
@doc "GET /api/v1/filters"
|
@doc "GET /api/v1/filters"
|
||||||
def index(%{assigns: %{user: user}} = conn, _) do
|
def index(%{assigns: %{user: user}} = conn, _) do
|
||||||
filters = Filter.get_by_user(Filter, user)
|
filters = Filter.get_filters(user)
|
||||||
|
|
||||||
render(conn, "index.json", filters: filters)
|
render(conn, "index.json", filters: filters)
|
||||||
end
|
end
|
||||||
|
|
|
@ -3,37 +3,39 @@
|
||||||
# SPDX-License-Identifier: AGPL-3.0-only
|
# SPDX-License-Identifier: AGPL-3.0-only
|
||||||
|
|
||||||
defmodule Pleroma.FilterTest do
|
defmodule Pleroma.FilterTest do
|
||||||
alias Pleroma.Repo
|
|
||||||
use Pleroma.DataCase
|
use Pleroma.DataCase
|
||||||
|
|
||||||
import Pleroma.Factory
|
import Pleroma.Factory
|
||||||
|
|
||||||
|
alias Pleroma.Filter
|
||||||
|
alias Pleroma.Repo
|
||||||
|
|
||||||
describe "creating filters" do
|
describe "creating filters" do
|
||||||
test "creating one filter" do
|
test "creating one filter" do
|
||||||
user = insert(:user)
|
user = insert(:user)
|
||||||
|
|
||||||
query = %Pleroma.Filter{
|
query = %Filter{
|
||||||
user_id: user.id,
|
user_id: user.id,
|
||||||
filter_id: 42,
|
filter_id: 42,
|
||||||
phrase: "knights",
|
phrase: "knights",
|
||||||
context: ["home"]
|
context: ["home"]
|
||||||
}
|
}
|
||||||
|
|
||||||
{:ok, %Pleroma.Filter{} = filter} = Pleroma.Filter.create(query)
|
{:ok, %Filter{} = filter} = Filter.create(query)
|
||||||
result = Pleroma.Filter.get(filter.filter_id, user)
|
result = Filter.get(filter.filter_id, user)
|
||||||
assert query.phrase == result.phrase
|
assert query.phrase == result.phrase
|
||||||
end
|
end
|
||||||
|
|
||||||
test "creating one filter without a pre-defined filter_id" do
|
test "creating one filter without a pre-defined filter_id" do
|
||||||
user = insert(:user)
|
user = insert(:user)
|
||||||
|
|
||||||
query = %Pleroma.Filter{
|
query = %Filter{
|
||||||
user_id: user.id,
|
user_id: user.id,
|
||||||
phrase: "knights",
|
phrase: "knights",
|
||||||
context: ["home"]
|
context: ["home"]
|
||||||
}
|
}
|
||||||
|
|
||||||
{:ok, %Pleroma.Filter{} = filter} = Pleroma.Filter.create(query)
|
{:ok, %Filter{} = filter} = Filter.create(query)
|
||||||
# Should start at 1
|
# Should start at 1
|
||||||
assert filter.filter_id == 1
|
assert filter.filter_id == 1
|
||||||
end
|
end
|
||||||
|
@ -41,23 +43,23 @@ test "creating one filter without a pre-defined filter_id" do
|
||||||
test "creating additional filters uses previous highest filter_id + 1" do
|
test "creating additional filters uses previous highest filter_id + 1" do
|
||||||
user = insert(:user)
|
user = insert(:user)
|
||||||
|
|
||||||
query_one = %Pleroma.Filter{
|
query_one = %Filter{
|
||||||
user_id: user.id,
|
user_id: user.id,
|
||||||
filter_id: 42,
|
filter_id: 42,
|
||||||
phrase: "knights",
|
phrase: "knights",
|
||||||
context: ["home"]
|
context: ["home"]
|
||||||
}
|
}
|
||||||
|
|
||||||
{:ok, %Pleroma.Filter{} = filter_one} = Pleroma.Filter.create(query_one)
|
{:ok, %Filter{} = filter_one} = Filter.create(query_one)
|
||||||
|
|
||||||
query_two = %Pleroma.Filter{
|
query_two = %Filter{
|
||||||
user_id: user.id,
|
user_id: user.id,
|
||||||
# No filter_id
|
# No filter_id
|
||||||
phrase: "who",
|
phrase: "who",
|
||||||
context: ["home"]
|
context: ["home"]
|
||||||
}
|
}
|
||||||
|
|
||||||
{:ok, %Pleroma.Filter{} = filter_two} = Pleroma.Filter.create(query_two)
|
{:ok, %Filter{} = filter_two} = Filter.create(query_two)
|
||||||
assert filter_two.filter_id == filter_one.filter_id + 1
|
assert filter_two.filter_id == filter_one.filter_id + 1
|
||||||
end
|
end
|
||||||
|
|
||||||
|
@ -65,29 +67,29 @@ test "filter_id is unique per user" do
|
||||||
user_one = insert(:user)
|
user_one = insert(:user)
|
||||||
user_two = insert(:user)
|
user_two = insert(:user)
|
||||||
|
|
||||||
query_one = %Pleroma.Filter{
|
query_one = %Filter{
|
||||||
user_id: user_one.id,
|
user_id: user_one.id,
|
||||||
phrase: "knights",
|
phrase: "knights",
|
||||||
context: ["home"]
|
context: ["home"]
|
||||||
}
|
}
|
||||||
|
|
||||||
{:ok, %Pleroma.Filter{} = filter_one} = Pleroma.Filter.create(query_one)
|
{:ok, %Filter{} = filter_one} = Filter.create(query_one)
|
||||||
|
|
||||||
query_two = %Pleroma.Filter{
|
query_two = %Filter{
|
||||||
user_id: user_two.id,
|
user_id: user_two.id,
|
||||||
phrase: "who",
|
phrase: "who",
|
||||||
context: ["home"]
|
context: ["home"]
|
||||||
}
|
}
|
||||||
|
|
||||||
{:ok, %Pleroma.Filter{} = filter_two} = Pleroma.Filter.create(query_two)
|
{:ok, %Filter{} = filter_two} = Filter.create(query_two)
|
||||||
|
|
||||||
assert filter_one.filter_id == 1
|
assert filter_one.filter_id == 1
|
||||||
assert filter_two.filter_id == 1
|
assert filter_two.filter_id == 1
|
||||||
|
|
||||||
result_one = Pleroma.Filter.get(filter_one.filter_id, user_one)
|
result_one = Filter.get(filter_one.filter_id, user_one)
|
||||||
assert result_one.phrase == filter_one.phrase
|
assert result_one.phrase == filter_one.phrase
|
||||||
|
|
||||||
result_two = Pleroma.Filter.get(filter_two.filter_id, user_two)
|
result_two = Filter.get(filter_two.filter_id, user_two)
|
||||||
assert result_two.phrase == filter_two.phrase
|
assert result_two.phrase == filter_two.phrase
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
@ -95,38 +97,38 @@ test "filter_id is unique per user" do
|
||||||
test "deleting a filter" do
|
test "deleting a filter" do
|
||||||
user = insert(:user)
|
user = insert(:user)
|
||||||
|
|
||||||
query = %Pleroma.Filter{
|
query = %Filter{
|
||||||
user_id: user.id,
|
user_id: user.id,
|
||||||
filter_id: 0,
|
filter_id: 0,
|
||||||
phrase: "knights",
|
phrase: "knights",
|
||||||
context: ["home"]
|
context: ["home"]
|
||||||
}
|
}
|
||||||
|
|
||||||
{:ok, _filter} = Pleroma.Filter.create(query)
|
{:ok, _filter} = Filter.create(query)
|
||||||
{:ok, filter} = Pleroma.Filter.delete(query)
|
{:ok, filter} = Filter.delete(query)
|
||||||
assert is_nil(Repo.get(Pleroma.Filter, filter.filter_id))
|
assert is_nil(Repo.get(Filter, filter.filter_id))
|
||||||
end
|
end
|
||||||
|
|
||||||
test "getting all filters by an user" do
|
test "getting all filters by an user" do
|
||||||
user = insert(:user)
|
user = insert(:user)
|
||||||
|
|
||||||
query_one = %Pleroma.Filter{
|
query_one = %Filter{
|
||||||
user_id: user.id,
|
user_id: user.id,
|
||||||
filter_id: 1,
|
filter_id: 1,
|
||||||
phrase: "knights",
|
phrase: "knights",
|
||||||
context: ["home"]
|
context: ["home"]
|
||||||
}
|
}
|
||||||
|
|
||||||
query_two = %Pleroma.Filter{
|
query_two = %Filter{
|
||||||
user_id: user.id,
|
user_id: user.id,
|
||||||
filter_id: 2,
|
filter_id: 2,
|
||||||
phrase: "who",
|
phrase: "who",
|
||||||
context: ["home"]
|
context: ["home"]
|
||||||
}
|
}
|
||||||
|
|
||||||
{:ok, filter_one} = Pleroma.Filter.create(query_one)
|
{:ok, filter_one} = Filter.create(query_one)
|
||||||
{:ok, filter_two} = Pleroma.Filter.create(query_two)
|
{:ok, filter_two} = Filter.create(query_two)
|
||||||
filters = Pleroma.Filter.get_by_user(Pleroma.Filter, user)
|
filters = Filter.get_filters(user)
|
||||||
assert filter_one in filters
|
assert filter_one in filters
|
||||||
assert filter_two in filters
|
assert filter_two in filters
|
||||||
end
|
end
|
||||||
|
@ -134,7 +136,7 @@ test "getting all filters by an user" do
|
||||||
test "updating a filter" do
|
test "updating a filter" do
|
||||||
user = insert(:user)
|
user = insert(:user)
|
||||||
|
|
||||||
query_one = %Pleroma.Filter{
|
query_one = %Filter{
|
||||||
user_id: user.id,
|
user_id: user.id,
|
||||||
filter_id: 1,
|
filter_id: 1,
|
||||||
phrase: "knights",
|
phrase: "knights",
|
||||||
|
@ -146,8 +148,9 @@ test "updating a filter" do
|
||||||
context: ["home", "timeline"]
|
context: ["home", "timeline"]
|
||||||
}
|
}
|
||||||
|
|
||||||
{:ok, filter_one} = Pleroma.Filter.create(query_one)
|
{:ok, filter_one} = Filter.create(query_one)
|
||||||
{:ok, filter_two} = Pleroma.Filter.update(filter_one, changes)
|
{:ok, filter_two} = Filter.update(filter_one, changes)
|
||||||
|
|
||||||
assert filter_one != filter_two
|
assert filter_one != filter_two
|
||||||
assert filter_two.phrase == changes.phrase
|
assert filter_two.phrase == changes.phrase
|
||||||
assert filter_two.context == changes.context
|
assert filter_two.context == changes.context
|
||||||
|
|
|
@ -334,7 +334,7 @@ test "it doesn't create notifications if content matches with an irreversible fi
|
||||||
|
|
||||||
{:ok, status} = CommonAPI.post(user, %{"status" => "got cofe?"})
|
{:ok, status} = CommonAPI.post(user, %{"status" => "got cofe?"})
|
||||||
|
|
||||||
assert {:ok, [nil]} == Notification.create_notifications(status)
|
assert {:ok, []} == Notification.create_notifications(status)
|
||||||
end
|
end
|
||||||
|
|
||||||
test "it creates notifications if content matches with a not irreversible filter" do
|
test "it creates notifications if content matches with a not irreversible filter" do
|
||||||
|
|
Loading…
Reference in a new issue