aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorlain <lain@soykaf.club>2020-08-22 23:26:42 +0200
committerlain <lain@soykaf.club>2020-08-22 23:26:42 +0200
commit1ddb55a6d23cb01c2ed4940782fcacd86a56e23f (patch)
treee45ee10efc3d9acf62ebd7fedde227173f018414
parentdb057e9e13fdb920147af486c76fadc4e032e6db (diff)
downloadpleroma-1ddb55a6d23cb01c2ed4940782fcacd86a56e23f.tar.gz
MatrixController: Keep the transaction id around
Prevents message doubling in element (web)
-rw-r--r--lib/pleroma/application.ex2
-rw-r--r--lib/pleroma/web/matrix_controller.ex19
2 files changed, 15 insertions, 6 deletions
diff --git a/lib/pleroma/application.ex b/lib/pleroma/application.ex
index db493ef22..7f7531195 100644
--- a/lib/pleroma/application.ex
+++ b/lib/pleroma/application.ex
@@ -157,7 +157,7 @@ defmodule Pleroma.Application do
build_cachex("emoji_packs", expiration: emoji_packs_expiration(), limit: 10),
build_cachex("failed_proxy_url", limit: 2500),
build_cachex("banned_urls", default_ttl: :timer.hours(24 * 30), limit: 5_000),
- build_cachex("matrix_compat", default_ttl: :timer.hours(120), limit: 5000)
+ build_cachex("matrix", default_ttl: :timer.hours(120), limit: 5000)
]
end
diff --git a/lib/pleroma/web/matrix_controller.ex b/lib/pleroma/web/matrix_controller.ex
index 8746cc114..cb2e6df73 100644
--- a/lib/pleroma/web/matrix_controller.ex
+++ b/lib/pleroma/web/matrix_controller.ex
@@ -135,7 +135,7 @@ defmodule Pleroma.Web.MatrixController do
def set_filter(conn, params) do
filter_id = :crypto.strong_rand_bytes(32) |> Base.url_encode64(padding: false)
- Cachex.put(:matrix_compat, "filter:#{filter_id}", params)
+ Cachex.put(:matrix_cache, "filter:#{filter_id}", params)
data = %{
filter_id: filter_id
@@ -146,11 +146,11 @@ defmodule Pleroma.Web.MatrixController do
end
def filter(conn, params) do
- result = Cachex.get(:matrix_compat, "filter:#{params["filter_id"]}")
+ {:ok, result} = Cachex.get(:matrix_cache, "filter:#{params["filter_id"]}")
conn
|> put_status(200)
- |> json(result)
+ |> json(result || %{})
end
defp matrix_name(%{local: true, nickname: nick}) do
@@ -209,6 +209,7 @@ defmodule Pleroma.Web.MatrixController do
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 = [
%{
@@ -224,7 +225,8 @@ defmodule Pleroma.Web.MatrixController do
sender: matrix_name(author),
origin_server_ts: date |> DateTime.to_unix(:millisecond),
unsigned: %{
- age: DateTime.diff(DateTime.utc_now(), date, :millisecond)
+ age: DateTime.diff(DateTime.utc_now(), date, :millisecond),
+ transaction_id: txn_id
}
}
]
@@ -504,7 +506,8 @@ defmodule Pleroma.Web.MatrixController do
"msgtype" => "m.text",
"body" => body,
"room_id" => chat_id,
- "event_type" => "m.room.message"
+ "event_type" => "m.room.message",
+ "txn_id" => txn_id
}
) do
with %Chat{user_id: ^user_id, recipient: recipient_id} = chat <- Chat.get_by_id(chat_id),
@@ -513,6 +516,12 @@ defmodule Pleroma.Web.MatrixController do
object = Object.normalize(activity, false)
cmr = MessageReference.for_chat_and_object(chat, object)
+ # Hard to believe, but element (web) does not use the event id to figure out
+ # if an event returned via sync is the same as the event we send off, but
+ # instead it uses this transaction id, so if we don't save this (for a
+ # little while) we get doubled messages in the frontend.
+ Cachex.put(:matrix_cache, "txn_id:#{cmr.id}", txn_id)
+
data = %{
event_id: cmr.id
}