diff options
Diffstat (limited to 'src/main/java/com/juick/server/TelegramBotManager.java')
-rw-r--r-- | src/main/java/com/juick/server/TelegramBotManager.java | 50 |
1 files changed, 23 insertions, 27 deletions
diff --git a/src/main/java/com/juick/server/TelegramBotManager.java b/src/main/java/com/juick/server/TelegramBotManager.java index 9e53fbe1..7ca9e347 100644 --- a/src/main/java/com/juick/server/TelegramBotManager.java +++ b/src/main/java/com/juick/server/TelegramBotManager.java @@ -20,17 +20,14 @@ package com.juick.server; import com.juick.User; import com.juick.model.AnonymousUser; import com.juick.model.CommandResult; +import com.juick.server.api.SystemActivity; import com.juick.server.util.HttpUtils; import com.juick.service.MessagesService; import com.juick.service.TelegramService; import com.juick.service.UserService; -import com.juick.service.component.LikeEvent; -import com.juick.service.component.MessageEvent; -import com.juick.service.component.MessageReadEvent; +import com.juick.service.component.SystemEvent; import com.juick.service.component.NotificationListener; import com.juick.service.component.PingEvent; -import com.juick.service.component.SubscribeEvent; -import com.juick.service.component.TopEvent; import com.juick.util.MessageUtils; import com.pengrad.telegrambot.Callback; import com.pengrad.telegrambot.TelegramBot; @@ -370,7 +367,7 @@ public class TelegramBotManager implements NotificationListener { User user = userService.getUserByUID(userId).orElseThrow(IllegalStateException::new); userService.updateLastSeen(user); applicationEventPublisher.publishEvent( - new MessageReadEvent(this, user, source)); + new SystemEvent(this, SystemActivity.read(user, source))); } } } @@ -394,9 +391,22 @@ public class TelegramBotManager implements NotificationListener { } @Override - public void processMessageEvent(MessageEvent messageEvent) { - com.juick.Message jmsg = messageEvent.getMessage(); - List<User> subscribedUsers = messageEvent.getUsers(); + public void processSystemEvent(SystemEvent systemEvent) { + var activity = systemEvent.getActivity(); + var type = activity.getType(); + if (type.equals(SystemActivity.ActivityType.message)) { + processMessage(activity.getMessage(), activity.getTo()); + } else if (type.equals(SystemActivity.ActivityType.like)) { + if (systemEvent.getActivity().getFrom().getName().equals(serviceUser)) { + processTop(systemEvent.getActivity().getMessage()); + } else { + processLike(activity.getFrom(), activity.getMessage(), activity.getTo()); + } + } else if (type.equals(SystemActivity.ActivityType.follow)) { + processFollow(activity.getFrom(), activity.getTo()); + } + } + private void processMessage(com.juick.Message jmsg, List<User> subscribedUsers) { if (jmsg.isService()) { return; } @@ -422,12 +432,8 @@ public class TelegramBotManager implements NotificationListener { } } - @Override - public void processLikeEvent(LikeEvent likeEvent) { - User liker = likeEvent.getUser(); + private void processLike(User liker, com.juick.Message message, List<User> subscribers) { if (!liker.getName().equals(serviceUser)) { - com.juick.Message message = likeEvent.getMessage(); - List<User> subscribers = likeEvent.getSubscribers(); logger.info("Like received in tg listener"); if (!userService.isInBLAny(message.getUser().getUid(), liker.getUid())) { telegramService.getTelegramIdentifiers(Collections.singletonList(message.getUser())) @@ -445,24 +451,14 @@ public class TelegramBotManager implements NotificationListener { } - @Override - public void processMessageReadEvent(MessageReadEvent messageReadEvent) { - - } - - @Override - public void processTopEvent(TopEvent topEvent) { - com.juick.Message message = topEvent.getMessage(); + private void processTop(com.juick.Message message) { telegramService.getTelegramIdentifiers(Collections.singletonList(message.getUser())) .forEach(c -> telegramNotify(c, String.format("Your [post](%s) became popular!", formatUrl(message)), new com.juick.Message())); } - @Override - public void processSubscribeEvent(SubscribeEvent subscribeEvent) { - User subscriber = subscribeEvent.getUser(); - User target = subscribeEvent.getToUser(); - telegramService.getTelegramIdentifiers(Collections.singletonList(target)) + private void processFollow(User subscriber, List<User> target) { + telegramService.getTelegramIdentifiers(target) .forEach(c -> telegramNotify(c, String.format("%s subscribed to your blog", MessageUtils.getMarkdownUser(subscriber)), new com.juick.Message())); } |