Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: forceSticker viewtype #4814

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 6 additions & 1 deletion deltachat-jsonrpc/src/api/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1750,10 +1750,15 @@ impl CommandApi {
account_id: u32,
chat_id: u32,
sticker_path: String,
force: bool,
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This could also be a new function to not break desktop, but I think this adjustment is fine @Simon-Laux

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yeah as long as it is mentioned as breaking api change in the changelog

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Another option is to always force viewtype in JSON-RPC as we do not need heuristics on desktop currently.

) -> Result<u32> {
let ctx = self.get_context(account_id).await?;

let mut msg = Message::new(Viewtype::Sticker);
let mut msg = Message::new(if force {
Viewtype::ForceSticker
} else {
Viewtype::Sticker
});
msg.set_file(&sticker_path, None);

let message_id = deltachat::chat::send_msg(&ctx, ChatId::new(chat_id), &mut msg).await?;
Expand Down
9 changes: 9 additions & 0 deletions deltachat-jsonrpc/src/api/types/message.rs
Original file line number Diff line number Diff line change
Expand Up @@ -147,6 +147,7 @@ impl MessageObject {
image: if quote.get_viewtype() == Viewtype::Image
|| quote.get_viewtype() == Viewtype::Gif
|| quote.get_viewtype() == Viewtype::Sticker
|| quote.get_viewtype() == Viewtype::ForceSticker
{
match quote.get_file(context) {
Some(path_buf) => path_buf.to_str().map(|s| s.to_owned()),
Expand Down Expand Up @@ -257,6 +258,12 @@ pub enum MessageViewtype {
/// A click on a sticker will offer to install the sticker set in some future.
Sticker,

/// Message containing a sticker, similar to image.
/// If possible, the ui should display the image without borders in a transparent way.
/// A click on a sticker will offer to install the sticker set in some future.
/// This stick is guaranteed to be displayed as a stick in the ui.
ForceSticker,

/// Message containing an Audio file.
Audio,

Expand Down Expand Up @@ -285,6 +292,7 @@ impl From<Viewtype> for MessageViewtype {
Viewtype::Image => MessageViewtype::Image,
Viewtype::Gif => MessageViewtype::Gif,
Viewtype::Sticker => MessageViewtype::Sticker,
Viewtype::ForceSticker => MessageViewtype::ForceSticker,
Viewtype::Audio => MessageViewtype::Audio,
Viewtype::Voice => MessageViewtype::Voice,
Viewtype::Video => MessageViewtype::Video,
Expand All @@ -303,6 +311,7 @@ impl From<MessageViewtype> for Viewtype {
MessageViewtype::Image => Viewtype::Image,
MessageViewtype::Gif => Viewtype::Gif,
MessageViewtype::Sticker => Viewtype::Sticker,
MessageViewtype::ForceSticker => Viewtype::ForceSticker,
MessageViewtype::Audio => Viewtype::Audio,
MessageViewtype::Voice => Viewtype::Voice,
MessageViewtype::Video => Viewtype::Video,
Expand Down
30 changes: 24 additions & 6 deletions src/chat.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2207,11 +2207,12 @@ async fn prepare_msg_blob(context: &Context, msg: &mut Message) -> Result<()> {
.await?
.with_context(|| format!("attachment missing for message of type #{}", msg.viewtype))?;

let mut maybe_sticker = msg.viewtype == Viewtype::Sticker;
let mut maybe_sticker =
msg.viewtype == Viewtype::Sticker || msg.viewtype == Viewtype::ForceSticker;
if msg.viewtype == Viewtype::Image || maybe_sticker {
blob.recode_to_image_size(context, &mut maybe_sticker)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd better pass viewtype here then. Otherwise it can be processed/recoded as an image while being a sticker

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should this function be called at all for stickers that cannot be images?

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think yes, it removes Exif from all files also

.await?;
if !maybe_sticker {
if !maybe_sticker && msg.viewtype != Viewtype::ForceSticker {
msg.viewtype = Viewtype::Image;
}
}
Expand Down Expand Up @@ -3573,7 +3574,7 @@ pub async fn forward_msgs(context: &Context, msg_ids: &[MsgId], chat_id: ChatId)
// by not marking own forwarded messages as such,
// however, this turned out to be to confusing and unclear.

if msg.get_viewtype() != Viewtype::Sticker {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Btw, why do we have this logic for stickers?

if msg.get_viewtype() != Viewtype::Sticker && msg.get_viewtype() != Viewtype::ForceSticker {
msg.param
.set_int(Param::Forwarded, src_msg_id.to_u32() as i32);
}
Expand Down Expand Up @@ -5618,6 +5619,7 @@ mod tests {
async fn test_sticker(
filename: &str,
bytes: &[u8],
viewtype: Viewtype,
res_viewtype: Viewtype,
w: i32,
h: i32,
Expand All @@ -5630,12 +5632,12 @@ mod tests {
let file = alice.get_blobdir().join(filename);
tokio::fs::write(&file, bytes).await?;

let mut msg = Message::new(Viewtype::Sticker);
let mut msg = Message::new(viewtype);
msg.set_file(file.to_str().unwrap(), None);

let sent_msg = alice.send_msg(alice_chat.id, &mut msg).await;
let mime = sent_msg.payload();
if res_viewtype == Viewtype::Sticker {
if res_viewtype == Viewtype::Sticker || res_viewtype == Viewtype::ForceSticker {
assert_eq!(mime.match_indices("Chat-Content: sticker").count(), 1);
}

Expand All @@ -5644,7 +5646,7 @@ mod tests {
assert_eq!(msg.get_viewtype(), res_viewtype);
let msg_filename = msg.get_filename().unwrap();
match res_viewtype {
Viewtype::Sticker => assert_eq!(msg_filename, filename),
Viewtype::Sticker | Viewtype::ForceSticker => assert_eq!(msg_filename, filename),
Viewtype::Image => assert!(msg_filename.starts_with("image_")),
_ => panic!("Not implemented"),
}
Expand All @@ -5661,6 +5663,7 @@ mod tests {
"sticker.png",
include_bytes!("../test-data/image/logo.png"),
Viewtype::Sticker,
Viewtype::Sticker,
135,
135,
)
Expand All @@ -5672,19 +5675,34 @@ mod tests {
test_sticker(
"sticker.jpg",
include_bytes!("../test-data/image/avatar1000x1000.jpg"),
Viewtype::Sticker,
Viewtype::Image,
1000,
1000,
)
.await
}

#[tokio::test(flavor = "multi_thread", worker_threads = 2)]
async fn test_sticker_jpeg_force() -> Result<()> {
test_sticker(
"sticker.jpg",
include_bytes!("../test-data/image/avatar1000x1000.jpg"),
Viewtype::ForceSticker,
Viewtype::Sticker,
1000,
1000,
)
.await
}

#[tokio::test(flavor = "multi_thread", worker_threads = 2)]
async fn test_sticker_gif() -> Result<()> {
test_sticker(
"sticker.gif",
include_bytes!("../test-data/image/logo.gif"),
Viewtype::Sticker,
Viewtype::Sticker,
135,
135,
)
Expand Down
7 changes: 7 additions & 0 deletions src/message.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1858,6 +1858,12 @@ pub enum Viewtype {
/// A click on a sticker will offer to install the sticker set in some future.
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd rather add a comment here that it can be converted to Image using some heuristics

Sticker = 23,

/// Message containing a sticker, similar to image.
/// If possible, the ui should display the image without borders in a transparent way.
/// A click on a sticker will offer to install the sticker set in some future.
/// This stick is guaranteed to be displayed as a stick in the ui.
ForceSticker = 24,

/// Message containing an Audio file.
/// File and duration are set via dc_msg_set_file(), dc_msg_set_duration()
/// and retrieved via dc_msg_get_file(), dc_msg_get_duration().
Expand Down Expand Up @@ -1898,6 +1904,7 @@ impl Viewtype {
Viewtype::Image => true,
Viewtype::Gif => true,
Viewtype::Sticker => true,
Viewtype::ForceSticker => true,
Viewtype::Audio => true,
Viewtype::Voice => true,
Viewtype::Video => true,
Expand Down
2 changes: 1 addition & 1 deletion src/mimefactory.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1118,7 +1118,7 @@ impl<'a> MimeFactory<'a> {
.push(Header::new("Chat-Group-Avatar".into(), filename_as_sent));
}

if self.msg.viewtype == Viewtype::Sticker {
if self.msg.viewtype == Viewtype::Sticker || self.msg.viewtype == Viewtype::ForceSticker {
headers
.protected
.push(Header::new("Chat-Content".into(), "sticker".into()));
Expand Down
1 change: 1 addition & 0 deletions src/mimeparser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -552,6 +552,7 @@ impl MimeMessage {
Viewtype::Image
| Viewtype::Gif
| Viewtype::Sticker
| Viewtype::ForceSticker
| Viewtype::Audio
| Viewtype::Voice
| Viewtype::Video
Expand Down
3 changes: 2 additions & 1 deletion src/summary.rs
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,7 @@ impl Summary {
let thumbnail_path = if msg.viewtype == Viewtype::Image
|| msg.viewtype == Viewtype::Gif
|| msg.viewtype == Viewtype::Sticker
|| msg.viewtype == Viewtype::ForceSticker
{
msg.get_file(context)
.and_then(|path| path.to_str().map(|p| p.to_owned()))
Expand Down Expand Up @@ -124,7 +125,7 @@ impl Message {
let prefix = match self.viewtype {
Viewtype::Image => stock_str::image(context).await,
Viewtype::Gif => stock_str::gif(context).await,
Viewtype::Sticker => stock_str::sticker(context).await,
Viewtype::Sticker | Viewtype::ForceSticker => stock_str::sticker(context).await,
Viewtype::Video => stock_str::video(context).await,
Viewtype::Voice => stock_str::voice_message(context).await,
Viewtype::Audio | Viewtype::File => {
Expand Down