Skip to content
Snippets Groups Projects
Commit f6446d7b authored by lain's avatar lain
Browse files

Handle incoming announces for chat messages.

parent ad4c9183
Branches develop
No related tags found
No related merge requests found
......@@ -58,7 +58,8 @@ defmodule Pleroma.Chat.Room do
def find_remote_room(room_name) do
with [name, domain] <- String.split(room_name, ":"),
url <- "http://#{domain}/rooms/#{name}",
# Assumes https
url <- "https://#{domain}/rooms/#{name}",
nil <- Object.get_by_ap_id(url),
{:ok, %{body: body, status_code: 200}} <- Pleroma.HTTP.get(url),
{:ok, room_data} <- Jason.decode(body) do
......
......@@ -169,8 +169,11 @@ defmodule Pleroma.Formatter do
subs ++
Enum.map(links, fn {uuid, url} ->
{:safe, link} = Phoenix.HTML.Link.link(url, to: url)
link = link
|> IO.iodata_to_binary
link =
link
|> IO.iodata_to_binary()
{uuid, link}
end)
......
......@@ -124,6 +124,24 @@ defmodule Pleroma.Web.ActivityPub.Transmogrifier do
end
end
def handle_incoming(%{
"type" => "Announce",
"actor" => room_id,
"to" => [room_id],
"object" => %{
"type" => "ChatMessage",
"content" => content,
"attributedTo" => user_id
}
}) do
with %Object{} = room <- Object.get_cached_by_ap_id(room_id),
%User{} = user <- User.get_or_fetch_by_ap_id(user_id),
true <- String.length(content) < @chat_incoming_limit do
Chat.add_remote_message(user, room, content)
{:ok, :nothing}
end
end
# TODO: validate those with a Ecto scheme
# - tags
# - emoji
......
......@@ -76,6 +76,10 @@ defmodule Pleroma.Web.ChatChannel.ChatChannelState do
Agent.start_link(fn -> %{max_id: 1, rooms: %{}} end, name: __MODULE__)
end
def clear do
Agent.update(__MODULE__, fn _ -> %{max_id: 1, rooms: %{}} end)
end
def add_message(message, room_name) do
Agent.get_and_update(__MODULE__, fn state ->
id = state[:max_id] + 1
......
......@@ -54,4 +54,14 @@ defmodule Pleroma.ChatTest do
assert Chat.Room.topic_name(room) == "public:pleroma.soykaf.com"
end
test "it adds a chat message to an existing room" do
{:ok, room} = Chat.Room.create_room("2hu")
user = insert(:user)
Chat.add_remote_message(user, room, "Why is Tenshi eating a corndog so cute?")
assert [message] = Pleroma.Web.ChatChannel.ChatChannelState.messages(room.data["id"])
assert message.text == "Why is Tenshi eating a corndog so cute?"
assert message.author.acct == user.nickname
end
end
......@@ -29,6 +29,8 @@ defmodule Pleroma.DataCase do
Cachex.clear(:user_cache)
:ok = Ecto.Adapters.SQL.Sandbox.checkout(Pleroma.Repo)
Pleroma.Web.ChatChannel.ChatChannelState.clear()
unless tags[:async] do
Ecto.Adapters.SQL.Sandbox.mode(Pleroma.Repo, {:shared, self()})
end
......
......@@ -426,5 +426,26 @@ defmodule Pleroma.Web.ActivityPub.TransmogrifierTest do
assert message.text == "A message"
assert message.author.acct == "admin@mastodon.example.org"
end
test "it handles incoming messages to chatrooms announced by the room" do
{:ok, room} = Chat.Room.create_room("2hu")
user_id = "http://mastodon.example.org/users/admin"
activity = %{
"type" => "Announce",
"object" => %{
"type" => "ChatMessage",
"attributedTo" => user_id,
"content" => "A message"
},
"actor" => room.data["id"],
"to" => [room.data["id"]]
}
{:ok, :nothing} = Transmogrifier.handle_incoming(activity)
assert [message] = Pleroma.Web.ChatChannel.ChatChannelState.messages(room.data["id"])
assert message.text == "A message"
assert message.author.acct == "admin@mastodon.example.org"
end
end
end
......@@ -22,7 +22,7 @@ defmodule Pleroma.Web.Salmon.SalmonTest do
end
test "generates an RSA private key pem" do
{:ok, key} = Salmon.generate_rsa_pem()
{:ok, key} = Pleroma.Web.ActivityPub.Actor.generate_rsa_pem()
assert is_binary(key)
assert Regex.match?(~r/RSA/, key)
end
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment