Created
December 26, 2013 16:00
-
-
Save ierceg/8135390 to your computer and use it in GitHub Desktop.
An example script for webscript.io platform that works with their persistent storage It's an an unfinished script that was meant as an implementation of a very simple push notification service with backend in Apple Push Notification Service (APNS). I didn't finish it as webscript.io promises unlimited storage but it's actually severely limited a…
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
-- Finds the index of the device token or -1 if the token doesn't exist in the devices. | |
function findDeviceIndexForToken(devices, deviceToken) | |
for i,v in ipairs(devices) do | |
if v.token == deviceToken then | |
return i | |
end | |
end | |
return -1 | |
end | |
-- Adds a new device to the devices, unless it already exists. | |
function register(devices, deviceToken) | |
index = findDeviceIndexForToken(devices, deviceToken) | |
if index > 0 then | |
return index | |
end | |
-- Add new device and return the new index | |
devices[#devices + 1] = { | |
token = deviceToken, | |
badgeCounter = 0 | |
} | |
return #devices | |
end | |
-- Increments the badge counter for all d evices on the network | |
-- except for the one that invoked increment | |
function increment(devices, deviceToken) | |
deviceIndex = register(devices, deviceToken) | |
for i,device in ipairs(devices) do | |
if device.token ~= deviceToken then | |
device.badgeCounter = device.badgeCounter + 1 | |
-- Create the payload | |
local payload = { | |
aps = { | |
badge = device.badgeCounter, | |
sound = 'default' | |
} | |
} | |
payload['aps']['content-available'] = 1 | |
end | |
end | |
return deviceIndex | |
end | |
-- Resets the badge counter for the device. | |
function reset(devices, deviceToken) | |
deviceIndex = register(devices, deviceToken) | |
devices[deviceIndex].badgeCounter = 0 | |
return deviceIndex | |
end | |
-- The actual script execution starts here. | |
-- Gets all the query parameters. | |
local action = request.query.action | |
if not action then | |
return "Invalid action" | |
end | |
local appName = request.query.appName | |
if not appName then | |
return "Invalid app name" | |
end | |
local networkId = request.query.networkId | |
if not networkId then | |
return "Invalid network ID" | |
end | |
local deviceToken = request.query.deviceToken | |
if not deviceToken then | |
return "Invalid device token" | |
end | |
-- Creates the key for the application name and network ID. | |
-- This isolates the application name and its network ID from all the other | |
-- applications/networks in the persisted storage. | |
local appNetworkDevicesId = appName .. networkId | |
-- We acquire a lock on the specific application name and network ID. | |
-- All leases are released when script finishes. | |
lease.acquire(appNetworkDevicesId) | |
local devices = json.parse(storage.appNetworkDevicesId or '[]') | |
local returnValue | |
-- Returns all the registered device tokens and their network IDs. | |
if action == 'get' then | |
returnValue = devices | |
end | |
-- Registers a new device token with the network. | |
if action == 'register' then | |
returnValue = devices[register(devices, deviceToken)] | |
end | |
-- Increments the badges in the network. | |
if action == 'increment' then | |
deviceIndex = increment(devices, deviceToken) | |
-- Cannot implement: pushNotification(devices, deviceIndex) | |
returnValue = devices[deviceIndex] | |
end | |
-- Resets the badge count for the given device. | |
if action == 'reset' then | |
returnValue = devices[reset(devices, deviceToken)] | |
end | |
-- Persists the state of devices of the network for the specific app. | |
storage.appNetworkDevicesId = json.stringify(devices) | |
-- Explicitly releases the lock to avoid keeping the lock more than strictly necessary. | |
lease.release(appNetworkDevicesId) | |
-- Returns the response as JSON. | |
return json.stringify(returnValue) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment