-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathplugin.js
More file actions
135 lines (124 loc) · 3.32 KB
/
plugin.js
File metadata and controls
135 lines (124 loc) · 3.32 KB
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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
/**
* User plugin main file
*/
module.exports = function loadUserPlugin(projectPath, Plugin) {
const plugin = new Plugin(__dirname);
plugin.setConfigs({
/**
* Field privacity settings
* @type {Object}
*/
privacity: {
userFields: {
public: ['displayName', 'avatar', 'banner'],
changeable: [
'fullName', 'biography', 'gender', 'language', 'organization'
]
}
},
user: {
disableContext: false,
goToOptions: {
view(req, res, next) {
if (!req.isAuthenticated()) return next();
res.goTo('/user/'+req.user.id);
},
edit(req, res, next) {
if (!req.isAuthenticated()) return next();
res.goTo('/user/'+req.user.id+'/edit');
},
privacity(req, res, next) {
if (!req.isAuthenticated()) return next();
res.goTo('/user/'+req.user.id+'/edit/privacity');
}
},
/**
* getPreloadQueryOptions
*
* @param {Object} req
* @param {Object} res
* @return {Object}
*/
getPreloadQueryOptions() {
return {};
}
}
});
plugin.setRoutes({
'get /user-goto': {
'controller' : 'user',
'action' : 'goTo'
},
'post /block-user/:id': {
'controller' : 'user',
'action' : 'blockUser',
'permission' : 'blockUser'
},
'get /user/:userId([0-9]+)/edit/privacity': {
'name' : 'user.privacity',
'controller' : 'user',
'model' : 'userPrivacity',
'action' : 'findUserPrivacity',
'titleHandler' : 'i18n',
'titleI18n' : 'Privacity',
'layoutName' : 'user-layout',
'template' : 'user/findUserPrivacity'
},
'post /user/:userId([0-9]+)/edit/privacity': {
'name' : 'user.privacity',
'controller' : 'user',
'model' : 'userPrivacity',
'action' : 'findUserPrivacity',
'titleHandler' : 'i18n',
'titleI18n' : 'Privacity',
'layoutName' : 'user-layout',
'template' : 'user/findUserPrivacity'
},
});
plugin.setResource({
'name': 'user',
'findOne': {
'metatagHandler': 'userFindOne'
},
'findAll': {
'search': {
'q': {
'parser': 'userSearchQuery',
'target': {
'type': 'field',
'field': 'displayName'
}
},
'username': {
'parser': 'equal',
'target': {
'type': 'field',
'field': 'username'
}
}
}
}
});
plugin.events.on('we:express:set:params', function(data) {
const we = plugin.we;
// user pre-loader
data.express.param('userId', function (req, res, next, id) {
if (!/^\d+$/.exec(String(id))) return res.notFound();
data.we.db.models.user
.findById(id, we.config.user.getPreloadQueryOptions())
.then( (user)=> {
if (user) {
res.locals.user = user;
// set user context if userId is the first param
if (we.config.user.disableContext && Object.keys(req.params)[0] == 'userId'){
res.locals.widgetContext = 'user-' + id;
}
}
next();
return null;
})
.catch(next);
});
});
return plugin;
};