Skip to content

Commit

Permalink
лента форума - заголовок и меню
Browse files Browse the repository at this point in the history
  • Loading branch information
maxcom committed Aug 12, 2023
1 parent f7fb1a8 commit b7dda4b
Show file tree
Hide file tree
Showing 4 changed files with 91 additions and 56 deletions.
7 changes: 4 additions & 3 deletions src/main/scala/ru/org/linux/tag/TagService.scala
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ import ru.org.linux.group.{Group, GroupDao}
import ru.org.linux.search.ElasticsearchIndexService.{COLUMN_TOPIC_AWAITS_COMMIT, MessageIndex}
import ru.org.linux.section.{Section, SectionController, SectionService}
import ru.org.linux.topic.TagTopicListController
import ru.org.linux.topic.TopicListController.ForumFilter
import ru.org.linux.util.RichFuture.RichFuture

import java.time.LocalDate
Expand Down Expand Up @@ -108,18 +109,18 @@ class TagService(tagDao: TagDao, elastic: ElasticClient, actorSystem: ActorSyste
}).sorted.filterNot(_.name == tag) // filtering in query is broken in elastic4s-tcp 6.2.x
}

def getActiveTopTags(section: Section, group: Option[Group], filter: Option[String], deadline: Deadline): Future[Seq[TagRef]] = {
def getActiveTopTags(section: Section, group: Option[Group], filter: Option[ForumFilter], deadline: Deadline): Future[Seq[TagRef]] = {
if (group.exists(g => g.getId == 4068)) {
Future.successful(Seq.empty)
} else {
val groupFilter = group.map(g => termQuery("group", g.getUrlName))

val additionalFilter = filter.collect {
case "tech" =>
case ForumFilter.Tech =>
boolQuery()
.filter(termQuery("section", sectionForum.getUrlName))
.not(termsQuery("group", NonTechNames))
case "notalks" =>
case ForumFilter.NoTalks =>
boolQuery()
.not(termQuery("group", "talks"))
}
Expand Down
60 changes: 44 additions & 16 deletions src/main/scala/ru/org/linux/topic/TopicListController.scala
Original file line number Diff line number Diff line change
Expand Up @@ -29,11 +29,13 @@ import ru.org.linux.group.{Group, GroupDao, GroupNotFoundException}
import ru.org.linux.section.{Section, SectionController, SectionNotFoundException, SectionService}
import ru.org.linux.site.{ScriptErrorException, Template}
import ru.org.linux.tag.{TagPageController, TagService}
import ru.org.linux.topic.TopicListController.calculatePTitle
import ru.org.linux.topic.TopicListController.ForumFilter.{NoTalks, Tech}
import ru.org.linux.topic.TopicListController.{ForumFilter, ForumFilters, calculatePTitle}
import ru.org.linux.user.UserErrorException
import ru.org.linux.util.{DateUtil, ServletParameterException}

import java.util.concurrent.CompletionStage
import javax.annotation.Nullable
import scala.compat.java8.FutureConverters.*
import scala.concurrent.ExecutionContext.Implicits.global
import scala.concurrent.Future
Expand All @@ -46,7 +48,7 @@ object TopicListController {
s"${section.getName} - ${group.getTitle}"
case None =>
if (topicListForm.yearMonth.isEmpty) {
section.getName
section.getName + topicListForm.filter.map(f => s" (${f.title})").getOrElse("")
} else {
s"Архив: ${section.getName}, ${topicListForm.getYear.get}, ${DateUtil.getMonth(topicListForm.getMonth.get)}"
}
Expand All @@ -56,18 +58,42 @@ object TopicListController {
private def calculateNavTitle(section: Section, group: Option[Group], topicListForm: TopicListRequest): String = {
val navTitle = new StringBuilder(section.getName)

group foreach { group =>
group.foreach { group =>
navTitle.append(s" «${group.getTitle}»")
}

topicListForm.filter.foreach { f =>
navTitle.append(s" (${f.title})")
}

if (topicListForm.getMonth.isDefined) {
navTitle.append(" - Архив ").append(topicListForm.getYear.get).append(", ").append(DateUtil.getMonth(topicListForm.getMonth.get))
}

navTitle.toString
}

private val ForumFilters: Set[String] = Set("all", "notalks", "tech")
sealed trait ForumFilter {
def id: String
def title: String

final def getId: String = id
final def getTitle: String = title
}

object ForumFilter {
case object NoTalks extends ForumFilter {
override val id = "notalks"
override val title = "без talks"
}

case object Tech extends ForumFilter {
override val id = "tech"
override val title = "тех. форум"
}
}

private val ForumFilters = Seq(NoTalks, Tech)
}

@Controller
Expand Down Expand Up @@ -102,15 +128,18 @@ class TopicListController(sectionService: SectionService, topicListService: Topi
if (section.getId != Section.SECTION_FORUM) {
modelAndView.addObject("groupList",
SectionController.groupsSorted(groupDao.getGroups(section).asScala).asJava)
} else {
modelAndView.addObject("filters", ForumFilters.asJava)
modelAndView.addObject("filter", topicListForm.filter.getOrElse(""))
}

modelAndView.addObject("navtitle", TopicListController.calculateNavTitle(section, group, topicListForm))

val tmpl = Template.getTemplate

val messages = topicListService.getTopicsFeed(Some(section), group, None, topicListForm.offset,
topicListForm.yearMonth, 20, currentUserOpt.map(_.user), topicListForm.filter.contains("notalks"),
topicListForm.filter.contains("tech"))
topicListForm.yearMonth, 20, currentUserOpt.map(_.user), topicListForm.filter.contains(NoTalks),
topicListForm.filter.contains(Tech))

modelAndView.addObject(
"messages",
Expand Down Expand Up @@ -140,16 +169,17 @@ class TopicListController(sectionService: SectionService, topicListService: Topi
}.toJava
}

private def parseFilter(@Nullable value: String): Option[ForumFilter] = {
Option(value)
.map(v => ForumFilters.find(_.id == v).getOrElse(throw new UserErrorException("Некорректное значение filter")))
}

@RequestMapping(Array("/forum/lenta"))
def forum(@RequestParam(value="offset", defaultValue = "0") offset: Int,
@RequestParam(value = "filter", required = false) filter: String): CompletionStage[ModelAndView] = {
val section = sectionService.getSection(Section.SECTION_FORUM)

if (filter != null && !TopicListController.ForumFilters.contains(filter)) {
throw new UserErrorException("Некорректное значение filter")
}

val topicListForm = TopicListRequest.ofOffset(offset).withFilter(filter)
val topicListForm = TopicListRequest.ofOffset(offset).copy(filter = parseFilter(filter))

mainTopicsFeedHandler(section, topicListForm, None).map { modelAndView =>
if (filter==null) {
Expand Down Expand Up @@ -205,9 +235,7 @@ class TopicListController(sectionService: SectionService, topicListService: Topi
@RequestParam(value = "group", defaultValue = "0") groupId: Int,
@RequestParam(value = "filter", required = false) filter: String,
webRequest: WebRequest): ModelAndView = {
if (filter != null && !TopicListController.ForumFilters.contains(filter)) {
throw new UserErrorException("Некорректное значение filter")
}
val forumFilter = parseFilter(filter)

val section = sectionService.getSection(sectionId)
var ptitle = section.getName
Expand All @@ -230,8 +258,8 @@ class TopicListController(sectionService: SectionService, topicListService: Topi
modelAndView.addObject("section", section)
modelAndView.addObject("ptitle", ptitle)

val notalks = "notalks" == filter
val tech = "tech" == filter
val notalks = forumFilter.contains(NoTalks)
val tech = forumFilter.contains(Tech)

val fromDate = DateTime.now.minusMonths(3)
val messages = topicListService.getRssTopicsFeed(section, group, fromDate.toDate, notalks, tech)
Expand Down
8 changes: 5 additions & 3 deletions src/main/scala/ru/org/linux/topic/TopicListRequest.scala
Original file line number Diff line number Diff line change
Expand Up @@ -14,14 +14,16 @@
*/
package ru.org.linux.topic

import javax.annotation.Nullable
import ru.org.linux.topic.TopicListController.ForumFilter

import scala.beans.BeanProperty

case class TopicListRequest(@BeanProperty offset: Int, yearMonth: Option[(Int, Int)], filter: Option[String]) {
case class TopicListRequest(@BeanProperty offset: Int, yearMonth: Option[(Int, Int)], filter: Option[ForumFilter]) {
def getMonth: Option[Int] = yearMonth.map(_._2)
def getYear: Option[Int] = yearMonth.map(_._1)

def withFilter(@Nullable value: String): TopicListRequest = copy(filter = Option(value))
// for jsp
def getFilter = filter.map(_.id).getOrElse("")
}

object TopicListRequest {
Expand Down
72 changes: 38 additions & 34 deletions src/main/webapp/WEB-INF/jsp/view-news.jsp
Original file line number Diff line number Diff line change
@@ -1,9 +1,3 @@
<%@ page contentType="text/html; charset=utf-8"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<%@ taglib tagdir="/WEB-INF/tags" prefix="lor" %>
<%@ taglib prefix="l" uri="http://www.linux.org.ru" %>
<%@ taglib prefix="fn" uri="http://java.sun.com/jsp/jstl/functions" %>
<%@ taglib prefix="form" uri="http://www.springframework.org/tags/form" %>
<%--
~ Copyright 1998-2023 Linux.org.ru
~ Licensed under the Apache License, Version 2.0 (the "License");
Expand All @@ -18,6 +12,12 @@
~ See the License for the specific language governing permissions and
~ limitations under the License.
--%>
<%@ page contentType="text/html; charset=utf-8"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<%@ taglib tagdir="/WEB-INF/tags" prefix="lor" %>
<%@ taglib prefix="l" uri="http://www.linux.org.ru" %>
<%@ taglib prefix="fn" uri="http://java.sun.com/jsp/jstl/functions" %>
<%@ taglib prefix="form" uri="http://www.springframework.org/tags/form" %>
<%--@elvariable id="section" type="ru.org.linux.section.Section"--%>
<%--@elvariable id="activeTags" type="java.util.List<java.lang.String>"--%>
<jsp:include page="/WEB-INF/jsp/head.jsp"/>
Expand All @@ -38,32 +38,34 @@
<h1>${navtitle}</h1>

<nav>
<c:if test="${section!=null and section.premoderated}">
<c:if test="${section.premoderated}">
<c:if test="${offsetNavigation}">
<a class="btn btn-selected" href="${section.sectionLink}">Новые темы</a>
</c:if>
<c:if test="${not offsetNavigation}">
<a class="btn btn-default" href="${section.sectionLink}">Новые темы</a>
</c:if>
</c:if>

<c:if test="${sectionList == null and template.moderatorSession and group!=null}">
<a class="btn btn-default" href="groupmod.jsp?group=${group.id}">Править группу</a>
<a class="btn btn-default" href="/view-all.jsp?section=${section.id}">Неподтвержденные</a>
</c:if>
<c:if test="${sectionList == null and section != null}">
<c:if test="${section.premoderated}">
<a class="btn btn-default" href="/view-all.jsp?section=${section.id}">Неподтвержденные</a>

<c:if test="${filters != null}">
<c:if test="${empty topicListRequest.filter}">
<a class="btn btn-selected" href="${section.newsViewerLink}">все</a>
</c:if>
<c:if test="${section.id == Section.SECTION_FORUM}">
<c:choose>
<c:when test="${group == null}">
<a class="btn btn-default" href="/forum/">Таблица</a>
</c:when>
<c:otherwise>
<a class="btn btn-default" href="/forum/${group.urlName}">Таблица</a>
</c:otherwise>
</c:choose>

<c:if test="${not empty topicListRequest.filter}">
<a class="btn btn-default" href="${section.newsViewerLink}">все</a>
</c:if>

<c:forEach var="f" items="${filters}">
<c:if test="${f.id == topicListRequest.filter}">
<a class="btn btn-selected" href="${section.newsViewerLink}?filter=${f.id}">${f.title}</a>
</c:if>
<c:if test="${f.id != topicListRequest.filter}">
<a class="btn btn-default" href="${section.newsViewerLink}?filter=${f.id}">${f.title}</a>
</c:if>
</c:forEach>
</c:if>

<c:if test="${archiveLink != null}">
Expand All @@ -75,20 +77,22 @@
</c:if>
</c:if>

<c:if test="${section != null}">
<c:choose>
<c:when test="${section.pollPostAllowed}">
<a class="btn btn-primary" href="add.jsp?group=19387">Добавить</a>
</c:when>
<c:when test="${group == null}">
<a class="btn btn-primary" href="add-section.jsp?section=${section.id}">Добавить</a>
</c:when>
<c:otherwise>
<a class="btn btn-primary" href="add.jsp?group=${group.id}">Добавить</a>
</c:otherwise>
</c:choose>
<c:if test="${template.moderatorSession and group!=null}">
<a class="btn btn-default" href="groupmod.jsp?group=${group.id}">Править группу</a>
</c:if>

<c:choose>
<c:when test="${section.pollPostAllowed}">
<a class="btn btn-primary" href="add.jsp?group=19387">Добавить</a>
</c:when>
<c:when test="${group == null}">
<a class="btn btn-primary" href="add-section.jsp?section=${section.id}">Добавить</a>
</c:when>
<c:otherwise>
<a class="btn btn-primary" href="add.jsp?group=${group.id}">Добавить</a>
</c:otherwise>
</c:choose>

<c:if test="${fn:length(groupList)>1 and offsetNavigation}">
<div class="nav-buttons">
<form>
Expand Down

0 comments on commit b7dda4b

Please sign in to comment.