Skip to content

Commit

Permalink
Update sending notifications
Browse files Browse the repository at this point in the history
  • Loading branch information
skanderm committed Jul 2, 2024
1 parent 480aab1 commit 7cca2b9
Show file tree
Hide file tree
Showing 9 changed files with 46 additions and 37 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ defmodule Orcasite.Notifications.Changes.ExtractNotificationInstanceMeta do
with {:get_sub, {:ok, subscription}} <-
{:get_sub, Ash.get(Subscription, changeset.arguments.subscription)},
{:get_notif, {:ok, notification}} <-
{:get_notif, Ash.get(Notification, changeset.arguments.notification)} do
{:get_notif, Ash.get(Notification, changeset.arguments.notification, authorize?: false)} do
changeset
|> Ash.Changeset.change_attribute(:meta, %{
email: Map.get(subscription.meta, "email"),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,13 @@ defmodule Orcasite.Notifications.ManualReadNotificationsSince do

notification =
Notification
|> Ash.get!(notification_id)
|> Ash.get!(notification_id, authorize?: false)

Notification
|> Ash.Query.filter(
event_type == ^notification.event_type and
inserted_at > ^notification.inserted_at
)
|> Ash.read()
|> Ash.read(authorize?: false)
end
end
45 changes: 23 additions & 22 deletions server/lib/orcasite/notifications/resources/notification.ex
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,8 @@ defmodule Orcasite.Notifications.Notification do
do: 1,
else: notified_count / target_count
)
), public?: true
),
public?: true

calculate :finished, :boolean, expr(notified_count == target_count), public?: true
end
Expand Down Expand Up @@ -143,22 +144,22 @@ defmodule Orcasite.Notifications.Notification do

change set_attribute(:event_type, :confirmed_candidate)

change fn changeset, _context ->
candidate_id =
Ash.Changeset.get_argument(changeset, :candidate_id)

candidate =
Orcasite.Radio.Candidate
|> Ash.get(candidate_id)
|> Ash.load!(:feed)

changeset
|> Ash.Changeset.change_attribute(:meta, %{
candidate_id: candidate_id,
node: candidate.feed.slug,
message: Ash.Changeset.get_argument(changeset, :message)
})
end
change before_action(fn changeset, _context ->
candidate_id =
Ash.Changeset.get_argument(changeset, :candidate_id)

candidate =
Orcasite.Radio.Candidate
|> Ash.get(candidate_id)
|> Ash.load!(:feed)

changeset
|> Ash.Changeset.change_attribute(:meta, %{
candidate_id: candidate_id,
node: candidate.feed.slug,
message: Ash.Changeset.get_argument(changeset, :message)
})
end)
end

create :notify_new_detection do
Expand Down Expand Up @@ -218,28 +219,28 @@ defmodule Orcasite.Notifications.Notification do
changes do
change fn changeset, _context ->
changeset
|> Ash.Changeset.after_action(fn _, notification ->
|> Ash.Changeset.after_action(fn _, %{id: notification_id} = notification ->
Task.Supervisor.async_nolink(Orcasite.TaskSupervisor, fn ->
target_count =
Orcasite.Notifications.Subscription
|> Ash.Query.for_read(:available_for_notification, %{
notification_id: notification.id,
notification_id: notification_id,
event_type: notification.event_type
})
|> Ash.stream!()
|> Stream.map(fn subscription ->
Orcasite.Notifications.NotificationInstance
|> Ash.Changeset.for_create(:create_with_relationships, %{
notification: notification.id,
subscription: subscription.id
notification: notification,
subscription: subscription
})
|> Ash.create!()
end)
|> Enum.reduce(0, fn _, sum -> sum + 1 end)

notification
|> Ash.Changeset.for_update(:update, %{target_count: target_count})
|> Ash.update()
|> Ash.update(authorize?: false)
end)

{:ok, notification}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,8 +46,8 @@ defmodule Orcasite.Notifications.NotificationInstance do
defaults [:create, :read, :update, :destroy]

create :create_with_relationships do
argument :subscription, :uuid
argument :notification, :uuid
argument :subscription, :map
argument :notification, :map

change manage_relationship(:subscription, type: :append)
change manage_relationship(:notification, type: :append)
Expand Down
5 changes: 2 additions & 3 deletions server/lib/orcasite/notifications/resources/subscription.ex
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ defmodule Orcasite.Notifications.Subscription do

attribute :active, :boolean, default: true

attribute :event_type, Orcasite.Types.NotificationEventType
attribute :event_type, Orcasite.Types.NotificationEventType

attribute :last_notified_at, :utc_datetime_usec

Expand Down Expand Up @@ -136,6 +136,7 @@ defmodule Orcasite.Notifications.Subscription do

argument :name, :string
argument :email, :string

argument :event_type, Orcasite.Types.NotificationEventType do
default nil
end
Expand Down Expand Up @@ -167,7 +168,6 @@ defmodule Orcasite.Notifications.Subscription do
change manage_relationship(:subscriber_id, :subscriber, type: :append)
end


update :update_last_notification do
require_atomic? false

Expand All @@ -176,7 +176,6 @@ defmodule Orcasite.Notifications.Subscription do
change manage_relationship(:last_notification, type: :append)
change set_attribute(:last_notified_at, &DateTime.utc_now/0)
end

end

def unsubscribe_token(subscription) do
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ defmodule Orcasite.Notifications.Workers.SendNotificationEmail do
"notification_instance_id" => notification_instance_id
} = _args
}) do
notification = Notification |> Ash.get!(notification_id)
notification = Notification |> Ash.get!(notification_id, authorize?: false)

subscription =
Subscription |> Ash.get!(subscription_id) |> Ash.load!(:subscriber)
Expand All @@ -35,7 +35,7 @@ defmodule Orcasite.Notifications.Workers.SendNotificationEmail do
notif_id ->
Notification
|> Ash.Query.for_read(:since_notification, %{notification_id: notif_id})
|> Ash.read!()
|> Ash.read!(authorize?: false)
end
|> Enum.filter(&(&1.id != notification_id))

Expand All @@ -50,7 +50,7 @@ defmodule Orcasite.Notifications.Workers.SendNotificationEmail do
notifications_since: notifications_since |> Enum.map(& &1.meta),
notifications_since_count: Enum.count(notifications_since)
})
|> email_for_notif(stringify(params["event_type"]))
|> email_for_notif(stringify(notification.event_type))
|> Orcasite.Mailer.deliver()

subscription
Expand All @@ -70,7 +70,7 @@ defmodule Orcasite.Notifications.Workers.SendNotificationEmail do
end
end)

Orcasite.Notifications.Notification.increment_notified_count(notification)
Orcasite.Notifications.Notification.increment_notified_count(notification, authorize?: false)

:ok
end
Expand Down
4 changes: 2 additions & 2 deletions server/lib/orcasite/radio/candidate.ex
Original file line number Diff line number Diff line change
Expand Up @@ -165,11 +165,11 @@ defmodule Orcasite.Radio.Candidate do
event_type: Ash.Changeset.get_argument(changeset, :event_type),
active: true
})
|> Ash.read!()
|> Ash.read!(authorize?: false)
|> Enum.map(fn notification ->
notification
|> Ash.Changeset.for_update(:cancel_notification, %{})
|> Ash.update!()
|> Ash.update!(authorize?: false)
end)

{:ok, record}
Expand Down
3 changes: 2 additions & 1 deletion server/lib/orcasite/radio/detection.ex
Original file line number Diff line number Diff line change
Expand Up @@ -281,7 +281,8 @@ defmodule Orcasite.Radio.Detection do
detection.feed.slug,
detection.description,
detection.listener_count,
detection.candidate.id
detection.candidate.id,
authorize?: false
)
end)
end
Expand Down
8 changes: 8 additions & 0 deletions server/lib/orcasite/utils.ex
Original file line number Diff line number Diff line change
Expand Up @@ -28,4 +28,12 @@ defmodule Orcasite.Utils do
def atomize_keys(not_a_map) do
not_a_map
end

def oban_error(error_tuple) do
error_tuple
|> elem(0)
|> Jason.decode!()
|> Map.get("error")
|> IO.puts
end
end

0 comments on commit 7cca2b9

Please sign in to comment.