2022-10-05 15:33:57 +02:00
|
|
|
use std::sync::Arc;
|
|
|
|
|
2022-09-06 23:15:09 +02:00
|
|
|
use ruma::{UserId, RoomId};
|
|
|
|
|
2022-10-05 12:45:54 +02:00
|
|
|
use crate::{service, database::KeyValueDatabase, utils, Error, Result, services};
|
2022-09-06 23:15:09 +02:00
|
|
|
|
2022-10-05 15:33:57 +02:00
|
|
|
impl service::rooms::user::Data for Arc<KeyValueDatabase> {
|
2022-08-14 13:38:21 +02:00
|
|
|
fn reset_notification_counts(&self, user_id: &UserId, room_id: &RoomId) -> Result<()> {
|
2021-04-12 12:40:16 +02:00
|
|
|
let mut userroom_id = user_id.as_bytes().to_vec();
|
|
|
|
userroom_id.push(0xff);
|
|
|
|
userroom_id.extend_from_slice(room_id.as_bytes());
|
|
|
|
|
2022-06-19 22:56:14 +02:00
|
|
|
self.userroomid_notificationcount
|
|
|
|
.insert(&userroom_id, &0_u64.to_be_bytes())?;
|
2021-04-12 12:40:16 +02:00
|
|
|
self.userroomid_highlightcount
|
2022-06-19 22:56:14 +02:00
|
|
|
.insert(&userroom_id, &0_u64.to_be_bytes())?;
|
2021-04-13 15:00:45 +02:00
|
|
|
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
2022-08-14 13:38:21 +02:00
|
|
|
fn notification_count(&self, user_id: &UserId, room_id: &RoomId) -> Result<u64> {
|
2020-08-26 11:15:52 -04:00
|
|
|
let mut userroom_id = user_id.as_bytes().to_vec();
|
2020-05-03 17:25:31 +02:00
|
|
|
userroom_id.push(0xff);
|
2020-08-26 11:15:52 -04:00
|
|
|
userroom_id.extend_from_slice(room_id.as_bytes());
|
2020-05-03 17:25:31 +02:00
|
|
|
|
2022-06-19 22:56:14 +02:00
|
|
|
self.userroomid_notificationcount
|
|
|
|
.get(&userroom_id)?
|
2021-10-13 11:51:30 +02:00
|
|
|
.map(|bytes| {
|
2022-06-19 22:56:14 +02:00
|
|
|
utils::u64_from_bytes(&bytes)
|
|
|
|
.map_err(|_| Error::bad_database("Invalid notification count in db."))
|
2020-05-25 23:24:13 +02:00
|
|
|
})
|
2022-06-19 22:56:14 +02:00
|
|
|
.unwrap_or(Ok(0))
|
2020-05-25 23:24:13 +02:00
|
|
|
}
|
|
|
|
|
2022-08-14 13:38:21 +02:00
|
|
|
fn highlight_count(&self, user_id: &UserId, room_id: &RoomId) -> Result<u64> {
|
2022-06-19 22:56:14 +02:00
|
|
|
let mut userroom_id = user_id.as_bytes().to_vec();
|
|
|
|
userroom_id.push(0xff);
|
|
|
|
userroom_id.extend_from_slice(room_id.as_bytes());
|
2022-05-30 12:58:43 +02:00
|
|
|
|
2022-06-19 22:56:14 +02:00
|
|
|
self.userroomid_highlightcount
|
|
|
|
.get(&userroom_id)?
|
|
|
|
.map(|bytes| {
|
|
|
|
utils::u64_from_bytes(&bytes)
|
|
|
|
.map_err(|_| Error::bad_database("Invalid highlight count in db."))
|
|
|
|
})
|
|
|
|
.unwrap_or(Ok(0))
|
2020-05-24 08:30:57 +02:00
|
|
|
}
|
|
|
|
|
2022-08-14 13:38:21 +02:00
|
|
|
fn associate_token_shortstatehash(
|
2022-06-19 22:56:14 +02:00
|
|
|
&self,
|
2020-08-18 12:15:27 +02:00
|
|
|
room_id: &RoomId,
|
2022-06-19 22:56:14 +02:00
|
|
|
token: u64,
|
|
|
|
shortstatehash: u64,
|
|
|
|
) -> Result<()> {
|
2022-10-05 12:45:54 +02:00
|
|
|
let shortroomid = services().rooms.short.get_shortroomid(room_id)?.expect("room exists");
|
2020-08-18 12:15:27 +02:00
|
|
|
|
2022-06-19 22:56:14 +02:00
|
|
|
let mut key = shortroomid.to_be_bytes().to_vec();
|
|
|
|
key.extend_from_slice(&token.to_be_bytes());
|
2020-08-18 12:15:27 +02:00
|
|
|
|
2022-06-19 22:56:14 +02:00
|
|
|
self.roomsynctoken_shortstatehash
|
|
|
|
.insert(&key, &shortstatehash.to_be_bytes())
|
|
|
|
}
|
2021-06-17 20:44:29 +02:00
|
|
|
|
2022-08-14 13:38:21 +02:00
|
|
|
fn get_token_shortstatehash(&self, room_id: &RoomId, token: u64) -> Result<Option<u64>> {
|
2022-10-05 12:45:54 +02:00
|
|
|
let shortroomid = services().rooms.short.get_shortroomid(room_id)?.expect("room exists");
|
2021-06-17 20:44:29 +02:00
|
|
|
|
2022-06-19 22:56:14 +02:00
|
|
|
let mut key = shortroomid.to_be_bytes().to_vec();
|
|
|
|
key.extend_from_slice(&token.to_be_bytes());
|
2020-08-18 12:15:27 +02:00
|
|
|
|
2022-06-19 22:56:14 +02:00
|
|
|
self.roomsynctoken_shortstatehash
|
|
|
|
.get(&key)?
|
|
|
|
.map(|bytes| {
|
|
|
|
utils::u64_from_bytes(&bytes).map_err(|_| {
|
|
|
|
Error::bad_database("Invalid shortstatehash in roomsynctoken_shortstatehash")
|
|
|
|
})
|
|
|
|
})
|
|
|
|
.transpose()
|
2020-08-21 21:22:59 +02:00
|
|
|
}
|
2020-08-18 12:15:27 +02:00
|
|
|
|
2022-08-14 13:38:21 +02:00
|
|
|
fn get_shared_rooms<'a>(
|
2020-08-21 21:22:59 +02:00
|
|
|
&'a self,
|
2021-11-26 20:36:40 +01:00
|
|
|
users: Vec<Box<UserId>>,
|
2022-09-07 13:25:51 +02:00
|
|
|
) -> Result<Box<dyn Iterator<Item = Result<Box<RoomId>>>>> {
|
2021-06-17 20:34:14 +02:00
|
|
|
let iterators = users.into_iter().map(move |user_id| {
|
|
|
|
let mut prefix = user_id.as_bytes().to_vec();
|
|
|
|
prefix.push(0xff);
|
|
|
|
|
|
|
|
self.userroomid_joined
|
|
|
|
.scan_prefix(prefix)
|
|
|
|
.map(|(key, _)| {
|
|
|
|
let roomid_index = key
|
|
|
|
.iter()
|
|
|
|
.enumerate()
|
|
|
|
.find(|(_, &b)| b == 0xff)
|
|
|
|
.ok_or_else(|| Error::bad_database("Invalid userroomid_joined in db."))?
|
|
|
|
.0
|
|
|
|
+ 1; // +1 because the room id starts AFTER the separator
|
|
|
|
|
|
|
|
let room_id = key[roomid_index..].to_vec();
|
|
|
|
|
|
|
|
Ok::<_, Error>(room_id)
|
|
|
|
})
|
|
|
|
.filter_map(|r| r.ok())
|
|
|
|
});
|
2020-08-21 21:22:59 +02:00
|
|
|
|
2020-08-22 23:09:53 +02:00
|
|
|
// We use the default compare function because keys are sorted correctly (not reversed)
|
2021-06-08 18:10:00 +02:00
|
|
|
Ok(utils::common_elements(iterators, Ord::cmp)
|
2020-08-21 21:22:59 +02:00
|
|
|
.expect("users is not empty")
|
|
|
|
.map(|bytes| {
|
2021-11-27 00:30:28 +01:00
|
|
|
RoomId::parse(utils::string_from_bytes(&*bytes).map_err(|_| {
|
2020-08-21 21:22:59 +02:00
|
|
|
Error::bad_database("Invalid RoomId bytes in userroomid_joined")
|
|
|
|
})?)
|
|
|
|
.map_err(|_| Error::bad_database("Invalid RoomId in userroomid_joined."))
|
2021-06-08 18:10:00 +02:00
|
|
|
}))
|
2020-08-18 12:15:27 +02:00
|
|
|
}
|
2022-08-14 13:38:21 +02:00
|
|
|
}
|