activity-streams-mongoose
Advanced tools
Comparing version 0.0.13 to 0.0.14
61
index.js
@@ -1,1 +0,60 @@ | ||
module.exports = require('./lib/activityMongoose'); | ||
module.exports = function (){ | ||
var _ = require('underscore')._; | ||
var state = require('./lib/setup.js')(_, arguments); | ||
var defaultSort = '-published'; | ||
state.types = require('./lib/activityMongoose.js')(state.mongoose, state.db, state.options.defaultActorImage); | ||
this.Activity = state.types.Activity; | ||
// Functions | ||
this.getActivityStreamFirehose = function(n, fx) { | ||
state.Activity.find().sort(defaultSort).limit(n).exec(fx); | ||
} | ||
this.getActivityStream = function(streamName, n, fx) { | ||
state.Activity.find({streams:streamName}).sort(defaultSort).limit(n).exec(fx); | ||
} | ||
this.publish = function(streamName, activity) { | ||
var publisher = state.redisPublisher; | ||
if (!_.isArray(activity.streams)) { | ||
activity.streams = [] | ||
} | ||
if (!_.include(activity.streams, streamName)) { | ||
activity.streams.push(streamName); | ||
} | ||
activity.save(function(err) { | ||
if (!err && streamName && publisher) { | ||
publisher.publish(streamName, JSON.stringify(activity)); | ||
} | ||
}); | ||
} | ||
this.subscribe = function(streamName, fx) { | ||
if (state.redisClient && streamName) { | ||
state.redisClient.subscribe(streamName); | ||
state.redisClient.on("message", fx); | ||
} | ||
} | ||
this.unsubscribe = function(streamName, fx) { | ||
if (state.redisClient && streamName) { | ||
state.redisClient.unsubscribe(streamName); | ||
} | ||
} | ||
this.close = function() { | ||
state.mongoose.disconnect(); | ||
if (state.redisClient && state.redisPublisher) { | ||
state.redisClient.quit(); | ||
state.redisPublisher.quit(); | ||
} | ||
} | ||
return this; | ||
}; |
@@ -1,32 +0,3 @@ | ||
module.exports = function (){ | ||
module.exports = function(mongoose, db, defaultActorImage) { | ||
var _ = require('underscore')._; | ||
var mongoose = null, | ||
options = {}; | ||
if (arguments.length == 2) { | ||
mongoose = arguments[0]; | ||
options = arguments[1]; | ||
} else if (arguments.length == 1) { | ||
if (_.has(arguments[0], "redis") || _.has(arguments[0], "mongoUrl")) { | ||
options = arguments[0]; | ||
} else if (mongoose.connection != undefined) { | ||
mongoose = arguments[0]; | ||
} | ||
} | ||
if (!options.mongoUrl) { | ||
options.mongoUrl = "mongodb://localhost/mongodb-asms"; | ||
} | ||
var db = null; | ||
if (!mongoose) { | ||
mongoose = require('mongoose'); | ||
db = mongoose.createConnection(options.mongoUrl); | ||
} else { | ||
db = mongoose.connection; | ||
} | ||
var Schema = mongoose.Schema, | ||
@@ -43,2 +14,11 @@ ObjectId = Schema.ObjectId, | ||
var LocationHash = { | ||
displayName: {type: String}, | ||
position: { | ||
latitude: Number, | ||
longitude: Number | ||
} | ||
}; | ||
var ActivityObjectHash = { | ||
@@ -58,7 +38,8 @@ id: {type: String}, | ||
downstreamDuplicates: [{type: String, default: null}], | ||
updated: {type: Date, default: null} | ||
updated: {type: Date, default: null}, | ||
location: LocationHash | ||
}; | ||
this.ActivityObject = db.model('activityObject', new Schema(ActivityObjectHash)); | ||
var defaultActor = {displayName: 'Someone', image: {url: options.defaultActorImage || ''}}; | ||
var defaultActor = {displayName: 'Someone', image: {url: defaultActorImage || ''}}; | ||
@@ -82,65 +63,6 @@ var ActivityHash = { | ||
}; | ||
this.Activity = db.model('activity', new Schema(ActivityHash)); | ||
this.getActivityStreamFirehose = function(n, fx) { | ||
Activity.find().sort('-published').limit(n).exec(fx); | ||
} | ||
this.getActivityStream = function(streamName, n, fx) { | ||
Activity.find({streams:streamName}).sort('-published').limit(n).exec(fx); | ||
} | ||
this.redisClient = null; | ||
this.redisPublisher = null; | ||
if (options.redis) { | ||
var redis = require("redis"); | ||
this.redisClient = redis.createClient(options.redis.port, options.redis.host); | ||
this.redisPublisher = redis.createClient(options.redis.port, options.redis.host); | ||
if(options.redis.pass) { | ||
redisClient.auth(options.redis.pass); | ||
redisPublisher.auth(options.redis.pass); | ||
} | ||
} | ||
this.publish = function(streamName, activity) { | ||
var publisher = this.redisPublisher; | ||
if (!_.isArray(activity.streams)) { | ||
activity.streams = [] | ||
} | ||
if (!_.include(activity.streams, streamName)) { | ||
activity.streams.push(streamName); | ||
} | ||
activity.save(function(err) { | ||
if (!err && streamName && publisher) { | ||
publisher.publish(streamName, JSON.stringify(activity)); | ||
} | ||
}); | ||
} | ||
this.subscribe = function(streamName, fx) { | ||
if (this.redisClient && streamName) { | ||
this.redisClient.subscribe(streamName); | ||
this.redisClient.on("message", fx); | ||
} | ||
} | ||
this.unsubscribe = function(streamName, fx) { | ||
if (this.redisClient && streamName) { | ||
this.redisClient.unsubscribe(streamName); | ||
} | ||
} | ||
this.close = function() { | ||
mongoose.disconnect(); | ||
if (this.redisClient && this.redisPublisher) { | ||
this.redisClient.quit(); | ||
this.redisPublisher.quit(); | ||
} | ||
} | ||
return this; | ||
}; |
@@ -5,3 +5,3 @@ { | ||
"description": "Activity Streams Engine using MongoDB(Mongoose odm) and Redis", | ||
"version": "0.0.13", | ||
"version": "0.0.14", | ||
"repository": { | ||
@@ -8,0 +8,0 @@ "type": "git", |
@@ -11,3 +11,3 @@ var mongoUrl = 'mongodb://localhost/test-activity-mongoose'; | ||
mongoose.connect(mongoUrl); | ||
asmsDB = require('../lib/activityMongoose')(mongoose, { | ||
asmsDB = require('../index')(mongoose, { | ||
full: true, | ||
@@ -25,3 +25,5 @@ redis: redisOptions | ||
tearDown : function(callback) { | ||
asmsDB.close(); | ||
if (asmsDB) { | ||
asmsDB.close(); | ||
} | ||
callback(); | ||
@@ -28,0 +30,0 @@ }, |
@@ -8,3 +8,3 @@ var mongoUrl = 'mongodb://localhost/test-activity-mongoose-2'; | ||
setUp : function (callback) { | ||
asmsDB = require('../lib/activityMongoose')({ | ||
asmsDB = require('../index')({ | ||
full: true, | ||
@@ -23,5 +23,13 @@ redis: redisOptions, | ||
tearDown : function(callback) { | ||
asmsDB.close(); | ||
if (asmsDB) { | ||
asmsDB.close(); | ||
} | ||
callback(); | ||
}, | ||
Basic: function(test) { | ||
var act = new asmsDB.Activity(); | ||
test.notEqual(act, null); | ||
test.done(); | ||
}, | ||
DefaultFullActivity: function(test) { | ||
@@ -28,0 +36,0 @@ var act = new asmsDB.Activity(); |
16075
9
332