diff options
author | Vitaly Takmazov | 2023-01-28 18:24:32 +0300 |
---|---|---|
committer | Vitaly Takmazov | 2023-01-28 19:07:28 +0300 |
commit | 906bf8ef21463f99820764807b656cb617b0aaa2 (patch) | |
tree | 542c02066866decfe50c7127578967b3b0b02537 /src/main | |
parent | 5700d7c5dd1f578cd52451074ce316e192532ca6 (diff) |
Cleanup Tag model
Diffstat (limited to 'src/main')
8 files changed, 51 insertions, 32 deletions
diff --git a/src/main/java/com/juick/CommandsManager.java b/src/main/java/com/juick/CommandsManager.java index ec89975c..702433e0 100644 --- a/src/main/java/com/juick/CommandsManager.java +++ b/src/main/java/com/juick/CommandsManager.java @@ -481,7 +481,7 @@ public class CommandsManager { Tag tag = tagService.getTag(arguments[0], false); if (tag != null) { // TODO: synonyms - List<Integer> mids = messagesService.getTag(tag.TID, user.getUid(), 0, 10); + List<Integer> mids = messagesService.getTag(tag.getId(), user.getUid(), 0, 10); return CommandResult.fromString("Last messages with *" + tag.getName() + ":\n" + printMessages(user, mids, true)); } return CommandResult.fromString("Tag not found"); diff --git a/src/main/java/com/juick/model/Tag.java b/src/main/java/com/juick/model/Tag.java index e7fcde5d..b27fe7e6 100644 --- a/src/main/java/com/juick/model/Tag.java +++ b/src/main/java/com/juick/model/Tag.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 @@ -38,9 +38,9 @@ public class Tag implements Comparable<Tag>, Serializable { private String name; @XmlTransient - public int TID = 0; + private int id; @XmlTransient - public int SynonymID = 0; + private int synonymId; public Tag() { // required for (de)serialization @@ -76,4 +76,20 @@ public class Tag implements Comparable<Tag>, Serializable { public int compareTo(Tag o) { return Objects.compare(name, o.getName(), Comparator.naturalOrder()); } + + public int getId() { + return id; + } + + public void setId(int id) { + this.id = id; + } + + public int getSynonymId() { + return synonymId; + } + + public void setSynonymId(int synonymId) { + this.synonymId = synonymId; + } } diff --git a/src/main/java/com/juick/service/MessagesServiceImpl.java b/src/main/java/com/juick/service/MessagesServiceImpl.java index 790e695e..4247cee5 100644 --- a/src/main/java/com/juick/service/MessagesServiceImpl.java +++ b/src/main/java/com/juick/service/MessagesServiceImpl.java @@ -153,7 +153,7 @@ public class MessagesServiceImpl extends BaseJdbcService implements MessagesServ @Override public void setValues(PreparedStatement ps, int i) throws SQLException { ps.setInt(1, mid); - ps.setInt(2, newTags.get(i).TID); + ps.setInt(2, newTags.get(i).getId()); } @Override @@ -1032,13 +1032,14 @@ public class MessagesServiceImpl extends BaseJdbcService implements MessagesServ @Override public List<Integer> getPopularCandidates() { var beforeTime = Instant.now().minus(2, ChronoUnit.HOURS); + var nsfw = tagService.getTag("NSFW", true); var sql = """ SELECT replies.message_id FROM replies INNER JOIN messages ON replies.message_id = messages.message_id LEFT JOIN favorites ON favorites.message_id = messages.message_id LEFT JOIN messages_tags ON messages_tags.message_id = messages.message_id WHERE COALESCE(messages_tags.tag_id, 0) != 2 - AND COALESCE(messages_tags.tag_id, 0) != 805 AND replies.ts > :before + AND COALESCE(messages_tags.tag_id, 0) != :nsfw_id AND replies.ts > :before AND NOT EXISTS (SELECT 1 FROM favorites WHERE message_id = messages.message_id AND user_id = 2) GROUP BY replies.message_id HAVING COUNT(DISTINCT(replies.user_id)) > 5 UNION ALL SELECT favorites.message_id FROM favorites @@ -1051,7 +1052,9 @@ public class MessagesServiceImpl extends BaseJdbcService implements MessagesServ GROUP BY favorites.message_id HAVING COUNT(DISTINCT favorites.user_id) > 2 """; return getNamedParameterJdbcTemplate().queryForList(sql, new MapSqlParameterSource() - .addValue("before", beforeTime.atOffset(ZoneOffset.UTC), java.sql.Types.TIMESTAMP_WITH_TIMEZONE), + .addValue("nsfw_id", nsfw.getId()) + .addValue("before", beforeTime.atOffset(ZoneOffset.UTC), + java.sql.Types.TIMESTAMP_WITH_TIMEZONE), Integer.class); } diff --git a/src/main/java/com/juick/service/PrivacyQueriesServiceImpl.java b/src/main/java/com/juick/service/PrivacyQueriesServiceImpl.java index e13b54fc..437c9f8c 100644 --- a/src/main/java/com/juick/service/PrivacyQueriesServiceImpl.java +++ b/src/main/java/com/juick/service/PrivacyQueriesServiceImpl.java @@ -49,14 +49,14 @@ public class PrivacyQueriesServiceImpl extends BaseJdbcService implements Privac public PrivacyResult blacklistTag(final User user, final Tag tag) { int result = getJdbcTemplate().update( "DELETE FROM bl_tags WHERE user_id = ? AND tag_id = ?", - user.getUid(), tag.TID); + user.getUid(), tag.getId()); if (result > 0) return PrivacyResult.Removed; getJdbcTemplate().update( "INSERT INTO bl_tags(user_id, tag_id) VALUES (?, ?)", - user.getUid(), tag.TID); + user.getUid(), tag.getId()); return PrivacyResult.Added; } diff --git a/src/main/java/com/juick/service/SubscriptionServiceImpl.java b/src/main/java/com/juick/service/SubscriptionServiceImpl.java index bf7d1045..fae1b824 100644 --- a/src/main/java/com/juick/service/SubscriptionServiceImpl.java +++ b/src/main/java/com/juick/service/SubscriptionServiceImpl.java @@ -175,7 +175,7 @@ public class SubscriptionServiceImpl extends BaseJdbcService implements Subscrip try { return getJdbcTemplate().update( - "INSERT INTO subscr_tags(tag_id,suser_id) VALUES (?,?)", toTag.TID, + "INSERT INTO subscr_tags(tag_id,suser_id) VALUES (?,?)", toTag.getId(), user.getUid()) == 1; } catch (DuplicateKeyException e) { return true; @@ -186,7 +186,7 @@ public class SubscriptionServiceImpl extends BaseJdbcService implements Subscrip @Override public boolean unSubscribeTag(final User user, final Tag toTag) { return getJdbcTemplate().update( - "DELETE FROM subscr_tags WHERE tag_id=? AND suser_id=?", toTag.TID, user.getUid()) > 0; + "DELETE FROM subscr_tags WHERE tag_id=? AND suser_id=?", toTag.getId(), user.getUid()) > 0; } @Transactional(readOnly = true) diff --git a/src/main/java/com/juick/service/TagServiceImpl.java b/src/main/java/com/juick/service/TagServiceImpl.java index 1f77eb81..f15fd113 100644 --- a/src/main/java/com/juick/service/TagServiceImpl.java +++ b/src/main/java/com/juick/service/TagServiceImpl.java @@ -60,8 +60,8 @@ public class TagServiceImpl extends BaseJdbcService implements TagService { "SELECT synonym_id,name FROM tags WHERE tag_id=?", (rs, num) -> { Tag ret = new Tag(rs.getString(2)); - ret.TID = tid; - ret.SynonymID = rs.getInt(1); + ret.setId(tid); + ret.setSynonymId(rs.getInt(1)); return ret; }, tid); @@ -79,8 +79,8 @@ public class TagServiceImpl extends BaseJdbcService implements TagService { "SELECT tag_id, synonym_id, name FROM tags WHERE name = ?", (rs, rowNum) -> { Tag ret1 = new Tag(rs.getString(3)); - ret1.TID = rs.getInt(1); - ret1.SynonymID = rs.getInt(2); + ret1.setId(rs.getInt(1)); + ret1.setSynonymId(rs.getInt(2)); return ret1; }, tag); @@ -89,7 +89,7 @@ public class TagServiceImpl extends BaseJdbcService implements TagService { if (ret == null && autoCreate) { ret = new Tag(tag); - ret.TID = createTag(tag); + ret.setId(createTag(tag)); } return ret; @@ -199,7 +199,7 @@ public class TagServiceImpl extends BaseJdbcService implements TagService { List<Integer> idsForDelete = newTags.stream() .filter(currentTags::contains) - .map(tag -> tag.TID) + .map(Tag::getId) .toList(); if (newTags.size() - idsForDelete.size() >= 5) { return currentTags; @@ -216,7 +216,7 @@ public class TagServiceImpl extends BaseJdbcService implements TagService { @Override public void setValues(PreparedStatement ps, int i) throws SQLException { ps.setInt(1, mid); - ps.setInt(2, addedTags.get(i).TID); + ps.setInt(2, addedTags.get(i).getId()); } @Override @@ -248,8 +248,8 @@ public class TagServiceImpl extends BaseJdbcService implements TagService { "INNER JOIN messages_tags ON (messages_tags.message_id = ? AND messages_tags.tag_id = tags.tag_id)", (rs, num) -> { Tag t = new Tag(rs.getString(3)); - t.TID = rs.getInt(1); - t.SynonymID = rs.getInt(2); + t.setId(rs.getInt(1)); + t.setSynonymId(rs.getInt(2)); TagStats s = new TagStats(); s.setTag(t); s.setUsageCount(rs.getInt(4)); @@ -269,10 +269,10 @@ public class TagServiceImpl extends BaseJdbcService implements TagService { public boolean blacklistTag(User user, Tag tag) { int rowcount = getNamedParameterJdbcTemplate().update( "DELETE FROM bl_tags WHERE tag_id = :tid AND user_id = :uid", - new MapSqlParameterSource().addValue("tid", tag.TID).addValue("uid", user.getUid())); + new MapSqlParameterSource().addValue("tid", tag.getId()).addValue("uid", user.getUid())); return rowcount <= 0 && getNamedParameterJdbcTemplate() .update("INSERT INTO bl_tags(user_id, tag_id) VALUES(:uid,:tid)", - new MapSqlParameterSource().addValue("tid", tag.TID) + new MapSqlParameterSource().addValue("tid", tag.getId()) .addValue("uid", user.getUid())) > 0; } @@ -281,7 +281,7 @@ public class TagServiceImpl extends BaseJdbcService implements TagService { 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.TID); + Integer.class, user.getUid(), tag.getId()); return !list.isEmpty() && list.get(0) == 1; } @@ -290,7 +290,7 @@ public class TagServiceImpl extends BaseJdbcService implements TagService { 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.TID); + Integer.class, user.getUid(), tag.getId()); return !list.isEmpty() && list.get(0) == 1; } diff --git a/src/main/java/com/juick/www/api/Messages.java b/src/main/java/com/juick/www/api/Messages.java index 7b54f126..06e3ac87 100644 --- a/src/main/java/com/juick/www/api/Messages.java +++ b/src/main/java/com/juick/www/api/Messages.java @@ -96,7 +96,7 @@ public class Messages { } else if (StringUtils.hasText(tag)) { Tag tagObject = tagService.getTag(tag, false); if (tagObject != null) { - mids = messagesService.getUserTag(user.getUid(), tagObject.TID, 0, before); + mids = messagesService.getUserTag(user.getUid(), tagObject.getId(), 0, before); } else { throw new HttpNotFoundException(); } @@ -121,7 +121,7 @@ public class Messages { } else if (StringUtils.hasText(tag)) { Tag tagObject = tagService.getTag(tag, false); if (tagObject != null) { - mids = messagesService.getTag(tagObject.TID, visitor.getUid(), before, 20); + mids = messagesService.getTag(tagObject.getId(), visitor.getUid(), before, 20); } else { throw new HttpNotFoundException(); } diff --git a/src/main/java/com/juick/www/controllers/Site.java b/src/main/java/com/juick/www/controllers/Site.java index d0ea3dee..eb483ccc 100644 --- a/src/main/java/com/juick/www/controllers/Site.java +++ b/src/main/java/com/juick/www/controllers/Site.java @@ -267,7 +267,7 @@ public class Site { if (paramShow == null) { if (paramTag != null) { title = "Блог " + user.getName() + ": *" + StringEscapeUtils.escapeHtml4(paramTag.getName()); - mids = messagesService.getUserTag(user.getUid(), paramTag.TID, privacy, before); + mids = messagesService.getUserTag(user.getUid(), paramTag.getId(), privacy, before); } else if (paramSearch != null) { title = "Блог " + user.getName() + ": " + StringEscapeUtils.escapeHtml4(paramSearch); mids = messagesService.getUserSearch(visitor, user.getUid(), paramSearch, @@ -289,7 +289,7 @@ public class Site { String head = "<link rel=\"alternate\" type=\"application/rss+xml\" title=\"@" + user.getName() + "\" href=\"//rss.juick.com/" + user.getName() + "/blog\"/>"; head += "<meta name=\"Description\" content=\"" + title + "\" />\n"; - if (paramTag != null && tagService.getTagNoIndex(paramTag.TID)) { + if (paramTag != null && tagService.getTagNoIndex(paramTag.getId())) { head += "<meta name=\"robots\" content=\"noindex,nofollow\"/>"; } else if (before > 0 || paramShow != null) { head += "<meta name=\"robots\" content=\"noindex\"/>"; @@ -402,8 +402,8 @@ public class Site { Tag paramTag = tagService.getTag(paramTagStr, false); if (paramTag == null) { throw new HttpNotFoundException(); - } else if (paramTag.SynonymID > 0 && paramTag.TID != paramTag.SynonymID) { - Tag synTag = tagService.getTag(paramTag.SynonymID); + } else if (paramTag.getSynonymId() > 0 && paramTag.getId() != paramTag.getSynonymId()) { + Tag synTag = tagService.getTag(paramTag.getSynonymId()); String url = "/tag/" + URLEncoder.encode(StringEscapeUtils.escapeHtml4(synTag.getName()), StandardCharsets.UTF_8); if (request.getQueryString() != null) { @@ -421,7 +421,7 @@ public class Site { String title = "*" + StringEscapeUtils.escapeHtml4(paramTag.getName()); model.addAttribute("title", title); - List<Integer> mids = messagesService.getTag(paramTag.TID, visitor.getUid(), before, + List<Integer> mids = messagesService.getTag(paramTag.getId(), visitor.getUid(), before, (visitor.isAnonymous()) ? 40 : 20); List<Message> msgs = messagesService.getMessages(visitor.getUid(), mids); msgs.forEach(m -> m.getUser().setAvatar(webApp.getAvatarWebPath(m.getUser()))); @@ -435,7 +435,7 @@ public class Site { } String head = StringUtils.EMPTY; - if (tagService.getTagNoIndex(paramTag.TID)) { + if (tagService.getTagNoIndex(paramTag.getId())) { head = "<meta name=\"robots\" content=\"noindex,nofollow\"/>"; } else if (before > 0 || mids.size() < 5) { head = "<meta name=\"robots\" content=\"noindex\"/>"; |