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

Sun, 10 Oct 2021 14:46:10 +0200

author
Mike Becker <universe@uap-core.de>
date
Sun, 10 Oct 2021 14:46:10 +0200
changeset 236
819c5178b6fe
parent 235
4258b9e010ae
child 238
1d48b38ca349
permissions
-rw-r--r--

add diffs to RSS feed

     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(prev.subject, prev.component, prev.status,
    77                 prev.category, prev.assignee, prev.eta, prev.affected, prev.resolved),
    78             listOf(diff.subject, diff.component, diff.status,
    79                 diff.category, diff.assignee, diff.eta, diff.affected, diff.resolved)
    80         )
    82         diff.subject = result[0].oldLine
    83         diff.component = result[1].oldLine
    84         diff.status = result[2].oldLine
    85         diff.category = result[3].oldLine
    86         diff.assignee = result[4].oldLine
    87         diff.eta = result[5].oldLine
    88         diff.affected = result[6].oldLine
    89         diff.resolved = result[7].oldLine
    91         diff.description = generator.generateDiffRows(
    92             prev.description.split('\n'),
    93             diff.description.split('\n')
    94         ).joinToString("<br>", transform = DiffRow::getOldLine)
    96         return diff
    97     }
    99     /**
   100      * Generates the feed entries.
   101      * Assumes that [historyEntry] is already sorted by timestamp (descending).
   102      */
   103     private fun generateFeedEntries(historyEntry: List<IssueHistoryEntry>) =
   104         if (historyEntry.isEmpty()) emptyList()
   105         else historyEntry.zipWithNext().map { (cur, next) ->
   106             IssueFeedEntry(
   107                 cur.time, cur.type, diffContent(cur.data, next.data)
   108             )
   109         }.plus(
   110             historyEntry.last().let { IssueFeedEntry(it.time, it.type, fullContent(it.data)) }
   111         )
   113     private fun issues(http: HttpRequest, dao: DataAccessObject) {
   114         val project = http.pathParams["project"]?.let { dao.findProjectByNode(it) }
   115         if (project == null) {
   116             http.response.sendError(404)
   117             return
   118         }
   120         val days = http.param("days")?.toIntOrNull() ?: 30
   122         val issueHistory = dao.listIssueHistory(project.id, days)
   123         // TODO: add comment history depending on parameter
   125         http.view = IssueFeed(project, generateFeedEntries(issueHistory))
   126         http.renderFeed("issues-feed")
   127     }
   128 }

mercurial