Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Upgrade Ash 3.0 #521

Merged
merged 12 commits into from
Jul 3, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 1 addition & 3 deletions server/config/config.exs
Original file line number Diff line number Diff line change
Expand Up @@ -73,13 +73,11 @@ config :orcasite, OrcasiteWeb.Auth.AuthAccessPipeline,
error_handler: OrcasiteWeb.Auth.AuthErrorHandler

config :orcasite, :ecto_repos, [Orcasite.Repo]
config :orcasite, :ash_apis, [Orcasite.Notifications, Orcasite.Accounts, Orcasite.Radio]
config :orcasite, :ash_domains, [Orcasite.Notifications, Orcasite.Accounts, Orcasite.Radio]
config :orcasite, :ash_uuid, migration_default?: true
config :ash, :use_all_identities_in_manage_relationship?, false
config :ash, :custom_types, geometry: Orcasite.Types.Geometry
config :ash_graphql, :default_managed_relationship_type_name_template, :action_name
config :ash_graphql, :json_type, :json
config :ash, :utc_datetime_type, :datetime

config :mime, :types, %{
"application/vnd.api+json" => ["json"]
Expand Down
5 changes: 3 additions & 2 deletions server/lib/orcasite/accounts/accounts.ex
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
defmodule Orcasite.Accounts do
use Ash.Api, extensions: [AshAdmin.Api, AshGraphql.Api]
use Ash.Domain, extensions: [AshAdmin.Domain, AshGraphql.Domain]

resources do
registry Orcasite.Accounts.Registry
resource Orcasite.Accounts.User
resource Orcasite.Accounts.Token
end

admin do
Expand Down
8 changes: 0 additions & 8 deletions server/lib/orcasite/accounts/registry.ex

This file was deleted.

3 changes: 2 additions & 1 deletion server/lib/orcasite/accounts/token.ex
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
defmodule Orcasite.Accounts.Token do
use Ash.Resource,
domain: Orcasite.Accounts,
data_layer: AshPostgres.DataLayer,
extensions: [AshAuthentication.TokenResource],
authorizers: [Ash.Policy.Authorizer]

token do
api Orcasite.Accounts
domain Orcasite.Accounts
end

postgres do
Expand Down
17 changes: 9 additions & 8 deletions server/lib/orcasite/accounts/user.ex
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
defmodule Orcasite.Accounts.User do
use Ash.Resource,
domain: Orcasite.Accounts,
data_layer: AshPostgres.DataLayer,
extensions: [AshAuthentication, AshAdmin.Resource, AshGraphql.Resource],
authorizers: [Ash.Policy.Authorizer]
Expand All @@ -16,14 +17,15 @@ defmodule Orcasite.Accounts.User do

attributes do
uuid_primary_key :id
attribute :email, :ci_string, allow_nil?: false
attribute :email, :ci_string, allow_nil?: false, public?: true
attribute :hashed_password, :string, allow_nil?: false, sensitive?: true
attribute :first_name, :string
attribute :last_name, :string
attribute :admin, :boolean, default: false, allow_nil?: false
attribute :moderator, :boolean, default: false, allow_nil?: false
attribute :first_name, :string, public?: true
attribute :last_name, :string, public?: true
attribute :admin, :boolean, default: false, allow_nil?: false, public?: true
attribute :moderator, :boolean, default: false, allow_nil?: false, public?: true

attribute :username, :string do
public? true
allow_nil? true
constraints allow_empty?: false, trim?: true
end
Expand All @@ -33,7 +35,7 @@ defmodule Orcasite.Accounts.User do
end

authentication do
api Orcasite.Accounts
domain Orcasite.Accounts

strategies do
password :password do
Expand Down Expand Up @@ -106,7 +108,7 @@ defmodule Orcasite.Accounts.User do
end

code_interface do
define_for Orcasite.Accounts
domain Orcasite.Accounts

define :register_with_password
define :sign_in_with_password
Expand All @@ -133,7 +135,6 @@ defmodule Orcasite.Accounts.User do

graphql do
type :user
hide_fields [:hashed_password]

queries do
read_one :current_user, :current_user
Expand Down
4 changes: 2 additions & 2 deletions server/lib/orcasite/global_setup.ex
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,12 @@ defmodule Orcasite.GlobalSetup do
def populate_feed_streams do
Orcasite.Radio.Feed
|> Ash.Query.for_read(:read)
|> Orcasite.Radio.read!()
|> Ash.read!()
|> Stream.map(fn feed ->
Orcasite.Radio.AwsClient.list_timestamps(feed, fn timestamps ->
timestamps
|> Enum.map(&%{feed: feed, playlist_timestamp: &1})
|> Orcasite.Radio.bulk_create(Orcasite.Radio.FeedStream, :create)
|> Ash.bulk_create(Orcasite.Radio.FeedStream, :create)
end)
:ok
end)
Expand Down
9 changes: 7 additions & 2 deletions server/lib/orcasite/notifications.ex
Original file line number Diff line number Diff line change
@@ -1,8 +1,13 @@
defmodule Orcasite.Notifications do
use Ash.Api, extensions: [AshAdmin.Api, AshJsonApi.Api, AshGraphql.Api]
use Ash.Domain, extensions: [AshAdmin.Domain, AshJsonApi.Domain, AshGraphql.Domain]

resources do
registry Orcasite.Notifications.Registry
resource Orcasite.Notifications.Notification
resource Orcasite.Notifications.Subscriber
resource Orcasite.Notifications.Subscription
resource Orcasite.Notifications.NotificationInstance
resource Orcasite.Notifications.Token
resource Orcasite.Notifications.Job
end

admin do
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
defmodule Orcasite.Notifications.Changes.ExtractNotificationInstanceMeta do
use Ash.Resource.Change

alias Orcasite.Notifications
alias Orcasite.Notifications.{Notification, Subscription}

def change(
Expand All @@ -19,9 +18,9 @@ defmodule Orcasite.Notifications.Changes.ExtractNotificationInstanceMeta do
# body based on event type

with {:get_sub, {:ok, subscription}} <-
{:get_sub, Notifications.get(Subscription, changeset.arguments.subscription)},
{:get_sub, Ash.get(Subscription, changeset.arguments.subscription)},
{:get_notif, {:ok, notification}} <-
{:get_notif, Notifications.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
4 changes: 0 additions & 4 deletions server/lib/orcasite/notifications/event.ex
Original file line number Diff line number Diff line change
@@ -1,8 +1,4 @@
defmodule Orcasite.Notifications.Event do
def types do
[:confirmed_candidate, :new_detection]
end

def humanize(event_type, plural \\ false)
def humanize(:confirmed_candidate, false), do: "confirmed candidate"
def humanize(:confirmed_candidate, true), do: "confirmed candidates"
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
defmodule Orcasite.Notifications.ManualReadNotificationsSince do
use Ash.Resource.ManualRead

alias Orcasite.Notifications
alias Orcasite.Notifications.Notification

require Ash.Query
Expand All @@ -14,13 +13,13 @@ defmodule Orcasite.Notifications.ManualReadNotificationsSince do

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

Notification
|> Ash.Query.filter(
event_type == ^notification.event_type and
inserted_at > ^notification.inserted_at
)
|> Notifications.read()
|> Ash.read(authorize?: false)
end
end
15 changes: 0 additions & 15 deletions server/lib/orcasite/notifications/registry.ex

This file was deleted.

7 changes: 3 additions & 4 deletions server/lib/orcasite/notifications/resources/job.ex
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
defmodule Orcasite.Notifications.Job do
use Ash.Resource,
extensions: [AshAdmin.Resource, AshGraphql.Resource],
domain: Orcasite.Notifications,
extensions: [AshAdmin.Resource],
data_layer: AshPostgres.DataLayer

@states [:available, :scheduled, :executing, :retryable, :completed, :discarded, :cancelled]
Expand Down Expand Up @@ -59,9 +60,7 @@ defmodule Orcasite.Notifications.Job do
argument :email, :string
argument :node, :string

argument :event_type, :atom do
constraints one_of: Orcasite.Notifications.Event.types()
end
argument :event_type, Orcasite.Types.NotificationEventType

filter expr(
if(is_nil(^arg(:notification_id)),
Expand Down
Loading
Loading