Skip to content

Commit

Permalink
Fix channel topics not being set after channel creation.
Browse files Browse the repository at this point in the history
  • Loading branch information
ian-deans authored and SJ Pratt committed Aug 9, 2017
1 parent 03b26b9 commit daba5ab
Showing 1 changed file with 18 additions and 32 deletions.
50 changes: 18 additions & 32 deletions src/server/services/chatService/cache.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import cacheManager from 'cache-manager'

import Promise from 'bluebird'
import getChannelList from './getChannelList'
import getUserList from './getUserList'
import {usernameFor} from './util'
Expand All @@ -9,42 +9,28 @@ const memoryCache = cacheManager.caching({
ttl: 5 * 60, // seconds
})

export async function getUserId(userHandle) {
const userIdMap = await _getUserIdMap()
return userIdMap.get(usernameFor(userHandle))
}

export async function getChannelId(channelName) {
const channelIdMap = await _getChannelIdMap()
return channelIdMap.get(channelName)
}
const USER_KEY = 'user_'
const CHANNEL_KEY = 'channel_'

function _getUserIdMap() {
return memoryCache.wrap('userIdMap', () => {
return _getUserIdMapUncached()
export async function getUserId(userHandle) {
const userName = usernameFor(userHandle)
const userKey = _buildKey(USER_KEY, userName)
return memoryCache.wrap(userKey, async () => {
const users = (await getUserList()).members
await Promise.each(users, ({name, id}) => memoryCache.set(_buildKey(USER_KEY, name), id))
return memoryCache.get(userKey)
})
}

function _getChannelIdMap() {
return memoryCache.wrap('channelIdMap', () => {
return _getChannelIdMapUncached()
export async function getChannelId(channelName) {
const channelKey = _buildKey(CHANNEL_KEY, channelName)
return memoryCache.wrap(channelKey, async () => {
const channels = (await getChannelList()).channels
await Promise.each(channels, ({name, id}) => memoryCache.set(_buildKey(CHANNEL_KEY, name), id))
return memoryCache.get(channelKey)
})
}

async function _getUserIdMapUncached() {
return await _getIdMapUncached(getUserList, 'members')
}

async function _getChannelIdMapUncached() {
return await _getIdMapUncached(getChannelList, 'channels')
}

async function _getIdMapUncached(getListFromAPI, attrName) {
const apiResult = await getListFromAPI()
const list = apiResult[attrName]
const map = list.reduce((result, {id, name}) => {
result.set(name, id)
return result
}, new Map())
return map
function _buildKey(keyType, name) {
return keyType + name.toLowerCase()
}

0 comments on commit daba5ab

Please sign in to comment.