Permalink
Browse files

Show the networks with the most recent activity first

  • Loading branch information...
1 parent 41aedc4 commit 86790cc3881143ead0035051f10334fe9cded2d3 @timjb timjb committed Jan 21, 2012
Showing with 75 additions and 19 deletions.
  1. +5 −0 couch/views/recent_networks/map.js
  2. +6 −0 db/schema.json
  3. +27 −9 server.js
  4. +17 −8 src/client/model.js
  5. +1 −1 src/server/document.js
  6. +19 −1 src/server/network.js
@@ -0,0 +1,5 @@
+function (doc) {
+ if (doc.type.indexOf('/type/network') >= 0) {
+ emit(doc.updated_at, doc._id);
+ }
+}
View
@@ -551,6 +551,12 @@
"unique": true,
"type": "date",
"required": true
+ },
+ "updated_at": {
+ "name": "Last modified",
+ "unique": true,
+ "type": "date",
+ "required": false
}
},
"indexes": {
View
@@ -449,7 +449,7 @@ app.post('/login', function(req, res) {
// Logout
-app.post('/logout', function(req, res) {
+app.post('/logout', function(req, res) {
req.session = {};
res.send({status: "ok"});
});
@@ -516,7 +516,6 @@ app.post('/updateuser', function(req, res) {
user.set({
name: req.body.name,
email: req.body.email,
- location: req.body.location,
website: req.body.website,
company: req.body.company,
location: req.body.location
@@ -658,7 +657,7 @@ app.post('/confirm_collaborator', function(req, res) {
graph.sync(function(err) {
return err ? res.send({"status": "error"}) : res.send({"status": "ok", "graph": {}});
- });
+ });
});
});
@@ -789,10 +788,11 @@ app.post('/publish', function(req, res) {
function createPublications(callback) {
graph.fetch({"type": "/type/publication", "document": document}, function(err, nodes) {
- var remoteNetworks = nodes.map(function(n) { return n.get('network')._id }).values(),
- newNetworks = _.difference(networks, remoteNetworks),
- deletedNetworks = _.difference(remoteNetworks, networks);
-
+ var remoteNetworks = nodes.map(function(n) { return n.get('network')._id; }).values(),
+ newNetworks = _.difference(networks, remoteNetworks),
+ deletedNetworks = _.difference(remoteNetworks, networks),
+ changedNetworks = [].concat(newNetworks).concat(deletedNetworks);
+
_.each(newNetworks, function(network) {
graph.set({
type: "/type/publication",
@@ -804,7 +804,15 @@ app.post('/publish', function(req, res) {
var pub = graph.find({type: "/type/publication", "network": network}).first()._id;
graph.del(pub);
});
- callback();
+ async.forEach(changedNetworks, function (id, cb) {
+ graph.fetch({ _id: id }, function (err, nodes) {
+ if (err) { cb(err); return; }
+ nodes.each(function (network) {
+ network.set({ updated_at: new Date() });
+ });
+ cb(null);
+ });
+ }, callback);
});
}
@@ -835,7 +843,7 @@ app.post('/publish', function(req, res) {
newGraph[id] = graph.get(id);
res.send({
"status": "ok",
- "version": version._id,
+ "version": version._id,
"graph": newGraph
});
});
@@ -877,6 +885,16 @@ app.post('/unpublish', function(req, res) {
});
+app.get('/networks/recent', function (req, res) {
+ Network.getNMostRecent(50, function (err, graph) {
+ res.json({
+ error: err,
+ graph: graph.objects()
+ });
+ });
+});
+
+
// Returns a specific version of the requested doc
app.get('/documents/:username/:name/:version', function(req, res) {
Document.get(req.params.username, req.params.name, req.params.version, req.session ? req.session.username : null, function(err, graph, id, authorized, version, published) {
View
@@ -373,14 +373,23 @@ function loadExplore (callback) {
dataType: "json",
success: function (res) {
graph.merge(res.graph);
-
- // Populate results
- var documents = graph.find({"type|=": "/type/document"});
- graph.fetch({type: "/type/network"}, function(err, nodes) {
- err ? callback(err) : callback(null, {
- networks: nodes,
- documents: sortDocuments(documents)
- });
+
+ $.ajax({
+ type: 'GET',
+ url: '/networks/recent',
+ dataType: 'json',
+ success: function (res) {
+ graph.merge(res.graph);
+
+ // Populate results
+ var documents = graph.find({"type|=": "/type/document"});
+ var networks = graph.find({"type": "/type/network"});
+ callback(null, {
+ networks: networks,
+ documents: sortDocuments(documents)
+ });
+ },
+ error: function (err) { callback(err); }
});
},
error: function(err) {
View
@@ -257,7 +257,7 @@ Document.getContent = function(documentId, callback) {
var qry = {
"_id": documentId,
"children": {
- "_recursive": true,
+ "_recursive": true
}
};
View
@@ -10,12 +10,30 @@ var Util = require('./util');
var Network = {};
+Network.getNMostRecent = function (n, callback) {
+ var graph = new Data.Graph(seed).connect('couch', {
+ url: config.couchdb_url,
+ filters: [Filters.addMeta()]
+ });
+ db.view('substance/recent_networks', { limit: n, descending: true }, function (err, res) {
+ if (err) { callback(err); return; }
+ var qry = {
+ type: '/type/network',
+ _id: _.map(res.rows, function (row) { return row.value; })
+ };
+ graph.fetch(qry, function (err) {
+ if (err) { callback(err); return; }
+ callback(null, graph);
+ });
+ });
+};
+
Network.get = function(network, currentUser, callback) {
var graph = new Data.Graph(seed).connect('couch', {url: config.couchdb_url});
graph.fetch({type: "/type/network", _id: "/network/"+network}, function(err, nodes) {
graph.fetch({type: "/type/publication", "network": "/network/"+network, "document": {}}, function(err, nodes) {
graph.fetch({"type": "/type/membership", "network": "/network/"+network, "user": {}}, function(err, nodes) {
- callback(null, {
+ callback(null, {
graph: graph.objects().toJSON(),
isMember: !!nodes.get('/user/'+currentUser)
});

0 comments on commit 86790cc

Please sign in to comment.