defmodule TribeOne.TribesPlugin.Aether.Chat.Message do @moduledoc """ Public synced chat message. """ use Ash.Resource, otp_app: :tribe_one_aether, domain: TribeOne.TribesPlugin.Aether.Chat, data_layer: AshPostgres.DataLayer, extensions: [AshNostrSync] import Ash.Expr postgres do table("aether_chat_messages") repo(Tribes.Repo) custom_indexes do index([:channel_id, :inserted_at]) index([:author_pubkey]) index([:client_message_id], unique: true, where: "client_message_id IS NOT NULL AND deleted_at IS NULL" ) end end nostr_sync do namespace("plugins.aether.chat.message") lane(:bulk) publish?(true) consume?(true) end actions do defaults([:read]) read :by_id do get?(true) get_by(:id) end read :by_channel do argument :channel_id, :uuid do allow_nil?(false) end prepare( build(filter: expr(channel_id == ^arg(:channel_id)), sort: [inserted_at: :asc, id: :asc]) ) end create :create do accept([ :id, :channel_id, :author_id, :author_pubkey, :body, :client_message_id, :metadata ]) change(AshNostrSync.PublishChange) end create :sync_upsert do accept([ :id, :channel_id, :author_id, :author_pubkey, :body, :client_message_id, :metadata ]) upsert?(true) end update :update do require_atomic?(false) accept([:body, :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 :author_id, :uuid do public?(true) end attribute :author_pubkey, :string do public?(true) end attribute :body, :string do allow_nil?(false) public?(true) constraints(allow_empty?: false, trim?: true) end attribute :client_message_id, :string do public?(true) end attribute :metadata, :map do allow_nil?(false) default(%{}) public?(true) end timestamps(type: :utc_datetime) end end