Radish alpha
h
Radicle Heartwood Protocol & Stack
Radicle
Git (anonymous pull)
Log in to clone via SSH
radicle: Add FilterBy for modifying notifications
Matthias Beyer committed 7 months ago
commit d686ca86e3e040aab8e1de063ccec81a1b4bdbec
parent 646d4360e7d905f0a9ec87d32c6768c5390eabf3
1 file changed +94 -24
modified crates/radicle/src/node/notifications/store.rs
@@ -190,35 +190,77 @@ impl Store<Write> {
    }

    /// Delete the given notifications.
-
    pub fn clear(&mut self, ids: &[NotificationId]) -> Result<usize, Error> {
-
        transaction(&self.db, |_| {
-
            let mut stmt = self
-
                .db
-
                .prepare("DELETE FROM `repository-notifications` WHERE rowid = ?")?;
-

-
            // N.b. we need to keep the count manually since the change count
-
            // will always be `1` because of each reset.
-
            let mut count = 0;
-
            for id in ids {
-
                stmt.bind((1, *id as i64))?;
-
                stmt.next()?;
-
                stmt.reset()?;
-
                count += self.db.change_count();
-
            }
-
            Ok(count)
-
        })
+
    pub fn clear(
+
        &mut self,
+
        filter_by: Option<FilterBy>,
+
        ids: &[NotificationId],
+
    ) -> Result<usize, Error> {
+
        match filter_by {
+
            Some(FilterBy { ref_update }) => transaction(&self.db, |_| {
+
                let mut stmt = self.db.prepare(
+
                    "DELETE FROM `repository-notifications` WHERE rowid = ?1 AND status = ?2",
+
                )?;
+

+
                // N.b. we need to keep the count manually since the change count
+
                // will always be `1` because of each reset.
+
                let mut count = 0;
+
                for id in ids {
+
                    stmt.bind((1, *id as i64))?;
+
                    stmt.bind((2, ref_update.as_str()))?;
+
                    stmt.next()?;
+
                    stmt.reset()?;
+
                    count += self.db.change_count();
+
                }
+
                Ok(count)
+
            }),
+
            None => transaction(&self.db, |_| {
+
                let mut stmt = self
+
                    .db
+
                    .prepare("DELETE FROM `repository-notifications` WHERE rowid = ?")?;
+

+
                // N.b. we need to keep the count manually since the change count
+
                // will always be `1` because of each reset.
+
                let mut count = 0;
+
                for id in ids {
+
                    stmt.bind((1, *id as i64))?;
+
                    stmt.next()?;
+
                    stmt.reset()?;
+
                    count += self.db.change_count();
+
                }
+
                Ok(count)
+
            }),
+
        }
    }

    /// Delete all notifications of a repo.
-
    pub fn clear_by_repo(&mut self, repo: &RepoId) -> Result<usize, Error> {
-
        let mut stmt = self
-
            .db
-
            .prepare("DELETE FROM `repository-notifications` WHERE repo = ?")?;
+
    pub fn clear_by_repo(
+
        &mut self,
+
        filter_by: Option<FilterBy>,
+
        repo: &RepoId,
+
    ) -> Result<usize, Error> {
+
        match filter_by {
+
            Some(FilterBy { ref_update }) => transaction(&self.db, |_| {
+
                let mut stmt = self.db.prepare(
+
                    "DELETE FROM `repository-notifications` WHERE repo = ?1 where ref = ?2",
+
                )?;
+

+
                stmt.bind((1, repo))?;
+
                stmt.bind((2, ref_update.as_str()))?;
+
                stmt.next()?;

-
        stmt.bind((1, repo))?;
-
        stmt.next()?;
+
                Ok(self.db.change_count())
+
            }),
+
            None => transaction(&self.db, |_| {
+
                let mut stmt = self
+
                    .db
+
                    .prepare("DELETE FROM `repository-notifications` WHERE repo = ?")?;

-
        Ok(self.db.change_count())
+
                stmt.bind((1, repo))?;
+
                stmt.next()?;
+

+
                Ok(self.db.change_count())
+
            }),
+
        }
    }

    /// Delete all notifications from all repos.
@@ -411,6 +453,34 @@ mod parse {
    }
}

+
#[derive(Debug)]
+
pub struct FilterBy {
+
    pub ref_update: FilterByRefUpdate,
+
}
+

+
#[derive(Debug)]
+
pub enum FilterByRefUpdate {
+
    Merged,
+
    Deleted,
+
    Created,
+
    Opened,
+
    Updated,
+
    Draft,
+
}
+

+
impl FilterByRefUpdate {
+
    fn as_str(&self) -> &'static str {
+
        match self {
+
            Self::Merged => "merged",
+
            Self::Deleted => "deleted",
+
            Self::Created => "created",
+
            Self::Opened => "opened",
+
            Self::Updated => "updated",
+
            Self::Draft => "draft",
+
        }
+
    }
+
}
+

#[cfg(test)]
#[allow(clippy::unwrap_used)]
mod test {