Merge pull request #192 from teloxide/fix_unmark

Fix chat unmark
This commit is contained in:
Waffle Maybe 2022-03-23 03:46:44 +04:00 committed by GitHub
commit b9df9abe9b
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -23,18 +23,21 @@ impl ChatId {
pub(crate) fn unmark(&self) -> Option<UnmarkedChatId> {
use UnmarkedChatId::*;
const MAX_CHANNEL_ID: i64 = -(10i64.pow(12));
const MIN_CHANNEL_ID: i64 = MAX_CHANNEL_ID - (i32::MAX as i64);
const MAX_USER_ID: i64 = i32::MAX as _;
const MIN_CHAT_ID: i64 = -MAX_USER_ID;
// https://github.com/mtcute/mtcute/blob/6933ecc3f82dd2e9100f52b0afec128af564713b/packages/core/src/utils/peer-utils.ts#L4
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;
let res = match self {
&Self::Id(id @ MIN_CHAT_ID..=-1) => Chat(-id as _),
&Self::Id(id @ MIN_CHANNEL_ID..=MAX_CHANNEL_ID) => Channel((MAX_CHANNEL_ID - id) as _),
&Self::Id(id) => {
debug_assert!(0 < id && id < MAX_USER_ID, "malformed chat id: {}", id);
User(id as _)
&Self::Id(id @ MIN_MARKED_CHAT_ID..=MAX_MARKED_CHAT_ID) => Chat(-id as _),
&Self::Id(id @ MIN_MARKED_CHANNEL_ID..=MAX_MARKED_CHANNEL_ID) => {
Channel((MAX_MARKED_CHANNEL_ID - id) as _)
}
&Self::Id(id @ MIN_USER_ID..=MAX_USER_ID) => User(id as _),
&Self::Id(id) => panic!("malformed chat id: {}", id),
Self::ChannelUsername(_) => return None,
};
@ -43,9 +46,9 @@ impl ChatId {
}
pub(crate) enum UnmarkedChatId {
User(u32),
Chat(u32),
Channel(u32),
User(u64),
Chat(u64),
Channel(u64),
}
#[cfg(test)]
@ -68,4 +71,12 @@ mod tests {
assert_eq!(expected_json, actual_json)
}
#[test]
fn user_id_unmark() {
assert!(matches!(
ChatId::Id(5298363099).unmark(),
Some(UnmarkedChatId::User(5298363099))
));
}
}