Skip to content

Commit

Permalink
try to fix #2163 (#2164)
Browse files Browse the repository at this point in the history
- by unwrapping logic into modern async/await and ensuring array
  • Loading branch information
foxriver76 authored Oct 20, 2023
1 parent b002641 commit fda7786
Show file tree
Hide file tree
Showing 2 changed files with 97 additions and 99 deletions.
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,9 @@ The icons may not be reused in other projects without the proper flaticon licens
### **WORK IN PROGRESS**
-->
## Changelog
### **WORK IN PROGRESS**
* (foxriver76) fixed issue when updating news in backend

### 6.11.0 (2023-10-19)
* (foxriver76) jsonConfig type number now stores values as number instead of string
* (foxriver76) objects browser number value input behavior change to allow leading minus
Expand Down
193 changes: 94 additions & 99 deletions main.js
Original file line number Diff line number Diff line change
Expand Up @@ -667,121 +667,116 @@ class Admin extends utils.Adapter {
});
}

// read news from server
updateNews() {
/**
* Read news from server and register them as notifications
*
* @return {Promise<void>}
*/
async updateNews() {
this.timerNews && clearTimeout(this.timerNews);
this.timerNews = null;

this.checkNodeJsVersion().catch(e => this.log.warn(`Cannot check node.js versions: ${e}`));

let oldEtag;
let newNews;
let oldNews;
let originalOldNews;
let newEtag;

return (
this.getStateAsync('info.newsETag')
.then(state => {
oldEtag = state?.val;
return axios
.get('https://iobroker.live/repo/news-hash.json', {
timeout: 13_000,
validateStatus: status => status < 400,
})
.then(response => response.data)
.catch(error =>
this.log.warn(
`Cannot update news: ${
error.response ? error.response.data : error.message || error.code
}`
)
);
})
.then(etag => {
if (etag && etag.hash !== oldEtag) {
newEtag = etag.hash;
return axios
.get('https://iobroker.live/repo/news.json', {
timeout: 14_000,
validateStatus: status => status < 400,
})
.then(response => response.data)
.catch(error =>
this.log.warn(
`Cannot update news_: ${
error.response ? error.response.data : error.message || error.code
}`
)
);
} else {
newEtag = oldEtag;
return Promise.resolve([]);
}
})
.then(_newNews => {
newNews = _newNews || [];
return this.getStateAsync('info.newsFeed');
})
.then(state => {
try {
/** @ts-expect-error */
oldNews = state?.val ? JSON.parse(state.val) : [];
} catch (e) {
oldNews = [];
}
originalOldNews = JSON.stringify(oldNews);

return this.getStateAsync('info.newsLastId');
})
/** @ts-expect-error */
.then(lastState => {
// find time of last ID
let time = '';
if (lastState?.val) {
const item = oldNews.find(item => item.id === lastState.val);
if (item) {
time = item.created;
}
}
const oldEtag = (await this.getStateAsync('info.newsETag'))?.val;

// add all IDs newer than last seen
newNews.forEach(item => {
if (!lastState || !time || item.created > time) {
if (!oldNews.find(it => it.created === item.created)) {
oldNews.push(item);
}
}
});
let etag;

oldNews.sort((a, b) => (a.created > b.created ? -1 : a.created < b.created ? 1 : 0));
try {
const res = await axios.get('https://iobroker.live/repo/news-hash.json', {
timeout: 13_000,
validateStatus: status => status < 400,
});

// delete news older than 3 months
let i;
for (i = oldNews.length - 1; i >= 0; i--) {
if (Date.now() - new Date(oldNews[i].created).getTime() > 180 * 24 * 3600000) {
oldNews.splice(i, 1);
}
}
etag = res.data;
} catch (e) {
this.log.warn(`Cannot update news: ${e.response ? e.response.data : e.message || e.code}`);
}

if (originalOldNews !== JSON.stringify(oldNews)) {
this.registerNewsNotifications(oldNews, lastState?.val);
return this.setStateAsync('info.newsFeed', JSON.stringify(oldNews), true);
} else {
return Promise.resolve();
let _newNews;

if (etag && etag.hash !== oldEtag) {
newEtag = etag.hash;

try {
const res = await axios.get('https://iobroker.live/repo/news.json', {
timeout: 14_000,
validateStatus: status => status < 400,
});

_newNews = res.data;
} catch (e) {
this.log.warn(`Cannot update news_: ${e.response ? e.response.data : e.message || e.code}`);
}
} else {
newEtag = oldEtag;
_newNews = [];
}

const newNews = Array.isArray(_newNews) ? _newNews : [];

const newsState = await this.getStateAsync('info.newsFeed');

try {
/** @ts-expect-error */
oldNews = newsState?.val ? JSON.parse(newsState.val) : [];
} catch {
oldNews = [];
}

const originalOldNews = JSON.stringify(oldNews);

const lastState = await this.getStateAsync('info.newsLastId');

// find time of last ID
let time = '';
if (lastState?.val) {
const item = oldNews.find(item => item.id === lastState.val);
if (item) {
time = item.created;
}
}

try {
// add all IDs newer than last seen
newNews.forEach(item => {
if (!lastState || !time || item.created > time) {
if (!oldNews.find(it => it.created === item.created)) {
oldNews.push(item);
}
})
/** @ts-expect-error */
.then(() =>
newEtag !== oldEtag ? this.setStateAsync('info.newsETag', newEtag, true) : Promise.resolve()
)
.catch(e => this.log.error(`Cannot update news: ${e}`))
.then(() => (this.timerNews = setTimeout(() => this.updateNews(), 24 * ONE_HOUR_MS + 1)))
);
}
});

oldNews.sort((a, b) => (a.created > b.created ? -1 : a.created < b.created ? 1 : 0));

// delete news older than 3 months
let i;
for (i = oldNews.length - 1; i >= 0; i--) {
if (Date.now() - new Date(oldNews[i].created).getTime() > 180 * 24 * 3600000) {
oldNews.splice(i, 1);
}
}

if (originalOldNews !== JSON.stringify(oldNews)) {
this.registerNewsNotifications(oldNews, lastState?.val);
this.setStateAsync('info.newsFeed', JSON.stringify(oldNews), true);
}

if (newEtag !== oldEtag) {
this.setStateAsync('info.newsETag', newEtag, true);
}
} catch (e) {
this.log.error(`Cannot update news: ${e.message}`);
}

this.timerNews = setTimeout(() => this.updateNews(), 24 * ONE_HOUR_MS + 1);
}

/**
* Add the nes to the notification system
* Add the news to the notification system
*
* @param {Record<string, any>[]} messages sorted news
* @param {string | undefined | null} lastMessageId lastMessageId, all after this has already been seen
Expand Down

0 comments on commit fda7786

Please sign in to comment.