2024-05-09 15:59:08 -07:00
|
|
|
use std::sync::Arc;
|
|
|
|
|
2025-02-23 01:17:45 -05:00
|
|
|
use conduwuit::{Result, implement};
|
2024-12-06 12:44:10 +00:00
|
|
|
use database::{Database, Deserialized, Map};
|
2024-08-08 17:18:30 +00:00
|
|
|
use ruma::{RoomId, UserId};
|
2021-04-12 12:40:16 +02:00
|
|
|
|
2025-02-23 01:17:45 -05:00
|
|
|
use crate::{Dep, globals, rooms, rooms::short::ShortStateHash};
|
2024-07-18 06:37:47 +00:00
|
|
|
|
2024-05-09 15:59:08 -07:00
|
|
|
pub struct Service {
|
2024-06-28 22:51:39 +00:00
|
|
|
db: Data,
|
2024-10-15 09:54:20 +00:00
|
|
|
services: Services,
|
|
|
|
}
|
|
|
|
|
|
|
|
struct Data {
|
2024-12-06 12:44:10 +00:00
|
|
|
db: Arc<Database>,
|
2024-10-15 09:54:20 +00:00
|
|
|
userroomid_notificationcount: Arc<Map>,
|
|
|
|
userroomid_highlightcount: Arc<Map>,
|
|
|
|
roomuserid_lastnotificationread: Arc<Map>,
|
|
|
|
roomsynctoken_shortstatehash: Arc<Map>,
|
|
|
|
}
|
|
|
|
|
|
|
|
struct Services {
|
|
|
|
globals: Dep<globals::Service>,
|
|
|
|
short: Dep<rooms::short::Service>,
|
2022-08-14 13:38:21 +02:00
|
|
|
}
|
2021-04-13 15:00:45 +02:00
|
|
|
|
2024-07-04 03:26:19 +00:00
|
|
|
impl crate::Service for Service {
|
|
|
|
fn build(args: crate::Args<'_>) -> Result<Arc<Self>> {
|
|
|
|
Ok(Arc::new(Self {
|
2024-10-15 09:54:20 +00:00
|
|
|
db: Data {
|
2024-12-06 12:44:10 +00:00
|
|
|
db: args.db.clone(),
|
|
|
|
userroomid_notificationcount: args.db["userroomid_notificationcount"].clone(),
|
|
|
|
userroomid_highlightcount: args.db["userroomid_highlightcount"].clone(),
|
|
|
|
roomuserid_lastnotificationread: args.db["userroomid_highlightcount"].clone(),
|
|
|
|
roomsynctoken_shortstatehash: args.db["roomsynctoken_shortstatehash"].clone(),
|
|
|
|
},
|
2024-10-15 09:54:20 +00:00
|
|
|
|
|
|
|
services: Services {
|
|
|
|
globals: args.depend::<globals::Service>("globals"),
|
|
|
|
short: args.depend::<rooms::short::Service>("rooms::short"),
|
|
|
|
},
|
2024-07-04 03:26:19 +00:00
|
|
|
}))
|
2024-05-27 03:17:20 +00:00
|
|
|
}
|
|
|
|
|
2024-07-04 03:26:19 +00:00
|
|
|
fn name(&self) -> &str { crate::service::make_name(std::module_path!()) }
|
|
|
|
}
|
|
|
|
|
2024-10-15 09:54:20 +00:00
|
|
|
#[implement(Service)]
|
|
|
|
pub fn reset_notification_counts(&self, user_id: &UserId, room_id: &RoomId) {
|
|
|
|
let userroom_id = (user_id, room_id);
|
|
|
|
self.db.userroomid_highlightcount.put(userroom_id, 0_u64);
|
|
|
|
self.db.userroomid_notificationcount.put(userroom_id, 0_u64);
|
2024-03-05 19:48:54 -05:00
|
|
|
|
2024-10-15 09:54:20 +00:00
|
|
|
let roomuser_id = (room_id, user_id);
|
|
|
|
let count = self.services.globals.next_count().unwrap();
|
|
|
|
self.db
|
|
|
|
.roomuserid_lastnotificationread
|
|
|
|
.put(roomuser_id, count);
|
|
|
|
}
|
2024-03-05 19:48:54 -05:00
|
|
|
|
2024-10-15 09:54:20 +00:00
|
|
|
#[implement(Service)]
|
|
|
|
pub async fn notification_count(&self, user_id: &UserId, room_id: &RoomId) -> u64 {
|
|
|
|
let key = (user_id, room_id);
|
|
|
|
self.db
|
|
|
|
.userroomid_notificationcount
|
|
|
|
.qry(&key)
|
|
|
|
.await
|
|
|
|
.deserialized()
|
|
|
|
.unwrap_or(0)
|
|
|
|
}
|
2024-03-05 19:48:54 -05:00
|
|
|
|
2024-10-15 09:54:20 +00:00
|
|
|
#[implement(Service)]
|
|
|
|
pub async fn highlight_count(&self, user_id: &UserId, room_id: &RoomId) -> u64 {
|
|
|
|
let key = (user_id, room_id);
|
|
|
|
self.db
|
|
|
|
.userroomid_highlightcount
|
|
|
|
.qry(&key)
|
|
|
|
.await
|
|
|
|
.deserialized()
|
|
|
|
.unwrap_or(0)
|
|
|
|
}
|
2024-03-05 19:48:54 -05:00
|
|
|
|
2024-10-15 09:54:20 +00:00
|
|
|
#[implement(Service)]
|
|
|
|
pub async fn last_notification_read(&self, user_id: &UserId, room_id: &RoomId) -> u64 {
|
|
|
|
let key = (room_id, user_id);
|
|
|
|
self.db
|
|
|
|
.roomuserid_lastnotificationread
|
|
|
|
.qry(&key)
|
|
|
|
.await
|
|
|
|
.deserialized()
|
|
|
|
.unwrap_or(0)
|
|
|
|
}
|
2024-03-05 19:48:54 -05:00
|
|
|
|
2024-10-15 09:54:20 +00:00
|
|
|
#[implement(Service)]
|
2024-12-15 00:05:47 -05:00
|
|
|
pub async fn associate_token_shortstatehash(
|
|
|
|
&self,
|
|
|
|
room_id: &RoomId,
|
|
|
|
token: u64,
|
|
|
|
shortstatehash: ShortStateHash,
|
|
|
|
) {
|
2024-10-15 09:54:20 +00:00
|
|
|
let shortroomid = self
|
|
|
|
.services
|
|
|
|
.short
|
|
|
|
.get_shortroomid(room_id)
|
|
|
|
.await
|
|
|
|
.expect("room exists");
|
2024-03-05 19:48:54 -05:00
|
|
|
|
2024-12-06 12:44:10 +00:00
|
|
|
let _cork = self.db.db.cork();
|
2024-10-15 09:54:20 +00:00
|
|
|
let key: &[u64] = &[shortroomid, token];
|
|
|
|
self.db
|
|
|
|
.roomsynctoken_shortstatehash
|
|
|
|
.put(key, shortstatehash);
|
|
|
|
}
|
2024-08-08 17:18:30 +00:00
|
|
|
|
2024-10-15 09:54:20 +00:00
|
|
|
#[implement(Service)]
|
2024-12-15 00:05:47 -05:00
|
|
|
pub async fn get_token_shortstatehash(
|
|
|
|
&self,
|
|
|
|
room_id: &RoomId,
|
|
|
|
token: u64,
|
|
|
|
) -> Result<ShortStateHash> {
|
2024-10-15 09:54:20 +00:00
|
|
|
let shortroomid = self.services.short.get_shortroomid(room_id).await?;
|
2024-08-08 17:18:30 +00:00
|
|
|
|
2024-10-15 09:54:20 +00:00
|
|
|
let key: &[u64] = &[shortroomid, token];
|
|
|
|
self.db
|
|
|
|
.roomsynctoken_shortstatehash
|
|
|
|
.qry(key)
|
|
|
|
.await
|
|
|
|
.deserialized()
|
|
|
|
}
|