-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfunctions.js
More file actions
275 lines (234 loc) · 10.6 KB
/
functions.js
File metadata and controls
275 lines (234 loc) · 10.6 KB
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
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
import lastDates from './data/lastDates.json' assert { type: 'json' }
import fs from 'node:fs'
import nodeHtmlToImage from "node-html-to-image";
let PDS_URL
let DID
let ACCESS_JWT
let REFRESH_JWT
export async function authenticate() {
if(!ACCESS_JWT) {
// Create a session and fetch our PDS URL (the server that stores this user's data), JWT for authenticating our requests and DID (user's identifier)
const response = await fetch('https://bsky.social/xrpc/com.atproto.server.createSession',
{
method: 'POST',
headers: {
"Content-Type": "application/json",
},
body: JSON.stringify({
identifier: process.env.BOT_HANDLE,
password: process.env.BOT_PASSWORD
})
})
const json = await response.json()
PDS_URL = json.didDoc.service[0].serviceEndpoint
ACCESS_JWT = json.accessJwt
REFRESH_JWT = json.refreshJwt
DID = json.did
} else {
// Refresh our session
const response = await fetch('https://bsky.social/xrpc/com.atproto.server.refreshSession',
{
method: 'POST',
headers: {
"Authorization": `Bearer ${REFRESH_JWT}`
}
})
const json = await response.json()
ACCESS_JWT = json.accessJwt
REFRESH_JWT = json.refreshJwt
}
}
export function saveLastDates() {
// We store the last datetime of each petition state that we look at, so that on the next run
// we can only look at petitions which have entered that state after this time
try {
fs.writeFileSync('./data/lastDates.json', JSON.stringify(lastDates));
} catch (err) {
console.error(err);
}
}
export async function processRecentPetitions() {
const petitions = await getNewPetitionsAtState('recent', 'opened')
for (const p of petitions) {
const name = p.attributes.action.trim()
const description = p.attributes.background.trim()
const openedDate = new Date(p.attributes.opened_at)
const deadline = new Date(openedDate.setMonth(openedDate.getMonth() + 6))
const deadlineStr = deadline.toLocaleDateString("en-GB", {year: "numeric", month: "long", day: "numeric"})
const image = await createImage(name, 'New petition', description, 'green')
sendPost(p.attributes[`opened_at`],`New petition: "${name}"\r\n\r\nDeadline ${deadlineStr}, created by ${p.attributes.creator_name}`, p.links.self.replace('.json',''), `New petition: ${name}`, description, '#NewPetition', image)
}
}
export async function processRejectedPetitions() {
const petitions = await getNewPetitionsAtState('rejected', 'rejected')
for (const p of petitions) {
const name = p.attributes.action.trim()
const description = p.attributes.background.trim()
const image = await createImage(name, 'Rejected petition', description, 'red')
const rootPost = await sendPost(p.attributes[`rejected_at`], `Rejected petition: "${name}"`, p.links.self.replace('.json',''), `Rejected petition: ${name}`, description, '#RejectedPetition', image)
if(p.attributes.rejection.code === "duplicate") {
const dupePetitionUrlMatches = p.attributes.rejection.details.match(/https:\/\/petition.parliament.uk\/petitions\/[0-9]+/g)
if(dupePetitionUrlMatches) {
const dupePetitionUrl = dupePetitionUrlMatches[0]
const response = await fetch(`${dupePetitionUrl}.json`)
const data = (await response.json()).data
const dupeName = data.attributes.action.trim()
const dupeDescription = data.attributes.background.trim()
const image = await createImage(dupeName, 'Petition', dupeDescription, 'green')
sendPost(p.attributes[`rejected_at`], `This petition was rejected because it was too similar to another petition: "${dupeName}"`, dupePetitionUrl, `Petition: ${dupeName}`, dupeDescription, '#RejectedPetition', image, rootPost)
}
}
}
}
export async function processAwaitingResponsePetitions() {
const petitions = await getNewPetitionsAtState('awaiting_response', 'response_threshold_reached')
for (const p of petitions) {
const name = p.attributes.action.trim()
const description = p.attributes.background.trim()
const image = await createImage(name, 'Passed 10,000 signatures', description, 'green')
sendPost(p.attributes[`response_threshold_reached_at`], `Petition has passed the threshold for a response: "${name}"\r\n\r\nThe government will respond to this petition as it has ${p.attributes.signature_count} signatures`, p.links.self.replace('.json',''), `Passed 10,000 signatures: ${name}`, description, '#PetitionAwaitingResponse', image)
}
}
export async function processWithResponsePetitions() {
const petitions = await getNewPetitionsAtState('with_response', 'government_response')
for (const p of petitions) {
const name = p.attributes.action.trim()
const response = p.attributes['government_response'].summary.trim()
const image = await createImage(name, 'Government response', response, 'black')
sendPost(p.attributes['government_response_at'], `Government has responded to the petition "${name}"`, p.links.self.replace('.json','')+'#response-threshold-heading', `Government response: ${name}`, response, '#PetitionGovResponse', image)
}
}
export async function processAwaitingDebatePetitions() {
const petitions = await getNewPetitionsAtState('awaiting_debate', 'debate_threshold_reached')
for (const p of petitions) {
const name = p.attributes.action.trim()
const description = p.attributes.background.trim()
const image = await createImage(name, 'Passed 100,000 signatures', description, 'green')
sendPost(p.attributes[`debate_threshold_reached_at`], `Petition has passed the threshold for a debate: "${name}"\r\n\r\nParliament will consider this petition for a debate as it has ${p.attributes.signature_count} signatures`, p.links.self.replace('.json',''), `Passed 100,000 signatures: ${name}`, description, '#PetitionAwaitingDebate', image)
}
}
export async function processDebatedPetitions() {
const lastDate = lastDates['debate_outcome']
const debatedPetitions = await getNewPetitionsAtState('debated', 'debate_outcome')
debatedPetitions.forEach((p) => {
sendPost(`Parliament has debated this petition: "${p.attributes.action.trim()}"`, p.links.self.replace('.json','')+'#debate-threshold-heading', `Petition: ${p.attributes.action.trim()}`, p.attributes.background.trim(), '#PetitionDebated')
})
for (const p of debatedPetitions) {
const name = p.attributes.action.trim()
const description = p.attributes.background.trim()
const image = await createImage(name, 'Debated in Parliament', description, 'green')
sendPost(p.attributes[`debate_outcome_at`], `Parliament has debated this petition: "${name}"`, p.links.self.replace('.json',''), `Debated: ${name}`, description, '#PetitionDebated', image)
}
// Because 'debated' and 'not_debated' petitions use the same 'debate_outcome' date we have to reset the last date here otherwise we won't pick up new 'not_debated' petitions
lastDates['debate_outcome'] = lastDate
const notDebatedPetitions = await getNewPetitionsAtState('not_debated', 'debate_outcome')
for (const p of notDebatedPetitions) {
const name = p.attributes.action.trim()
const description = p.attributes.background.trim()
const image = await createImage(name, 'Not debated', description, 'red')
sendPost(p.attributes[`debate_outcome_at`], `The Petitions Committee has decided not to debate this petition: "${name}"`, p.links.self.replace('.json',''), `Not debated: ${name}`, description, '#PetitionNotDebated', image)
}
}
async function getNewPetitionsAtState(state, date_type) {
// Fetch all of the petitions at the specified state that have entered that state since our last run
const response = await fetch(`https://petition.parliament.uk/petitions.json?page=1&state=${state}`)
const json = await response.json()
const newPetitionsAtState = json.data.filter((p) => p.attributes[`${date_type}_at`] != null && p.attributes[`${date_type}_at`] > (lastDates[state] ?? '0'))
if(newPetitionsAtState.length > 0) {
// There are new petitions, so update our store of last dates
lastDates[state] = newPetitionsAtState.map((p) => p.attributes[`${date_type}_at`]).sort().reverse()[0]
console.log(`${newPetitionsAtState.length} new petitions at ${state}`)
}
return newPetitionsAtState
}
async function createImage(petitionName, petitionState, quote, colour) {
const template = fs.readFileSync('templates/imagetemplate.html', 'utf8');
const backgroundImage = 'data:image/png;base64,' + fs.readFileSync(`templates/${colour}.png`, { encoding: 'base64' })
const imgPath = './tmpimg.png';
await nodeHtmlToImage({
output: imgPath,
html: template,
content: {
name: petitionName,
state: petitionState,
quote: quote,
backgroundimage: backgroundImage,
class: colour === 'black' ? 'greenborder': null
}
})
return new Buffer(fs.readFileSync(imgPath))
}
async function sendPost(d, text, url, linkTitle, linkDescription, hashtag = null, image = null, inReplyTo = null) {
let facets = []
if(hashtag) {
const textEncoder = new TextEncoder();
facets.push(
{
index: {
byteStart: textEncoder.encode(`${text}\r\n\r\n`).length,
byteEnd: textEncoder.encode(`${text}\r\n\r\n${hashtag}`).length,
},
features: [{
$type: 'app.bsky.richtext.facet#tag',
tag: hashtag.replace('#', '')
}]
}
)
}
let imageBlobJson
if(image) {
const response = await fetch(PDS_URL+"/xrpc/com.atproto.repo.uploadBlob",
{
method: 'POST',
headers: {
"Authorization": `Bearer ${ACCESS_JWT}`,
"Content-Type": "image/png",
},
body: image
})
imageBlobJson = (await response.json()).blob
}
const post = {
"$type": 'app.bsky.feed.post',
text: `${text}${hashtag ? `\r\n\r\n${hashtag}` : ''}`,
createdAt: new Date(),
embed: {
"$type": 'app.bsky.embed.external',
external: {
uri: url,
title: linkTitle,
description: linkDescription
}
},
facets: facets
}
if(imageBlobJson) {
post.embed.external.thumb = imageBlobJson
}
if(inReplyTo) {
post.reply = {
root: {
uri: inReplyTo.uri,
cid: inReplyTo.cid
},
parent: {
uri: inReplyTo.uri,
cid: inReplyTo.cid
}
}
}
const result = await fetch(PDS_URL+"/xrpc/com.atproto.repo.createRecord",
{
method: 'POST',
headers: {
"Authorization": `Bearer ${ACCESS_JWT}`,
"Content-Type": "application/json",
},
body: JSON.stringify({
repo: DID,
collection: "app.bsky.feed.post",
record: post
})
})
return await result.json()
}