2024-02-21 07:22:49 +13:00
|
|
|
defmodule AshGraphql.Resource.Subscription.DefaultConfig do
|
2024-02-22 07:01:20 +13:00
|
|
|
alias AshGraphql.Resource.Subscription
|
|
|
|
|
2024-07-02 07:01:05 +12:00
|
|
|
def create_config(%Subscription{} = subscription, _domain, resource) do
|
2024-02-22 07:01:20 +13:00
|
|
|
config_module = String.to_atom(Macro.camelize(Atom.to_string(subscription.name)) <> ".Config")
|
|
|
|
|
|
|
|
defmodule config_module do
|
|
|
|
require Ash.Query
|
|
|
|
|
|
|
|
@subscription subscription
|
|
|
|
@resource resource
|
|
|
|
def config(_args, %{context: context}) do
|
|
|
|
read_action =
|
|
|
|
@subscription.read_action || Ash.Resource.Info.primary_action!(@resource, :read).name
|
|
|
|
|
2024-07-02 07:01:05 +12:00
|
|
|
case Ash.can(
|
2024-08-05 23:56:42 +12:00
|
|
|
Ash.Query.for_read(@resource, read_action),
|
2024-02-22 07:01:20 +13:00
|
|
|
context[:actor],
|
|
|
|
run_queries?: false,
|
|
|
|
alter_source?: true
|
|
|
|
) do
|
|
|
|
{:ok, true} ->
|
2024-08-05 23:56:42 +12:00
|
|
|
{:ok, topic: "*", context_id: "global"}
|
2024-02-22 07:01:20 +13:00
|
|
|
|
|
|
|
{:ok, true, filter} ->
|
2024-08-05 23:56:42 +12:00
|
|
|
# context_id is exposed to the client so we might need to encrypt it
|
|
|
|
# or save it in ets or something and send generate a hash or something
|
|
|
|
# as the context_id
|
2024-08-06 01:05:03 +12:00
|
|
|
dbg(filter)
|
|
|
|
{:ok, topic: "*", context_id: dbg(Base.encode64(:erlang.term_to_binary(filter)))}
|
2024-02-22 07:01:20 +13:00
|
|
|
|
2024-08-06 01:05:03 +12:00
|
|
|
e ->
|
2024-02-22 07:01:20 +13:00
|
|
|
{:error, "unauthorized"}
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
&config_module.config/2
|
|
|
|
end
|
2024-02-21 07:22:49 +13:00
|
|
|
end
|