From 906bf8ef21463f99820764807b656cb617b0aaa2 Mon Sep 17 00:00:00 2001 From: Vitaly Takmazov Date: Sat, 28 Jan 2023 18:24:32 +0300 Subject: Cleanup Tag model --- src/main/java/com/juick/CommandsManager.java | 2 +- src/main/java/com/juick/model/Tag.java | 22 +++++++++++++++--- .../com/juick/service/MessagesServiceImpl.java | 9 +++++--- .../juick/service/PrivacyQueriesServiceImpl.java | 4 ++-- .../com/juick/service/SubscriptionServiceImpl.java | 4 ++-- .../java/com/juick/service/TagServiceImpl.java | 26 +++++++++++----------- src/main/java/com/juick/www/api/Messages.java | 4 ++-- src/main/java/com/juick/www/controllers/Site.java | 12 +++++----- .../java/com/juick/server/tests/ServerTests.java | 10 ++++----- 9 files changed, 55 insertions(+), 38 deletions(-) (limited to 'src') 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 mids = messagesService.getTag(tag.TID, user.getUid(), 0, 10); + List 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, 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, 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 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 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 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 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 = ""; head += "\n"; - if (paramTag != null && tagService.getTagNoIndex(paramTag.TID)) { + if (paramTag != null && tagService.getTagNoIndex(paramTag.getId())) { head += ""; } else if (before > 0 || paramShow != null) { head += ""; @@ -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 mids = messagesService.getTag(paramTag.TID, visitor.getUid(), before, + List mids = messagesService.getTag(paramTag.getId(), visitor.getUid(), before, (visitor.isAnonymous()) ? 40 : 20); List 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 = ""; } else if (before > 0 || mids.size() < 5) { head = ""; diff --git a/src/test/java/com/juick/server/tests/ServerTests.java b/src/test/java/com/juick/server/tests/ServerTests.java index dba662b0..d695ffb8 100644 --- a/src/test/java/com/juick/server/tests/ServerTests.java +++ b/src/test/java/com/juick/server/tests/ServerTests.java @@ -732,7 +732,7 @@ public class ServerTests { assertThat(topCandidates.size(), is(1)); assertThat(topCandidates.get(0), is(topmid)); Tag juickTag = tagService.getTag("juick", false); - assertThat(juickTag.TID, is(2)); + assertThat(juickTag.getId(), is(2)); tagService.updateTags(topmid, Collections.singletonList(juickTag)); assertThat(messagesService.getPopularCandidates().isEmpty(), is(true)); tagService.updateTags(topmid, Collections.singletonList(juickTag)); @@ -746,9 +746,7 @@ public class ServerTests { CommandResult resultUndo = commandsManager.commandRecommend(serviceUser, URI.create(""), String.valueOf(topmid)); assertThat(messagesService.getPopularCandidates().isEmpty(), is(false)); - jdbcTemplate.update("INSERT INTO tags(tag_id, name) VALUES(805, 'NSFW')"); - Tag nsfw = tagService.getTag("NSFW", false); - assertThat(nsfw.TID, equalTo(805)); + Tag nsfw = tagService.getTag("NSFW", true); tagService.updateTags(topmid, Collections.singletonList(nsfw)); assertThat(messagesService.getPopularCandidates().isEmpty(), is(true)); User recommender = userService.createUser("recommender2", "x").orElseThrow(IllegalStateException::new); @@ -1277,8 +1275,8 @@ public class ServerTests { commandsManager.processCommand(ugnich, "BL @freefd", emptyUri); Tag tag = tagService.getTag("linux", true); int freefdMsg = messagesService.createMessage(freefd.getUid(), "sux", null, Set.of(tag)); - assertThat(messagesService.getTag(tag.TID, freefd.getUid(), 0, 10).size(), is(1)); - assertThat(messagesService.getTag(tag.TID, ugnich.getUid(), 0, 10).size(), is(0)); + assertThat(messagesService.getTag(tag.getId(), freefd.getUid(), 0, 10).size(), is(1)); + assertThat(messagesService.getTag(tag.getId(), ugnich.getUid(), 0, 10).size(), is(0)); messagesService.recommendMessage(freefdMsg, serviceUser.getUid()); assertThat(messagesService.getUserBlogWithRecommendations(serviceUser, ugnich, 0, 0) .contains(freefdMsg), -- cgit v1.2.3