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
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
// Copyright 2023, 2024 The Matrix.org Foundation C.I.C.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//     http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

use std::collections::HashSet;

use anyhow::Context;
use apalis_core::{context::JobContext, executor::TokioExecutor, monitor::Monitor};
use mas_data_model::Device;
use mas_matrix::ProvisionRequest;
use mas_storage::{
    compat::CompatSessionFilter,
    job::{
        DeleteDeviceJob, JobRepositoryExt as _, JobWithSpanContext, ProvisionDeviceJob,
        ProvisionUserJob, SyncDevicesJob,
    },
    oauth2::OAuth2SessionFilter,
    user::{UserEmailRepository, UserRepository},
    Pagination, RepositoryAccess,
};
use tracing::info;

use crate::{storage::PostgresStorageFactory, JobContextExt, State};

/// Job to provision a user on the Matrix homeserver.
/// This works by doing a PUT request to the /_synapse/admin/v2/users/{user_id}
/// endpoint.
#[tracing::instrument(
    name = "job.provision_user"
    fields(user.id = %job.user_id()),
    skip_all,
    err(Debug),
)]
async fn provision_user(
    job: JobWithSpanContext<ProvisionUserJob>,
    ctx: JobContext,
) -> Result<(), anyhow::Error> {
    let state = ctx.state();
    let matrix = state.matrix_connection();
    let mut repo = state.repository().await?;

    let user = repo
        .user()
        .lookup(job.user_id())
        .await?
        .context("User not found")?;

    let mxid = matrix.mxid(&user.username);
    let emails = repo
        .user_email()
        .all(&user)
        .await?
        .into_iter()
        .filter(|email| email.confirmed_at.is_some())
        .map(|email| email.email)
        .collect();
    let mut request = ProvisionRequest::new(mxid.clone(), user.sub.clone()).set_emails(emails);

    if let Some(display_name) = job.display_name_to_set() {
        request = request.set_displayname(display_name.to_owned());
    }

    let created = matrix.provision_user(&request).await?;

    if created {
        info!(%user.id, %mxid, "User created");
    } else {
        info!(%user.id, %mxid, "User updated");
    }

    // Schedule a device sync job
    let sync_device_job = SyncDevicesJob::new(&user);
    repo.job().schedule_job(sync_device_job).await?;

    repo.save().await?;

    Ok(())
}

/// Job to provision a device on the Matrix homeserver.
///
/// This job is deprecated and therefore just schedules a [`SyncDevicesJob`]
#[tracing::instrument(
    name = "job.provision_device"
    fields(
        user.id = %job.user_id(),
        device.id = %job.device_id(),
    ),
    skip_all,
    err(Debug),
)]
async fn provision_device(
    job: JobWithSpanContext<ProvisionDeviceJob>,
    ctx: JobContext,
) -> Result<(), anyhow::Error> {
    let state = ctx.state();
    let mut repo = state.repository().await?;

    let user = repo
        .user()
        .lookup(job.user_id())
        .await?
        .context("User not found")?;

    // Schedule a device sync job
    repo.job().schedule_job(SyncDevicesJob::new(&user)).await?;

    Ok(())
}

/// Job to delete a device from a user's account.
///
/// This job is deprecated and therefore just schedules a [`SyncDevicesJob`]
#[tracing::instrument(
    name = "job.delete_device"
    fields(
        user.id = %job.user_id(),
        device.id = %job.device_id(),
    ),
    skip_all,
    err(Debug),
)]
async fn delete_device(
    job: JobWithSpanContext<DeleteDeviceJob>,
    ctx: JobContext,
) -> Result<(), anyhow::Error> {
    let state = ctx.state();
    let mut repo = state.repository().await?;

    let user = repo
        .user()
        .lookup(job.user_id())
        .await?
        .context("User not found")?;

    // Schedule a device sync job
    repo.job().schedule_job(SyncDevicesJob::new(&user)).await?;

    Ok(())
}

/// Job to sync the list of devices of a user with the homeserver.
#[tracing::instrument(
    name = "job.sync_devices",
    fields(user.id = %job.user_id()),
    skip_all,
    err(Debug),
)]
async fn sync_devices(
    job: JobWithSpanContext<SyncDevicesJob>,
    ctx: JobContext,
) -> Result<(), anyhow::Error> {
    let state = ctx.state();
    let matrix = state.matrix_connection();
    let mut repo = state.repository().await?;

    let user = repo
        .user()
        .lookup(job.user_id())
        .await?
        .context("User not found")?;

    // Lock the user sync to make sure we don't get into a race condition
    repo.user().acquire_lock_for_sync(&user).await?;

    let mut devices = HashSet::new();

    // Cycle through all the compat sessions of the user, and grab the devices
    let mut cursor = Pagination::first(100);
    loop {
        let page = repo
            .compat_session()
            .list(
                CompatSessionFilter::new().for_user(&user).active_only(),
                cursor,
            )
            .await?;

        for (compat_session, _) in page.edges {
            devices.insert(compat_session.device.as_str().to_owned());
            cursor = cursor.after(compat_session.id);
        }

        if !page.has_next_page {
            break;
        }
    }

    // Cycle though all the oauth2 sessions of the user, and grab the devices
    let mut cursor = Pagination::first(100);
    loop {
        let page = repo
            .oauth2_session()
            .list(
                OAuth2SessionFilter::new().for_user(&user).active_only(),
                cursor,
            )
            .await?;

        for oauth2_session in page.edges {
            for scope in &*oauth2_session.scope {
                if let Some(device) = Device::from_scope_token(scope) {
                    devices.insert(device.as_str().to_owned());
                }
            }

            cursor = cursor.after(oauth2_session.id);
        }

        if !page.has_next_page {
            break;
        }
    }

    let mxid = matrix.mxid(&user.username);
    matrix.sync_devices(&mxid, devices).await?;

    // We kept the connection until now, so that we still hold the lock on the user
    // throughout the sync
    repo.save().await?;

    Ok(())
}

pub(crate) fn register(
    suffix: &str,
    monitor: Monitor<TokioExecutor>,
    state: &State,
    storage_factory: &PostgresStorageFactory,
) -> Monitor<TokioExecutor> {
    let provision_user_worker =
        crate::build!(ProvisionUserJob => provision_user, suffix, state, storage_factory);
    let provision_device_worker =
        crate::build!(ProvisionDeviceJob => provision_device, suffix, state, storage_factory);
    let delete_device_worker =
        crate::build!(DeleteDeviceJob => delete_device, suffix, state, storage_factory);
    let sync_devices_worker =
        crate::build!(SyncDevicesJob => sync_devices, suffix, state, storage_factory);

    monitor
        .register(provision_user_worker)
        .register(provision_device_worker)
        .register(delete_device_worker)
        .register(sync_devices_worker)
}