Skip to content

Commit

Permalink
Add handling if size of text is too long and add explicit message to …
Browse files Browse the repository at this point in the history
…first tab if no data is found
  • Loading branch information
SaskiaKeil committed Aug 8, 2022
1 parent 8d382f5 commit e6d1fc9
Showing 1 changed file with 13 additions and 1 deletion.
14 changes: 13 additions & 1 deletion companion/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,16 @@ function getNearbyArticles(position) {
return response.json();
})
.then(function(response) {
// If no articles are found return with the content for the first tab
if ((response.hasOwnProperty("query") === false) || (response.query.hasOwnProperty("pages") === false)) {
var responseDict = {
'index': 1,
'title': 'No data available.',
'extract': 'Probably there are no other articles available in 5km distance of your location.'
};
messaging.peerSocket.send(responseDict);
}

// Iterate over list to build list of articles with relevant fields
// Only pick necessary fields in order to minimize the message size
var index = 1;
Expand All @@ -35,7 +45,9 @@ function getNearbyArticles(position) {
var responseDict = {
'index': index,
'title': page['title'],
'extract': page['extract']
// In total our message can only have 1027 bytes, so we slice it down and leave some space for index and title
// Reference: https://dev.fitbit.com/build/guides/communications/messaging/#maximum-message-size
'extract': page['extract'].slice(0, 900)
};
if (messaging.peerSocket.readyState == messaging.peerSocket.OPEN) {
messaging.peerSocket.send(responseDict);
Expand Down

0 comments on commit e6d1fc9

Please sign in to comment.