...
 
Commits (6)
var BASE_URL = "https://api.ubports.com/v1/"
var BASE_URL = "/api/"
var get = {
devices: function(t) {
......
......@@ -29,7 +29,7 @@ var communityList = new Vue({
},
methods: {
go: function(device) {
window.location.href = '/device/'+device;
window.location.href = '/communitydevice/'+device;
},
get: function() {
get.communityDevices(this).then(function(response) {
......
......@@ -12,85 +12,123 @@ const time = () => Math.floor(new Date() / 1000);
const BASE_URL = "https://api.ubports.com/v1/";
var cache = {}
var testRun = 0;
// Internal
const getDevices = () => {
const getCached = (url) => {
return new Promise(function(resolve, reject) {
console.log("devices request");
console.log("cached request", url);
var now=time();
if (!cache[url])
cache[url] = {expire:0};
// Cache baby cache!!! :D :D
if (cache.devices.expire > now) {
console.log("use cache");
resolve(cache.devices.data);
if (cache[url].expire > now) {
console.log("use cache")
resolve(cache[url].data);
return;
}
console.log("get new");
request({
method: "get",
url: BASE_URL+"devices",
url: url,
json: true,
headers: {
'User-Agent': 'client request: server devices.ubuntu-touch.io'
}
}, (err, res, body) => {
// If we hit an error, try using cache!
if (err)
resolve(cache.devices.data)
resolve(body);
// 3 munutes cache!
cache.devices.expire = time()+180;
cache.devices.data = body;
});
});
}
var data = {err: err, res: res, body: body};
var saveCache = true;
const getDevice = (device) => {
return new Promise(function(resolve, reject) {
console.log("devices request");
var now=time();
/* For testing
if (testRun == 1) {
console.log("testing err")
err = true;
}
if (testRun == 2) {
console.log("testing testing 500")
data.res.statusCode = 500;
}
if (testRun == 3) {
console.log("testing 404")
data.res.statusCode = 404;
}
if (testRun == 4) {
console.log("testing no data")
err =true;
delete cache[url].data;
}
if (!cache[device])
cache[device] = {expire:0};
testRun++;
*/
// Cache baby cache!!! :D :D
if (cache[device].expire > now) {
console.log("use cache")
resolve(cache[device].data);
return;
}
console.log("get new");
request({
method: "get",
url: BASE_URL+"devices/"+device,
json: true,
headers: {
'User-Agent': 'client request: server devices.ubuntu-touch.io'
if (err || !res) {
if (cache[url].data) {
console.log('Something is wrong with the server, serving cached content');
data = cache[url].data;
saveCache = false;
} else {
console.log('Something is wrong with the server, but we don\'t have cached contect! rejecting!');
return reject();
}
}
}, (err, res, body) => {
// Device not found, send reject!
if(res.statusCode === 404)
if(data.res.statusCode === 404) {
console.log("Server is returning 404!");
return reject();
}
// If we hit an error, try using cache!
if (err)
return resolve(cache[device].data);
if (body.about)
body.about = md.render(body.about);
if (data.res.statusCode != 200) {
console.log('Server is returning', data.res.statusCode, 'serving cached content');
return resolve(cache[url].data);
}
resolve(body);
resolve({err: err, res: res, body: body});
// 3 minutes cache!
cache[device].expire = time()+180;
cache[device].data = body;
if (saveCache) {
console.log("saving cache");
cache[url].expire = time()+10 //time()+180;
cache[url].data = {err: err, res: res, body: body};
}
});
});
}
// Internal
const getDevices = () => {
return getCached(BASE_URL+"devices").then(data => {
return data.body;
});
}
const getCommunityDevices = () => {
return getCached(BASE_URL+"devices/community").then(data => {
return data.body;
});
}
const getDevice = (device) => {
return getCached(BASE_URL+"devices/"+device).then(data => {
if (data.body.about)
data.body.about = md.render(data.body.about);
return data.body;
});
}
const getCommunityDevice = (device) => {
return getCached(BASE_URL+"devices/community/"+device).then(data => {
if (data.body.about)
data.body.about = md.render(data.body.about);
return data.body;
});
}
const getInstaller = (installerPackage) => {
return new Promise((resolve, reject) => {
switch (installerPackage) {
......@@ -107,12 +145,10 @@ const getInstaller = (installerPackage) => {
case "microsoft":
case "windows":
return installerRelease.getAssetUrl("latest", "exe").then((r) => {resolve(r);});
return;
case "apple":
case "mac":
case "macos":
return installerRelease.getAssetUrl("latest", "dmg").then((r) => {resolve(r);});
return;
default:
return installerRelease.getAssetUrl("latest", installerPackage).then((r) => {resolve(r);});
}
......@@ -164,10 +200,18 @@ router.get('/api/devices', function(req, res, next) {
getDevices().then(r => res.send(r));
});
router.get('/api/devices/community', function(req, res, next) {
getCommunityDevices().then(r => res.send(r));
});
router.get('/api/device/:device', function(req, res, next) {
getDevice(req.params.device).then(r => res.send(r)).catch(() => res.send(404));
});
router.get('/api/devices/community/:device', function(req, res, next) {
getCommunityDevice(req.params.device).then(r => res.send(r)).catch(() => res.send(404));
});
router.get('/device/:device', function(req, res, next) {
Promise.all([
getDevice(req.params.device),
......@@ -211,6 +255,18 @@ router.get('/device/:device', function(req, res, next) {
});
});
router.get('/communitydevice/:device', function(req, res, next) {
getCommunityDevice(req.params.device).then((r) => {
console.log(JSON.stringify(r))
res.render('communitydevice', {
data: r
});
}).catch((e) => {
console.log(e);
notFound(res)
});
});
router.get('/installer/:package', function(req, res, next) {
// redirect to download url of the requested package
// or package list if the requested package does not exist
......
extends components/layout
block content
include components/donate-modal
include components/bitcoin-modal
include components/installer-modal
script.
setTimeout(() => {$('#donateModal').modal('show');}, 10000);
if data.install.installable
include components/manual-installation-modal
.container
.row
.col.col-md-12
p.device-header #{data.name} (#{data.device})
.row
.col.col-md-8
.card
.card-header.orange-background About the #{data.name} with Ubuntu Touch
.card-body!=data.about
.card(style="margin-top: 20px")
include device/install
.card(style="margin-top: 20px")
.card-header.orange-background Donate
.card-body
include components/donation-options
.col.col-md-4
.text-center
img.sidebar-img(src=data.image)
img.sidebar-img(src=data.deviceLogo)
if data.video
.card(style="margin-bottom: 20px")
include device/video
.card(style="margin-bottom: 20px")
include device/community
.card(style="margin-bottom: 20px")
include device/buy
if !data.video
.card(style="margin-bottom: 20px")
include device/join
......@@ -35,7 +35,7 @@
th Comment
th(style='width: 3%;')
tbody(id="communityList")
tr(v-for='device in devices', style='', v-on:click="")
tr(v-for='device in devices', style='', v-on:click="go(device.device)")
td {{device.name}}
td {{device.device}}
td(style='width: 45%; max-width: 0; text-overflow: ellipsis; overflow: hidden; white-space: nowrap;')
......
.card-header.orange-background Community device
.card-body
p This device is maintained by a community member and may not be fully functional yet. Find out how to create your own port #[a(href="http://docs.halium.org/en/latest/porting/first-steps.html", target="_blank") here].
.card-header.orange-background Make a move!
.card-body
div(style="margin-top: 0px;" class="aspect-ratio")
iframe(width="854" height="480" src="https://www.youtube.com/embed/XppN4Hqm1dQ?autoplay=0" frameborder="0" allowfullscreen)
iframe(width="854" height="480" src="https://www.youtube.com/embed/1sPtRdZlEu4?autoplay=0" frameborder="0" allowfullscreen)
br
a.btn.btn-default(href="https://ubports.com/get-involved", target="_blank") Join the Community!