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

Sat, 09 Oct 2021 20:05:39 +0200

author
Mike Becker <universe@uap-core.de>
date
Sat, 09 Oct 2021 20:05:39 +0200
changeset 235
4258b9e010ae
parent 199
59393c8cc557
child 236
819c5178b6fe
permissions
-rw-r--r--

change rss feed to display the issue history

TODO: diffs and comments

     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 de.uapcore.lightpit.AbstractServlet
    29 import de.uapcore.lightpit.HttpRequest
    30 import de.uapcore.lightpit.dao.DataAccessObject
    31 import de.uapcore.lightpit.entities.IssueHistoryData
    32 import de.uapcore.lightpit.entities.IssueHistoryEntry
    33 import de.uapcore.lightpit.viewmodel.IssueDiff
    34 import de.uapcore.lightpit.viewmodel.IssueFeed
    35 import de.uapcore.lightpit.viewmodel.IssueFeedEntry
    36 import java.text.SimpleDateFormat
    37 import javax.servlet.annotation.WebServlet
    39 @WebServlet(urlPatterns = ["/feed/*"])
    40 class FeedServlet : AbstractServlet() {
    42     init {
    43         get("/%project/issues.rss", this::issues)
    44     }
    46     private fun String.convertLF() = replace("\r", "").replace("\n", "<br>")
    48     private fun fullContent(issue: IssueHistoryData) = IssueDiff(
    49         issue.id,
    50         issue.subject,
    51         issue.component,
    52         issue.status.name,
    53         issue.category.name,
    54         issue.subject,
    55         issue.description.convertLF(),
    56         issue.assignee,
    57         issue.eta?.let { SimpleDateFormat("dd.MM.yyyy").format(it) } ?: "",
    58         issue.affected,
    59         issue.resolved
    60     )
    62     private fun diffContent(cur: IssueHistoryData, next: IssueHistoryData): IssueDiff {
    63         val prev = fullContent(next)
    64         val diff = fullContent(cur)
    65         // TODO: compute and apply diff
    66         return diff
    67     }
    69     /**
    70      * Generates the feed entries.
    71      * Assumes that [historyEntry] is already sorted by timestamp (descending).
    72      */
    73     private fun generateFeedEntries(historyEntry: List<IssueHistoryEntry>) =
    74         if (historyEntry.isEmpty()) emptyList()
    75         else historyEntry.zipWithNext().map { (cur, next) ->
    76             IssueFeedEntry(
    77                 cur.time, cur.type, diffContent(cur.data, next.data)
    78             )
    79         }.plus(
    80             historyEntry.last().let { IssueFeedEntry(it.time, it.type, fullContent(it.data)) }
    81         )
    83     private fun issues(http: HttpRequest, dao: DataAccessObject) {
    84         val project = http.pathParams["project"]?.let { dao.findProjectByNode(it) }
    85         if (project == null) {
    86             http.response.sendError(404)
    87             return
    88         }
    90         val days = http.param("days")?.toIntOrNull() ?: 30
    92         val issueHistory = dao.listIssueHistory(project.id, days)
    93         // TODO: add comment history depending on parameter
    95         http.view = IssueFeed(project, generateFeedEntries(issueHistory))
    96         http.renderFeed("issues-feed")
    97     }
    98 }

mercurial