Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature/jtelegraf/cleanup #40

Open
wants to merge 11 commits into
base: jtelegraf
Choose a base branch
from
5 changes: 5 additions & 0 deletions jtelegrambotapi-core/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,11 @@
<artifactId>okhttp</artifactId>
</dependency>

<dependency>
<groupId>com.google.code.findbugs</groupId>
<artifactId>jsr305</artifactId>
</dependency>

<!-- test dependencies -->

<dependency>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@
import com.jtelegram.api.inline.keyboard.InlineKeyboardRow;
import com.jtelegram.api.inline.result.framework.InlineResultType;
import com.jtelegram.api.message.entity.MessageEntity;
import com.jtelegram.api.message.entity.MessageEntityType;
import com.jtelegram.api.message.media.Photo;
import com.jtelegram.api.update.Update;
import com.jtelegram.api.update.UpdateProvider;
import com.jtelegram.api.update.UpdateType;
Expand Down Expand Up @@ -46,9 +48,11 @@ public class TelegramBotRegistry {
.registerTypeAdapter(InlineKeyboardRow.class, new InlineKeyboardRow.Serializer())
.registerTypeAdapter(ReplyKeyboardRow.class, new ReplyKeyboardRow.Serializer())
.registerTypeAdapter(Update.class, new Update.Deserializer())
.registerTypeAdapter(Photo.class, new Photo.Deserializer())
.registerTypeAdapter(Chat.class, new Chat.Deserializer())
.registerTypeAdapter(Message.class, new Message.Deserializer())
.registerTypeAdapter(MessageEntity.class, new MessageEntity.Deserializer())
.registerTypeAdapter(MessageEntityType.class, new MessageEntityType.Adapter())
.registerTypeHierarchyAdapter(InputFile.class, new InputFile.Serializer())
.registerTypeHierarchyAdapter(ChatId.class, new ChatId.Serializer())
.create();
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.jtelegram.api.events;

import com.jtelegram.api.TelegramBot;
import javax.annotation.Nonnull;
import lombok.AccessLevel;
import lombok.AllArgsConstructor;
import lombok.Getter;
Expand All @@ -10,8 +11,10 @@
@AllArgsConstructor(access = AccessLevel.PROTECTED)
public abstract class Event {
@Getter
@Nonnull
private final TelegramBot bot;

@Nonnull
public Class<? extends Event> getType() {
return getClass();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
import com.jtelegram.api.events.inline.ChosenInlineResultEvent;
import com.jtelegram.api.requests.inline.AnswerInlineQuery;
import com.jtelegram.api.util.ExceptionThreadFactory;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
Expand Down Expand Up @@ -58,14 +57,20 @@ public <E extends Event> void dispatch(E event) {
}

this.threadPool.submit(() -> {
List<EventHandler<? extends Event>> h = handlers.get(event.getType());

if (h != null) {
h.forEach(handler -> {
EventHandler<E> eh = (EventHandler<E>) handler;
eh.onEvent(event);
});
Class<?> eventClass = event.getClass();
List<EventHandler<? extends Event>> h = new ArrayList<>();
while (Event.class.isAssignableFrom(eventClass) && eventClass != Event.class) {
List<EventHandler<? extends Event>> handlerList = handlers.get(eventClass.asSubclass(Event.class));
if (handlerList != null) {
h.addAll(handlerList);
}
eventClass = eventClass.getSuperclass();
}

h.forEach(handler -> {
EventHandler<E> eh = (EventHandler<E>) handler;
eh.onEvent(event);
});
});
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package com.jtelegram.api.events;

import com.jtelegram.api.TelegramBot;
import com.jtelegram.api.update.Update;
import javax.annotation.Nonnull;
import lombok.Getter;

/**
* @author Nick Robson
*/
@Getter
public abstract class UpdateEvent<T extends Update<?>> extends Event {

@Nonnull
private T update;

protected UpdateEvent(@Nonnull TelegramBot bot, @Nonnull T update) {
super(bot);
this.update = update;
}

}
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
package com.jtelegram.api.events.channel;

import com.jtelegram.api.TelegramBot;
import com.jtelegram.api.events.Event;
import com.jtelegram.api.events.UpdateEvent;
import com.jtelegram.api.message.Message;
import com.jtelegram.api.update.Update;
import lombok.Getter;
import lombok.ToString;

Expand All @@ -11,11 +12,11 @@
*/
@Getter
@ToString(callSuper = true)
public class ChannelPostEditEvent extends Event {
public class ChannelPostEditEvent extends UpdateEvent<Update.EditedChannelPostUpdate> {
private Message post;

public ChannelPostEditEvent(TelegramBot bot, Message post) {
super(bot);
this.post = post;
public ChannelPostEditEvent(TelegramBot bot, Update.EditedChannelPostUpdate update) {
super(bot, update);
this.post = update.getEditedChannelPost();
}
}
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
package com.jtelegram.api.events.channel;

import com.jtelegram.api.TelegramBot;
import com.jtelegram.api.events.Event;
import com.jtelegram.api.events.UpdateEvent;
import com.jtelegram.api.message.Message;
import com.jtelegram.api.update.Update;
import lombok.Getter;
import lombok.ToString;

Expand All @@ -11,11 +12,11 @@
*/
@Getter
@ToString(callSuper = true)
public class ChannelPostEvent extends Event {
public class ChannelPostEvent extends UpdateEvent<Update.ChannelPostUpdate> {
private Message post;

public ChannelPostEvent(TelegramBot bot, Message post) {
super(bot);
this.post = post;
public ChannelPostEvent(TelegramBot bot, Update.ChannelPostUpdate update) {
super(bot, update);
this.post = update.getChannelPost();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import com.jtelegram.api.TelegramBot;
import com.jtelegram.api.events.message.ServiceMessageEvent;
import com.jtelegram.api.message.impl.service.NewChatMembersMessage;
import com.jtelegram.api.update.Update;
import com.jtelegram.api.user.User;
import lombok.Getter;

Expand All @@ -14,8 +15,8 @@
public class ChatMemberJoinedEvent extends ServiceMessageEvent<NewChatMembersMessage> {
private List<User> newMembers;

public ChatMemberJoinedEvent(TelegramBot bot, NewChatMembersMessage originMessage) {
super(bot, originMessage);
public ChatMemberJoinedEvent(TelegramBot bot, Update.MessageUpdate update, NewChatMembersMessage originMessage) {
super(bot, update, originMessage);
this.newMembers = originMessage.getNewChatMembers();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import com.jtelegram.api.TelegramBot;
import com.jtelegram.api.events.message.ServiceMessageEvent;
import com.jtelegram.api.update.Update;
import com.jtelegram.api.user.User;
import com.jtelegram.api.message.impl.service.LeftChatMemberMessage;
import lombok.Getter;
Expand All @@ -12,8 +13,8 @@
public class ChatMemberLeftEvent extends ServiceMessageEvent<LeftChatMemberMessage> {
private User leftMember;

public ChatMemberLeftEvent(TelegramBot bot, LeftChatMemberMessage originMessage) {
super(bot, originMessage);
public ChatMemberLeftEvent(TelegramBot bot, Update.MessageUpdate update, LeftChatMemberMessage originMessage) {
super(bot, update, originMessage);
this.leftMember = originMessage.getLeftChatMember();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import com.jtelegram.api.chat.Chat;
import com.jtelegram.api.events.message.ServiceMessageEvent;
import com.jtelegram.api.message.impl.service.MigrateFromChatIdMessage;
import com.jtelegram.api.update.Update;
import lombok.Getter;
import lombok.ToString;

Expand All @@ -12,8 +13,8 @@
public class ChatMigratedFromChatEvent extends ServiceMessageEvent<MigrateFromChatIdMessage> {
private Chat newChat;

public ChatMigratedFromChatEvent(TelegramBot bot, MigrateFromChatIdMessage originMessage) {
super(bot, originMessage);
public ChatMigratedFromChatEvent(TelegramBot bot, Update.MessageUpdate update, MigrateFromChatIdMessage originMessage) {
super(bot, update, originMessage);
this.newChat = originMessage.getChat();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import com.jtelegram.api.chat.Chat;
import com.jtelegram.api.events.message.ServiceMessageEvent;
import com.jtelegram.api.message.impl.service.MigrateToChatIdMessage;
import com.jtelegram.api.update.Update;
import lombok.Getter;
import lombok.ToString;

Expand All @@ -12,8 +13,8 @@
public class ChatMigratedToChatEvent extends ServiceMessageEvent<MigrateToChatIdMessage> {
private Chat newChat;

public ChatMigratedToChatEvent(TelegramBot bot, MigrateToChatIdMessage originMessage) {
super(bot, originMessage);
public ChatMigratedToChatEvent(TelegramBot bot, Update.MessageUpdate update, MigrateToChatIdMessage originMessage) {
super(bot, update, originMessage);
this.newChat = originMessage.getChat();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,12 @@
import com.jtelegram.api.TelegramBot;
import com.jtelegram.api.events.message.ServiceMessageEvent;
import com.jtelegram.api.message.impl.service.DeleteChatPhotoMessage;
import com.jtelegram.api.update.Update;
import lombok.ToString;

@ToString(callSuper = true)
public class ChatPhotoDeletedEvent extends ServiceMessageEvent<DeleteChatPhotoMessage> {
public ChatPhotoDeletedEvent(TelegramBot bot, DeleteChatPhotoMessage originMessage) {
super(bot, originMessage);
public ChatPhotoDeletedEvent(TelegramBot bot, Update.MessageUpdate update, DeleteChatPhotoMessage originMessage) {
super(bot, update, originMessage);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import com.jtelegram.api.TelegramBot;
import com.jtelegram.api.events.message.ServiceMessageEvent;
import com.jtelegram.api.message.impl.service.GroupChatCreatedMessage;
import com.jtelegram.api.update.Update;
import lombok.Getter;
import lombok.ToString;

Expand All @@ -12,8 +13,8 @@
public class GroupChatCreatedEvent extends ServiceMessageEvent<GroupChatCreatedMessage> {
private Chat newChat;

public GroupChatCreatedEvent(TelegramBot bot, GroupChatCreatedMessage originMessage) {
super(bot, originMessage);
public GroupChatCreatedEvent(TelegramBot bot, Update.MessageUpdate update, GroupChatCreatedMessage originMessage) {
super(bot, update, originMessage);
this.newChat = originMessage.getChat();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import com.jtelegram.api.events.message.ServiceMessageEvent;
import com.jtelegram.api.message.impl.service.NewChatPhotoMessage;
import com.jtelegram.api.message.media.PhotoSize;
import com.jtelegram.api.update.Update;
import java.util.List;
import lombok.Getter;
import lombok.ToString;
Expand All @@ -13,8 +14,8 @@
public class NewChatPhotoEvent extends ServiceMessageEvent<NewChatPhotoMessage> {
private List<PhotoSize> newChatPhoto;

public NewChatPhotoEvent(TelegramBot bot, NewChatPhotoMessage originMessage) {
super(bot, originMessage);
public NewChatPhotoEvent(TelegramBot bot, Update.MessageUpdate update, NewChatPhotoMessage originMessage) {
super(bot, update, originMessage);
this.newChatPhoto = originMessage.getNewChatPhoto();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import com.jtelegram.api.events.message.ServiceMessageEvent;
import com.jtelegram.api.message.impl.service.NewChatTitleMessage;
import com.jtelegram.api.TelegramBot;
import com.jtelegram.api.update.Update;
import lombok.Getter;
import lombok.ToString;

Expand All @@ -11,8 +12,8 @@
public class NewChatTitleEvent extends ServiceMessageEvent<NewChatTitleMessage> {
private String newTitle;

public NewChatTitleEvent(TelegramBot bot, NewChatTitleMessage originMessage) {
super(bot, originMessage);
public NewChatTitleEvent(TelegramBot bot, Update.MessageUpdate update, NewChatTitleMessage originMessage) {
super(bot, update, originMessage);
this.newTitle = originMessage.getNewChatTitle();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import com.jtelegram.api.TelegramBot;
import com.jtelegram.api.message.Message;
import com.jtelegram.api.message.impl.service.PinnedMessageMessage;
import com.jtelegram.api.update.Update;
import lombok.Getter;
import lombok.ToString;

Expand All @@ -12,8 +13,8 @@
public class PinnedMessageEvent extends ServiceMessageEvent<PinnedMessageMessage> {
private Message pinnedMessage;

public PinnedMessageEvent(TelegramBot bot, PinnedMessageMessage originMessage) {
super(bot, originMessage);
public PinnedMessageEvent(TelegramBot bot, Update.MessageUpdate update, PinnedMessageMessage originMessage) {
super(bot, update, originMessage);
this.pinnedMessage = originMessage.getPinnedMessage();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package com.jtelegram.api.events.chat;

import com.jtelegram.api.TelegramBot;
import com.jtelegram.api.chat.Chat;
import com.jtelegram.api.events.message.ServiceMessageEvent;
import com.jtelegram.api.message.impl.service.SupergroupChatCreatedMessage;
import com.jtelegram.api.update.Update;
import lombok.Getter;
import lombok.ToString;

@Getter
@ToString(callSuper = true)
public class SupergroupChatCreatedEvent extends ServiceMessageEvent<SupergroupChatCreatedMessage> {
private Chat newChat;

public SupergroupChatCreatedEvent(TelegramBot bot, Update.MessageUpdate update, SupergroupChatCreatedMessage originMessage) {
super(bot, update, originMessage);
this.newChat = originMessage.getChat();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import com.jtelegram.api.TelegramBot;
import com.jtelegram.api.events.message.ServiceMessageEvent;
import com.jtelegram.api.message.impl.service.UserLoggedInMessage;
import com.jtelegram.api.update.Update;
import lombok.ToString;

/**
Expand All @@ -14,7 +15,7 @@
*/
@ToString(callSuper = true)
public class UserLoggedInEvent extends ServiceMessageEvent<UserLoggedInMessage> {
public UserLoggedInEvent(TelegramBot bot, UserLoggedInMessage originMessage) {
super(bot, originMessage);
public UserLoggedInEvent(TelegramBot bot, Update.MessageUpdate update, UserLoggedInMessage originMessage) {
super(bot, update, originMessage);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@

import com.jtelegram.api.TelegramBot;
import com.jtelegram.api.events.Event;
import com.jtelegram.api.events.UpdateEvent;
import com.jtelegram.api.inline.result.ChosenInlineResult;
import com.jtelegram.api.update.Update;
import lombok.Getter;
import lombok.ToString;

Expand All @@ -11,11 +13,11 @@
*/
@Getter
@ToString(callSuper = true)
public class ChosenInlineResultEvent extends Event {
public class ChosenInlineResultEvent extends UpdateEvent<Update.ChosenInlineResultUpdate> {
private ChosenInlineResult chosenResult;

public ChosenInlineResultEvent(TelegramBot bot, ChosenInlineResult chosenResult) {
super(bot);
this.chosenResult = chosenResult;
public ChosenInlineResultEvent(TelegramBot bot, Update.ChosenInlineResultUpdate update) {
super(bot, update);
this.chosenResult = update.getChosenInlineResult();
}
}
Loading