This commit is contained in:
parent
d0577cc66f
commit
cd1875240d
|
@ -1,7 +1,8 @@
|
||||||
use crate::meme::Meme;
|
use crate::meme::{Meme, MemeIdent};
|
||||||
use crate::meme::MemeIdent;
|
use serde::{
|
||||||
use serde::de::{self, Deserializer, MapAccess, Visitor};
|
de::{self, Deserializer, MapAccess, Visitor},
|
||||||
use serde::Deserialize;
|
Deserialize,
|
||||||
|
};
|
||||||
use std::path::PathBuf;
|
use std::path::PathBuf;
|
||||||
use url::Url;
|
use url::Url;
|
||||||
|
|
||||||
|
@ -92,7 +93,7 @@ impl<'de> Deserialize<'de> for Meme {
|
||||||
}
|
}
|
||||||
|
|
||||||
keyword = Some(map.next_value()?);
|
keyword = Some(map.next_value()?);
|
||||||
}
|
},
|
||||||
|
|
||||||
Field::Ident(i) => {
|
Field::Ident(i) => {
|
||||||
if ident.is_some() {
|
if ident.is_some() {
|
||||||
|
@ -104,12 +105,12 @@ impl<'de> Deserialize<'de> for Meme {
|
||||||
match i {
|
match i {
|
||||||
IdentField::Id => {
|
IdentField::Id => {
|
||||||
ident = Some(MemeIdent::Id(map.next_value()?));
|
ident = Some(MemeIdent::Id(map.next_value()?));
|
||||||
}
|
},
|
||||||
IdentField::RandomCat => {
|
IdentField::RandomCat => {
|
||||||
ident = Some(MemeIdent::RandomCat(map.next_value()?));
|
ident = Some(MemeIdent::RandomCat(map.next_value()?));
|
||||||
}
|
},
|
||||||
}
|
}
|
||||||
}
|
},
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
let keyword = keyword.ok_or_else(|| de::Error::missing_field("keyword"))?;
|
let keyword = keyword.ok_or_else(|| de::Error::missing_field("keyword"))?;
|
||||||
|
|
20
src/main.rs
20
src/main.rs
|
@ -10,11 +10,14 @@ use matrix_sdk::{
|
||||||
member::MemberEventContent,
|
member::MemberEventContent,
|
||||||
message::{MessageEventContent, MessageType, TextMessageEventContent},
|
message::{MessageEventContent, MessageType, TextMessageEventContent},
|
||||||
},
|
},
|
||||||
AnyToDeviceEvent, StrippedStateEvent, SyncMessageEvent,
|
AnyToDeviceEvent,
|
||||||
|
StrippedStateEvent,
|
||||||
|
SyncMessageEvent,
|
||||||
},
|
},
|
||||||
room::Room,
|
room::Room,
|
||||||
verification::Verification,
|
verification::Verification,
|
||||||
EventHandler, LoopCtrl,
|
EventHandler,
|
||||||
|
LoopCtrl,
|
||||||
};
|
};
|
||||||
use rand::{rngs::StdRng, SeedableRng};
|
use rand::{rngs::StdRng, SeedableRng};
|
||||||
use sled::Db;
|
use sled::Db;
|
||||||
|
@ -28,8 +31,7 @@ use std::{
|
||||||
time::Duration,
|
time::Duration,
|
||||||
};
|
};
|
||||||
use structopt::StructOpt;
|
use structopt::StructOpt;
|
||||||
use tokio::sync::Mutex;
|
use tokio::sync::{Mutex, RwLock};
|
||||||
use tokio::sync::RwLock;
|
|
||||||
|
|
||||||
use config::Config;
|
use config::Config;
|
||||||
|
|
||||||
|
@ -121,8 +123,8 @@ pub struct Bot {
|
||||||
memecache: Db,
|
memecache: Db,
|
||||||
config: Arc<Config>,
|
config: Arc<Config>,
|
||||||
/// used to keep track of how many memes have been sent.
|
/// used to keep track of how many memes have been sent.
|
||||||
/// this is reset once the threshold set in the config has been reached, and the JMClient cache
|
/// this is reset once the threshold set in the config has been reached, and
|
||||||
/// is cleared.
|
/// the JMClient cache is cleared.
|
||||||
meme_count: AtomicU32,
|
meme_count: AtomicU32,
|
||||||
rng: Mutex<StdRng>,
|
rng: Mutex<StdRng>,
|
||||||
}
|
}
|
||||||
|
@ -230,7 +232,7 @@ async fn on_response(response: &SyncResponse, client: &Client) -> anyhow::Result
|
||||||
error!("Error accepting key verification request: {}", e);
|
error!("Error accepting key verification request: {}", e);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
},
|
||||||
|
|
||||||
AnyToDeviceEvent::KeyVerificationKey(e) => {
|
AnyToDeviceEvent::KeyVerificationKey(e) => {
|
||||||
if let Some(Verification::SasV1(sas)) = &client
|
if let Some(Verification::SasV1(sas)) = &client
|
||||||
|
@ -241,9 +243,9 @@ async fn on_response(response: &SyncResponse, client: &Client) -> anyhow::Result
|
||||||
error!("Error confirming key verification request: {}", e);
|
error!("Error confirming key verification request: {}", e);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
},
|
||||||
|
|
||||||
_ => {}
|
_ => {},
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -5,7 +5,10 @@ use matrix_sdk::{
|
||||||
api::r0::media::create_content,
|
api::r0::media::create_content,
|
||||||
events::{
|
events::{
|
||||||
room::message::{
|
room::message::{
|
||||||
ImageMessageEventContent, MessageEventContent, MessageType, VideoMessageEventContent,
|
ImageMessageEventContent,
|
||||||
|
MessageEventContent,
|
||||||
|
MessageType,
|
||||||
|
VideoMessageEventContent,
|
||||||
},
|
},
|
||||||
AnyMessageEventContent,
|
AnyMessageEventContent,
|
||||||
},
|
},
|
||||||
|
@ -13,8 +16,7 @@ use matrix_sdk::{
|
||||||
room::{Joined, Room},
|
room::{Joined, Room},
|
||||||
};
|
};
|
||||||
use serde::{Deserialize, Serialize};
|
use serde::{Deserialize, Serialize};
|
||||||
use std::io::Cursor;
|
use std::{io::Cursor, sync::atomic::Ordering};
|
||||||
use std::sync::atomic::Ordering;
|
|
||||||
|
|
||||||
#[derive(Debug, Deserialize, Serialize)]
|
#[derive(Debug, Deserialize, Serialize)]
|
||||||
struct CachedMeme {
|
struct CachedMeme {
|
||||||
|
@ -63,7 +65,7 @@ pub async fn on_msg(msg: &str, room: Room, bot: &Bot) -> anyhow::Result<()> {
|
||||||
room.name()
|
room.name()
|
||||||
);
|
);
|
||||||
return Ok(());
|
return Ok(());
|
||||||
}
|
},
|
||||||
};
|
};
|
||||||
|
|
||||||
for meme in &bot.config.memes {
|
for meme in &bot.config.memes {
|
||||||
|
@ -74,8 +76,12 @@ pub async fn on_msg(msg: &str, room: Room, bot: &Bot) -> anyhow::Result<()> {
|
||||||
if let Some(meme) = meme.get_meme(bot).await? {
|
if let Some(meme) = meme.get_meme(bot).await? {
|
||||||
match meme.id.parse::<u32>() {
|
match meme.id.parse::<u32>() {
|
||||||
Err(e) => {
|
Err(e) => {
|
||||||
error!("Meme {:?} has invalid ID! tilera, you messed up with your stupid php again: {}", &meme, e);
|
error!(
|
||||||
}
|
"Meme {:?} has invalid ID! tilera, you messed up with your stupid php \
|
||||||
|
again: {}",
|
||||||
|
&meme, e
|
||||||
|
);
|
||||||
|
},
|
||||||
Ok(id) => {
|
Ok(id) => {
|
||||||
if let Some(ivec) = bot.memecache.get(id.to_le_bytes())? {
|
if let Some(ivec) = bot.memecache.get(id.to_le_bytes())? {
|
||||||
let cached = bincode::deserialize::<CachedMeme>(&ivec)?;
|
let cached = bincode::deserialize::<CachedMeme>(&ivec)?;
|
||||||
|
@ -119,7 +125,7 @@ pub async fn on_msg(msg: &str, room: Room, bot: &Bot) -> anyhow::Result<()> {
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
},
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
error!("Found meme with invalid id! {:?}", &meme);
|
error!("Found meme with invalid id! {:?}", &meme);
|
||||||
|
|
Loading…
Reference in a new issue