aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorlain <lain@soykaf.club>2020-08-25 11:30:08 +0200
committerlain <lain@soykaf.club>2020-08-25 11:30:08 +0200
commit4fd705e832702f24377d6273a47cfc3752e1241b (patch)
tree8db446d74f1fdebfd2f1bb507f08df00a56a0c13
parentdd3ee3516cdae57616fca3f7cb90a24e7bf93843 (diff)
downloadpleroma-4fd705e832702f24377d6273a47cfc3752e1241b.tar.gz
MatrixController: Implement basic `messages` endpoint.
-rw-r--r--lib/pleroma/web/matrix_controller.ex161
-rw-r--r--lib/pleroma/web/router.ex1
-rw-r--r--test/web/matrix_controller_test.exs16
3 files changed, 110 insertions, 68 deletions
diff --git a/lib/pleroma/web/matrix_controller.ex b/lib/pleroma/web/matrix_controller.ex
index 1bde2746b..1a0af2d95 100644
--- a/lib/pleroma/web/matrix_controller.ex
+++ b/lib/pleroma/web/matrix_controller.ex
@@ -49,7 +49,8 @@ defmodule Pleroma.Web.MatrixController do
:capabilities,
:room_members,
:publicised_groups,
- :turn_server
+ :turn_server,
+ :room_messages
]
)
@@ -78,7 +79,8 @@ defmodule Pleroma.Web.MatrixController do
end
def login(conn, params) do
- username = params["identifier"]["user"]
+ IO.inspect(params)
+ username = params["identifier"]["user"] || params["user"]
password = params["password"]
dn = params["initial_device_display_name"]
@@ -91,7 +93,8 @@ defmodule Pleroma.Web.MatrixController do
data = %{
user_id: "@#{user.nickname}:#{Pleroma.Web.Endpoint.host()}",
access_token: token.token,
- device_id: app.client_id
+ device_id: app.client_id,
+ home_server: "matrixtest.ngrok.io"
}
conn
@@ -165,6 +168,75 @@ defmodule Pleroma.Web.MatrixController do
"@" <> nick
end
+ defp messages_from_cmrs(cmrs) do
+ cmrs
+ |> Enum.map(fn message ->
+ chat_data = message.object.data
+ author = User.get_cached_by_ap_id(chat_data["actor"])
+
+ {:ok, date, _} = DateTime.from_iso8601(chat_data["published"])
+ {:ok, txn_id} = Cachex.get(:matrix_cache, "txn_id:#{message.id}")
+
+ messages = [
+ %{
+ content: %{
+ body: chat_data["content"] |> HTML.strip_tags(),
+ msgtype: "m.text",
+ format: "org.matrix.custom.html",
+ formatted_body: chat_data["content"]
+ },
+ type: "m.room.message",
+ event_id: message.id,
+ room_id: message.chat_id,
+ sender: matrix_name(author),
+ origin_server_ts: date |> DateTime.to_unix(:millisecond),
+ unsigned: %{
+ age: DateTime.diff(DateTime.utc_now(), date, :millisecond),
+ transaction_id: txn_id
+ }
+ }
+ ]
+
+ messages =
+ if attachment = chat_data["attachment"] do
+ attachment =
+ Pleroma.Web.MastodonAPI.StatusView.render("attachment.json",
+ attachment: attachment
+ )
+
+ att = %{
+ content: %{
+ body: "an image",
+ msgtype: "m.image",
+ url: mxc(attachment.url),
+ info: %{
+ h: 640,
+ w: 480,
+ size: 500_000,
+ mimetype: attachment.pleroma.mime_type
+ }
+ },
+ type: "m.room.message",
+ event_id: attachment.id,
+ room_id: message.chat_id,
+ sender: matrix_name(author),
+ origin_server_ts: date |> DateTime.to_unix(:millisecond),
+ unsigned: %{
+ age: DateTime.diff(DateTime.utc_now(), date, :millisecond)
+ }
+ }
+
+ [att | messages]
+ else
+ messages
+ end
+
+ messages
+ end)
+ |> List.flatten()
+ |> Enum.reverse()
+ end
+
def sync(%{assigns: %{user: user}} = conn, params) do
with {:ok, timeout} when not is_nil(timeout) <- Ecto.Type.cast(:integer, params["timeout"]) do
Phoenix.PubSub.subscribe(:matrix, "user:#{user.id}")
@@ -211,71 +283,7 @@ defmodule Pleroma.Web.MatrixController do
messages =
q
|> Repo.all()
- |> Enum.map(fn message ->
- chat_data = message.object.data
- author = User.get_cached_by_ap_id(chat_data["actor"])
-
- {:ok, date, _} = DateTime.from_iso8601(chat_data["published"])
- {:ok, txn_id} = Cachex.get(:matrix_cache, "txn_id:#{message.id}")
-
- messages = [
- %{
- content: %{
- body: chat_data["content"] |> HTML.strip_tags(),
- msgtype: "m.text",
- format: "org.matrix.custom.html",
- formatted_body: chat_data["content"]
- },
- type: "m.room.message",
- event_id: message.id,
- room_id: chat.id,
- sender: matrix_name(author),
- origin_server_ts: date |> DateTime.to_unix(:millisecond),
- unsigned: %{
- age: DateTime.diff(DateTime.utc_now(), date, :millisecond),
- transaction_id: txn_id
- }
- }
- ]
-
- messages =
- if attachment = chat_data["attachment"] do
- attachment =
- Pleroma.Web.MastodonAPI.StatusView.render("attachment.json",
- attachment: attachment
- )
-
- att = %{
- content: %{
- body: "an image",
- msgtype: "m.image",
- url: mxc(attachment.url),
- info: %{
- h: 640,
- w: 480,
- size: 500_000,
- mimetype: attachment.pleroma.mime_type
- }
- },
- type: "m.room.message",
- event_id: attachment.id,
- room_id: chat.id,
- sender: matrix_name(author),
- origin_server_ts: date |> DateTime.to_unix(:millisecond),
- unsigned: %{
- age: DateTime.diff(DateTime.utc_now(), date, :millisecond)
- }
- }
-
- [att | messages]
- else
- messages
- end
-
- messages
- end)
- |> List.flatten()
- |> Enum.reverse()
+ |> messages_from_cmrs
room = %{
chat.id => %{
@@ -549,4 +557,21 @@ defmodule Pleroma.Web.MatrixController do
conn
|> json(%{})
end
+
+ def room_messages(%{assigns: %{user: %{id: user_id} = _user}} = conn, %{"room_id" => chat_id}) do
+ with %Chat{user_id: ^user_id} = chat <- Chat.get_by_id(chat_id) do
+ cmrs =
+ chat
+ |> MessageReference.for_chat_query()
+ |> limit(30)
+ |> Repo.all()
+
+ messages =
+ cmrs
+ |> messages_from_cmrs()
+
+ conn
+ |> json(%{chunk: messages, start: List.first(cmrs).id, end: List.last(cmrs).id})
+ end
+ end
end
diff --git a/lib/pleroma/web/router.ex b/lib/pleroma/web/router.ex
index ebffbf6a6..61cd0312e 100644
--- a/lib/pleroma/web/router.ex
+++ b/lib/pleroma/web/router.ex
@@ -745,6 +745,7 @@ defmodule Pleroma.Web.Router do
post("/client/r0/rooms/:room_id/read_markers", MatrixController, :set_read_marker)
put("/client/r0/rooms/:room_id/typing/:user_id", MatrixController, :typing)
get("/client/r0/rooms/:room_id/members", MatrixController, :room_members)
+ get("/client/r0/rooms/:room_id/messages", MatrixController, :room_messages)
post("/client/r0/publicised_groups", MatrixController, :publicised_groups)
get("/client/r0/voip/turnServer", MatrixController, :turn_server)
put("/client/r0/rooms/:room_id/send/:event_type/:txn_id", MatrixController, :send_event)
diff --git a/test/web/matrix_controller_test.exs b/test/web/matrix_controller_test.exs
index 0a74ea9f4..c8273641b 100644
--- a/test/web/matrix_controller_test.exs
+++ b/test/web/matrix_controller_test.exs
@@ -124,4 +124,20 @@ defmodule Pleroma.Web.MatrixControllerTest do
assert message["content"]["body"] == "morning weebs"
end
end
+
+ describe "room messages" do
+ test "it returns messages", %{
+ conn: conn,
+ chat: chat
+ } do
+ res =
+ conn
+ |> get("_matrix/client/r0/rooms/#{chat.id}/messages")
+ |> json_response(200)
+
+ assert res["chunk"]
+ assert res["start"]
+ assert res["end"]
+ end
+ end
end