diff options
Diffstat (limited to 'src')
-rw-r--r-- | src/main/java/com/juick/CommandsManager.java | 10 | ||||
-rw-r--r-- | src/main/java/com/juick/TwitterManager.java | 4 | ||||
-rw-r--r-- | src/main/java/com/juick/service/MessagesService.java | 4 | ||||
-rw-r--r-- | src/main/java/com/juick/service/MessagesServiceImpl.java | 15 | ||||
-rw-r--r-- | src/main/java/com/juick/service/TagService.java | 5 | ||||
-rw-r--r-- | src/main/java/com/juick/service/TagServiceImpl.java | 15 | ||||
-rw-r--r-- | src/main/java/com/juick/util/TagUtils.java | 7 | ||||
-rw-r--r-- | src/test/java/com/juick/server/tests/ServerTests.java | 102 |
8 files changed, 94 insertions, 68 deletions
diff --git a/src/main/java/com/juick/CommandsManager.java b/src/main/java/com/juick/CommandsManager.java index 6ad19bee..0c16fbbd 100644 --- a/src/main/java/com/juick/CommandsManager.java +++ b/src/main/java/com/juick/CommandsManager.java @@ -25,6 +25,7 @@ import java.util.Arrays; import java.util.Collections; import java.util.List; import java.util.Optional; +import java.util.Set; import java.util.regex.Matcher; import java.util.regex.Pattern; import java.util.stream.Collectors; @@ -50,6 +51,7 @@ import com.juick.service.component.PingEvent; import com.juick.service.component.SystemEvent; import com.juick.util.HttpUtils; import com.juick.util.MessageUtils; +import com.juick.util.TagUtils; import com.juick.util.annotation.UserCommand; import com.juick.util.formatters.PlainTextFormatter; import com.juick.www.WebApp; @@ -122,7 +124,7 @@ public class CommandsManager { return commandResult; } } - Pair<String, List<Tag>> tags = tagService.fromString(input); + Pair<String, Set<Tag>> tags = tagService.fromString(input); if (tags.getRight().size() > 5) { return CommandResult.fromString("Sorry, 5 tags maximum."); } @@ -539,9 +541,11 @@ public class CommandsManager { return CommandResult.fromString("Reply not found"); } } - Pair<String, List<Tag>> messageTags = tagService.fromString(txt); + Pair<String, Set<Tag>> messageTags = tagService.fromString(txt); if (user.getUid() == msg.get().getUser().getUid() && rid == 0 && messageTags.getRight().size() > 0) { - if (!CollectionUtils.isEqualCollection(tagService.updateTags(mid, messageTags.getRight()), msg.get().getTags())) { + var updatedTags = tagService.updateTags(mid, messageTags.getRight()); + if (!CollectionUtils.isEqualCollection(updatedTags, msg.get().getTags())) { + messagesService.setReadOnly(msg.get().getMid(), TagUtils.hasTag(updatedTags, "readonly")); return CommandResult.fromString("Tags are updated"); } else { return CommandResult.fromString("Tags are NOT updated (5 tags maximum?)"); diff --git a/src/main/java/com/juick/TwitterManager.java b/src/main/java/com/juick/TwitterManager.java index 34864708..8af672a3 100644 --- a/src/main/java/com/juick/TwitterManager.java +++ b/src/main/java/com/juick/TwitterManager.java @@ -21,6 +21,8 @@ import com.juick.www.api.SystemActivity; import com.juick.service.UserService; import com.juick.service.component.*; import com.juick.util.MessageUtils; +import com.juick.util.TagUtils; + import org.apache.commons.lang3.StringUtils; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -76,7 +78,7 @@ public class TwitterManager implements NotificationListener { return; } if (StringUtils.isNotEmpty(userService.getTwitterName(msg.getUser().getUid()))) { - if (msg.getTags().stream().noneMatch(t -> t.getName().equals("notwitter"))) { + if (TagUtils.hasNoTag(msg.getTags(), "notwitter")) { twitterPost(msg); } } diff --git a/src/main/java/com/juick/service/MessagesService.java b/src/main/java/com/juick/service/MessagesService.java index 188a81c0..d102031d 100644 --- a/src/main/java/com/juick/service/MessagesService.java +++ b/src/main/java/com/juick/service/MessagesService.java @@ -31,7 +31,7 @@ import java.util.*; * Created by aalexeev on 11/13/16. */ public interface MessagesService { - int createMessage(int uid, String txt, String attachment, List<Tag> tags); + int createMessage(int uid, String txt, String attachment, Set<Tag> tags); int createReply(int mid, int rid, User user, String txt, String attachment); enum RecommendStatus { @@ -55,6 +55,8 @@ public interface MessagesService { boolean isReadOnly(int mid); + void setReadOnly(int mid, boolean readonly); + boolean isSubscribed(int uid, int mid); int getMessagePrivacy(int mid); diff --git a/src/main/java/com/juick/service/MessagesServiceImpl.java b/src/main/java/com/juick/service/MessagesServiceImpl.java index ddf7234e..b135f3c2 100644 --- a/src/main/java/com/juick/service/MessagesServiceImpl.java +++ b/src/main/java/com/juick/service/MessagesServiceImpl.java @@ -21,6 +21,8 @@ import com.juick.model.*; import com.juick.model.User; import com.juick.www.WebApp; import com.juick.util.MessageUtils; +import com.juick.util.TagUtils; + import org.apache.commons.collections4.CollectionUtils; import org.apache.commons.lang3.StringUtils; import org.apache.commons.lang3.tuple.ImmutablePair; @@ -131,9 +133,9 @@ public class MessagesServiceImpl extends BaseJdbcService implements MessagesServ */ @Transactional @Override - public int createMessage(final int uid, final String txt, final String attachment, final List<Tag> tags) { + public int createMessage(final int uid, final String txt, final String attachment, @Nonnull final Set<Tag> tags) { SimpleJdbcInsert simpleJdbcInsert = new SimpleJdbcInsert(getJdbcTemplate()).withTableName("messages") - .usingColumns("user_id", "attach", "ts").usingGeneratedKeyColumns("message_id"); + .usingColumns("user_id", "attach", "ts", "readonly").usingGeneratedKeyColumns("message_id"); Map<String, Object> insertMap = new HashMap<>(); insertMap.put("user_id", uid); Instant now = Instant.now(); @@ -141,15 +143,17 @@ public class MessagesServiceImpl extends BaseJdbcService implements MessagesServ if (StringUtils.isNotEmpty(attachment)) { insertMap.put("attach", attachment); } + insertMap.put("readonly", TagUtils.hasTag(tags, "readonly")); int mid = simpleJdbcInsert.executeAndReturnKey(insertMap).intValue(); if (mid > 0) { if (CollectionUtils.isNotEmpty(tags)) { + var newTags = new ArrayList<>(tags); getJdbcTemplate().batchUpdate("INSERT INTO messages_tags(message_id, tag_id) VALUES (?, ?)", new BatchPreparedStatementSetter() { @Override public void setValues(@Nonnull PreparedStatement ps, int i) throws SQLException { ps.setInt(1, mid); - ps.setInt(2, tags.get(i).TID); + ps.setInt(2, newTags.get(i).TID); } @Override @@ -343,6 +347,11 @@ public class MessagesServiceImpl extends BaseJdbcService implements MessagesServ return !list.isEmpty() && list.get(0) == 1; } + @Override + public void setReadOnly(final int mid, final boolean readonly) { + getJdbcTemplate().update("UPDATE messages SET readonly=?", readonly); + } + @Transactional(readOnly = true) @Override public boolean isSubscribed(final int uid, final int mid) { diff --git a/src/main/java/com/juick/service/TagService.java b/src/main/java/com/juick/service/TagService.java index 82aa9151..96e5975b 100644 --- a/src/main/java/com/juick/service/TagService.java +++ b/src/main/java/com/juick/service/TagService.java @@ -24,6 +24,7 @@ import org.apache.commons.lang3.tuple.Pair; import java.util.Collection; import java.util.List; +import java.util.Set; import java.util.stream.Stream; /** @@ -48,9 +49,9 @@ public interface TagService { List<TagStats> getTagStats(); - List<Tag> updateTags(int mid, Collection<Tag> newTags); + Set<Tag> updateTags(int mid, Collection<Tag> newTags); - Pair<String, List<Tag>> fromString(String txt); + Pair<String, Set<Tag>> fromString(String txt); List<TagStats> getMessageTags(int mid); diff --git a/src/main/java/com/juick/service/TagServiceImpl.java b/src/main/java/com/juick/service/TagServiceImpl.java index 0bd6bb0c..a5f3241a 100644 --- a/src/main/java/com/juick/service/TagServiceImpl.java +++ b/src/main/java/com/juick/service/TagServiceImpl.java @@ -40,6 +40,7 @@ import java.util.Arrays; import java.util.Collection; import java.util.List; import java.util.Objects; +import java.util.Set; import java.util.function.Supplier; import java.util.stream.Collectors; import java.util.stream.Stream; @@ -180,9 +181,9 @@ public class TagServiceImpl extends BaseJdbcService implements TagService { @Transactional @Override - public List<Tag> updateTags(final int mid, final Collection<Tag> newTags) { - List<Tag> currentTags = getMessageTags(mid).stream() - .map(TagStats::getTag).collect(Collectors.toList()); + public Set<Tag> updateTags(final int mid, final Collection<Tag> newTags) { + Set<Tag> currentTags = getMessageTags(mid).stream() + .map(TagStats::getTag).collect(Collectors.toSet()); if (CollectionUtils.isEmpty(newTags)) return currentTags; @@ -214,18 +215,18 @@ public class TagServiceImpl extends BaseJdbcService implements TagService { }); return getMessageTags(mid).stream() - .map(TagStats::getTag).collect(Collectors.toList()); + .map(TagStats::getTag).collect(Collectors.toSet()); } @Override - public Pair<String, List<Tag>> fromString(final String txt) { + public Pair<String, Set<Tag>> fromString(final String txt) { String firstLine = txt.split("\\n", 2)[0]; Supplier<Stream<String>> tagsStream = () -> Arrays.stream(firstLine.split("\\ ")) .takeWhile(t -> t.matches("\\*[^\\s]+|#[^\\s]*[^\\d][^\\s]*")); int tagsLength = tagsStream.get().collect(Collectors.joining(" ")).length(); String body = txt.substring(tagsLength); - List<Tag> tags = tagsStream.get().map(t -> getTag(t.substring(1), true)) - .distinct().collect(Collectors.toList()); + Set<Tag> tags = tagsStream.get().map(t -> getTag(t.substring(1), true)) + .distinct().collect(Collectors.toSet()); return Pair.of(body, tags); } diff --git a/src/main/java/com/juick/util/TagUtils.java b/src/main/java/com/juick/util/TagUtils.java index 2ec03e48..5069ae6f 100644 --- a/src/main/java/com/juick/util/TagUtils.java +++ b/src/main/java/com/juick/util/TagUtils.java @@ -22,6 +22,7 @@ import org.apache.commons.collections4.CollectionUtils; import org.apache.commons.lang3.StringUtils; import java.util.List; +import java.util.Set; import java.util.stream.Collectors; /** @@ -39,4 +40,10 @@ public class TagUtils { return tags.stream().map(t -> "*" + t.getName()) .collect(Collectors.joining(" ")); } + public static boolean hasTag(final Set<Tag> tags, String name) { + return tags.stream().anyMatch(tag -> tag.getName().equals(name)); + } + public static boolean hasNoTag(final Set<Tag> tags, String name) { + return tags.stream().noneMatch(tag -> tag.getName().equals(name)); + } } diff --git a/src/test/java/com/juick/server/tests/ServerTests.java b/src/test/java/com/juick/server/tests/ServerTests.java index df247b66..8cff4826 100644 --- a/src/test/java/com/juick/server/tests/ServerTests.java +++ b/src/test/java/com/juick/server/tests/ServerTests.java @@ -412,12 +412,12 @@ public class ServerTests { jdbcTemplate.execute("DELETE FROM telegram"); jdbcTemplate.execute("DELETE FROM subscr_users"); subscriptionService.subscribeUser(freefd, ugnich); - int mid0 = messagesService.createMessage(ugnich.getUid(), "test", null, null); - int mid2 = messagesService.createMessage(ugnich.getUid(), "test2", null, null); + int mid0 = messagesService.createMessage(ugnich.getUid(), "test", null, Set.of()); + int mid2 = messagesService.createMessage(ugnich.getUid(), "test2", null, Set.of()); List<Integer> freefdFeed = messagesService.getMyFeed(freefd.getUid(), 0, false); assertThat(freefdFeed.get(0), equalTo(mid2)); User tonya = userService.createUser("Tonya", "secret").orElseThrow(IllegalStateException::new); - int mid3 = messagesService.createMessage(tonya.getUid(), "test3", null, null); + int mid3 = messagesService.createMessage(tonya.getUid(), "test3", null, Set.of()); messagesService.recommendMessage(mid3, ugnich.getUid()); assertThat(messagesService.getMyFeed(freefd.getUid(), 0, false).get(0), equalTo(mid2)); assertThat(messagesService.getMyFeed(freefd.getUid(), 0, true).get(0), equalTo(mid3)); @@ -431,7 +431,7 @@ public class ServerTests { assertThat(messagesService.getAll(ugnich.getUid(), 0).get(0), equalTo(mid3)); Tag yoTag = tagService.getTag("yoyo", true); assertThat(tagService.getTag("YOYO", false), equalTo(yoTag)); - int mid = messagesService.createMessage(ugnich.getUid(), "yo", null, Collections.singletonList(yoTag)); + int mid = messagesService.createMessage(ugnich.getUid(), "yo", null, Set.of(yoTag)); Message msg = messagesService.getMessage(mid).get(); List<User> subscribers = subscriptionService.getSubscribedUsers(ugnich.getUid(), msg); @@ -472,7 +472,7 @@ public class ServerTests { public void messageTests() { User user = userService.createUser("mmmme", "secret").orElseThrow(IllegalStateException::new); assertEquals("mmmme", user.getName()); - int mid = messagesService.createMessage(user.getUid(), "yo", null, new ArrayList<>()); + int mid = messagesService.createMessage(user.getUid(), "yo", null, Set.of()); Message msg = messagesService.getMessage(mid).get(); assertEquals("yo", msg.getText()); User me = msg.getUser(); @@ -480,8 +480,7 @@ public class ServerTests { assertEquals("mmmme", messagesService.getMessageAuthor(mid).getName()); int tagID = tagService.createTag("weather"); Tag tag = tagService.getTag(tagID); - List<Tag> tagList = new ArrayList<>(); - tagList.add(tag); + Set<Tag> tagList = Set.of(tag); int mid2 = messagesService.createMessage(user.getUid(), "yo2", null, tagList); Message msg2 = messagesService.getMessage(mid2).get(); assertEquals(1, msg2.getTags().size()); @@ -526,7 +525,7 @@ public class ServerTests { htmlTagStats.setTag(htmlTag); String dbTagName = jdbcTemplate.queryForObject("select name from tags where name=?", String.class, htmlTagName); assertEquals(dbTagName, htmlTag.getName()); - int mid4 = messagesService.createMessage(user.getUid(), "yoyoyo", null, null); + int mid4 = messagesService.createMessage(user.getUid(), "yoyoyo", null, Set.of()); Message msg4 = messagesService.getMessage(mid4).get(); assertEquals(StringUtils.EMPTY, MessageUtils.getTagsString(msg4)); messagesService.deleteMessage(user.getUid(), mid4); @@ -548,7 +547,7 @@ public class ServerTests { public void likeTypeStatsTests() { User dsdss = userService.createUser("dsdss", "secret").orElseThrow(IllegalStateException::new); final int freefdId = freefd.getUid(); - int mid = messagesService.createMessage(dsdss.getUid(), "yo", null, new ArrayList<>()); + int mid = messagesService.createMessage(dsdss.getUid(), "yo", null, Set.of()); messagesService.likeMessage(mid, freefdId, 2); messagesService.likeMessage(mid, freefdId, 2); messagesService.likeMessage(mid, freefdId, 3); @@ -591,7 +590,7 @@ public class ServerTests { @Test public void messageUpdatedTimeShouldMatchLastReplyTime() throws InterruptedException { User hugnich4 = userService.createUser("hugnich4", "x").orElseThrow(IllegalStateException::new); - int mid = messagesService.createMessage(hugnich4.getUid(), "yo", null, null); + int mid = messagesService.createMessage(hugnich4.getUid(), "yo", null, Set.of()); Instant ts = jdbcTemplate .queryForObject("SELECT updated FROM messages WHERE message_id=?", Timestamp.class, mid).toInstant(); Thread.sleep(1000); @@ -686,7 +685,7 @@ public class ServerTests { String freefdHash = userService.getHashByUID(freefd.getUid()); String userIdHash = userService.getHashByUID(dsds4345.getUid()); final int freefdId = freefd.getUid(); - int mid = messagesService.createMessage(dsds4345.getUid(), "yo", null, new ArrayList<>()); + int mid = messagesService.createMessage(dsds4345.getUid(), "yo", null, Set.of()); messagesService.likeMessage(mid, freefdId, 2); messagesService.likeMessage(mid, freefdId, 2); messagesService.likeMessage(mid, freefdId, 3); @@ -704,8 +703,8 @@ public class ServerTests { public void tags() throws Exception { Tag weather = tagService.getTag("weather", true); Tag yo = tagService.getTag("yo", true); - messagesService.createMessage(ugnich.getUid(), "text", null, Arrays.asList(yo, weather)); - messagesService.createMessage(freefd.getUid(), "text2", null, Collections.singletonList(yo)); + messagesService.createMessage(ugnich.getUid(), "text", null, Set.of(yo, weather)); + messagesService.createMessage(freefd.getUid(), "text2", null, Set.of(yo)); MvcResult result = mockMvc.perform(get("/api/tags")).andExpect(status().isOk()).andReturn(); List<TagStats> tagsFromApi = jsonMapper.readValue(result.getResponse().getContentAsString(), new TypeReference<>() { @@ -797,7 +796,7 @@ public class ServerTests { @Test public void topTest() { - int topmid = messagesService.createMessage(ugnich.getUid(), "top message", null, null); + int topmid = messagesService.createMessage(ugnich.getUid(), "top message", null, Set.of()); IntStream.rangeClosed(6, 12).forEach(i -> { User next = new User(); next.setUid(i); @@ -828,7 +827,7 @@ public class ServerTests { tagService.updateTags(topmid, Collections.singletonList(nsfw)); assertThat(messagesService.getPopularCandidates().isEmpty(), is(true)); User recommender = userService.createUser("recommender2", "x").orElseThrow(IllegalStateException::new); - int anotherMid = messagesService.createMessage(ugnich.getUid(), "top2", null, null); + int anotherMid = messagesService.createMessage(ugnich.getUid(), "top2", null, Set.of()); messagesService.recommendMessage(anotherMid, freefd.getUid()); messagesService.recommendMessage(anotherMid, recommender.getUid()); assertThat(messagesService.getPopularCandidates().get(0), is(anotherMid)); @@ -1003,7 +1002,7 @@ public class ServerTests { @Test public void mailParserTest() throws Exception { emailService.addEmail(ugnich.getUid(), "ugnich@example.com"); - int mid = messagesService.createMessage(ugnich.getUid(), "text", StringUtils.EMPTY, Collections.emptyList()); + int mid = messagesService.createMessage(ugnich.getUid(), "text", StringUtils.EMPTY, Set.of()); String mail = String.format( "MIME-Version: 1.0\n" + "Received: by 10.176.0.242 with HTTP; Fri, 16 Mar 2018 05:31:50 -0700 (PDT)\n" + "In-Reply-To: <%d.0@juick.com>\n" + "References: <%d.0@juick.com>\n" @@ -1038,9 +1037,9 @@ public class ServerTests { @Test public void recommendTests() throws Exception { - int mid = messagesService.createMessage(ugnich.getUid(), "to be liked", null, null); + int mid = messagesService.createMessage(ugnich.getUid(), "to be liked", null, Set.of()); String freefdHash = userService.getHashByUID(freefd.getUid()); - int freefdMid = messagesService.createMessage(freefd.getUid(), "to be not liked", null, null); + int freefdMid = messagesService.createMessage(freefd.getUid(), "to be not liked", null, Set.of()); mockMvc.perform(post("/api/like?mid=" + mid + "&hash=" + freefdHash)).andExpect(status().isOk()) .andExpect(jsonPath("$.status", is("Message is added to your recommendations"))); @@ -1054,7 +1053,7 @@ public class ServerTests { public void likesTests() throws Exception { User dsds = userService.createUser("dsds", "secret").orElseThrow(IllegalStateException::new); String freefdHash = userService.getHashByUID(freefd.getUid()); - int mid1 = messagesService.createMessage(dsds.getUid(), "yo", null, new ArrayList<>()); + int mid1 = messagesService.createMessage(dsds.getUid(), "yo", null, Set.of()); mockMvc.perform(post("/api/react?mid=" + mid1 + "&hash=" + freefdHash + "&reactionId=2")) .andExpect(status().isOk()); @@ -1079,7 +1078,7 @@ public class ServerTests { jdbcTemplate.execute("DELETE FROM bl_users"); jdbcTemplate.execute("DELETE FROM messages"); assertThat(userService.isInBLAny(ugnich.getUid(), freefd.getUid()), is(false)); - int mid = messagesService.createMessage(ugnich.getUid(), "to be watched", null, null); + int mid = messagesService.createMessage(ugnich.getUid(), "to be watched", null, Set.of()); subscriptionService.subscribeMessage(messagesService.getMessage(mid).get(), ugnich); messagesService.createReply(mid, 0, freefd, "new reply", null); BiFunction<User, Integer, Integer> lastRead = (user, m) -> jdbcTemplate.queryForObject( @@ -1118,7 +1117,7 @@ public class ServerTests { @Test public void feedsShouldNotContainMessagesWithBannedTags() { Tag banned = tagService.getTag("banned", true); - int mid = messagesService.createMessage(ugnich.getUid(), "yo", "jpg", Collections.singletonList(banned)); + int mid = messagesService.createMessage(ugnich.getUid(), "yo", "jpg", Set.of(banned)); privacyQueriesService.blacklistTag(freefd, banned); assertThat(messagesService.getMessages(AnonymousUser.INSTANCE, messagesService.getAll(freefd.getUid(), 0)) .stream().noneMatch(m -> m.getTags().contains(banned)), is(true)); @@ -1142,8 +1141,7 @@ public class ServerTests { .stream().noneMatch(m -> m.getTags().contains(banned)), is(true)); User newUser1 = userService.createUser("newUser1", "12345").orElseThrow(IllegalStateException::new); - int newMid = messagesService.createMessage(newUser1.getUid(), "people", null, - Collections.singletonList(banned)); + int newMid = messagesService.createMessage(newUser1.getUid(), "people", null, Set.of(banned)); messagesService.recommendMessage(newMid, ugnich.getUid()); assertThat( messagesService.getMessages(AnonymousUser.INSTANCE, messagesService.getMyFeed(freefd.getUid(), 0, true)) @@ -1291,7 +1289,7 @@ public class ServerTests { assertThat(nonblmsgs.get(0).getReplies(), is(5)); commandsManager.processCommand(ugnich, "BL @freefd", emptyUri); Tag tag = tagService.getTag("linux", true); - int freefdMsg = messagesService.createMessage(freefd.getUid(), "sux", null, Collections.singletonList(tag)); + int freefdMsg = messagesService.createMessage(freefd.getUid(), "sux", null, Set.of(tag)); assertThat(messagesService.getTag(tag.TID, freefd.getUid(), 0, 10).size(), is(1)); assertThat(messagesService.getTag(tag.TID, ugnich.getUid(), 0, 10).size(), is(0)); messagesService.recommendMessage(freefdMsg, serviceUser.getUid()); @@ -1459,7 +1457,7 @@ public class ServerTests { subscriptionService.subscribeUser(reader, lateRecommender); Tag sampleTag = tagService.getTag("banned", true); int posterMid = messagesService.createMessage(poster.getUid(), "YO", null, - Collections.singletonList(sampleTag)); + Set.of(sampleTag)); messagesService.recommendMessage(posterMid, recommender.getUid()); BiFunction<Integer, Message, List<User>> subscribers = (recommId, msg) -> subscriptionService .getUsersSubscribedToUserRecommendations(recommId, msg); @@ -1479,7 +1477,7 @@ public class ServerTests { List<User> lateRecommendSubscribers = subscribers.apply(recommender.getUid(), messagesService.getMessage(posterMid).get()); assertThat(lateRecommendSubscribers.size(), is(0)); - int readerMid = messagesService.createMessage(reader.getUid(), "PEOPLE", null, null); + int readerMid = messagesService.createMessage(reader.getUid(), "PEOPLE", null, Set.of()); messagesService.recommendMessage(readerMid, recommender.getUid()); assertThat(subscribers.apply(recommender.getUid(), messagesService.getMessage(readerMid).get()).size(), is(0)); } @@ -1489,7 +1487,7 @@ public class ServerTests { User poster = userService.createUser("p", "secret").orElseThrow(IllegalStateException::new); User commenter = userService.createUser("cc", "secret").orElseThrow(IllegalStateException::new); User mentioner = userService.createUser("mmm", "secret").orElseThrow(IllegalStateException::new); - int mid = messagesService.createMessage(poster.getUid(), "who is dick?", null, null); + int mid = messagesService.createMessage(poster.getUid(), "who is dick?", null, Set.of()); Message msg = messagesService.getMessage(mid).get(); int rid = messagesService.createReply(mid, 0, commenter, "@mmm is dick", null); Message reply = messagesService.getReply(mid, rid); @@ -1503,12 +1501,12 @@ public class ServerTests { @Test public void mentionsInPosts() { jdbcTemplate.execute("DELETE FROM bl_users"); - int mid = messagesService.createMessage(ugnich.getUid(), "@freefd is dick", null, null); + int mid = messagesService.createMessage(ugnich.getUid(), "@freefd is dick", null, Set.of()); Message msg = messagesService.getMessage(mid).get(); assertThat(subscriptionService.getSubscribedUsers(ugnich.getUid(), msg).get(0), is(freefd)); privacyQueriesService.blacklistUser(freefd, ugnich); assertThat(subscriptionService.getSubscribedUsers(ugnich.getUid(), msg).size(), is(0)); - int mid2 = messagesService.createMessage(freefd.getUid(), "@ugnich is dick", null, null); + int mid2 = messagesService.createMessage(freefd.getUid(), "@ugnich is dick", null, Set.of()); Message msg2 = messagesService.getMessage(mid2).get(); assertThat(subscriptionService.getSubscribedUsers(freefd.getUid(), msg2).get(0), is(ugnich)); jdbcTemplate.execute("DELETE FROM bl_users"); @@ -1547,7 +1545,7 @@ public class ServerTests { String msgText = "автор этого поста был забанен"; User isilmine = userService.createUser(userName, userPassword).orElseThrow(IllegalStateException::new); - int mid = messagesService.createMessage(isilmine.getUid(), msgText, null, null); + int mid = messagesService.createMessage(isilmine.getUid(), msgText, null, Set.of()); mockMvc.perform(get(String.format("/api/thread?mid=%d", mid)).with(httpBasic(ugnichName, ugnichPassword))) .andExpect(status().isOk()); jdbcTemplate.update("UPDATE users SET banned=1 WHERE id=?", isilmine.getUid()); @@ -1579,7 +1577,7 @@ public class ServerTests { User monstreek = userService.createUser("monstreek", "secret").orElseThrow(IllegalStateException::new); User pogo = userService.createUser("pogo", "secret").orElseThrow(IllegalStateException::new); User fmap = userService.createUser("fmap", "secret").orElseThrow(IllegalStateException::new); - int mid = messagesService.createMessage(monstreek.getUid(), "KURWA", null, null); + int mid = messagesService.createMessage(monstreek.getUid(), "KURWA", null, Set.of()); assertThat(messagesService.recommendMessage(mid, ermine.getUid()), is(MessagesService.RecommendStatus.Added)); assertThat(messagesService.recommendMessage(mid, fmap.getUid()), is(MessagesService.RecommendStatus.Added)); assertThat(messagesService.recommendMessage(mid, pogo.getUid()), is(MessagesService.RecommendStatus.Added)); @@ -1604,12 +1602,12 @@ public class ServerTests { User casualUser = userService.createUser("user", "secret").orElseThrow(IllegalStateException::new); User bannedUser = userService.createUser("banned", "banned").orElseThrow(IllegalStateException::new); jdbcTemplate.update("UPDATE users SET banned=1 WHERE id=?", bannedUser.getUid()); - messagesService.createMessage(bannedUser.getUid(), "KURWA", null, Collections.emptyList()); + messagesService.createMessage(bannedUser.getUid(), "KURWA", null, Set.of()); assertThat(messagesService.getAll(casualUser.getUid(), 0).size(), is(0)); assertThat(messagesService.getDiscussions(casualUser.getUid(), 0L).size(), is(0)); assertThat(messagesService.getDiscussions(0, 0L).size(), is(0)); assertThat(messagesService.getAll(bannedUser.getUid(), 0).size(), is(1)); - int mid = messagesService.createMessage(casualUser.getUid(), "PEACE", null, Collections.emptyList()); + int mid = messagesService.createMessage(casualUser.getUid(), "PEACE", null, Set.of()); User banned = userService.getUserByName("banned"); int bannedRid = messagesService.createReply(mid, 0, banned, "KURWA", null); int casualRid = messagesService.createReply(mid, 0, userService.getUserByName("user"), "DOOR", null); @@ -1640,7 +1638,7 @@ public class ServerTests { jdbcTemplate.execute("DELETE FROM messages"); List<Integer> mids = IteratorUtils.toList(IntStream.rangeClosed(1, 30) .mapToObj( - i -> messagesService.createMessage(ugnich.getUid(), String.format("message %d", i), null, null)) + i -> messagesService.createMessage(ugnich.getUid(), String.format("message %d", i), null, Set.of())) .collect(Collectors.toCollection(ArrayDeque::new)).descendingIterator()); List<Integer> midsPage = mids.stream().limit(20).collect(Collectors.toList()); mockMvc.perform(get("/u/ugnich/blog").accept(Context.ACTIVITYSTREAMS_PROFILE_MEDIA_TYPE)) @@ -1652,14 +1650,14 @@ public class ServerTests { @Test public void postWithoutTagsShouldNotHaveAsteriskInTitle() throws Exception { String msgText = "Привет, я - Угнич"; - int mid = messagesService.createMessage(ugnich.getUid(), msgText, null, null); + int mid = messagesService.createMessage(ugnich.getUid(), msgText, null, Set.of()); HtmlPage threadPage = webClient.getPage(String.format("http://localhost:8080/ugnich/%d", mid)); assertThat(threadPage.getTitleText(), equalTo("ugnich:")); } @Test public void repliesList() throws IOException { - int mid = messagesService.createMessage(ugnich.getUid(), "hello", null, null); + int mid = messagesService.createMessage(ugnich.getUid(), "hello", null, Set.of()); IntStream.range(1, 15) .forEach(i -> messagesService.createReply(mid, i - 1, freefd, String.valueOf(i - 1), null)); @@ -1675,7 +1673,7 @@ public class ServerTests { @Test public void userShouldNotSeeReplyButtonToBannedUser() throws Exception { - int mid = messagesService.createMessage(ugnich.getUid(), "freefd bl me", null, null); + int mid = messagesService.createMessage(ugnich.getUid(), "freefd bl me", null, Set.of()); messagesService.createReply(mid, 0, ugnich, "yo", null); MvcResult loginResult = mockMvc .perform(post("/login").param("username", freefdName).param("password", freefdPassword)) @@ -1718,11 +1716,11 @@ public class ServerTests { @Test public void testTwitterCards() throws Exception { - int mid = messagesService.createMessage(ugnich.getUid(), "without image", null, null); + int mid = messagesService.createMessage(ugnich.getUid(), "without image", null, Set.of()); assertThat(fetchMeta(String.format("http://localhost:8080/ugnich/%d", mid), "twitter:card") .getAttribute("content"), equalTo("summary")); - int mid2 = messagesService.createMessage(ugnich.getUid(), "with image", "png", null); + int mid2 = messagesService.createMessage(ugnich.getUid(), "with image", "png", Set.of()); Message message = messagesService.getMessage(mid2).get(); assertThat(fetchMeta(String.format("http://localhost:8080/ugnich/%d", mid2), "twitter:card") .getAttribute("content"), equalTo("summary_large_image")); @@ -1759,8 +1757,8 @@ public class ServerTests { @Test public void discussionsShouldBePageableByTimestamp() throws Exception { String msgText = "Привет, я снова Угнич"; - int mid = messagesService.createMessage(ugnich.getUid(), msgText, null, null); - int midNew = messagesService.createMessage(ugnich.getUid(), "Я более новый Угнич", null, null); + int mid = messagesService.createMessage(ugnich.getUid(), msgText, null, Set.of()); + int midNew = messagesService.createMessage(ugnich.getUid(), "Я более новый Угнич", null, Set.of()); MvcResult loginResult = mockMvc .perform(post("/login").param("username", freefdName).param("password", freefdPassword)) .andExpect(status().is3xxRedirection()).andReturn(); @@ -1792,7 +1790,7 @@ public class ServerTests { assertThat(discussionsOld.querySelectorAll("article").get(0).getAttributes().getNamedItem("data-mid") .getNodeValue(), is(String.valueOf(midNew))); List<Integer> newMids = IntStream.rangeClosed(1, 19) - .map(i -> messagesService.createMessage(ugnich.getUid(), String.valueOf(i), null, null)).boxed() + .map(i -> messagesService.createMessage(ugnich.getUid(), String.valueOf(i), null, Set.of())).boxed() .collect(Collectors.toList()); for (Integer m : newMids) { subscriptionService.subscribeMessage(messagesService.getMessage(m).get(), freefd); @@ -1828,7 +1826,7 @@ public class ServerTests { @Test public void anythingRedirects() throws Exception { - int mid = messagesService.createMessage(ugnich.getUid(), "yo", null, null); + int mid = messagesService.createMessage(ugnich.getUid(), "yo", null, Set.of()); mockMvc.perform(get(String.format("/%d", mid))).andExpect(status().isMovedPermanently()) .andExpect(redirectedUrl(String.format("/%s/%d", ugnich.getName(), mid))); } @@ -1853,7 +1851,7 @@ public class ServerTests { webClient.setCookieManager(new CookieManager()); webClient.getCookieManager().addCookie(new com.gargoylesoftware.htmlunit.util.Cookie(loginCookie.getDomain(), loginCookie.getName(), loginCookie.getValue())); - int mid = messagesService.createMessage(ugnich.getUid(), "new test", null, null); + int mid = messagesService.createMessage(ugnich.getUid(), "new test", null, Set.of()); subscriptionService.subscribeMessage(messagesService.getMessage(mid).get(), freefd); messagesService.createReply(mid, 0, ugnich, "new reply", null); HtmlPage discussionsPage = webClient.getPage("http://localhost:8080/?show=discuss"); @@ -1932,7 +1930,7 @@ public class ServerTests { String attachment = attachmentObj != null ? (String) attachmentObj.getUrl() : StringUtils.EMPTY; String deleteJsonStr = IOUtils.toString(new ClassPathResource("delete.json").getURI(), StandardCharsets.UTF_8); Delete delete = jsonMapper.readValue(deleteJsonStr, Delete.class); - int mid = messagesService.createMessage(ugnich.getUid(), "YO", "", null); + int mid = messagesService.createMessage(ugnich.getUid(), "YO", "", Set.of()); User extUser = new User(); extUser.setUri(URI.create("http://localhost:8080/users/xwatt")); int rid = messagesService.createReply(mid, 0, extUser, "PEOPLE", null); @@ -2103,7 +2101,7 @@ public class ServerTests { User newUser = userService.createUser("newuser", "assword").orElseThrow(IllegalStateException::new); assertThat(newUser.getUid(), greaterThanOrEqualTo(0)); assertThat(newUser.getSeen(), is(nullValue())); - messagesService.createMessage(newUser.getUid(), "YO", "", null); + messagesService.createMessage(newUser.getUid(), "YO", "", Set.of()); assertThat(userService.getUserByUID(newUser.getUid()).get().getSeen(), greaterThanOrEqualTo(now)); } @@ -2311,7 +2309,7 @@ public class ServerTests { @Test public void federatedAttachmentsAsLinks() throws Exception { - int mid = messagesService.createMessage(ugnich.getUid(), "test", StringUtils.EMPTY, Collections.emptyList()); + int mid = messagesService.createMessage(ugnich.getUid(), "test", StringUtils.EMPTY, Set.of()); Message testMessage = MockUtils.mockMessage(mid, freefd, "reply"); String activity = IOUtils.toString(noteWithDocument.getInputStream(), StandardCharsets.UTF_8); Announce announce = jsonMapper.readValue(activity, Announce.class); @@ -2363,7 +2361,7 @@ public class ServerTests { @Test public void messagePropertiesTest() { - int mid = messagesService.createMessage(ugnich.getUid(), "YO", null, null); + int mid = messagesService.createMessage(ugnich.getUid(), "YO", null, Set.of()); messagesService.setMessageProperty(mid, 0, "tg_id", "YO"); assertThat(messagesService.getMessageProperty(mid, 0, "tg_id"), is("YO")); messagesService.setMessageProperty(mid, 0, "tg_id", "YO2"); @@ -2402,7 +2400,7 @@ public class ServerTests { @Test public void wsThreadsShouldRedirect() throws Exception { - int mid = messagesService.createMessage(ugnich.getUid(), "tst", null, Collections.emptyList()); + int mid = messagesService.createMessage(ugnich.getUid(), "tst", null, Set.of()); mockMvc.perform(get("/ugnich/" + mid)).andExpect(status().isOk()); mockMvc.perform(get("/s/" + mid)).andExpect(status().isFound()).andExpect(redirectedUrl("/ugnich/" + mid)); mockMvc.perform(get("/ws/" + mid)).andExpect(status().isFound()).andExpect(redirectedUrl("/ugnich/" + mid)); @@ -2466,13 +2464,15 @@ public class ServerTests { @Test public void readonlyTest() throws Exception { - var result = commandsManager.processCommand(ugnich, "YO", emptyUri); + var result = commandsManager.processCommand(ugnich, "*readonly YO", emptyUri); var mid = result.getNewMessage().get().getMid(); - jdbcTemplate.update("UPDATE messages SET readonly=1 WHERE message_id=?", mid); var readonlyResult = commandsManager.processCommand(freefd, String.format("#%d PEOPLE", mid), emptyUri); assertThat(readonlyResult.getNewMessage().isPresent(), is(false)); var authorResult = commandsManager.processCommand(ugnich, String.format("#%d PEOPLE", mid), emptyUri); assertThat(authorResult.getNewMessage().isPresent(), is(true)); + commandsManager.processCommand(ugnich, String.format("#%d *readonly", mid), emptyUri); + Message updatedMessage = messagesService.getMessage(mid).orElseThrow(); + assertThat(updatedMessage.ReadOnly, is(false)); } @Test |