diff options
author | Vitaly Takmazov | 2023-02-03 03:02:09 +0300 |
---|---|---|
committer | Vitaly Takmazov | 2023-02-03 03:39:43 +0300 |
commit | febad867a57cfa1ae33c83dd9c0ddf51a303eb03 (patch) | |
tree | d21e0c5f1f2d9d5356a26aaaf6651418bd34049c | |
parent | 22aaf28c779725962a3ebec3e689bef35fe05085 (diff) |
tag.isIgnored, tag.isSubscribed
-rw-r--r-- | src/main/java/com/juick/model/Tag.java | 20 | ||||
-rw-r--r-- | src/main/java/com/juick/service/TagService.java | 11 | ||||
-rw-r--r-- | src/main/java/com/juick/service/TagServiceImpl.java | 71 | ||||
-rw-r--r-- | src/main/java/com/juick/www/controllers/Site.java | 6 |
4 files changed, 66 insertions, 42 deletions
diff --git a/src/main/java/com/juick/model/Tag.java b/src/main/java/com/juick/model/Tag.java index 64469a69..9ddda315 100644 --- a/src/main/java/com/juick/model/Tag.java +++ b/src/main/java/com/juick/model/Tag.java @@ -45,6 +45,10 @@ public class Tag implements Comparable<Tag>, Serializable { private boolean noindex; @XmlTransient private boolean notop; + @XmlTransient + private boolean subscribed; + @XmlTransient + private boolean ignored; public Tag() { // required for (de)serialization @@ -108,4 +112,20 @@ public class Tag implements Comparable<Tag>, Serializable { public void setHiddenFromTop(boolean notop) { this.notop = notop; } + + public boolean isSubscribed() { + return subscribed; + } + + public void setSubscribed(boolean subscribed) { + this.subscribed = subscribed; + } + + public boolean isIgnored() { + return ignored; + } + + public void setIgnored(boolean ignored) { + this.ignored = ignored; + } } diff --git a/src/main/java/com/juick/service/TagService.java b/src/main/java/com/juick/service/TagService.java index 0701bedf..84bde121 100644 --- a/src/main/java/com/juick/service/TagService.java +++ b/src/main/java/com/juick/service/TagService.java @@ -1,5 +1,5 @@ /* - * Copyright (C) 2008-2020, Juick + * Copyright (C) 2008-2023, Juick * * This program is free software: you can redistribute it and/or modify * it under the terms of the GNU Affero General Public License as @@ -33,10 +33,11 @@ import java.util.stream.Stream; */ public interface TagService { Tag getTag(int tid); - + Tag getTag(int userId, int tid); Tag getTag(String tag, boolean autoCreate); - + Tag getTag(int userId, String tag, boolean autoCreate); List<Tag> getTags(Stream<String> tags, boolean autoCreate); + List<Tag> getTags(int userId, Stream<String> tags, boolean autoCreate); int createTag(String name); @@ -58,8 +59,4 @@ public interface TagService { List<Integer> getMessageTagsIDs(int mid); boolean blacklistTag(User user, Tag tag); - - boolean isInBL(User user, Tag tag); - - boolean isSubscribed(User user, Tag tag); } diff --git a/src/main/java/com/juick/service/TagServiceImpl.java b/src/main/java/com/juick/service/TagServiceImpl.java index 0d040273..403f05c4 100644 --- a/src/main/java/com/juick/service/TagServiceImpl.java +++ b/src/main/java/com/juick/service/TagServiceImpl.java @@ -57,31 +57,52 @@ public class TagServiceImpl extends BaseJdbcService implements TagService { tag.setSynonymId(rs.getInt(2)); tag.setNotIndexed(rs.getInt("noindex") > 0); tag.setHiddenFromTop(rs.getInt("notop") > 0); + tag.setSubscribed(rs.getInt("subscribed") > 0); + tag.setIgnored(rs.getInt("ignored") > 0); return tag; } } + @Override + public Tag getTag(int tid) { + return getTag(0, tid); + } @Transactional(readOnly = true) @Override - public Tag getTag(final int tid) { - List<Tag> list = getJdbcTemplate().query( - "SELECT tag_id, synonym_id, name, noindex, notop FROM tags WHERE tag_id=?", - new TagMapper(), - tid); + public Tag getTag(int userId, int tid) { + List<Tag> list = namedParameterJdbcTemplate.query(""" + SELECT tag_id, synonym_id, name, noindex, notop, + (EXISTS(SELECT 1 FROM bl_tags WHERE tag_id=:tagId AND user_id=:userId)) ignored, + (EXISTS(SELECT 1 FROM subscr_tags WHERE tag_id=:tagId AND suser_id=:userId)) subscribed + FROM tags WHERE tag_id = :tagId""", + new MapSqlParameterSource() + .addValue("userId", userId) + .addValue("tagId", tid), + new TagMapper()); return list.isEmpty() ? null : list.get(0); } + @Override + public Tag getTag(final String tagName, boolean autoCreate) { + return getTag(0, tagName, autoCreate); + } + @Transactional @Override - public Tag getTag(final String tagName, final boolean autoCreate) { + public Tag getTag(int userId, final String tagName, boolean autoCreate) { if (StringUtils.isBlank(tagName)) return null; - - List<Tag> list = getJdbcTemplate().query( - "SELECT tag_id, synonym_id, name, noindex, notop FROM tags WHERE name = ?", - new TagMapper(), - tagName); + List<Tag> list = namedParameterJdbcTemplate.query(""" + SELECT t.tag_id, t.synonym_id, t.name, t.noindex, t.notop, + (EXISTS(SELECT 1 FROM bl_tags WHERE tag_id=t.tag_id AND user_id=:userId)) ignored, + (EXISTS(SELECT 1 FROM subscr_tags WHERE tag_id=t.tag_id AND suser_id=:userId)) subscribed + FROM (SELECT tag_id, synonym_id, name, noindex, notop FROM tags) t + WHERE name = :tagName""", + new MapSqlParameterSource() + .addValue("userId", userId) + .addValue("tagName", tagName), + new TagMapper()); Tag ret = list.isEmpty() ? null : list.get(0); @@ -95,6 +116,11 @@ public class TagServiceImpl extends BaseJdbcService implements TagService { @Override public List<Tag> getTags(Stream<String> tags, final boolean autoCreate) { + return getTags(0, tags, autoCreate); + } + + @Override + public List<Tag> getTags(int userId, Stream<String> tags, final boolean autoCreate) { return tags.filter(StringUtils::isNotBlank).map(tag -> getTag(tag, autoCreate)).filter(Objects::nonNull) .distinct() .toList(); @@ -222,8 +248,8 @@ public class TagServiceImpl extends BaseJdbcService implements TagService { @Override 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]*")); + 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); Set<Tag> tags = tagsStream.get().map(t -> getTag(t.substring(1), true)).collect(Collectors.toSet()); @@ -263,23 +289,4 @@ public class TagServiceImpl extends BaseJdbcService implements TagService { new MapSqlParameterSource().addValue("tid", tag.getId()) .addValue("uid", user.getUid())) > 0; } - - @Transactional(readOnly = true) - @Override - public boolean isInBL(User user, Tag tag) { - List<Integer> list = getJdbcTemplate().queryForList( - "SELECT 1 FROM bl_tags WHERE user_id = ? AND tag_id = ?", - Integer.class, user.getUid(), tag.getId()); - return !list.isEmpty() && list.get(0) == 1; - } - - @Transactional(readOnly = true) - @Override - public boolean isSubscribed(User user, Tag tag) { - List<Integer> list = getJdbcTemplate().queryForList( - "SELECT 1 FROM subscr_tags WHERE suser_id = ? AND tag_id = ?", - Integer.class, user.getUid(), tag.getId()); - return !list.isEmpty() && list.get(0) == 1; - } - } diff --git a/src/main/java/com/juick/www/controllers/Site.java b/src/main/java/com/juick/www/controllers/Site.java index 2a1c89ae..9773c098 100644 --- a/src/main/java/com/juick/www/controllers/Site.java +++ b/src/main/java/com/juick/www/controllers/Site.java @@ -397,7 +397,7 @@ public class Site { @RequestParam(required = false, defaultValue = "0") int before, ModelMap model) { visitor.setAvatar(webApp.getAvatarWebPath(visitor)); String paramTagStr = StringEscapeUtils.unescapeHtml4(tagName); - Tag paramTag = tagService.getTag(paramTagStr, false); + Tag paramTag = tagService.getTag(visitor.getUid(), paramTagStr, false); if (paramTag == null) { throw new HttpNotFoundException(); } else if (paramTag.getSynonymId() > 0 && paramTag.getId() != paramTag.getSynonymId()) { @@ -445,8 +445,8 @@ public class Site { model.addAttribute("msgs", msgs); model.addAttribute("tags", tagService.getPopularTags()); model.addAttribute("noindex", before > 0); - model.addAttribute("isSubscribed", tagService.isSubscribed(visitor, paramTag)); - model.addAttribute("isInBL", tagService.isInBL(visitor, paramTag)); + model.addAttribute("isSubscribed", paramTag.isSubscribed()); + model.addAttribute("isInBL", paramTag.isIgnored()); if (mids.size() >= 20) { String nextpage = "/tag/" + URLEncoder.encode(paramTag.getName(), StandardCharsets.UTF_8) + "?before=" + mids.get(mids.size() - 1); |