summaryrefslogtreecommitdiff
path: root/lib/lsg_matrix/matrix.ex
diff options
context:
space:
mode:
Diffstat (limited to 'lib/lsg_matrix/matrix.ex')
-rw-r--r--lib/lsg_matrix/matrix.ex19
1 files changed, 15 insertions, 4 deletions
diff --git a/lib/lsg_matrix/matrix.ex b/lib/lsg_matrix/matrix.ex
index bea6d8b..49da6b2 100644
--- a/lib/lsg_matrix/matrix.ex
+++ b/lib/lsg_matrix/matrix.ex
@@ -5,6 +5,7 @@ defmodule LSG.Matrix do
@behaviour MatrixAppService.Adapter.Room
@behaviour MatrixAppService.Adapter.Transaction
@behaviour MatrixAppService.Adapter.User
+ @env Mix.env
def dets(part) do
(LSG.data_path() <> "/matrix-#{to_string(part)}.dets") |> String.to_charlist()
@@ -18,9 +19,16 @@ defmodule LSG.Matrix do
end
def myself?("@_dev:random.sh"), do: true
+ def myself?("@_bot:random.sh"), do: true
def myself?("@_dev."<>_), do: true
+ def myself?("@_bot."<>_), do: true
def myself?(_), do: false
+ def mxc_to_http(mxc = "mxc://"<>_) do
+ uri = URI.parse(mxc)
+ %URI{uri | scheme: "https", path: "/_matrix/media/r0/download/#{uri.authority}#{uri.path}"}
+ |> URI.to_string()
+ end
def get_or_create_matrix_user(id) do
if mxid = lookup_user(id) do
@@ -31,7 +39,7 @@ defmodule LSG.Matrix do
inhibit_login: true,
device_id: "APP_SERVICE",
initial_device_display_name: "Application Service",
- username: "_dev.#{id}"
+ username: if(@env == :dev, do: "_dev.#{id}", else: "_bot.#{id}")
]
Logger.debug("Registering user for #{id}")
{:ok, %{"user_id" => mxid}} = Polyjuice.Client.LowLevel.register(client(), opts)
@@ -89,7 +97,7 @@ defmodule LSG.Matrix do
localpart = localpart(room_alias)
with {:ok, network, channel} <- extract_network_channel_from_localpart(localpart),
%IRC.Connection{} <- IRC.Connection.get_network(network, channel),
- room = [visibility: :public, room_alias_name: localpart, name: "#{network}/#{channel}"],
+ room = [visibility: :public, room_alias_name: localpart, name: if(network == "random", do: channel, else: "#{network}/#{channel}")],
{:ok, %{"room_id" => room_id}} <- Client.Room.create_room(client(), room) do
Logger.info("Matrix: created room #{room_alias} #{room_id}")
:dets.insert(dets(:rooms), {room_id, network, channel, %{}})
@@ -109,10 +117,11 @@ defmodule LSG.Matrix do
def extract_network_channel_from_localpart(localpart) do
s = localpart
|> String.replace("dev.", "")
- |> String.split("_", parts: 2)
+ |> String.split("/", parts: 2)
case s do
[network, channel] -> {:ok, network, channel}
+ [channel] -> {:ok, "random", channel}
_ -> {:error, :invalid_localpart}
end
end
@@ -130,7 +139,9 @@ defmodule LSG.Matrix do
@impl MatrixAppService.Adapter.Transaction
def new_event(event = %MatrixAppService.Event{}) do
Logger.debug("New matrix event: #{inspect event}")
- if room_id = event.room_id, do: LSG.Matrix.Room.start_and_send_matrix_event(room_id, event)
+ if event.room_id do
+ LSG.Matrix.Room.start_and_send_matrix_event(event.room_id, event)
+ end
:noop
end