diff options
Diffstat (limited to 'src/main/java/com/juick/service')
-rw-r--r-- | src/main/java/com/juick/service/TagService.java | 11 | ||||
-rw-r--r-- | src/main/java/com/juick/service/TagServiceImpl.java | 71 |
2 files changed, 43 insertions, 39 deletions
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; - } - } |