|
| 1 | +var debug = require('debug')('hackafe-events'); |
| 2 | +var config = require('./config'); |
| 3 | +var Promise = require('promise'); |
| 4 | +var get = require('simple-get'); |
| 5 | + |
| 6 | +var FACEBOOK_EVENT_PATTERN = /((https?:)?\/\/(www.)?facebook\.com\/events\/\d+)/i; |
| 7 | +var FORUM_THREAD_PATTERN = /((https?:)?\/\/frm\.hackafe\.org\/t\/[-a-zA-Z0-9]+\/\d+)/i; |
| 8 | + |
| 9 | +var labelCache = {}; |
| 10 | + |
| 11 | +module.exports = function (opts, cb) { |
| 12 | + if (typeof opts === 'function') { |
| 13 | + cb = opts; |
| 14 | + opts = {}; |
| 15 | + } |
| 16 | + |
| 17 | + return getBoardVisibleCards(config.boardId) |
| 18 | + .then(function (cards) { |
| 19 | + return Promise.all(cards.map(transform)); |
| 20 | + }) |
| 21 | + .then(function (events) { |
| 22 | + return events.filter(upcomming); |
| 23 | + }) |
| 24 | + .nodeify(cb); |
| 25 | +}; |
| 26 | + |
| 27 | +function tfetch(opts) { |
| 28 | + if (typeof opts === 'string') opts = {url: opts}; |
| 29 | + opts.url += '?key=' + config.apiKey; |
| 30 | + opts.json = true; |
| 31 | + return new Promise(function (resolve, reject) { |
| 32 | + debug('fetching %s', opts.url); |
| 33 | + get.concat(opts, function (err, res, data) { |
| 34 | + if (err) return reject(err); |
| 35 | + debug('fetched %s', opts.url); |
| 36 | + resolve(data); |
| 37 | + }); |
| 38 | + }); |
| 39 | +} |
| 40 | + |
| 41 | +function getBoardVisibleCards(boardId) { |
| 42 | + return tfetch('https://api.trello.com/1/boards/' + config.boardId + '/cards/visible'); |
| 43 | +} |
| 44 | + |
| 45 | +function getCardAttachment(cardId, attachmentId) { |
| 46 | + return tfetch('https://api.trello.com/1/cards/' + cardId + '/attachments/' + attachmentId); |
| 47 | +} |
| 48 | + |
| 49 | +function getLabel(labelId) { |
| 50 | + return labelCache[labelId] = labelCache[labelId] || tfetch('https://api.trello.com/1/labels/' + labelId); |
| 51 | +} |
| 52 | + |
| 53 | +function transform(card) { |
| 54 | + return Promise.resolve({ |
| 55 | + id: card.id, |
| 56 | + desc: card.desc, |
| 57 | + name: card.name, |
| 58 | + start: card.due && new Date(card.due), |
| 59 | + url: card.url |
| 60 | + }) |
| 61 | + .then(function fetchCover(event) { |
| 62 | + if (!card.idAttachmentCover) return event; |
| 63 | + return getCardAttachment(card.id, card.idAttachmentCover) |
| 64 | + .then(function (attachment) { |
| 65 | + event.cover = attachment.url; |
| 66 | + return event; |
| 67 | + }) |
| 68 | + }) |
| 69 | + .then(function extractFacebookEvent(event) { |
| 70 | + var match = event.desc && |
| 71 | + event.desc.match(FACEBOOK_EVENT_PATTERN); |
| 72 | + if (match) |
| 73 | + event.facebookEvent = match[0]; |
| 74 | + return event; |
| 75 | + }) |
| 76 | + .then(function extractForumThread(event) { |
| 77 | + var match = event.desc && |
| 78 | + event.desc.match(FORUM_THREAD_PATTERN); |
| 79 | + if (match) |
| 80 | + event.forumThread = match[0]; |
| 81 | + return event; |
| 82 | + }) |
| 83 | + .then(function resolveLabels(event) { |
| 84 | + if (!card.idLabels) return event; |
| 85 | + return Promise.all(card.idLabels.map(getLabel)) |
| 86 | + .then(function (labels) { |
| 87 | + event.labels = labels; |
| 88 | + return event; |
| 89 | + }); |
| 90 | + }) |
| 91 | +} |
| 92 | + |
| 93 | +function upcomming(event) { |
| 94 | + return event.start && event.start.getTime() >= new Date().getTime(); |
| 95 | +} |
0 commit comments