Skip to content

Commit

Permalink
Code refactoring
Browse files Browse the repository at this point in the history
Added locales
  • Loading branch information
pavloniym committed Mar 4, 2018
1 parent 8a8cb1c commit e011533
Show file tree
Hide file tree
Showing 22 changed files with 563 additions and 350 deletions.
5 changes: 5 additions & 0 deletions callbacks/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import meal from './meal'

export default {
meal
}
57 changes: 57 additions & 0 deletions callbacks/meal/action.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
import Meal from '../../helpers/meal'


export default async (DB, bot, {callback, chats_id, messages_id}) => {

/*
* Get meal
*/
const meal = await DB.models.Meals.get({meals_id: callback.meals_id});


switch (callback.value) {
case 'confirm':


/*
* Confirm meal only if Payer is defined
*/
if(meal.Payer) {

/*
* Update status to confirmed
*/
await meal.updateAttributes({confirmed: true});


/*
* Get text and keyboard's markup
* Update message
*/
const text = await Meal.text(meal);
bot.editMessageText(text, {
chat_id: chats_id,
message_id: messages_id,
parse_mode: 'HTML'
});
}

break;


case 'remove':

/*
* Remove meal
*/
if (meal) await meal.destroy();

/*
* Delete message
*/
bot.deleteMessage(chats_id, messages_id);

break;

}
}
17 changes: 17 additions & 0 deletions callbacks/meal/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import member from './member'
import action from './action'

export default async (DB, bot, {callback, message}) => {

const data = {callback, chats_id: message.chat.id, messages_id: message.message_id};
switch (callback.type) {

case 'member':
await member(DB, bot, data);
break;

case 'action':
await action(DB, bot, data);
break;
}
};
39 changes: 39 additions & 0 deletions callbacks/meal/member.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
import Meal from '../../helpers/meal'

export default async (DB, bot, {callback, chats_id, messages_id}) => {

/*
* Switch meal's member
*/
await DB.models.MealsMembers.switch({meals_id: callback.meals_id, members_id: callback.value});


/*
* Update meal's payer
*/
await DB.models.Meals.payer({meals_id: callback.meals_id});


/*
* Get meal
*/
const meal = await DB.models.Meals.get({meals_id: callback.meals_id});


/*
* Get text and keyboard's markup
*/
const text = await Meal.text(meal);
const keyboard = await Meal.keyboard(meal);


bot.editMessageText(text, {
chat_id: chats_id,
message_id: messages_id,
parse_mode: 'HTML',
reply_markup: JSON.stringify({
inline_keyboard: keyboard
})
});

}
24 changes: 24 additions & 0 deletions commands/clear.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import localize from "../helpers/localize";

/**
*
* Clear all previous meals from chat
*
* @param DB
* @param bot
* @param chats_id
* @returns {Promise<void>}
*/
export default async (DB, bot, {chats_id}) => {

/*
* Get all meals from chat
*/
await DB.models.Meals.clear({chats_id});


/*
* Send message about deletion
*/
bot.sendMessage(chats_id, localize('clear'));
}
7 changes: 7 additions & 0 deletions commands/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import Clear from './clear'
import Meal from './meal'

export default {
Clear,
Meal
}
41 changes: 41 additions & 0 deletions commands/meal.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
import Meal from './../helpers/meal'


/**
*
* Create meal instance
* Get group members as eaters
* Calculate today's payer
*
* @param DB
* @param bot
* @param chats_id
* @returns {Promise<void>}
*/
export default async (DB, bot, {chats_id}) => {


/*
* Create new meal instance in DB
* Re-query created meal because sequelize can't load relations after creating
*/
const meal = await DB.models.Meals.get({meals_id: (await DB.models.Meals.create({chats_id})).id});


/*
* Create text and keyboard markup
*/
const message = await Meal.text(meal);
const keyboard = await Meal.keyboard(meal);


/*
* Send message to chat with new meal
*/
bot.sendMessage(chats_id, message, {
parse_mode: 'HTML',
reply_markup: JSON.stringify({
inline_keyboard: keyboard
})
});
}
3 changes: 2 additions & 1 deletion config/custom-environment-variables.json
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
{
"hook": "WEB_HOOK",
"token": "BOT_TOKEN"
"token": "BOT_TOKEN",
"local": "LOCALE"
}
3 changes: 2 additions & 1 deletion config/default.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,6 @@
"port": 80,
"hook": null,
"token": null,
"db": "foodometer"
"db": "foodometer",
"locale": "en"
}
53 changes: 53 additions & 0 deletions events/callback_query.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
import callbacks from './../callbacks'
import get from 'lodash/get'


const callers = {
1: 'meal'
};

const types = {
1: 'member',
2: 'action'
};

const values = {
action: {
1: 'confirm',
2: 'remove'
}
};


export default async (DB, bot, query) => {

const data = query.data.split(':');

/*
* Decode callback
*/
const caller = callers[data[0]] || null;
const type = types[data[1]] || null;
const value = get(values, [type, data[2]], data[2] || null);
const meals_id = data[3] || null;
const callback = {caller, type, value, meals_id};


const message = query.message;


/*
* Process callback
* Get caller and process caller's callback
*/
switch (callback.caller) {
case 'meal': await callbacks.meal(DB, bot, {callback, message})
}


/*
* Answer to callback
*/
return bot.answerCallbackQuery({callback_query_id: query.id});

}
36 changes: 36 additions & 0 deletions events/group_chat_created.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
import localize from './../helpers/localize'

/**
*
* Add creator to chat member
* Welcome new member
*
* @param DB
* @param bot
* @param chats_id
* @param creator
* @returns {Promise<void>}
*/
export default async (DB, bot, {chats_id, creator}) => {


/*
* Collect creator's name and username
*/
const name = creator.first_name || null + creator.last_name || null;
const username = creator.username || null;


/*
* Add new member to db
*/
const member = await DB.models.Members.add({chats_id, users_id: creator.id, name, username});


/*
* Send message with welcome text
*/
bot.sendMessage(chats_id, localize('creation', {name: member.name}), {parse_mode: 'HTML'});


}
12 changes: 12 additions & 0 deletions events/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import group_chat_created from './group_chat_created'
import new_chat_members from './new_chat_members'
import left_chat_member from './left_chat_member'
import callback_query from './callback_query'


export default {
group_chat_created,
new_chat_members,
left_chat_member,
callback_query
}
21 changes: 21 additions & 0 deletions events/left_chat_member.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import localize from "../helpers/localize";

export default async (DB, bot, {chats_id, left_member}) => {

/*
* Find member in chat
*/
const member = await DB.models.Members.get({chats_id, users_id: left_member.id});


/*
* If member found - remove him from chat
*/
if (member) {

await member.destroy();
bot.sendMessage(chats_id, localize('left', {name: member.name}), {parse_mode: 'HTML'});

}

}
45 changes: 45 additions & 0 deletions events/new_chat_members.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
import localize from "../helpers/localize";

/**
*
* Add invited user to chat member
* Welcome new member
*
* @param DB
* @param bot
* @param chats_id
* @param members
* @returns {Promise<void>}
*/
export default async (DB, bot, {chats_id, members}) => {

/*
* Add each member
*/
members.forEach(async m => {


/*
* Collect member's name and username
*/
const name = m.first_name || null + m.last_name || null;
const username = m.username || null;

/*
* Try to find member in DB
* IF found - restore
* IF not - add new
*/
let member = await DB.models.Members.get({chats_id, users_id: m.id, paranoid: false});
if (member) await member.restore();
else member = await DB.models.Members.add({chats_id, users_id: m.id, name, username});


/*
* Send message with welcome text
*/
bot.sendMessage(chats_id, localize('welcome', {name: member.name}), {parse_mode: 'HTML'});

});

}
Loading

0 comments on commit e011533

Please sign in to comment.