-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
82 lines (66 loc) · 2.74 KB
/
index.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
'use strict';
let Firebase = require("firebase");
let email = require("emailjs");
let config = require('./config');
let server = email.server.connect({
user: config.username,
password: config.password,
host: "smtp.gmail.com", //Use your SMTP server if it is not Gmail
ssl: true //This option also depends on your SMTP server
});
let newPostsRef = new Firebase("https://hacker-news.firebaseio.com/v0/newstories/0");
newPostsRef.on("value", dealWithSinglePost);
function dealWithSinglePost (snapshot) {
let postRef = new Firebase("https://hacker-news.firebaseio.com/v0/item/"+snapshot.val());
postRef.on('value', filterInterestingPosts);
function filterInterestingPosts(postSnapshot) {
//Return if there is no actual valid data or no url
if(!postSnapshot.val() || !postSnapshot.val().url) {
return;
}
//If we have received some valid data, we unsubcribe from the post updates
postRef.off();
//An array of terms I want to subscribe to.
//We want to match the 'js' string in any part of the word (e.g. Node.js, jsTree).
//We want to add word boundaries to the other terms to limit false positives like 'Reactive' or 'Remember'
let terms = [
{term: 'js', fullWord: false},
{term: 'javascript', fullWord: true},
{term: 'react', fullWord: true},
{term: 'angular', fullWord: true},
{term: 'ember', fullWord: true},
{term: 'backbone', fullWord: true},
{term: 'jquery', fullWord: true}
].map(function (t) {
const WORD_BOUNDARY = '\\b'; //Escape the \b special character for word boundary
return t.fullWord ? WORD_BOUNDARY + t.term + WORD_BOUNDARY : t.term;
});
//Build a RegEx using the terms defined
let regEx = new RegExp(terms.join('|'), 'i');
let title = postSnapshot.val().title;
let url = postSnapshot.val().url;
let isAboutJs = regEx.test(title);
if (isAboutJs) {
sendEmail(title, url);
}
}
}
function sendEmail(title, url) {
let email = {
from: config.email,
to: config.email,
subject: `[JS on HN] ${title}`,
text: `A New Post About Javascript has been posted to Hacker News: ${title}\nRead it here: ${url}`,
attachment: [{
data: `<html>
<div>
<span>A New Post About Javascript has been posted to Hacker News:<span>
<strong>${title}</strong>
</div>
<div>Read it here: ${url}</div>
</html>`,
alternative:true
}]
};
server.send(email);
}