Skip to content

Commit

Permalink
Merge pull request #9 from chao-master/feature/DataAccess
Browse files Browse the repository at this point in the history
Add /upcoming command
  • Loading branch information
querkmachine authored Jan 3, 2017
2 parents 84bffc9 + bad1143 commit 6ed94ec
Show file tree
Hide file tree
Showing 5 changed files with 181 additions and 3 deletions.
34 changes: 32 additions & 2 deletions Commands.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
const Command = require("./Ship/Command.js");
const Meets = require("./DataAccess/Meets.js");


const START_WELCOME_MESSAGE = `
Hello, I am SailBot, the helpbot for the severnbronies chat.
Expand All @@ -25,8 +27,36 @@ module.exports = function(commandSource){

//Upcoming command
new Command("upcoming",commandSource)
.addHandler("","Posts the next upcoming meet",notImplemented,Command.nArgs(0))
.addHandler("_location(s)_","Filters the meets to the given locations",notImplemented,Command.atleastArgs(1));
.addHandler("","Posts the next upcoming meet",function(parmaters,message){
Meets.getUpcomingMeets().then(upcoming=>{
let reply = upcoming.length?
upcoming.map(m=>m.asMarkdown()).join("\n"):
"They are no announced meets coming up";
message.connection.sendMessage({
chat_id:message.chat.id,
//reply_to_message_id:message.message_id,
parse_mode:"Markdown",
text:reply,
disable_web_page_preview:true,
display_notification:true
});
});
},Command.nArgs(0))
.addHandler("_location(s)_","Filters the meets to the given locations",function(paramaters,message){
Meets.getUpcomingMeetsAt(paramaters).then(upcoming=>{
let reply = upcoming.length?
upcoming.map(m=>m.asMarkdown()).join("\n"):
"They are no announced meets coming up at given locations";
message.connection.sendMessage({
chat_id:message.chat.id,
//reply_to_message_id:message.message_id,
parse_mode:"Markdown",
text:reply,
disable_web_page_preview:true,
display_notification:true
});
});
},Command.atleastArgs(1));

//Easter egg
new Command("⬆⬆⬇⬇⬅➡⬅➡ba",commandSource)
Expand Down
14 changes: 14 additions & 0 deletions DataAccess/.jshintrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
{
"curly":true,
"eqeqeq":true,
"notypeof":true,
"undef":false,
"unused":true,
"varstmt":true,
"esnext":true,
"globals":{
"module":true,
"require":false,
"console":false
}
}
101 changes: 101 additions & 0 deletions DataAccess/Meets.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
const MySql = require("./MySql.js");
const entities = require("entities");
const moment = require("moment");
const phpUnserialize = require('php-unserialize').unserialize;

class Meet{
constructor(obj){
Object.keys(obj).forEach(k=>this[k]=entities.decodeHTML(obj[k]));
}

loadMetaData(){
let meta = {};
return MySql.query(`
SELECT meta_key,meta_value
FROM wp_postmeta
WHERE post_id=?
AND meta_key NOT LIKE "\\_%"
`,[this.ID]).then(results=>{
results.results.forEach(row=>meta[row.meta_key]=row.meta_value);

this.meet_start_time = meta.meet_start_time;
this.meet_end_time = meta.meet_end_time;
return getLocationTable();
}).then(locationTable => {
let usedLocations = {};
let locData = phpUnserialize(meta.meet_location);
Object.keys(locData).forEach(k=>
usedLocations[locationTable[locData[k]]] = 1
);

this.meet_locations = Object.keys(usedLocations);
console.log(this.meet_locations);
if(this.meet_locations.length === 0){
this.meet_location = "no specified location";
} else if (this.meet_locations.length === 1){
this.meet_location = this.meet_locations[0];
} else {
let body = this.meet_locations.slice(0,-1);
let tail = this.meet_locations[this.meet_locations.length-1];
this.meet_location = `${body.join(", ")}, and ${tail}`;
}
});
}

asMarkdown(){
return `[${this.post_title}](${this.guid}) ${moment(this.meet_start_time).calendar()} at ${this.meet_location}`;
}

}
Meet.fromObjArray = function(objArray){
let meetArray = objArray.map(o=>new Meet(o));
return Promise.all(meetArray.map(o=>o.loadMetaData())).then(()=>meetArray);
};

function intercepts(as,bs){
bs = bs.map(b=>b.toLowerCase());
as = as.map(a=>a.toLowerCase());
return as.some(a=>bs.includes(a));
}

function getUpcomingMeetsAt(locations){
return getUpcomingMeets().then(meets=>meets.filter(m=>intercepts(locations,m.meet_locations)));
}

function getUpcomingMeets(){
return MySql.query(`
SELECT meta_value AS meet_start_time, wp_posts.*
FROM wp_postmeta LEFT JOIN wp_posts ON post_id=ID
WHERE meta_key = 'meet_start_time'
AND post_type = 'meet'
AND post_status = 'publish'
AND CAST(meta_value AS DATE) >= CURDATE();
`).then(results=>Meet.fromObjArray(results.results));
}


let locationTable;
function updateLocationTable(){
console.log("Updating meet location table...");
return MySql.query(`
SELECT post_id, meta_value
FROM wp_postmeta
WHERE meta_key = 'location_locality'
`).then(results=>{
locationTable = {};
results.results.forEach(row=>locationTable[row.post_id]=row.meta_value);
console.log("Meet location table updated...");
return locationTable;
});
}
updateLocationTable();

function getLocationTable(update){
if(locationTable === undefined || update){
return updateLocationTable();
} else {
return Promise.resolve(locationTable);
}
}

module.exports = {getUpcomingMeets,getUpcomingMeetsAt,getLocationTable};
28 changes: 28 additions & 0 deletions DataAccess/MySql.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
const MySql = require("mysql");
const config = require("../config.json");

const pool = MySql.createPool(config.mysql);

function getConnection(){
return new Promise(good=>pool.getConnection((err,con)=>{
if (err){
throw err;
}
good(con);
}));
}

function query(sql,values){
return new Promise(good=>pool.query(sql,values,(err,results,fields)=>{
if (err){
throw err;
}
good({results,fields});
}));
}

function queryStream(sql,values){
return pool.query(sql,values).stream({highWaterMark:10});
}

module.exports = {getConnection,query,queryStream,pool};
7 changes: 6 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,5 +7,10 @@
"test": "echo \"Error: no test specified\" && exit 1"
},
"author": "",
"license": "ISC"
"license": "ISC",
"dependencies": {
"entities": "^1.1.1",
"mysql": "^2.12.0",
"php-serialization": "0.0.4"
}
}

0 comments on commit 6ed94ec

Please sign in to comment.