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
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
|
defmodule MatrixAppService.Client do
@moduledoc """
Convenience wrapper around `Polyjuice.Client.LowLevel`.
Library users can use the wrapped functions or call `user/1` and pass the
returned struct to Polyjuice functions.
"""
@behaviour MatrixAppService.ClientBehaviour
@type create_opts :: [
access_token: String.t() | nil,
user_id: String.t() | nil,
device_id: String.t() | nil,
storage: Polyjuice.Client.Storage.t() | nil
]
@type client_options :: {:base_url, String.t()} | create_opts()
@doc """
Returns a client for the application service.
By default, the client gets its homeserver URL and access token from configuration.
Different options can be provided to override the defaults, those are:
* `:base_url`: homerver URL
* `:acces_token`: access token
* `:device_id`: device ID
* `:user_id`: user ID
* `:storage`: a `t:Polyjuice.Client.Storage.t/0`
"""
@impl true
def client(opts \\ []) do
base_url =
Keyword.get(opts, :base_url) ||
(MatrixAppService.Application.start_endpoint?() &&
MatrixAppServiceWeb.Endpoint.config(:base_url)) ||
Application.get_env(:matrix_app_service, :app_service)[:base_url] ||
raise "MatrixAppService: config key base_url missing"
access_token =
Keyword.get(opts, :access_token) ||
(MatrixAppService.Application.start_endpoint?() &&
MatrixAppServiceWeb.Endpoint.config(:access_token)) ||
Application.get_env(:matrix_app_service, :app_service)[:access_token] ||
raise "MatrixAppService: config key access_token missing"
default_opts = [
access_token: access_token,
device_id: "APP_SERVICE",
application_service: true
]
opts = Keyword.merge(default_opts, opts)
Polyjuice.Client.LowLevel.create(base_url, opts)
end
@doc """
Creates a Matrix room.
Arguments:
1. `options`: see `Polyjuice.Client.Room.create_room/2`
2. `client_options`: see `client/1`
"""
@impl true
def create_room(options \\ [], client_options \\ []) do
client(client_options)
|> Polyjuice.Client.Room.create_room(options)
end
@doc """
Creates a new alias for a Matrix room.
Arguments:
1. `room_id`: room ID
2. `room_alias`: room alias
3. `client_options`: see `client/1`
"""
@impl true
def create_alias(room_id, room_alias, client_options \\ []) do
client(client_options)
|> Polyjuice.Client.Room.create_alias(room_id, room_alias)
end
@doc """
Sends a message to a Matrix room.
Arguments:
1. `room_id`: room ID
2. `msg`: see `Polyjuice.Client.Room.send_message/3`
3. `client_options`: see `client/1`
"""
@impl true
def send_message(room_id, msg, client_options \\ []) do
client(client_options)
|> Polyjuice.Client.Room.send_message(room_id, msg)
end
@doc """
Registers a new Matrix user.
Arguments:
1. `opts`: a keyword list that can contain these keys:
* `:inhibit_login`: true
* `:device_id`: device ID, defaults to `"APP_SERVICE"`
* `:initial_device_display_name`: device name, defaults to
`"ApplicationService"`
* `:kind`: kind of account to register, defaults to `"user"`, can also be
`"guest"`
2. `client_options`: see `client/1`
"""
@impl true
def register(opts \\ [], client_options \\ []) do
default_opts = [
inhibit_login: true,
device_id: "APP_SERVICE",
initial_device_display_name: "Application Service"
]
opts = Keyword.merge(default_opts, opts)
client(client_options)
|> Polyjuice.Client.LowLevel.register(opts)
end
defdelegate get_data(client, id, key), to: Polyjuice.Client.Account
defdelegate get_profile(client, id), to: Polyjuice.Client.Profile
defdelegate put_displayname(client, id, displayname), to: Polyjuice.Client.Profile
defdelegate put_avatar_url(client, id, avatar_url), to: Polyjuice.Client.Profile
defdelegate join(client, room_id), to: Polyjuice.Client.Room
defdelegate upload(client, data, opts), to: Polyjuice.Client.Media
end
|