contrib/tnitter: intro push notification service to the server
authorAlexis Laferrière <alexis.laf@xymus.net>
Tue, 10 Nov 2015 13:48:08 +0000 (08:48 -0500)
committerAlexis Laferrière <alexis.laf@xymus.net>
Wed, 11 Nov 2015 20:25:26 +0000 (15:25 -0500)
Signed-off-by: Alexis Laferrière <alexis.laf@xymus.net>

contrib/tnitter/src/push.nit [new file with mode: 0644]
contrib/tnitter/src/tnitter.nit

diff --git a/contrib/tnitter/src/push.nit b/contrib/tnitter/src/push.nit
new file mode 100644 (file)
index 0000000..cdb2945
--- /dev/null
@@ -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
index 35d2432..e0311a3 100644 (file)
@@ -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