Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import org.telegram.telegrambots.meta.api.methods.groupadministration.GetChatAdministrators;
import org.telegram.telegrambots.meta.api.methods.send.SendMessage;
import org.telegram.telegrambots.meta.api.objects.Chat;
import org.telegram.telegrambots.meta.api.objects.chatmember.ChatMember;
import org.telegram.telegrambots.meta.api.objects.Message;
import org.telegram.telegrambots.meta.bots.AbsSender;
Expand Down Expand Up @@ -51,6 +52,11 @@ private boolean isReplyAuthor(Message message) {
}

private boolean isChatAdmin(Message message, AbsSender sender) throws TelegramApiException {
Chat chat = message.getChat();
if (chat != null && chat.isUserChat()) {
return true;
}

GetChatAdministrators getChatAdministrators = new GetChatAdministrators();
getChatAdministrators.setChatId(message.getChatId());

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import org.mockito.Mock;
import org.mockito.junit.MockitoJUnitRunner;
import org.telegram.telegrambots.meta.api.methods.groupadministration.GetChatAdministrators;
import org.telegram.telegrambots.meta.api.objects.Chat;
import org.telegram.telegrambots.meta.api.objects.Message;
import org.telegram.telegrambots.meta.api.objects.User;
import org.telegram.telegrambots.meta.api.objects.chatmember.ChatMember;
Expand Down Expand Up @@ -75,6 +76,19 @@ public void chatAdminCanCancelAnotherAuthorTask() throws TelegramApiException {
verify(messageSchedulerService).cancelTaskByChatAndMessage(CHAT_ID, TASK_MESSAGE_ID);
}

@Test
public void personalChatUserCanCancelAnotherAuthorTask() throws TelegramApiException {
setMessageAuthor(111L);
setReplyAuthor(999L);
setPersonalChat();
when(messageSchedulerService.cancelTaskByChatAndMessage(CHAT_ID, TASK_MESSAGE_ID)).thenReturn(true);

assertTrue(handler.handle(message, "", sender));

verify(messageSchedulerService).cancelTaskByChatAndMessage(CHAT_ID, TASK_MESSAGE_ID);
verify(sender, never()).execute(any(GetChatAdministrators.class));
}

@Test
public void nonAdminCanNotCancelAnotherAuthorTask() throws TelegramApiException {
setMessageAuthor(111L);
Expand All @@ -86,6 +100,12 @@ public void nonAdminCanNotCancelAnotherAuthorTask() throws TelegramApiException
verify(messageSchedulerService, never()).cancelTaskByChatAndMessage(anyLong(), anyInt());
}

private void setPersonalChat() {
Chat chat = mock(Chat.class);
when(chat.isUserChat()).thenReturn(true);
when(message.getChat()).thenReturn(chat);
}

private void setMessageAuthor(Long userId) {
when(message.getFrom()).thenReturn(user(userId));
}
Expand Down
Loading