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(ffi): generate formatted captions for send_* media fns #4261

Merged
merged 2 commits into from
Nov 14, 2024

Conversation

jmartinesp
Copy link
Contributor

@jmartinesp jmartinesp commented Nov 13, 2024

At the moment we could only pass existing caption and formatted_caption parameters, assuming they were either plain text for caption or already parsed for formatted_caption. With these changes, the caption parameter is now treated as MD and transformed to HTML which will be used as formatted_caption if none is provided.

Bonus screenshot:

image
  • Public API changes documented in changelogs (optional)

Signed-off-by:

@jmartinesp jmartinesp requested a review from a team as a code owner November 13, 2024 15:35
@jmartinesp jmartinesp requested review from poljar and removed request for a team November 13, 2024 15:35
Changelog: For `Timeline::send_*` fns, treat the passed `caption` parameter as markdown and use the HTML generated from it as the `formatted_caption` if there is none.
Copy link

codecov bot commented Nov 13, 2024

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 84.91%. Comparing base (aca83fb) to head (5d77f32).
Report is 2 commits behind head on main.

Additional details and impacted files
@@            Coverage Diff             @@
##             main    #4261      +/-   ##
==========================================
- Coverage   84.92%   84.91%   -0.01%     
==========================================
  Files         274      274              
  Lines       29805    29805              
==========================================
- Hits        25311    25309       -2     
- Misses       4494     4496       +2     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

/// - Return `None` if there are no `caption` or `formatted_caption` parameters.
fn formatted_caption_from(
caption: &Option<String>,
formatted_caption: &Option<FormattedBody>,
Copy link
Member

Choose a reason for hiding this comment

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

nit: don't pass formatted_caption by ref, since it must be consumed by this function; avoids a clone() below

/// return the result.
/// - Return `None` if there are no `caption` or `formatted_caption` parameters.
fn formatted_caption_from(
caption: &Option<String>,
Copy link
Member

Choose a reason for hiding this comment

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

nit: use Option<&str> and have the caller use .deref() here

Comment on lines 730 to 731
(Some(body), None) => RumaFormattedBody::markdown(body),
(_, Some(formatted_body)) => Some(formatted_body.clone().into()),
Copy link
Member

Choose a reason for hiding this comment

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

I would invert those two branches, otherwise a body and a formatted_body will result in the body overriding the formatted body 🤪

…This needs a test 😁

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I would invert those two branches, otherwise a body and a formatted_body will result in the body overriding the formatted body 🤪

I don't mind, but can't the branch that overrides only happen if there is no formatted body (body -> Some, formatted_body -> None)? Maybe I misunderstood how this pattern matching of tuples work.

…This needs a test 😁

I guess I can move this into the SDK crate then.

Copy link
Member

Choose a reason for hiding this comment

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

Ah you're right, sorry I misread that. That would then be clearer as a series of if let some return, maybe?

Copy link
Contributor

Choose a reason for hiding this comment

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

Doesn't matter much but I find the match much clearer, every single branch is neatly presented to you and the compiler ensures that all the branches are handled.

A series of if let Some return let's you skip branches and then I need to spend more time investigating which branches are skipped if any.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Doesn't matter much but I find the match much clearer

I just changed it 😭 .

Copy link
Contributor

Choose a reason for hiding this comment

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

Ah sorry, though I don't really think you need to change it back. I'm just saying that using a match like that is completely fine and even preferred by me.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I also found it more expressive. Since it states all 3 possible causes and the states of the variables for those. Anyway, it's not a big deal as you said.

Copy link
Member

Choose a reason for hiding this comment

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

I was definitely confused and misread it, I do find the new code much much clearer since it's in-your-face at the beginning that the formatted body is preferred over anything else, so YMMV, heh :)

@bnjbvr bnjbvr removed the request for review from poljar November 13, 2024 16:18
@jmartinesp
Copy link
Contributor Author

It seems like, if I enable the markdown feature as default for the main SDK crate I also need to add it for testing for it to work.

Copy link
Member

@bnjbvr bnjbvr left a comment

Choose a reason for hiding this comment

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

Thanks for the tests!

Comment on lines 730 to 731
(Some(body), None) => RumaFormattedBody::markdown(body),
(_, Some(formatted_body)) => Some(formatted_body.clone().into()),
Copy link
Member

Choose a reason for hiding this comment

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

I was definitely confused and misread it, I do find the new code much much clearer since it's in-your-face at the beginning that the formatted body is preferred over anything else, so YMMV, heh :)

Comment on lines 224 to 227
/// - If a `formatted_body` exists, return it.
/// - If it doesn't exist but there is a `body`, parse it as markdown and return
/// the result.
/// - Return `None` if there are no `body` or `formatted_body` parameters.
Copy link
Member

Choose a reason for hiding this comment

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

Think this comment could be more concise: "return the formatted body if available, or interpret the body as markdown, if provided."

Comment on lines 234 to 237
} else if let Some(body) = body {
FormattedBody::markdown(body)
} else {
None
}
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
} else if let Some(body) = body {
FormattedBody::markdown(body)
} else {
None
}
} else {
body.map(FormattedBody::markdown)
}

@jmartinesp jmartinesp force-pushed the feat/parse-md-for-sending-media-captions branch 2 times, most recently from f6c6fc1 to f76d83d Compare November 14, 2024 09:14
Add the `markdown` feature to the SDK crate, otherwise we can't use `FormattedBody::markdown`.

Refactor the pattern matching into an if, add tests to check its behaviour.
@jmartinesp jmartinesp force-pushed the feat/parse-md-for-sending-media-captions branch from f76d83d to 5d77f32 Compare November 14, 2024 09:23
@jmartinesp jmartinesp enabled auto-merge (rebase) November 14, 2024 09:36
@jmartinesp jmartinesp merged commit d614878 into main Nov 14, 2024
40 checks passed
@jmartinesp jmartinesp deleted the feat/parse-md-for-sending-media-captions branch November 14, 2024 09:38
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants