diff options
author | Cheng-Han, Wu | 2016-04-20 18:03:55 +0800 |
---|---|---|
committer | Cheng-Han, Wu | 2016-04-20 18:03:55 +0800 |
commit | 49b51e478fa75b8d5254662de3265edcf8906004 (patch) | |
tree | 3b09213baae129156339b5ad496924f591790e88 /lib/models/user.js | |
parent | e613aeba75aec5ceb4f10ae62881a3635183857d (diff) |
Refactor server with Sequelize ORM, refactor server configs, now will show note status (created or updated) and support docs (note alias)
Diffstat (limited to 'lib/models/user.js')
-rw-r--r-- | lib/models/user.js | 77 |
1 files changed, 77 insertions, 0 deletions
diff --git a/lib/models/user.js b/lib/models/user.js new file mode 100644 index 00000000..e1a373d6 --- /dev/null +++ b/lib/models/user.js @@ -0,0 +1,77 @@ +"use strict"; + +// external modules +var md5 = require("blueimp-md5"); +var Sequelize = require("sequelize"); + +// core +var logger = require("../logger.js"); + +module.exports = function (sequelize, DataTypes) { + var User = sequelize.define("User", { + id: { + type: DataTypes.UUID, + primaryKey: true, + defaultValue: Sequelize.UUIDV4 + }, + profileid: { + type: DataTypes.STRING, + unique: true + }, + profile: { + type: DataTypes.TEXT + }, + history: { + type: DataTypes.TEXT + } + }, { + classMethods: { + associate: function (models) { + User.hasMany(models.Note, { + foreignKey: "ownerId", + constraints: false + }); + User.hasMany(models.Note, { + foreignKey: "lastchangeuserId", + constraints: false + }); + }, + parseProfile: function (profile) { + try { + var profile = JSON.parse(profile); + } catch (err) { + logger.error(err); + profile = null; + } + if (profile) { + profile = { + name: profile.displayName || profile.username, + photo: User.parsePhotoByProfile(profile) + } + } + return profile; + }, + parsePhotoByProfile: function (profile) { + var photo = null; + switch (profile.provider) { + case "facebook": + photo = 'https://graph.facebook.com/' + profile.id + '/picture'; + break; + case "twitter": + photo = profile.photos[0].value; + break; + case "github": + photo = 'https://avatars.githubusercontent.com/u/' + profile.id + '?s=48'; + break; + case "dropbox": + //no image api provided, use gravatar + photo = 'https://www.gravatar.com/avatar/' + md5(profile.emails[0].value); + break; + } + return photo; + } + } + }); + + return User; +};
\ No newline at end of file |