2016-04-20 10:03:55 +00:00
|
|
|
"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
|
2016-05-11 21:04:45 +00:00
|
|
|
},
|
|
|
|
accessToken: {
|
|
|
|
type: DataTypes.STRING
|
2016-05-15 04:20:42 +00:00
|
|
|
},
|
|
|
|
refreshToken: {
|
|
|
|
type: DataTypes.STRING
|
2016-04-20 10:03:55 +00:00
|
|
|
}
|
|
|
|
}, {
|
|
|
|
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":
|
2016-05-21 14:48:21 +00:00
|
|
|
photo = 'https://graph.facebook.com/' + profile.id + '/picture?width=96';
|
2016-04-20 10:03:55 +00:00
|
|
|
break;
|
|
|
|
case "twitter":
|
2016-05-19 18:13:22 +00:00
|
|
|
photo = 'https://twitter.com/' + profile.username + '/profile_image?size=bigger';
|
2016-04-20 10:03:55 +00:00
|
|
|
break;
|
|
|
|
case "github":
|
2016-05-19 18:13:22 +00:00
|
|
|
photo = 'https://avatars.githubusercontent.com/u/' + profile.id + '?s=96';
|
2016-04-20 10:03:55 +00:00
|
|
|
break;
|
2016-05-12 17:26:50 +00:00
|
|
|
case "gitlab":
|
2016-05-21 14:48:21 +00:00
|
|
|
photo = profile.avatarUrl.replace(/(\?s=)\d*$/i, '$196');
|
2016-05-12 17:26:50 +00:00
|
|
|
break;
|
2016-04-20 10:03:55 +00:00
|
|
|
case "dropbox":
|
|
|
|
//no image api provided, use gravatar
|
2016-05-21 14:48:21 +00:00
|
|
|
photo = 'https://www.gravatar.com/avatar/' + md5(profile.emails[0].value) + '?s=96';
|
2016-04-20 10:03:55 +00:00
|
|
|
break;
|
2016-05-21 14:48:00 +00:00
|
|
|
case "google":
|
|
|
|
photo = profile.photos[0].value.replace(/(\?sz=)\d*$/i, '$196');
|
|
|
|
break;
|
2016-04-20 10:03:55 +00:00
|
|
|
}
|
|
|
|
return photo;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
2016-05-11 21:04:45 +00:00
|
|
|
|
2016-04-20 10:03:55 +00:00
|
|
|
return User;
|
|
|
|
};
|