SideEffects: Handle ChatMessage creation.
This commit is contained in:
parent
3775683a04
commit
2e78686686
5 changed files with 95 additions and 11 deletions
|
|
@ -18,23 +18,28 @@ defmodule Pleroma.Chat do
|
|||
schema "chats" do
|
||||
belongs_to(:user, User, type: FlakeId.Ecto.CompatType)
|
||||
field(:recipient, :string)
|
||||
field(:unread, :integer, default: 0)
|
||||
field(:unread, :integer, default: 0, read_after_writes: true)
|
||||
|
||||
timestamps()
|
||||
end
|
||||
|
||||
def creation_cng(struct, params) do
|
||||
struct
|
||||
|> cast(params, [:user_id, :recipient])
|
||||
|> cast(params, [:user_id, :recipient, :unread])
|
||||
|> validate_required([:user_id, :recipient])
|
||||
|> unique_constraint(:user_id, name: :chats_user_id_recipient_index)
|
||||
end
|
||||
|
||||
def get_or_create(user_id, recipient) do
|
||||
def get(user_id, recipient) do
|
||||
__MODULE__
|
||||
|> Repo.get_by(user_id: user_id, recipient: recipient)
|
||||
end
|
||||
|
||||
def bump_or_create(user_id, recipient) do
|
||||
%__MODULE__{}
|
||||
|> creation_cng(%{user_id: user_id, recipient: recipient})
|
||||
|> creation_cng(%{user_id: user_id, recipient: recipient, unread: 1})
|
||||
|> Repo.insert(
|
||||
on_conflict: [set: [updated_at: NaiveDateTime.utc_now()]],
|
||||
on_conflict: [set: [updated_at: NaiveDateTime.utc_now()], inc: [unread: 1]],
|
||||
conflict_target: [:user_id, :recipient]
|
||||
)
|
||||
end
|
||||
|
|
|
|||
|
|
@ -10,6 +10,28 @@ defmodule Pleroma.Web.ActivityPub.Builder do
|
|||
alias Pleroma.Web.ActivityPub.Utils
|
||||
alias Pleroma.Web.ActivityPub.Visibility
|
||||
|
||||
def create(actor, object_id, recipients) do
|
||||
{:ok,
|
||||
%{
|
||||
"id" => Utils.generate_activity_id(),
|
||||
"actor" => actor.ap_id,
|
||||
"to" => recipients,
|
||||
"object" => object_id,
|
||||
"type" => "Create"
|
||||
}, []}
|
||||
end
|
||||
|
||||
def chat_message(actor, recipient, content) do
|
||||
{:ok,
|
||||
%{
|
||||
"id" => Utils.generate_object_id(),
|
||||
"actor" => actor.ap_id,
|
||||
"type" => "ChatMessage",
|
||||
"to" => [recipient],
|
||||
"content" => content
|
||||
}, []}
|
||||
end
|
||||
|
||||
@spec like(User.t(), Object.t()) :: {:ok, map(), keyword()}
|
||||
def like(actor, object) do
|
||||
object_actor = User.get_cached_by_ap_id(object.data["actor"])
|
||||
|
|
|
|||
|
|
@ -5,8 +5,10 @@ defmodule Pleroma.Web.ActivityPub.SideEffects do
|
|||
liked object, a `Follow` activity will add the user to the follower
|
||||
collection, and so on.
|
||||
"""
|
||||
alias Pleroma.Chat
|
||||
alias Pleroma.Notification
|
||||
alias Pleroma.Object
|
||||
alias Pleroma.User
|
||||
alias Pleroma.Web.ActivityPub.Utils
|
||||
|
||||
def handle(object, meta \\ [])
|
||||
|
|
@ -21,8 +23,35 @@ defmodule Pleroma.Web.ActivityPub.SideEffects do
|
|||
{:ok, object, meta}
|
||||
end
|
||||
|
||||
def handle(%{data: %{"type" => "Create", "object" => object_id}} = activity, meta) do
|
||||
object = Object.get_by_ap_id(object_id)
|
||||
|
||||
{:ok, _object} = handle_object_creation(object)
|
||||
|
||||
{:ok, activity, meta}
|
||||
end
|
||||
|
||||
# Nothing to do
|
||||
def handle(object, meta) do
|
||||
{:ok, object, meta}
|
||||
end
|
||||
|
||||
def handle_object_creation(%{data: %{"type" => "ChatMessage"}} = object) do
|
||||
actor = User.get_cached_by_ap_id(object.data["actor"])
|
||||
recipient = User.get_cached_by_ap_id(hd(object.data["to"]))
|
||||
|
||||
[[actor, recipient], [recipient, actor]]
|
||||
|> Enum.each(fn [user, other_user] ->
|
||||
if user.local do
|
||||
Chat.bump_or_create(user.id, other_user.ap_id)
|
||||
end
|
||||
end)
|
||||
|
||||
{:ok, object}
|
||||
end
|
||||
|
||||
# Nothing to do
|
||||
def handle_object_creation(object) do
|
||||
{:ok, object}
|
||||
end
|
||||
end
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue