-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathformat-asset.js
49 lines (40 loc) · 1.37 KB
/
format-asset.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
"use strict";
const assetTypes = require('./assetTypes')
const baseUrl = process.env.V1_URL
if (!baseUrl)
throw new Error("V1_URL is not defined")
// see https://api.slack.com/docs/message-formatting#how_to_escape_characters
const escapes = {
'&': '&',
'<': '<',
'>': '>',
}
const escape = char => escapes[char]
const slackEscape = text => text.replace(/[&<>]/g, escape)
const logicalState = (numericState) =>
numericState >= 255? 'deleted':
numericState >= 192? 'template':
numericState >= 128? 'closed':
numericState >= 64? 'open':
'future'
function formatAsset(asset) {
if (!asset) return null
const state = logicalState(asset.state)
const type = assetTypes.localize(asset.assetType) + (state === 'template'? ' Template': '')
const number = `*${asset.number}*`;
const decorator = (state === 'deleted' || state === 'closed')? ` (${state})`: ''
const href = `${baseUrl}/assetdetail.v1?Number=${asset.number}`
const title = slackEscape(asset.title)
const link = `<${href}|${title}>`
const styledLink = (state === 'deleted' || state === 'closed')? `~${link}~`: link
return `${type} ${number}${decorator} ${styledLink}`
}
function formatSearch(q) {
const query = slackEscape(encodeURIComponent(q))
const href = `${baseUrl}/Search.mvc/Advanced?q=${query}`
return `<${href}|More...>`
}
module.exports = {
asset: formatAsset,
search: formatSearch,
}