fix compile warnings
[akkoma] / lib / pleroma / list.ex
1 defmodule Pleroma.List do
2 use Ecto.Schema
3 import Ecto.{Changeset, Query}
4 alias Pleroma.{User, Repo, Activity}
5
6 schema "lists" do
7 belongs_to(:user, Pleroma.User)
8 field(:title, :string)
9 field(:following, {:array, :string}, default: [])
10
11 timestamps()
12 end
13
14 def title_changeset(list, attrs \\ %{}) do
15 list
16 |> cast(attrs, [:title])
17 |> validate_required([:title])
18 end
19
20 def follow_changeset(list, attrs \\ %{}) do
21 list
22 |> cast(attrs, [:following])
23 |> validate_required([:following])
24 end
25
26 def for_user(user, _opts) do
27 query =
28 from(
29 l in Pleroma.List,
30 where: l.user_id == ^user.id,
31 order_by: [desc: l.id],
32 limit: 50
33 )
34
35 Repo.all(query)
36 end
37
38 def get(id, %{id: user_id} = _user) do
39 query =
40 from(
41 l in Pleroma.List,
42 where: l.id == ^id,
43 where: l.user_id == ^user_id
44 )
45
46 Repo.one(query)
47 end
48
49 def get_following(%Pleroma.List{following: following} = _list) do
50 q =
51 from(
52 u in User,
53 where: u.follower_address in ^following
54 )
55
56 {:ok, Repo.all(q)}
57 end
58
59 # Get lists the activity should be streamed to.
60 def get_lists_from_activity(%Activity{actor: ap_id}) do
61 actor = User.get_cached_by_ap_id(ap_id)
62
63 query =
64 from(
65 l in Pleroma.List,
66 where: fragment("? && ?", l.following, ^[actor.follower_address])
67 )
68
69 Repo.all(query)
70 end
71
72 # Get lists to which the account belongs.
73 def get_lists_account_belongs(%User{} = owner, account_id) do
74 user = Repo.get(User, account_id)
75
76 query =
77 from(
78 l in Pleroma.List,
79 where:
80 l.user_id == ^owner.id and
81 fragment(
82 "? = ANY(?)",
83 ^user.follower_address,
84 l.following
85 )
86 )
87
88 Repo.all(query)
89 end
90
91 def rename(%Pleroma.List{} = list, title) do
92 list
93 |> title_changeset(%{title: title})
94 |> Repo.update()
95 end
96
97 def create(title, %User{} = creator) do
98 list = %Pleroma.List{user_id: creator.id, title: title}
99 Repo.insert(list)
100 end
101
102 def follow(%Pleroma.List{following: following} = list, %User{} = followed) do
103 update_follows(list, %{following: Enum.uniq([followed.follower_address | following])})
104 end
105
106 def unfollow(%Pleroma.List{following: following} = list, %User{} = unfollowed) do
107 update_follows(list, %{following: List.delete(following, unfollowed.follower_address)})
108 end
109
110 def delete(%Pleroma.List{} = list) do
111 Repo.delete(list)
112 end
113
114 def update_follows(%Pleroma.List{} = list, attrs) do
115 list
116 |> follow_changeset(attrs)
117 |> Repo.update()
118 end
119 end