From c7caeea4de1a016171934c831bb73425e26bfe20 Mon Sep 17 00:00:00 2001 From: Ian Bicking Date: Wed, 2 Jan 2019 13:16:07 -0600 Subject: [PATCH] Fix #5251, add endpoint to check for indefinite shots --- server/src/server.js | 8 ++++++++ server/src/servershot.js | 33 +++++++++++++++++++++++++++++++++ 2 files changed, 41 insertions(+) diff --git a/server/src/server.js b/server/src/server.js index bd60d21a0b..c8499fe956 100644 --- a/server/src/server.js +++ b/server/src/server.js @@ -1198,6 +1198,14 @@ app.get("/api/fxa-oauth/confirm-login", async function(req, res, next) { } }); +app.get("/api/has-shots", async function(req, res) { + if (!(req.deviceId || req.accountId)) { + res.status(401).send("Authentication required"); + } + const hasShots = await Shot.userHasShots(req.deviceId, req.accountId); + res.send(hasShots); +}); + app.post("/watchdog/:submissionId", function(req, res) { Watchdog.handleResult(req); res.end(); diff --git a/server/src/servershot.js b/server/src/servershot.js index 6b27bfb6a5..f8b0ab103d 100644 --- a/server/src/servershot.js +++ b/server/src/servershot.js @@ -725,6 +725,39 @@ Shot.deleteEverythingForDevice = function(backend, deviceId, accountId) { .then(deleteShotRecords); }; +Shot.userHasShots = async function(deviceId, accountId) { + let deviceIds = [{id: deviceId}]; + if (accountId) { + deviceIds = await db.select( + `SELECT devices.id + FROM devices + WHERE devices.id = $1 OR devices.accountid = $2 + `, + [deviceId, accountId]); + } + deviceIds = deviceIds.map(row => row.id); + const markers1 = db.markersForArgs(1, deviceIds.length); + const markers2 = db.markersForArgs(deviceIds.length + 1, deviceIds.length); + const hasSome = await db.select(` + SELECT + EXISTS ( + SELECT data.id + FROM data + WHERE data.expire_time IS NULL + AND NOT data.deleted + AND data.deviceid IN (${markers1}) + ) AS hasindefinite, + EXISTS ( + SELECT data.id + FROM data + WHERE NOT data.deleted + AND data.deviceid IN (${markers2}) + ) AS hasany + `, deviceIds.concat(deviceIds)); + return {hasIndefinite: hasSome[0].hasindefinite, hasAny: hasSome[0].hasany}; +}; + + const ClipRewrites = class ClipRewrites { constructor(shot) {