memes have their file name as image description
Some checks failed
continuous-integration/drone/push Build is failing

This commit is contained in:
LordMZTE 2021-07-17 14:47:03 +02:00
parent 568607fe3e
commit ccf8c65c80
3 changed files with 25 additions and 16 deletions

View file

@ -1,6 +1,3 @@
# 0.2.4
- fix match = "contains" commands matching when keyword is at the start of message, and there are invalid characters after it.
- add tests
- only 1 meme per message
- add Xd and xd commands
- add match_case option
# 0.2.5
- DATABASE FORMAT CHANGE!
- memes will now have their file name as the image description.

View file

@ -1,6 +1,6 @@
[package]
name = "ruff"
version = "0.2.4"
version = "0.2.5"
authors = ["LordMZTE <lord@mzte.de>"]
edition = "2018"

View file

@ -31,15 +31,22 @@ struct CachedMeme {
mime: Mime,
/// file size of the meme
size: UInt,
/// file name of the meme
meme_name: String,
}
impl CachedMeme {
fn into_message_type(self, name: String) -> Option<MessageType> {
let Self { mxc, mime, size } = self;
fn into_message_type(self) -> Option<MessageType> {
let Self {
mxc,
mime,
size,
meme_name,
} = self;
match mime.type_() {
mime::IMAGE => Some(MessageType::Image(ImageMessageEventContent::plain(
name,
meme_name,
mxc,
Some(Box::new({
let mut info = ImageInfo::new();
@ -49,7 +56,7 @@ impl CachedMeme {
})),
))),
mime::VIDEO => Some(MessageType::Video(VideoMessageEventContent::plain(
name,
meme_name,
mxc,
Some(Box::new({
let mut info = VideoInfo::new();
@ -80,7 +87,6 @@ pub async fn on_msg(msg: &str, room: Room, bot: &Bot) -> anyhow::Result<()> {
if meme.matches(msg) {
bot.meme_count.fetch_add(1, Ordering::SeqCst);
let meme_name = &meme.keyword;
if let Some(meme) = meme.get_meme(bot).await? {
match meme.id.parse::<u32>() {
Err(e) => {
@ -93,7 +99,7 @@ pub async fn on_msg(msg: &str, room: Room, bot: &Bot) -> anyhow::Result<()> {
Ok(id) => {
if let Some(ivec) = bot.memecache.get(id.to_le_bytes())? {
let cached = bincode::deserialize::<CachedMeme>(&ivec)?;
send_meme(&room, cached, meme_name.clone()).await?;
send_meme(&room, cached).await?;
} else {
info!("Meme {} not found in cache, uploading...", id);
let resp = bot
@ -121,12 +127,18 @@ pub async fn on_msg(msg: &str, room: Room, bot: &Bot) -> anyhow::Result<()> {
mime,
size: UInt::new(size as u64)
.context("Meme has file size over allowed limit!")?,
meme_name: meme
.link
.split('/')
.last()
.unwrap_or(&meme.link)
.to_string(),
};
bot.memecache
.insert(id.to_le_bytes(), bincode::serialize(&cached)?)?;
send_meme(&room, cached, meme_name.clone()).await?;
send_meme(&room, cached).await?;
} else {
error!(
"Couldn't guess MIME type of meme '{}', skipping.",
@ -156,9 +168,9 @@ pub async fn on_msg(msg: &str, room: Room, bot: &Bot) -> anyhow::Result<()> {
Ok(())
}
async fn send_meme(room: &Joined, cached: CachedMeme, meme_name: String) -> anyhow::Result<()> {
async fn send_meme(room: &Joined, cached: CachedMeme) -> anyhow::Result<()> {
let msg_ty = cached
.into_message_type(meme_name)
.into_message_type()
.context("Found meme that is neither image nor video!")?;
room.send(