You've already forked tribes-plugin-aether
forked from tribes/tribes-plugin-template
b4b8c83ddb
Adopt canonical plugin id/slug manifest fields, vendor-prefixed OTP app naming, and fully-qualified capability ids for Aether.
128 lines
2.6 KiB
Elixir
128 lines
2.6 KiB
Elixir
defmodule TribeOne.TribesPlugin.Aether.Chat.Participant do
|
|
@moduledoc """
|
|
Participant projection for Aether conversations.
|
|
"""
|
|
|
|
use Ash.Resource,
|
|
otp_app: :tribe_one_aether,
|
|
domain: TribeOne.TribesPlugin.Aether.Chat,
|
|
data_layer: AshPostgres.DataLayer,
|
|
extensions: [AshNostrSync]
|
|
|
|
import Ash.Expr
|
|
|
|
@roles [:owner, :member]
|
|
|
|
postgres do
|
|
table("aether_chat_participants")
|
|
repo(Tribes.Repo)
|
|
|
|
custom_indexes do
|
|
index([:channel_id, :pubkey], unique: true, where: "deleted_at IS NULL")
|
|
index([:pubkey], where: "deleted_at IS NULL")
|
|
end
|
|
end
|
|
|
|
nostr_sync do
|
|
namespace("plugins.aether.chat.participant")
|
|
lane(:control)
|
|
publish?(true)
|
|
consume?(true)
|
|
end
|
|
|
|
actions do
|
|
defaults([:read])
|
|
|
|
read :by_channel_pubkey do
|
|
get?(true)
|
|
|
|
argument :channel_id, :uuid do
|
|
allow_nil?(false)
|
|
end
|
|
|
|
argument :pubkey, :string do
|
|
allow_nil?(false)
|
|
end
|
|
|
|
filter(expr(channel_id == ^arg(:channel_id) and pubkey == ^arg(:pubkey)))
|
|
end
|
|
|
|
read :by_pubkey do
|
|
argument :pubkey, :string do
|
|
allow_nil?(false)
|
|
end
|
|
|
|
prepare(build(filter: expr(pubkey == ^arg(:pubkey)), sort: [inserted_at: :desc]))
|
|
end
|
|
|
|
create :create do
|
|
accept([:id, :channel_id, :pubkey, :user_id, :display_name, :role, :metadata])
|
|
change(AshNostrSync.PublishChange)
|
|
end
|
|
|
|
create :sync_upsert do
|
|
accept([:id, :channel_id, :pubkey, :user_id, :display_name, :role, :metadata])
|
|
upsert?(true)
|
|
end
|
|
|
|
update :update do
|
|
require_atomic?(false)
|
|
accept([:user_id, :display_name, :role, :metadata])
|
|
change(AshNostrSync.PublishChange)
|
|
end
|
|
|
|
destroy :destroy do
|
|
require_atomic?(false)
|
|
soft?(true)
|
|
soft_delete()
|
|
change(AshNostrSync.PublishChange)
|
|
end
|
|
end
|
|
|
|
relationships do
|
|
belongs_to :channel, TribeOne.TribesPlugin.Aether.Chat.Channel do
|
|
allow_nil?(false)
|
|
attribute_type(:uuid)
|
|
public?(true)
|
|
end
|
|
end
|
|
|
|
attributes do
|
|
attribute :id, :uuid do
|
|
allow_nil?(false)
|
|
primary_key?(true)
|
|
public?(true)
|
|
writable?(true)
|
|
default(&Ash.UUID.generate/0)
|
|
end
|
|
|
|
attribute :pubkey, :string do
|
|
allow_nil?(false)
|
|
public?(true)
|
|
end
|
|
|
|
attribute :user_id, :uuid do
|
|
public?(true)
|
|
end
|
|
|
|
attribute :display_name, :string do
|
|
public?(true)
|
|
end
|
|
|
|
attribute :role, :atom do
|
|
constraints(one_of: @roles)
|
|
allow_nil?(false)
|
|
default(:member)
|
|
public?(true)
|
|
end
|
|
|
|
attribute :metadata, :map do
|
|
allow_nil?(false)
|
|
default(%{})
|
|
public?(true)
|
|
end
|
|
|
|
timestamps(type: :utc_datetime)
|
|
end
|
|
end
|