mirror of
https://github.com/teloxide/teloxide.git
synced 2024-12-22 22:46:39 +01:00
Fixed ChatPermissions
bug and improved trace Settings
updated CHANGELOG.md
This commit is contained in:
parent
9262583640
commit
98cc02c9ce
3 changed files with 52 additions and 54 deletions
|
@ -56,6 +56,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
|
||||||
- High CPU usage on network errors ([PR 1002](https://github.com/teloxide/teloxide/pull/1002), [Issue 780](https://github.com/teloxide/teloxide/issues/780))
|
- High CPU usage on network errors ([PR 1002](https://github.com/teloxide/teloxide/pull/1002), [Issue 780](https://github.com/teloxide/teloxide/issues/780))
|
||||||
- Fix app build errors when using items gated behind sqlite-storage with the feature sqlite-storage-rustls ([PR 1018](https://github.com/teloxide/teloxide/pull/1018))
|
- Fix app build errors when using items gated behind sqlite-storage with the feature sqlite-storage-rustls ([PR 1018](https://github.com/teloxide/teloxide/pull/1018))
|
||||||
- Fix typo in `ApiError::ToMuchMessages` variant (rename it to `TooMuchMessages`) ([PR 1046](https://github.com/teloxide/teloxide/pull/1046))
|
- Fix typo in `ApiError::ToMuchMessages` variant (rename it to `TooMuchMessages`) ([PR 1046](https://github.com/teloxide/teloxide/pull/1046))
|
||||||
|
- Fix `ChatPermission` behavior to accurately reflect Telegram's functionality ([PR 1068](https://github.com/teloxide/teloxide/pull/1068))
|
||||||
|
|
||||||
### Changed
|
### Changed
|
||||||
|
|
||||||
|
|
|
@ -66,20 +66,20 @@ bitflags::bitflags! {
|
||||||
/// ```
|
/// ```
|
||||||
pub struct Settings: u8 {
|
pub struct Settings: u8 {
|
||||||
/// Trace requests (only request kind, e.g. `send_message`)
|
/// Trace requests (only request kind, e.g. `send_message`)
|
||||||
const TRACE_REQUESTS = 0b00000001;
|
const TRACE_REQUESTS = 1;
|
||||||
|
|
||||||
/// Trace requests verbosely (with all parameters).
|
/// Trace requests verbosely (with all parameters).
|
||||||
///
|
///
|
||||||
/// Implies [`TRACE_REQUESTS`]
|
/// Implies [`TRACE_REQUESTS`]
|
||||||
const TRACE_REQUESTS_VERBOSE = 0b00000011;
|
const TRACE_REQUESTS_VERBOSE = (1 << 1) | Self::TRACE_REQUESTS.bits;
|
||||||
|
|
||||||
/// Trace responses (only request kind, e.g. `send_message`)
|
/// Trace responses (only request kind, e.g. `send_message`)
|
||||||
const TRACE_RESPONSES = 0b00000100;
|
const TRACE_RESPONSES = 1 << 2;
|
||||||
|
|
||||||
/// Trace responses verbosely (with full response).
|
/// Trace responses verbosely (with full response).
|
||||||
///
|
///
|
||||||
/// Implies [`TRACE_RESPONSES`]
|
/// Implies [`TRACE_RESPONSES`]
|
||||||
const TRACE_RESPONSES_VERBOSE = 0b00001100;
|
const TRACE_RESPONSES_VERBOSE = (1 << 3) | Self::TRACE_RESPONSES.bits;
|
||||||
|
|
||||||
/// Trace everything.
|
/// Trace everything.
|
||||||
///
|
///
|
||||||
|
|
|
@ -30,84 +30,67 @@ bitflags::bitflags! {
|
||||||
/// assert!(permissions_v1.contains(ChatPermissions::INVITE_USERS));
|
/// assert!(permissions_v1.contains(ChatPermissions::INVITE_USERS));
|
||||||
/// assert!(permissions_v1.contains(ChatPermissions::SEND_VIDEOS));
|
/// assert!(permissions_v1.contains(ChatPermissions::SEND_VIDEOS));
|
||||||
///
|
///
|
||||||
/// // Implied by `SEND_VIDEOS`
|
|
||||||
/// assert!(permissions_v1.contains(ChatPermissions::SEND_MESSAGES));
|
|
||||||
///
|
|
||||||
/// // Difference, remove permissions
|
/// // Difference, remove permissions
|
||||||
/// let permissions_v2 = permissions_v1 - ChatPermissions::SEND_VIDEOS;
|
/// let permissions_v2 = permissions_v1 - ChatPermissions::SEND_VIDEOS;
|
||||||
/// assert!(!permissions_v2.contains(ChatPermissions::SEND_VIDEOS));
|
/// assert!(!permissions_v2.contains(ChatPermissions::SEND_VIDEOS));
|
||||||
///
|
|
||||||
/// // Removing `SEND_VIDEOS` also removes `SEND_MESSAGES` and vice versa
|
|
||||||
/// // because `SEND_MESSAGES` is implied by `SEND_VIDEOS`
|
|
||||||
/// assert!(!permissions_v2.contains(ChatPermissions::SEND_MESSAGES));
|
|
||||||
///
|
|
||||||
/// let permissions_v3 = permissions_v1 - ChatPermissions::SEND_MESSAGES;
|
|
||||||
/// assert!(!permissions_v3.contains(ChatPermissions::SEND_VIDEOS));
|
|
||||||
/// ```
|
/// ```
|
||||||
#[derive(Serialize, Deserialize)]
|
#[derive(Serialize, Deserialize)]
|
||||||
#[serde(from = "ChatPermissionsRaw", into = "ChatPermissionsRaw")]
|
#[serde(from = "ChatPermissionsRaw", into = "ChatPermissionsRaw")]
|
||||||
pub struct ChatPermissions: u16 {
|
pub struct ChatPermissions: u16 {
|
||||||
/// Set if the user is allowed to send text messages, contacts,
|
/// Set if the user is allowed to send text messages, contacts,
|
||||||
/// locations and venues.
|
/// giveaways, giveaway winners, invoices, locations and venues
|
||||||
const SEND_MESSAGES = 1;
|
const SEND_MESSAGES = 1;
|
||||||
|
|
||||||
/// Set if the user is allowed to send polls, implies
|
/// Set if the user is allowed to send polls
|
||||||
/// `SEND_MESSAGES`.
|
const SEND_POLLS = 1 << 1;
|
||||||
const SEND_POLLS = (1 << 2) | Self::SEND_MESSAGES.bits;
|
|
||||||
|
|
||||||
/// Set if the user is allowed to send animations, games, stickers and
|
/// Set if the user is allowed to send animations, games, stickers and
|
||||||
/// use inline bots, implies `SEND_MEDIA_MESSAGES`.
|
/// use inline bots.
|
||||||
const SEND_OTHER_MESSAGES = (1 << 3);
|
const SEND_OTHER_MESSAGES = 1 << 2;
|
||||||
|
|
||||||
/// Set if the user is allowed to add web page previews to
|
/// Set if the user is allowed to add web page previews to
|
||||||
/// their messages, implies `SEND_MEDIA_MESSAGES`.
|
/// their messages.
|
||||||
const ADD_WEB_PAGE_PREVIEWS = (1 << 4);
|
const ADD_WEB_PAGE_PREVIEWS = 1 << 3;
|
||||||
|
|
||||||
/// Set if the user is allowed to change the chat title, photo and
|
/// Set if the user is allowed to change the chat title, photo and
|
||||||
/// other settings. Ignored in public supergroups.
|
/// other settings. Ignored in public supergroups.
|
||||||
const CHANGE_INFO = (1 << 5);
|
const CHANGE_INFO = 1 << 4;
|
||||||
|
|
||||||
/// Set if the user is allowed to invite new users to the chat.
|
/// Set if the user is allowed to invite new users to the chat.
|
||||||
const INVITE_USERS = (1 << 6);
|
const INVITE_USERS = 1 << 5;
|
||||||
|
|
||||||
/// Set if the user is allowed to pin messages. Ignored in public
|
/// Set if the user is allowed to pin messages. Ignored in public
|
||||||
/// supergroups.
|
/// supergroups.
|
||||||
const PIN_MESSAGES = (1 << 7);
|
const PIN_MESSAGES = 1 << 6;
|
||||||
|
|
||||||
/// Set if the user is allowed to create, rename, close, and reopen forum topics.
|
/// Set if the user is allowed to create, rename, close, and reopen forum topics.
|
||||||
const MANAGE_TOPICS = (1 << 8);
|
const MANAGE_TOPICS = 1 << 7;
|
||||||
|
|
||||||
/// Set if the user is allowed to send audios. implies
|
/// Set if the user is allowed to send audios.
|
||||||
/// `SEND_MESSAGES`.
|
const SEND_AUDIOS = 1 << 8;
|
||||||
const SEND_AUDIOS = (1 << 9) | Self::SEND_MESSAGES.bits;
|
|
||||||
|
|
||||||
/// Set if the user is allowed to send documents. implies
|
/// Set if the user is allowed to send documents.
|
||||||
/// `SEND_MESSAGES`.
|
const SEND_DOCUMENTS = 1 << 9;
|
||||||
const SEND_DOCUMENTS = (1 << 10) | Self::SEND_MESSAGES.bits;
|
|
||||||
|
|
||||||
/// Set if the user is allowed to send photos. implies
|
/// Set if the user is allowed to send photos.
|
||||||
/// `SEND_MESSAGES`.
|
const SEND_PHOTOS = 1 << 10;
|
||||||
const SEND_PHOTOS = (1 << 11) | Self::SEND_MESSAGES.bits;
|
|
||||||
|
|
||||||
/// Set if the user is allowed to send videos. implies
|
/// Set if the user is allowed to send videos.
|
||||||
/// `SEND_MESSAGES`.
|
const SEND_VIDEOS = 1 << 11;
|
||||||
const SEND_VIDEOS = (1 << 12) | Self::SEND_MESSAGES.bits;
|
|
||||||
|
|
||||||
/// Set if the user is allowed to send video notes. implies
|
/// Set if the user is allowed to send video notes.
|
||||||
/// `SEND_MESSAGES`.
|
const SEND_VIDEO_NOTES = 1 << 12;
|
||||||
const SEND_VIDEO_NOTES = (1 << 13) | Self::SEND_MESSAGES.bits;
|
|
||||||
|
|
||||||
/// Set if the user is allowed to send voice notes. implies
|
/// Set if the user is allowed to send voice notes. implies
|
||||||
/// `SEND_MESSAGES`.
|
/// `SEND_MESSAGES`.
|
||||||
const SEND_VOICE_NOTES = (1 << 14) | Self::SEND_MESSAGES.bits;
|
const SEND_VOICE_NOTES = 1 << 13;
|
||||||
|
|
||||||
/// Set if the user is allowed to send audios, documents,
|
/// Set if the user is allowed to send audios, documents,
|
||||||
/// photos, videos, video notes and voice notes, implies
|
/// photos, videos, video notes and voice notes, implies
|
||||||
/// `SEND_MESSAGES`, `SEND_AUDIOS`, `SEND_DOCUMENTS`,
|
/// `SEND_AUDIOS`, `SEND_DOCUMENTS`, `SEND_PHOTOS`,
|
||||||
/// `SEND_PHOTOS`, `SEND_VIDEOS`, `SEND_VIDEO_NOTES` and `SEND_VOICE_NOTES`.
|
/// `SEND_VIDEOS`, `SEND_VIDEO_NOTES` and `SEND_VOICE_NOTES`.
|
||||||
/// Note: this is not a separate permission on it's own, this is just a alias for all the permissions mentioned.
|
/// Note: this is not a separate permission on it's own, this is just a alias for all the permissions mentioned.
|
||||||
const SEND_MEDIA_MESSAGES = Self::SEND_MESSAGES.bits
|
const SEND_MEDIA_MESSAGES = Self::SEND_AUDIOS.bits
|
||||||
| Self::SEND_AUDIOS.bits
|
|
||||||
| Self::SEND_DOCUMENTS.bits
|
| Self::SEND_DOCUMENTS.bits
|
||||||
| Self::SEND_PHOTOS.bits
|
| Self::SEND_PHOTOS.bits
|
||||||
| Self::SEND_VIDEOS.bits
|
| Self::SEND_VIDEOS.bits
|
||||||
|
@ -278,12 +261,12 @@ impl From<ChatPermissions> for ChatPermissionsRaw {
|
||||||
fn from(this: ChatPermissions) -> Self {
|
fn from(this: ChatPermissions) -> Self {
|
||||||
Self {
|
Self {
|
||||||
can_send_messages: this.can_send_messages(),
|
can_send_messages: this.can_send_messages(),
|
||||||
can_send_audios: this.contains(ChatPermissions::SEND_AUDIOS),
|
can_send_audios: this.can_send_audios(),
|
||||||
can_send_documents: this.contains(ChatPermissions::SEND_DOCUMENTS),
|
can_send_documents: this.can_send_documents(),
|
||||||
can_send_photos: this.contains(ChatPermissions::SEND_PHOTOS),
|
can_send_photos: this.can_send_photos(),
|
||||||
can_send_videos: this.contains(ChatPermissions::SEND_VIDEOS),
|
can_send_videos: this.can_send_videos(),
|
||||||
can_send_video_notes: this.contains(ChatPermissions::SEND_VIDEO_NOTES),
|
can_send_video_notes: this.can_send_video_notes(),
|
||||||
can_send_voice_notes: this.contains(ChatPermissions::SEND_VOICE_NOTES),
|
can_send_voice_notes: this.can_send_voice_notes(),
|
||||||
can_send_polls: this.can_send_polls(),
|
can_send_polls: this.can_send_polls(),
|
||||||
can_send_other_messages: this.can_send_other_messages(),
|
can_send_other_messages: this.can_send_other_messages(),
|
||||||
can_add_web_page_previews: this.can_add_web_page_previews(),
|
can_add_web_page_previews: this.can_add_web_page_previews(),
|
||||||
|
@ -370,7 +353,9 @@ mod tests {
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn serialization() {
|
fn serialization() {
|
||||||
let permissions = ChatPermissions::SEND_AUDIOS | ChatPermissions::PIN_MESSAGES;
|
let permissions = ChatPermissions::SEND_MESSAGES
|
||||||
|
| ChatPermissions::SEND_AUDIOS
|
||||||
|
| ChatPermissions::PIN_MESSAGES;
|
||||||
let expected = r#"{"can_send_messages":true,"can_send_audios":true,"can_pin_messages":true,"can_manage_topics":false}"#;
|
let expected = r#"{"can_send_messages":true,"can_send_audios":true,"can_pin_messages":true,"can_manage_topics":false}"#;
|
||||||
let actual = serde_json::to_string(&permissions).unwrap();
|
let actual = serde_json::to_string(&permissions).unwrap();
|
||||||
assert_eq!(expected, actual);
|
assert_eq!(expected, actual);
|
||||||
|
@ -379,8 +364,20 @@ mod tests {
|
||||||
#[test]
|
#[test]
|
||||||
fn deserialization() {
|
fn deserialization() {
|
||||||
let json = r#"{"can_send_messages":true,"can_send_photos":true,"can_pin_messages":true}"#;
|
let json = r#"{"can_send_messages":true,"can_send_photos":true,"can_pin_messages":true}"#;
|
||||||
let expected = ChatPermissions::SEND_PHOTOS | ChatPermissions::PIN_MESSAGES;
|
let expected = ChatPermissions::SEND_MESSAGES
|
||||||
|
| ChatPermissions::SEND_PHOTOS
|
||||||
|
| ChatPermissions::PIN_MESSAGES;
|
||||||
let actual = serde_json::from_str(json).unwrap();
|
let actual = serde_json::from_str(json).unwrap();
|
||||||
assert_eq!(expected, actual);
|
assert_eq!(expected, actual);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn modfiy_permission() {
|
||||||
|
let before = ChatPermissions::SEND_MESSAGES
|
||||||
|
| ChatPermissions::SEND_PHOTOS
|
||||||
|
| ChatPermissions::SEND_AUDIOS;
|
||||||
|
let after = before - ChatPermissions::SEND_MESSAGES;
|
||||||
|
let expected = ChatPermissions::SEND_PHOTOS | ChatPermissions::SEND_AUDIOS;
|
||||||
|
assert_eq!(after, expected);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue