Skip to content

Commit

Permalink
refactor: simplify code style at handlers.js
Browse files Browse the repository at this point in the history
  • Loading branch information
manuartero committed Mar 9, 2021
1 parent f57a4dd commit 61efbdd
Showing 1 changed file with 50 additions and 73 deletions.
123 changes: 50 additions & 73 deletions src/server/handlers.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,52 +12,6 @@ const { createConversationHelper } = require('./conversation-helper')
const createHandlers = (adapter, storage, bot) => {
const conversationHelper = createConversationHelper(adapter)

/**
* @param {import("restify").Request} req,
* @param {import("restify").Response} res
*/
const processMessage = (req, res) =>
adapter.processActivity(req, res, async turnContext => {
/* route to main dialog */
await bot.run(turnContext)
})

/**
* @param {string} user
* @param {string} message
* @param {boolean} mention
*/
const notify = async (user, message, mention) => {
const conversationRef = await storage.getConversation(user)
if (!conversationRef) {
throw new NotFoundError(`user not found: '${user}'`)
}
await conversationHelper.sendMessage(conversationRef, message, mention)
return conversationRef.conversation.id
}

/**
* @param {string} topic
* @param {string} message
* @param {boolean} mention
*/
const broadcast = async (topic, message, mention) => {
const subscribers = await storage.getSubscribers(topic)
const conversationKeys = []
for (const user of subscribers) {
const conversationRef = await storage.getConversation(user)
if (!conversationRef) {
log.warn(
`weird status: user "${user}" seems to be subscribed to "${topic}" but conversationRef not found. SKIPPING.`
)
} else {
conversationKeys.push(conversationRef.conversation.id)
conversationHelper.sendMessage(conversationRef, message, mention)
}
}
return conversationKeys
}

const getTopics = async () => {
const topicNames = await storage.listTopics()
/** @type {{[name: string]: string[]}} */
Expand All @@ -73,37 +27,60 @@ const createHandlers = (adapter, storage, bot) => {
return topics
}

const getUsers = async () => {
const users = await storage.listUsers()
return users
}
return {
processMessage: (req, res) =>
adapter.processActivity(req, res, async turnContext => {
/* route to main dialog */
await bot.run(turnContext)
}),

/**
* @param {string} topic
*/
const createTopic = async topic => {
await storage.registerTopic(topic) // may already exist
return getTopics()
}
notify: async (user, message, mention) => {
const conversationRef = await storage.getConversation(user)
if (!conversationRef) {
throw new NotFoundError(`user not found: '${user}'`)
}
await conversationHelper.sendMessage(conversationRef, message, mention)
return conversationRef.conversation.id
},

/**
* @param {string} user
* @param {string} topic
*/
const forceSubscription = async (user, topic) => {
await storage.subscribe(user, topic)
const subscribers = await storage.getSubscribers(topic)
return subscribers
}
broadcast: async (topic, message, includeMention) => {
const subscribers = await storage.getSubscribers(topic)
const conversationKeys = []
for (const user of subscribers) {
const conversationRef = await storage.getConversation(user)
if (!conversationRef) {
log.warn(
`weird status: user "${user}" seems to be subscribed to "${topic}" but conversationRef not found. SKIPPING.`
)
} else {
conversationKeys.push(conversationRef.conversation.id)
conversationHelper.sendMessage(
conversationRef,
message,
includeMention
)
}
}
return conversationKeys
},

return {
processMessage,
notify,
broadcast,
getTopics,
getUsers,
createTopic,
forceSubscription

getUsers: async () => {
const users = await storage.listUsers()
return users
},

createTopic: async topic => {
await storage.registerTopic(topic) // may already exist
return getTopics()
},

forceSubscription: async (user, topic) => {
await storage.subscribe(user, topic)
const subscribers = await storage.getSubscribers(topic)
return subscribers
}
}
}

Expand Down

0 comments on commit 61efbdd

Please sign in to comment.