From 612171d28837d2a3613c02cd9ac29d62801330e1 Mon Sep 17 00:00:00 2001 From: Sam Nicholls Date: Tue, 22 Jan 2013 20:44:45 +0000 Subject: [PATCH] Preliminary Profiles [#183][#184][MADE BY @samstudio8 #185] --- modules/profile/api.js | 22 +++++++++++ modules/profile/commands.js | 28 +++++++++++++ modules/profile/config.json | 18 +++++++++ modules/profile/pages.js | 30 ++++++++++++++ modules/profile/profile.js | 40 +++++++++++++++++++ modules/users/pages.js | 32 --------------- views/profile/profile.jade | 79 +++++++++++++++++++++++++++++++++++++ views/users/users.jade | 6 ++- 8 files changed, 221 insertions(+), 34 deletions(-) create mode 100644 modules/profile/api.js create mode 100644 modules/profile/commands.js create mode 100644 modules/profile/config.json create mode 100644 modules/profile/pages.js create mode 100644 modules/profile/profile.js create mode 100644 views/profile/profile.jade diff --git a/modules/profile/api.js b/modules/profile/api.js new file mode 100644 index 0000000..e6b22c8 --- /dev/null +++ b/modules/profile/api.js @@ -0,0 +1,22 @@ +var _ = require('underscore')._; + +var api = function(dbot) { + return { + + /** + * Create a profile for a new primary user on a given server. + * If the server does not already exist, create it. + */ + "createProfile": function(server, primary){ + if(!_.has(this.profiles, server)){ + this.profiles[server] = {}; + } + this.profiles[server][primary] = {}; + _.defaults(this.profiles[server][primary], this.config.schema); + }, + } +}; + +exports.fetch = function(dbot) { + return api(dbot); +}; diff --git a/modules/profile/commands.js b/modules/profile/commands.js new file mode 100644 index 0000000..a836c2d --- /dev/null +++ b/modules/profile/commands.js @@ -0,0 +1,28 @@ +var _ = require('underscore')._; + +var commands = function(dbot){ + return { + + "~test_getprop": function(event){ + if(event.params[1]){ + var res = dbot.db.profiles[event.server][event.user.toLowerCase()].profile[event.params[1]]; + if(res){ + event.reply(res); + } + else{ + event.reply("Nope."); + } + } + }, + + "~test_setprop": function(event){ + if(event.params[1] && event.params[2]){ + dbot.db.profiles[event.server][event.user.toLowerCase()].profile[event.params[1]] = event.params[2]; + } + } + } +}; + +exports.fetch = function(dbot){ + return commands(dbot); +}; diff --git a/modules/profile/config.json b/modules/profile/config.json new file mode 100644 index 0000000..0082dd3 --- /dev/null +++ b/modules/profile/config.json @@ -0,0 +1,18 @@ +{ + "ignorable": false, + "dbKeys": [ "profiles" ], + "help": "https://github.com/reality/depressionbot/blob/master/modules/profile/README.md", + "schema": { + "profile": { + "primary": null, + "name": null, + "tagline": null, + "favourites": { + "colour": null + } + }, + "preferences": { + "timezone": null + } + } +} diff --git a/modules/profile/pages.js b/modules/profile/pages.js new file mode 100644 index 0000000..379892c --- /dev/null +++ b/modules/profile/pages.js @@ -0,0 +1,30 @@ +var pages = function(dbot) { + var _ = require('underscore')._; + var connections = dbot.instance.connections; + + return { + '/profile/:connection/:user': function(req, res) { + var connection = req.params.connection; + var user = dbot.cleanNick(req.params.user); + + var primary = dbot.api.users.resolveUser(connection, user, true); + //var profile = dbot.api.profile.getProfile(primary); + var profile = dbot.db.profiles[connection][primary.toLowerCase()].profile; + var stats = dbot.api.stats.getUserChansStats(connection, primary.toLowerCase(), [ + "lines", "words", "lincent", "wpl", "in_mentions"] + ); + + res.render('profile', { + 'name': dbot.config.name, + 'connection': connection, + 'primary': primary, + 'profile': profile, + 'stats': stats.channels, + }); + } + } +}; + +exports.fetch = function(dbot) { + return pages(dbot); +}; diff --git a/modules/profile/profile.js b/modules/profile/profile.js new file mode 100644 index 0000000..4c991b4 --- /dev/null +++ b/modules/profile/profile.js @@ -0,0 +1,40 @@ +var _ = require('underscore')._; + +var profile = function(dbot) { + + this.profiles = dbot.db.profiles; + + /** + * Iterate over known user profiles and ensure they contain all the + * required properties as defined in the configuation. + */ + this.onLoad = function(){ + var schema = this.config.schema; + + // Ensure all known users have a profile + _.each(dbot.api.users.getAllUsers(), function(server, serverName){ + if(!_.has(dbot.db.profiles, serverName)){ + dbot.db.profiles[serverName] = {} + } + _.each(server, function(userName){ + var primary = userName; + userName = userName.toLowerCase(); + if(!_.has(dbot.db.profiles[serverName], userName)){ + dbot.db.profiles[serverName][userName] = { + "profile": {}, + "preferences": {} + }; + } + //TODO(samstudio8) Currently only handles "top-level" + _.defaults(dbot.db.profiles[serverName][userName].profile, schema.profile); + _.defaults(dbot.db.profiles[serverName][userName].preferences, schema.preferences); + dbot.db.profiles[serverName][userName].profile.primary = primary; + }); + }); + dbot.save(); + }; +}; + +exports.fetch = function(dbot) { + return new profile(dbot); +}; diff --git a/modules/users/pages.js b/modules/users/pages.js index 4c6cf84..c3fad15 100644 --- a/modules/users/pages.js +++ b/modules/users/pages.js @@ -66,38 +66,6 @@ var pages = function(dbot) { res.render_core('error', { 'name': dbot.config.name, 'message': 'No such connection or channel.' }); } }, - - '/user/:connection/:channel/:user': function(req, res) { - var connection = req.params.connection; - var channel = '#' + req.params.channel; - var user = dbot.cleanNick(req.params.user); - - var quoteCount = 'no'; - if(dbot.db.quoteArrs.hasOwnProperty(user)) { - var quoteCount = dbot.db.quoteArrs[user].length; - } - - if(dbot.config.moduleNames.include('kick')) { - if(!dbot.db.kicks.hasOwnProperty(req.params.user)) { - var kicks = '0'; - } else { - var kicks = dbot.db.kicks[req.params.user]; - } - - if(!dbot.db.kickers.hasOwnProperty(req.params.user)) { - var kicked = '0'; - } else { - var kicked = dbot.db.kickers[req.params.user]; - } - } else { - var kicks = 'N/A'; - var kicked = 'N/A'; - } - - res.render('user', { 'name': dbot.config.name, 'user': req.params.user, - 'channel': channel, 'connection': connection, 'cleanUser': user, - 'quotecount': quoteCount, 'kicks': kicks, 'kicked': kicked }); - } }; }; diff --git a/views/profile/profile.jade b/views/profile/profile.jade new file mode 100644 index 0000000..4648573 --- /dev/null +++ b/views/profile/profile.jade @@ -0,0 +1,79 @@ +extends ../layout + +block content + script(type="text/javascript", src="http://ajax.aspnetcdn.com/ajax/jquery.dataTables/1.9.4/jquery.dataTables.min.js") + + script + $(document).ready(function(){ + // Allowing forcing of string stats data to sort as numeric + jQuery.extend( jQuery.fn.dataTableExt.oSort, { + "forcenum-pre": function ( a ) { + a = a.replace("\,", ""); + return parseFloat( a ); + }, + + "forcenum-asc": function ( a, b ) { + return a - b; + }, + + "forcenum-desc": function ( a, b ) { + return b - a; + } + } ); + + $('.data').dataTable({ + "aoColumnDefs": [ + { "sType": "forcenum", + "asSorting": [ "desc", "asc" ], + "aTargets": [ 1, 2, 3, 4, 5 ] } + ], + "bPaginate": false, + "bFilter": false, + "bLengthChange": false, + "oLanguage": { + "sInfo": "", + "sInfoEmpty": "", + "sInfoFiltered": "" + }, + }); + }); + + div.page-header + h1 + #{primary} + + div#row + table.table.table-hover.data + thead + tr + th Channel + th Lines + th Words + th Lincent + th Verbosity + th Mentions + tbody + for chan, key in stats + if stats.hasOwnProperty(key) + tr + td + a(href='/users/'+connection+'/'+encodeURIComponent(key)) + #{key} + span + if chan.online + if chan.active + span.label.label-success Active + else + span.label.label-important Inactive + else + span.label Offline + td + #{chan.fields.lines.data} + td + #{chan.fields.words.data} + td + #{chan.fields.lincent.data} + td + #{chan.fields.wpl.data} + td + #{chan.fields.in_mentions.data} diff --git a/views/users/users.jade b/views/users/users.jade index 4ef3022..23251a1 100644 --- a/views/users/users.jade +++ b/views/users/users.jade @@ -25,7 +25,9 @@ block content "aoColumnDefs": [ { "aDataSort": [ 1, 0 ], "asSorting": [ "asc" ], "aTargets": [ 0 ] }, { "bVisible": false, "aTargets": [ 1 ] }, - { "sType": "forcenum", "aTargets": [ 2, 3, 4, 5, 6 ] } + { "sType": "forcenum", + "asSorting": [ "desc", "asc" ], + "aTargets": [ 2, 3, 4, 5, 6 ] } ], "bPaginate": false, "bLengthChange": false, @@ -55,7 +57,7 @@ block content -each nick in nicks tr td - a(href='/user/'+connection+'/'+channel.substr(1,channel.length)+'/'+nick.primary) + a(href='/profile/'+connection+'/'+nick.primary) #{nick.display} span if nick.online