revolt_database/models/file_hashes/ops/
reference.rs

1use revolt_result::Result;
2
3use crate::FileHash;
4use crate::ReferenceDb;
5
6use super::AbstractAttachmentHashes;
7
8#[async_trait]
9impl AbstractAttachmentHashes for ReferenceDb {
10    /// Insert a new attachment hash into the database.
11    async fn insert_attachment_hash(&self, hash: &FileHash) -> Result<()> {
12        let mut hashes = self.file_hashes.lock().await;
13        if hashes.contains_key(&hash.id) {
14            Err(create_database_error!("insert", "attachment"))
15        } else {
16            hashes.insert(hash.id.to_string(), hash.clone());
17            Ok(())
18        }
19    }
20
21    /// Fetch an attachment hash entry by sha256 hash.
22    async fn fetch_attachment_hash(&self, hash_value: &str) -> Result<FileHash> {
23        let hashes = self.file_hashes.lock().await;
24        hashes
25            .values()
26            .find(|&hash| hash.id == hash_value || hash.processed_hash == hash_value)
27            .cloned()
28            .ok_or(create_error!(NotFound))
29    }
30
31    /// Update an attachment hash nonce value.
32    async fn set_attachment_hash_nonce(&self, hash: &str, nonce: &str) -> Result<()> {
33        let mut hashes = self.file_hashes.lock().await;
34        if let Some(file) = hashes.get_mut(hash) {
35            file.iv = nonce.to_owned();
36            Ok(())
37        } else {
38            Err(create_error!(NotFound))
39        }
40    }
41
42    /// Delete attachment hash by id.
43    async fn delete_attachment_hash(&self, id: &str) -> Result<()> {
44        let mut file_hashes = self.file_hashes.lock().await;
45        if file_hashes.remove(id).is_some() {
46            Ok(())
47        } else {
48            Err(create_error!(NotFound))
49        }
50    }
51}