From 4e1091fb83ff363998918245d7445903e9fcaabc Mon Sep 17 00:00:00 2001 From: jysperm Date: Mon, 20 Oct 2014 11:22:42 +0800 Subject: [PATCH] model enum pluggable --- app.coffee | 42 +++++++++++++++++++++------------- core/db.coffee | 3 +++ core/model/account.coffee | 11 +++++---- core/model/balance_log.coffee | 7 ++++-- core/model/coupon_code.coffee | 7 ++++-- core/model/notification.coffee | 11 +++++---- core/model/security_log.coffee | 7 ++++-- core/model/ticket.coffee | 9 ++++---- core/pluggable.coffee | 29 ++++++++++++++++++++--- package.json | 3 ++- 10 files changed, 89 insertions(+), 40 deletions(-) diff --git a/app.coffee b/app.coffee index cf37418..d90132a 100755 --- a/app.coffee +++ b/app.coffee @@ -7,6 +7,7 @@ app.libs = async: require 'async' bodyParser: require 'body-parser' cookieParser: require 'cookie-parser' + copy: require 'copy-to' crypto: require 'crypto' depd: require 'depd' express: require 'express' @@ -23,7 +24,7 @@ app.libs = ObjectId: (require 'mongoose').Schema.Types.ObjectId -{cookieParser, crypto, bodyParser, depd, express, fs, harp, middlewareInjector, mongoose} = exports.libs +{cookieParser, copy, crypto, bodyParser, depd, express, fs, harp, middlewareInjector, mongoose} = exports.libs {morgan, nodemailer, path, redis, redisStore, expressSession} = exports.libs RedisStore = redisStore expressSession @@ -66,14 +67,35 @@ do -> app.config = config app.db = require './core/db' +app.pluggable = require './core/pluggable' + +app.schemas = {} + +app.models = + Account: {} + BalanceLog: {} + CouponCode: {} + Notification: {} + SecurityLog: {} + Ticket: {} + +app.pluggable.initializePlugins() + +require './core/model/account' +require './core/model/balance_log' +require './core/model/coupon_code' +require './core/model/notification' +require './core/model/security_log' +require './core/model/ticket' + +for name, schema of app.schemas + copy(mongoose.model(name, schema)).to app.models[name] + app.templates = require './core/templates' app.i18n = require './core/i18n' app.utils = require './core/utils' app.cache = require './core/cache' -app.config = require './config' -app.package = require './package.json' app.billing = require './core/billing' -app.pluggable = require './core/pluggable' app.middleware = require './core/middleware' app.notification = require './core/notification' app.authenticator = require './core/authenticator' @@ -84,16 +106,6 @@ app.express = express() app.redis = redis.createClient 6379, '127.0.0.1', auth_pass: config.redis.password -app.schemas = {} - -app.models = - Account: require './core/model/account' - BalanceLog: require './core/model/balance_log' - CouponCode: require './core/model/coupon_code' - Notification: require './core/model/notification' - SecurityLog: require './core/model/security_log' - Ticket: require './core/model/ticket' - app.express.use bodyParser.json() app.express.use morgan 'dev' app.express.use cookieParser() @@ -164,8 +176,6 @@ app.express.use '/ticket', require './core/router/ticket' app.express.use '/admin', require './core/router/admin' app.express.use '/panel', require './core/router/panel' -app.pluggable.initializePlugins() - app.express.get '/', (req, res) -> unless res.headerSent res.redirect '/panel/' diff --git a/core/db.coffee b/core/db.coffee index b3328eb..1cdda6d 100644 --- a/core/db.coffee +++ b/core/db.coffee @@ -11,3 +11,6 @@ else module.exports = mongoose.createConnection mongodb_uri exports.mongodb_uri = mongodb_uri + +mongoose.connection.on 'error', (err) -> + console.error err if err diff --git a/core/model/account.coffee b/core/model/account.coffee index 9bd1155..5ad3e86 100644 --- a/core/model/account.coffee +++ b/core/model/account.coffee @@ -1,13 +1,12 @@ -async = require 'async' -_ = require 'underscore' - +{pluggable} = app +{selectModelEnum} = pluggable {_, async, mongoose} = app.libs Token = mongoose.Schema type: required: true type: String - enum: ['full_access'] + enum: ['full_access'].concat selectModelEnum 'Token', 'type' token: required: true @@ -81,7 +80,9 @@ Account = mongoose.Schema type: Object default: {} -module.exports = mongoose.model 'Account', Account +_.extend app.schemas, + Account: Account + Token: Token # @param account: username, email, password # @param callback(account) diff --git a/core/model/balance_log.coffee b/core/model/balance_log.coffee index 24f8cca..284daef 100644 --- a/core/model/balance_log.coffee +++ b/core/model/balance_log.coffee @@ -1,3 +1,5 @@ +{pluggable} = app +{selectModelEnum} = pluggable {_, ObjectId, mongoose} = app.libs BalanceLog = mongoose.Schema @@ -9,7 +11,7 @@ BalanceLog = mongoose.Schema type: required: true type: String - enum: ['deposit'] + enum: ['deposit'].concat selectModelEnum 'BalanceLog', 'type' amount: required: true @@ -23,4 +25,5 @@ BalanceLog = mongoose.Schema type: Object default: {} -module.exports = mongoose.model 'BalanceLog', BalanceLog +_.extend app.schemas, + BalanceLog: BalanceLog diff --git a/core/model/coupon_code.coffee b/core/model/coupon_code.coffee index f9f4d79..2ded09c 100644 --- a/core/model/coupon_code.coffee +++ b/core/model/coupon_code.coffee @@ -1,3 +1,5 @@ +{pluggable} = app +{selectModelEnum} = pluggable {_, ObjectId, mongoose} = app.libs CouponCode = mongoose.Schema @@ -16,7 +18,7 @@ CouponCode = mongoose.Schema type: required: true type: String - enum: ['amount'] + enum: ['amount'].concat selectModelEnum 'CouponCode', 'type' meta: type: Object @@ -33,7 +35,8 @@ CouponCode = mongoose.Schema default: Date.now ] -module.exports = mongoose.model 'CouponCode', CouponCode +_.extend app.schemas, + CouponCode: CouponCode exports.type_meta = amount: diff --git a/core/model/notification.coffee b/core/model/notification.coffee index af282d6..dc7d598 100644 --- a/core/model/notification.coffee +++ b/core/model/notification.coffee @@ -1,7 +1,7 @@ +{pluggable} = app +{selectModelEnum} = pluggable {_, ObjectId, mongoose} = app.libs -module.exports = exports = app.db.collection 'notifications' - Notification = mongoose.Schema account_id: type: ObjectId @@ -15,12 +15,12 @@ Notification = mongoose.Schema type: required: true type: String - enum: ['payment_success'] + enum: ['payment_success'].concat selectModelEnum 'Notification', 'type' level: required: true type: String - enum: ['notice', 'event', 'log'] + enum: ['notice', 'event', 'log'].concat selectModelEnum 'Notification', 'type' created_at: type: Date @@ -30,7 +30,8 @@ Notification = mongoose.Schema type: Object default: {} -module.exports = mongoose.model 'Notification', Notification +_.extend app.schemas, + Notification: Notification exports.createNotice = (account, group_name, type, level, meta, callback) -> exports.insert diff --git a/core/model/security_log.coffee b/core/model/security_log.coffee index 8e01647..e078f11 100644 --- a/core/model/security_log.coffee +++ b/core/model/security_log.coffee @@ -1,3 +1,5 @@ +{pluggable} = app +{selectModelEnum} = pluggable {_, ObjectId, mongoose} = app.libs SecurityLog = mongoose.Schema @@ -9,7 +11,7 @@ SecurityLog = mongoose.Schema type: required: true type: String - enum: ['update_password', 'update_setting', 'update_email'] + enum: ['update_password', 'update_setting', 'update_email'].concat selectModelEnum 'SecurityLog', 'type' created_at: type: Date @@ -24,7 +26,8 @@ SecurityLog = mongoose.Schema type: ObjectId ref: 'Token' -module.exports = mongoose.model 'SecurityLog', SecurityLog +_.extend app.schemas, + SecurityLog: SecurityLog exports.create = (account, type, token, payload, callback) -> matched_token = _.findWhere account.tokens, diff --git a/core/model/ticket.coffee b/core/model/ticket.coffee index 9e7f166..f48b898 100644 --- a/core/model/ticket.coffee +++ b/core/model/ticket.coffee @@ -1,7 +1,7 @@ +{pluggable} = app +{selectModelEnum} = pluggable {_, ObjectId, mongoose} = app.libs -module.exports = exports = app.db.collection 'tickets' - Reply = mongoose.Schema _id: type: ObjectId @@ -50,7 +50,7 @@ Ticket = mongoose.Schema status: type: String - enum: ['open', 'pending', 'finish', 'closed'] + enum: ['open', 'pending', 'finish', 'closed'].concat selectModelEnum 'Ticket', 'type' option: type: Object @@ -64,7 +64,8 @@ Ticket = mongoose.Schema Reply ] -module.exports = mongoose.model 'Ticket', Ticket +_.extend app.schemas, + Ticket: Ticket exports.createTicket = (account, title, content, members, status, options, callback) -> exports.insert diff --git a/core/pluggable.coffee b/core/pluggable.coffee index c8e78c3..3ddee53 100644 --- a/core/pluggable.coffee +++ b/core/pluggable.coffee @@ -14,13 +14,27 @@ hookHelper = (options) -> return _.extend [], options exports.hooks = + app: + # action: function(callback) + started: hookHelper + global_event: true + + model: + # model: string, field: string, type: string + type_enum: hookHelper + global_event: true + + # model: string, action(schema, callback) + middleware: hookHelper + global_event: true + account: # filter: function(username, callback(is_allow)) username_filter: hookHelper - always_notice: true + global_event: true # filter: function(account, callback) before_register: hookHelper - always_notice: true + global_event: true # action: function(account, callback) resources_limit_changed: [] @@ -105,7 +119,7 @@ exports.selectHook = (account, hook_name) -> return _.filter pointer, (hook) -> if hook.plugin_info.type == 'extension' return true - else if pointer.always_notice or hook.always_notice + else if pointer.global_event or hook.always_notice return true else if !account return false @@ -114,6 +128,15 @@ exports.selectHook = (account, hook_name) -> else return false +exports.selectModelEnum = (model, field) -> + result = [] + + for hook in exports.selectHook null, 'model.type_enum' + if hook.model == model and hook.field == field + result.push hook.type + + return result + exports.initializePlugins = (callback) -> checkDependencies = -> all_plugins = _.union config.plugin.available_extensions, config.plugin.available_services diff --git a/package.json b/package.json index e41c46f..39ccbf7 100644 --- a/package.json +++ b/package.json @@ -53,7 +53,8 @@ "morgan": "^1.3.2", "mongoose": "^3.8.17", "depd": "^1.0.0", - "body-parser": "^1.9.0" + "body-parser": "^1.9.0", + "copy-to": "^1.0.1" }, "devDependencies": { "mocha": "^1.21.5",