summaryrefslogtreecommitdiff
path: root/lib/matrix_app_service/client.ex
blob: 5cb720de91f475a80d25c4b4b7034e1b754ec67d (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
defmodule MatrixAppService.Client do
  @type client_options :: [user_id: String.t(), storage: Polyjuice.Client.Storage.t()]

  @spec client(String.t() | nil, Polyjuice.Client.Storage.t() | nil) ::
          Polyjuice.Client.LowLevel.t()
  def client(user_id \\ nil, storage \\ nil) do
    base_url = Application.get_env(:matrix_app_service, :base_url)
    access_token = Application.get_env(:matrix_app_service, :access_token)

    Polyjuice.Client.LowLevel.create(base_url,
      access_token: access_token,
      # "@alice:matrix.imago.local",
      user_id: user_id,
      device_id: "Application service",
      storage: storage
    )
  end

  @spec create_room(Keyword.t()) :: {:ok, String.t()} | Any
  def create_room(options) do
    client_with_options(options)
    |> Polyjuice.Client.Room.create_room(options)
  end

  @spec create_alias(String.t(), String.t(), client_options()) :: {:ok, String.t()} | Any
  def create_alias(room_id, room_alias, options \\ []) do
    client_with_options(options)
    |> Polyjuice.Client.Room.create_alias(room_id, room_alias)
  end

  @spec client(client_options(), Polyjuice.Client.Storage.t()) :: Polyjuice.Client.LowLevel.t()
  defp client_with_options(options) do
    user_id = Keyword.get(options, :user_id, nil)
    storage = Keyword.get(options, :storage, nil)
    client(user_id, storage)
  end
end