This repository has been archived by the owner on Jan 14, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 6
/
twitter.js
148 lines (148 loc) · 5.68 KB
/
twitter.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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
'use strict';
Object.defineProperty(exports, "__esModule", { value: true });
// Packages
const twemoji = require("twemoji");
const io = require("socket.io-client");
// Ours
const nodecgApiContext = require("./util/nodecg-api-context");
const nodecg = nodecgApiContext.get();
const log = new nodecg.Logger(`${nodecg.bundleName}:twitter`);
const tweets = nodecg.Replicant('tweets');
const fanartTweets = nodecg.Replicant('fanartTweets');
// Clear queue of tweets when currentRun changes
nodecg.Replicant('currentRun').on('change', (newVal, oldVal) => {
if (oldVal && newVal.pk !== oldVal.pk) {
tweets.value = [];
}
});
nodecg.listenFor('acceptTweet', (tweet) => {
if (!nodecg.bundleConfig.twitter.debug) {
removeTweetById(tweet.id_str);
}
nodecg.sendMessage('showTweet', tweet);
});
nodecg.listenFor('acceptFanart', (tweet) => {
if (!nodecg.bundleConfig.twitter.debug) {
removeTweetById(tweet.id_str);
}
nodecg.sendMessage('showFanart', tweet);
});
nodecg.listenFor('rejectTweet', removeTweetById);
const socket = io.connect(nodecg.bundleConfig.twitter.websocketUrl);
socket.on('connect', () => {
socket.on('authenticated', () => {
log.info('Twitter socket authenticated.');
});
socket.on('unauthorized', (err) => {
log.error('There was an error with the authentication:', (err && err.message) ? err.message : err);
});
socket.on('twitter-webhook-payload', (payload) => {
// `payload` will be an object in the format described here:
// https://developer.twitter.com/en/docs/accounts-and-users/subscribe-account-activity/guides/account-activity-data-objects
if (!payload) {
return;
}
if (payload.favorite_events) {
payload.favorite_events.forEach(favoriteEvent => {
// Discard favorites not made by us.
if (favoriteEvent.user.screen_name.toLowerCase() !== 'gamesdonequick') {
return;
}
if (favoriteEvent.favorited_status) {
addTweet(favoriteEvent.favorited_status);
}
});
}
if (payload.tweet_create_events) {
payload.tweet_create_events.forEach(tweetCreateEvent => {
// Discard tweets not made by us.
if (tweetCreateEvent.user.screen_name.toLowerCase() !== 'gamesdonequick') {
return;
}
// Discard quoted statuses because we can't show them.
if (tweetCreateEvent.quoted_status) {
return;
}
if (tweetCreateEvent.retweeted_status) {
const retweetedStatus = tweetCreateEvent.retweeted_status;
retweetedStatus.gdqRetweetId = tweetCreateEvent.id_str;
addTweet(retweetedStatus);
return;
}
// Discard @ replies because we don't want to show them.
if (tweetCreateEvent.in_reply_to_user_id) {
return;
}
addTweet(tweetCreateEvent);
});
}
});
socket.emit('authentication', { preSharedKey: nodecg.bundleConfig.twitter.preSharedKey });
});
/**
* Adds a Tweet to the queue.
* @param tweet - The tweet to add.
*/
function addTweet(tweet) {
// Don't add the tweet if we already have it
const isDupe = tweets.value.find((t) => t.id_str === tweet.id_str) ||
fanartTweets.value.find((t) => t.id_str === tweet.id_str);
if (isDupe) {
return;
}
// Parse emoji.
tweet.text = twemoji.parse(tweet.extended_tweet ? tweet.extended_tweet.full_text : tweet.text);
// Replace newlines with spaces
tweet.text = tweet.text.replace(/\n/ig, ' ');
// Highlight the #SGDQ2018 hashtag.
tweet.text = tweet.text.replace(/#sgdq2018/ig, '<span class="hashtag">#SGDQ2018</span>');
if (tweet.extended_tweet &&
tweet.extended_tweet.extended_entities &&
tweet.extended_tweet.extended_entities.media &&
tweet.extended_tweet.extended_entities.media.length > 0) {
tweet.gdqMedia = tweet.extended_tweet.extended_entities.media;
delete tweet.extended_tweet.extended_entities.media;
}
else if (tweet.extended_entities &&
tweet.extended_entities.media &&
tweet.extended_entities.media.length > 0) {
tweet.gdqMedia = tweet.extended_entities.media;
delete tweet.extended_entities.media;
}
else if (tweet.entities.media &&
tweet.entities.media.length > 0) {
tweet.gdqMedia = tweet.entities.media;
delete tweet.entities.media;
}
// If the tweet has media, place it into the Fanart queue.
// Else, place it into the normal tweet queue.
if (tweet.gdqMedia) {
fanartTweets.value.push(tweet);
}
else {
tweets.value.push(tweet);
}
}
/**
* Removes a Tweet (by id) from the queue.
* @param idToRemove - The ID string of the Tweet to remove.
* @returns The removed tweet. "Undefined" if tweet not found.
*/
function removeTweetById(idToRemove) {
if (typeof idToRemove !== 'string') {
throw new Error(`[twitter] Must provide a string ID when removing a tweet. ID provided was: ${idToRemove}`);
}
let didRemoveTweet = false;
[tweets, fanartTweets].forEach(tweetReplicant => {
tweetReplicant.value.some((tweet, index) => {
if (tweet.id_str === idToRemove || tweet.gdqRetweetId === idToRemove) {
tweetReplicant.value.splice(index, 1);
didRemoveTweet = true;
return true;
}
return false;
});
});
return didRemoveTweet;
}
//# sourceMappingURL=twitter.js.map