|
1 | 1 | var fs = require('fs'); |
2 | 2 | var formidable = require('formidable'); |
3 | 3 | var async = require('async'); |
4 | | -var scriptStorage = require('./scriptStorage'); |
5 | | -var User = require('../models/user').User; |
6 | | -var Script = require('../models/script').Script; |
7 | | -var Vote = require('../models/vote').Vote; |
| 4 | + |
| 5 | +var Discussion = require('../models/discussion').Discussion; |
8 | 6 | var Flag = require('../models/flag').Flag; |
9 | 7 | var Group = require('../models/group').Group; |
10 | | -var Discussion = require('../models/discussion').Discussion; |
| 8 | +var Script = require('../models/script').Script; |
| 9 | +var User = require('../models/user').User; |
| 10 | +var Vote = require('../models/vote').Vote; |
| 11 | + |
| 12 | +var scriptStorage = require('./scriptStorage'); |
11 | 13 | var addScriptToGroups = require('./group').addScriptToGroups |
12 | 14 | var flagLib = require('../libs/flag'); |
13 | 15 | var removeLib = require('../libs/remove'); |
14 | 16 | var modelsList = require('../libs/modelsList'); |
| 17 | +var modelParser = require('../libs/modelParser'); |
15 | 18 | var renderMd = require('../libs/markdown').renderMd; |
16 | 19 | var formatDate = require('../libs/helpers').formatDate; |
17 | 20 |
|
@@ -47,194 +50,196 @@ exports.useLib = function (req, res, next) { |
47 | 50 | // View a detailed description of a script |
48 | 51 | // This is the most intensive page to render on the site |
49 | 52 | exports.view = function (req, res, next) { |
| 53 | + var user = req.session.user; |
| 54 | + |
50 | 55 | var installName = scriptStorage.getInstallName(req); |
| 56 | + var scriptAuthor = req.route.params.username; |
| 57 | + var scriptNameSlug = req.route.params.scriptname; |
51 | 58 | var isLib = req.route.params.isLib; |
52 | | - var user = req.session.user; |
53 | 59 |
|
54 | | - Script.findOne({ installName: installName + (isLib ? '.js' : '.user.js') }, |
55 | | - function (err, script) { |
56 | | - if (err || !script) { return next(); } |
57 | | - var editUrl = installName.split('/'); |
58 | | - var fork = script.fork; |
59 | | - var options = null; |
60 | | - var tasks = []; |
61 | | - editUrl.shift(); |
62 | | - |
63 | | - // Set the forks to be label properly |
64 | | - if (fork instanceof Array && fork.length > 0) { |
65 | | - fork[0].first = true; |
66 | | - fork[fork.length - 1].original = true; |
| 60 | + Script.findOne({ |
| 61 | + installName: installName + (isLib ? '.js' : '.user.js') |
| 62 | + }, function (err, scriptData) { |
| 63 | + if (err || !scriptData) { return next(); } |
| 64 | + |
| 65 | + var options = {}; |
| 66 | + var tasks = []; |
| 67 | + |
| 68 | + // |
| 69 | + options.title = scriptData.name + ' | OpenUserJS.org'; |
| 70 | + options.user = user; |
| 71 | + |
| 72 | + // |
| 73 | + var script = options.script = modelParser.parseScript(scriptData); |
| 74 | + options.script.isOwner = options.user && options.user._id == script._authorId, |
| 75 | + options.script.aboutRendered = renderMd(script.about); |
| 76 | + |
| 77 | + var fork = script.fork; |
| 78 | + // Set the forks to be label properly |
| 79 | + if (fork instanceof Array && fork.length > 0) { |
| 80 | + fork[0].first = true; |
| 81 | + fork[fork.length - 1].original = true; |
| 82 | + } else { |
| 83 | + fork = null; |
| 84 | + } |
| 85 | + |
| 86 | + // Show the number of open issues |
| 87 | + tasks.push(function (callback) { |
| 88 | + var category = (script.isLib ? 'libs' : 'scripts') + '/' + installName; |
| 89 | + options.scriptIssuesPageUrl = '/' + category + '/issues'; |
| 90 | + options.scriptOpenIssuePageUrl = '/' + category + '/issue/new'; |
| 91 | + |
| 92 | + Discussion.count({ category: category + '/issues', open: true }, |
| 93 | + function (err, count) { |
| 94 | + if (err) { count = 0; } |
| 95 | + options.issuesCount = count; |
| 96 | + callback(); |
| 97 | + }); |
| 98 | + }); |
| 99 | + |
| 100 | + // Show collaborators of the script |
| 101 | + if (script.meta.author && script.meta.collaborator) { |
| 102 | + options.hasCollab = true; |
| 103 | + if (typeof script.meta.collaborator === 'string') { |
| 104 | + options.collaborators = [{ name: script.meta.collaborator }]; |
67 | 105 | } else { |
68 | | - fork = null; |
| 106 | + script.meta.collaborator.forEach(function (collaborator) { |
| 107 | + options.collaborators.push({ name: collaborator }); |
| 108 | + }); |
69 | 109 | } |
| 110 | + } |
70 | 111 |
|
71 | | - // Set up all the general options |
72 | | - options = { |
73 | | - title: script.name, |
74 | | - name: script.name, |
75 | | - version: script.isLib ? '' : script.meta.version, |
76 | | - install: (script.isLib ? '/libs/src/' : '/install/') |
77 | | - + script.installName, |
78 | | - source: (script.isLib ? '/libs/' : '/scripts/') |
79 | | - + installName + '/source', |
80 | | - edit: (script.isLib ? '/lib/' : '/script/') |
81 | | - + editUrl.join('/') + '/edit', |
82 | | - author: script.author, |
83 | | - updated: formatDate(script.updated), |
84 | | - rating: script.rating, |
85 | | - installs: script.installs, |
86 | | - fork: fork, |
87 | | - description: script.isLib ? '' : script.meta.description, |
88 | | - about: renderMd(script.about), |
89 | | - hasCollab: false, |
90 | | - collaborators: [], |
91 | | - isYou: user && user._id == script._authorId, |
92 | | - isLib: script.isLib, |
93 | | - username: user ? user.name : null, |
94 | | - isFork: !!fork |
95 | | - }; |
96 | | - |
97 | | - function render() { res.render('script', options); } |
98 | | - |
99 | | - // Show collaborators of the script |
100 | | - if (script.meta.author && script.meta.collaborator) { |
101 | | - options.hasCollab = true; |
102 | | - if (typeof script.meta.collaborator === 'string') { |
103 | | - options.collaborators = [{ name: script.meta.collaborator }]; |
104 | | - } else { |
105 | | - script.meta.collaborator.forEach(function (collaborator) { |
106 | | - options.collaborators.push({ name: collaborator }); |
107 | | - }); |
108 | | - } |
109 | | - } |
| 112 | + // Show the groups the script belongs to |
| 113 | + tasks.push(function (callback) { |
| 114 | + if (script.isLib) { return callback(); } |
110 | 115 |
|
111 | | - // Show the number of open issues |
| 116 | + Group.find({ _scriptIds: script._id }, 'name', function (err, groups) { |
| 117 | + options.hasGroups = !err && groups.length > 0; |
| 118 | + options.groups = (groups || []).map(function (group) { |
| 119 | + return { name: group.name, url: group.name.replace(/\s+/g, '_') }; |
| 120 | + }); |
| 121 | + callback(); |
| 122 | + }); |
| 123 | + }); |
| 124 | + |
| 125 | + // Show which libraries hosted on the site a script uses |
| 126 | + if (!script.isLib && script.uses && script.uses.length > 0) { |
| 127 | + options.usesLibs = true; |
| 128 | + options.libs = []; |
112 | 129 | tasks.push(function (callback) { |
113 | | - var category = (script.isLib ? 'libs' : 'scripts') |
114 | | - + '/' + installName; |
115 | | - options.issuesUrl = '/' + category + '/issues'; |
116 | | - options.openIssueUrl = '/' + category + '/issue/new'; |
117 | | - |
118 | | - Discussion.count({ category: category + '/issues', open: true }, |
119 | | - function (err, count) { |
120 | | - if (err) { count = 0; } |
121 | | - options.issuesCount = count; |
| 130 | + Script.find({ installName: { $in: script.uses } }, |
| 131 | + function (err, libs) { |
| 132 | + libs.forEach(function (lib) { |
| 133 | + options.libs.push({ |
| 134 | + name: lib.name, url: lib.installName.replace(/\.js$/, '') |
| 135 | + }); |
| 136 | + }); |
122 | 137 | callback(); |
123 | 138 | }); |
124 | 139 | }); |
125 | | - |
126 | | - // Show the groups the script belongs to |
| 140 | + } else if (script.isLib) { |
| 141 | + // Show how many scripts use this library |
127 | 142 | tasks.push(function (callback) { |
128 | | - if (script.isLib) { return callback(); } |
| 143 | + Script.count({ uses: script.installName }, function (err, count) { |
| 144 | + if (err) { count = 0; } |
| 145 | + if (count <= 0) { return callback(); } |
| 146 | + |
| 147 | + options.usedBy = { count: count, url: '/use/lib/' + installName }; |
| 148 | + if (count > 1) { options.usedBy.multiple = true; } |
129 | 149 |
|
130 | | - Group.find({ _scriptIds: script._id }, 'name', function (err, groups) { |
131 | | - options.hasGroups = !err && groups.length > 0; |
132 | | - options.groups = (groups || []).map(function (group) { |
133 | | - return { name: group.name, url: group.name.replace(/\s+/g, '_') }; |
134 | | - }); |
135 | 150 | callback(); |
136 | 151 | }); |
137 | 152 | }); |
| 153 | + } |
| 154 | + |
| 155 | + // Setup the voting UI |
| 156 | + tasks.push(function (callback) { |
| 157 | + var voteUrl = scriptData.url + '/vote/'; |
| 158 | + options.voteUpUrl = voteUrl + 'up'; |
| 159 | + options.voteDownUrl = voteUrl + 'down'; |
| 160 | + |
| 161 | + options.voteable = false; |
| 162 | + options.votedUp = false; |
| 163 | + options.votedDown = false; |
| 164 | + |
| 165 | + // Can't vote when not logged in or when user owns the script. |
| 166 | + if (!user || options.isOwner) { |
| 167 | + callback(); |
| 168 | + return; |
| 169 | + } |
138 | 170 |
|
139 | | - // Show which libraries hosted on the site a script uses |
140 | | - if (!script.isLib && script.uses && script.uses.length > 0) { |
141 | | - options.usesLibs = true; |
142 | | - options.libs = []; |
143 | | - tasks.push(function (callback) { |
144 | | - Script.find({ installName: { $in: script.uses } }, |
145 | | - function (err, libs) { |
146 | | - libs.forEach(function (lib) { |
147 | | - options.libs.push({ |
148 | | - name: lib.name, url: lib.installName.replace(/\.js$/, '') |
149 | | - }); |
150 | | - }); |
151 | | - callback(); |
152 | | - }); |
153 | | - }); |
154 | | - } else if (script.isLib) { |
155 | | - // Show how many scripts use this library |
156 | | - tasks.push(function (callback) { |
157 | | - Script.count({ uses: script.installName }, function (err, count) { |
158 | | - if (err) { count = 0; } |
159 | | - if (count <= 0) { return callback(); } |
| 171 | + Vote.findOne({ |
| 172 | + _scriptId: scriptData._id, |
| 173 | + _userId: user._id |
| 174 | + }, function (err, voteModel) { |
| 175 | + options.voteable = !options.script.isOwner; |
| 176 | + |
| 177 | + if (voteModel) { |
| 178 | + if (voteModel.vote) { |
| 179 | + options.votedUp = true; |
| 180 | + options.voteUpUrl = voteUrl + 'unvote'; |
| 181 | + } else { |
| 182 | + options.votedDown = true; |
| 183 | + options.voteDownUrl = voteUrl + 'unvote'; |
| 184 | + } |
| 185 | + } |
| 186 | + |
| 187 | + callback(); |
| 188 | + }); |
160 | 189 |
|
161 | | - options.usedBy = { count: count, url: '/use/lib/' + installName }; |
162 | | - if (count > 1) { options.usedBy.multiple = true; } |
| 190 | + }); |
163 | 191 |
|
164 | | - callback(); |
165 | | - }); |
166 | | - }); |
167 | | - } |
| 192 | + // Setup the flagging UI |
| 193 | + tasks.push(function (callback) { |
| 194 | + var flagUrl = '/flag' + (script.isLib ? '/libs/' : '/scripts/') + installName; |
168 | 195 |
|
169 | | - if (!user || options.isYou) { |
170 | | - return async.parallel(tasks, render); |
| 196 | + // Can't flag when not logged in or when user owns the script. |
| 197 | + if (!user || options.isOwner) { |
| 198 | + callback(); |
| 199 | + return; |
171 | 200 | } |
172 | 201 |
|
173 | | - // Setup the voting UI |
174 | | - tasks.push(function (callback) { |
175 | | - Vote.findOne({ _scriptId: script._id, _userId: user._id }, |
176 | | - function (err, voteModel) { |
177 | | - var voteUrl = '/vote' + (script.isLib ? '/libs/' : '/scripts/') |
178 | | - + installName + '/'; |
179 | | - |
180 | | - options.voteable = true; |
181 | | - options.upUrl = voteUrl + 'up'; |
182 | | - options.downUrl = voteUrl + 'down'; |
183 | | - |
184 | | - if (voteModel) { |
185 | | - if (voteModel.vote) { |
186 | | - options.up = true; |
187 | | - options.upUrl = voteUrl + 'unvote'; |
188 | | - } else { |
189 | | - options.down = true; |
190 | | - options.downUrl = voteUrl + 'unvote'; |
191 | | - } |
192 | | - } |
| 202 | + flagLib.flaggable(Script, script, user, |
| 203 | + function (canFlag, author, flag) { |
| 204 | + if (flag) { |
| 205 | + flagUrl += '/unflag'; |
| 206 | + options.flagged = true; |
| 207 | + options.flaggable = true; |
| 208 | + } else { |
| 209 | + options.flaggable = canFlag; |
| 210 | + } |
| 211 | + options.flagUrl = flagUrl; |
193 | 212 |
|
194 | | - callback(); |
195 | | - }); |
| 213 | + callback(); |
196 | 214 | }); |
| 215 | + }); |
197 | 216 |
|
198 | | - // Setup the flagging UI |
199 | | - tasks.push(function (callback) { |
200 | | - flagLib.flaggable(Script, script, user, |
201 | | - function (canFlag, author, flag) { |
202 | | - var flagUrl = '/flag' + (script.isLib ? '/libs/' : '/scripts/') |
203 | | - + installName; |
204 | | - |
205 | | - if (flag) { |
206 | | - flagUrl += '/unflag'; |
207 | | - options.flagged = true; |
208 | | - options.flaggable = true; |
209 | | - } else { |
210 | | - options.flaggable = canFlag; |
211 | | - } |
212 | | - options.flagUrl = flagUrl; |
| 217 | + // Set up the removal UI |
| 218 | + tasks.push(function (callback) { |
| 219 | + // Can't remove when not logged in or when user owns the script. |
| 220 | + if (!user || options.isOwner) { |
| 221 | + callback(); |
| 222 | + return; |
| 223 | + } |
213 | 224 |
|
214 | | - callback(); |
215 | | - }); |
216 | | - }); |
| 225 | + removeLib.removeable(Script, script, user, |
| 226 | + function (canRemove, author) { |
| 227 | + options.moderation = canRemove; |
| 228 | + options.flags = script.flags || 0; |
| 229 | + options.removeUrl = '/remove' + (script.isLib ? '/libs/' : '/scripts/') + installName; |
217 | 230 |
|
218 | | - // Set up the removal UI |
219 | | - tasks.push(function (callback) { |
220 | | - removeLib.removeable(Script, script, user, |
221 | | - function (canRemove, author) { |
222 | | - options.moderation = canRemove; |
223 | | - options.flags = script.flags || 0; |
224 | | - options.removeUrl = '/remove' |
225 | | - + (script.isLib ? '/libs/' : '/scripts/') + installName; |
226 | | - |
227 | | - if (!canRemove) { return callback(); } |
228 | | - |
229 | | - flagLib.getThreshold(Script, script, author, |
230 | | - function (threshold) { |
231 | | - options.threshold = threshold; |
232 | | - callback(); |
233 | | - }); |
234 | | - }); |
| 231 | + if (!canRemove) { return callback(); } |
| 232 | + |
| 233 | + flagLib.getThreshold(Script, script, author, |
| 234 | + function (threshold) { |
| 235 | + options.threshold = threshold; |
| 236 | + callback(); |
| 237 | + }); |
235 | 238 | }); |
| 239 | + }); |
236 | 240 |
|
237 | | - async.parallel(tasks, render); |
| 241 | + function render(){ res.render('pages/scriptPage', options); } |
| 242 | + async.parallel(tasks, render); |
238 | 243 | }); |
239 | 244 | }; |
240 | 245 |
|
|
0 commit comments