alias Pleroma.Repo
alias Pleroma.User
- @ap_id_regex ~r/^\/users\/(?<nickname>\w+)\/lists\/(?<list_id>\d+)/
-
schema "lists" do
belongs_to(:user, User, type: Pleroma.FlakeId)
field(:title, :string)
field(:following, {:array, :string}, default: [])
+ field(:ap_id, :string)
timestamps()
end
|> validate_required([:following])
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
query =
from(
end
def get_by_ap_id(ap_id) do
- host = Pleroma.Web.Endpoint.host()
-
- 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
+ Repo.get_by(__MODULE__, ap_id: ap_id)
end
def get_following(%Pleroma.List{following: following} = _list) do
def create(title, %User{} = creator) do
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
def follow(%Pleroma.List{following: following} = list, %User{} = followed) do
def memberships(%User{follower_address: follower_address}) do
Pleroma.List
|> where([l], ^follower_address in l.following)
- |> join(:inner, [l], u in User, on: l.user_id == u.id)
- |> select([l, u], {u.nickname, l.id})
+ |> select([l], l.ap_id)
|> Repo.all()
- |> Enum.map(&ap_id/1)
end
def memberships(_), do: []
def to_for_user_and_mentions(_user, _mentions, _inReplyTo, _), 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
def bcc_for_list(_, _), do: []
--- /dev/null
+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
refute not_owned_list in lists_2
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
user = insert(:user)
{:ok, list} = Pleroma.List.create("foo", user)
- ap_id = Pleroma.List.ap_id(user, list.id)
-
- assert Pleroma.List.get_by_ap_id(ap_id) == list
+ assert Pleroma.List.get_by_ap_id(list.ap_id) == list
end
test "memberships" do
{:ok, list} = Pleroma.List.create("foo", user)
{: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
user = insert(:user)
{:ok, list} = Pleroma.List.create("foo", user)
- list_ap_id = Pleroma.List.ap_id(user, list.id)
-
{:ok, activity} =
CommonAPI.post(user, %{"status" => "foobar", "visibility" => "list:#{list.id}"})
- assert activity.data["bcc"] == [list_ap_id]
- assert activity.recipients == [list_ap_id, user.ap_id]
+ assert activity.data["bcc"] == [list.ap_id]
+ assert activity.recipients == [list.ap_id, user.ap_id]
end
end