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
|
defmodule MatrixAppService do
@moduledoc """
Library that adds Matrix Application Service functionalities to Phoenix
applications.
## Installation
Add this library to your dependencies in `mix.exs`
```
defp deps do
# ...,
{:matrix_app_service, "~> 0.1.0"}
end
```
## Usage
### Write adapters
Create one or multiple modules that implement the following modules:
`MatrixAppService.Adapter.Room`, `MatrixAppService.Adapter.User`,
`MatrixAppService.Adapter.Transaction`.
For instance:
```
@behaviour MatrixAppService.Adapter.Room
@impl MatrixAppService.Adapter.Room
def query_alias(room_alias) do
# Do something with the room alias
# If the room exists, return :ok
end
@behaviour MatrixAppService.Adapter.User
@impl MatrixAppService.Adapter.User
def query_user(user_id) do
# Do something with the user ID
# If the user exists, return :ok
end
@behaviour MatrixAppService.Adapter.Transaction
@impl MatrixAppService.Adapter.Transaction
def new_event(%MatrixAppService.Event{type: type, content: content}) do
# Do something with the event
end
```
### Configure
#### Option 1: Using the :matrix_app_service supervision tree
Configuration:
```
config :matrix_app_service,
internal_supervisor: true,
transaction_adapter: App.Matrix.Transaction,
room_adapter: App.Matrix.Room,
user_adapter: App.Matrix.User,
path: "/matrix"
base_url: "http://synapse:8008",
access_token: "access token",
homeserver_token: "homeserver token"
```
#### Option 2: Using the :matrix_app_service endpoint in your own supervision tree
In your application module:
```
children = [
# ...,
{MatrixAppServiceWeb.Endpoint, app_service_config()}
]
# ...
defp app_service_config(), do: Application.get_env(:app, :app_service)
```
Configuration:
```
config :app, :app_service,
transaction_adapter: App.Matrix.Transaction,
room_adapter: App.Matrix.Room,
user_adapter: App.Matrix.User,
path: "/matrix"
base_url: "http://synapse:8008",
access_token: "access token",
homeserver_token: "homeserver token"
```
#### Option 3: Using your own endpoint
In your Phoenix Router:
```
use MatrixAppServiceWeb.Routes
MatrixAppServiceWeb.Routes.routes(Application.get_env(:app, :app_service))
```
Configuration:
```
config :app, :app_service,
transaction_adapter: App.Matrix.Transaction,
room_adapter: App.Matrix.Room,
user_adapter: App.Matrix.User,
path: "/matrix"
base_url: "http://synapse:8008",
access_token: "access token",
homeserver_token: "homeserver token"
```
### Use the Client-Server API
You can use the functions in `MatrixAppService.Client`
"""
end
|