Skip to content

Commit

Permalink
⬆️ [email protected] and friends
Browse files Browse the repository at this point in the history
  • Loading branch information
dgurkaynak committed Aug 17, 2018
1 parent d0a7169 commit 9f6d678
Show file tree
Hide file tree
Showing 8 changed files with 1,209 additions and 1,389 deletions.
2,432 changes: 1,134 additions & 1,298 deletions package-lock.json

Large diffs are not rendered by default.

20 changes: 10 additions & 10 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -25,17 +25,17 @@
"homepage": "https://github.com/dgurkaynak/slack-poker-planner#readme",
"dependencies": {
"@slack/client": "4.4.0",
"boom": "4.3.1",
"dotenv": "4.0.0",
"handlebars": "4.0.7",
"hapi": "16.1.1",
"inert": "4.2.0",
"boom": "7.2.0",
"dotenv": "6.0.0",
"handlebars": "4.0.11",
"hapi": "17.5.3",
"inert": "5.1.0",
"lodash": "4.17.10",
"request": "2.81.0",
"request-promise": "4.2.0",
"sqlite": "2.6.0",
"uuid": "3.0.1",
"vision": "4.1.1",
"request": "2.88.0",
"request-promise": "4.2.2",
"sqlite": "2.9.2",
"uuid": "3.3.2",
"vision": "5.3.3",
"winston": "3.0.0"
}
}
2 changes: 1 addition & 1 deletion src/app.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ async function main() {
await db.migrate({ force: process.env.DB_FORCE_MIGRATIONS ? 'last' : false });

winston.info('Starting the server...');
const server = Server.create();
const server = await Server.create();
await Server.start(server);

winston.info('Boot successful!');
Expand Down
42 changes: 21 additions & 21 deletions src/routes/action-endpoint.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,20 +10,20 @@ module.exports = async (request, reply) => {

if (payload.token != process.env.SLACK_VERIFICATION_TOKEN) {
winston.error(`Could not process action, invalid verification token`, payload);
return reply({
return {
text: `Invalid slack verification token, please get in touch with the maintainer`,
response_type: 'ephemeral',
replace_original: false
});
};
}

if (parts.length != 2) {
winston.error(`Could not process action, could not parse callback_id`, payload);
return reply({
return {
text: `Could not parse callback_id "${payload.callback_id}"`,
response_type: 'ephemeral',
replace_original: false
});
};
}

const action = parts[0];
Expand All @@ -37,19 +37,19 @@ module.exports = async (request, reply) => {
]);

if (!topic) {
return reply({
return {
text: `Ooops, could not find that topic`,
response_type: 'ephemeral',
replace_original: false
});
};
}

if (!team) {
return reply({
return {
text: `Your slack team with id "${payload.team.id}" could not be found. Please try to add Poker Planner app to your slack team again.`,
response_type: 'ephemeral',
replace_original: false
});
};
}

switch (action) {
Expand All @@ -61,60 +61,60 @@ module.exports = async (request, reply) => {
winston.info(`[${team.name}(${team.id})] ${username}(${payload.user.id}) revealing votes ` +
`for "${topic.title}" w/ id: ${topic.id}`);
await Topic.revealTopicMessage(topic, team);
return reply();
return '';
} catch (err) {
winston.error(`Could not reveal topic, ${err}`);
return reply({
return {
text: `Could not reveal the topic. Internal server error, please try again later.`,
response_type: 'ephemeral',
replace_original: false
});
};
}
break;
/**
* VOTE
*/
case 'vote':
if (topic.isRevealed) {
return reply({
return {
text: `You cannot vote already revealed topic`,
response_type: 'ephemeral',
replace_original: false
});
};
}

if (!Topic.getParticipant(topic, payload.user.name)) {
return reply({
return {
text: `You are not a participant of that topic`,
response_type: 'ephemeral',
replace_original: false
});
};
}

try {
winston.info(`[${team.name}(${team.id})] ${username}(${payload.user.id}) voting ` +
`${payload.actions[0].value} points for "${topic.title}" w/ id: ${topic.id}`);
await Topic.vote(topic, team, payload.user.name, payload.actions[0].value);
return reply({
return {
text: `You voted ${payload.actions[0].value}`,
response_type: 'ephemeral',
replace_original: false
});
};
} catch (err) {
winston.error(`Could not vote, ${err}`);
return reply({
return {
text: `Could not vote. Internal server error, please try again later.`,
response_type: 'ephemeral',
replace_original: false
});
};
}
break;
default:
winston.error(`Unexpected action: "${action}"`);
return reply({
return {
text: `Unexpected action: "${action}"`,
response_type: 'ephemeral',
replace_original: false
});
};
}
};
2 changes: 1 addition & 1 deletion src/routes/index.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
module.exports = (request, reply) => {
reply.view('index', {
return reply.view('index', {
SLACK_CLIENT_ID: process.env.SLACK_CLIENT_ID,
SLACK_SCOPE: process.env.SLACK_SCOPE
});
Expand Down
8 changes: 4 additions & 4 deletions src/routes/oauth.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,19 +24,19 @@ module.exports = async (request, reply) => {
});

winston.info(`Added to team "${team.name}" (${team.id}) by user ${team.user_id}`);
reply('Success');
return 'Success';
} catch (err) {
winston.error(`Could not oauth, slack-side error - ${err}`);
reply(err);
return 'Internal server error, please try again later';
}
} else if (request.query.error) {
// Error
// Display error
winston.error(`Could not oauth, error from query - ${request.query.error}`);
reply(request.query.error);
return request.query.error;
} else {
// Unknown error
winston.error(`Could not oauth, unknown error`);
reply('Unknown error');
return 'Unknown error';
}
};
43 changes: 23 additions & 20 deletions src/routes/pp-command.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,59 +10,62 @@ module.exports = async (request, reply) => {

if (ppCommand.token != process.env.SLACK_VERIFICATION_TOKEN) {
winston.error(`Could not created topic, slack verification token is invalid`, ppCommand);
return reply({
return {
text: `Invalid slack verification token, please get in touch with the maintainer`,
response_type: 'ephemeral',
replace_original: false
});
};
}

if (!topic.title) {
return reply({
return {
text: `Topic cannot be empty`,
response_type: 'ephemeral',
replace_original: false
});
};
}

if (topic.ppCommand.channel_name == 'directmessage') {
return reply({
return {
text: `Poker planning cannot be started in direct messages`,
response_type: 'ephemeral',
replace_original: false
});
};
}

let team;
try {
team = await Team.get(ppCommand.team_id);
} catch (err) {
winston.error(`Could not created topic, could not get the team from db, ${err}`, ppCommand);
return reply({
return {
text: `Internal server error, please try again later`,
response_type: 'ephemeral',
replace_original: false
});
};
}

if (!team) {
winston.error(`Could not created topic, team could not be found`, ppCommand);
return reply({
return {
text: `Your slack team "${ppCommand.team_domain}" could not be found, please add Poker Planner app to your slack team again`,
response_type: 'ephemeral',
replace_original: false
});
};
}

reply();
// WTF HAPI???
setTimeout(async () => {
try {
winston.info(`[${team.name}(${team.id})] ${ppCommand.user_name}(${ppCommand.user_id}) creating ` +
`a topic with title "${topic.title}" on #${ppCommand.channel_name}(${ppCommand.channel_id}) ` +
`w/ ${topic.mentions.length} mention(s), id: ${topic.id}`);
await Topic.init(topic, team);
} catch (err) {
winston.error(`Could not created topic, ${err}`, ppCommand);
await Topic.rejectPPCommand(ppCommand, `Internal server error, please try again later`);
}
}, 0);

try {
winston.info(`[${team.name}(${team.id})] ${ppCommand.user_name}(${ppCommand.user_id}) creating ` +
`a topic with title "${topic.title}" on #${ppCommand.channel_name}(${ppCommand.channel_id}) ` +
`w/ ${topic.mentions.length} mention(s), id: ${topic.id}`);
await Topic.init(topic, team);
} catch (err) {
winston.error(`Could not created topic, ${err}`, ppCommand);
await Topic.rejectPPCommand(ppCommand, `Internal server error, please try again later`);
}
return '';
};
49 changes: 15 additions & 34 deletions src/server.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,45 +6,26 @@ const Handlebars = require('handlebars');
const inert = require('inert');


function create() {
const server = new Hapi.Server();
server.connection({ port: process.env.PORT });
server.register(Vision, (err) => {
if (err) {
winston.error(`Cannot register vision - ${err}`);
}

server.views({
engines: {
html: Handlebars
},
path: __dirname + '/views'
});
});
server.register(inert, (err) => {
if (err) {
winston.error(`Cannot register inert - ${err}`);
}
async function create() {
const server = new Hapi.Server({ port: process.env.PORT });

await server.register(Vision);
server.views({
engines: {
html: Handlebars
},
path: __dirname + '/views'
});
await server.register(inert);

return server;
}


function start(server) {
return new Promise((resolve, reject) => {
initRoutes(server);

server.start((err) => {
if (err) {
winston.error(`Could not start server - ${err}`);
return reject(err);
}

winston.info(`Server running at: ${server.info.uri}`);
resolve();
});
});
async function start(server) {
initRoutes(server);
await server.start();
winston.info(`Server running at: ${server.info.uri}`);
}


Expand All @@ -59,7 +40,7 @@ function initRoutes(server) {
method: 'GET',
path: path.join(process.env.BASE_PATH, 'demo.gif'),
handler: function (request, reply) {
reply.file('./demo.gif');
return reply.file('./demo.gif');
}
});

Expand Down

0 comments on commit 9f6d678

Please sign in to comment.