Merge branch 'master' of https://github.com/teloxide/teloxide into fix-hide-attr-bug-work-as-MetaNameValueStr

This commit is contained in:
TheAwiteb 2023-09-09 04:27:38 +03:00
commit 9149dc244e
No known key found for this signature in database
GPG key ID: ABF818BD15DC2D34
6 changed files with 85 additions and 16 deletions

View file

@ -16,9 +16,13 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
- `Seconds` type, which represents a duration is seconds ([#859][pr859])
- `VideoChatEnded::duration` field that was previously missed ([#859][pr859])
- `ThreadId` newtype over `MessageId`, used for identifying reply threads ([#887][pr887])
- `ChatId::as_user` ([#905][pr905])
- Implement `PartialEq<ChatId> for UserId` and `PartialEq<UserId> for ChatId` ([#905][pr905])
- `ChatId::{MIN, MAX}` ([#905][pr905])
[pr851]: https://github.com/teloxide/teloxide/pull/851
[pr887]: https://github.com/teloxide/teloxide/pull/887
[pr905]: https://github.com/teloxide/teloxide/pull/905
### Fixed

View file

@ -72,7 +72,7 @@ takecell = "0.1"
take_mut = "0.2"
rc-box = "1.1.1"
never = "0.1.0"
chrono = { version = "0.4.19", default-features = false }
chrono = { version = "0.4.30", default-features = false }
either = "1.6.1"
bitflags = { version = "1.2" }

View file

@ -269,7 +269,7 @@ pub(crate) fn serde_timestamp<E: serde::de::Error>(
NaiveDateTime::from_timestamp_opt(timestamp, 0)
.ok_or_else(|| E::custom("invalid timestump"))
.map(|naive| DateTime::from_utc(naive, Utc))
.map(|naive| DateTime::from_naive_utc_and_offset(naive, Utc))
}
pub(crate) mod serde_opt_date_from_unix_timestamp {
@ -305,8 +305,10 @@ pub(crate) mod serde_opt_date_from_unix_timestamp {
{
let json = r#"{"date":1}"#;
let expected =
DateTime::from_utc(chrono::NaiveDateTime::from_timestamp_opt(1, 0).unwrap(), Utc);
let expected = DateTime::from_naive_utc_and_offset(
chrono::NaiveDateTime::from_timestamp_opt(1, 0).unwrap(),
Utc,
);
let Struct { date } = serde_json::from_str(json).unwrap();
assert_eq!(date, Some(expected));

View file

@ -50,6 +50,15 @@ impl ChatId {
matches!(self.to_bare(), BareChatId::Channel(_))
}
/// Returns user id, if this is an id of a user.
#[must_use]
pub fn as_user(self) -> Option<UserId> {
match self.to_bare() {
BareChatId::User(u) => Some(u),
BareChatId::Group(_) | BareChatId::Channel(_) => None,
}
}
/// Converts this id to "bare" MTProto peer id.
///
/// See [`BareChatId`] for more.
@ -73,6 +82,12 @@ impl From<UserId> for ChatId {
}
}
impl PartialEq<UserId> for ChatId {
fn eq(&self, other: &UserId) -> bool {
self.is_user() && *self == ChatId::from(*other)
}
}
impl BareChatId {
/// Converts bare chat id back to normal bot API [`ChatId`].
#[allow(unused)]
@ -92,8 +107,8 @@ const MIN_MARKED_CHANNEL_ID: i64 = -1997852516352;
const MAX_MARKED_CHANNEL_ID: i64 = -1000000000000;
const MIN_MARKED_CHAT_ID: i64 = MAX_MARKED_CHANNEL_ID + 1;
const MAX_MARKED_CHAT_ID: i64 = MIN_USER_ID - 1;
const MIN_USER_ID: i64 = 0;
const MAX_USER_ID: i64 = (1 << 40) - 1;
pub(crate) const MIN_USER_ID: i64 = 0;
pub(crate) const MAX_USER_ID: i64 = (1 << 40) - 1;
#[cfg(test)]
mod tests {
@ -143,4 +158,16 @@ mod tests {
fn display() {
assert_eq!(ChatId(1).to_string(), "1");
}
#[test]
fn user_id_eq() {
assert_eq!(ChatId(12), UserId(12));
assert_eq!(ChatId(4652762), UserId(4652762));
assert_ne!(ChatId(17), UserId(42));
// The user id is not well formed, so even though `-1 == max` is true,
// we don't want user id to match
assert_eq!(-1i64, u64::MAX as i64);
assert_ne!(ChatId(-1), UserId(u64::MAX));
}
}

View file

@ -155,16 +155,35 @@ impl Update {
/// replies, pinned messages, message entities, "via bot" fields and more.
/// Also note that this function can return duplicate users.
pub fn mentioned_users(&self) -> impl Iterator<Item = &User> {
use either::Either::{Left, Right};
use either::Either::{Left as L, Right as R};
use std::iter::{empty, once};
let i0 = Left;
let i1 = |x| Right(Left(x));
let i2 = |x| Right(Right(Left(x)));
let i3 = |x| Right(Right(Right(Left(x))));
let i4 = |x| Right(Right(Right(Right(Left(x)))));
let i5 = |x| Right(Right(Right(Right(Right(Left(x))))));
let i6 = |x| Right(Right(Right(Right(Right(Right(x))))));
// [root]
// / \
// left - / \ - right
// / \
// /\ /\
// / \ / \
// / \ / \
// 0 /\ /\ /\
// / \ / \ / \
// 1 2 3 4 5 6
//
// 0 = LL
// 1 = LRL
// 2 = LRR
// 3 = RLL
// 4 = RLR
// 5 = RRL
// 6 = RRR
let i0 = |x| L(L(x));
let i1 = |x| L(R(L(x)));
let i2 = |x| L(R(R(x)));
let i3 = |x| R(L(L(x)));
let i4 = |x| R(L(R(x)));
let i5 = |x| R(R(L(x)));
let i6 = |x| R(R(R(x)));
match &self.kind {
UpdateKind::Message(message)
@ -376,8 +395,10 @@ mod test {
#[test]
fn message() {
let timestamp = 1_569_518_342;
let date =
DateTime::from_utc(NaiveDateTime::from_timestamp_opt(timestamp, 0).unwrap(), Utc);
let date = DateTime::from_naive_utc_and_offset(
NaiveDateTime::from_timestamp_opt(timestamp, 0).unwrap(),
Utc,
);
let json = r#"{
"update_id":892252934,

View file

@ -1,5 +1,7 @@
use serde::{Deserialize, Serialize};
use crate::types::{ChatId, MAX_USER_ID, MIN_USER_ID};
/// Identifier of a user.
#[derive(Clone, Copy)]
#[derive(Debug, derive_more::Display)]
@ -50,6 +52,19 @@ impl UserId {
self == TELEGRAM_USER_ID
}
/// The smallest user id that could possibly be returned by Telegram.
pub const MIN: Self = Self(MIN_USER_ID as u64);
/// The largest user id that could possibly be returned by Telegram.
pub const MAX: Self = Self(MAX_USER_ID as u64);
}
impl PartialEq<ChatId> for UserId {
fn eq(&self, other: &ChatId) -> bool {
// Reuse `PartialEq<UserId> for ChatId` impl
other == self
}
}
#[cfg(test)]