diff options
author | Vitaly Takmazov | 2022-03-03 15:15:39 +0300 |
---|---|---|
committer | Vitaly Takmazov | 2022-03-03 15:15:39 +0300 |
commit | c5d03ea70ce732e0eabc7ea90b13b8ab95601c07 (patch) | |
tree | 394417ff54f2067d0e9882e80275d3fbc597a959 /src/main/java/com/juick/service | |
parent | 5f06a38b6c2a6641900f79c051c1885a935ff9c3 (diff) |
Fix *readonly tag
Diffstat (limited to 'src/main/java/com/juick/service')
4 files changed, 26 insertions, 13 deletions
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); } |