Skip to content

Commit

Permalink
sameip: фильтрация по score
Browse files Browse the repository at this point in the history
  • Loading branch information
maxcom committed Jul 31, 2024
1 parent ff99dfb commit 2262648
Show file tree
Hide file tree
Showing 4 changed files with 52 additions and 14 deletions.
30 changes: 27 additions & 3 deletions src/main/scala/ru/org/linux/sameip/SameIpDao.scala
Original file line number Diff line number Diff line change
@@ -1,3 +1,18 @@
/*
* Copyright 1998-2024 Linux.org.ru
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package ru.org.linux.sameip

import org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate
Expand All @@ -13,7 +28,7 @@ import scala.jdk.CollectionConverters.{ListHasAsScala, MutableMapHasAsJava}
class SameIpDao(dataSource: DataSource) {
private val namedJdbcTemplate: NamedParameterJdbcTemplate = new NamedParameterJdbcTemplate(dataSource)

def getComments(ip: Option[String], userAgent: Option[Int], limit: Int): collection.Seq[PostListItem] = {
def getComments(ip: Option[String], userAgent: Option[Int], score: Option[Int], limit: Int): collection.Seq[PostListItem] = {
val ipQuery: String = if (ip.isDefined) {
"AND m.postip <<= :ip::inet "
} else {
Expand All @@ -26,26 +41,35 @@ class SameIpDao(dataSource: DataSource) {
""
}

val scoreQuery = if (score.isDefined) {
"AND m.userid IN (SELECT id FROM users WHERE score < :score OR id = 2)"
} else {
""
}

val params = mutable.HashMap[String, AnyRef]()

ip.foreach(ip => params.put("ip", ip))
userAgent.foreach(userAgent => params.put("userAgent", Integer.valueOf(userAgent)))
score.foreach(score => params.put("score", Integer.valueOf(score)))

params.put("limit", Integer.valueOf(limit))


namedJdbcTemplate.query(
"SELECT groups.title as group_title, topics.title, topics.id as topic_id, " +
"m.id as cid, m.postdate, m.deleted, del_info.reason, " +
"m.userid " +
"FROM groups JOIN topics ON groups.id=topics.groupid " +
"JOIN comments m ON m.topic=topics.id " + "LEFT JOIN del_info ON del_info.msgid=m.id " +
"WHERE m.postdate>CURRENT_TIMESTAMP-'5 days'::interval " + ipQuery + userAgentQuery +
"WHERE m.postdate>CURRENT_TIMESTAMP-'5 days'::interval " + ipQuery + userAgentQuery + scoreQuery +
"UNION ALL " +
"SELECT groups.title as group_title, m.title, m.id as topic_id, " +
"0 as cid, m.postdate, m.deleted, del_info.reason, " +
"m.userid " +
"FROM groups JOIN topics m ON groups.id=m.groupid " +
"LEFT JOIN del_info ON del_info.msgid=m.id " +
"WHERE m.postdate>CURRENT_TIMESTAMP-'5 days'::interval " + ipQuery + userAgentQuery +
"WHERE m.postdate>CURRENT_TIMESTAMP-'5 days'::interval " + ipQuery + userAgentQuery + scoreQuery +
"ORDER BY postdate DESC LIMIT :limit",
params.asJava, (rs: ResultSet, _: Int) =>
PostListItem(
Expand Down
6 changes: 2 additions & 4 deletions src/main/scala/ru/org/linux/sameip/SameIpService.scala
Original file line number Diff line number Diff line change
Expand Up @@ -20,13 +20,11 @@ import ru.org.linux.markup.MessageTextService
import ru.org.linux.spring.dao.MsgbaseDao
import ru.org.linux.user.UserService

import scala.jdk.CollectionConverters.SeqHasAsJava

@Service
class SameIpService(userService: UserService, msgbaseDao: MsgbaseDao, textService: MessageTextService,
sameIpDao: SameIpDao) {
def getPosts(ip: Option[String], userAgent: Option[Integer], limit: Int): java.util.List[PreparedPostListItem] = {
prepareCommentList(sameIpDao.getComments(ip, userAgent.map(_.toInt), limit)).asJava
def getPosts(ip: Option[String], userAgent: Option[Int], score: Option[Int], limit: Int): collection.Seq[PreparedPostListItem] = {
prepareCommentList(sameIpDao.getComments(ip, userAgent, score, limit))
}

private def prepareCommentList(items: collection.Seq[PostListItem]): collection.Seq[PreparedPostListItem] = {
Expand Down
18 changes: 12 additions & 6 deletions src/main/scala/ru/org/linux/spring/SameIPController.scala
Original file line number Diff line number Diff line change
Expand Up @@ -35,12 +35,17 @@ object SameIPController {
@Controller
class SameIPController(ipBlockDao: IPBlockDao, userService: UserService, userAgentDao: UserAgentDao, sameIpService: SameIpService) {
@ModelAttribute("masks")
def getMasks: util.List[(String, String)] =
Seq("32" -> "IP", "24" -> "Сеть /24", "16" -> "Сеть /16", "0" -> "Не фильтровать").asJava
def masks: util.List[(String, String)] =
Seq("32" -> "IP", "24" -> "Сеть /24", "16" -> "Сеть /16", "0" -> "Любой IP").asJava

@ModelAttribute("scores")
def scores: util.List[(String, String)] =
Seq("" -> "Любой score", "45" -> "score < 45", "50" -> "score < 50", "100" -> "score < 100").asJava

@RequestMapping(Array("/sameip.jsp"))
def sameIP(@RequestParam(required = false) ip: String, @RequestParam(required = false, defaultValue = "32") mask: Int,
@RequestParam(required = false, name = "ua") userAgent: Integer): ModelAndView = ModeratorOnly { _ =>
@RequestParam(required = false, name = "ua") userAgent: Integer,
@RequestParam(required = false) score: Integer): ModelAndView = ModeratorOnly { _ =>
val mv = new ModelAndView("sameip")

val ipMask = Option(ip).flatMap { ip =>
Expand All @@ -61,16 +66,17 @@ class SameIPController(ipBlockDao: IPBlockDao, userService: UserService, userAge
}.orNull

val rowsLimit = 50
val comments = sameIpService.getPosts(Option(ipMask), Option(userAgent), rowsLimit)
val posts = sameIpService.getPosts(Option(ipMask), Option(userAgent), Option(score), rowsLimit)

mv.getModel.put("comments", comments)
mv.getModel.put("hasMoreComments", comments.size == rowsLimit)
mv.getModel.put("comments", posts.asJava)
mv.getModel.put("hasMoreComments", posts.size == rowsLimit)
mv.getModel.put("rowsLimit", rowsLimit)

val users = userService.getUsersWithAgent(ipMask, userAgent, rowsLimit)

mv.getModel.put("users", users)
mv.getModel.put("hasMoreUsers", users.size == rowsLimit)
mv.getModel.put("score", score)

if (ip != null) {
mv.getModel.put("ip", ip)
Expand Down
12 changes: 11 additions & 1 deletion src/main/webapp/WEB-INF/jsp/sameip.jsp
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,6 @@
<input type="hidden" name="ua" value="${ua}">
</c:if>
<div class="control-group">
<label class="control-label" for="ip-field">Адрес: </label>
<div class="controls">
<input class="input-lg" name="ip" type="search" size="17" maxlength="17" value="${ip}" id="ip-field" pattern="[0-9]+\.[0-9]+\.[0-9]+\.[0-9]+">

Expand All @@ -53,6 +52,17 @@
<button name="mask" value="${v._1()}" type="submit" class="btn btn-default">${v._2()}</button>
</c:if>
</c:forEach>

<select name="score" class="btn btn-default" onchange="this.form.submit()">
<c:forEach items="${scores}" var="v">
<c:if test="${v._1() == score}">
<option value="${v._1()}" type="submit" selected>${v._2()}</option>
</c:if>
<c:if test="${v._1() != score}">
<option value="${v._1()}" type="submit">${v._2()}</option>
</c:if>
</c:forEach>
</select>
</div>
</div>
</form>
Expand Down

0 comments on commit 2262648

Please sign in to comment.