Skip to content
Permalink
Browse files

4.3.0: Attachment bug fixes, polls and config -> dataPath

  • Loading branch information...
Bjornskjald committed Dec 15, 2018
2 parents b3b5e15 + fd35c71 commit 8e13f553a5e90bdc6f3d6576e5fac867e3af7816
@@ -3,7 +3,8 @@ name: Bug report
about: Create a report to help us improve

---

<!-- Please search existing issues to avoid creating duplicates. -->
<!-- Also please test using the latest version to make sure your issue has not already been fixed -->
**Describe the bug**
A clear and concise description of what the bug is.

@@ -21,9 +22,14 @@ A clear and concise description of what you expected to happen.
If applicable, add screenshots to help explain your problem.

**Specifications:**
- OS: [e.g. Windows 10]
- Version (`miscord -v`) [e.g. 3.2.2]
- Node.js version (`node -v`) [e.g. 8.9.4]
- OS:
<!-- e.g. Windows 10 -->

- Version:
<!-- "miscord -v", e.g. 4.2.0 -->

- Node.js version:
<!-- "node -v", e.g. 11.0.0 -->

**Additional context**
Add any other context about the problem here.

This file was deleted.

@@ -0,0 +1,7 @@
---
name: Other
about: |-
Issues are not a place for questions.
Ask them on Discord instead - https://discord.gg/DkmTvVz
---
@@ -1,5 +1,18 @@
# Changelog

## [4.3.0] - 2018-12-15
### Added
- Polls support
- Homebrew install instructions

### Changed
- `config` CLI parameter is now `dataPath` (see #370)

### Fixed
- XMA attachment bugs
- Getting sender object in the runtime
- Some typos

## [4.2.1] - 2018-12-05
### Fixed
- Config path being "undefined" on Node 9 and lower
@@ -5,4 +5,4 @@ RUN npm install --only=production -g miscord

VOLUME ["/config"]

CMD [ "miscord", "-c", "/config/config.json" ]
CMD [ "miscord" ]
@@ -22,7 +22,8 @@
# :wrench: Setup

## Installation
- [NPM install (Linux/macOS)](../../wiki/install#npm)
- [NPM install (Linux)](../../wiki/install#npm)
- [Homebrew install (macOS)](../../wiki/install#homebrew)
- [Binary packages (Windows)](../../releases/latest)
- [Docker install (headless servers)](../../wiki/install#docker)

@@ -1,13 +1,12 @@
const chalk = require('chalk')
const args = require('../lib/arguments').getHelp()
const miscord = chalk.green('miscord')

module.exports = `Miscord v${require('../package.json').version}
module.exports = `${chalk.green(`Miscord v${require('../package.json').version}`)}
Usage: miscord
--help [-h] ${chalk.cyan('shows this message')}
--version [-v] ${chalk.cyan('shows version')}
--config [-c] configPath ${chalk.cyan('reads config from custom path')}
--getConfigPath ${chalk.cyan('shows config path')}
Usage: ${miscord}
${args}
Example:
miscord -c /path/to/config.json
miscord -c D:\\Miscord\\config.json`
${miscord} ${chalk.magenta('-d')} /path/to/miscord-data-folder
${miscord} ${chalk.magenta('-d')} D:\\Miscord`
@@ -17,26 +17,30 @@ if (cluster.isMaster) {
const outdated = 'Hey! Your version of Node.JS seems outdated. Minimum version required: v8.5.0, your version: ' + process.version
if (!require('semver').gte(process.version, '8.5.0')) printAndExit(chalk.yellow(outdated))

const args = require('minimist')(process.argv.slice(2))
if (args.h || args.help) printAndExit(require('./help'))
if (args.v || args.version) printAndExit(require('../package.json').version)
if (args.getConfigPath) printAndExit(require('path').join(getConfigDir(), 'config.json'))
const args = require('../lib/arguments').getArgs()
if (args.help) printAndExit(require('./help'))
if (args.version) printAndExit(require('../package.json').version)
if (args.getPath) printAndExit(require('path').join(getConfigDir(), 'config.json'))

fork(args.c || args.config)
const configUnsupported = c => `The -c option is now deprecated.
Use --dataPath [-d] with your base folder (where your config is), probably ${require('path').parse(c).dir}.`
if (args.config) printAndExit(chalk.yellow(configUnsupported(args.config)))

fork(args.dataPath)

cluster.on('exit', (worker, code, signal) => {
logger.error(`Worker process ${worker.process.pid} died.`)
if ((new Date().getMilliseconds() - lastRunTime.getMilliseconds()) < (2 * 1000)) {
logger.fatal('Process crashed less than 2 seconds since the last launch, exiting.')
process.exit(1)
}
fork(args.c || args.config)
fork(args.dataPath)
})
} else {
logger.success(`Worker process ${process.pid} started.`)
const configPath = process.env.CONFIG !== 'undefined' ? process.env.CONFIG : undefined
require('../lib/logger.js').inject(configPath)
getConfig(configPath).then(miscord).catch(err => sendError(err))
const dataPath = process.env.DATA_PATH !== 'undefined' ? process.env.DATA_PATH : undefined
require('../lib/logger.js').inject(dataPath)
getConfig(dataPath).then(miscord).catch(err => sendError(err))

const catchError = error => {
if (!error) return
@@ -0,0 +1,45 @@
const chalk = require('chalk')
const fillMissingArgs = el => ({ ...el, args: el.args || '' })
const buildRow = ({ name, short, args, desc }) => `${chalk.yellow('--' + name)} [${chalk.magenta('-' + short)}] ${args} ${desc}`
const equalPad = param => (line, index, arr) => ({ ...line, [param]: line[param].padEnd(Math.max.apply(null, arr.map(el => el[param].length))) })
const a = [
{
name: 'help',
short: 'h',
desc: 'shows this message'
},
{
name: 'version',
short: 'v',
desc: 'shows version'
},
{ name: 'dataPath',
short: 'd',
args: '<path>',
desc: 'reads data from another folder'
},
{ name: 'getPath',
short: 'g',
desc: 'shows default data folder path'
},
{
name: 'config',
short: 'c',
args: '<path>',
unsupported: true
}
]

module.exports = {
getArgs: () => {
const args = require('minimist')(process.argv.slice(2))
let newArgs = {}
a.forEach(arg => {
if (args[arg.name] != null) newArgs[arg.name] = args[arg.name]
if (args[arg.short] != null) newArgs[arg.name] = args[arg.short]
if (newArgs[arg.name] != null && arg.args && typeof newArgs[arg.name] !== 'string') delete newArgs[arg.name]
})
return newArgs
},
getHelp: () => a.filter(a => !a.unsupported).map(equalPad('name')).map(fillMissingArgs).map(equalPad('args')).map(buildRow).join('\n ')
}
@@ -34,9 +34,9 @@ const defaultConfig = {
ignoredSequences: []
}

module.exports = async configPath => {
const config = await getConfig(configPath)
config.path = getConfigDir(configPath)
module.exports = async (dataPath = getConfigDir()) => {
const config = await getConfig(dataPath)
config.path = dataPath
config.logLevel = process.env.MISCORD_LOG_LEVEL || config.logLevel
logger.setLevel(config.logLevel || 'info')
// if any of the optional values is undefined, return default value
@@ -45,9 +45,9 @@ module.exports = async configPath => {

module.exports.defaultConfig = defaultConfig

function getConfig (configPath) {
function getConfig (dataPath) {
return new Promise(async (resolve, reject) => {
const configFile = path.join(getConfigDir(configPath), path.parse(configPath || 'config.json').base)
const configFile = path.join(dataPath, 'config.json')
log.info(`Using config at ${configFile}`)
try {
const data = await util.promisify(fs.readFile)(configFile, 'utf8')
@@ -72,6 +72,9 @@ function getConfig (configPath) {
} else {
fs.copyFileSync(example, configFile)
}
log.fatal(`Default config copied to ${configFile}
Fill it with data or use config generator here:
https://miscord.net/config-generator.html`)
}
})
}
@@ -1,13 +1,9 @@
const path = require('path')
const os = require('os')
let c
const isDocker = require('is-docker')

module.exports = configPath => {
if (configPath) {
c = configPath
return path.parse(configPath).dir
}
if (c) return path.parse(c).dir
module.exports = () => {
if (isDocker()) return '/config'
switch (process.platform) {
case 'win32':
return path.join(process.env.APPDATA, 'Miscord')
@@ -3,16 +3,12 @@ const handleMentions = require('../discord/handleMentions')
const emojiCount = require('./emojiCount')
const getAttachmentURL = require('./getAttachmentURL')
const thumbs = [ 369239263222822, 369239383222810, 369239343222814 ]
const parseMessengerMessage = require('./parseMessengerMessage')

module.exports = async (thread, sender, message) => {
// set description to message body, set author to message sender
const nickname = thread.nicknames ? thread.nicknames[message.authorId.toString()] : null
const authorName = nickname ? (config.discord.showFullNames ? `${nickname} (${sender.name})` : nickname) : sender.name

log.debug(`Nickname: ${nickname}, author name: ${authorName}`)
log.debug('content', message.message)

if (message.stickerId && thumbs.includes(message.stickerId)) message.message = '👍'
let authorName
({ authorName, message } = parseMessengerMessage(thread, sender, message))

let body = handleMentions(message.message)
if (body.length > 2000) body = body.slice(0, 1997) + '...'
@@ -25,11 +21,15 @@ module.exports = async (thread, sender, message) => {
if ((!message.attachments || !message.attachments.length) && !message.stickerId) return { body, opts }

const files = await Promise.all(
(message.attachments || []).map(async attach => ({
name: attach.filename,
attachment: await getAttachmentURL(message, attach),
size: attach.fileSize || 1
}))
(message.attachments || []).map(async attach => {
const url = await getAttachmentURL(message, attach)
if (!url) return {}
return {
name: attach.filename || require('url').parse(url).pathname.split('/').slice(-1)[0],
attachment: url,
size: attach.fileSize || 1
}
})
)

if (message.stickerId && !thumbs.includes(message.stickerId)) {
@@ -2,16 +2,12 @@ const downloadFile = require('./downloadFile')
const thumbs = [ 369239263222822, 369239383222810, 369239343222814 ]
const log = logger.withScope('createMessage:fromMessenger:messenger')
const getAttachmentURL = require('./getAttachmentURL')
const parseMessengerMessage = require('./parseMessengerMessage')

module.exports = async (thread, sender, message, source) => {
// set description to message body, set author to message sender
const nickname = thread.nicknames ? thread.nicknames[message.authorId.toString()] : null
const authorName = nickname ? (config.discord.showFullNames ? `${nickname} (${sender.name})` : nickname) : sender.name

log.debug(`Nickname: ${nickname}, author name: ${authorName}`)
log.debug('content', message.message)

if (message.stickerId && thumbs.includes(message.stickerId)) message.message = '👍'
let authorName
({ authorName, message } = parseMessengerMessage(thread, sender, message))

let body = config.messenger.format
.replace('{username}', authorName)
@@ -27,7 +23,7 @@ module.exports = async (thread, sender, message, source) => {
const url = await getAttachmentURL(message, attach)
if (!url) return
const { stream } = await downloadFile(url)
return { stream, extension: attach.mimeType }
return { stream, extension: attach.mimeType || require('path').parse(stream.path).ext }
})
)).filter(a => a)

@@ -8,8 +8,12 @@ module.exports = (message, attach) => {
attach = parsed[Object.keys(parsed)[0]]
const story = attach.story_attachment
if (!story) return
if (story.media &&
story.media.playable_url) return story.media.playable_url
if (story.media) {
const { media } = story
if (media.playable_url) return media.playable_url
if (media.image && media.image.uri) return media.image.uri
}

if (story.url) {
const url = parseStoryUrl(story)
if (url.match(/http[s]?:\/\/(www.)?google\.[a-z.]{2,7}\/url\?/)) {
@@ -0,0 +1,23 @@
const log = logger.withScope('createMessage:parseMessengerMessage')
const thumbs = [ 369239263222822, 369239383222810, 369239343222814 ]

module.exports = (thread, sender, message) => {
const nickname = thread.nicknames ? thread.nicknames[message.authorId.toString()] : null
const authorName = nickname ? (config.discord.showFullNames ? `${nickname} (${sender.name})` : nickname) : sender.name

log.debug(`Nickname: ${nickname}, author name: ${authorName}`)
log.debug('content', message.message)

if (message.stickerId && thumbs.includes(message.stickerId)) message.message = '👍'

if (message.message === '' && message.attachments && message.attachments.some(attach => attach.xmaGraphQL)) {
let attach = message.attachments.find(attach => attach.xmaGraphQL)
attach = JSON.parse(attach.xmaGraphQL)
attach = attach[Object.keys(attach)[0]]
const story = attach.story_attachment
if (story.description && story.description.text) message.message = story.description.text
if (story.title) message.message = story.title
}

return { authorName, message }
}
@@ -7,8 +7,8 @@ module.exports = async senderID => {
return messenger.senders.get(senderID)
} else {
log.debug('Getting sender from API')
const senders = await messenger.client.getUserInfo(senderID)
messenger.senders.set(senderID, senders[senderID])
return senders[senderID]
const sender = await messenger.client.getUserInfo(senderID)
messenger.senders.set(senderID, sender)
return sender
}
}
@@ -2,6 +2,7 @@ const log = logger.withScope('messenger:handleEvent')

const { getThread, getChannelName } = require('./index')
const handlePlan = require('./handlePlan')
const handlePoll = require('./handlePoll')

module.exports = async _event => {
if (!config.discord.showEvents) return
@@ -11,6 +12,7 @@ module.exports = async _event => {
if (type === 'readReceiptEvent' || type === 'deliveryReceiptEvent') return

if (type.startsWith('event')) return handlePlan(_event)
if (type.startsWith('poll')) return handlePoll(_event)

const thread = await getThread(event.threadId, _event.type !== '')
log.debug('Got Messenger thread')

0 comments on commit 8e13f55

Please sign in to comment.
You can’t perform that action at this time.