Skip to content

Commit

Permalink
Merge 44b4937 into 9c3860b
Browse files Browse the repository at this point in the history
  • Loading branch information
martindale committed May 29, 2016
2 parents 9c3860b + 44b4937 commit 3839b17
Show file tree
Hide file tree
Showing 10 changed files with 134 additions and 17 deletions.
3 changes: 3 additions & 0 deletions config/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,9 @@ module.exports = {
sockets: {
timeout: process.env.MAKI_SOCKETS_TIMEOUT || 30000
},
slack: {
token: process.env.MAKI_SLACK_TOKEN || 'https://api.slack.com/docs/oauth-test-tokens'
},
auth: {
local: {
enabled: true
Expand Down
18 changes: 11 additions & 7 deletions lib/Resource/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ var Resource = function( name , options ) {
if (!options.plugins) options.plugins = [];
if (!options.requires) options.requires = [];
if (!options.handlers) options.handlers = {};
if (!options.params) options.params = { query: {} };
if (!options.attributes) options.attributes = {};
if (!options.names) options.names = {};

Expand Down Expand Up @@ -56,7 +57,9 @@ var Resource = function( name , options ) {
self.verbs = {};
self.names = _.defaults( self.options.names , {
query: self.collection,
queryProper: self.plural,
get: nameLower,
getProper: self.name,
create: nameLower
});
self.templates = {
Expand Down Expand Up @@ -174,10 +177,10 @@ Resource.prototype.attach = function( maki ) {
maki: maki, // <- le sigh. can we do this another way?
resource: self
});

self.Schema.methods.Resource = self;
self.Schema.methods.Resources = maki.resources;

Object.keys( self.options.methods ).forEach(function( name ) {
self.Schema.methods[ name ] = self.options.methods[ name ];
});
Expand Down Expand Up @@ -211,19 +214,19 @@ Resource.prototype.attach = function( maki ) {
}

});

if (self.options.indices && self.options.indices instanceof Array) {
self.options.indices.forEach(function(index) {
var fields = {};

index.fields.forEach(function(field) {
fields[field] = 1;
});

var opts = {
unique: index.unique || false
};

self.Schema.index(fields, opts);
});
}
Expand Down Expand Up @@ -359,7 +362,8 @@ Resource.prototype.query = function( q , opts , complete ) {
});

function executeMethod( q , done ) {
var query = Model.find( q ).sort( opts.sort || '-_id' );
var limit = self.options.params.query.limit;
var query = Model.find( q ).limit(self.options.params.query.limit || 20).sort( opts.sort || '-_id' );
if (opts.populate) {
if (typeof opts.populate === 'string') {
query.deepPopulate( opts.populate );
Expand Down
24 changes: 19 additions & 5 deletions lib/Service/http.js
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,20 @@ HTTP.prototype.provide = function() {
// TODO: use Jade's lexer to determine requirements
locals[ resource.names[ req.m ] ] = data;

var stub = {
title: resource.names[req.m + 'Proper'] + ' on ' + self.maki.config.service.name,
description: resource.description
};

console.log('stub:', stub);

if (!(data instanceof Array)) {
stub.title = (data && data.name) ? data.name.display + ', a ' + stub.title : stub.title;
}

// TODO: use defaults / fallback
always.page = _.merge(stub, {});

// only use collectors if HTML
var collectors = [];
Object.keys( resource.requires ).forEach(function( r ) {
Expand Down Expand Up @@ -137,7 +151,6 @@ HTTP.prototype.provide = function() {
res.render( resource.templates[ req.m ] , locals , function( err , body , next ) {
if (self.maki.debug) console.log('render callback', err );
if (err && err.view && err.view.path) return res.render('error'); // TODO: replace with Error handler

// internal express render succeeded, send it!
if (body) {
if (self.maki.debug) console.log('successful render! sending.');
Expand Down Expand Up @@ -275,6 +288,7 @@ HTTP.prototype.attach = function( maki ) {
maki.app.set('views', ['views', __dirname + '/../../views'] );
maki.app.locals.pretty = true;

maki.app.locals._ = require('lodash');
maki.app.locals.markdown = require('marked');
maki.app.locals.moment = require('moment');
maki.app.locals.user = null;
Expand Down Expand Up @@ -604,7 +618,7 @@ HTTP.prototype.attach = function( maki ) {
// there is a slight difference in how Resource methods are implemented//
// specifically, "editing" a resourcing requires 1) a query , and 2)//
// patches. Perhaps we can use a "builder" and .apply() here.

if (p === 'query') {
var executor = function(req, res, next) {
req.m = p;
Expand Down Expand Up @@ -694,7 +708,7 @@ HTTP.prototype.attach = function( maki ) {
});
});
}

maki.app['put']( regex + '/:id' , function(req, res, next) {
req.m = p;
if (resource.source) return res.status( 405 ).provide( r );
Expand All @@ -707,7 +721,7 @@ HTTP.prototype.attach = function( maki ) {
return res.provide( r , instance );
});
});

} else if (p === 'update') {
var executor = function(req, res, next) {
req.m = p;
Expand Down Expand Up @@ -752,7 +766,7 @@ HTTP.prototype.attach = function( maki ) {
var stack = [ regex ];
var pre = [];
var post = [];

pre.push(function setupMiddleware(req, res, next) {
req.resource = resource;
next();
Expand Down
23 changes: 20 additions & 3 deletions maki.js
Original file line number Diff line number Diff line change
Expand Up @@ -45,28 +45,45 @@ maki.use(developers);
maki.use(auth);

var Person = maki.define('Person', {
icon: 'user',
description: 'The list of people working on Maki, including all extended members of the community.',
attributes: {
username: { type: String , max: 80 , required: true , slug: true },
name: {
family: { type: String , max: 80 },
given: { type: String , max: 80 }
},
bio: { type: String },
hash: { type: String , restricted: true },
salt: { type: String , restricted: true },
email: { type: String , max: 80 , restricted: true },
created: { type: Date , default: Date.now }
created: { type: Date , default: Date.now },
image: {
original: { type: String , max: 1024 },
avatar: { type: String , max: 1024 },
}
},
auth: {
'patch': ['admin', function(done) {
var person = this;
return false;
}]
},
icon: 'user'
params: {
query: {
limit: 1000
}
}
});

Person.post('patch', function(done) {
var person = this;
console.log('person updated:', person);
done();
});

Person.post('get', function(done) {
var person = this;
person.name.display = person.username;
done();
});

Expand Down
42 changes: 42 additions & 0 deletions tools/slack.sync.people.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
process.env.NODE_TLS_REJECT_UNAUTHORIZED = 0;

var config = require('../config');
var async = require('async');
var rest = require('restler');
var qs = require('querystring');

var home = 'http://localhost:9200';
var base = 'https://slack.com/api/users.list';
var params = {
token: config.slack.token
};
var url = base + '?' + qs.stringify(params);

console.log('using URL:', url);

rest.get(url).on('complete', function(people) {
console.log('people:', people.members);

people.members.forEach(function(person) {
rest.post(home + '/people', {
headers: {
'Accept': 'application/json'
},
data: {
username: person.name,
name: {
given: person.profile.first_name,
family: person.profile.last_name
},
email: person.profile.email,
bio: person.profile.title,
image: {
original: person.profile.image_original,
avatar: person.profile.image_192,
}
}
}).on('complete', function(result) {
console.log('person result:', person.name, result);
});
});
});
2 changes: 2 additions & 0 deletions views/index.jade
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ block page
| Docs
a.item(href="/plugins")
| Plugins
a.item(href="/people")
| Community
.right.item
script(async, defer, src="https://community.ericmartindale.com/slackin.js?large")
a.ui.inverted.button(href="https://github.com/martindale/maki")
Expand Down
4 changes: 2 additions & 2 deletions views/layouts/default.jade
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@ html.no-js
meta(charset="utf-8")
meta(http-equiv="X-UA-Compatible", content="IE=edge")

title #{config.service.name}
title #{(page) ? page.title : config.service.name}

meta(name="description", content="#{ (page) ? page.title || config.service.name + ': ' + config.service.mission : config.service.name + ': ' + config.service.mission }")
meta(name="description", content="#{ (page) ? page.description || page.title || config.service.name + ': ' + config.service.mission : config.service.name + ': ' + config.service.mission }")
meta(name="viewport", content="width=device-width, initial-scale=1")

link(rel="stylesheet", href="/css/semantic.min.css")
Expand Down
8 changes: 8 additions & 0 deletions views/partials/person.jade
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
.ui.card
a.image(href="/people/#{person.slug}")
img(src="#{person.image.avatar}")
.content
a.header(href="/people/#{person.slug}") #{person.username}
.meta joined in
abbr.tooltipped(title="#{person.created}") #{person.created.getFullYear()}
p #{person.bio}
18 changes: 18 additions & 0 deletions views/people.jade
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
extends layouts/default

block content

.ui.grid
.row
.ui.text.container(style="margin-top: 2em;")
h1 People: the Maki Community
p We are the people working towards <a href="https://slack-files.com/T09HF1HK9-F1827U79U-5cf8742c4f">the Maki Vision</a> by being members of <a href="https://chat.maki.io">the public chat</a>. Add yourself to this list by <a href="https://chat.maki.io">signing in to Slack</a>, and update it by editing your profile there.
p Conveniently, this page also serves as an example resource that can be synchronized with an oracle (or more simply, a trusted API). Take a <a href="https://github.com/martindale/maki/blob/recognition/tools/slack.sync.people.js">look at the source code</a>!
a.ui.right.floated.huge.primary.labeled.button(href="https://chat.maki.io")
| Add Yourself
i.icon.right.chevron
.row
.ui.five.stackable.cards(style="margin-top: 2em;")
- var shuffled = _.shuffle(people);
for person in shuffled
include partials/person
9 changes: 9 additions & 0 deletions views/person.jade
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
extends layouts/default

block content
.ui.grid.stackable
.row
.four.wide.column
include partials/person
.twelve.wide.column

0 comments on commit 3839b17

Please sign in to comment.