files that aren't videos or images are now uploaded as file
Some checks failed
continuous-integration/drone/push Build is failing

This commit is contained in:
LordMZTE 2021-07-26 23:58:49 +02:00
parent 298c0a7f6f
commit 05e00f412b

View file

@ -6,7 +6,12 @@ use matrix_sdk::{
events::{
room::{
message::{
ImageMessageEventContent, MessageEventContent, MessageType, VideoInfo,
FileInfo,
FileMessageEventContent,
ImageMessageEventContent,
MessageEventContent,
MessageType,
VideoInfo,
VideoMessageEventContent,
},
ImageInfo,
@ -36,7 +41,7 @@ struct CachedMeme {
}
impl CachedMeme {
fn into_message_type(self) -> Option<MessageType> {
fn into_message_type(self) -> MessageType {
let Self {
mxc,
mime,
@ -45,7 +50,7 @@ impl CachedMeme {
} = self;
match mime.type_() {
mime::IMAGE => Some(MessageType::Image(ImageMessageEventContent::plain(
mime::IMAGE => MessageType::Image(ImageMessageEventContent::plain(
meme_name,
mxc,
Some(Box::new({
@ -54,8 +59,8 @@ impl CachedMeme {
info.size = Some(size);
info
})),
))),
mime::VIDEO => Some(MessageType::Video(VideoMessageEventContent::plain(
)),
mime::VIDEO => MessageType::Video(VideoMessageEventContent::plain(
meme_name,
mxc,
Some(Box::new({
@ -64,8 +69,17 @@ impl CachedMeme {
info.size = Some(size);
info
})),
))),
_ => None,
)),
_ => MessageType::File(FileMessageEventContent::plain(
meme_name,
mxc,
Some(Box::new({
let mut info = FileInfo::new();
info.mimetype = Some(mime.to_string());
info.size = Some(size);
info
})),
)),
}
}
}
@ -80,7 +94,7 @@ pub async fn on_msg(msg: &str, room: Room, bot: &Bot) -> anyhow::Result<()> {
room.name()
);
return Ok(());
}
},
};
if let Some(ref sendmeme_cmd) = bot.config.sendmeme_command {
@ -169,7 +183,6 @@ async fn cache_send_meme(meme_id: u32, bot: &Bot, room: Joined) -> anyhow::Resul
.insert(meme_id.to_be_bytes(), bincode::serialize(&cached)?)?;
send_meme(&room, cached).await?;
//
// we do this after we have responded, in order to not delay the response
if bot.meme_count.load(Ordering::SeqCst) >= bot.config.clear_cache_threshold {
let mut client = bot.jm_client.write().await;
@ -200,10 +213,7 @@ async fn cache_send_meme(meme_id: u32, bot: &Bot, room: Joined) -> anyhow::Resul
}
async fn send_meme(room: &Joined, cached: CachedMeme) -> anyhow::Result<()> {
let msg_ty = cached
.into_message_type()
.context("Found meme that is neither image nor video!")?;
let msg_ty = cached.into_message_type();
room.send(
AnyMessageEventContent::RoomMessage(MessageEventContent::new(msg_ty)),
None,