Skip to content

Commit

Permalink
add corner case handling
Browse files Browse the repository at this point in the history
  • Loading branch information
tsullivan committed Oct 30, 2019
1 parent 8561c1e commit c5a9d12
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 11 deletions.
34 changes: 28 additions & 6 deletions src/plugins/newsfeed/public/lib/api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,7 @@ const NEWSFEED_MAIN_INTERVAL = 120000; // A main interval to check for need to r
const NEWSFEED_FETCH_INTERVAL = moment.duration(1, 'day'); // how often to actually fetch the API
const NEWSFEED_LAST_FETCH_STORAGE_KEY = 'newsfeed.lastfetchtime';
const NEWSFEED_HASH_SET_STORAGE_KEY = 'newsfeed.hashes';
// const NEWSFEED_SERVICE_URL_TEMPLATE = 'https://feeds.elastic.co/kibana/v{VERSION}.json';
const NEWSFEED_SERVICE_URL_TEMPLATE = 'https://feeds.elastic.co/kibana/v7.4.1.json'; // FIXME: needs to support untagged dev branches
const NEWSFEED_SERVICE_URL_TEMPLATE = 'https://feeds.elastic.co/kibana/v7.0.0.json'; // FIXME: should act as a real template

class NewsfeedApiDriver {
constructor(private readonly kibanaVersion: string) {}
Expand Down Expand Up @@ -79,22 +78,45 @@ class NewsfeedApiDriver {
);
}

validateItem(item: Partial<NewsfeedItem>) {
if ([item.title, item.description, item.linkText, item.linkUrl].includes(undefined)) {
return false;
}
return true;
}

modelItems(items: ApiItem[]): Rx.Observable<FetchResult> {
// calculate hasNew
const { previous, current } = this.updateHashes(items);
const hasNew = current.length > previous.length;

// model feed items
const feedItems: NewsfeedItem[] = items.map(it => {
return {
// build feedItems
const feedItems: NewsfeedItem[] = items.reduce((accum: NewsfeedItem[], it: ApiItem) => {
const { expire_on: expireOn, languages } = it;
const expiration = moment(expireOn);
if (expiration.isBefore(Date.now())) {
return accum; // ignore item if expired
}

if (languages && !languages.includes(DEFAULT_LANGUAGE)) {
return accum; // ignore language mismatch
}

const tempItem = {
title: it.title[DEFAULT_LANGUAGE],
description: it.description[DEFAULT_LANGUAGE],
linkText: it.link_text[DEFAULT_LANGUAGE],
linkUrl: it.link_url[DEFAULT_LANGUAGE],
badge: it.badge != null ? it.badge![DEFAULT_LANGUAGE] : it.badge,
languages: it.languages,
};
});

if (!this.validateItem(tempItem)) {
return accum; // ignore if title, description, etc is missing
}

return [...accum, tempItem];
}, []);

return Rx.of({
hasNew,
Expand Down
9 changes: 4 additions & 5 deletions src/plugins/newsfeed/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,11 +24,10 @@ export interface ApiItem {
description: { [lang: string]: string };
link_text: { [lang: string]: string };
link_url: { [lang: string]: string };

badge: null; // not used phase 1
image_url: null; // not used phase 1
languages: null; // not used phase 1
publish_on: null; // not used phase 1
badge: { [lang: string]: string } | null;
languages: string[] | null;
image_url?: null; // not used phase 1
publish_on?: null; // not used phase 1
}

export interface NewsfeedItem {
Expand Down

0 comments on commit c5a9d12

Please sign in to comment.