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,47 +869,45 @@ 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<_>, _>>()?;
for (name, grpid) in blocked_mailinglists {
let count = transaction.query_row(
"SELECT COUNT(id) FROM contacts WHERE addr=?",
[&grpid],
|row| {
let count: isize = row.get(0)?;
Ok(count)
},
)?;
if count == 0 {
transaction.execute("INSERT INTO contacts (addr) VALUES (?)", [&grpid])?;
}
// Always do an update in case the blocking is reset or name is changed.
transaction.execute(
"UPDATE contacts SET name=?, origin=?, blocked=1 WHERE addr=?",
params![&name, Origin::MailinglistAddress, &grpid],
)?;
}
Ok(())
})
.await?; .await?;
for (name, grpid) in blocked_mailinglists {
if !context
.sql
.exists(
"SELECT COUNT(id) FROM contacts WHERE addr=?;",
paramsv![grpid],
)
.await?
{
context
.sql
.execute("INSERT INTO contacts (addr) VALUES (?);", paramsv![grpid])
.await?;
}
// always do an update in case the blocking is reset or name is changed
context
.sql
.execute(
"UPDATE contacts SET name=?, origin=?, blocked=1 WHERE addr=?;",
paramsv![name, Origin::MailinglistAddress, grpid],
)
.await?;
}
Ok(()) Ok(())
} }