Skip to content

Commit

Permalink
Support enabling spoiler for media
Browse files Browse the repository at this point in the history
  • Loading branch information
SpriteOvO committed Jun 30, 2024
1 parent 5a6386c commit 85e5c3e
Show file tree
Hide file tree
Showing 6 changed files with 91 additions and 26 deletions.
16 changes: 11 additions & 5 deletions src/notify/telegram/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -207,7 +207,13 @@ impl Notifier {

let text = make_live_text(&title_history, live_status, source);
let resp = Request::new(&token)
.send_photo(&self.params.chat, &live_status.cover_image_url)
.send_photo(
&self.params.chat,
MediaPhoto {
url: &live_status.cover_image_url,
has_spoiler: false,
},
)
.thread_id_opt(self.params.thread_id)
.text(text)
.send()
Expand Down Expand Up @@ -431,10 +437,10 @@ impl Notifier {
match attachment {
PostAttachment::Image(image) => {
// TODO: `sendAnimation` for single GIF?
Request::new(token).send_photo(&self.params.chat, &image.media_url)
Request::new(token).send_photo(&self.params.chat, image.into())
}
PostAttachment::Video(video) => {
Request::new(token).send_video(&self.params.chat, &video.media_url)
Request::new(token).send_video(&self.params.chat, video.into())
}
}
.text(text)
Expand Down Expand Up @@ -468,8 +474,8 @@ impl Notifier {
let medias = attachments.iter().map(|attachment| match attachment {
// TODO: Mixing GIF in media group to send is not yet supported in Telegram, add
// an overlay like video? (see comment in twitter.com implementation)
PostAttachment::Image(image) => Media::Photo(&image.media_url),
PostAttachment::Video(video) => Media::Video(&video.media_url),
PostAttachment::Image(image) => Media::Photo(image.into()),
PostAttachment::Video(video) => Media::Video(video.into()),
});

Request::new(token)
Expand Down
67 changes: 50 additions & 17 deletions src/notify/telegram/request.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ use serde_json::{self as json, json};
use spdlog::prelude::*;

use super::ConfigChat;
use crate::source::{PostAttachmentImage, PostAttachmentVideo};

pub struct Request<'a> {
token: &'a str,
Expand Down Expand Up @@ -110,7 +111,7 @@ impl<'a> Request<'a> {
}
}

pub fn send_photo(self, chat: &'a ConfigChat, photo: &'a str) -> SendMedia<'a> {
pub fn send_photo(self, chat: &'a ConfigChat, photo: MediaPhoto<'a>) -> SendMedia<'a> {
SendMedia {
base: self,
chat,
Expand All @@ -122,7 +123,7 @@ impl<'a> Request<'a> {
}
}

pub fn send_video(self, chat: &'a ConfigChat, video: &'a str) -> SendMedia<'a> {
pub fn send_video(self, chat: &'a ConfigChat, video: MediaVideo<'a>) -> SendMedia<'a> {
SendMedia {
base: self,
chat,
Expand Down Expand Up @@ -303,8 +304,36 @@ impl<'a> SendMessage<'a> {
}

pub enum Media<'a> {
Photo(&'a str), // TODO: Make a enum for it
Video(&'a str),
Photo(MediaPhoto<'a>),
Video(MediaVideo<'a>),
}

pub struct MediaPhoto<'a> {
pub url: &'a str,
pub has_spoiler: bool,
}

impl<'a> From<&'a PostAttachmentImage> for MediaPhoto<'a> {
fn from(value: &'a PostAttachmentImage) -> Self {
Self {
url: &value.media_url,
has_spoiler: value.has_spoiler,
}
}
}

pub struct MediaVideo<'a> {
pub url: &'a str,
pub has_spoiler: bool,
}

impl<'a> From<&'a PostAttachmentVideo> for MediaVideo<'a> {
fn from(value: &'a PostAttachmentVideo) -> Self {
Self {
url: &value.media_url,
has_spoiler: value.has_spoiler,
}
}
}

pub struct SendMedia<'a> {
Expand Down Expand Up @@ -365,14 +394,16 @@ impl<'a> SendMedia<'a> {
"disable_notification": self.disable_notification
}
);
let (method, url) = match self.media {
Media::Photo(url) => {
body["photo"] = url.into();
("sendPhoto", url)
let (method, url) = match &self.media {
Media::Photo(photo) => {
body["photo"] = photo.url.into();
body["has_spoiler"] = photo.has_spoiler.into();
("sendPhoto", photo.url)
}
Media::Video(url) => {
body["video"] = url.into();
("sendVideo", url)
Media::Video(video) => {
body["video"] = video.url.into();
body["has_spoiler"] = video.has_spoiler.into();
("sendVideo", video.url)
}
};
if let Some(text) = self.text {
Expand Down Expand Up @@ -467,16 +498,18 @@ impl<'a> SendMediaGroup<'a> {
.medias
.iter()
.map(|media| match media {
Media::Photo(url) => {
Media::Photo(photo) => {
json!({
"type": "photo",
"media": url,
"media": photo.url,
"has_spoiler": photo.has_spoiler,
})
}
Media::Video(url) => {
Media::Video(video) => {
json!({
"type": "video",
"media": url,
"media": video.url,
"has_spoiler": video.has_spoiler,
})
}
})
Expand Down Expand Up @@ -508,8 +541,8 @@ impl<'a> SendMediaGroup<'a> {
.map(|(i, (media, kind))| {
media["media"] = format_attach_url(i).into();
match kind {
Media::Photo(url) => FileUrl::new_photo(url),
Media::Video(url) => FileUrl::new_video(url),
Media::Photo(photo) => FileUrl::new_photo(photo.url),
Media::Video(video) => FileUrl::new_video(video.url),
}
})
.collect::<Vec<_>>();
Expand Down
6 changes: 6 additions & 0 deletions src/source/bilibili/space.rs
Original file line number Diff line number Diff line change
Expand Up @@ -537,12 +537,14 @@ fn parse_response(resp: data::SpaceHistory) -> anyhow::Result<Posts> {
.map(|pic| {
PostAttachment::Image(PostAttachmentImage {
media_url: upgrade_to_https(&pic.url),
has_spoiler: false,
})
})
.collect(),
data::ModuleDynamicMajor::Archive(archive) => {
vec![PostAttachment::Image(PostAttachmentImage {
media_url: upgrade_to_https(&archive.archive.cover),
has_spoiler: false,
})]
}
data::ModuleDynamicMajor::Article(article) => article
Expand All @@ -552,6 +554,7 @@ fn parse_response(resp: data::SpaceHistory) -> anyhow::Result<Posts> {
.map(|cover| {
PostAttachment::Image(PostAttachmentImage {
media_url: upgrade_to_https(cover),
has_spoiler: false,
})
})
.collect(),
Expand All @@ -562,17 +565,20 @@ fn parse_response(resp: data::SpaceHistory) -> anyhow::Result<Posts> {
.map(|item| {
PostAttachment::Image(PostAttachmentImage {
media_url: upgrade_to_https(&item.src),
has_spoiler: false,
})
})
.collect(),
data::ModuleDynamicMajor::Common(common) => {
vec![PostAttachment::Image(PostAttachmentImage {
media_url: upgrade_to_https(&common.common.cover),
has_spoiler: false,
})]
}
data::ModuleDynamicMajor::Pgc(pgc) => {
vec![PostAttachment::Image(PostAttachmentImage {
media_url: upgrade_to_https(&pgc.pgc.cover),
has_spoiler: false,
})]
}
data::ModuleDynamicMajor::LiveRcmd => unreachable!(),
Expand Down
1 change: 1 addition & 0 deletions src/source/bilibili/video.rs
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,7 @@ fn parse_response(resp: data::SeriesArchives) -> anyhow::Result<Posts> {
repost_from: None,
attachments: vec![PostAttachment::Image(PostAttachmentImage {
media_url: upgrade_to_https(&archive.pic),
has_spoiler: false,
})],
})
.collect();
Expand Down
21 changes: 17 additions & 4 deletions src/source/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -253,21 +253,34 @@ impl Post {
}
}

#[derive(Clone, Debug, PartialEq, Eq)]
#[derive(Clone, Debug, PartialEq)]
pub enum PostAttachment {
Image(PostAttachmentImage),
#[allow(dead_code)]
Video(PostAttachmentVideo),
}

#[derive(Clone, Debug, PartialEq, Eq)]
#[derive(Clone, Debug)]
pub struct PostAttachmentImage {
pub media_url: String,
pub has_spoiler: bool,
}

impl PartialEq for PostAttachmentImage {
fn eq(&self, other: &Self) -> bool {
self.media_url.eq(&other.media_url)
}
}

#[derive(Clone, Debug, PartialEq, Eq)]
#[derive(Clone, Debug)]
pub struct PostAttachmentVideo {
pub media_url: String,
pub has_spoiler: bool,
}

impl PartialEq for PostAttachmentVideo {
fn eq(&self, other: &Self) -> bool {
self.media_url.eq(&other.media_url)
}
}

#[derive(Debug, Eq, PartialEq)]
Expand Down
6 changes: 6 additions & 0 deletions src/source/twitter/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -477,6 +477,8 @@ fn parse_tweet(tweet: data::Tweet) -> Post {
}
.map(|result| RepostFrom::Recursion(Box::new(parse_tweet(result.result.into_tweet()))));

let possibly_sensitive = tweet.legacy.possibly_sensitive.unwrap_or(false);

let card_attachment = tweet.card.and_then(|card| {
const IMAGE_KEYS: [&str; 3] = [
"photo_image_full_size_original",
Expand All @@ -495,6 +497,7 @@ fn parse_tweet(tweet: data::Tweet) -> Post {
Some(data::TweetCardValue::Image { image_value }) => {
Some(PostAttachment::Image(PostAttachmentImage {
media_url: image_value.url.clone(),
has_spoiler: possibly_sensitive,
}))
}
Some(_) => {
Expand All @@ -517,6 +520,7 @@ fn parse_tweet(tweet: data::Tweet) -> Post {
.map(|media| match media.kind {
data::TweetLegacyEntityMediaKind::Photo => PostAttachment::Image(PostAttachmentImage {
media_url: media.media_url_https,
has_spoiler: possibly_sensitive,
}),
data::TweetLegacyEntityMediaKind::Video
| data::TweetLegacyEntityMediaKind::AnimatedGif => {
Expand All @@ -530,9 +534,11 @@ fn parse_tweet(tweet: data::Tweet) -> Post {
match video_info {
Some(video_info) => PostAttachment::Video(PostAttachmentVideo {
media_url: video_info.url,
has_spoiler: possibly_sensitive,
}),
None => PostAttachment::Image(PostAttachmentImage {
media_url: media.media_url_https,
has_spoiler: possibly_sensitive,
}),
}
}
Expand Down

0 comments on commit 85e5c3e

Please sign in to comment.