summaryrefslogtreecommitdiff
path: root/lib/models/user.js
blob: e1a373d6b383535437417bb5e1e646971c7b2e9e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
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;
};