From c6972c05dcee19afb5725767f755a56f5162aee2 Mon Sep 17 00:00:00 2001 From: Vitaly Takmazov Date: Thu, 21 Oct 2021 22:52:14 +0300 Subject: Make popular feed equal to service user recommendations feed to have correct RSS feeds --- src/main/java/com/juick/ActivityPubManager.java | 11 ----- src/main/java/com/juick/ServerManager.java | 13 +----- src/main/java/com/juick/TelegramBotManager.java | 8 ++-- src/main/java/com/juick/TopManager.java | 26 ++--------- src/main/java/com/juick/XMPPManager.java | 5 +-- src/main/java/com/juick/config/WebConfig.java | 13 ++++++ .../java/com/juick/service/MessagesService.java | 6 +-- .../com/juick/service/MessagesServiceImpl.java | 52 ++++------------------ src/main/java/com/juick/service/UserService.java | 2 - .../java/com/juick/service/UserServiceImpl.java | 8 ---- src/main/java/com/juick/www/api/Messages.java | 6 ++- src/main/java/com/juick/www/api/Notifications.java | 10 ++--- src/main/java/com/juick/www/api/Service.java | 8 ++-- .../java/com/juick/www/api/activity/Profile.java | 4 +- src/main/java/com/juick/www/controllers/Site.java | 4 +- src/main/java/com/juick/www/rss/Feeds.java | 2 +- 16 files changed, 52 insertions(+), 126 deletions(-) (limited to 'src/main/java') diff --git a/src/main/java/com/juick/ActivityPubManager.java b/src/main/java/com/juick/ActivityPubManager.java index 3b90530c..57110729 100644 --- a/src/main/java/com/juick/ActivityPubManager.java +++ b/src/main/java/com/juick/ActivityPubManager.java @@ -74,22 +74,11 @@ public class ActivityPubManager implements ActivityListener, NotificationListene @Inject private SocialService socialService; @Inject - private UserService userService; - @Inject private MessagesService messagesService; @Inject private PebbleEngine pebbleEngine; @Value("${ap_base_uri:http://localhost:8080/}") private String baseUri; - @Value("${service_user:juick}") - private String serviceUsername; - - private User serviceUser; - - @PostConstruct - public void init() { - serviceUser = userService.getUserByName(serviceUsername); - } @Override public void processFollowEvent(@Nonnull FollowEvent followEvent) { diff --git a/src/main/java/com/juick/ServerManager.java b/src/main/java/com/juick/ServerManager.java index f8f8b8c6..50389705 100644 --- a/src/main/java/com/juick/ServerManager.java +++ b/src/main/java/com/juick/ServerManager.java @@ -28,12 +28,10 @@ import com.juick.util.MessageUtils; import org.apache.commons.collections4.ListUtils; import org.slf4j.Logger; import org.slf4j.LoggerFactory; -import org.springframework.beans.factory.annotation.Value; import org.springframework.scheduling.annotation.Scheduled; import org.springframework.web.servlet.mvc.method.annotation.SseEmitter; import javax.annotation.Nonnull; -import javax.annotation.PostConstruct; import javax.inject.Inject; import java.util.ArrayList; import java.util.Arrays; @@ -55,18 +53,9 @@ public class ServerManager implements NotificationListener { @Inject private UserService userService; private final CopyOnWriteArrayList sessions = new CopyOnWriteArrayList<>(); - - @Value("${service_user:juick}") - private String serviceUsername; - + @Inject private User serviceUser; - @PostConstruct - public void init() { - serviceUser = userService.getUserByName(serviceUsername); - } - - private void onJuickPM(final User to, final Message jmsg) { messageEvent(jmsg, Arrays.asList(to, jmsg.getUser())); } diff --git a/src/main/java/com/juick/TelegramBotManager.java b/src/main/java/com/juick/TelegramBotManager.java index 3538a27b..33cac896 100644 --- a/src/main/java/com/juick/TelegramBotManager.java +++ b/src/main/java/com/juick/TelegramBotManager.java @@ -93,8 +93,8 @@ public class TelegramBotManager implements NotificationListener { private ApplicationEventPublisher applicationEventPublisher; @Value("${upload_tmp_dir:#{systemEnvironment['TEMP'] ?: '/tmp'}}") private String tmpDir; - @Value("${service_user:juick}") - private String serviceUser; + @Inject + private User serviceUser; private static final String MSG_LINK = "🔗"; @@ -397,7 +397,7 @@ public class TelegramBotManager implements NotificationListener { 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)) { + if (systemEvent.getActivity().getFrom().equals(serviceUser)) { processTop(systemEvent.getActivity().getMessage()); } else { processLike(activity.getFrom(), activity.getMessage(), activity.getTo()); @@ -433,7 +433,7 @@ public class TelegramBotManager implements NotificationListener { } private void processLike(User liker, com.juick.model.Message message, List subscribers) { - if (!liker.getName().equals(serviceUser)) { + if (!liker.equals(serviceUser)) { logger.info("Like received in tg listener"); if (!userService.isInBLAny(message.getUser().getUid(), liker.getUid())) { telegramService.getTelegramIdentifiers(Collections.singletonList(message.getUser())) diff --git a/src/main/java/com/juick/TopManager.java b/src/main/java/com/juick/TopManager.java index ca3b3e84..c58af428 100644 --- a/src/main/java/com/juick/TopManager.java +++ b/src/main/java/com/juick/TopManager.java @@ -20,19 +20,12 @@ package com.juick; import com.juick.model.Message; import com.juick.model.Tag; import com.juick.model.User; -import com.juick.www.api.SystemActivity; import com.juick.service.MessagesService; -import com.juick.service.UserService; -import com.juick.service.component.SystemEvent; import org.slf4j.Logger; import org.slf4j.LoggerFactory; -import org.springframework.beans.factory.annotation.Value; -import org.springframework.context.ApplicationEventPublisher; import org.springframework.scheduling.annotation.Scheduled; -import javax.annotation.PostConstruct; import javax.inject.Inject; -import java.util.Collections; import java.util.List; import java.util.stream.Collectors; @@ -41,30 +34,19 @@ public class TopManager { @Inject private MessagesService messagesService; @Inject - private UserService userService; - @Inject - private ApplicationEventPublisher applicationEventPublisher; - - @Value("${service_user:juick}") - private String serviceUsername; + private CommandsManager commandsManager; + @Inject private User serviceUser; - @PostConstruct - public void init() { - serviceUser = userService.getUserByName(serviceUsername); - } - @Scheduled(fixedRate = 3600000) public void updateTop() { messagesService.getPopularCandidates().forEach(m -> { Message jmsg = messagesService.getMessage(m).orElseThrow(IllegalStateException::new); - logger.info("added {} to popular", m); - messagesService.setMessagePopular(m, 1); List tags = jmsg.getTags().stream().map(Tag::getName).map(String::toLowerCase).collect(Collectors.toList()); if (!tags.contains("juick")) { - applicationEventPublisher.publishEvent(new SystemEvent(this, - SystemActivity.like(serviceUser, jmsg, Collections.emptyList()))); + logger.info("added {} to popular", m); + commandsManager.commandRecommend(serviceUser, null, String.valueOf(m)); } }); } diff --git a/src/main/java/com/juick/XMPPManager.java b/src/main/java/com/juick/XMPPManager.java index 8b775495..b42b4b9f 100644 --- a/src/main/java/com/juick/XMPPManager.java +++ b/src/main/java/com/juick/XMPPManager.java @@ -115,11 +115,9 @@ public class XMPPManager implements NotificationListener { private PMQueriesService pmQueriesService; @Inject private Executor applicationTaskExecutor; - @Value("${service_user:juick}") - private String serviceUsername; @Inject private WebApp webApp; - + @Inject private User serviceUser; @PostConstruct @@ -263,7 +261,6 @@ public class XMPPManager implements NotificationListener { logger.warn("xmpp exception", e); } }); - serviceUser = userService.getUserByName(serviceUsername); } private void sendJuickMessage(com.juick.model.Message jmsg, List users) { diff --git a/src/main/java/com/juick/config/WebConfig.java b/src/main/java/com/juick/config/WebConfig.java index ccfa5f1e..fcc61b72 100644 --- a/src/main/java/com/juick/config/WebConfig.java +++ b/src/main/java/com/juick/config/WebConfig.java @@ -18,7 +18,9 @@ package com.juick.config; import com.juick.*; +import com.juick.model.User; import com.juick.service.HelpService; +import com.juick.service.UserService; import com.mitchellbosecke.pebble.PebbleEngine; import com.mitchellbosecke.pebble.extension.FormatterExtension; import com.mitchellbosecke.pebble.loader.ClasspathLoader; @@ -52,6 +54,8 @@ import java.nio.file.Paths; import java.util.Collections; import java.util.concurrent.TimeUnit; +import javax.inject.Inject; + /** * Created by aalexeev on 11/22/16. */ @@ -176,5 +180,14 @@ public class WebConfig implements WebMvcConfigurer { e.printStackTrace(); } } + + @Inject + private UserService userService; + @Value("${service_user:juick}") + private String serviceUsername; + @Bean + public User serviceUser() { + return userService.getUserByName(serviceUsername); + } } diff --git a/src/main/java/com/juick/service/MessagesService.java b/src/main/java/com/juick/service/MessagesService.java index bb12a61e..5e3a1e83 100644 --- a/src/main/java/com/juick/service/MessagesService.java +++ b/src/main/java/com/juick/service/MessagesService.java @@ -85,8 +85,6 @@ public interface MessagesService { List getRecommended(int uid, int before); - List getPopular(int visitorUid, int before); - List getPhotos(int visitorUid, int before); List getSearch(User visitor, String search, int page); @@ -97,7 +95,7 @@ public interface MessagesService { List getUserBlogAtDay(int uid, int privacy, int daysback); - List getUserBlogWithRecommendations(int uid, int privacy, int before); + List getUserBlogWithRecommendations(User user, User visitor, int privacy, int before); List getUserRecommendations(int uid, int before); @@ -111,8 +109,6 @@ public interface MessagesService { List getReplies(User user, int mid); - boolean setMessagePopular(int mid, int popular); - boolean setMessagePrivacy(int mid); boolean deleteMessage(int uid, int mid); diff --git a/src/main/java/com/juick/service/MessagesServiceImpl.java b/src/main/java/com/juick/service/MessagesServiceImpl.java index c2e5d15d..0625716a 100644 --- a/src/main/java/com/juick/service/MessagesServiceImpl.java +++ b/src/main/java/com/juick/service/MessagesServiceImpl.java @@ -621,20 +621,6 @@ public class MessagesServiceImpl extends BaseJdbcService implements MessagesServ + "ORDER BY f.message_id DESC LIMIT 20", sqlParameterSource, Integer.class); } - @Transactional(readOnly = true) - @Override - public List getPopular(final int visitorUid, final int before) { - SqlParameterSource sqlParameterSource = new MapSqlParameterSource().addValue("vid", visitorUid) - .addValue("before", before); - - return getNamedParameterJdbcTemplate().queryForList("SELECT m.message_id FROM messages m WHERE m.privacy > 0 " - + (before > 0 ? " AND m.message_id < :before " : StringUtils.EMPTY) - + " AND m.popular > 0 AND NOT EXISTS (SELECT 1 FROM bl_users b WHERE b.user_id = :vid and b.bl_user_id = m.user_id) " - + " AND NOT EXISTS (SELECT 1 FROM bl_tags bt WHERE bt.tag_id IN " - + "(SELECT tag_id FROM messages_tags WHERE message_id = m.message_id) and :vid = bt.user_id)" - + " ORDER BY m.message_id DESC LIMIT 20", sqlParameterSource, Integer.class); - } - @Transactional(readOnly = true) @Override public List getPhotos(final int visitorUid, final int before) { @@ -705,9 +691,11 @@ public class MessagesServiceImpl extends BaseJdbcService implements MessagesServ @Transactional(readOnly = true) @Override - public List getUserBlogWithRecommendations(final int uid, final int privacy, final int before) { - SqlParameterSource sqlParameterSource = new MapSqlParameterSource().addValue("uid", uid) - .addValue("privacy", privacy).addValue("before", before); + public List getUserBlogWithRecommendations(final User user, final User visitor, final int privacy, final int before) { + SqlParameterSource sqlParameterSource = new MapSqlParameterSource() + .addValue("uid", user.getUid()) + .addValue("vid", visitor.getUid()) + .addValue("privacy", privacy).addValue("before", before); return getNamedParameterJdbcTemplate() .queryForList("SELECT message_id FROM " + "(SELECT message_id FROM favorites " + " INNER JOIN users" @@ -716,6 +704,9 @@ public class MessagesServiceImpl extends BaseJdbcService implements MessagesServ + " ORDER BY message_id DESC LIMIT 20) as r" + " UNION ALL " + "SELECT message_id FROM " + "(SELECT message_id FROM messages" + " INNER JOIN users" + " ON messages.user_id = users.id" + " WHERE user_id = :uid AND users.banned = 0" + + " AND NOT EXISTS (SELECT 1 FROM bl_users b WHERE b.user_id = :vid and b.bl_user_id = messages.user_id) " + + " AND NOT EXISTS (SELECT 1 FROM bl_tags bt WHERE bt.tag_id IN " + + "(SELECT tag_id FROM messages_tags WHERE message_id = messages.message_id) and :vid = bt.user_id)" + (before > 0 ? " AND message_id < :before" : StringUtils.EMPTY) + " AND privacy >= :privacy ORDER BY message_id DESC LIMIT 20) as m " + "ORDER BY message_id DESC LIMIT 20", sqlParameterSource, Integer.class); @@ -868,33 +859,6 @@ public class MessagesServiceImpl extends BaseJdbcService implements MessagesServ return replies; } - @Transactional - @Override - public boolean setMessagePopular(final int mid, final int popular) { - int ret; - MapSqlParameterSource sqlParameterSource = new MapSqlParameterSource().addValue("mid", mid).addValue("popular", - popular); - - switch (popular) { - case -2: - ret = getNamedParameterJdbcTemplate().update("UPDATE messages SET hidden = 1 WHERE message_id = :mid", - sqlParameterSource); - break; - case -1: - sqlParameterSource.addValue("popular", 0); - default: - ret = getNamedParameterJdbcTemplate() - .update("UPDATE messages SET popular = :popular WHERE message_id = :mid", sqlParameterSource); - break; - } - - if (popular == -1) - ret = getNamedParameterJdbcTemplate().update("INSERT INTO top_ignore_messages VALUES (:mid)", - sqlParameterSource); - - return ret > 0; - } - @Transactional @Override public boolean setMessagePrivacy(final int mid) { diff --git a/src/main/java/com/juick/service/UserService.java b/src/main/java/com/juick/service/UserService.java index dc879821..c0428169 100644 --- a/src/main/java/com/juick/service/UserService.java +++ b/src/main/java/com/juick/service/UserService.java @@ -131,8 +131,6 @@ public interface UserService { boolean isAdminUser(User user); - boolean isServiceUser(User user); - Optional getTwitterToken(int uid); boolean deleteTwitterToken(Integer uid); diff --git a/src/main/java/com/juick/service/UserServiceImpl.java b/src/main/java/com/juick/service/UserServiceImpl.java index 9762802f..e73f4ea3 100644 --- a/src/main/java/com/juick/service/UserServiceImpl.java +++ b/src/main/java/com/juick/service/UserServiceImpl.java @@ -60,9 +60,6 @@ public class UserServiceImpl extends BaseJdbcService implements UserService { @Value("${juick.admin_users:}") List adminUsers; - @Value("${service_user:juick}") - private String serviceUser; - private class UserMapper implements RowMapper { @Override public User mapRow(@Nonnull ResultSet rs, int rowNum) throws SQLException { @@ -669,11 +666,6 @@ public class UserServiceImpl extends BaseJdbcService implements UserService { public boolean isAdminUser(User user) { return adminUsers.contains(user.getName()); } - - @Override - public boolean isServiceUser(User user) { - return user.getName().equals(serviceUser); - } @Transactional(readOnly = true) @Override diff --git a/src/main/java/com/juick/www/api/Messages.java b/src/main/java/com/juick/www/api/Messages.java index 7b003220..9608813d 100644 --- a/src/main/java/com/juick/www/api/Messages.java +++ b/src/main/java/com/juick/www/api/Messages.java @@ -65,6 +65,8 @@ public class Messages { private WebApp webApp; @Value("classpath:Transparent.gif") private Resource invisiblePixel; + @Inject + private User serviceUser; // TODO: serialize image urls @@ -102,7 +104,7 @@ public class Messages { throw new HttpNotFoundException(); } } else if (StringUtils.hasText(withrecommended)) { - mids = messagesService.getUserBlogWithRecommendations(user.getUid(), 0, before); + mids = messagesService.getUserBlogWithRecommendations(user, visitor, 0, before); } else if (daysback > 0) { mids = messagesService.getUserBlogAtDay(user.getUid(), 0, daysback); } else if (StringUtils.hasText(search)) { @@ -116,7 +118,7 @@ public class Messages { } } else { if (StringUtils.hasText(popular)) { - mids = messagesService.getPopular(visitor.getUid(), before); + mids = messagesService.getUserBlogWithRecommendations(serviceUser, visitor, 0, before); } else if (StringUtils.hasText(media)) { mids = messagesService.getPhotos(visitor.getUid(), before); } else if (StringUtils.hasText(tag)) { diff --git a/src/main/java/com/juick/www/api/Notifications.java b/src/main/java/com/juick/www/api/Notifications.java index d00fe4ca..613e6cfc 100644 --- a/src/main/java/com/juick/www/api/Notifications.java +++ b/src/main/java/com/juick/www/api/Notifications.java @@ -63,8 +63,8 @@ public class Notifications { private UserService userService; @Inject private TelegramService telegramService; - @Value("${api_user:juick}") - private String serviceUser; + @Inject + private User serviceUser; private User collectTokens(Integer uid) { User user = userService.getUserByUID(uid).orElse(AnonymousUser.INSTANCE); @@ -90,7 +90,7 @@ public class Notifications { @RequestParam(required = false, defaultValue = "0") int uid, @RequestParam(required = false, defaultValue = "0") int mid, @RequestParam(required = false, defaultValue = "0") int rid) { - if (!(visitor.getName().equals(serviceUser))) { + if (!(visitor.equals(serviceUser))) { throw new HttpForbiddenException(); } if (uid > 0 && mid == 0) { @@ -127,7 +127,7 @@ public class Notifications { public Status doDelete( @Visitor User visitor, @RequestBody List list) { - if (!visitor.getName().equals(serviceUser)) { + if (!visitor.equals(serviceUser)) { throw new HttpForbiddenException(); } list.forEach(t -> { @@ -153,7 +153,7 @@ public class Notifications { public Status doDeleteTokens( @Visitor User visitor, @RequestBody List list) { - if (!visitor.getName().equals(serviceUser)) { + if (!visitor.equals(serviceUser)) { throw new HttpForbiddenException(); } list.forEach(t -> { diff --git a/src/main/java/com/juick/www/api/Service.java b/src/main/java/com/juick/www/api/Service.java index 3844d003..fc0132b1 100644 --- a/src/main/java/com/juick/www/api/Service.java +++ b/src/main/java/com/juick/www/api/Service.java @@ -75,8 +75,8 @@ public class Service { private CommandsManager commandsManager; @Inject private ApplicationEventPublisher applicationEventPublisher; - @Value("${api_user:juick}") - private String serviceUser; + @Inject + private User serviceUser; @Value("${upload_tmp_dir:#{systemEnvironment['TEMP'] ?: '/tmp'}}") private String tmpDir; @Value("${banned_emails:}") @@ -90,7 +90,7 @@ public class Service { @PostMapping("/api/mail") @ResponseStatus(value = HttpStatus.OK) public void processMail(@Visitor User current, InputStream data) throws Exception { - if (current.getName().equals(serviceUser)) { + if (current.equals(serviceUser)) { MimeMessage msg = new MimeMessage(session, data); String[] returnPaths = msg.getHeader("Return-Path"); if (returnPaths != null) { @@ -197,7 +197,7 @@ public class Service { @PostMapping("/api/mail/unsubscribe") @ResponseStatus(value = HttpStatus.OK) public void processMailUnsubscribe(@Visitor User current, InputStream data) throws Exception { - if (current.getName().equals(serviceUser)) { + if (current.equals(serviceUser)) { MimeMessage msg = new MimeMessage(session, data); String from = msg.getFrom() == null || msg.getFrom().length > 1 ? ((InternetAddress) msg.getSender()).getAddress() diff --git a/src/main/java/com/juick/www/api/activity/Profile.java b/src/main/java/com/juick/www/api/activity/Profile.java index 3dc717e9..045e919d 100644 --- a/src/main/java/com/juick/www/api/activity/Profile.java +++ b/src/main/java/com/juick/www/api/activity/Profile.java @@ -105,13 +105,15 @@ public class Profile { private WebApp webApp; @Inject private Remark remarkConverter; + @Inject + private User serviceUser; @GetMapping(value = "/u/{userName}", produces = { Context.LD_JSON_MEDIA_TYPE, Context.ACTIVITYSTREAMS_PROFILE_MEDIA_TYPE, Context.FALLBACK_JSON_MEDIA_TYPE }) public Actor getUser(@PathVariable String userName) { User user = userService.getUserByName(userName); if (!user.isAnonymous()) { - Actor profile = userService.isServiceUser(user) ? new Application() : new Person(); + Actor profile = user.equals(serviceUser) ? new Application() : new Person(); profile.setId(activityPubManager.personUri(user)); profile.setUrl(activityPubManager.personWebUri(user)); profile.setName(userName); diff --git a/src/main/java/com/juick/www/controllers/Site.java b/src/main/java/com/juick/www/controllers/Site.java index 682ed247..9eb4c917 100644 --- a/src/main/java/com/juick/www/controllers/Site.java +++ b/src/main/java/com/juick/www/controllers/Site.java @@ -69,6 +69,8 @@ public class Site { private PMQueriesService pmQueriesService; @Inject private WebApp webApp; + @Inject + private User serviceUser; private void fillUserModel(ModelMap model, User user, User visitor) { user.setAvatar(webApp.getAvatarWebPath(user)); @@ -118,7 +120,7 @@ public class Site { mids = messagesService.getDiscussions(visitor.getUid(), paramTo); } else if (paramShow.equals("top")) { title = ResourceBundle.getBundle("messages", locale).getString("link.popular"); - mids = messagesService.getPopular(visitor.getUid(), paramBefore); + mids = messagesService.getUserBlogWithRecommendations(serviceUser, visitor, 0, paramBefore); model.addAttribute("discover", true); } else if (paramShow.equals("my") && !visitor.isAnonymous()) { title = ResourceBundle.getBundle("messages", locale).getString("link.my"); diff --git a/src/main/java/com/juick/www/rss/Feeds.java b/src/main/java/com/juick/www/rss/Feeds.java index 34ad7e5e..8b396fd2 100644 --- a/src/main/java/com/juick/www/rss/Feeds.java +++ b/src/main/java/com/juick/www/rss/Feeds.java @@ -51,7 +51,7 @@ public class Feeds { public ModelAndView getBlog(@Visitor User visitor, @PathVariable String userName, @PathVariable FeedType feedType) { User user = userService.getUserByName(userName); if (!user.isAnonymous() && !user.isBanned()) { - List mids = feedType == FeedType.blog ? messagesService.getUserBlog(user.getUid(), 0, 0) : messagesService.getUserBlogWithRecommendations(user.getUid(), 0, 0); + List mids = feedType == FeedType.blog ? messagesService.getUserBlog(user.getUid(), 0, 0) : messagesService.getUserBlogWithRecommendations(user, visitor, 0, 0); ModelAndView modelAndView = new ModelAndView(); modelAndView.setViewName("messagesView"); modelAndView.addObject("user", user); -- cgit v1.2.3