Пример #1
0
posts.undelete = function(postId) {
  var postKey = Post.key(postId);
  var deletedPost = null;

  // see if post already exists
  return db.content.getAsync(postKey)
  .then(function(postData) {
    deletedPost = postData;
    var timestamp = Date.now();

    // add deleted: true flag to board
    delete deletedPost.deleted;
    deletedPost.updated_at = timestamp;
    deletedPost.version = timestamp;

    // insert back into db
    return db.content.putAsync(postKey, deletedPost);
  })
  .then(function() {
    // version already updated above
    var versionKey = Post.versionKey(deletedPost.id, deletedPost.version);
    return db.content.putAsync(versionKey, deletedPost);
  })
  .then(function() { return deletedPost; });
};
Пример #2
0
boards.find = function(id) {
  var addProperty = function(key, keyName) {
    return db.metadata.getAsync(key)
    .then(function(value) {
      if (_.isNumber(board[keyName]) && _.isNumber(value)) {
        board[keyName] += Number(value);
      }
      else if (_.isNumber(value)) {
        board[keyName] = Number(value);
      }
      else if (_.isString(value)) {
        board[keyName] = value;
      }
    });
  };

  var board;
  var boardKey = Board.key(id);
  var postCountKey = Board.postCountKey(id);
  var threadCountKey = Board.threadCountKey(id);
  var totalPostCountKey = Board.totalPostCountKey(id);
  var totalThreadCountKey = Board.totalThreadCountKey(id);
  var lastPostUsernameKey = Board.lastPostUsernameKey(id);
  var lastPostCreatedAtKey = Board.lastPostCreatedAtKey(id);
  var lastThreadTitleKey = Board.lastThreadTitleKey(id);
  var lastThreadIdKey = Board.lastThreadIdKey(id);

  return db.content.getAsync(boardKey)
  .then(function(boardDb) {
    board = boardDb;
    board.post_count = 0;
    board.thread_count = 0;
    if (board.children_ids && board.children_ids.length > 0) {
      board.children = [];
      return Promise.all(board.children_ids.map(function(childId) {
        return boards.find(childId)
        .then(function(childBoard) {
          board.children.push(childBoard);
        });
      }));
    }
  })
  .then(function() {
    return Promise.join(
      addProperty(postCountKey, 'post_count'),
      addProperty(threadCountKey, 'thread_count'),
      addProperty(totalPostCountKey, 'total_post_count'),
      addProperty(totalThreadCountKey, 'total_thread_count'),
      addProperty(lastPostUsernameKey, 'last_post_username'),
      addProperty(lastPostCreatedAtKey, 'last_post_created_at'),
      addProperty(lastThreadTitleKey, 'last_thread_title'),
      addProperty(lastThreadIdKey, 'last_thread_id'),
      function() { return board; });
  });
};
Пример #3
0
          Model.prototype[key] = function() {
            var query = new Query(this);
            if ((arguments.length === 1)
              && (typeof arguments[0] === 'string')
              && (this._getModel()._indexes[arguments[0]] === true)) {

                query = query[key]({index: arguments[0]});
                return query;
            }
            else {
              query = query[key].apply(query, arguments);
              return query;
            }
          }
Пример #4
0
 updateParentLock.runwithlock(function () {
   var parentBoardKey = Board.key(parentId);
   return db.content.getAsync(parentBoardKey)
   .then(function(dbParentBoard) {
     parentBoard = dbParentBoard;
     parentBoard.children_ids = dbParentBoard.children_ids || [];
     if (!_.contains(parentBoard.children_ids, childId)) {
       parentBoard.children_ids.push(childId);
       return db.content.putAsync(parentBoardKey, parentBoard);
     }
     // parent board already has child board id in children_ids
     return;
   })
   .then(function() { fulfill(parentBoard); })
   .catch(function(err) { reject(err); })
   .finally(function() { updateParentLock.release(); });
 });
Пример #5
0
boards.undelete = function(boardId) {
  var boardKey = Board.key(boardId);
  var deleteBoard;

  // see if board already exists
  return db.content.getAsync(boardKey)
  .then(function(boardData) {
    deleteBoard = boardData;

    // add deleted: true flag to board
    delete deleteBoard.deleted;
    deleteBoard.updated_at = Date.now();

    // insert back into db
    return db.content.putAsync(boardKey, deleteBoard);
  })
  .then(function() { return deleteBoard; });
};
Пример #6
0
boards.update = function(board) {
  var boardKey = Board.key(board.id);
  var updateBoard = null;

  // get old board from db
  return db.content.getAsync(boardKey)
  .then(function(oldBoard) {
    updateBoard = oldBoard;

    // update board values
    if (board.name) { updateBoard.name = board.name; }

    if (board.description) { updateBoard.description = board.description; }
    else if (board.description === null) { delete updateBoard.description; }
    else if (board.description && board.description === "") {
      delete updateBoard.description;
    }

    if (board.category_id) { updateBoard.category_id = board.category_id; }
    else if (board.category_id === null) { delete updateBoard.category_id; }
    else if (board.category_id && board.category_id === "") {
      delete updateBoard.category_id;
    }

    if (board.parent_id) { updateBoard.parent_id = board.parent_id; }
    else if (board.parent_id === null) { delete updateBoard.parent_id; }
    else if (board.parent_id && board.parent_id === "") {
      delete updateBoard.parent_id;
    }

    if (board.children_ids) { updateBoard.children_ids = board.children_ids; }
    else if (board.children_ids === null) { delete updateBoard.children_ids; }
    else if (board.children_ids && board.children_ids.length === 0) {
      delete updateBoard.children_ids;
    }

    updateBoard.updated_at = Date.now();

    // insert back into db
    return db.content.putAsync(boardKey, updateBoard)
    .then(function() { return updateBoard; });
  });
};
Пример #7
0
 updateParentLock.runwithlock(function () {
   var parentBoardKey = Board.key(parentId);
   return db.content.getAsync(parentBoardKey)
   .then(function(dbParentBoard) {
     parentBoard = dbParentBoard;
     if (_.contains(parentBoard.children_ids, childId)) {
       parentBoard.children_ids = _.pull(parentBoard.children_ids, childId);
       if (parentBoard.children_ids.length === 0) {
         delete parentBoard.children_ids;
       }
       return db.content.putAsync(parentBoardKey, parentBoard);
     }
     // parent board doesn't have child board id in children_ids
     return;
   })
   .then(function() { fulfill(parentBoard); })
   .catch(function(err) { reject(err); })
   .finally(function() { updateParentLock.release(); });
 });
Пример #8
0
boards.create = function(board) {
  // insert into db
  var timestamp = Date.now();
  if (!board.created_at) {
    board.created_at = timestamp;
    board.updated_at = timestamp;
  }
  else if (!board.updated_at) {
    board.updated_at = board.created_at;
  }
  board.id = helper.genId(board.created_at);
  var boardKey = Board.key(board.id);
  var boardLastPostUsernameKey = Board.lastPostUsernameKey(board.id);
  var boardLastPostCreatedAtKey = Board.lastPostCreatedAtKey(board.id);
  var boardLastThreadTitleKey = Board.lastThreadTitleKey(board.id);
  var boardLastThreadIdKey = Board.lastThreadIdKey(board.id);
  var totalPostCountKey = Board.totalPostCountKey(board.id);
  var totalThreadCountKey = Board.totalThreadCountKey(board.id);
  var postCountKey = Board.postCountKey(board.id);
  var threadCountKey = Board.threadCountKey(board.id);

  var metadataBatch = [
    // TODO: There should be a better solution than initializing with strings
    { type: 'put', key: boardLastPostUsernameKey , value: 'none' },
    { type: 'put', key: boardLastPostCreatedAtKey , value: 0 },
    { type: 'put', key: boardLastThreadTitleKey , value: 'none' },
    { type: 'put', key: boardLastThreadIdKey , value: 'none' },
    { type: 'put', key: totalPostCountKey , value: 0 },
    { type: 'put', key: totalThreadCountKey , value: 0 },
    { type: 'put', key: postCountKey , value: 0 },
    { type: 'put', key: threadCountKey , value: 0 }
  ];
  return db.metadata.batchAsync(metadataBatch)
  .then(function() {
    if (board.parent_id) {
      return addChildToBoard(board.id, board.parent_id);
    }
    else { return; }
  })
  .then(function() { return db.content.putAsync(boardKey, board); })
  .then(function() { return board; });
};
Пример #9
0
posts.update = function(post) {
  var postKey = Post.key(post.id);
  var updatePost = null;
  var threadFirstPostIdKey = Thread.firstPostIdKey(post.thread_id);

  // check if first post in thread
  return db.metadata.getAsync(threadFirstPostIdKey)
  .then(function(firstPostId) {
    if (firstPostId === post.id) {
      return db.metadata.putAsync(threadFirstPostIdKey, post.title)
      .then(function() { return; });
    }
    else { return; }
  })
  .then(function() {
    return db.content.getAsync(postKey); // get old post
  })
  .then(function(oldPost) {
    updatePost = oldPost;
    var timestamp = Date.now();

    // update post values
    if (post.title) { updatePost.title = post.title; }
    if (post.body) { updatePost.body = post.body; }
    if (post.encodedBody) { updatePost.encodedBody = post.encodedBody; }
    else if (post.encodedBody === null) { delete updatePost.encodedBody; }
    updatePost.updated_at = timestamp;
    updatePost.version = timestamp;

    // insert back into db
    return db.content.putAsync(postKey, updatePost);
  })
  .then(function() {
    // version already updated above
    var versionKey = Post.versionKey(updatePost.id, updatePost.version);
    return db.content.putAsync(versionKey, updatePost);
  })
  .then(function() { return updatePost; })
  .catch(function(err) { console.log(err); });
};
Пример #10
0
boards.delete = function(boardId) {
  var boardKey = Board.key(boardId);
  var deleteBoard;

  // see if board already exists
  return db.content.getAsync(boardKey)
  .then(function(boardData) {
    deleteBoard = boardData;
    if (deleteBoard.children_ids && deleteBoard.children_ids.length > 0) {
      throw new Error('Cannot delete parent board with child boards.');
    }
    else {
      // add deleted: true flag to board
      deleteBoard.deleted = true;
      deleteBoard.updated_at = Date.now();

      // insert back into db
      return db.content.putAsync(boardKey, deleteBoard);
    }
  })
  .then(function() { return deleteBoard; });
};
Пример #11
0
posts.purge = function(id) {
  var postKey = Post.key(id);
  var deletedPost;
  var threadId;

  return db.content.getAsync(postKey) // get post
  .then(function(post) {
    deletedPost = post;
    return id;
  })
  .then(posts.versions)
  .then(function(versions) { // move versions to deleted db
    var batchArray = versions.map(function(version) {
      var versionKey = Post.versionKey(version.id, version.version);
      return { type: 'put', key: versionKey, value: version };
    });
    return db.deleted.batchAsync(batchArray)
    .then(function() {
      batchArray = batchArray.map(function(item) {
        item.type = 'del';
        delete item.value;
        return item;
      });
      return db.content.batchAsync(batchArray);
    });
  })
  .then(function() { // remove from this db
    return db.content.delAsync(postKey);
  })
  .then(function() { // decrement threadPostCount
    return threadsDb.decPostCount(deletedPost.thread_id);
  })
  .then(function() { // decrement boardPostCount
    return threadsDb.find(deletedPost.thread_id)
    .then(function(thread) {
      threadId = thread.id;
      return boardsDb.decPostCount(thread.board_id);
    });
  })
  .then(function() { // delete ThreadPostOrder and PostOrder
    var postOrderKey = Post.postOrderKey(id);
    return db.metadata.getAsync(postOrderKey)
    .then(function(postOrder) {
      return db.metadata.delAsync(postOrderKey)
      .then(function() { return postOrder; });
    })
    .then(function(postOrder) {
      var order = Number(postOrder);
      return reorderPostOrder(threadId, order);
    });
  })
  // temporary solution to handling ThreadFirstPostIdKey (first post)
  .then(function() { // manage ThreadFirstPostIdKey
    var threadFirstPostIdKey = Thread.firstPostIdKey((deletedPost.thread_id));
    return db.metadata.getAsync(threadFirstPostIdKey)
    .then(function(postId) {
      if (postId === deletedPost.id) {
        // remove key
        return db.metadata.delAsync(threadFirstPostIdKey);
      }
      else { return; }
    });
  })
  // temporarily not handling lastPostCreatedAtKey (last post)
  // temporarily not hanlding lastPostUsernameKey
  // temporarily not handling threadTitle (first post)
  // temporarily not handling lastThreadId
  // temporarily not handling thread username (first post)
  .then(function() { // delete legacy key
    if (deletedPost.smf) {
      var legacyKey = Post.legacyKey(deletedPost.smf.ID_MSG);
      return db.legacy.delAsync(legacyKey);
    }
    else { return; }
  })
  .then(function() { return deletedPost; });
};
Пример #12
0
posts.find = function(id) {
  var postKey = Post.key(id);
  return db.content.getAsync(postKey);
};
Пример #13
0
 .then(function() { return db.content.putAsync(Post.key(post.id), post); })
Пример #14
0
 it('should return the thread\'s key', function() {
   var key = Thread.key(thread.id);
   var check = threadPrefix + sep + thread.id;
   key.should.be.equal(check);
 });
Пример #15
0
boards.purge = function(id) {
  var purgeBoard;
  var boardKey = Board.key(id);

  // see if board already exists
  return db.content.getAsync(boardKey)
  // set board to function scope
  .then(function(boardData) {
    purgeBoard = boardData;
    if (purgeBoard.children_ids && purgeBoard.children_ids.length > 0) {
      throw new Error('Cannot purge parent board with child boards.');
    }
  })
  // delete id from parent board if necessary
  .then(function() {
    if (purgeBoard.parent_id) {
      return removeChildFromBoard(purgeBoard.id, purgeBoard.parent_id);
    }
    else { return; }
  })
  // delete metadata
  .then(function() {
    var postCountKey = Board.postCountKey(id);
    var threadCountKey = Board.threadCountKey(id);
    var totalPostCountKey = Board.totalPostCountKey(id);
    var totalThreadCountKey = Board.totalThreadCountKey(id);
    var lastPostUsernameKey = Board.lastPostUsernameKey(id);
    var lastPostCreatedAtKey = Board.lastPostCreatedAtKey(id);
    var lastThreadTitleKey = Board.lastThreadTitleKey(id);
    var lastThreadIdKey = Board.lastThreadIdKey(id);
    var deleteBatch = [
      { type: 'del', key: postCountKey },
      { type: 'del', key: threadCountKey },
      { type: 'del', key: totalPostCountKey },
      { type: 'del', key: totalThreadCountKey },
      { type: 'del', key: lastPostUsernameKey },
      { type: 'del', key: lastPostCreatedAtKey },
      { type: 'del', key: lastThreadTitleKey },
      { type: 'del', key: lastThreadIdKey }
    ];
    return db.metadata.batchAsync(deleteBatch);
  })
  // delete legacy key index
  .then(function() {
    if (purgeBoard.smf) {
      var legacyKey = Board.legacyKey(purgeBoard.smf.ID_BOARD);
      return db.legacy.delAsync(legacyKey);
    }
    return;
  })
  // delete Board from category and remove boards category id
  .then(function() {
    if (purgeBoard.category_id) {
      return boards.categoryDeleteBoard(purgeBoard)
      .then(function() { delete purgeBoard.category_id; });
    }
    return;
  })
  // move board to deleted db
  .then(function() {
    return db.deleted.putAsync(boardKey, purgeBoard);
  })
  // remove board from content
  .then(function() {
    return db.content.delAsync(boardKey);
  })
  // return this board
  .then(function() { return purgeBoard; });
};
Пример #16
0
Database.prototype._key = function(sublevel, key) {
  return docUtils.key(this.sep, sublevel, key)
}
Пример #17
0
	fs.readdirSync(__dirname + '/../controllers').forEach(function(name) {
		verbose && logger.info('\n %s:', name);
		var obj = require('./../controllers/' + name),
			name = obj.name || name,
			prefix = obj.prefix || '',
			app = express(),
			method,
			path;

		// allow specifying the view engine
		if (obj.engine) {
			app.set('view engine', obj.engine);
		}
		app.set('views', __dirname + '/../views/' + name);

		// error handlers

		// development error handler
		// will print stacktrace
		if (app.get('env') === 'development') {
			app.use(function(err, req, res, next) {
				res.status(err.status || 500);
				console.log(__dirname + '/../views/');
				res.render('error', {
					message: err.message,
					error: err
				});
			});
		}

		// production error handler
		// no stacktraces leaked to user
		/*app.use(function(err, req, res, next) {
			res.status(err.status || 500);
			res.render('error', {
				message: err.message,
				error: {}
			});
		});*/

		// before middleware support
		if (obj.before) {
			path = '/' + name + '/:' + name + '_id';
			app.all(path, obj.before);
			verbose && console.log(' ALL %s -> before', path);
			path = '/' + name + '/:' + name + '_id/*';
			app.all(path, obj.before);
			verbose && console.log(' ALL %s -> before', path);
		}

		// generate routes based
		// on the exported methods
		for (var key in obj) {
			// "reserved" exports
			if (~['name', 'prefix', 'engine', 'before'].indexOf(key)) {
				continue;
			}
			// route exports
			switch (key) {
				case 'index':
					method = 'get';
					path = '/';
					break;
				case 'show':
					method = 'get';
					path = '/' + name + '/:' + '_specieId';
					break;
				case 'terms':
					method = 'get';
					path = '/terms';
					break;
				default:
					throw new Error('unrecognized route: ' + name + '.' + key);
			}
			path = prefix + path;
			app[method](path, obj[key](services));
			verbose && console.log(' %s %s -> %s', method.toUpperCase(), path, key);
		}
		// mount the app
		parent.use(app);
	});
Пример #18
0
 securePromise.then(()=>{
     return controller[key](req);
 }).then((results)=> {