Skip to content

Commit

Permalink
feat(filters): add administrator filter
Browse files Browse the repository at this point in the history
  • Loading branch information
AndrielFR committed Jan 28, 2025
1 parent d627ed7 commit a0f93f8
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 4 deletions.
46 changes: 46 additions & 0 deletions lib/ferogram/src/filters/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -427,6 +427,52 @@ pub async fn forwarded(_: Client, update: Update) -> Flow {
flow::break_now()
}

/// Pass if the message or callback query is sent by an administrator.
pub async fn administrator(client: Client, update: Update) -> Flow {
let chat;
let sender;

match update {
Update::NewMessage(message) | Update::MessageEdited(message) => {
chat = message.chat();
sender = message.sender();
}
Update::CallbackQuery(query) => {
chat = query.chat().clone();
sender = Some(query.sender().clone());
}
_ => return flow::break_now(),
}

match chat {
Chat::User(_) => return flow::continue_now(),
_ => {
if let Some(sender) = sender {
if let Ok(tl::enums::channels::ChannelParticipant::Participant(
channel_participant,
)) = client
.invoke(&tl::functions::channels::GetParticipant {
channel: chat
.pack()
.try_to_input_channel()
.expect("Invalid input channel"),
participant: sender.pack().to_input_peer(),
})
.await
{
match channel_participant.participant {
tl::enums::ChannelParticipant::Admin(_)
| tl::enums::ChannelParticipant::Creator(_) => return flow::continue_now(),
_ => return flow::break_now(),
}
}
}
}
}

flow::break_now()
}

/// Pass if the chat is private.
///
/// Injects `Chat`: private chat.
Expand Down
11 changes: 7 additions & 4 deletions lib/ferogram/src/filters/or.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,14 +21,17 @@ pub struct Or {
impl Filter for Or {
async fn check(&mut self, client: Client, update: Update) -> Flow {
let first_flow = self.first.check(client.clone(), update.clone()).await;
let other_flow = self.other.check(client, update).await;

if first_flow.is_continue() {
first_flow
} else if other_flow.is_continue() {
other_flow
} else {
flow::break_now()
let other_flow = self.other.check(client, update).await;

if other_flow.is_continue() {
other_flow
} else {
flow::break_now()
}
}
}
}

0 comments on commit a0f93f8

Please sign in to comment.