~ps-jenkins/ubuntu-push/ubuntu-vivid-proposed

« back to all changes in this revision

Viewing changes to docs/example-server/app.js

  • Committer: Roberto Alsina
  • Date: 2014-10-24 14:05:51 UTC
  • mfrom: (91.179.41 automatic)
  • mto: This revision was merged to the branch mainline in revision 136.
  • Revision ID: roberto.alsina@canonical.com-20141024140551-tsdz3xggo2rbwlqj
MergeĀ fromĀ automatic

Show diffs side-by-side

added added

removed removed

Lines of Context:
182
182
    */
183
183
    if (cfg.play_notify_form) {
184
184
        app.post("/play-notify-form", function(req, resp) {
185
 
            resp.type('text/plain')
186
 
            if (!req.body.data||!req.body.nick) {
187
 
                resp.send(400, "invalid/empty fields\n")
188
 
                return
189
 
            }
190
 
            var data
191
 
            try {
192
 
                data = JSON.parse(req.body.data)
193
 
            } catch(e) {
194
 
                resp.send(400, "data is not JSON\n")
195
 
                return
 
185
            if (!req.body.message||!req.body.nick) {
 
186
                resp.redirect("/?error=invalid or empty fields in form")
 
187
                return
 
188
            }
 
189
            var data = {
 
190
                "message": {
 
191
                    "from": "website",
 
192
                    "message": req.body.message,
 
193
                    "to": req.body.nick.toLowerCase()
 
194
                },
 
195
                "notification": {
 
196
                }
 
197
            }
 
198
 
 
199
            if (req.body.enable) {
 
200
                var card = {
 
201
                    "summary": "The website says:",
 
202
                    "body": req.body.message,
 
203
                    "actions": ["appid://com.ubuntu.developer.ralsina.hello/hello/current-user-version"]
 
204
                }
 
205
                if (req.body.popup) {card["popup"] = true}
 
206
                if (req.body.persist) {card["persist"] = true}
 
207
                data["notification"]["card"] = card
 
208
                if (req.body.sound) {data["notification"]["sound"] = true}
 
209
                if (req.body.vibrate) {data["notification"]["vibrate"] = {"duration": 200}}
 
210
                if (req.body.counter) {data["notification"]["emblem-counter"] = {
 
211
                    "count": Math.floor(req.body.counter),
 
212
                    "visible": true
 
213
                }}
196
214
            }
197
215
            doNotify(true, req.body.nick, data, function() {
198
 
                resp.send(200, 'OK\n')
 
216
                resp.redirect("/")
199
217
            }, function() { // not found
200
 
                resp.send(400, "unknown nick\n")
 
218
                resp.redirect("/?error=unknown nick")
201
219
            }, resp)
202
220
        })
203
221
    }