Use pseudo ap id of a list in BCC
This commit is contained in:
parent
a3dc02d282
commit
23276e8d68
5 changed files with 92 additions and 24 deletions
|
|
@ -12,6 +12,8 @@ defmodule Pleroma.List do
|
|||
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)
|
||||
|
|
@ -32,6 +34,12 @@ defmodule Pleroma.List do
|
|||
|> 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(
|
||||
|
|
@ -55,6 +63,19 @@ defmodule Pleroma.List do
|
|||
Repo.one(query)
|
||||
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
|
||||
end
|
||||
|
||||
def get_following(%Pleroma.List{following: following} = _list) do
|
||||
q =
|
||||
from(
|
||||
|
|
@ -125,4 +146,15 @@ defmodule Pleroma.List do
|
|||
|> follow_changeset(attrs)
|
||||
|> Repo.update()
|
||||
end
|
||||
|
||||
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})
|
||||
|> Repo.all()
|
||||
|> Enum.map(&ap_id/1)
|
||||
end
|
||||
|
||||
def memberships(_), do: []
|
||||
end
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue