From 415dad29407362a76fde6d07d8b100285a84518d Mon Sep 17 00:00:00 2001 From: Jules Laplace Date: Thu, 4 Sep 2014 17:52:48 -0400 Subject: media api --- public/assets/img/profile.png | Bin 0 -> 9320 bytes public/assets/javascripts/ui/site/StaffView.js | 33 +++--- public/assets/stylesheets/staff.css | 4 + server/lib/views/staff.js | 141 ++++++++++++++++++++++--- views/staff/_media.ejs | 1 + views/staff/_stats.ejs | 4 + views/staff/_users.ejs | 2 +- views/staff/_users_recent.ejs | 6 +- views/staff/index.ejs | 1 + views/staff/media/index.ejs | 16 +++ views/staff/media/show.ejs | 14 +++ views/staff/media/show_404.ejs | 14 +++ views/staff/projects/index.ejs | 2 + views/staff/projects/show.ejs | 1 + views/staff/projects/show_404.ejs | 2 + views/staff/users/index.ejs | 2 + views/staff/users/show.ejs | 26 +++-- views/staff/users/show_404.ejs | 1 + 18 files changed, 230 insertions(+), 40 deletions(-) create mode 100644 public/assets/img/profile.png create mode 100644 views/staff/_media.ejs create mode 100644 views/staff/media/index.ejs create mode 100644 views/staff/media/show.ejs create mode 100644 views/staff/media/show_404.ejs diff --git a/public/assets/img/profile.png b/public/assets/img/profile.png new file mode 100644 index 0000000..bde68e0 Binary files /dev/null and b/public/assets/img/profile.png differ diff --git a/public/assets/javascripts/ui/site/StaffView.js b/public/assets/javascripts/ui/site/StaffView.js index 8f677cb..fd173e5 100644 --- a/public/assets/javascripts/ui/site/StaffView.js +++ b/public/assets/javascripts/ui/site/StaffView.js @@ -7,7 +7,7 @@ var StaffView = View.extend({ initialize: function() { this.$toggleStaff = $("#toggle-staff") - if (this.$toggleStaff.data().isstaff) { + if (this.$toggleStaff.length && this.$toggleStaff.data().isstaff) { this.$toggleStaff.html("Is Staff") } }, @@ -22,20 +22,23 @@ var StaffView = View.extend({ toggleStaff: function(){ var state = ! this.$toggleStaff.data().isstaff - $.ajax({ - type: "put", - dataType: "json", - url: window.location.href + "/bless", - data: { - state: state, - _csrf: $("#_csrf").val(), - }, - success: function(data){ - this.$toggleStaff.data("isstaff", data.state) - this.$toggleStaff.html(data.state ? "Is Staff" : "Make Staff") - $("#is-staff").html(data.state ? "yes" : "no") - }.bind(this) - }) + var verb = state ? "promote this user to staff?" : "remove this user from staff?" + ConfirmModal.confirm("Are you sure you want to " + verb, function(){ + $.ajax({ + type: "put", + dataType: "json", + url: window.location.href + "/bless", + data: { + state: state, + _csrf: $("#_csrf").val(), + }, + success: function(data){ + this.$toggleStaff.data("isstaff", data.state) + this.$toggleStaff.html(data.state ? "Is Staff" : "Make Staff") + $("#is-staff").html(data.state ? "yes" : "no") + }.bind(this) + }) + }.bind(this)) }, }) diff --git a/public/assets/stylesheets/staff.css b/public/assets/stylesheets/staff.css index 9b9226f..02510a3 100644 --- a/public/assets/stylesheets/staff.css +++ b/public/assets/stylesheets/staff.css @@ -78,6 +78,10 @@ h2 { display: inline-block; background-size: cover; } +#actions button { + float: none; + width: auto; +} iframe.embed { position: static; width: 100%; diff --git a/server/lib/views/staff.js b/server/lib/views/staff.js index b78fd5a..b9fe78a 100644 --- a/server/lib/views/staff.js +++ b/server/lib/views/staff.js @@ -28,10 +28,10 @@ var staff = module.exports = { middleware: { ensureUsers: function(req, res, next){ - var limit = Math.max( Number(req.params.limit) || 50, 200 ) - var offset = Number(req.params.offset) || 0 + var limit = Math.max( Number(req.query.limit) || 50, 200 ) + var offset = Number(req.query.offset) || 0 var sort - switch (req.params.sort) { + switch (req.query.sort) { case 'date': sort = {'created_at': -1} break @@ -60,9 +60,10 @@ var staff = module.exports = { }, ensureProjects: function(req, res, next){ - var offset = Number(req.params.offset) || 0 + var limit = Math.max( Number(req.query.limit) || 50, 200 ) + var offset = Number(req.query.offset) || 0 var sort - switch (req.params.sort) { + switch (req.query.sort) { case 'date': sort = {'created_at': -1} break @@ -74,13 +75,34 @@ var staff = module.exports = { .select(staff.fields.project) .sort(sort) .skip(offset) - .limit(50) + .limit(limit) .exec(function (err, projects) { res.locals.projects = projects.map(staff.helpers.project) next() }) }, + ensureMedia: function(req, res, next){ + var limit = Math.max( Number(req.query.limit) || 50, 200 ) + var offset = Number(req.query.offset) || 0 + var sort + switch (req.query.sort) { + default: + case 'date': + sort = {'created_at': -1} + break + } + Media.find({}) + // .select(staff.fields.media) + .sort(sort) + .skip(offset) + .limit(limit) + .exec(function (err, media) { + res.locals.media = media.map(staff.helpers.media) + next() + }) + }, + ensureRecentProjects: function(req, res, next){ var dreq = { params: { sort: 'created_at', limit: 20, offset: 0 } } staff.middleware.ensureProjects(dreq, res, next) @@ -88,18 +110,32 @@ var staff = module.exports = { ensureProjectsUsers: function(req, res, next){ if (! res.locals.projects || ! res.locals.projects.length) { return next() } + staff.middleware.ensureObjectsUsers(res.locals.projects, next) + }, + + ensureMediaUsers: function(req, res, next){ + if (! res.locals.media || ! res.locals.media.length) { return next() } + staff.middleware.ensureObjectsUsers(res.locals.media, next) + }, + + ensureMediaUser: function(req, res, next){ + if (! res.locals.media) { return next() } + staff.middleware.ensureObjectsUsers([ res.locals.media ], next) + }, + + ensureObjectsUsers: function(objects, next){ var dedupe = {}, user_ids - res.locals.projects.forEach(function(project){ - dedupe[ project.user_id ] = dedupe[ project.user_id ] || [] - dedupe[ project.user_id ].push(project) + objects.forEach(function(obj){ + dedupe[ obj.user_id ] = dedupe[ obj.user_id ] || [] + dedupe[ obj.user_id ].push(obj) }) user_ids = _.keys(dedupe) User.find({ _id: user_ids }) .select(staff.fields.user) .exec(function (err, users) { users.forEach(function(user){ - dedupe[user._id].forEach(function(project){ - project.user = user + dedupe[user._id].forEach(function(obj){ + obj.user = user }) }) next() @@ -125,6 +161,25 @@ var staff = module.exports = { } }, + ensureSingleMedia: function(req, res, next){ + var id = req.params.id + if (id) { + Media.findOne({ _id: id }, function (err, id) { + if (media) { + res.locals.media = req.method == "GET" ? staff.helpers.media(media) : media + } + else { + res.locals.media = null + } + next() + }) + } + else { + res.locals.media = null + next() + } + }, + ensureUsersCount: function(req, res, next){ User.count({}, function(err, count){ res.locals.userCount = count || 0 @@ -138,6 +193,13 @@ var staff = module.exports = { next() }) }, + + ensureMediaCount: function(req, res, next){ + Media.count({}, function(err, count){ + res.locals.mediaCount = count || 0 + next() + }) + }, ensureProfileProjectCount: function(req, res, next){ if (! res.locals.profile) { return next() } @@ -202,7 +264,14 @@ var staff = module.exports = { project.date = moment( project.updated_at || project.created_at ).format("M/DD/YYYY H:MM") project.user = {} return project - } + }, + + media: function(media){ + media = media.toObject() + media.date = moment( media.updated_at || media.created_at ).format("M/DD/YYYY H:MM") + media.user = {} + return media + } }, route: function(app){ @@ -213,9 +282,14 @@ var staff = module.exports = { staff.middleware.ensureRecentUsers, staff.middleware.ensureUsersCount, staff.middleware.ensureProjectsCount, + staff.middleware.ensureMediaCount, staff.index ); + + // + // users + app.get('/staff/users', middleware.ensureAuthenticated, middleware.ensureIsStaff, @@ -244,6 +318,10 @@ var staff = module.exports = { staff.users.bless ); + + // + // projects + app.get('/staff/projects', middleware.ensureAuthenticated, middleware.ensureIsStaff, @@ -266,6 +344,28 @@ var staff = module.exports = { staff.projects.show ); + + // + // media + + app.get('/staff/media', + middleware.ensureAuthenticated, + middleware.ensureIsStaff, + + staff.middleware.ensureMedia, + staff.middleware.ensureMediaUsers, + + staff.media.index + ); + app.get('/staff/media/:id', + middleware.ensureAuthenticated, + middleware.ensureIsStaff, + + staff.middleware.ensureSingleMedia, + staff.middleware.ensureMediaUser, + + staff.media.show + ); }, @@ -316,5 +416,22 @@ var staff = module.exports = { } }, }, + + media: { + index: function(req, res){ + res.render('staff/media/index') + }, + show: function(req, res){ + if (res.locals.project) { + res.render('staff/media/show', { + mediaJSON: util.escape( JSON.stringify( res.locals.media ) ), + mediaUserJSON: util.escape( JSON.stringify( res.locals.mediaUser ) ), + }) + } + else { + res.render('staff/media/show_404') + } + }, + } } \ No newline at end of file diff --git a/views/staff/_media.ejs b/views/staff/_media.ejs new file mode 100644 index 0000000..27949aa --- /dev/null +++ b/views/staff/_media.ejs @@ -0,0 +1 @@ +media \ No newline at end of file diff --git a/views/staff/_stats.ejs b/views/staff/_stats.ejs index 6439ab9..d1ad96f 100644 --- a/views/staff/_stats.ejs +++ b/views/staff/_stats.ejs @@ -10,4 +10,8 @@ projects [[- projectCount ]] + + media + [[- mediaCount ]] + diff --git a/views/staff/_users.ejs b/views/staff/_users.ejs index 344ee0c..d46058f 100644 --- a/views/staff/_users.ejs +++ b/views/staff/_users.ejs @@ -11,7 +11,7 @@ [[- user.displayName ]] - [view profile] + [view profile] [[- user.last_seen ]] diff --git a/views/staff/_users_recent.ejs b/views/staff/_users_recent.ejs index e55fe28..b452c08 100644 --- a/views/staff/_users_recent.ejs +++ b/views/staff/_users_recent.ejs @@ -7,15 +7,13 @@ [[ users.forEach(function(user){ ]] - [[ if (user.photo) { ]] -
- [[ } ]] +
[[- user.username ]] - [view profile] + [view profile] [[- user.displayName ]] diff --git a/views/staff/index.ejs b/views/staff/index.ejs index 842e086..f721339 100644 --- a/views/staff/index.ejs +++ b/views/staff/index.ejs @@ -5,6 +5,7 @@
diff --git a/views/staff/media/index.ejs b/views/staff/media/index.ejs new file mode 100644 index 0000000..76aaafb --- /dev/null +++ b/views/staff/media/index.ejs @@ -0,0 +1,16 @@ +[[ include ../_header ]] + +

Media

+ + + +
+ +[[ include ../_media ]] + +[[ include ../_footer ]] diff --git a/views/staff/media/show.ejs b/views/staff/media/show.ejs new file mode 100644 index 0000000..43afe14 --- /dev/null +++ b/views/staff/media/show.ejs @@ -0,0 +1,14 @@ +[[ include ../_header ]] + +

Media [[- media.type ]]

+ + + +
+ +[[ include ../_footer ]] diff --git a/views/staff/media/show_404.ejs b/views/staff/media/show_404.ejs new file mode 100644 index 0000000..f07cef2 --- /dev/null +++ b/views/staff/media/show_404.ejs @@ -0,0 +1,14 @@ +[[ include ../_header ]] + +

Media not found

+ + + +
+ +[[ include ../_footer ]] diff --git a/views/staff/projects/index.ejs b/views/staff/projects/index.ejs index a5a9308..a6fad6b 100644 --- a/views/staff/projects/index.ejs +++ b/views/staff/projects/index.ejs @@ -1,10 +1,12 @@ [[ include ../_header ]] +

Projects


diff --git a/views/staff/projects/show.ejs b/views/staff/projects/show.ejs index 23e96b2..0fdb00b 100644 --- a/views/staff/projects/show.ejs +++ b/views/staff/projects/show.ejs @@ -6,6 +6,7 @@ home users projects + media
diff --git a/views/staff/projects/show_404.ejs b/views/staff/projects/show_404.ejs index 193333f..70320c0 100644 --- a/views/staff/projects/show_404.ejs +++ b/views/staff/projects/show_404.ejs @@ -1,10 +1,12 @@ [[ include ../_header ]] +

Project not found


diff --git a/views/staff/users/index.ejs b/views/staff/users/index.ejs index ad92fc3..8c56565 100644 --- a/views/staff/users/index.ejs +++ b/views/staff/users/index.ejs @@ -1,10 +1,12 @@ [[ include ../_header ]] +

Users


diff --git a/views/staff/users/show.ejs b/views/staff/users/show.ejs index 2315043..afcd761 100644 --- a/views/staff/users/show.ejs +++ b/views/staff/users/show.ejs @@ -5,6 +5,7 @@ home users projects + media
@@ -21,7 +22,7 @@ [[- profile.displayName ]] - [view profile] + [view profile] @@ -43,6 +44,22 @@ [[- profile.last_seen ]] + + + projects + + + [[- profile.projectCount ]] + + + + + media + + + [[- profile.mediaCount ]] + + ip addresses @@ -77,10 +94,3 @@ [[ include ../_footer ]] - \ No newline at end of file diff --git a/views/staff/users/show_404.ejs b/views/staff/users/show_404.ejs index 3fd20a8..bcd0271 100644 --- a/views/staff/users/show_404.ejs +++ b/views/staff/users/show_404.ejs @@ -5,6 +5,7 @@ home users projects + media
-- cgit v1.2.3-70-g09d2