From e7d77b8c97d09c296fe6877dca376af95d18f25f Mon Sep 17 00:00:00 2001 From: =?utf8?q?Alexis=20Laferri=C3=A8re?= Date: Tue, 10 Nov 2015 08:48:08 -0500 Subject: [PATCH] contrib/tnitter: intro push notification service to the server MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit Signed-off-by: Alexis Laferrière --- contrib/tnitter/src/push.nit | 60 +++++++++++++++++++++++++++++++++++++++ contrib/tnitter/src/tnitter.nit | 2 ++ 2 files changed, 62 insertions(+) create mode 100644 contrib/tnitter/src/push.nit diff --git a/contrib/tnitter/src/push.nit b/contrib/tnitter/src/push.nit new file mode 100644 index 0000000..cdb2945 --- /dev/null +++ b/contrib/tnitter/src/push.nit @@ -0,0 +1,60 @@ +# This file is part of NIT ( http://www.nitlanguage.org ). +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +# Tnitter support for push notifications +module push + +import nitcorn +import json::serialization + +import model +import database + +# Tnitter push notification interface +class TnitterPush + super Action + + # Intercept the full answer to set aside the connection and complete it later + redef fun prepare_respond_and_close(request, turi, connection) + do + push_connections.add connection + end +end + +redef class Sys + # Connections left open for a push notification + private var push_connections = new Array[HttpServer] +end + +redef class DB + # Reopen to trigger sending push notifications + redef fun post(user, text) + do + super + + # Everyone gets the same response + var posts = list_posts(0, 16) + var response = new HttpResponse(400) + response.body = posts.to_json_string + + for conn in push_connections do + # Complete the answer to `conn` + conn.respond response + conn.close + end + + # Clients need to open new connections + push_connections.clear + end +end diff --git a/contrib/tnitter/src/tnitter.nit b/contrib/tnitter/src/tnitter.nit index 35d2432..e0311a3 100644 --- a/contrib/tnitter/src/tnitter.nit +++ b/contrib/tnitter/src/tnitter.nit @@ -21,6 +21,7 @@ import privileges import model import action +import push redef class OptionContext var drop = new OptionUserAndGroup.for_dropping_privileges @@ -59,6 +60,7 @@ if user_group != null then user_group.drop_privileges # Complete server config vh.routes.add new Route("/rest/", new TnitterREST) +vh.routes.add new Route("/push/", new TnitterPush) vh.routes.add new Route(null, new TnitterWeb) # Run -- 1.7.9.5