-
-
Notifications
You must be signed in to change notification settings - Fork 80
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
v2.4.5: Split Slack message into chunks.
- Loading branch information
1 parent
d02e082
commit ac5c6eb
Showing
8 changed files
with
212 additions
and
34 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
const getSlackCharsLimit = () => 39000; | ||
|
||
module.exports = { | ||
getSlackCharsLimit, | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
49 changes: 49 additions & 0 deletions
49
src/interactors/postSlackMessage/__tests__/splitInChunks.test.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
// const { getSlackCharsLimit } = require('../../../config'); | ||
const splitInChunks = require('../splitInChunks'); | ||
|
||
const buildBlock = (str, length) => `${str}`.padEnd(length, '.'); | ||
|
||
jest.mock('../../../config', () => ({ | ||
getSlackCharsLimit: () => 100, | ||
})); | ||
|
||
describe('Interactors | .postSlackMessage | .splitInChunks', () => { | ||
it('wraps block in array when length is ok', () => { | ||
const block1 = buildBlock('BLOCK 1', 10); | ||
const message = { | ||
blocks: [block1], | ||
}; | ||
const expectedChunks = [message]; | ||
const result = splitInChunks(message); | ||
expect(result).toEqual(expectedChunks); | ||
}); | ||
|
||
it('divides block in chunks when above length, keeping order', () => { | ||
const block1 = buildBlock('BLOCK 1', 15); | ||
const block2 = buildBlock('BLOCK 2', 15); | ||
const block3 = buildBlock('BLOCK 3', 120); | ||
const block4 = buildBlock('BLOCK 4', 60); | ||
const block5 = buildBlock('BLOCK 5', 50); | ||
const block6 = buildBlock('BLOCK 6', 10); | ||
const block7 = buildBlock('BLOCK 7', 10); | ||
const message = { | ||
blocks: [ | ||
block1, | ||
block2, | ||
block3, | ||
block4, | ||
block5, | ||
block6, | ||
block7, | ||
], | ||
}; | ||
const expectedChunks = [ | ||
{ blocks: [block1, block2] }, | ||
{ blocks: [block3] }, | ||
{ blocks: [block4] }, | ||
{ blocks: [block5, block6, block7] }, | ||
]; | ||
const result = splitInChunks(message); | ||
expect(result).toEqual(expectedChunks); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
const { getSlackCharsLimit } = require('../../config'); | ||
const { median } = require('../../utils'); | ||
|
||
const CHARS_LIMIT = getSlackCharsLimit(); | ||
|
||
const getSize = (obj) => JSON.stringify(obj).length; | ||
|
||
const getBlockLengths = (blocks) => blocks | ||
.filter(({ type }) => type === 'section') // Ignoring "divider" blocks | ||
.map((block) => getSize(block)); | ||
|
||
const getSizePerBlock = (blocks) => Math.round(median(getBlockLengths(blocks))); | ||
|
||
module.exports = (message) => { | ||
const blockSize = Math.max(1, getSizePerBlock(message.blocks)); | ||
|
||
const getBlocksToSplit = (blocks) => { | ||
const currentSize = getSize({ blocks }); | ||
const diff = currentSize - CHARS_LIMIT; | ||
if (diff < 0 || blocks.length === 1) return 0; | ||
|
||
const blocksSpace = Math.ceil(diff / blockSize); | ||
const blocksCount = Math.max(1, Math.min(blocks.length - 1, blocksSpace)); | ||
const firsts = blocks.slice(0, blocksCount); | ||
return getBlocksToSplit(firsts) || blocksCount; | ||
}; | ||
|
||
const getChunks = (prev, msg) => { | ||
const blocksToSplit = getBlocksToSplit(msg.blocks); | ||
if (!blocksToSplit) return [...prev, msg]; | ||
const blocks = msg.blocks.slice(0, blocksToSplit); | ||
const others = msg.blocks.slice(blocksToSplit); | ||
return getChunks([...prev, { blocks }], { blocks: others }); | ||
}; | ||
|
||
return getChunks([], message); | ||
}; |