-
Notifications
You must be signed in to change notification settings - Fork 2
/
formatter.js
95 lines (76 loc) · 2.48 KB
/
formatter.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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
var moment = require("moment");
var twttr = require('twitter-text');
module.exports = function(webhook, customFormat, cb){
if(!webhook) return cb('should provide a webhook', null)
var label = webhook.webhook.label;
doFormat(label)
//different formatters depending on label hook(format)
function doFormat(format) {
var formats = {
'talk proposal': function talk () {
var talks = webhook.event.talks
, eventTwitter = webhook.event.twitter
, eventDate = moment(webhook.event.date).format('L')
, eventURL = webhook.event.url
, testTweetFails
, description= '';
talks.map(function(talk){
var talkDescription = talk.description;
if (customFormat !== undefined && customFormat === 'custom') {
description = "LNUG talk - @" + talk.speaker.twitter + " - " + talk.title;
}
else {
description = 'New talk:';
//start text creation
description += ' ' + eventURL;
description += ' ' + talkDescription.slice(0, getRemainingChars(description)) + '...'
}
//check tweet
checkTweet(description, function(err, res){
if(err) return cb(err, null)
//send back our correct formatted tweet
cb(null, description)
})
})
},
'jobs': function jobs () {
var jobs = webhook.jobs
, description = ''
jobs.map(function(job){
var company = job.company
, jobDescription = job.description;
description = 'New job offer:';
description += ' ' + company
description += ' ' + twttr.htmlEscape(jobDescription.slice(0, getRemainingChars(description))) + '...'
//check tweet
checkTweet(description, function(err, res){
if(err) return cb(err, null)
//send back our correct formatted tweet
cb(null, description)
})
})
}
}
if(typeof formats[format] != 'function'){
return cb("uknown label wook", null);
}
return formats[format]()
}
function getRemainingChars(text){
return 140 - 4 - twttr.getTweetLength(text);
}
//check if our tweet will be accepted bt the twitter api
function checkTweet(text, cb){
var testTweetFails = twttr.isInvalidTweet(text)
if(testTweetFails) return cb(testTweetFails, null)
return cb(null)
}
//just to create a different tweet than previous
function makeid(){
var text = "";
var possible = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789";
for( var i=0; i < 5; i++ )
text += possible.charAt(Math.floor(Math.random() * possible.length));
return text;
}
}