Просмотр исходного кода

Create page when user created and activated

Sotaro KARASAWA 10 лет назад
Родитель
Сommit
0ddaa43987
5 измененных файлов с 69 добавлено и 0 удалено
  1. 18 0
      lib/crowi/index.js
  2. 34 0
      lib/events/user.js
  3. 4 0
      lib/models/page.js
  4. 12 0
      lib/models/user.js
  5. 1 0
      package.json

+ 18 - 0
lib/crowi/index.js

@@ -25,12 +25,15 @@ function Crowi (rootdir, env)
   this.pluginDir = path.join(this.rootDir, 'node_modules') + sep;
   this.publicDir = path.join(this.rootDir, 'public') + sep;
   this.libDir    = path.join(this.rootDir, 'lib') + sep;
+  this.eventsDir = path.join(this.libDir, 'events') + sep;
   this.viewsDir  = path.join(this.libDir, 'views') + sep;
   this.mailDir   = path.join(this.viewsDir, 'mail') + sep;
 
   this.config = {};
   this.mailer = {};
 
+  this.events = {};
+
   this.models = {};
 
   this.env = env;
@@ -51,6 +54,12 @@ Crowi.prototype.init = function() {
   .then(function() {
     // setup database server and load all modesl
     return self.setupDatabase();
+  }).then(function() {
+    // load events
+    self.events = {
+      user: new (require(self.eventsDir + 'user'))(self),
+    };
+    return Promise.resolve();
   }).then(function() {
     return self.setupModels();
   }).then(function() {
@@ -94,6 +103,15 @@ Crowi.prototype.model = function(name, model) {
   return this.models[name];
 };
 
+// getter/setter of event instance
+Crowi.prototype.event = function(name, event) {
+  if (event) {
+    return this.events[name] = event;
+  }
+
+  return this.events[name];
+};
+
 Crowi.prototype.setupDatabase = function() {
   // mongoUri = mongodb://user:password@host/dbname
   var mongoUri = this.env.MONGOLAB_URI ||

+ 34 - 0
lib/events/user.js

@@ -0,0 +1,34 @@
+var debug = require('debug')('crowi:events:user');
+var util = require('util');
+var events = require('events');
+var sprintf = require('sprintf');
+
+function UserEvent(crowi) {
+  this.crowi = crowi;
+
+  events.EventEmitter.call(this);
+}
+util.inherits(UserEvent, events.EventEmitter);
+
+UserEvent.prototype.onActivated = function(user) {
+  var User = this.crowi.model('User');
+  var Page = this.crowi.model('Page');
+
+  var userPagePath = Page.getUserPagePath(user);
+  Page.findPage(userPagePath, user, {}, false)
+  .then(function(page) {
+    // do nothing because user page is already exists.
+  }).catch(function(err) {
+    var body = sprintf('# %s\nThis is %s\'s page', user.username, user.username)
+    // create user page
+    Page.create(userPagePath, body, user, {})
+    .then(function(page) {
+      // page created
+      debug('User page created', page);
+    }).catch(function(err) {
+      debug('Failed to create user page', err);
+    });
+  });
+};
+
+module.exports = UserEvent;

+ 4 - 0
lib/models/page.js

@@ -242,6 +242,10 @@ module.exports = function(crowi) {
     return path;
   };
 
+  pageSchema.statics.getUserPagePath = function(user) {
+    return '/user/' + user.username;
+  };
+
   pageSchema.statics.isCreatableName = function(name) {
     var forbiddenPages = [
       /\^|\$|\*|\+|\#/,

+ 12 - 0
lib/models/user.js

@@ -15,6 +15,8 @@ module.exports = function(crowi) {
 
     , PAGE_ITEMS        = 20
 
+    , userEvent = crowi.event('user')
+
     , userSchema;
 
   userSchema = new mongoose.Schema({
@@ -34,6 +36,8 @@ module.exports = function(crowi) {
   });
   userSchema.plugin(mongoosePaginate);
 
+  userEvent.on('activated', userEvent.onActivated);
+
   function decideUserStatusOnRegistration () {
     var Config = crowi.model('Config'),
       config = crowi.getConfig();
@@ -161,6 +165,7 @@ module.exports = function(crowi) {
     this.username = username;
     this.status = STATUS_ACTIVE;
     this.save(function(err, userData) {
+      userEvent.emit('activated', userData);
       return callback(err, userData);
     });
   };
@@ -185,6 +190,7 @@ module.exports = function(crowi) {
     debug('Activate User', this);
     this.status = STATUS_ACTIVE;
     this.save(function(err, userData) {
+      userEvent.emit('activated', userData);
       return callback(err, userData);
     });
   };
@@ -535,6 +541,9 @@ module.exports = function(crowi) {
     newUser.status = decideUserStatusOnRegistration();
 
     newUser.save(function(err, userData) {
+      if (userData.status == STATUS_ACTIVE) {
+        userEvent.emit('activated', userData);
+      }
       return callback(err, userData);
     });
   };
@@ -552,6 +561,9 @@ module.exports = function(crowi) {
     newUser.status = decideUserStatusOnRegistration();
 
     newUser.save(function(err, userData) {
+      if (userData.status == STATUS_ACTIVE) {
+        userEvent.emit('activated', userData);
+      }
       return callback(err, userData);
     });
   };

+ 1 - 0
package.json

@@ -75,6 +75,7 @@
     "reveal.js": "~3.2.0",
     "socket.io": "~1.3.0",
     "socket.io-client": "~1.3.0",
+    "sprintf": "~0.1.5",
     "swig": "~1.4.0",
     "time": "~0.11.0",
     "vinyl-source-stream": "~1.1.0"