Lint and fix test to match new log message
[akkoma] / lib / mix / tasks / pleroma / notification_settings.ex
1 defmodule Mix.Tasks.Pleroma.NotificationSettings do
2 @shortdoc "Enable&Disable privacy option for push notifications"
3 @moduledoc """
4 Example:
5
6 > mix pleroma.notification_settings --privacy-option=false --nickname-users="parallel588" # set false only for parallel588 user
7 > mix pleroma.notification_settings --privacy-option=true # set true for all users
8
9 """
10
11 use Mix.Task
12 import Mix.Pleroma
13 import Ecto.Query
14
15 def run(args) do
16 start_pleroma()
17
18 {options, _, _} =
19 OptionParser.parse(
20 args,
21 strict: [
22 privacy_option: :boolean,
23 email_users: :string,
24 nickname_users: :string
25 ]
26 )
27
28 privacy_option = Keyword.get(options, :privacy_option)
29
30 if not is_nil(privacy_option) do
31 privacy_option
32 |> build_query(options)
33 |> Pleroma.Repo.update_all([])
34 end
35
36 shell_info("Done")
37 end
38
39 defp build_query(privacy_option, options) do
40 query =
41 from(u in Pleroma.User,
42 update: [
43 set: [
44 notification_settings:
45 fragment(
46 "jsonb_set(notification_settings, '{privacy_option}', ?)",
47 ^privacy_option
48 )
49 ]
50 ]
51 )
52
53 user_emails =
54 options
55 |> Keyword.get(:email_users, "")
56 |> String.split(",")
57 |> Enum.map(&String.trim(&1))
58 |> Enum.reject(&(&1 == ""))
59
60 query =
61 if length(user_emails) > 0 do
62 where(query, [u], u.email in ^user_emails)
63 else
64 query
65 end
66
67 user_nicknames =
68 options
69 |> Keyword.get(:nickname_users, "")
70 |> String.split(",")
71 |> Enum.map(&String.trim(&1))
72 |> Enum.reject(&(&1 == ""))
73
74 query =
75 if length(user_nicknames) > 0 do
76 where(query, [u], u.nickname in ^user_nicknames)
77 else
78 query
79 end
80
81 query
82 end
83 end