Add `ap_id` to List
This commit is contained in:
parent
fc7246d715
commit
f2936e0a07
|
@ -12,12 +12,11 @@ defmodule Pleroma.List do
|
||||||
alias Pleroma.Repo
|
alias Pleroma.Repo
|
||||||
alias Pleroma.User
|
alias Pleroma.User
|
||||||
|
|
||||||
@ap_id_regex ~r/^\/users\/(?<nickname>\w+)\/lists\/(?<list_id>\d+)/
|
|
||||||
|
|
||||||
schema "lists" do
|
schema "lists" do
|
||||||
belongs_to(:user, User, type: Pleroma.FlakeId)
|
belongs_to(:user, User, type: Pleroma.FlakeId)
|
||||||
field(:title, :string)
|
field(:title, :string)
|
||||||
field(:following, {:array, :string}, default: [])
|
field(:following, {:array, :string}, default: [])
|
||||||
|
field(:ap_id, :string)
|
||||||
|
|
||||||
timestamps()
|
timestamps()
|
||||||
end
|
end
|
||||||
|
@ -34,12 +33,6 @@ def follow_changeset(list, attrs \\ %{}) do
|
||||||
|> validate_required([:following])
|
|> validate_required([:following])
|
||||||
end
|
end
|
||||||
|
|
||||||
def ap_id(%User{nickname: nickname}, list_id) do
|
|
||||||
Pleroma.Web.Endpoint.url() <> "/users/#{nickname}/lists/#{list_id}"
|
|
||||||
end
|
|
||||||
|
|
||||||
def ap_id({nickname, list_id}), do: ap_id(%User{nickname: nickname}, list_id)
|
|
||||||
|
|
||||||
def for_user(user, _opts) do
|
def for_user(user, _opts) do
|
||||||
query =
|
query =
|
||||||
from(
|
from(
|
||||||
|
@ -64,16 +57,7 @@ def get(id, %{id: user_id} = _user) do
|
||||||
end
|
end
|
||||||
|
|
||||||
def get_by_ap_id(ap_id) do
|
def get_by_ap_id(ap_id) do
|
||||||
host = Pleroma.Web.Endpoint.host()
|
Repo.get_by(__MODULE__, ap_id: ap_id)
|
||||||
|
|
||||||
with %{host: ^host, path: path} <- URI.parse(ap_id),
|
|
||||||
%{"list_id" => list_id, "nickname" => nickname} <-
|
|
||||||
Regex.named_captures(@ap_id_regex, path),
|
|
||||||
%User{} = user <- User.get_cached_by_nickname(nickname) do
|
|
||||||
get(list_id, user)
|
|
||||||
else
|
|
||||||
_ -> nil
|
|
||||||
end
|
|
||||||
end
|
end
|
||||||
|
|
||||||
def get_following(%Pleroma.List{following: following} = _list) do
|
def get_following(%Pleroma.List{following: following} = _list) do
|
||||||
|
@ -126,7 +110,14 @@ def rename(%Pleroma.List{} = list, title) do
|
||||||
|
|
||||||
def create(title, %User{} = creator) do
|
def create(title, %User{} = creator) do
|
||||||
list = %Pleroma.List{user_id: creator.id, title: title}
|
list = %Pleroma.List{user_id: creator.id, title: title}
|
||||||
Repo.insert(list)
|
|
||||||
|
Repo.transaction(fn ->
|
||||||
|
list = Repo.insert!(list)
|
||||||
|
|
||||||
|
list
|
||||||
|
|> change(ap_id: "#{creator.ap_id}/lists/#{list.id}")
|
||||||
|
|> Repo.update!()
|
||||||
|
end)
|
||||||
end
|
end
|
||||||
|
|
||||||
def follow(%Pleroma.List{following: following} = list, %User{} = followed) do
|
def follow(%Pleroma.List{following: following} = list, %User{} = followed) do
|
||||||
|
@ -150,10 +141,8 @@ def update_follows(%Pleroma.List{} = list, attrs) do
|
||||||
def memberships(%User{follower_address: follower_address}) do
|
def memberships(%User{follower_address: follower_address}) do
|
||||||
Pleroma.List
|
Pleroma.List
|
||||||
|> where([l], ^follower_address in l.following)
|
|> where([l], ^follower_address in l.following)
|
||||||
|> join(:inner, [l], u in User, on: l.user_id == u.id)
|
|> select([l], l.ap_id)
|
||||||
|> select([l, u], {u.nickname, l.id})
|
|
||||||
|> Repo.all()
|
|> Repo.all()
|
||||||
|> Enum.map(&ap_id/1)
|
|
||||||
end
|
end
|
||||||
|
|
||||||
def memberships(_), do: []
|
def memberships(_), do: []
|
||||||
|
|
|
@ -105,7 +105,8 @@ def to_for_user_and_mentions(_user, mentions, inReplyTo, "direct") do
|
||||||
def to_for_user_and_mentions(_user, _mentions, _inReplyTo, _), do: {[], []}
|
def to_for_user_and_mentions(_user, _mentions, _inReplyTo, _), do: {[], []}
|
||||||
|
|
||||||
def bcc_for_list(user, {:list, list_id}) do
|
def bcc_for_list(user, {:list, list_id}) do
|
||||||
[Pleroma.List.ap_id(user, list_id)]
|
list = Pleroma.List.get(list_id, user)
|
||||||
|
[list.ap_id]
|
||||||
end
|
end
|
||||||
|
|
||||||
def bcc_for_list(_, _), do: []
|
def bcc_for_list(_, _), do: []
|
||||||
|
|
|
@ -0,0 +1,26 @@
|
||||||
|
defmodule Pleroma.Repo.Migrations.AddApIdToLists do
|
||||||
|
use Ecto.Migration
|
||||||
|
|
||||||
|
def up do
|
||||||
|
alter table(:lists) do
|
||||||
|
add(:ap_id, :string)
|
||||||
|
end
|
||||||
|
|
||||||
|
execute("""
|
||||||
|
UPDATE lists
|
||||||
|
SET ap_id = u.ap_id || '/lists/' || lists.id
|
||||||
|
FROM users AS u
|
||||||
|
WHERE lists.user_id = u.id
|
||||||
|
""")
|
||||||
|
|
||||||
|
create(unique_index(:lists, :ap_id))
|
||||||
|
end
|
||||||
|
|
||||||
|
def down do
|
||||||
|
drop(index(:lists, [:ap_id]))
|
||||||
|
|
||||||
|
alter table(:lists) do
|
||||||
|
remove(:ap_id)
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
|
@ -114,22 +114,10 @@ test "getting own lists a given user belongs to" do
|
||||||
refute not_owned_list in lists_2
|
refute not_owned_list in lists_2
|
||||||
end
|
end
|
||||||
|
|
||||||
test "get ap_id by user nickname and list id" do
|
|
||||||
nickname = "foo"
|
|
||||||
list_id = 42
|
|
||||||
|
|
||||||
expected = Pleroma.Web.Endpoint.url() <> "/users/#{nickname}/lists/#{list_id}"
|
|
||||||
|
|
||||||
assert Pleroma.List.ap_id(%Pleroma.User{nickname: nickname}, list_id) == expected
|
|
||||||
assert Pleroma.List.ap_id({nickname, list_id}) == expected
|
|
||||||
end
|
|
||||||
|
|
||||||
test "get by ap_id" do
|
test "get by ap_id" do
|
||||||
user = insert(:user)
|
user = insert(:user)
|
||||||
{:ok, list} = Pleroma.List.create("foo", user)
|
{:ok, list} = Pleroma.List.create("foo", user)
|
||||||
ap_id = Pleroma.List.ap_id(user, list.id)
|
assert Pleroma.List.get_by_ap_id(list.ap_id) == list
|
||||||
|
|
||||||
assert Pleroma.List.get_by_ap_id(ap_id) == list
|
|
||||||
end
|
end
|
||||||
|
|
||||||
test "memberships" do
|
test "memberships" do
|
||||||
|
@ -138,8 +126,6 @@ test "memberships" do
|
||||||
{:ok, list} = Pleroma.List.create("foo", user)
|
{:ok, list} = Pleroma.List.create("foo", user)
|
||||||
{:ok, list} = Pleroma.List.follow(list, member)
|
{:ok, list} = Pleroma.List.follow(list, member)
|
||||||
|
|
||||||
list_ap_id = Pleroma.List.ap_id(user, list.id)
|
assert Pleroma.List.memberships(member) == [list.ap_id]
|
||||||
|
|
||||||
assert Pleroma.List.memberships(member) == [list_ap_id]
|
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
|
@ -114,13 +114,11 @@ test "it allows to address a list" do
|
||||||
user = insert(:user)
|
user = insert(:user)
|
||||||
{:ok, list} = Pleroma.List.create("foo", user)
|
{:ok, list} = Pleroma.List.create("foo", user)
|
||||||
|
|
||||||
list_ap_id = Pleroma.List.ap_id(user, list.id)
|
|
||||||
|
|
||||||
{:ok, activity} =
|
{:ok, activity} =
|
||||||
CommonAPI.post(user, %{"status" => "foobar", "visibility" => "list:#{list.id}"})
|
CommonAPI.post(user, %{"status" => "foobar", "visibility" => "list:#{list.id}"})
|
||||||
|
|
||||||
assert activity.data["bcc"] == [list_ap_id]
|
assert activity.data["bcc"] == [list.ap_id]
|
||||||
assert activity.recipients == [list_ap_id, user.ap_id]
|
assert activity.recipients == [list.ap_id, user.ap_id]
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue