2019-08-21 11:37:55 +00:00
|
|
|
const nonAtomsTuples = ['replace', ':replace']
|
|
|
|
const nonAtomsObjects = ['match_actor', ':match_actor']
|
2019-08-24 11:55:47 +00:00
|
|
|
const objects = ['digest', 'pleroma_fe', 'masto_fe', 'poll_limits', 'styling']
|
2019-08-21 11:37:55 +00:00
|
|
|
const objectParents = ['mascots']
|
2019-08-09 16:45:25 +00:00
|
|
|
|
2019-12-24 14:00:32 +00:00
|
|
|
const getValueWithoutKey = (key, [type, value]) => {
|
|
|
|
if (type === 'atom' && value.length > 1) {
|
|
|
|
return `:${value}`
|
|
|
|
} else if (key === ':backends') {
|
|
|
|
const index = value.findIndex(el => el === ':ex_syslogger')
|
|
|
|
const updatedArray = value.slice()
|
|
|
|
if (index !== -1) {
|
|
|
|
updatedArray[index] = { 'tuple': ['ExSyslogger', ':ex_syslogger'] }
|
|
|
|
}
|
|
|
|
return updatedArray
|
|
|
|
}
|
|
|
|
return value
|
|
|
|
}
|
|
|
|
|
2019-12-23 23:12:41 +00:00
|
|
|
export const parseNonTuples = (key, value) => {
|
|
|
|
if (key === ':backends') {
|
2019-12-24 14:00:32 +00:00
|
|
|
const index = value.findIndex(el => typeof el === 'object' && el.tuple.includes(':ex_syslogger'))
|
2019-12-23 23:12:41 +00:00
|
|
|
const updated = value.map((el, i) => i === index ? ':ex_syslogger' : el)
|
|
|
|
return { value: updated }
|
|
|
|
}
|
|
|
|
return { value }
|
|
|
|
}
|
2019-08-09 16:45:25 +00:00
|
|
|
// REFACTOR
|
|
|
|
export const parseTuples = (tuples, key) => {
|
|
|
|
return tuples.reduce((accum, item) => {
|
|
|
|
if (key === 'rate_limit') {
|
2019-11-08 12:39:22 +00:00
|
|
|
accum[item.tuple[0]] = item.tuple[1]
|
2019-12-13 23:06:52 +00:00
|
|
|
} else if (item.tuple[0] === ':mascots') {
|
|
|
|
accum[item.tuple[0]] = item.tuple[1].reduce((acc, mascot) => {
|
|
|
|
return [...acc, { [mascot.tuple[0]]: { ...mascot.tuple[1], id: `f${(~~(Math.random() * 1e8)).toString(16)}` }}]
|
|
|
|
}, [])
|
2019-12-22 00:46:06 +00:00
|
|
|
} else if (item.tuple[0] === ':groups' || item.tuple[0] === ':replace' || item.tuple[0] === ':retries') {
|
2019-12-13 19:11:29 +00:00
|
|
|
accum[item.tuple[0]] = item.tuple[1].reduce((acc, group) => {
|
|
|
|
return [...acc, { [group.tuple[0]]: { value: group.tuple[1], id: `f${(~~(Math.random() * 1e8)).toString(16)}` }}]
|
|
|
|
}, [])
|
2019-12-20 20:17:39 +00:00
|
|
|
} else if (item.tuple[0] === ':match_actor') {
|
|
|
|
accum[item.tuple[0]] = Object.keys(item.tuple[1]).reduce((acc, regex) => {
|
|
|
|
return [...acc, { [regex]: { value: item.tuple[1][regex], id: `f${(~~(Math.random() * 1e8)).toString(16)}` }}]
|
|
|
|
}, [])
|
2019-12-15 18:37:55 +00:00
|
|
|
} else if (item.tuple[0] === ':icons') {
|
|
|
|
accum[item.tuple[0]] = item.tuple[1].map(icon => {
|
|
|
|
return Object.keys(icon).map(name => {
|
|
|
|
return { key: name, value: icon[name], id: `f${(~~(Math.random() * 1e8)).toString(16)}` }
|
|
|
|
})
|
|
|
|
}, [])
|
2019-12-21 21:52:42 +00:00
|
|
|
} else if (item.tuple[0] === ':prune') {
|
|
|
|
accum[item.tuple[0]] = item.tuple[1] === ':disabled' ? [item.tuple[1]] : item.tuple[1].tuple
|
2019-12-17 19:43:40 +00:00
|
|
|
} else if (item.tuple[0] === ':proxy_url') {
|
|
|
|
accum[item.tuple[0]] = parseProxyUrl(item.tuple[1])
|
2019-11-11 13:47:54 +00:00
|
|
|
} else if ((item.tuple[0] === ':sslopts' && item.tuple[1].length === 0) || // should be removed
|
|
|
|
(item.tuple[0] === ':tlsopts' && item.tuple[1].length === 0)) {
|
|
|
|
accum[item.tuple[0]] = {}
|
2019-08-09 16:45:25 +00:00
|
|
|
} else if (Array.isArray(item.tuple[1]) &&
|
2019-11-11 13:47:54 +00:00
|
|
|
(typeof item.tuple[1][0] === 'object' && !Array.isArray(item.tuple[1][0])) && item.tuple[1][0]['tuple']) {
|
2019-12-09 15:40:33 +00:00
|
|
|
accum[item.tuple[0]] = parseTuples(item.tuple[1], item.tuple[0])
|
2019-11-17 14:06:31 +00:00
|
|
|
} else if (Array.isArray(item.tuple[1])) {
|
2019-08-09 16:45:25 +00:00
|
|
|
nonAtomsTuples.includes(item.tuple[0])
|
2019-11-08 12:39:22 +00:00
|
|
|
? accum[item.tuple[0]] = parseNonAtomTuples(item.tuple[1])
|
2019-11-17 14:06:31 +00:00
|
|
|
: accum[item.tuple[0]] = item.tuple[1]
|
2019-11-11 13:47:54 +00:00
|
|
|
} else if (item.tuple[0] === ':ip') {
|
|
|
|
accum[item.tuple[0]] = item.tuple[1].tuple.join('.')
|
2019-08-09 16:45:25 +00:00
|
|
|
} else if (item.tuple[1] && typeof item.tuple[1] === 'object' && 'tuple' in item.tuple[1]) {
|
2019-11-11 13:47:54 +00:00
|
|
|
accum[item.tuple[0]] = { [item.tuple[1].tuple[0]]: item.tuple[1].tuple[1] }
|
2019-08-21 17:48:30 +00:00
|
|
|
} else if (item.tuple[1] && typeof item.tuple[1] === 'object') {
|
|
|
|
nonAtomsObjects.includes(item.tuple[0])
|
2019-11-08 12:39:22 +00:00
|
|
|
? accum[item.tuple[0]] = parseNonAtomObject(item.tuple[1])
|
2019-12-09 15:40:33 +00:00
|
|
|
: accum[item.tuple[0]] = parseObject(item.tuple[1])
|
2019-08-09 16:45:25 +00:00
|
|
|
} else {
|
2019-12-06 06:50:23 +00:00
|
|
|
accum[item.tuple[0]] = item.tuple[1]
|
2019-08-09 16:45:25 +00:00
|
|
|
}
|
|
|
|
return accum
|
|
|
|
}, {})
|
|
|
|
}
|
|
|
|
|
|
|
|
const parseNonAtomTuples = (tuples) => {
|
|
|
|
return tuples.reduce((acc, item) => {
|
|
|
|
acc[item.tuple[0]] = item.tuple[1]
|
|
|
|
return acc
|
|
|
|
}, {})
|
2019-08-21 17:48:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
const parseNonAtomObject = (object) => {
|
|
|
|
return Object.keys(object).reduce((acc, item) => {
|
|
|
|
acc[item] = object[item]
|
|
|
|
return acc
|
|
|
|
}, {})
|
|
|
|
}
|
|
|
|
|
2019-12-09 15:40:33 +00:00
|
|
|
const parseObject = object => {
|
|
|
|
return Object.keys(object).reduce((acc, item) => {
|
|
|
|
acc[item] = object[item]
|
|
|
|
return acc
|
|
|
|
}, {})
|
2019-08-09 16:45:25 +00:00
|
|
|
}
|
|
|
|
|
2019-12-17 19:43:40 +00:00
|
|
|
const parseProxyUrl = value => {
|
2019-12-18 19:56:18 +00:00
|
|
|
if (value && !Array.isArray(value) &&
|
|
|
|
typeof value === 'object' &&
|
|
|
|
value.tuple.length === 3 &&
|
|
|
|
value.tuple[0] === ':socks5') {
|
2019-12-17 19:43:40 +00:00
|
|
|
const [, host, port] = value.tuple
|
|
|
|
return { socks5: true, host, port }
|
|
|
|
} else if (typeof value === 'string') {
|
|
|
|
const [host, port] = value.split(':')
|
|
|
|
return { socks5: false, host, port }
|
|
|
|
}
|
|
|
|
return { socks5: false, host: null, port: null }
|
|
|
|
}
|
|
|
|
|
2019-12-10 21:24:43 +00:00
|
|
|
export const partialUpdate = (group, key) => {
|
2019-12-22 00:46:06 +00:00
|
|
|
if ((group === ':pleroma' && key === 'Oban') ||
|
|
|
|
(group === ':mime' && key === ':types') ||
|
|
|
|
(group === ':auto_linker' && key === ':opts')) {
|
2019-12-10 21:24:43 +00:00
|
|
|
return false
|
|
|
|
}
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
2019-08-09 16:45:25 +00:00
|
|
|
export const valueHasTuples = (key, value) => {
|
2019-12-24 14:00:32 +00:00
|
|
|
const valueIsArrayOfNonObjects = Array.isArray(value) && value.length > 0 && value.every(el => typeof el !== 'object')
|
2019-11-08 12:39:22 +00:00
|
|
|
return key === ':meta' ||
|
|
|
|
key === ':types' ||
|
2019-12-24 14:00:32 +00:00
|
|
|
key === ':backends' ||
|
2019-11-08 12:39:22 +00:00
|
|
|
key === ':compiled_template_engines' ||
|
|
|
|
key === ':compiled_format_encoders' ||
|
2019-08-09 16:45:25 +00:00
|
|
|
typeof value === 'string' ||
|
|
|
|
typeof value === 'number' ||
|
|
|
|
typeof value === 'boolean' ||
|
2019-11-08 12:39:22 +00:00
|
|
|
value === null ||
|
2019-08-09 16:45:25 +00:00
|
|
|
valueIsArrayOfNonObjects
|
|
|
|
}
|
|
|
|
|
2019-12-19 16:21:44 +00:00
|
|
|
export const wrapUpdatedSettings = (group, settings, currentState) => {
|
2019-12-03 16:19:46 +00:00
|
|
|
return Object.keys(settings).map((key) => {
|
2019-12-24 14:00:32 +00:00
|
|
|
return settings[key]._value
|
|
|
|
? { group, key, value: getValueWithoutKey(key, settings[key]._value) }
|
|
|
|
: { group, key, value: wrapValues(settings[key], currentState[group][key]) }
|
2019-12-03 16:19:46 +00:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2019-12-19 16:21:44 +00:00
|
|
|
const wrapValues = (settings, currentState) => {
|
2019-12-03 16:19:46 +00:00
|
|
|
return Object.keys(settings).map(setting => {
|
2019-12-09 08:30:53 +00:00
|
|
|
const [type, value] = Array.isArray(settings[setting]) ? settings[setting] : ['', settings[setting]]
|
2019-12-20 20:17:39 +00:00
|
|
|
if (type === 'keyword' || type.includes('keyword') || setting === ':replace') {
|
2019-12-22 00:46:06 +00:00
|
|
|
return { 'tuple': [setting, wrapValues(value, currentState)] }
|
2019-12-18 19:56:18 +00:00
|
|
|
} else if (type === 'atom' && value.length > 0) {
|
2019-12-04 14:17:05 +00:00
|
|
|
return { 'tuple': [setting, `:${value}`] }
|
2019-12-17 19:43:40 +00:00
|
|
|
} else if (type.includes('tuple') && Array.isArray(value)) {
|
|
|
|
return { 'tuple': [setting, { 'tuple': value }] }
|
2019-12-06 08:30:17 +00:00
|
|
|
} else if (type === 'map') {
|
2019-12-19 16:21:44 +00:00
|
|
|
const mapValue = Object.keys(value).reduce((acc, key) => {
|
2019-12-20 20:17:39 +00:00
|
|
|
acc[key] = setting === ':match_actor' ? value[key] : value[key][1]
|
2019-12-06 08:30:17 +00:00
|
|
|
return acc
|
|
|
|
}, {})
|
2019-12-20 20:17:39 +00:00
|
|
|
const mapCurrentState = setting === ':match_actor'
|
|
|
|
? currentState[setting].reduce((acc, element) => {
|
|
|
|
return { ...acc, ...{ [Object.keys(element)[0]]: Object.values(element)[0].value }}
|
|
|
|
}, {})
|
|
|
|
: currentState[setting]
|
|
|
|
return { 'tuple': [setting, { ...mapCurrentState, ...mapValue }] }
|
2019-12-06 06:50:23 +00:00
|
|
|
} else if (setting === ':ip') {
|
|
|
|
const ip = value.split('.').map(s => parseInt(s, 10))
|
|
|
|
return { 'tuple': [setting, { 'tuple': ip }] }
|
2019-12-18 14:36:40 +00:00
|
|
|
} else if (setting === ':ssl_options') {
|
2019-12-22 00:46:06 +00:00
|
|
|
return { 'tuple': [setting, wrapValues(value, currentState)] }
|
2019-12-03 16:19:46 +00:00
|
|
|
} else {
|
|
|
|
return { 'tuple': [setting, value] }
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2019-08-09 16:45:25 +00:00
|
|
|
const wrapNestedTuples = setting => {
|
|
|
|
return Object.keys(setting).reduce((acc, settingName) => {
|
|
|
|
const data = setting[settingName]
|
2019-08-09 17:05:43 +00:00
|
|
|
if (data === null || data === '') {
|
2019-08-09 16:45:25 +00:00
|
|
|
return acc
|
|
|
|
} else if (settingName === 'ip') {
|
2019-08-21 11:37:55 +00:00
|
|
|
const ip = data.split('.').map(s => parseInt(s, 10))
|
2019-08-09 16:45:25 +00:00
|
|
|
return [...acc, { 'tuple': [`:${settingName}`, { 'tuple': ip }] }]
|
2019-08-21 11:37:55 +00:00
|
|
|
} else if (Array.isArray(data) || typeof data !== 'object') {
|
|
|
|
return [...acc, { 'tuple': [`:${settingName}`, data] }]
|
|
|
|
} else if (nonAtomsObjects.includes(settingName)) {
|
|
|
|
return [...acc, { 'tuple': [`:${settingName}`, wrapNonAtomsObjects(data)] }]
|
|
|
|
} else if (objectParents.includes(settingName)) {
|
|
|
|
return [...acc, { 'tuple': [`:${settingName}`, wrapNestedObjects(data)] }]
|
|
|
|
} else if (objects.includes(settingName)) {
|
|
|
|
return [...acc, { 'tuple': [`:${settingName}`, wrapObjects(data)] }]
|
|
|
|
} else if (nonAtomsTuples.includes(settingName)) {
|
|
|
|
return [...acc, { 'tuple': [`:${settingName}`, wrapNonAtomsTuples(data)] }]
|
|
|
|
} else {
|
2019-08-09 16:45:25 +00:00
|
|
|
return [...acc, { 'tuple': [`:${settingName}`, wrapNestedTuples(data)] }]
|
|
|
|
}
|
|
|
|
}, [])
|
|
|
|
}
|
|
|
|
|
|
|
|
const wrapNonAtomsTuples = setting => {
|
|
|
|
return Object.keys(setting).reduce((acc, settingName) => {
|
|
|
|
return [...acc, { 'tuple': [`${settingName}`, setting[settingName]] }]
|
|
|
|
}, [])
|
|
|
|
}
|
|
|
|
|
2019-08-21 11:37:55 +00:00
|
|
|
const wrapNestedObjects = setting => {
|
|
|
|
return Object.keys(setting).reduce((acc, settingName) => {
|
|
|
|
return [...acc, { 'tuple': [`:${settingName}`, wrapObjects(setting[settingName])] }]
|
|
|
|
}, [])
|
|
|
|
}
|
|
|
|
|
|
|
|
const wrapNonAtomsObjects = setting => {
|
|
|
|
return Object.keys(setting).reduce((acc, settingName) => {
|
|
|
|
return { ...acc, [`${settingName}`]: setting[settingName] }
|
|
|
|
}, {})
|
|
|
|
}
|
|
|
|
|
|
|
|
const wrapObjects = setting => {
|
|
|
|
return Object.keys(setting).reduce((acc, settingName) => {
|
|
|
|
return { ...acc, [`:${settingName}`]: setting[settingName] }
|
|
|
|
}, {})
|
|
|
|
}
|