From 753d79d6bbabfdac33c6c382717fe0acaec2a3bd Mon Sep 17 00:00:00 2001 From: Vitaly Takmazov Date: Wed, 11 Oct 2017 12:47:02 +0300 Subject: telegram: do not duplicate subscription requests --- .../java/com/juick/api/TelegramBotManager.java | 9 +++-- .../main/java/com/juick/service/TagService.java | 3 ++ .../java/com/juick/service/TelegramService.java | 6 ++-- .../java/com/juick/service/TagServiceImpl.java | 11 ++++++ .../com/juick/service/TelegramServiceImpl.java | 24 ++++++------- .../java/com/juick/service/MessageServiceTest.java | 39 +++++++++++++++++----- 6 files changed, 66 insertions(+), 26 deletions(-) diff --git a/juick-api/src/main/java/com/juick/api/TelegramBotManager.java b/juick-api/src/main/java/com/juick/api/TelegramBotManager.java index 161680e9..4de5c840 100644 --- a/juick-api/src/main/java/com/juick/api/TelegramBotManager.java +++ b/juick-api/src/main/java/com/juick/api/TelegramBotManager.java @@ -19,6 +19,7 @@ package com.juick.api; import com.fasterxml.jackson.databind.ObjectMapper; import com.juick.service.MessagesService; +import com.juick.service.SubscriptionService; import com.juick.service.TelegramService; import com.pengrad.telegrambot.Callback; import com.pengrad.telegrambot.TelegramBot; @@ -64,6 +65,8 @@ public class TelegramBotManager extends TextWebSocketHandler { @Inject private MessagesService messagesService; @Inject + private SubscriptionService subscriptionService; + @Inject private ObjectMapper jsonMapper; private WebSocketSession session; @@ -115,7 +118,7 @@ public class TelegramBotManager extends TextWebSocketHandler { if (jmsg.getRid() == 0) { String msg = formatPost(jmsg); - List users = telegramService.getSubscribers(jmsg.getUser().getUid()); + List users = telegramService.getTelegramIdentifiers(subscriptionService.getSubscribedUsers(jmsg.getUser().getUid(), jmsg.getMid())); List chats = telegramService.getChats(); // registered subscribed users @@ -126,7 +129,9 @@ public class TelegramBotManager extends TextWebSocketHandler { // get quote com.juick.Message msg = messagesService.getReply(jmsg.getMid(), jmsg.getRid()); String fmsg = formatPost(msg); - telegramService.getSubscribersToComments(jmsg.getMid(), jmsg.getUser().getUid()).forEach(c -> telegramNotify(c, fmsg, msgUrl)); + telegramService.getTelegramIdentifiers( + subscriptionService.getUsersSubscribedToComments(jmsg.getMid(), jmsg.getUser().getUid()) + ).forEach(c -> telegramNotify(c, fmsg, msgUrl)); } } diff --git a/juick-server-core/src/main/java/com/juick/service/TagService.java b/juick-server-core/src/main/java/com/juick/service/TagService.java index b59acd0b..e24cf928 100644 --- a/juick-server-core/src/main/java/com/juick/service/TagService.java +++ b/juick-server-core/src/main/java/com/juick/service/TagService.java @@ -18,6 +18,7 @@ package com.juick.service; import com.juick.Tag; +import com.juick.User; import com.juick.server.helpers.TagStats; import java.util.Collection; @@ -51,4 +52,6 @@ public interface TagService { List getMessageTags(int mid); List getMessageTagsIDs(int mid); + + boolean blacklistTag(User user, Tag tag); } diff --git a/juick-server-core/src/main/java/com/juick/service/TelegramService.java b/juick-server-core/src/main/java/com/juick/service/TelegramService.java index 5bbcc40d..4c8ebc4b 100644 --- a/juick-server-core/src/main/java/com/juick/service/TelegramService.java +++ b/juick-server-core/src/main/java/com/juick/service/TelegramService.java @@ -17,6 +17,8 @@ package com.juick.service; +import com.juick.User; + import java.util.List; /** @@ -33,7 +35,5 @@ public interface TelegramService { boolean deleteTelegramUser(Integer uid); - List getSubscribers(int uid); - - List getSubscribersToComments(int mid, int ignore_uid); + List getTelegramIdentifiers(List users); } diff --git a/juick-server-jdbc/src/main/java/com/juick/service/TagServiceImpl.java b/juick-server-jdbc/src/main/java/com/juick/service/TagServiceImpl.java index 18e9485c..79163d1c 100644 --- a/juick-server-jdbc/src/main/java/com/juick/service/TagServiceImpl.java +++ b/juick-server-jdbc/src/main/java/com/juick/service/TagServiceImpl.java @@ -18,6 +18,7 @@ package com.juick.service; import com.juick.Tag; +import com.juick.User; import com.juick.server.helpers.TagStats; import org.apache.commons.collections4.CollectionUtils; import org.apache.commons.lang3.ArrayUtils; @@ -274,4 +275,14 @@ public class TagServiceImpl extends BaseJdbcService implements TagService { "SELECT tag_id FROM messages_tags WHERE message_id = ?", Integer.class, mid); } + + @Override + public boolean blacklistTag(User user, Tag tag) { + int rowcount = getNamedParameterJdbcTemplate().update("DELETE FROM bl_tags WHERE tag_id = :tid AND user_id = :uid", + new MapSqlParameterSource().addValue("tid", tag.TID).addValue("uid", user.getUid())); + return rowcount <= 0 && getNamedParameterJdbcTemplate() + .update("INSERT INTO bl_tags(user_id, tag_id) VALUES(:uid,:tid)", + new MapSqlParameterSource().addValue("tid", tag.TID) + .addValue("uid", user.getUid())) > 0; + } } diff --git a/juick-server-jdbc/src/main/java/com/juick/service/TelegramServiceImpl.java b/juick-server-jdbc/src/main/java/com/juick/service/TelegramServiceImpl.java index 1ccb8f25..325396e4 100644 --- a/juick-server-jdbc/src/main/java/com/juick/service/TelegramServiceImpl.java +++ b/juick-server-jdbc/src/main/java/com/juick/service/TelegramServiceImpl.java @@ -17,13 +17,17 @@ package com.juick.service; +import com.juick.User; import org.springframework.jdbc.core.JdbcTemplate; +import org.springframework.jdbc.core.namedparam.MapSqlParameterSource; import org.springframework.stereotype.Repository; import org.springframework.transaction.annotation.Transactional; import javax.inject.Inject; +import java.util.Collections; import java.util.List; import java.util.UUID; +import java.util.stream.Collectors; /** * Created by vt on 24/11/2016. @@ -74,17 +78,13 @@ public class TelegramServiceImpl extends BaseJdbcService implements TelegramServ @Transactional(readOnly = true) @Override - public List getSubscribers(final int uid) { - return getJdbcTemplate().queryForList("" + - "SELECT tg_id FROM telegram INNER JOIN subscr_users " + - "ON (subscr_users.user_id=? AND telegram.user_id=subscr_users.suser_id)", Long.class, uid); - } - - @Transactional(readOnly = true) - @Override - public List getSubscribersToComments(final int mid, final int ignore_uid) { - return getJdbcTemplate().queryForList( - "SELECT tg_id FROM telegram INNER JOIN subscr_messages " + - "ON (telegram.user_id=subscr_messages.suser_id) WHERE message_id=? AND suser_id!=?", Long.class, mid, ignore_uid); + public List getTelegramIdentifiers(List users) { + List uids = users.stream().map(User::getUid).collect(Collectors.toList()); + if (uids.isEmpty()) { + return Collections.emptyList(); + } + return getNamedParameterJdbcTemplate().queryForList("" + + "SELECT tg_id FROM telegram WHERE user_id IN(:uids)", new MapSqlParameterSource() + .addValue("uids", uids), Long.class); } } diff --git a/juick-server-jdbc/src/test/java/com/juick/service/MessageServiceTest.java b/juick-server-jdbc/src/test/java/com/juick/service/MessageServiceTest.java index 738a1250..44e46b70 100644 --- a/juick-server-jdbc/src/test/java/com/juick/service/MessageServiceTest.java +++ b/juick-server-jdbc/src/test/java/com/juick/service/MessageServiceTest.java @@ -17,22 +17,22 @@ package com.juick.service; -import com.juick.Message; +import com.juick.Tag; import com.juick.User; import com.juick.configuration.RepositoryConfiguration; import com.juick.server.helpers.AnonymousUser; import org.junit.Before; import org.junit.Test; +import org.springframework.jdbc.core.JdbcTemplate; import org.springframework.test.context.ContextConfiguration; import org.springframework.test.context.junit4.AbstractJUnit4SpringContextTests; import javax.inject.Inject; -import java.time.ZoneId; +import java.util.Collections; import java.util.List; import static org.hamcrest.MatcherAssert.assertThat; import static org.hamcrest.Matchers.equalTo; -import static org.hamcrest.Matchers.is; /** * Created by aalexeev on 11/25/16. @@ -45,6 +45,14 @@ public class MessageServiceTest extends AbstractJUnit4SpringContextTests { private UserService userService; @Inject private SubscriptionService subscriptionService; + @Inject + private TagService tagService; + @Inject + private TelegramService telegramService; + @Inject + private JdbcTemplate jdbcTemplate; + @Inject + private CrosspostService crosspostService; int ugnichId, freefdId, mid; @@ -77,11 +85,24 @@ public class MessageServiceTest extends AbstractJUnit4SpringContextTests { User ugnich = userService.getUserByUID(ugnichId).orElse(AnonymousUser.INSTANCE); User freefd = userService.getUserByUID(freefdId).orElse(AnonymousUser.INSTANCE); - int mid = messagesService.createMessage(ugnichId, "yo", null, null); - subscriptionService.subscribeMessage(mid, freefdId); - int rid = messagesService.createReply(mid, 0, ugnichId, "yo", null); - Message reply = messagesService.getReply(mid, rid); - assertThat(messagesService.getNotifications(freefd, null).size(), is(1)); - assertThat(messagesService.getNotifications(freefd, reply.getTimestamp().atZone(ZoneId.systemDefault()).toLocalDateTime()).size(), is(0)); + Tag yoTag = tagService.getTag("yoyo", true); + int mid = messagesService.createMessage(ugnichId, "yo", null, Collections.singletonList(yoTag)); + List subscribers = subscriptionService.getSubscribedUsers(ugnichId, mid); + + telegramService.createTelegramUser(12345, "freefd"); + String loginhash = jdbcTemplate.queryForObject("SELECT loginhash FROM telegram where tg_id=?", + String.class, 12345); + crosspostService.setTelegramUser(loginhash, freefd.getUid()); + + List telegramSubscribers = telegramService.getTelegramIdentifiers(subscribers); + assertThat(subscribers.size(), equalTo(1)); + assertThat(subscribers.size(), equalTo(telegramSubscribers.size())); + assertThat(subscribers.get(0).getUid(), equalTo(freefd.getUid())); + tagService.blacklistTag(freefd, yoTag); + List subscribers2 = subscriptionService.getSubscribedUsers(ugnichId, mid); + assertThat(subscribers2.size(), equalTo(0)); + assertThat(telegramService.getTelegramIdentifiers(subscribers2).size(), equalTo(0)); + tagService.blacklistTag(freefd, yoTag); + assertThat(subscriptionService.getSubscribedUsers(ugnichId, mid).size(), equalTo(1)); } } -- cgit v1.2.3