{"size": 1220, "ext": "js", "max_stars_repo_path": "data/article/FetchArticle.js", "max_stars_repo_name": "davwheat/MSFS2020-Livery-API", "max_stars_repo_stars_event_min_datetime": "2020-09-04T14:52:29.000Z", "max_stars_repo_stars_event_max_datetime": "2022-03-12T05:27:59.000Z", "max_issues_repo_path": "data/article/FetchArticle.js", "max_issues_repo_name": "GewoonJaap/MSFS2020-Livery-API", "max_issues_repo_issues_event_min_datetime": "2020-10-24T20:45:30.000Z", "max_issues_repo_issues_event_max_datetime": "2021-08-31T20:32:29.000Z", "max_forks_repo_path": "data/article/FetchArticle.js", "max_forks_repo_name": "davwheat/MSFS2020-Livery-API", "max_forks_repo_forks_event_min_datetime": "2020-09-05T09:48:49.000Z", "max_forks_repo_forks_event_max_datetime": "2020-10-24T20:31:43.000Z"}
{"max_stars_count": 6, "max_issues_count": 4, "max_forks_count": 2, "avg_line_length": 35.8823529412, "max_line_length": 143, "alphanum_fraction": 0.6713114754}
const CacheItem = require('../../Cache/CacheItem');
const Log = require('../../logger');
const { CACHE_ENABLED } = require('../../Constants');
const RefetchArticle = require('./RefetchArticle');

module.exports = FetchArticle;

/**
 * @param {import('../../Cache/Cache')} cache
 * @param {string} articleName
 *
 * @returns {Promise<[CacheItem, boolean]>}
 */
async function FetchArticle(cache, articleName) {
  if (!CACHE_ENABLED || !cache.data.baseManifests.feedArticles[articleName] || cache.data.baseManifests.feedArticles[articleName].hasExpired) {
    let s = await RefetchArticle(articleName);

    if (s === null) {
      if (cache.data.baseManifests.feedArticles[articleName]) {
        Log("Couldn't fetch updated article! Re-using old one.");
        return [cache.data.baseManifests.feedArticles[articleName], true];
      } else {
        Log("Couldn't find specified article");
        return ['**Article not found.**', false];
      }
    } else {
      cache.data.baseManifests.feedArticles[articleName] = new CacheItem(s, true, true);
      return [cache.data.baseManifests.feedArticles[articleName], false];
    }
  } else {
    return [cache.data.baseManifests.feedArticles[articleName], true];
  }
}
