From c5d03ea70ce732e0eabc7ea90b13b8ab95601c07 Mon Sep 17 00:00:00 2001 From: Vitaly Takmazov Date: Thu, 3 Mar 2022 15:15:39 +0300 Subject: Fix *readonly tag --- src/main/java/com/juick/CommandsManager.java | 10 +++++++--- src/main/java/com/juick/TwitterManager.java | 4 +++- src/main/java/com/juick/service/MessagesService.java | 4 +++- src/main/java/com/juick/service/MessagesServiceImpl.java | 15 ++++++++++++--- src/main/java/com/juick/service/TagService.java | 5 +++-- src/main/java/com/juick/service/TagServiceImpl.java | 15 ++++++++------- src/main/java/com/juick/util/TagUtils.java | 7 +++++++ 7 files changed, 43 insertions(+), 17 deletions(-) (limited to 'src/main/java/com/juick') 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> tags = tagService.fromString(input); + Pair> 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> messageTags = tagService.fromString(txt); + Pair> 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 tags); + int createMessage(int uid, String txt, String attachment, Set 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 tags) { + public int createMessage(final int uid, final String txt, final String attachment, @Nonnull final Set 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 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 getTagStats(); - List updateTags(int mid, Collection newTags); + Set updateTags(int mid, Collection newTags); - Pair> fromString(String txt); + Pair> fromString(String txt); List 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 updateTags(final int mid, final Collection newTags) { - List currentTags = getMessageTags(mid).stream() - .map(TagStats::getTag).collect(Collectors.toList()); + public Set updateTags(final int mid, final Collection newTags) { + Set 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> fromString(final String txt) { + public Pair> fromString(final String txt) { String firstLine = txt.split("\\n", 2)[0]; Supplier> 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 tags = tagsStream.get().map(t -> getTag(t.substring(1), true)) - .distinct().collect(Collectors.toList()); + Set 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 tags, String name) { + return tags.stream().anyMatch(tag -> tag.getName().equals(name)); + } + public static boolean hasNoTag(final Set tags, String name) { + return tags.stream().noneMatch(tag -> tag.getName().equals(name)); + } } -- cgit v1.2.3