Add notifications for updates in events you joined

Signed-off-by: marcin mikołajczak <git@mkljczk.pl>
This commit is contained in:
marcin mikołajczak 2022-10-01 22:26:35 +02:00
parent 9ff0eb5c4b
commit 0612c32c6e
8 changed files with 64 additions and 11 deletions

View file

@ -77,6 +77,7 @@ def unread_notifications_count(%User{id: user_id}) do
pleroma:participation_accepted
pleroma:participation_request
pleroma:event_reminder
pleroma:event_update
}
def changeset(%Notification{} = notification, attrs) do
@ -407,11 +408,20 @@ def create_notifications(_, _), do: {:ok, []}
defp do_create_notifications(%Activity{} = activity, options) do
do_send = Keyword.get(options, :do_send, true)
{enabled_participants, disabled_participants} =
get_notified_participants_from_activity(activity)
potential_participants = enabled_participants ++ disabled_participants
{enabled_receivers, disabled_receivers} = get_notified_from_activity(activity)
potential_receivers = enabled_receivers ++ disabled_receivers
potential_receivers = (enabled_receivers ++ disabled_receivers) -- potential_participants
{enabled_subscribers, disabled_subscribers} = get_notified_subscribers_from_activity(activity)
potential_subscribers = (enabled_subscribers ++ disabled_subscribers) -- potential_receivers
potential_subscribers =
(enabled_subscribers ++ disabled_subscribers) --
(potential_participants ++ potential_receivers)
notifications =
(Enum.map(potential_receivers, fn user ->
@ -421,6 +431,10 @@ defp do_create_notifications(%Activity{} = activity, options) do
Enum.map(potential_subscribers, fn user ->
do_send = do_send && user in enabled_subscribers
create_notification(activity, user, do_send: do_send, type: "status")
end) ++
Enum.map(potential_participants, fn user ->
do_send = do_send && user in enabled_participants
create_notification(activity, user, do_send: do_send, type: "pleroma:event_update")
end))
|> Enum.reject(&is_nil/1)
@ -616,6 +630,27 @@ def get_notified_subscribers_from_activity(
def get_notified_subscribers_from_activity(_, _), do: {[], []}
def get_notified_participants_from_activity(activity, local_only \\ true)
def get_notified_participants_from_activity(
%Activity{data: %{"type" => "Update"}} = activity,
local_only
) do
notification_enabled_ap_ids =
[]
|> Utils.maybe_notify_participants(activity)
potential_receivers =
User.get_users_from_set(notification_enabled_ap_ids, local_only: local_only)
notification_enabled_users =
Enum.filter(potential_receivers, fn u -> u.ap_id in notification_enabled_ap_ids end)
{notification_enabled_users, potential_receivers -- notification_enabled_users}
end
def get_notified_participants_from_activity(_, _), do: {[], []}
# For some activities, only notify the author of the object
def get_potential_receiver_ap_ids(%{data: %{"type" => type, "object" => object_id}})
when type in ~w{Like Announce EmojiReact} do

View file

@ -124,11 +124,6 @@ def make_update_object_data(original_data, new_data, date) do
original_data
|> update_content_fields(new_data)
IO.inspect(updated_data)
IO.inspect(updated)
IO.inspect(original_data)
IO.inspect(new_data)
if not updated do
updated_data
else

View file

@ -205,7 +205,8 @@ defp notification_type do
"status",
"pleroma:participation_accepted",
"pleroma:participation_request",
"pleroma:event_reminder"
"pleroma:event_reminder",
"pleroma:event_update"
],
description: """
The type of event that resulted in the notification.
@ -222,6 +223,7 @@ defp notification_type do
- `pleroma:report` - Someone was reported
- `status` - Someone you are subscribed to created a status
- `pleroma:event_reminder` An event you are participating in or created is taking place soon
- `pleroma:event_update` An event you are participating in was edited
- `pleroma:participation_request - Someone wants to participate in your event
- `pleroma:participation_accepted - Your event participation request was accepted
"""

View file

@ -141,7 +141,7 @@ defp event_object(draft) do
"mediaType" => Utils.get_content_type(draft.params[:content_type])
},
"generator" => draft.params[:generator],
"content_type" => draft.params[:content_type],
"content_type" => draft.params[:content_type]
}
%__MODULE__{draft | object: object}

View file

@ -424,6 +424,21 @@ def maybe_notify_followers(recipients, %Activity{data: %{"type" => "Move"}} = ac
def maybe_notify_followers(recipients, _), do: recipients
def maybe_notify_participants(
recipients,
%Activity{data: %{"type" => "Update"}} = activity
) do
with %Object{data: object} <- Object.normalize(activity, fetch: false) do
participant_ids = Map.get(object, "participations", [])
recipients ++ participant_ids
else
_e -> recipients
end
end
def maybe_notify_participants(recipients, _), do: recipients
def maybe_extract_mentions(%{"tag" => tag}) do
tag
|> Enum.filter(fn x -> is_map(x) && x["type"] == "Mention" end)

View file

@ -56,6 +56,7 @@ def index(conn, %{account_id: account_id} = params) do
pleroma:participation_request
pleroma:participation_accepted
pleroma:event_reminder
pleroma:event_update
}
def index(%{assigns: %{user: user}} = conn, params) do
params =

View file

@ -113,7 +113,7 @@ def render(
"reblog" ->
put_status(response, parent_activity_fn.(), reading_user, status_render_opts)
"update" ->
type when type in ["update", "pleroma:event_update"] ->
put_status(response, parent_activity_fn.(), reading_user, status_render_opts)
"move" ->

View file

@ -18,6 +18,11 @@ def up do
alter type notification_type add value 'pleroma:event_reminder'
"""
|> execute()
"""
alter type notification_type add value 'pleroma:event_update'
"""
|> execute()
end
def down do
@ -26,7 +31,7 @@ def down do
end
"""
delete from notifications where type in ('pleroma:participation_accepted', 'pleroma:participation_request', 'pleroma:event_reminder')
delete from notifications where type in ('pleroma:participation_accepted', 'pleroma:participation_request', 'pleroma:event_reminder', 'pleroma:event_update')
"""
|> execute()