Skip to content

Commit

Permalink
Merge branch 'master' of github.com:OpenUserJs/OpenUserJS.org
Browse files Browse the repository at this point in the history
  • Loading branch information
sizzlemctwizzle committed Jun 14, 2014
2 parents 3001267 + 99cfcc4 commit 65ce87e
Show file tree
Hide file tree
Showing 18 changed files with 158 additions and 71 deletions.
14 changes: 12 additions & 2 deletions controllers/admin.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,13 @@
var async = require('async');

var User = require('../models/user.js').User;
var Comment = require('../models/comment').Comment;
var Discussion = require('../models/discussion').Discussion;
var Flag = require('../models/flag').Flag;
var Group = require('../models/group').Group;
var Script = require('../models/script').Script;
var Strategy = require('../models/strategy.js').Strategy;
var Strategy = require('../models/strategy').Strategy;
var User = require('../models/user').User;
var Vote = require('../models/vote').Vote;

var userRoles = require('../models/userRoles.json');
var strategies = require('./strategies.json');
Expand Down Expand Up @@ -87,6 +92,11 @@ exports.adminUserView = function (req, res, next) {
var jsonModelMap = {
'User': User,
'Script': Script,
'Group': Group,
'Discussion': Discussion,
'Comment': Comment,
'Vote': Vote,
'Flag': Flag,
};
// View everything about a particular user
// This is mostly for debugging in production
Expand Down
21 changes: 6 additions & 15 deletions controllers/auth.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,29 +9,20 @@ var verifyPassport = require('../libs/passportVerify').verify;
var cleanFilename = require('../libs/helpers').cleanFilename;
var addSession = require('../libs/modifySessions').add;

// These functions serialize the user model so we can keep
// the info in the session
// Unused but removing it breaks passport
passport.serializeUser(function (user, done) {
done(null, user._id);
});
/*
passport.deserializeUser(function (id, done) {
User.findOne({ _id : id }, function (err, user) {
done(err, user);
});
});*/

// Setup all our auth strategies
var openIdStrategies = {};
Strategy.find({}, function (err, strategies) {

// Get OpenId strategies
if (process.env.NODE_ENV === 'production') {
for (var name in allStrategies) {
if (!allStrategies[name].oauth) {
openIdStrategies[name] = true;
strategies.push({ 'name' : name, 'openid' : true });
}
for (var name in allStrategies) {
if (!allStrategies[name].oauth) {
openIdStrategies[name] = true;
strategies.push({ 'name' : name, 'openid' : true });
}
}

Expand Down Expand Up @@ -152,7 +143,7 @@ exports.callback = function (req, res, next) {
user.sessionId = req.sessionID;

// Save GitHub username.
if (req.session.profile.provider === 'github') {
if (req.session.profile && req.session.profile.provider === 'github') {
user.ghUsername = req.session.profile.username;
}

Expand Down
19 changes: 19 additions & 0 deletions controllers/group.js
Original file line number Diff line number Diff line change
Expand Up @@ -191,6 +191,22 @@ exports.list = function (req, res) {
async.parallel(tasks, asyncComplete);
};

var setupGroupSidePanel = function(options) {
// Shortcuts
var group = options.group;
var authedUser = options.authedUser;

// Mod
if (authedUser && authedUser.isMod) {
options.modTools = {};
}

// Admin
if (authedUser && authedUser.isAdmin) {
options.adminTools = {};
}
};

// list the scripts in a group
exports.view = function (req, res, next) {
var authedUser = req.session.user;
Expand Down Expand Up @@ -242,6 +258,9 @@ exports.view = function (req, res, next) {
.sort('-rating')
.limit(25);

// SideBar
setupGroupSidePanel(options);

//--- Tasks

// Pagination
Expand Down
18 changes: 8 additions & 10 deletions controllers/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -199,16 +199,14 @@ exports.register = function (req, res) {
options.strategies = [];

// Get OpenId strategies
if (process.env.NODE_ENV === 'production') {
_.each(strategies, function(strategy, strategyKey){
if (!strategy.oauth) {
options.strategies.push({
'strat': strategyKey,
'display': strategy.name
});
}
});
}
_.each(strategies, function(strategy, strategyKey){
if (!strategy.oauth) {
options.strategies.push({
'strat': strategyKey,
'display': strategy.name
});
}
});

//--- Tasks

Expand Down
38 changes: 20 additions & 18 deletions controllers/script.js
Original file line number Diff line number Diff line change
Expand Up @@ -94,29 +94,31 @@ var getScriptPageTasks = function(options) {

// Show which libraries hosted on the site a script uses
if (!script.isLib && script.uses && script.uses.length > 0) {
options.usesLibs = true;
options.libs = [];
script.usesLibs = true;
script.libs = [];
tasks.push(function (callback) {
Script.find({ installName: { $in: script.uses } },
function (err, libs) {
libs.forEach(function (lib) {
options.libs.push({
name: lib.name, url: lib.installName.replace(/\.js$/, '')
});
});
callback();
Script.find({
installName: { $in: script.uses }
}, function (err, scriptLibraryList) {
if (err) return callback(err);

script.libs = scriptLibraryList;
script.libs = _.map(script.libs, modelParser.parseScript);
callback();
});
});
} else if (script.isLib) {
// Show how many scripts use this library
script.isUsed = false;
script.usedBy = [];
tasks.push(function (callback) {
Script.count({ uses: script.installName }, function (err, count) {
if (err) { count = 0; }
if (count <= 0) { return callback(); }

options.usedBy = { count: count, url: '/use/lib/' + script.installNameSlug };
if (count > 1) { options.usedBy.multiple = true; }

Script.find({
uses: script.installName
}, function (err, libraryScriptList) {
if (err) return callback(err);

script.isUsed = libraryScriptList.length > 0;
script.usedBy = libraryScriptList;
script.usedBy = _.map(script.usedBy, modelParser.parseScript);
callback();
});
});
Expand Down
6 changes: 3 additions & 3 deletions controllers/user.js
Original file line number Diff line number Diff line change
Expand Up @@ -693,12 +693,12 @@ exports.newLibraryPage = function (req, res, next) {
options.isAdmin = authedUser && authedUser.isAdmin;

//
options.newJSLibary = true;
options.newJSLibrary = true;
options.newScriptEditorPageUrl = '/user/add/lib/new';
options.uploadNewScriptPageUrl = '/user/add/lib/upload';

// Metadata
options.title = 'New Libary | OpenUserJS.org';
options.title = 'New Library | OpenUserJS.org';
options.pageMetaDescription = null;
options.pageMetaKeywords = null;

Expand Down Expand Up @@ -1209,7 +1209,7 @@ exports.update = function (req, res, next) {

if (!user) { return res.redirect('/login'); }

if (req.body.about) {
if (typeof req.body.about !== 'undefined') {
// Update the about section of a user's profile
User.findOneAndUpdate({ _id: user._id },
{ about: req.body.about },
Expand Down
2 changes: 1 addition & 1 deletion libs/githubClient.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ Strategy.findOne({ name: 'github' }, function(err, strat) {
});
console.log('GitHub client authenticated');
} else {
console.warning('GitHub client NOT authenticated. Will have a lower Rate Limit.');
console.warn('GitHub client NOT authenticated. Will have a lower Rate Limit.');
}

});
Expand Down
1 change: 0 additions & 1 deletion libs/modelParser.js
Original file line number Diff line number Diff line change
Expand Up @@ -263,7 +263,6 @@ var parseDiscussion = function(discussionData) {
name: username,
};
});
recentCommentors.reverse();
discussion.recentCommentors = recentCommentors;

//discussion.path = discussion.path + (discussion.duplicateId ? '_' + discussion.duplicateId : '');
Expand Down
11 changes: 7 additions & 4 deletions libs/modelQuery.js
Original file line number Diff line number Diff line change
Expand Up @@ -40,11 +40,16 @@ var parseSearchConditions = function(q, prefixSearchFields, fullSearchFields) {
var fullStr = '';
var prefixRegex = null;
var fullRegex = null;
var terms = q.replace(/([.*+?^=!:${}()|\[\]\/\\])/g, '\\$1').split(/\s+/);
var terms = q.replace(/([.*+?^=!:${}()|\[\]\/\\])/g, '\\$1').split(/\s+/).map(function (e) { return e.trim(); });

// Match all the terms but in any order
terms.forEach(function (term) {
prefixStr += '(?=.*?\\b' + term + ')';
var isNonASCII = /^\W/.test(term);
if (isNonASCII) {
prefixStr += '(?=.*?([ \n\r\t.,\'"\+!?-]+)' + term + ')';
} else {
prefixStr += '(?=.*?\\b' + term + ')';
}
fullStr += '(?=.*?' + term + ')';
});
prefixRegex = new RegExp(prefixStr, 'i');
Expand All @@ -68,8 +73,6 @@ exports.parseSearchConditions = parseSearchConditions;

var parseModelListSearchQuery = function(modelListQuery, query, searchOptions) {
var q = unescape(query);
var partialWordMatchFields = ['name', 'author', 'about', 'meta.description'];
var fullWordMatchFields = ['meta.include', 'meta.match'];
modelListQuery.and({
$or: parseSearchConditions(q, searchOptions.partialWordMatchFields, searchOptions.fullWordMatchFields)
});
Expand Down
12 changes: 11 additions & 1 deletion libs/modifySessions.js
Original file line number Diff line number Diff line change
Expand Up @@ -88,8 +88,18 @@ exports.update = function (req, user, callback) {
// Destory all sessions for a user
exports.destroy = function (req, user, callback) {
var store = req.sessionStore;
var emptySess = {
cookie: {
path: '/',
_expires: null,
originalMaxAge: null,
httpOnly: true
}
};

if (!user || !user.sessionIds) { return cb('No sessions', null); }

async.each(user.sessionIds, store.destroy, callback);
async.each(user.sessionIds, function (id, cb) {
store.set(id, emptySess, cb);
}, callback);
};
4 changes: 4 additions & 0 deletions public/css/common.css
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
html, body {
min-height: 100vh;
}

@media (max-width: 767px) {
span.visible-xs {
display: inline !important;
Expand Down
13 changes: 13 additions & 0 deletions views/includes/groupAdminToolsPanel.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
{{#adminTools}}
<div class="panel panel-default">
<div class="panel-heading">
<div class="panel-title">
<i class="fa fa-fw fa-coffee"></i>
Admin Tools
</div>
</div>
<div class="panel-body">
<a href="/admin/json?model=Group&id={{{group._id}}}" class="btn btn-link col-xs-12"><i class="fa fa-database"></i> Raw JSON Data</a>
</div>
</div>
{{/adminTools}}
2 changes: 1 addition & 1 deletion views/includes/scriptPageHeader.html
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ <h2 class="page-heading">
<button class="btn btn-info"><i class="fa fa-download"></i> Install</button>
{{/script.isLib}}
</a>
{{#script.icon45Url}}<span class="page-heading-icon"><img src="{{{script.icon45Url}}}" alt="" /></span> {{/script.icon45Url}} <a href="{{{script.author.userPageUrl}}}" class="script-author">{{script.author.name}}</a><span class="path-divider">/</span><a href="{{{scriptPageUrl}}}" class="script-name">{{script.name}}</a>
{{#script.icon45Url}}<span class="page-heading-icon"><img src="{{{script.icon45Url}}}" alt="" /></span> {{/script.icon45Url}} <a href="{{{script.author.userPageUrl}}}" class="script-author">{{script.author.name}}</a><span class="path-divider">/</span><a href="{{{script.scriptPageUrl}}}" class="script-name">{{script.name}}</a>
</h2>
<nav class="navbar navbar-default navbar-static-top" role="navigation">
<div class="navbar-header">
Expand Down
1 change: 1 addition & 0 deletions views/pages/groupScriptListPage.html
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ <h2 class="page-heading">
<div class="col-sm-4">
{{> includes/searchBarPanel.html }}
{{> includes/popularGroupsPanel.html }}
{{> includes/groupAdminToolsPanel.html }}
</div>
</div>
</div>
Expand Down
10 changes: 5 additions & 5 deletions views/pages/newScriptPage.html
Original file line number Diff line number Diff line change
Expand Up @@ -59,15 +59,15 @@ <h3>Collaboration</h3>
</div>
</div>
{{/newUserJS}}
{{#newJSLibary}}
<h2><i class="fa fa-question"></i> Libary Docs</h2>
{{#newJSLibrary}}
<h2><i class="fa fa-question"></i> Library Docs</h2>
<div class="panel panel-default">
<div class="panel-body">
<h3>OpenUserJS.org Specific</h3>
<p>Any UserScript on OpenUserJS.org that <code>//@require</code>'s libraries also on OpenUserJS.org will link to the library on the UserScript page.</p>
</div>
</div>
{{/newJSLibary}}
{{/newJSLibrary}}
</div>
<div class="col-md-4">
<h2><i class="fa fa-plus-square"></i> New Script</h2>
Expand All @@ -83,11 +83,11 @@ <h4 class="list-group-item-heading">Upload Script</h4>
<p class="list-group-item-text">
<form action="{{{uploadNewScriptPageUrl}}}" method="post" enctype="multipart/form-data" class="form-horizontal">
<input type="hidden" name="uploadScript" value="true">
{{#newJSLibary}}
{{#newJSLibrary}}
<div class="form-group">
<input type="text" required class="form-control" name="script_name" placeholder="Script Name" required>
</div>
{{/newJSLibary}}
{{/newJSLibrary}}
<div class="form-group">
<div class="input-group">
<input type="file" required class="form-control" name="script" placeholder="Enter email">
Expand Down
2 changes: 0 additions & 2 deletions views/pages/scriptIssueListPage.html
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,7 @@
<div class="container-fluid">
<div class="row">
<div class="container-fluid col-sm-8">
{{#script}}
{{> includes/scriptPageHeader.html }}
{{/script}}
<div class="list-controls row container-fluid">
<ol class="breadcrumb pull-left">
<li class=""><a href="{{{script.scriptPageUrl}}}">{{script.name}}</a></li>
Expand Down
2 changes: 0 additions & 2 deletions views/pages/scriptIssuePage.html
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,7 @@
<div class="container-fluid">
<div class="row">
<div class="container-fluid col-sm-8">
{{#script}}
{{> includes/scriptPageHeader.html }}
{{/script}}
<div class="col-xs-12">
<div class="topic-title row container-fluid">
<ol class="breadcrumb pull-left">
Expand Down
Loading

0 comments on commit 65ce87e

Please sign in to comment.