From 5ac96f4edc5b0a2ab528d424c38bb0d9aecad645 Mon Sep 17 00:00:00 2001 From: Brian Hendrickson Date: Sun, 23 Jun 2013 14:30:26 -0700 Subject: [PATCH] getting web mentions togeter --- node_modules/zygote/index.js | 81 ++++++++++++++++++++++++++++++++---- 1 file changed, 74 insertions(+), 7 deletions(-) diff --git a/node_modules/zygote/index.js b/node_modules/zygote/index.js index 6978b9a..d0afe0d 100755 --- a/node_modules/zygote/index.js +++ b/node_modules/zygote/index.js @@ -129,7 +129,7 @@ Model.prototype.events = {}; Model.prototype.data = []; Model.prototype.create = function( data ) { - var client = new pg.Client('postgres://'+prefs['dbuser']+':@'+prefs['dbhost']+':'+prefs['dbport'].toString()+'/'+prefs['dbname']); + var client = new pg.Client('postgres://'+prefs['dbuser']+':'+prefs['dbpass']+'@'+prefs['dbhost']+':'+prefs['dbport'].toString()+'/'+prefs['dbname']); var model = this; client.on('drain', client.end.bind(client)); client.connect(); @@ -165,10 +165,13 @@ Model.prototype.create = function( data ) { 'target': target }); var parsed = url.parse(endpoint); - console.log('posting to '+parsed.hostname+parsed.port+parsed.path); + var port = 80; + if (!(null == parsed.port)) + port = parsed.port; + console.log('posting to '+parsed.hostname+port.toString()+parsed.path); var post_req = http.request({ host: parsed.hostname, - port: parsed.port, + port: port, path: parsed.path, method: 'POST', headers: { @@ -217,7 +220,7 @@ Model.prototype.find = function(id) { var modelname = get_class(this).toLowerCase(); var model = this; var rows = []; - var client = new pg.Client('postgres://'+prefs['dbuser']+':@'+prefs['dbhost']+':'+prefs['dbport'].toString()+'/'+prefs['dbname']); + var client = new pg.Client('postgres://'+prefs['dbuser']+':'+prefs['dbpass']+'@'+prefs['dbhost']+':'+prefs['dbport'].toString()+'/'+prefs['dbname']); client.on('drain', client.end.bind(client)); client.connect(); var query = client.query({ @@ -376,8 +379,72 @@ get('/post/_show.html', function(req,res){ return file( 'views'+'/post/_show.html',res ); }); post('/mention',function(req,res){ - console.log('web mention'); - res.end('sweet'); + + var querystring = require('querystring'); + + var fullBody = ''; + + req.on('data', function(chunk) { + fullBody += chunk.toString(); + }); + + req.on('end', function() { + + var decodedBody = querystring.parse(fullBody); + + var in_reply_to_id = 0; + + var parsed = url.parse(decodedBody.target); + var myarray = parsed.path.split(/[\/]/); + if (!(undefined == myarray[1])) + if (isInt(myarray[1])) + in_reply_to_id = myarray[1]; + + if (in_reply_to_id > 0) { + + + var client = new pg.Client('postgres://'+prefs['dbuser']+':@'+prefs['dbhost']+':'+prefs['dbport'].toString()+'/'+prefs['dbname']); + client.on('drain', client.end.bind(client)); + client.connect(); + var query = client.query({ + text: 'CREATE TABLE mention (title text, body text, updated varchar(255), published varchar(255), author varchar(255), author_url varchar(255), author_email varchar(255), profile_image varchar(255), in_reply_to int, reply_url varchar(255), id serial)', + values: [] + }); + query.on('error', function(row) { + }); + query.on('end', function(result) { + + var ins_query = client.query({ + text: 'INSERT INTO mention (title,body,updated,published,author,author_url,author_email,profile_image,in_reply_to,reply_url) VALUES ($1,$2,$3,$4,$5,$6,$7,$8,$9,$10)', + values: ["","","","","","","","http://megapump.com/defaultavatar.jpg",in_reply_to_id,decodedBody.source] + }); + ins_query.on('end', function(result) { + + Post = (function(_super) { + __extends(Post, _super); + + function Post() { + _ref = Post.__super__.constructor.apply(this, arguments); + return _ref; + } + + return Post; + + })(Model); + + model = new Post(); + model.send( 'changed' ); + }); + + }); + + + } + + + + res.end('{"message":"sweet!"}'); + }); get('/webmention',function(req,res){ @@ -459,7 +526,7 @@ post('/post.json', function(req,res){ var id = false; var response = res; var rows = []; - var client = new pg.Client('postgres://'+prefs['dbuser']+':@'+prefs['dbhost']+':'+prefs['dbport'].toString()+'/'+prefs['dbname']); + var client = new pg.Client('postgres://'+prefs['dbuser']+':'+prefs['dbpass']+'@'+prefs['dbhost']+':'+prefs['dbport'].toString()+'/'+prefs['dbname']); client.on('drain', client.end.bind(client)); client.connect(); req.on('end', function() {