From cf6212a76c1bc727bdc68c4cea577f4b20f53ba6 Mon Sep 17 00:00:00 2001 From: =?utf8?q?Alexis=20Laferri=C3=A8re?= Date: Fri, 1 Apr 2016 11:58:33 -0400 Subject: [PATCH] contrib/benitlux: add push notification support MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit Signed-off-by: Alexis Laferrière --- contrib/benitlux/src/benitlux_controller.nit | 52 ++++++++++++++++++++++++++ contrib/benitlux/src/benitlux_web.nit | 1 + 2 files changed, 53 insertions(+) diff --git a/contrib/benitlux/src/benitlux_controller.nit b/contrib/benitlux/src/benitlux_controller.nit index e20eab7..4e666b2 100644 --- a/contrib/benitlux/src/benitlux_controller.nit +++ b/contrib/benitlux/src/benitlux_controller.nit @@ -292,6 +292,24 @@ class BenitluxRESTAction var common_followers = db.followed_followers(id) db.close + # Sent push notifications to connected reciprocal friends + if common_followers != null then + for friend in common_followers do + var conn = push_connections.get_or_null(friend.id) + if conn != null then + push_connections.keys.remove friend.id + if not conn.closed then + var report = db.checkedin_followed_followers(friend.id) + var response = if report == null then + new HttpResponse.server_error + else new HttpResponse.ok(report) + conn.respond response + conn.close + end + end + end + end + return new HttpResponse.ok(true) end @@ -334,6 +352,40 @@ class BenitluxRESTAction end # --- +# Push notification + +# Benitlux push notification interface +class BenitluxPushAction + super BenitluxAction + + # Intercept the full answer to set aside the connection and complete it later + redef fun prepare_respond_and_close(request, turi, connection) + do + var token = request.string_arg("token") + + var db = new DB.open(db_path) + var user = db.token_to_id(token) + db.close + + if user == null then + # Report errors right away + var response = new HttpResponse.invalid_token + connection.respond response + connection.close + return + end + + # Set aside the connection + push_connections[user] = connection + end +end + +redef class Sys + # Connections left open for a push notification, organized per user id + private var push_connections = new Map[Int, HttpServer] +end + +# --- # Misc services redef class Text diff --git a/contrib/benitlux/src/benitlux_web.nit b/contrib/benitlux/src/benitlux_web.nit index 302b5de..683e105 100644 --- a/contrib/benitlux/src/benitlux_web.nit +++ b/contrib/benitlux/src/benitlux_web.nit @@ -27,6 +27,7 @@ fun iface: String do return "localhost:8080" var vh = new VirtualHost(iface) vh.routes.add new Route("/rest/", new BenitluxRESTAction) +vh.routes.add new Route("/push/", new BenitluxPushAction) vh.routes.add new Route(null, new BenitluxSubscriptionAction) var factory = new HttpFactory.and_libevent -- 1.7.9.5