Use transaction in update_blocked_mailinglist_contacts

This commit is contained in:
link2xt
2023-02-19 14:38:01 +00:00
parent ef03a33b29
commit 56d10f7c42
2 changed files with 37 additions and 38 deletions

View File

@@ -6,6 +6,7 @@
- use transaction in `Contact::add_or_lookup()` #4059 - use transaction in `Contact::add_or_lookup()` #4059
- Organize the connection pool as a stack rather than a queue to ensure that - Organize the connection pool as a stack rather than a queue to ensure that
connection page cache is reused more often. #4065 connection page cache is reused more often. #4065
- Use transaction in `update_blocked_mailinglist_contacts`. #4058
### Fixes ### Fixes
- Start SQL transactions with IMMEDIATE behaviour rather than default DEFERRED one. #4063 - Start SQL transactions with IMMEDIATE behaviour rather than default DEFERRED one. #4063

View File

@@ -869,48 +869,46 @@ impl Contact {
Ok(ret) Ok(ret)
} }
// add blocked mailinglists as contacts /// Adds blocked mailinglists as contacts
// to allow unblocking them as if they are contacts /// to allow unblocking them as if they are contacts
// (this way, only one unblock-ffi is needed and only one set of ui-functions, /// (this way, only one unblock-ffi is needed and only one set of ui-functions,
// from the users perspective, /// from the users perspective,
// there is not much difference in an email- and a mailinglist-address) /// there is not much difference in an email- and a mailinglist-address)
async fn update_blocked_mailinglist_contacts(context: &Context) -> Result<()> { async fn update_blocked_mailinglist_contacts(context: &Context) -> Result<()> {
let blocked_mailinglists = context context
.sql .sql
.query_map( .transaction(move |transaction| {
"SELECT name, grpid FROM chats WHERE type=? AND blocked=?;", let mut stmt = transaction
paramsv![Chattype::Mailinglist, Blocked::Yes], .prepare("SELECT name, grpid FROM chats WHERE type=? AND blocked=?")?;
|row| Ok((row.get::<_, String>(0)?, row.get::<_, String>(1)?)), let rows = stmt.query_map(params![Chattype::Mailinglist, Blocked::Yes], |row| {
|rows| { let name: String = row.get(0)?;
rows.collect::<std::result::Result<Vec<_>, _>>() let grpid: String = row.get(1)?;
.map_err(Into::into) Ok((name, grpid))
}, })?;
) let blocked_mailinglists = rows.collect::<std::result::Result<Vec<_>, _>>()?;
.await?;
for (name, grpid) in blocked_mailinglists { for (name, grpid) in blocked_mailinglists {
if !context let count = transaction.query_row(
.sql "SELECT COUNT(id) FROM contacts WHERE addr=?",
.exists( [&grpid],
"SELECT COUNT(id) FROM contacts WHERE addr=?;", |row| {
paramsv![grpid], let count: isize = row.get(0)?;
) Ok(count)
.await? },
{ )?;
context if count == 0 {
.sql transaction.execute("INSERT INTO contacts (addr) VALUES (?)", [&grpid])?;
.execute("INSERT INTO contacts (addr) VALUES (?);", paramsv![grpid])
.await?;
} }
// always do an update in case the blocking is reset or name is changed
context // Always do an update in case the blocking is reset or name is changed.
.sql transaction.execute(
.execute( "UPDATE contacts SET name=?, origin=?, blocked=1 WHERE addr=?",
"UPDATE contacts SET name=?, origin=?, blocked=1 WHERE addr=?;", params![&name, Origin::MailinglistAddress, &grpid],
paramsv![name, Origin::MailinglistAddress, grpid], )?;
)
.await?;
} }
Ok(()) Ok(())
})
.await?;
Ok(())
} }
/// Returns number of blocked contacts. /// Returns number of blocked contacts.