contrib/benitlux: add push notification support
authorAlexis Laferrière <alexis.laf@xymus.net>
Fri, 1 Apr 2016 15:58:33 +0000 (11:58 -0400)
committerAlexis Laferrière <alexis.laf@xymus.net>
Tue, 5 Apr 2016 12:51:17 +0000 (08:51 -0400)
Signed-off-by: Alexis Laferrière <alexis.laf@xymus.net>

contrib/benitlux/src/benitlux_controller.nit
contrib/benitlux/src/benitlux_web.nit

index e20eab7..4e666b2 100644 (file)
@@ -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
index 302b5de..683e105 100644 (file)
@@ -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