81b842e9cd909048c77b7dbaf37e880e9404b1f7
[akkoma] / lib / pleroma / list.ex
1 # Pleroma: A lightweight social networking server
2 # Copyright © 2017-2019 Pleroma Authors <https://pleroma.social/>
3 # SPDX-License-Identifier: AGPL-3.0-only
4
5 defmodule Pleroma.List do
6 use Ecto.Schema
7
8 import Ecto.Query
9 import Ecto.Changeset
10
11 alias Pleroma.Activity
12 alias Pleroma.Repo
13 alias Pleroma.User
14
15 @ap_id_regex ~r/^\/users\/(?<nickname>\w+)\/lists\/(?<list_id>\d+)/
16
17 schema "lists" do
18 belongs_to(:user, User, type: Pleroma.FlakeId)
19 field(:title, :string)
20 field(:following, {:array, :string}, default: [])
21
22 timestamps()
23 end
24
25 def title_changeset(list, attrs \\ %{}) do
26 list
27 |> cast(attrs, [:title])
28 |> validate_required([:title])
29 end
30
31 def follow_changeset(list, attrs \\ %{}) do
32 list
33 |> cast(attrs, [:following])
34 |> validate_required([:following])
35 end
36
37 def ap_id(%User{nickname: nickname}, list_id) do
38 Pleroma.Web.Endpoint.url() <> "/users/#{nickname}/lists/#{list_id}"
39 end
40
41 def ap_id({nickname, list_id}), do: ap_id(%User{nickname: nickname}, list_id)
42
43 def for_user(user, _opts) do
44 query =
45 from(
46 l in Pleroma.List,
47 where: l.user_id == ^user.id,
48 order_by: [desc: l.id],
49 limit: 50
50 )
51
52 Repo.all(query)
53 end
54
55 def get(id, %{id: user_id} = _user) do
56 query =
57 from(
58 l in Pleroma.List,
59 where: l.id == ^id,
60 where: l.user_id == ^user_id
61 )
62
63 Repo.one(query)
64 end
65
66 def get_by_ap_id(ap_id) do
67 host = Pleroma.Web.Endpoint.host()
68
69 with %{host: ^host, path: path} <- URI.parse(ap_id),
70 %{"list_id" => list_id, "nickname" => nickname} <-
71 Regex.named_captures(@ap_id_regex, path),
72 %User{} = user <- User.get_cached_by_nickname(nickname) do
73 get(list_id, user)
74 else
75 _ -> nil
76 end
77 end
78
79 def get_following(%Pleroma.List{following: following} = _list) do
80 q =
81 from(
82 u in User,
83 where: u.follower_address in ^following
84 )
85
86 {:ok, Repo.all(q)}
87 end
88
89 # Get lists the activity should be streamed to.
90 def get_lists_from_activity(%Activity{actor: ap_id}) do
91 actor = User.get_cached_by_ap_id(ap_id)
92
93 query =
94 from(
95 l in Pleroma.List,
96 where: fragment("? && ?", l.following, ^[actor.follower_address])
97 )
98
99 Repo.all(query)
100 end
101
102 # Get lists to which the account belongs.
103 def get_lists_account_belongs(%User{} = owner, account_id) do
104 user = User.get_cached_by_id(account_id)
105
106 query =
107 from(
108 l in Pleroma.List,
109 where:
110 l.user_id == ^owner.id and
111 fragment(
112 "? = ANY(?)",
113 ^user.follower_address,
114 l.following
115 )
116 )
117
118 Repo.all(query)
119 end
120
121 def rename(%Pleroma.List{} = list, title) do
122 list
123 |> title_changeset(%{title: title})
124 |> Repo.update()
125 end
126
127 def create(title, %User{} = creator) do
128 list = %Pleroma.List{user_id: creator.id, title: title}
129 Repo.insert(list)
130 end
131
132 def follow(%Pleroma.List{following: following} = list, %User{} = followed) do
133 update_follows(list, %{following: Enum.uniq([followed.follower_address | following])})
134 end
135
136 def unfollow(%Pleroma.List{following: following} = list, %User{} = unfollowed) do
137 update_follows(list, %{following: List.delete(following, unfollowed.follower_address)})
138 end
139
140 def delete(%Pleroma.List{} = list) do
141 Repo.delete(list)
142 end
143
144 def update_follows(%Pleroma.List{} = list, attrs) do
145 list
146 |> follow_changeset(attrs)
147 |> Repo.update()
148 end
149
150 def memberships(%User{follower_address: follower_address}) do
151 Pleroma.List
152 |> where([l], ^follower_address in l.following)
153 |> join(:inner, [l], u in User, on: l.user_id == u.id)
154 |> select([l, u], {u.nickname, l.id})
155 |> Repo.all()
156 |> Enum.map(&ap_id/1)
157 end
158
159 def memberships(_), do: []
160 end