Skip to content
This repository has been archived by the owner on Oct 20, 2018. It is now read-only.

Commit

Permalink
Improved thanks more
Browse files Browse the repository at this point in the history
- fixed some messages
- formatted source a bit
  • Loading branch information
abhisekp committed May 18, 2016
1 parent 9476bd7 commit 04b9b6e
Showing 1 changed file with 60 additions and 60 deletions.
120 changes: 60 additions & 60 deletions lib/bot/cmds/thanks.js
Original file line number Diff line number Diff line change
@@ -1,24 +1,13 @@
'use strict';

const Utils = require('../../../lib/utils/Utils'),
HttpWrap = require('../../../lib/utils/HttpWrap'),
TextLib = require('../../../lib/utils/TextLib'),
dedent = require('dedent'),
_ = require('lodash');
const Utils = require('../../../lib/utils/Utils');
const HttpWrap = require('../../../lib/utils/HttpWrap');
const TextLib = require('../../../lib/utils/TextLib');
const dedent = require('dedent');
const _ = require('lodash');

const thanksCommands = {

// messages: {
// wikiHint: function(fromUser) {
// const wikiUrl = '(https://github.com/freecodecamp/' +
// 'freecodecamp/wiki/wiki-style-guide)';
//
// return '\n> hey @' + fromUser + ' if you found this info helpful ' +
// ':point_right: *[consider adding a wiki article!]' + wikiUrl + '*';
// }
// },

thanks: function(input, bot) {
thanks: function (input, bot) {
Utils.hasProperty(input, 'message', 'thanks expects an object');

const mentions = input.message.model.mentions;
Expand All @@ -30,56 +19,67 @@ const thanksCommands = {

const fromUser = input.message.model.fromUser.username;

const options = {
method: 'POST',
input,
bot
};

/*
* - filter the mentions by unique mentions
* - filter by removing the thanking user
* - grab the `screenName`
*/
const thankList = _.chain(mentions)
.uniq('screenName')
.filter((user) =>
.filter(user =>
user.screenName.toLowerCase() !== fromUser.toLowerCase()
&& !!user.userId
)
.map((user) => user.screenName)
.map('screenName')
.value();

thankList.forEach((toUser) => {
const apiPath = `/api/users/give-brownie-points?receiver=${toUser.toLowerCase()}&giver=${fromUser.toLowerCase()}`;
HttpWrap.callApi(apiPath, options, thanksCommands.showInfoCallback);
});

let message = '';

const THANKLIST_SIZE = thankList.length;
if (THANKLIST_SIZE > 0) {
const $symboledThankedList = _.chain(thankList)
.map((user) => `@${user}`);

const $lastUser = $symboledThankedList.last();
const $initialUsers = $symboledThankedList.take(THANKLIST_SIZE - 1);

let thankedUsersMsg = $initialUsers.value().join(', ');
// if more than one user is thanked
// then append an "and"
if (thankedUsersMsg !== '') {
thankedUsersMsg += ' and ';
}

thankedUsersMsg += $lastUser.value();
message += dedent`
> @${fromUser} sends brownie points to ${thankedUsersMsg} :sparkles: :thumbsup: :sparkles: `;
}

if (mentions.find(
(user) => user.screenName.toLowerCase() === fromUser.toLowerCase())
) {
message += dedent`
> sorry @${fromUser}, you can't send brownie points to yourself! :sparkles: :sparkles: `;
}

return message;
let message = '';

const THANKLIST_SIZE = thankList.length;
if (THANKLIST_SIZE > 0) {
const $symboledThankedList = _.chain(thankList).map(user => `@${user}`);

// get the last user
const $lastUser = $symboledThankedList.last();
const $initialUsers = $symboledThankedList.take(THANKLIST_SIZE - 1);

let thankedUsersMsg = $initialUsers.value().join(', ');
// if more than one user is thanked
// then append an "and"
if (thankedUsersMsg !== '') {
thankedUsersMsg += ' and ';
}

thankedUsersMsg += $lastUser.value();
message += dedent`\n
> @${fromUser} sends brownie points to ${thankedUsersMsg} \
:sparkles: :smiley: :thumbsup: :sparkles:
`;
}

if (mentions.find(
user => user.screenName.toLowerCase() === fromUser.toLowerCase()
)) {
message += dedent`\n
> sorry @${fromUser}, you can't send brownie points to yourself! \
:no_good: :no_good:
`;
}

const options = {
method: 'POST',
input,
bot
};

thankList.forEach(toUser => {
const apiPath = '/api/users/give-brownie-points'
+ `?receiver=${toUser.toLowerCase()}`
+ `&giver=${fromUser.toLowerCase()}`;
HttpWrap.callApi(apiPath, options, thanksCommands.showInfoCallback);
});

return message;
},

about: function(input, bot) {
Expand Down

0 comments on commit 04b9b6e

Please sign in to comment.