diff options
Diffstat (limited to 'lib')
| -rw-r--r-- | lib/pleroma/migration_helper.ex | 85 | ||||
| -rw-r--r-- | lib/pleroma/notification.ex | 37 | 
2 files changed, 87 insertions, 35 deletions
diff --git a/lib/pleroma/migration_helper.ex b/lib/pleroma/migration_helper.ex new file mode 100644 index 000000000..e6346aff1 --- /dev/null +++ b/lib/pleroma/migration_helper.ex @@ -0,0 +1,85 @@ +# Pleroma: A lightweight social networking server +# Copyright © 2017-2020 Pleroma Authors <https://pleroma.social/> +# SPDX-License-Identifier: AGPL-3.0-only + +defmodule Pleroma.MigrationHelper do +  alias Pleroma.User +  alias Pleroma.Object +  alias Pleroma.Notification +  alias Pleroma.Repo + +  import Ecto.Query + +  def fill_in_notification_types do +    query = +      from(n in Pleroma.Notification, +        where: is_nil(n.type), +        preload: :activity +      ) + +    query +    |> Repo.all() +    |> Enum.each(fn notification -> +      type = +        notification.activity +        |> type_from_activity() + +      notification +      |> Notification.changeset(%{type: type}) +      |> Repo.update() +    end) +  end + +  # This is copied over from Notifications to keep this stable. +  defp type_from_activity(%{data: %{"type" => type}} = activity) do +    case type do +      "Follow" -> +        accepted_function = fn activity -> +          with %User{} = follower <- User.get_by_ap_id(activity.data["actor"]), +               %User{} = followed <- User.get_by_ap_id(activity.data["object"]) do +            Pleroma.FollowingRelationship.following?(follower, followed) +          end +        end + +        if accepted_function.(activity) do +          "follow" +        else +          "follow_request" +        end + +      "Announce" -> +        "reblog" + +      "Like" -> +        "favourite" + +      "Move" -> +        "move" + +      "EmojiReact" -> +        "pleroma:emoji_reaction" + +      # Compatibility with old reactions +      "EmojiReaction" -> +        "pleroma:emoji_reaction" + +      "Create" -> +        activity +        |> type_from_activity_object() + +      t -> +        raise "No notification type for activity type #{t}" +    end +  end + +  defp type_from_activity_object(%{data: %{"type" => "Create", "object" => %{}}}), do: "mention" + +  defp type_from_activity_object(%{data: %{"type" => "Create"}} = activity) do +    object = Object.get_by_ap_id(activity.data["object"]) + +    case object && object.data["type"] do +      "ChatMessage" -> "pleroma:chat_mention" +      _ -> "mention" +    end +  end +end diff --git a/lib/pleroma/notification.ex b/lib/pleroma/notification.ex index 5c8994e35..682a26912 100644 --- a/lib/pleroma/notification.ex +++ b/lib/pleroma/notification.ex @@ -40,26 +40,6 @@ defmodule Pleroma.Notification do      timestamps()    end -  def fill_in_notification_types do -    query = -      from(n in __MODULE__, -        where: is_nil(n.type), -        preload: :activity -      ) - -    query -    |> Repo.all() -    |> Enum.each(fn notification -> -      type = -        notification.activity -        |> type_from_activity(no_cachex: true) - -      notification -      |> changeset(%{type: type}) -      |> Repo.update() -    end) -  end -    def update_notification_type(user, activity) do      with %__MODULE__{} = notification <-             Repo.get_by(__MODULE__, user_id: user.id, activity_id: activity.id) do @@ -371,23 +351,10 @@ defmodule Pleroma.Notification do      {:ok, notifications}    end -  defp type_from_activity(%{data: %{"type" => type}} = activity, opts \\ []) do +  defp type_from_activity(%{data: %{"type" => type}} = activity) do      case type do        "Follow" -> -        accepted_function = -          if Keyword.get(opts, :no_cachex, false) do -            # A special function to make this usable in a migration. -            fn activity -> -              with %User{} = follower <- User.get_by_ap_id(activity.data["actor"]), -                   %User{} = followed <- User.get_by_ap_id(activity.data["object"]) do -                Pleroma.FollowingRelationship.following?(follower, followed) -              end -            end -          else -            &Activity.follow_accepted?/1 -          end - -        if accepted_function.(activity) do +        if Activity.follow_accepted?(activity) do            "follow"          else            "follow_request"  | 
