Skip to content

Commit

Permalink
[MIM-2094] Mim 2094 rss feeds (#3041)
Browse files Browse the repository at this point in the history
* MIM-2094:Added service rssNews

* Added service rssStatkal

* MIM-2094:Refaktorering
  • Loading branch information
ssb-cgn authored Nov 25, 2024
1 parent a828b98 commit b65d377
Show file tree
Hide file tree
Showing 6 changed files with 53 additions and 15 deletions.
24 changes: 14 additions & 10 deletions src/main/resources/lib/ssb/rss/news.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,15 +15,12 @@ const dummyReq: Partial<XP.Request> = {
}

export function getRssItemsNews(): string | null {
const mainSubjects: SubjectItem[] = getMainSubjects(dummyReq as XP.Request)
const articles: Array<News> = getArticles(mainSubjects)
const statistics: Array<News> = getStatistics(mainSubjects)
const news: Array<News> = articles.concat(statistics)
const news: NewsItem[] = getNews()
const xml = `<?xml version="1.0" encoding="utf-8"?>
<rssitems count="${news.length}">
${news
.map(
(n: News) => `<rssitem>
(n: NewsItem) => `<rssitem>
<guid isPermalink="false">${n.guid}</guid>
<title>${xmlEscape(n.title)}</title>
<link>${n.link}</link>
Expand All @@ -40,13 +37,20 @@ export function getRssItemsNews(): string | null {
return xml
}

function getArticles(mainSubjects: SubjectItem[]): Array<News> {
export function getNews(): NewsItem[] {
const mainSubjects: SubjectItem[] = getMainSubjects(dummyReq as XP.Request)
const articles: NewsItem[] = getArticles(mainSubjects)
const statistics: NewsItem[] = getStatistics(mainSubjects)
return articles.concat(statistics)
}

function getArticles(mainSubjects: SubjectItem[]): NewsItem[] {
const from: string = subDays(new Date(), 1).toISOString()
const to: string = new Date().toISOString()
const serverOffsetInMilliSeconds: number = parseInt(app.config?.['serverOffsetInMs']) || 0
const timeZoneIso: string = getTimeZoneIso(serverOffsetInMilliSeconds)

const news: Array<News> = []
const news: Array<NewsItem> = []
mainSubjects.forEach((mainSubject) => {
const articles: Array<Content<Article>> = query({
start: 0,
Expand Down Expand Up @@ -89,12 +93,12 @@ function getArticles(mainSubjects: SubjectItem[]): Array<News> {
return news
}

function getStatistics(mainSubjects: SubjectItem[]): Array<News> {
function getStatistics(mainSubjects: SubjectItem[]): NewsItem[] {
const statregStatistics: Array<StatisticInListing> = fetchStatisticsWithReleaseToday()
const serverOffsetInMS: number = parseInt(app.config?.['serverOffsetInMs']) || 0
const timeZoneIso: string = getTimeZoneIso(serverOffsetInMS)

const statisticsNews: Array<News> = []
const statisticsNews: NewsItem[] = []
if (statregStatistics.length > 0) {
mainSubjects.forEach((mainSubject) => {
const statistics: Array<Content<Statistics & Statistic>> = query({
Expand Down Expand Up @@ -163,7 +167,7 @@ function getLinkByPath(path: string) {
return baseUrl + path.substring(site.length)
}

interface News {
interface NewsItem {
guid: string // _id
title: string // displayName
link: string // url
Expand Down
14 changes: 9 additions & 5 deletions src/main/resources/lib/ssb/rss/statkal.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,7 @@ const dummyReq: Partial<XP.Request> = {
}

export function getRssItemsStatkal(): string | null {
const statisticVariants: ContentLight<ReleaseVariant>[] = getUpcompingStatisticVariantsFromRepo()
const allMainSubjects: SubjectItem[] = getMainSubjects(dummyReq as XP.Request)
const upcomingVariants: StatkalVariant[] = getUpcomingVariants(statisticVariants, allMainSubjects)
const upcomingReleases: StatkalRelease[] = getUpcomingReleases(statisticVariants)
const rssReleases: RssRelease[] = getRssReleases(upcomingVariants, upcomingReleases)
const rssReleases: RssRelease[] = getRssReleasesStatkal()

const xml = rssReleases
? `<?xml version="1.0" encoding="utf-8"?>
Expand Down Expand Up @@ -58,6 +54,14 @@ export function getRssItemsStatkal(): string | null {
return xml
}

export function getRssReleasesStatkal(): RssRelease[] {
const statisticVariants: ContentLight<ReleaseVariant>[] = getUpcompingStatisticVariantsFromRepo()
const allMainSubjects: SubjectItem[] = getMainSubjects(dummyReq as XP.Request)
const upcomingVariants: StatkalVariant[] = getUpcomingVariants(statisticVariants, allMainSubjects)
const upcomingReleases: StatkalRelease[] = getUpcomingReleases(statisticVariants)
return getRssReleases(upcomingVariants, upcomingReleases)
}

function getUpcomingVariants(
statisticVariants: ContentLight<ReleaseVariant>[],
allMainSubjects: SubjectItem[]
Expand Down
10 changes: 10 additions & 0 deletions src/main/resources/services/rssNews/rssNews.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import { getNews, getRssItemsNews } from '/lib/ssb/rss/news'

export function get(req: XP.Request): XP.Response {
const format: string = req.params.format ?? 'json'
const isXml = format === 'xml'
return {
body: isXml ? getRssItemsNews() : getNews(),
contentType: isXml ? 'text/xml' : 'application/json',
}
}
5 changes: 5 additions & 0 deletions src/main/resources/services/rssNews/rssNews.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<service>
<allow>
<principal>role:system.everyone</principal>
</allow>
</service>
10 changes: 10 additions & 0 deletions src/main/resources/services/rssStatkal/rssStatkal.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import { getRssReleasesStatkal, getRssItemsStatkal } from '/lib/ssb/rss/statkal'

export function get(req: XP.Request): XP.Response {
const format: string = req.params.format ?? 'json'
const isXml = format === 'xml'
return {
body: isXml ? getRssItemsStatkal() : getRssReleasesStatkal(),
contentType: isXml ? 'text/xml' : 'application/json',
}
}
5 changes: 5 additions & 0 deletions src/main/resources/services/rssStatkal/rssStatkal.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<service>
<allow>
<principal>role:system.everyone</principal>
</allow>
</service>

0 comments on commit b65d377

Please sign in to comment.