forked from jmug/stoatchat
fix: force OOP bot updates to avoid inconsistencies
This commit is contained in:
@@ -56,9 +56,10 @@ auto_derived!(
|
|||||||
}
|
}
|
||||||
);
|
);
|
||||||
|
|
||||||
|
#[allow(clippy::disallowed_methods)]
|
||||||
impl Bot {
|
impl Bot {
|
||||||
/// Remove a field from this object
|
/// Remove a field from this object
|
||||||
pub fn remove(&mut self, field: &FieldsBot) {
|
pub fn remove_field(&mut self, field: &FieldsBot) {
|
||||||
match field {
|
match field {
|
||||||
FieldsBot::Token => self.token = nanoid::nanoid!(64),
|
FieldsBot::Token => self.token = nanoid::nanoid!(64),
|
||||||
FieldsBot::InteractionsURL => {
|
FieldsBot::InteractionsURL => {
|
||||||
@@ -67,6 +68,27 @@ impl Bot {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Update this bot
|
||||||
|
pub async fn update(
|
||||||
|
&mut self,
|
||||||
|
db: &Database,
|
||||||
|
mut partial: PartialBot,
|
||||||
|
remove: Vec<FieldsBot>,
|
||||||
|
) -> Result<()> {
|
||||||
|
if remove.contains(&FieldsBot::Token) {
|
||||||
|
partial.token = Some(nanoid::nanoid!(64));
|
||||||
|
}
|
||||||
|
|
||||||
|
for field in &remove {
|
||||||
|
self.remove_field(field);
|
||||||
|
}
|
||||||
|
|
||||||
|
db.update_bot(&self.id, &partial, remove).await?;
|
||||||
|
|
||||||
|
self.apply_options(partial);
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
|
||||||
/// Delete this bot
|
/// Delete this bot
|
||||||
pub async fn delete(&self, db: &Database) -> Result<()> {
|
pub async fn delete(&self, db: &Database) -> Result<()> {
|
||||||
// db.fetch_user(&self.id).await?.mark_deleted(db).await?;
|
// db.fetch_user(&self.id).await?.mark_deleted(db).await?;
|
||||||
@@ -76,7 +98,7 @@ impl Bot {
|
|||||||
|
|
||||||
#[cfg(test)]
|
#[cfg(test)]
|
||||||
mod tests {
|
mod tests {
|
||||||
use crate::{Bot, FieldsBot};
|
use crate::{Bot, FieldsBot, PartialBot};
|
||||||
|
|
||||||
#[async_std::test]
|
#[async_std::test]
|
||||||
async fn crud() {
|
async fn crud() {
|
||||||
@@ -94,16 +116,19 @@ mod tests {
|
|||||||
};
|
};
|
||||||
|
|
||||||
db.insert_bot(&bot).await.unwrap();
|
db.insert_bot(&bot).await.unwrap();
|
||||||
db.update_bot(
|
|
||||||
bot_id,
|
let mut updated_bot = bot.clone();
|
||||||
&crate::PartialBot {
|
updated_bot
|
||||||
public: Some(true),
|
.update(
|
||||||
..Default::default()
|
&db,
|
||||||
},
|
PartialBot {
|
||||||
vec![FieldsBot::Token, FieldsBot::InteractionsURL],
|
public: Some(true),
|
||||||
)
|
..Default::default()
|
||||||
.await
|
},
|
||||||
.unwrap();
|
vec![FieldsBot::Token, FieldsBot::InteractionsURL],
|
||||||
|
)
|
||||||
|
.await
|
||||||
|
.unwrap();
|
||||||
|
|
||||||
let fetched_bot1 = db.fetch_bot(bot_id).await.unwrap();
|
let fetched_bot1 = db.fetch_bot(bot_id).await.unwrap();
|
||||||
let fetched_bot2 = db.fetch_bot_by_token(&fetched_bot1.token).await.unwrap();
|
let fetched_bot2 = db.fetch_bot_by_token(&fetched_bot1.token).await.unwrap();
|
||||||
@@ -114,6 +139,7 @@ mod tests {
|
|||||||
assert!(bot.interactions_url.is_some());
|
assert!(bot.interactions_url.is_some());
|
||||||
assert!(fetched_bot1.interactions_url.is_none());
|
assert!(fetched_bot1.interactions_url.is_none());
|
||||||
assert_ne!(bot.token, fetched_bot1.token);
|
assert_ne!(bot.token, fetched_bot1.token);
|
||||||
|
assert_eq!(updated_bot, fetched_bot1);
|
||||||
assert_eq!(fetched_bot1, fetched_bot2);
|
assert_eq!(fetched_bot1, fetched_bot2);
|
||||||
assert_eq!(fetched_bot1, fetched_bots[0]);
|
assert_eq!(fetched_bot1, fetched_bots[0]);
|
||||||
assert_eq!(1, db.get_number_of_bots_by_user(user_id).await.unwrap());
|
assert_eq!(1, db.get_number_of_bots_by_user(user_id).await.unwrap());
|
||||||
|
|||||||
@@ -43,7 +43,8 @@ impl AbstractBots for ReferenceDb {
|
|||||||
let mut bots = self.bots.lock().await;
|
let mut bots = self.bots.lock().await;
|
||||||
if let Some(bot) = bots.get_mut(id) {
|
if let Some(bot) = bots.get_mut(id) {
|
||||||
for field in remove {
|
for field in remove {
|
||||||
bot.remove(&field);
|
#[allow(clippy::disallowed_methods)]
|
||||||
|
bot.remove_field(&field);
|
||||||
}
|
}
|
||||||
|
|
||||||
bot.apply_options(partial.clone());
|
bot.apply_options(partial.clone());
|
||||||
|
|||||||
Reference in New Issue
Block a user