2020-06-06 07:33:02 -06:00
|
|
|
# Pleroma: A lightweight social networking server
|
2021-01-12 23:49:20 -07:00
|
|
|
# Copyright © 2017-2021 Pleroma Authors <https://pleroma.social/>
|
2020-06-06 07:33:02 -06:00
|
|
|
# SPDX-License-Identifier: AGPL-3.0-only
|
|
|
|
|
2020-06-09 01:20:55 -06:00
|
|
|
defmodule Pleroma.MigrationHelper.NotificationBackfill do
|
2020-06-06 07:33:02 -06:00
|
|
|
alias Pleroma.Repo
|
2020-06-06 07:51:08 -06:00
|
|
|
alias Pleroma.User
|
2020-06-06 07:33:02 -06:00
|
|
|
|
|
|
|
import Ecto.Query
|
|
|
|
|
|
|
|
def fill_in_notification_types do
|
|
|
|
query =
|
|
|
|
from(n in Pleroma.Notification,
|
|
|
|
where: is_nil(n.type),
|
|
|
|
preload: :activity
|
|
|
|
)
|
|
|
|
|
|
|
|
query
|
2020-06-16 14:45:59 -06:00
|
|
|
|> Repo.chunk_stream(100)
|
2020-06-06 07:33:02 -06:00
|
|
|
|> Enum.each(fn notification ->
|
2020-09-14 05:08:12 -06:00
|
|
|
if notification.activity do
|
|
|
|
type = type_from_activity(notification.activity)
|
2020-06-06 07:33:02 -06:00
|
|
|
|
2020-09-14 05:08:12 -06:00
|
|
|
notification
|
|
|
|
|> Ecto.Changeset.change(%{type: type})
|
|
|
|
|> Repo.update()
|
|
|
|
end
|
2020-06-06 07:33:02 -06:00
|
|
|
end)
|
|
|
|
end
|
|
|
|
|
2020-07-03 03:25:12 -06:00
|
|
|
defp get_by_ap_id(ap_id) do
|
|
|
|
q =
|
|
|
|
from(u in User,
|
|
|
|
select: u.id
|
|
|
|
)
|
|
|
|
|
|
|
|
Repo.get_by(q, ap_id: ap_id)
|
|
|
|
end
|
|
|
|
|
2020-06-06 07:33:02 -06:00
|
|
|
# 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 ->
|
2020-07-03 03:25:12 -06:00
|
|
|
with %User{} = follower <- get_by_ap_id(activity.data["actor"]),
|
|
|
|
%User{} = followed <- get_by_ap_id(activity.data["object"]) do
|
2020-06-06 07:33:02 -06:00
|
|
|
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" ->
|
2020-09-14 05:08:12 -06:00
|
|
|
type_from_activity_object(activity)
|
2020-06-06 07:33:02 -06:00
|
|
|
|
|
|
|
t ->
|
|
|
|
raise "No notification type for activity type #{t}"
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2022-07-21 04:29:28 -06:00
|
|
|
defp type_from_activity_object(%{data: %{"type" => "Create"}}), do: "mention"
|
2020-06-06 07:33:02 -06:00
|
|
|
end
|