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