Skip to content

Commit

Permalink
Issue #91: If a number has no comments, show comments of related ones.
Browse files Browse the repository at this point in the history
  • Loading branch information
haumacher committed Dec 2, 2024
1 parent 9ac72f5 commit b91467a
Show file tree
Hide file tree
Showing 5 changed files with 37 additions and 28 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -175,9 +175,6 @@ private static SearchResult analyze(String query, boolean isBot, boolean isSeach

String phoneId = NumberAnalyzer.getPhoneId(number);

// Note: Search for comments first, since new comments may change the state of the number.
List<UserComment> comments = MetaSearchService.getInstance().fetchComments(phoneId, isBot);

NumberInfo numberInfo;
PhoneInfo info;
List<Integer> searches;
Expand All @@ -187,9 +184,19 @@ private static SearchResult analyze(String query, boolean isBot, boolean isSeach
String prev;
String next;

List<? extends UserComment> comments;

DB db = DBService.getInstance();
try (SqlSession session = db.openSession()) {
SpamReports reports = session.getMapper(SpamReports.class);

// Note: Search for comments first, since new comments may change the state of the number.
if (isSeachHit) {
comments = MetaSearchService.getInstance().fetchComments(phoneId);
} else {
comments = reports.getComments(phoneId);
}

boolean commit = false;

long now = System.currentTimeMillis();
Expand Down Expand Up @@ -221,9 +228,17 @@ private static SearchResult analyze(String query, boolean isBot, boolean isSeach

if (aggregation100.getCnt() >= DB.MIN_AGGREGATE_100) {
relatedNumbers = reports.getRelatedNumbers(aggregation100.getPrefix());

if (comments.isEmpty()) {
comments = reports.getAllComments(aggregation100.getPrefix());
}
} else {
if (aggregation10.getCnt() >= DB.MIN_AGGREGATE_10) {
relatedNumbers = reports.getRelatedNumbers(aggregation10.getPrefix());

if (comments.isEmpty()) {
comments = reports.getAllComments(aggregation10.getPrefix());
}
} else {
relatedNumbers = Collections.emptyList();
}
Expand Down Expand Up @@ -256,9 +271,9 @@ private static SearchResult analyze(String query, boolean isBot, boolean isSeach
}
negativeCnt = Math.min(10 - positiveCnt, negativeCnt);
}
comments = new ArrayList<>(positive.subList(0, positiveCnt));
comments.addAll(negative.subList(0, negativeCnt));
comments.sort(COMMENT_ORDER);
List<UserComment> shownComments = new ArrayList<>(positive.subList(0, positiveCnt));
shownComments.addAll(negative.subList(0, negativeCnt));
shownComments.sort(COMMENT_ORDER);

Map<Rating, Integer> ratings = new HashMap<>();
ratings.put(Rating.A_LEGITIMATE, numberInfo.getRatingLegitimate());
Expand All @@ -273,7 +288,7 @@ private static SearchResult analyze(String query, boolean isBot, boolean isSeach
return SearchResult.create()
.setPhoneId(phoneId)
.setNumber(number)
.setComments(comments)
.setComments(shownComments)
.setInfo(info)
.setSearches(searches)
.setAiSummary(aiSummary)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -330,6 +330,15 @@ SELECT COUNT(1) cnt, CASE WHEN s.VOTES < #{minVotes} THEN 0 WHEN s.VOTES < 6 THE
@Select("select s.ID, s.PHONE, s.RATING, s.COMMENT, s.SERVICE, s.CREATED, s.UP, s.DOWN from COMMENTS s where s.PHONE=#{phone}")
List<DBUserComment> getComments(String phone);

@Select("""
select s.ID, s.PHONE, s.RATING, s.COMMENT, s.SERVICE, s.CREATED, s.UP, s.DOWN
from COMMENTS s
where
s.PHONE > #{prefix} and
s.PHONE < concat(#{prefix}, 'Z')
""")
List<DBUserComment> getAllComments(String prefix);

@Insert("insert into COMMENTS (ID, PHONE, RATING, COMMENT, SERVICE, CREATED) values (#{id}, #{phone}, #{rating}, #{comment}, #{service}, #{created})")
void addComment(String id, String phone, Rating rating, String comment, String service, long created);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -140,8 +140,8 @@ public void contextDestroyed(ServletContextEvent sce) {
* @param bot
* Whether a bot requested the info.
*/
public List<UserComment> fetchComments(String phoneId, boolean bot) {
return createSearch(phoneId, bot).search().getComments();
public List<UserComment> fetchComments(String phoneId) {
return createSearch(phoneId).search().getComments();
}

/**
Expand Down Expand Up @@ -207,17 +207,7 @@ private void performSearch() {
* Creates a search for the given phone number.
*/
private SearchOperation createSearch(String phoneId) {
return createSearch(phoneId, false);
}

/**
* Creates a search for the given phone number.
*
* @param bot
* Whether the search is done for a bot.
*/
private SearchOperation createSearch(String phoneId, boolean bot) {
return new SearchOperation(_scheduler, _indexer, _plugins, phoneId, bot);
return new SearchOperation(_scheduler, _indexer, _plugins, phoneId);
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,17 +51,14 @@ public class SearchOperation {

private boolean _searchPerformed;

private boolean _bot;

/**
* Creates a {@link SearchOperation}.
*/
public SearchOperation(SchedulerService scheduler, IndexUpdateService indexer, List<AbstractMetaSearch> plugins, String phoneId, boolean bot) {
public SearchOperation(SchedulerService scheduler, IndexUpdateService indexer, List<AbstractMetaSearch> plugins, String phoneId) {
_scheduler = scheduler;
_indexer = indexer;
_plugins = plugins;
_phoneId = phoneId;
_bot = bot;
}

/**
Expand All @@ -73,9 +70,7 @@ public SearchOperation search() {
SpamReports mapper = session.getMapper(SpamReports.class);

_comments = new ArrayList<>(mapper.getComments(_phoneId));

// Do not perform a meta-search for a bot request.
if (!_bot) {
{
_searchPerformed = shouldSearch(mapper, _phoneId);

boolean indexUpdated = false;
Expand Down
2 changes: 1 addition & 1 deletion phoneblock/src/main/webapp/phone-info.jsp
Original file line number Diff line number Diff line change
Expand Up @@ -209,7 +209,7 @@
<div class="media-content">
<div class="content">
<p class="commentHeader">
<strong>☎ <%= info.getPhone()%></strong>
<strong>☎ <%= comment.getPhone()%></strong>
<small>
<% if (comment.getService() != null && !comment.getService().isEmpty()) { %>
<a target="_blank" href="<%= request.getContextPath()%><%= ExternalLinkServlet.LINK_PREFIX%><%= comment.getService()%>/<%= comment.getPhone()%>"><%= comment.getService()%></a>
Expand Down

0 comments on commit b91467a

Please sign in to comment.