2021-07-14 07:07:08 +00:00
|
|
|
use crate::{database::DatabaseGuard, pdu::PduBuilder, utils, ConduitResult, Error, Ruma};
|
2020-07-30 16:14:47 +00:00
|
|
|
use ruma::{
|
2021-07-20 17:49:47 +00:00
|
|
|
api::{
|
|
|
|
client::{
|
|
|
|
error::ErrorKind,
|
|
|
|
r0::profile::{
|
|
|
|
get_avatar_url, get_display_name, get_profile, set_avatar_url, set_display_name,
|
|
|
|
},
|
2020-07-30 16:14:47 +00:00
|
|
|
},
|
2021-07-20 17:49:47 +00:00
|
|
|
federation::{self, query::get_profile_information::v1::ProfileField},
|
2020-07-30 16:14:47 +00:00
|
|
|
},
|
|
|
|
events::EventType,
|
2020-12-04 23:16:17 +00:00
|
|
|
serde::Raw,
|
2020-07-30 16:14:47 +00:00
|
|
|
};
|
2021-07-13 13:44:25 +00:00
|
|
|
use std::{convert::TryInto, sync::Arc};
|
2020-07-30 16:14:47 +00:00
|
|
|
|
|
|
|
#[cfg(feature = "conduit_bin")]
|
|
|
|
use rocket::{get, put};
|
|
|
|
|
2021-08-31 17:14:37 +00:00
|
|
|
/// # `PUT /_matrix/client/r0/profile/{userId}/displayname`
|
|
|
|
///
|
|
|
|
/// Updates the displayname.
|
|
|
|
///
|
|
|
|
/// - Also makes sure other users receive the update using presence EDUs
|
2020-07-30 16:14:47 +00:00
|
|
|
#[cfg_attr(
|
|
|
|
feature = "conduit_bin",
|
|
|
|
put("/_matrix/client/r0/profile/<_>/displayname", data = "<body>")
|
|
|
|
)]
|
2021-02-28 11:41:03 +00:00
|
|
|
#[tracing::instrument(skip(db, body))]
|
2020-09-14 18:23:19 +00:00
|
|
|
pub async fn set_displayname_route(
|
2021-07-14 07:07:08 +00:00
|
|
|
db: DatabaseGuard,
|
2020-09-08 15:32:03 +00:00
|
|
|
body: Ruma<set_display_name::Request<'_>>,
|
2020-07-30 16:14:47 +00:00
|
|
|
) -> ConduitResult<set_display_name::Response> {
|
2020-10-18 18:33:12 +00:00
|
|
|
let sender_user = body.sender_user.as_ref().expect("user is authenticated");
|
2020-07-30 16:14:47 +00:00
|
|
|
|
|
|
|
db.users
|
2020-10-18 18:33:12 +00:00
|
|
|
.set_displayname(&sender_user, body.displayname.clone())?;
|
2020-07-30 16:14:47 +00:00
|
|
|
|
|
|
|
// Send a new membership event and presence update into all joined rooms
|
2021-08-31 19:20:03 +00:00
|
|
|
let all_rooms_joined: Vec<_> = db
|
|
|
|
.rooms
|
|
|
|
.rooms_joined(&sender_user)
|
2021-04-25 12:10:07 +00:00
|
|
|
.filter_map(|r| r.ok())
|
|
|
|
.map(|room_id| {
|
|
|
|
Ok::<_, Error>((
|
|
|
|
PduBuilder {
|
|
|
|
event_type: EventType::RoomMember,
|
|
|
|
content: serde_json::to_value(ruma::events::room::member::MemberEventContent {
|
|
|
|
displayname: body.displayname.clone(),
|
|
|
|
..serde_json::from_value::<Raw<_>>(
|
|
|
|
db.rooms
|
|
|
|
.room_state_get(
|
|
|
|
&room_id,
|
|
|
|
&EventType::RoomMember,
|
|
|
|
&sender_user.to_string(),
|
|
|
|
)?
|
|
|
|
.ok_or_else(|| {
|
|
|
|
Error::bad_database(
|
2021-04-29 18:16:52 +00:00
|
|
|
"Tried to send displayname update for user not in the \
|
2021-08-31 19:20:03 +00:00
|
|
|
room.",
|
2021-04-29 18:16:52 +00:00
|
|
|
)
|
2021-04-25 12:10:07 +00:00
|
|
|
})?
|
2021-06-30 07:52:01 +00:00
|
|
|
.content
|
|
|
|
.clone(),
|
2021-04-25 12:10:07 +00:00
|
|
|
)
|
|
|
|
.expect("from_value::<Raw<..>> can never fail")
|
|
|
|
.deserialize()
|
|
|
|
.map_err(|_| Error::bad_database("Database contains invalid PDU."))?
|
|
|
|
})
|
|
|
|
.expect("event is valid, we just created it"),
|
|
|
|
unsigned: None,
|
|
|
|
state_key: Some(sender_user.to_string()),
|
|
|
|
redacts: None,
|
|
|
|
},
|
|
|
|
room_id,
|
|
|
|
))
|
|
|
|
})
|
|
|
|
.filter_map(|r| r.ok())
|
2021-08-31 19:20:03 +00:00
|
|
|
.collect();
|
|
|
|
|
|
|
|
for (pdu_builder, room_id) in all_rooms_joined {
|
2021-08-03 09:10:58 +00:00
|
|
|
let mutex_state = Arc::clone(
|
2021-07-13 13:44:25 +00:00
|
|
|
db.globals
|
2021-08-03 09:10:58 +00:00
|
|
|
.roomid_mutex_state
|
2021-07-13 13:44:25 +00:00
|
|
|
.write()
|
|
|
|
.unwrap()
|
|
|
|
.entry(room_id.clone())
|
|
|
|
.or_default(),
|
|
|
|
);
|
2021-08-03 09:10:58 +00:00
|
|
|
let state_lock = mutex_state.lock().await;
|
2021-07-13 13:44:25 +00:00
|
|
|
|
|
|
|
let _ =
|
|
|
|
db.rooms
|
2021-08-03 09:10:58 +00:00
|
|
|
.build_and_append_pdu(pdu_builder, &sender_user, &room_id, &db, &state_lock);
|
2020-07-30 16:14:47 +00:00
|
|
|
|
|
|
|
// Presence update
|
|
|
|
db.rooms.edus.update_presence(
|
2020-10-18 18:33:12 +00:00
|
|
|
&sender_user,
|
2020-07-30 16:14:47 +00:00
|
|
|
&room_id,
|
|
|
|
ruma::events::presence::PresenceEvent {
|
|
|
|
content: ruma::events::presence::PresenceEventContent {
|
2020-10-18 18:33:12 +00:00
|
|
|
avatar_url: db.users.avatar_url(&sender_user)?,
|
2020-07-30 16:14:47 +00:00
|
|
|
currently_active: None,
|
2020-10-18 18:33:12 +00:00
|
|
|
displayname: db.users.displayname(&sender_user)?,
|
2020-07-30 16:14:47 +00:00
|
|
|
last_active_ago: Some(
|
|
|
|
utils::millis_since_unix_epoch()
|
|
|
|
.try_into()
|
|
|
|
.expect("time is valid"),
|
|
|
|
),
|
|
|
|
presence: ruma::presence::PresenceState::Online,
|
|
|
|
status_msg: None,
|
|
|
|
},
|
2020-10-18 18:33:12 +00:00
|
|
|
sender: sender_user.clone(),
|
2020-07-30 16:14:47 +00:00
|
|
|
},
|
|
|
|
&db.globals,
|
|
|
|
)?;
|
|
|
|
}
|
|
|
|
|
2021-08-02 08:13:34 +00:00
|
|
|
db.flush()?;
|
2020-10-21 19:28:02 +00:00
|
|
|
|
2021-07-15 21:17:58 +00:00
|
|
|
Ok(set_display_name::Response {}.into())
|
2020-07-30 16:14:47 +00:00
|
|
|
}
|
|
|
|
|
2021-08-31 17:14:37 +00:00
|
|
|
/// # `GET /_matrix/client/r0/profile/{userId}/displayname`
|
|
|
|
///
|
|
|
|
/// Returns the displayname of the user.
|
|
|
|
///
|
|
|
|
/// - If user is on another server: Fetches displayname over federation
|
2020-07-30 16:14:47 +00:00
|
|
|
#[cfg_attr(
|
|
|
|
feature = "conduit_bin",
|
|
|
|
get("/_matrix/client/r0/profile/<_>/displayname", data = "<body>")
|
|
|
|
)]
|
2021-02-28 11:41:03 +00:00
|
|
|
#[tracing::instrument(skip(db, body))]
|
2020-10-21 19:28:02 +00:00
|
|
|
pub async fn get_displayname_route(
|
2021-07-14 07:07:08 +00:00
|
|
|
db: DatabaseGuard,
|
2020-09-08 15:32:03 +00:00
|
|
|
body: Ruma<get_display_name::Request<'_>>,
|
2020-07-30 16:14:47 +00:00
|
|
|
) -> ConduitResult<get_display_name::Response> {
|
2021-07-20 17:49:47 +00:00
|
|
|
if body.user_id.server_name() != db.globals.server_name() {
|
|
|
|
let response = db
|
|
|
|
.sending
|
|
|
|
.send_federation_request(
|
|
|
|
&db.globals,
|
|
|
|
body.user_id.server_name(),
|
|
|
|
federation::query::get_profile_information::v1::Request {
|
|
|
|
user_id: &body.user_id,
|
|
|
|
field: Some(&ProfileField::DisplayName),
|
|
|
|
},
|
|
|
|
)
|
|
|
|
.await?;
|
|
|
|
|
|
|
|
return Ok(get_display_name::Response {
|
|
|
|
displayname: response.displayname,
|
|
|
|
}
|
|
|
|
.into());
|
|
|
|
}
|
|
|
|
|
2020-07-30 16:14:47 +00:00
|
|
|
Ok(get_display_name::Response {
|
|
|
|
displayname: db.users.displayname(&body.user_id)?,
|
|
|
|
}
|
|
|
|
.into())
|
|
|
|
}
|
|
|
|
|
2021-08-31 17:14:37 +00:00
|
|
|
/// # `PUT /_matrix/client/r0/profile/{userId}/avatar_url`
|
|
|
|
///
|
|
|
|
/// Updates the avatar_url and blurhash.
|
|
|
|
///
|
|
|
|
/// - Also makes sure other users receive the update using presence EDUs
|
2020-07-30 16:14:47 +00:00
|
|
|
#[cfg_attr(
|
|
|
|
feature = "conduit_bin",
|
|
|
|
put("/_matrix/client/r0/profile/<_>/avatar_url", data = "<body>")
|
|
|
|
)]
|
2021-02-28 11:41:03 +00:00
|
|
|
#[tracing::instrument(skip(db, body))]
|
2020-09-14 18:23:19 +00:00
|
|
|
pub async fn set_avatar_url_route(
|
2021-07-14 07:07:08 +00:00
|
|
|
db: DatabaseGuard,
|
2020-09-08 15:32:03 +00:00
|
|
|
body: Ruma<set_avatar_url::Request<'_>>,
|
2020-07-30 16:14:47 +00:00
|
|
|
) -> ConduitResult<set_avatar_url::Response> {
|
2020-10-18 18:33:12 +00:00
|
|
|
let sender_user = body.sender_user.as_ref().expect("user is authenticated");
|
2020-07-30 16:14:47 +00:00
|
|
|
|
|
|
|
db.users
|
2020-10-18 18:33:12 +00:00
|
|
|
.set_avatar_url(&sender_user, body.avatar_url.clone())?;
|
2020-07-30 16:14:47 +00:00
|
|
|
|
2021-07-15 21:17:58 +00:00
|
|
|
db.users.set_blurhash(&sender_user, body.blurhash.clone())?;
|
|
|
|
|
2020-07-30 16:14:47 +00:00
|
|
|
// Send a new membership event and presence update into all joined rooms
|
2021-08-31 19:20:03 +00:00
|
|
|
let all_joined_rooms: Vec<_> = db
|
|
|
|
.rooms
|
|
|
|
.rooms_joined(&sender_user)
|
2021-04-25 12:10:07 +00:00
|
|
|
.filter_map(|r| r.ok())
|
|
|
|
.map(|room_id| {
|
|
|
|
Ok::<_, Error>((
|
|
|
|
PduBuilder {
|
|
|
|
event_type: EventType::RoomMember,
|
|
|
|
content: serde_json::to_value(ruma::events::room::member::MemberEventContent {
|
|
|
|
avatar_url: body.avatar_url.clone(),
|
|
|
|
..serde_json::from_value::<Raw<_>>(
|
|
|
|
db.rooms
|
|
|
|
.room_state_get(
|
|
|
|
&room_id,
|
|
|
|
&EventType::RoomMember,
|
|
|
|
&sender_user.to_string(),
|
|
|
|
)?
|
|
|
|
.ok_or_else(|| {
|
|
|
|
Error::bad_database(
|
2021-04-29 18:16:52 +00:00
|
|
|
"Tried to send displayname update for user not in the \
|
2021-08-31 19:20:03 +00:00
|
|
|
room.",
|
2021-04-29 18:16:52 +00:00
|
|
|
)
|
2021-04-25 12:10:07 +00:00
|
|
|
})?
|
2021-06-30 07:52:01 +00:00
|
|
|
.content
|
|
|
|
.clone(),
|
2021-04-25 12:10:07 +00:00
|
|
|
)
|
|
|
|
.expect("from_value::<Raw<..>> can never fail")
|
|
|
|
.deserialize()
|
|
|
|
.map_err(|_| Error::bad_database("Database contains invalid PDU."))?
|
|
|
|
})
|
|
|
|
.expect("event is valid, we just created it"),
|
|
|
|
unsigned: None,
|
|
|
|
state_key: Some(sender_user.to_string()),
|
|
|
|
redacts: None,
|
|
|
|
},
|
|
|
|
room_id,
|
|
|
|
))
|
|
|
|
})
|
|
|
|
.filter_map(|r| r.ok())
|
2021-08-31 19:20:03 +00:00
|
|
|
.collect();
|
|
|
|
|
|
|
|
for (pdu_builder, room_id) in all_joined_rooms {
|
2021-08-03 09:10:58 +00:00
|
|
|
let mutex_state = Arc::clone(
|
2021-07-13 13:44:25 +00:00
|
|
|
db.globals
|
2021-08-03 09:10:58 +00:00
|
|
|
.roomid_mutex_state
|
2021-07-13 13:44:25 +00:00
|
|
|
.write()
|
|
|
|
.unwrap()
|
|
|
|
.entry(room_id.clone())
|
|
|
|
.or_default(),
|
|
|
|
);
|
2021-08-03 09:10:58 +00:00
|
|
|
let state_lock = mutex_state.lock().await;
|
2021-07-13 13:44:25 +00:00
|
|
|
|
|
|
|
let _ =
|
|
|
|
db.rooms
|
2021-08-03 09:10:58 +00:00
|
|
|
.build_and_append_pdu(pdu_builder, &sender_user, &room_id, &db, &state_lock);
|
2020-07-30 16:14:47 +00:00
|
|
|
|
|
|
|
// Presence update
|
|
|
|
db.rooms.edus.update_presence(
|
2020-10-18 18:33:12 +00:00
|
|
|
&sender_user,
|
2020-07-30 16:14:47 +00:00
|
|
|
&room_id,
|
|
|
|
ruma::events::presence::PresenceEvent {
|
|
|
|
content: ruma::events::presence::PresenceEventContent {
|
2020-10-18 18:33:12 +00:00
|
|
|
avatar_url: db.users.avatar_url(&sender_user)?,
|
2020-07-30 16:14:47 +00:00
|
|
|
currently_active: None,
|
2020-10-18 18:33:12 +00:00
|
|
|
displayname: db.users.displayname(&sender_user)?,
|
2020-07-30 16:14:47 +00:00
|
|
|
last_active_ago: Some(
|
|
|
|
utils::millis_since_unix_epoch()
|
|
|
|
.try_into()
|
|
|
|
.expect("time is valid"),
|
|
|
|
),
|
|
|
|
presence: ruma::presence::PresenceState::Online,
|
|
|
|
status_msg: None,
|
|
|
|
},
|
2020-10-18 18:33:12 +00:00
|
|
|
sender: sender_user.clone(),
|
2020-07-30 16:14:47 +00:00
|
|
|
},
|
|
|
|
&db.globals,
|
|
|
|
)?;
|
|
|
|
}
|
|
|
|
|
2021-08-02 08:13:34 +00:00
|
|
|
db.flush()?;
|
2020-10-21 19:28:02 +00:00
|
|
|
|
2021-07-15 21:17:58 +00:00
|
|
|
Ok(set_avatar_url::Response {}.into())
|
2020-07-30 16:14:47 +00:00
|
|
|
}
|
|
|
|
|
2021-08-31 17:14:37 +00:00
|
|
|
/// # `GET /_matrix/client/r0/profile/{userId}/avatar_url`
|
|
|
|
///
|
|
|
|
/// Returns the avatar_url and blurhash of the user.
|
|
|
|
///
|
|
|
|
/// - If user is on another server: Fetches avatar_url and blurhash over federation
|
2020-07-30 16:14:47 +00:00
|
|
|
#[cfg_attr(
|
|
|
|
feature = "conduit_bin",
|
|
|
|
get("/_matrix/client/r0/profile/<_>/avatar_url", data = "<body>")
|
|
|
|
)]
|
2021-02-28 11:41:03 +00:00
|
|
|
#[tracing::instrument(skip(db, body))]
|
2020-10-21 19:28:02 +00:00
|
|
|
pub async fn get_avatar_url_route(
|
2021-07-14 07:07:08 +00:00
|
|
|
db: DatabaseGuard,
|
2020-09-08 15:32:03 +00:00
|
|
|
body: Ruma<get_avatar_url::Request<'_>>,
|
2020-07-30 16:14:47 +00:00
|
|
|
) -> ConduitResult<get_avatar_url::Response> {
|
2021-07-20 17:49:47 +00:00
|
|
|
if body.user_id.server_name() != db.globals.server_name() {
|
|
|
|
let response = db
|
|
|
|
.sending
|
|
|
|
.send_federation_request(
|
|
|
|
&db.globals,
|
|
|
|
body.user_id.server_name(),
|
|
|
|
federation::query::get_profile_information::v1::Request {
|
|
|
|
user_id: &body.user_id,
|
|
|
|
field: Some(&ProfileField::AvatarUrl),
|
|
|
|
},
|
|
|
|
)
|
|
|
|
.await?;
|
|
|
|
|
|
|
|
return Ok(get_avatar_url::Response {
|
|
|
|
avatar_url: response.avatar_url,
|
|
|
|
blurhash: response.blurhash,
|
|
|
|
}
|
|
|
|
.into());
|
|
|
|
}
|
|
|
|
|
2020-07-30 16:14:47 +00:00
|
|
|
Ok(get_avatar_url::Response {
|
|
|
|
avatar_url: db.users.avatar_url(&body.user_id)?,
|
2021-07-15 21:17:58 +00:00
|
|
|
blurhash: db.users.blurhash(&body.user_id)?,
|
2020-07-30 16:14:47 +00:00
|
|
|
}
|
|
|
|
.into())
|
|
|
|
}
|
|
|
|
|
2021-08-31 17:14:37 +00:00
|
|
|
/// # `GET /_matrix/client/r0/profile/{userId}`
|
|
|
|
///
|
|
|
|
/// Returns the displayname, avatar_url and blurhash of the user.
|
|
|
|
///
|
|
|
|
/// - If user is on another server: Fetches profile over federation
|
2020-07-30 16:14:47 +00:00
|
|
|
#[cfg_attr(
|
|
|
|
feature = "conduit_bin",
|
|
|
|
get("/_matrix/client/r0/profile/<_>", data = "<body>")
|
|
|
|
)]
|
2021-02-28 11:41:03 +00:00
|
|
|
#[tracing::instrument(skip(db, body))]
|
2020-10-21 19:28:02 +00:00
|
|
|
pub async fn get_profile_route(
|
2021-07-14 07:07:08 +00:00
|
|
|
db: DatabaseGuard,
|
2020-09-08 15:32:03 +00:00
|
|
|
body: Ruma<get_profile::Request<'_>>,
|
2020-07-30 16:14:47 +00:00
|
|
|
) -> ConduitResult<get_profile::Response> {
|
2021-07-20 17:49:47 +00:00
|
|
|
if body.user_id.server_name() != db.globals.server_name() {
|
|
|
|
let response = db
|
|
|
|
.sending
|
|
|
|
.send_federation_request(
|
|
|
|
&db.globals,
|
|
|
|
body.user_id.server_name(),
|
|
|
|
federation::query::get_profile_information::v1::Request {
|
|
|
|
user_id: &body.user_id,
|
|
|
|
field: None,
|
|
|
|
},
|
|
|
|
)
|
|
|
|
.await?;
|
|
|
|
|
|
|
|
return Ok(get_profile::Response {
|
|
|
|
displayname: response.displayname,
|
|
|
|
avatar_url: response.avatar_url,
|
|
|
|
blurhash: response.blurhash,
|
|
|
|
}
|
|
|
|
.into());
|
|
|
|
}
|
|
|
|
|
2020-08-31 11:23:39 +00:00
|
|
|
if !db.users.exists(&body.user_id)? {
|
|
|
|
// Return 404 if this user doesn't exist
|
2020-07-30 16:14:47 +00:00
|
|
|
return Err(Error::BadRequest(
|
|
|
|
ErrorKind::NotFound,
|
|
|
|
"Profile was not found.",
|
|
|
|
));
|
|
|
|
}
|
|
|
|
|
|
|
|
Ok(get_profile::Response {
|
2020-08-31 11:23:39 +00:00
|
|
|
avatar_url: db.users.avatar_url(&body.user_id)?,
|
2021-07-15 21:17:58 +00:00
|
|
|
blurhash: db.users.blurhash(&body.user_id)?,
|
2020-08-31 11:23:39 +00:00
|
|
|
displayname: db.users.displayname(&body.user_id)?,
|
2020-07-30 16:14:47 +00:00
|
|
|
}
|
|
|
|
.into())
|
|
|
|
}
|