diff options
author | Vitaly Takmazov | 2021-10-21 22:52:14 +0300 |
---|---|---|
committer | Vitaly Takmazov | 2021-10-21 22:52:14 +0300 |
commit | c6972c05dcee19afb5725767f755a56f5162aee2 (patch) | |
tree | 64b8f3588e97d8e1141fc17646e99a11f57abe46 /src | |
parent | cfb6c1894d8223eac62d03eeececa7baf192aced (diff) |
Make popular feed equal to service user recommendations feed to have correct RSS feeds
Diffstat (limited to 'src')
18 files changed, 73 insertions, 147 deletions
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<EventSession> 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<User> 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<String> 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<User> 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<Integer> getRecommended(int uid, int before); - List<Integer> getPopular(int visitorUid, int before); - List<Integer> getPhotos(int visitorUid, int before); List<Integer> getSearch(User visitor, String search, int page); @@ -97,7 +95,7 @@ public interface MessagesService { List<Integer> getUserBlogAtDay(int uid, int privacy, int daysback); - List<Integer> getUserBlogWithRecommendations(int uid, int privacy, int before); + List<Integer> getUserBlogWithRecommendations(User user, User visitor, int privacy, int before); List<Integer> getUserRecommendations(int uid, int before); @@ -111,8 +109,6 @@ public interface MessagesService { List<Message> 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 @@ -623,20 +623,6 @@ public class MessagesServiceImpl extends BaseJdbcService implements MessagesServ @Transactional(readOnly = true) @Override - public List<Integer> 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<Integer> getPhotos(final int visitorUid, final int before) { SqlParameterSource sqlParameterSource = new MapSqlParameterSource().addValue("vid", visitorUid) .addValue("before", before); @@ -705,9 +691,11 @@ public class MessagesServiceImpl extends BaseJdbcService implements MessagesServ @Transactional(readOnly = true) @Override - public List<Integer> 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<Integer> 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); @@ -870,33 +861,6 @@ public class MessagesServiceImpl extends BaseJdbcService implements MessagesServ @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) { return getJdbcTemplate().update("UPDATE messages SET privacy=1 WHERE message_id=?", mid) > 0; } 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<ExternalToken> 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<String> adminUsers; - @Value("${service_user:juick}") - private String serviceUser; - private class UserMapper implements RowMapper<User> { @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<ExternalToken> 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<ExternalToken> 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<Integer> mids = feedType == FeedType.blog ? messagesService.getUserBlog(user.getUid(), 0, 0) : messagesService.getUserBlogWithRecommendations(user.getUid(), 0, 0); + List<Integer> 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); diff --git a/src/main/resources/data-h2.sql b/src/main/resources/data-h2.sql index aff3e286..e279e524 100644 --- a/src/main/resources/data-h2.sql +++ b/src/main/resources/data-h2.sql @@ -1,4 +1,5 @@ INSERT INTO users(id, nick, passw) VALUES(0, 'Anonymous', 'password'); +INSERT INTO users(id, nick, passw) VALUES(2, 'juick', 'password'); INSERT INTO tags(tag_id, name) VALUES(2, 'juick'); INSERT INTO reactions (like_id, description) VALUES (1, 'like'); INSERT INTO reactions (like_id, description) VALUES (2, 'love'); diff --git a/src/test/java/com/juick/server/tests/ServerTests.java b/src/test/java/com/juick/server/tests/ServerTests.java index edb43db4..ff86f4b4 100644 --- a/src/test/java/com/juick/server/tests/ServerTests.java +++ b/src/test/java/com/juick/server/tests/ServerTests.java @@ -272,9 +272,11 @@ public class ServerTests { @Inject private Users usersController; + @Inject + private User serviceUser; - private static User ugnich, freefd, juick; - static String ugnichName, ugnichPassword, freefdName, freefdPassword, juickName, juickPassword; + private static User ugnich, freefd; + static String ugnichName, ugnichPassword, freefdName, freefdPassword; URI emptyUri = URI.create(StringUtils.EMPTY); private static boolean isSetUp = false; @@ -300,11 +302,8 @@ public class ServerTests { ugnichPassword = "secret"; freefdName = "freefd"; freefdPassword = "MyPassw0rd!"; - juickName = "juick"; - juickPassword = "demo"; ugnich = userService.createUser(ugnichName, ugnichPassword).orElseThrow(IllegalStateException::new); freefd = userService.createUser(freefdName, freefdPassword).orElseThrow(IllegalStateException::new); - juick = userService.createUser(juickName, juickPassword).orElseThrow(IllegalStateException::new); webClient.getOptions().setJavaScriptEnabled(false); webClient.getOptions().setCssEnabled(false); isSetUp = true; @@ -670,7 +669,7 @@ public class ServerTests { .content(jsonMapper.writeValueAsBytes(Collections.singletonList(registration)))) .andExpect(status().isOk()); MvcResult result = mockMvc.perform(get("/api/notifications").param("uid", String.valueOf(ugnich.getUid())) - .with(httpBasic(juickName, juickPassword))).andExpect(status().isOk()).andReturn(); + .with(httpBasic(serviceUser.getName(), "password"))).andExpect(status().isOk()).andReturn(); List<User> users = jsonMapper.readValue(result.getResponse().getContentAsString(), new TypeReference<>() { }); assertThat(users.size(), is(1)); @@ -691,7 +690,7 @@ public class ServerTests { mockMvc.perform(delete("/api/notifications").with(httpBasic(ugnichName, ugnichPassword)) .contentType(MediaType.APPLICATION_JSON).content(jsonMapper.writeValueAsBytes(tokens))) .andExpect(status().isForbidden()); - mockMvc.perform(delete("/api/notifications").with(httpBasic(juickName, juickPassword)) + mockMvc.perform(delete("/api/notifications").with(httpBasic(serviceUser.getName(), "password")) .contentType(MediaType.APPLICATION_JSON).content(jsonMapper.writeValueAsBytes(tokens))) .andExpect(status().isOk()); } @@ -700,7 +699,7 @@ public class ServerTests { public void notificationsSettingsAllowedOnlyForServiceUser() throws Exception { CommandResult result = commandsManager.processCommand(ugnich, "yo", emptyUri); String stringValueOfMid = String.valueOf(result.getNewMessage().get().getMid()); - mockMvc.perform(get("/api/notifications").with(httpBasic(juickName, juickPassword)) + mockMvc.perform(get("/api/notifications").with(httpBasic(serviceUser.getName(), "password")) .param("mid", stringValueOfMid).param("uid", String.valueOf(ugnich.getUid()))) .andExpect(status().isOk()); mockMvc.perform( @@ -913,7 +912,7 @@ public class ServerTests { + "Content-Type: text/html; charset=\"UTF-8\"\n" + "\n" + "<div dir=\"ltr\">s2313334</div>\n" + "\n" + "--001a11454886e42be5056786ca70--", mid, mid); - mockMvc.perform(post("/api/mail").with(httpBasic(juickName, juickPassword)).content(mail)) + mockMvc.perform(post("/api/mail").with(httpBasic(serviceUser.getName(), "password")).content(mail)) .andExpect(status().isOk()); String reply = "Return-Path: <ugnich@example.com>\n" + "Received: from [192.168.88.140] ([91.244.168.38])\n" + " by smtp.gmail.com with ESMTPSA id r84sm3970197lja.54.2019.06.20.08.39.54\n" @@ -927,7 +926,7 @@ public class ServerTests { + "In-Reply-To: <CAF+0zPD_YLVgYovajLqUFwkRAgJT+FzyQ2EzikQsPKsrnfKv-Q@mail.gmail.com>\n" + "To: juick@juick.com\n" + "X-Mailer: iPhone Mail (16F203)\n" + "\n" + "0J3RgyDRjdGC0L4g0L/QvtC60LAhINCU0L7Qu9Cz0L4g0LvQuCwg0YPQvNC10Y7Rh9C4IQ=="; - mockMvc.perform(post("/api/mail").with(httpBasic(juickName, juickPassword)).content(reply)) + mockMvc.perform(post("/api/mail").with(httpBasic(serviceUser.getName(), "password")).content(reply)) .andExpect(status().isOk()); } @@ -999,7 +998,7 @@ public class ServerTests { privacyQueriesService.blacklistUser(ugnich, freefd); int newfreefdrid = messagesService.createReply(mid, 0, freefd, "from ban", null); serverManager.processSystemEvent(new SystemEvent(this, - SystemActivity.message(juick, messagesService.getReply(mid, newfreefdrid), Collections.emptyList()))); + SystemActivity.message(serviceUser, messagesService.getReply(mid, newfreefdrid), Collections.emptyList()))); assertThat(userService.isReplyToBL(ugnich, messagesService.getReply(mid, newfreefdrid)), is(true)); // TODO: test event listeners correctly Thread.sleep(2000L); @@ -1024,10 +1023,10 @@ public class ServerTests { .stream().noneMatch(m -> m.getTags().contains(banned)), is(true)); assertFalse(messagesService.getMessages(AnonymousUser.INSTANCE, messagesService.getPhotos(ugnich.getUid(), 0)) .stream().noneMatch(m -> m.getTags().contains(banned))); - jdbcTemplate.update("UPDATE messages SET popular=1 WHERE message_id=?", mid); - assertThat(messagesService.getMessages(AnonymousUser.INSTANCE, messagesService.getPopular(freefd.getUid(), 0)) + messagesService.recommendMessage(mid, serviceUser.getUid()); + assertThat(messagesService.getMessages(AnonymousUser.INSTANCE, messagesService.getUserBlogWithRecommendations(serviceUser, freefd, 0, 0)) .stream().noneMatch(m -> m.getTags().contains(banned)), is(true)); - assertFalse(messagesService.getMessages(AnonymousUser.INSTANCE, messagesService.getPopular(ugnich.getUid(), 0)) + assertFalse(messagesService.getMessages(AnonymousUser.INSTANCE, messagesService.getUserBlogWithRecommendations(serviceUser, ugnich, 0, 0)) .stream().noneMatch(m -> m.getTags().contains(banned))); assertThat( messagesService.getMessages(AnonymousUser.INSTANCE, messagesService.getMyFeed(freefd.getUid(), 0, true)) @@ -1121,7 +1120,7 @@ public class ServerTests { @Test public void emptyAuthenticatedPostShouldThrowBadRequest() throws Exception { - mockMvc.perform(post("/api/post").with(httpBasic(juickName, juickPassword))).andExpect(status().isBadRequest()); + mockMvc.perform(post("/api/post").with(httpBasic(serviceUser.getName(), "password"))).andExpect(status().isBadRequest()); } @Test @@ -1152,8 +1151,8 @@ public class ServerTests { CommandResult result = commandsManager.processCommand(ugnich, "freefd - dick", emptyUri); int mid = result.getNewMessage().get().getMid(); commandsManager.processCommand(freefd, String.format("#%d ugnich - dick too", mid), emptyUri); - commandsManager.processCommand(juick, String.format("#%d/1 ban for a hour!", mid), emptyUri); - commandsManager.processCommand(juick, String.format("#%d freefd is here but it is hidden from you", mid), + commandsManager.processCommand(serviceUser, String.format("#%d/1 ban for a hour!", mid), emptyUri); + commandsManager.processCommand(serviceUser, String.format("#%d freefd is here but it is hidden from you", mid), emptyUri); assertThat(messagesService.getMessage(mid).get().getReplies(), is(3)); Message reply = messagesService.getReply(mid, 3); @@ -1164,7 +1163,7 @@ public class ServerTests { assertThat(messagesService.getMessage(mid).get().getReplies(), is(4)); replies = messagesService.getReplies(ugnich, mid); assertThat(replies.size(), is(1)); - commandsManager.processCommand(juick, String.format("#%d/4 mmm?!", mid), emptyUri); + commandsManager.processCommand(serviceUser, String.format("#%d/4 mmm?!", mid), emptyUri); assertThat(messagesService.getMessage(mid).get().getReplies(), is(5)); replies = messagesService.getReplies(ugnich, mid); reply = messagesService.getReply(mid, 5); @@ -2023,8 +2022,8 @@ public class ServerTests { Message msg = commandsManager.processCommand(ugnich, "YO", emptyUri).getNewMessage().get(); messagesService.likeMessage(msg.getMid(), freefd.getUid(), Reaction.LIKE); messagesService.likeMessage(msg.getMid(), 0, Reaction.LIKE, "http://localhost:8080/u/test"); - jdbcTemplate.update("UPDATE messages SET popular=1 where message_id=?", msg.getMid()); - List<Message> top = messagesService.getMessages(ugnich, messagesService.getPopular(ugnich.getUid(), 0)); + messagesService.recommendMessage(msg.getMid(), serviceUser.getUid()); + List<Message> top = messagesService.getMessages(ugnich, messagesService.getUserBlogWithRecommendations(serviceUser, ugnich, 0, 0)); assertThat(top.size(), is(1)); } @@ -2277,7 +2276,7 @@ public class ServerTests { public void topEventShouldNotLossUser() { Message topMessage = MockUtils.mockMessage(1000, ugnich, "top message"); topMessage.setTo(AnonymousUser.INSTANCE); - SystemEvent event = new SystemEvent(this, SystemActivity.like(juick, topMessage, Collections.emptyList())); + SystemEvent event = new SystemEvent(this, SystemActivity.like(serviceUser, topMessage, Collections.emptyList())); applicationEventPublisher.publishEvent(event); Mockito.verify(notificationListener, Mockito.times(1)).onApplicationEvent(topEventCaptor.capture()); SystemEvent receivedEvent = topEventCaptor.getValue(); |