src/main/kotlin/de/uapcore/lightpit/servlet/FeedServlet.kt

Sat, 27 Nov 2021 12:12:20 +0100

author
Mike Becker <universe@uap-core.de>
date
Sat, 27 Nov 2021 12:12:20 +0100
changeset 241
1ca4f27cefe8
parent 240
7303812a4424
child 242
b7f3e972b13c
permissions
-rw-r--r--

#109 changes assignee filter

     1 /*
     2  * Copyright 2021 Mike Becker. All rights reserved.
     3  *
     4  * Redistribution and use in source and binary forms, with or without
     5  * modification, are permitted provided that the following conditions are met:
     6  *
     7  * 1. Redistributions of source code must retain the above copyright
     8  * notice, this list of conditions and the following disclaimer.
     9  *
    10  * 2. Redistributions in binary form must reproduce the above copyright
    11  * notice, this list of conditions and the following disclaimer in the
    12  * documentation and/or other materials provided with the distribution.
    13  *
    14  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
    15  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
    16  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
    17  * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
    18  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
    19  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
    20  * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
    21  * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
    22  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
    23  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
    24  */
    26 package de.uapcore.lightpit.servlet
    28 import com.github.difflib.text.DiffRow
    29 import com.github.difflib.text.DiffRowGenerator
    30 import de.uapcore.lightpit.AbstractServlet
    31 import de.uapcore.lightpit.HttpRequest
    32 import de.uapcore.lightpit.dao.DataAccessObject
    33 import de.uapcore.lightpit.entities.IssueHistoryData
    34 import de.uapcore.lightpit.entities.IssueHistoryEntry
    35 import de.uapcore.lightpit.viewmodel.IssueDiff
    36 import de.uapcore.lightpit.viewmodel.IssueFeed
    37 import de.uapcore.lightpit.viewmodel.IssueFeedEntry
    38 import java.text.SimpleDateFormat
    39 import javax.servlet.annotation.WebServlet
    42 @WebServlet(urlPatterns = ["/feed/*"])
    43 class FeedServlet : AbstractServlet() {
    45     init {
    46         get("/%project/issues.rss", this::issues)
    47     }
    49     private fun fullContent(issue: IssueHistoryData) = IssueDiff(
    50         issue.id,
    51         issue.subject,
    52         issue.component,
    53         issue.status.name,
    54         issue.category.name,
    55         issue.subject,
    56         issue.description.replace("\r", ""),
    57         issue.assignee,
    58         issue.eta?.let { SimpleDateFormat("dd.MM.yyyy").format(it) } ?: "",
    59         issue.affected,
    60         issue.resolved
    61     )
    63     private fun diffContent(cur: IssueHistoryData, next: IssueHistoryData): IssueDiff {
    64         val generator = DiffRowGenerator.create()
    65             .showInlineDiffs(true)
    66             .mergeOriginalRevised(true)
    67             .inlineDiffByWord(true)
    68             .oldTag { start -> if (start) "<strike style=\"color:red\">" else "</strike>" }
    69             .newTag { start -> if (start) "<i style=\"color: green\">" else "</i>" }
    70             .build()
    72         val prev = fullContent(next)
    73         val diff = fullContent(cur)
    75         val result = generator.generateDiffRows(
    76             listOf(
    77                 prev.subject, prev.component, prev.status,
    78                 prev.category, prev.assignee, prev.eta, prev.affected, prev.resolved
    79             ),
    80             listOf(
    81                 diff.subject, diff.component, diff.status,
    82                 diff.category, diff.assignee, diff.eta, diff.affected, diff.resolved
    83             )
    84         )
    86         diff.subject = result[0].oldLine
    87         diff.component = result[1].oldLine
    88         diff.status = result[2].oldLine
    89         diff.category = result[3].oldLine
    90         diff.assignee = result[4].oldLine
    91         diff.eta = result[5].oldLine
    92         diff.affected = result[6].oldLine
    93         diff.resolved = result[7].oldLine
    95         diff.description = generator.generateDiffRows(
    96             prev.description.split('\n'),
    97             diff.description.split('\n')
    98         ).joinToString("\n", transform = DiffRow::getOldLine)
   100         return diff
   101     }
   103     /**
   104      * Generates the feed entries.
   105      * Assumes that [historyEntry] is already sorted by timestamp (descending).
   106      */
   107     private fun generateFeedEntries(historyEntry: List<IssueHistoryEntry>): List<IssueFeedEntry> =
   108         if (historyEntry.isEmpty()) {
   109             emptyList()
   110         } else {
   111             historyEntry.groupBy { it.data.id }.mapValues { (_, history) ->
   112                 history.zipWithNext().map { (cur, next) ->
   113                     IssueFeedEntry(
   114                         cur.time, cur.type, diffContent(cur.data, next.data)
   115                     )
   116                 }.plus(
   117                     history.last().let { IssueFeedEntry(it.time, it.type, fullContent(it.data)) }
   118                 )
   119             }.flatMap { it.value }.sortedByDescending { it.time }
   120         }
   122     private fun issues(http: HttpRequest, dao: DataAccessObject) {
   123         val project = http.pathParams["project"]?.let { dao.findProjectByNode(it) }
   124         if (project == null) {
   125             http.response.sendError(404)
   126             return
   127         }
   128         val assignees = http.param("assignee")?.split(',')
   130         val days = http.param("days")?.toIntOrNull() ?: 30
   132         val issuesFromDb = dao.listIssueHistory(project.id, days)
   133         val issueHistory = if (assignees == null) issuesFromDb else
   134             issuesFromDb.filter { assignees.contains(it.currentAssignee) }
   136         // TODO: add comment history depending on parameter
   138         http.view = IssueFeed(project, generateFeedEntries(issueHistory))
   139         http.renderFeed("issues-feed")
   140     }
   141 }

mercurial