aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorlain <lain@soykaf.club>2020-08-21 12:31:17 +0200
committerlain <lain@soykaf.club>2020-08-21 12:31:17 +0200
commit2f1428d7ad1e5770d4fe4c65f13cdb1bf06915c8 (patch)
treeee29d2b81f1db635a1bfa38cf5dc5efe6bd5dbe3
parent6a6548113d94018a235b6de7b01df11550c455f6 (diff)
downloadpleroma-2f1428d7ad1e5770d4fe4c65f13cdb1bf06915c8.tar.gz
.
-rw-r--r--lib/pleroma/plugs/frontend_static.ex3
-rw-r--r--lib/pleroma/web/endpoint.ex6
-rw-r--r--lib/pleroma/web/matrix_controller.ex120
-rw-r--r--lib/pleroma/web/router.ex1
4 files changed, 105 insertions, 25 deletions
diff --git a/lib/pleroma/plugs/frontend_static.ex b/lib/pleroma/plugs/frontend_static.ex
index f549ca75f..017e0819e 100644
--- a/lib/pleroma/plugs/frontend_static.ex
+++ b/lib/pleroma/plugs/frontend_static.ex
@@ -32,6 +32,9 @@ defmodule Pleroma.Plugs.FrontendStatic do
|> Plug.Static.init()
end
+ def call(%{path_info: ["_matrix" | _]} = conn, _opts), do: conn
+ def call(%{path_info: ["api" | _]} = conn, _opts), do: conn
+
def call(conn, opts) do
frontend_type = Map.get(opts, :frontend_type, :primary)
path = file_path("", frontend_type)
diff --git a/lib/pleroma/web/endpoint.ex b/lib/pleroma/web/endpoint.ex
index 1d9c3acec..527fb288d 100644
--- a/lib/pleroma/web/endpoint.ex
+++ b/lib/pleroma/web/endpoint.ex
@@ -20,7 +20,7 @@ defmodule Pleroma.Web.Endpoint do
# If you're adding new paths to `only:` you'll need to configure them in InstanceStatic as well
# Cache-control headers are duplicated in case we turn off etags in the future
plug(Pleroma.Plugs.InstanceStatic,
- at: "/s",
+ at: "/",
gzip: true,
cache_control_for_etags: @static_cache_control,
headers: %{
@@ -30,7 +30,7 @@ defmodule Pleroma.Web.Endpoint do
# Careful! No `only` restriction here, as we don't know what frontends contain.
plug(Pleroma.Plugs.FrontendStatic,
- at: "/s",
+ at: "/",
frontend_type: :primary,
gzip: true,
cache_control_for_etags: @static_cache_control,
@@ -45,7 +45,7 @@ defmodule Pleroma.Web.Endpoint do
# when deploying your static files in production.
plug(
Plug.Static,
- at: "/s",
+ at: "/",
from: :pleroma,
only: Pleroma.Constants.static_only_files(),
# credo:disable-for-previous-line Credo.Check.Readability.MaxLineLength
diff --git a/lib/pleroma/web/matrix_controller.ex b/lib/pleroma/web/matrix_controller.ex
index b67b6d90a..a6f8c1efa 100644
--- a/lib/pleroma/web/matrix_controller.ex
+++ b/lib/pleroma/web/matrix_controller.ex
@@ -5,16 +5,17 @@
defmodule Pleroma.Web.MatrixController do
use Pleroma.Web, :controller
- alias Pleroma.User
# alias Pleroma.Web.MediaProxy
+ alias Pleroma.Chat
+ alias Pleroma.Chat.MessageReference
+ alias Pleroma.HTML
alias Pleroma.Plugs.AuthenticationPlug
alias Pleroma.Plugs.OAuthScopesPlug
+ alias Pleroma.Repo
+ alias Pleroma.User
+ alias Pleroma.Web.MediaProxy
alias Pleroma.Web.OAuth.App
alias Pleroma.Web.OAuth.Token
- alias Pleroma.Chat
- alias Pleroma.Chat.MessageReference
- alias Pleroma.Repo
- alias Pleroma.HTML
import Ecto.Query
plug(
@@ -29,6 +30,10 @@ defmodule Pleroma.Web.MatrixController do
when action in [:pushrules, :sync, :filter, :key_query, :profile]
)
+ def mxc(url) do
+ "mxc://localhost/#{url |> Base.encode64()}"
+ end
+
def client_versions(conn, _) do
data = %{
versions: ["r0.0.1", "r0.1.0", "r0.2.0", "r0.3.0", "r0.4.0", "r0.5.0"]
@@ -162,6 +167,26 @@ defmodule Pleroma.Web.MatrixController do
|> Enum.reduce(%{}, fn chat, acc ->
recipient = User.get_by_ap_id(chat.recipient)
+ membership_events =
+ [user, recipient]
+ |> Enum.map(fn member ->
+ avatar = User.avatar_url(user) |> MediaProxy.url()
+
+ %{
+ content: %{
+ membership: "join",
+ avatar_url: mxc(avatar),
+ displayname: member.name
+ },
+ type: "m.room.member",
+ event_id: "#{chat.id}/join/#{member.id}",
+ room_id: chat.id,
+ sender: matrix_name(member),
+ origin_ts: DateTime.utc_now() |> DateTime.to_unix(),
+ state_key: matrix_name(member)
+ }
+ end)
+
messages =
chat
|> MessageReference.for_chat_query()
@@ -172,23 +197,65 @@ defmodule Pleroma.Web.MatrixController do
{:ok, date, _} = DateTime.from_iso8601(chat_data["published"])
- %{
- 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_ts: date |> DateTime.to_unix(),
- unsigned: %{
- age: 0
+ IO.inspect(chat_data["attachment"])
+
+ 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)
+ }
}
- }
+ ]
+
+ 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()
room = %{
chat.id => %{
@@ -197,7 +264,7 @@ defmodule Pleroma.Web.MatrixController do
"m.joined_member_count" => 2,
"m.invited_member_count" => 0
},
- state: %{events: []},
+ state: %{events: membership_events},
ephemeral: %{events: []},
timeline: %{
events: messages,
@@ -259,12 +326,21 @@ defmodule Pleroma.Web.MatrixController do
|> String.trim_trailing("@#{Pleroma.Web.Endpoint.host()}")
user = User.get_by_nickname(nickname)
+ avatar = User.avatar_url(user) |> MediaProxy.url()
data = %{
- displayname: user.name
+ displayname: user.name,
+ avatar_url: mxc(avatar)
}
conn
|> json(data)
end
+
+ def download(conn, params) do
+ {:ok, url} = params["file"] |> Base.decode64()
+
+ conn
+ |> redirect(external: url)
+ end
end
diff --git a/lib/pleroma/web/router.ex b/lib/pleroma/web/router.ex
index 89aef5164..aa1112e2b 100644
--- a/lib/pleroma/web/router.ex
+++ b/lib/pleroma/web/router.ex
@@ -717,6 +717,7 @@ defmodule Pleroma.Web.Router do
post("/client/r0/login", MatrixController, :login)
get("/client/r0/presence/:user_id/status", MatrixController, :presence_status)
get("/client/r0/user/:user_id/filter/:filter_id", MatrixController, :filter)
+ get("/media/r0/download/:authority/:file", MatrixController, :download)
end
scope "/_matrix", Pleroma.Web do