logo

pleroma

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

notification_backfill.ex (2228B)


  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 Pleroma.MigrationHelper.NotificationBackfill do
  5. alias Pleroma.Object
  6. alias Pleroma.Repo
  7. alias Pleroma.User
  8. import Ecto.Query
  9. def fill_in_notification_types do
  10. query =
  11. from(n in Pleroma.Notification,
  12. where: is_nil(n.type),
  13. preload: :activity
  14. )
  15. query
  16. |> Repo.chunk_stream(100)
  17. |> Enum.each(fn notification ->
  18. if notification.activity do
  19. type = type_from_activity(notification.activity)
  20. notification
  21. |> Ecto.Changeset.change(%{type: type})
  22. |> Repo.update()
  23. end
  24. end)
  25. end
  26. defp get_by_ap_id(ap_id) do
  27. q =
  28. from(u in User,
  29. select: u.id
  30. )
  31. Repo.get_by(q, ap_id: ap_id)
  32. end
  33. # This is copied over from Notifications to keep this stable.
  34. defp type_from_activity(%{data: %{"type" => type}} = activity) do
  35. case type do
  36. "Follow" ->
  37. accepted_function = fn activity ->
  38. with %User{} = follower <- get_by_ap_id(activity.data["actor"]),
  39. %User{} = followed <- get_by_ap_id(activity.data["object"]) do
  40. Pleroma.FollowingRelationship.following?(follower, followed)
  41. end
  42. end
  43. if accepted_function.(activity) do
  44. "follow"
  45. else
  46. "follow_request"
  47. end
  48. "Announce" ->
  49. "reblog"
  50. "Like" ->
  51. "favourite"
  52. "Move" ->
  53. "move"
  54. "EmojiReact" ->
  55. "pleroma:emoji_reaction"
  56. # Compatibility with old reactions
  57. "EmojiReaction" ->
  58. "pleroma:emoji_reaction"
  59. "Create" ->
  60. type_from_activity_object(activity)
  61. t ->
  62. raise "No notification type for activity type #{t}"
  63. end
  64. end
  65. defp type_from_activity_object(%{data: %{"type" => "Create", "object" => %{}}}), do: "mention"
  66. defp type_from_activity_object(%{data: %{"type" => "Create"}} = activity) do
  67. object = Object.get_by_ap_id(activity.data["object"])
  68. case object && object.data["type"] do
  69. "ChatMessage" -> "pleroma:chat_mention"
  70. _ -> "mention"
  71. end
  72. end
  73. end