b446b91a044735fb8852765e96e347f3486a1f86
[anni] / lib / pleroma / list.ex
1 # Pleroma: A lightweight social networking server
2 # Copyright © 2017-2022 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   schema "lists" do
16     belongs_to(:user, User, type: FlakeId.Ecto.CompatType)
17     field(:title, :string)
18     field(:following, {:array, :string}, default: [])
19     field(:ap_id, :string)
20
21     timestamps()
22   end
23
24   def title_changeset(list, attrs \\ %{}) do
25     list
26     |> cast(attrs, [:title])
27     |> validate_required([:title])
28   end
29
30   def follow_changeset(list, attrs \\ %{}) do
31     list
32     |> cast(attrs, [:following])
33     |> validate_required([:following])
34   end
35
36   def for_user(user, _opts) do
37     query =
38       from(
39         l in Pleroma.List,
40         where: l.user_id == ^user.id,
41         order_by: [desc: l.id],
42         limit: 50
43       )
44
45     Repo.all(query)
46   end
47
48   def get(id, %{id: user_id} = _user) do
49     query =
50       from(
51         l in Pleroma.List,
52         where: l.id == ^id,
53         where: l.user_id == ^user_id
54       )
55
56     Repo.one(query)
57   end
58
59   def get_by_ap_id(ap_id) do
60     Repo.get_by(__MODULE__, ap_id: ap_id)
61   end
62
63   def get_following(%Pleroma.List{following: following} = _list) do
64     q =
65       from(
66         u in User,
67         where: u.follower_address in ^following
68       )
69
70     {:ok, Repo.all(q)}
71   end
72
73   # Get lists the activity should be streamed to.
74   def get_lists_from_activity(%Activity{actor: ap_id}) do
75     actor = User.get_cached_by_ap_id(ap_id)
76
77     query =
78       from(
79         l in Pleroma.List,
80         where: fragment("? && ?", l.following, ^[actor.follower_address])
81       )
82
83     Repo.all(query)
84   end
85
86   # Get lists to which the account belongs.
87   def get_lists_account_belongs(%User{} = owner, user) do
88     Pleroma.List
89     |> where([l], l.user_id == ^owner.id)
90     |> where([l], fragment("? = ANY(?)", ^user.follower_address, l.following))
91     |> Repo.all()
92   end
93
94   def rename(%Pleroma.List{} = list, title) do
95     list
96     |> title_changeset(%{title: title})
97     |> Repo.update()
98   end
99
100   def create(title, %User{} = creator) do
101     changeset = title_changeset(%Pleroma.List{user_id: creator.id}, %{title: title})
102
103     if changeset.valid? do
104       Repo.transaction(fn ->
105         list = Repo.insert!(changeset)
106
107         list
108         |> change(ap_id: "#{creator.ap_id}/lists/#{list.id}")
109         |> Repo.update!()
110       end)
111     else
112       {:error, changeset}
113     end
114   end
115
116   def follow(%Pleroma.List{id: id}, %User{} = followed) do
117     list = Repo.get(Pleroma.List, id)
118     %{following: following} = list
119     update_follows(list, %{following: Enum.uniq([followed.follower_address | following])})
120   end
121
122   def unfollow(%Pleroma.List{id: id}, %User{} = unfollowed) do
123     list = Repo.get(Pleroma.List, id)
124     %{following: following} = list
125     update_follows(list, %{following: List.delete(following, unfollowed.follower_address)})
126   end
127
128   def delete(%Pleroma.List{} = list) do
129     Repo.delete(list)
130   end
131
132   def update_follows(%Pleroma.List{} = list, attrs) do
133     list
134     |> follow_changeset(attrs)
135     |> Repo.update()
136   end
137
138   def memberships(%User{follower_address: follower_address}) do
139     Pleroma.List
140     |> where([l], ^follower_address in l.following)
141     |> select([l], l.ap_id)
142     |> Repo.all()
143   end
144
145   def memberships(_), do: []
146
147   def member?(%Pleroma.List{following: following}, %User{follower_address: follower_address}) do
148     Enum.member?(following, follower_address)
149   end
150
151   def member?(_, _), do: false
152 end