logo

pleroma

My custom branche(s) on git.pleroma.social/pleroma/pleroma git clone https://hacktivis.me/git/pleroma.git

notification_settings.ex (2153B)


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