first hello world working
authorMikael Frykholm <mikael@frykholm.com>
Mon, 13 Feb 2017 17:30:27 +0000 (18:30 +0100)
committerMikael Frykholm <mikael@frykholm.com>
Mon, 13 Feb 2017 17:30:27 +0000 (18:30 +0100)
This gets accepted by gnusocial with a manual PuSH (see TODO)

friends/server.py
friends/templates/feed.xml
setup.py

index 2895b8f6a166feaa01f67d06460fefc6b6b54002..853e78c77b4f175fe6506b948ed143355258596e 100644 (file)
@@ -2,11 +2,13 @@ import tornado.ioloop
 import tornado.web
 import os, os.path
 import tornado.httpserver
+import tornado.httpclient as httpclient
 import sqlite3
 import arrow
 import datetime
 from rd import RD, Link
 import hashlib
+import hmac
 db = None
 #insert into user (name,email) values('mikael','mikael@frykholm.com');
 #insert into entry (userid,text) values (1,'My thoughts on ostatus');
@@ -29,7 +31,7 @@ class PushHandler(tornado.web.RequestHandler):
         hub_topic = self.get_argument('hub.topic')
         hub_verify = self.get_argument('hub.verify')
         hub_lease_seconds = self.get_argument('hub.lease_seconds','')
-        hub_secret = self.get_argument('hub.sercret','')
+        hub_secret = self.get_argument('hub.secret','')
         hub_verify_token = self.get_argument('hub.verify_token','')
         print(self.request.body)
         if hub_mode == 'unsubscribe':
@@ -37,11 +39,24 @@ class PushHandler(tornado.web.RequestHandler):
         path = hub_topic.split(self.settings['domain'])[1]
         user = path.split('user/')[1]
         row = db.execute("select id from user where name=?",(user,)).fetchone()
-        if row: #FIXME calculate expire timestamp
-            db.execute("INSERT into subscriptions (userid, expires, callback, secret, verified) values (?,?,?,?)",(row['id'],datetime.datetime.now(),hub_secret,hub_callback,False))
+        expire = datetime.datetime.utcnow() + datetime.timedelta(seconds=int(hub_lease_seconds))
+        if row:
+            db.execute("INSERT into subscriptions (userid, expires, callback, secret, verified) "
+                       "values (?,?,?,?,?)",(row['id'],expire,hub_callback,hub_secret,False))
             db.commit()
             self.set_status(202)
-            #TODO add GET callback with the same data we got
+            http_client = httpclient.HTTPClient()
+            try:
+                response = http_client.fetch(hub_callback+"?hub.mode={}&hub.topic={}&hub.secret".format(hub_mode,hub_topic,hub_secret))
+                print(response.body)
+            except httpclient.HTTPError as e:
+                # HTTPError is raised for non-200 responses; the response
+                # can be found in e.response.
+                print("Error: " + str(e))
+            except Exception as e:
+                # Other errors are possible, such as IOError.
+                print("Error: " + str(e))
+                http_client.close()
             #TODO add secret to outgoing feeds with hmac
 
 class XrdHandler(tornado.web.RequestHandler):
@@ -80,12 +95,13 @@ class UserHandler(tornado.web.RequestHandler):
         entries = db.execute("select entry.id,text,ts from user,entry where user.id=entry.userid and user.name=?",(user,))
         #import pdb;pdb.set_trace()
         self.set_header("Content-Type", 'application/atom+xml')
-        self.render("templates/feed.xml", 
+        out = self.render("templates/feed.xml",
                     user=user, 
                     feed_url="{}/user/{}".format(self.settings['domain'], user), 
                     hub_url="{}/hub".format(self.settings['domain']), 
                     entries=entries,
                     arrow=arrow )
+        #digest = hmac.new()
 
 application = tornado.web.Application([
     (r"/.well-known/host-meta", XrdHandler),
@@ -93,10 +109,7 @@ application = tornado.web.Application([
     (r"/user/(.+)", UserHandler),
     (r"/hub", PushHandler),
     ],debug=True,**settings)
-srv = tornado.httpserver.HTTPServer(application, ssl_options={
-        "certfile":  "ronin.frykholm.com.pem",
-        "keyfile":  "ronin.frykholm.com.key",
-    })
+srv = tornado.httpserver.HTTPServer(application, )
 def setup_db(path):
     print("No db found, creating in {}".format(path))
     con = sqlite3.connect(path)
@@ -124,5 +137,7 @@ if __name__ == "__main__":
         setup_db(dbPath)
     db = sqlite3.connect(dbPath, detect_types=sqlite3.PARSE_DECLTYPES|sqlite3.PARSE_COLNAMES)
     db.row_factory = sqlite3.Row
-    srv.listen(443)
+    srv.listen(8080)
     tornado.ioloop.IOLoop.instance().start()
+    #TODO hmac.new(b'5cc324285ece71e21e9554f4056563806f6ce0b7e4ab18d0133b602f8ba7e87a',open("apa.xml","rb").read(),digestmod='sha1').hexdigest()
+'b75d4733e0802629a0c15d0faa8de8fc9778cd05' queue runner
index 320eb65d6ba20512c635b8986d3437ece800d9bd..30851312c3ae5b296d5822b6457bbfdbf578978d 100644 (file)
@@ -14,9 +14,8 @@
     <id>tag:frykholm.com,2016:{{entry['id']}}</id>
     <title>titeln</title>
     <updated>{{arrow.get(entry['ts']).isoformat()}}</updated>
-    <activity:verb>
-      http://activitystrea.ms/schema/1.0/post
-    </activity:verb>
+      <activity:object-type>http://activitystrea.ms/schema/1.0/note</activity:object-type>
+         <activity:verb>http://activitystrea.ms/schema/1.0/post</activity:verb>
     <content type="html" >
        {{entry['text']}}
     </content>
index 4f8f597851ee08c9d5260cb387ecaf9a603ad878..23edcec725b9f04a26ea726e2a838325bc68794f 100644 (file)
--- a/setup.py
+++ b/setup.py
@@ -12,7 +12,7 @@ setup(
     description='Ostatus app.',
     long_description=open('README.md').read(),
     install_requires=[
-        "tornadoweb >= 3.1",
+        "tornado >= 3.1",
         "sqlite >= 3",
     ],
 )