diff --git a/server/lib/orcasite/notifications/changes/extract_notification_instance_meta.ex b/server/lib/orcasite/notifications/changes/extract_notification_instance_meta.ex index 7f9ec859..a5f4c2c9 100644 --- a/server/lib/orcasite/notifications/changes/extract_notification_instance_meta.ex +++ b/server/lib/orcasite/notifications/changes/extract_notification_instance_meta.ex @@ -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"), diff --git a/server/lib/orcasite/notifications/manual_read_notifications_since.ex b/server/lib/orcasite/notifications/manual_read_notifications_since.ex index e275dbdd..9b8f5d78 100644 --- a/server/lib/orcasite/notifications/manual_read_notifications_since.ex +++ b/server/lib/orcasite/notifications/manual_read_notifications_since.ex @@ -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 diff --git a/server/lib/orcasite/notifications/resources/notification.ex b/server/lib/orcasite/notifications/resources/notification.ex index b5140f6e..9dabcded 100644 --- a/server/lib/orcasite/notifications/resources/notification.ex +++ b/server/lib/orcasite/notifications/resources/notification.ex @@ -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 @@ -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 @@ -218,20 +219,20 @@ 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) @@ -239,7 +240,7 @@ defmodule Orcasite.Notifications.Notification do notification |> Ash.Changeset.for_update(:update, %{target_count: target_count}) - |> Ash.update() + |> Ash.update(authorize?: false) end) {:ok, notification} diff --git a/server/lib/orcasite/notifications/resources/notification_instance.ex b/server/lib/orcasite/notifications/resources/notification_instance.ex index 2d09dda9..1e27ae2b 100644 --- a/server/lib/orcasite/notifications/resources/notification_instance.ex +++ b/server/lib/orcasite/notifications/resources/notification_instance.ex @@ -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) diff --git a/server/lib/orcasite/notifications/resources/subscription.ex b/server/lib/orcasite/notifications/resources/subscription.ex index 7631bbdf..c09da9bc 100644 --- a/server/lib/orcasite/notifications/resources/subscription.ex +++ b/server/lib/orcasite/notifications/resources/subscription.ex @@ -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 @@ -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 @@ -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 @@ -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 diff --git a/server/lib/orcasite/notifications/workers/send_notification_email.ex b/server/lib/orcasite/notifications/workers/send_notification_email.ex index 2cda3911..54105e02 100644 --- a/server/lib/orcasite/notifications/workers/send_notification_email.ex +++ b/server/lib/orcasite/notifications/workers/send_notification_email.ex @@ -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) @@ -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)) @@ -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 @@ -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 diff --git a/server/lib/orcasite/radio/candidate.ex b/server/lib/orcasite/radio/candidate.ex index eaea46a3..7c6db30e 100644 --- a/server/lib/orcasite/radio/candidate.ex +++ b/server/lib/orcasite/radio/candidate.ex @@ -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} diff --git a/server/lib/orcasite/radio/detection.ex b/server/lib/orcasite/radio/detection.ex index c19a2b0b..45c65e31 100644 --- a/server/lib/orcasite/radio/detection.ex +++ b/server/lib/orcasite/radio/detection.ex @@ -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 diff --git a/server/lib/orcasite/utils.ex b/server/lib/orcasite/utils.ex index aaffbced..c9a66b1c 100644 --- a/server/lib/orcasite/utils.ex +++ b/server/lib/orcasite/utils.ex @@ -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