-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
171 additions
and
10 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
const axios = require('axios') | ||
const FormData = require('form-data') | ||
|
||
/* | ||
* Send a single email | ||
*/ | ||
const add_email = async (member, sendy_info) => { | ||
const form = new FormData() | ||
form.append('api_key', sendy_info.api_key) | ||
form.append('name', member.name.full) | ||
form.append('email', `s${member.student}@ed.ac.uk`) | ||
form.append('list', sendy_info.target_list) | ||
form.append('boolean', 'true') | ||
|
||
await axios.post(sendy_info.sendy_url + 'subscribe', form, { headers: form.getHeaders() }) | ||
} | ||
|
||
const sendy_sync = async (members, sendy_info) => { | ||
// Got to send these in small batches and sequentially, because sendy is kinda shit. | ||
|
||
const partial_add_email = m => add_email(m, sendy_info) | ||
|
||
// split into batches of 10 | ||
const BATCH_SIZE = 10 | ||
const members_batched = members | ||
.reduce((a, c, i) => { | ||
// if we're at BATCH_SIZE or starting off, add a sub array | ||
if (0 == i % BATCH_SIZE) { | ||
a.push([]) | ||
} | ||
|
||
// add to the last batch | ||
a[a.length - 1].push(c) | ||
|
||
return a | ||
}, []) | ||
|
||
for (let batch of members_batched) { | ||
// resolve all these guys | ||
await Promise.all(batch.map(partial_add_email)) | ||
} | ||
} | ||
|
||
module.exports = sendy_sync |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
<!DOCTYPE html> | ||
|
||
<html> | ||
<head> | ||
<title>EUSA Members API</title> | ||
<meta name="viewport" content="width=device-width, initial-scale=1"> | ||
<link href="/static/index.css" rel="stylesheet"> | ||
<link href="/static/compsoc-icon.png" rel="icon"> | ||
</head> | ||
|
||
<body> | ||
<nav> | ||
<div class="nav__brand"> | ||
<img src="/static/compsoc-icon.png" alt="compsoc icon"/> | ||
<span class="nav__title">EUSA Members API</span> | ||
</div> | ||
<div class="nav__login"> | ||
<span class="nav__name"><%= user.name %></span> | ||
<img src="<%= user.photo %>" alt="<%= user.name %> photo"/> | ||
|
||
<a class="nav__logout" href="/auth/logout"> | ||
<span>logout</span> | ||
</a> | ||
</div> | ||
</nav> | ||
<main> | ||
<h1> Synchronizing... </h1> | ||
<p id="status"> | ||
Synchronizing the cached member list with Sendy. This may take 5-30 seconds. | ||
Please do not refresh the page! | ||
</p> | ||
|
||
<div class="spinner" id="spinner"></div> | ||
|
||
<a href="/dashboard" id="return" style="display: none;"> | ||
Return to the dashboard → | ||
</a> | ||
</main> | ||
|
||
<script type="text/javascript"> | ||
window.apikey = '<%- apikey %>' | ||
const statusP = document.querySelector('#status') | ||
const returnLink = document.querySelector('#return') | ||
const spinner = document.querySelector('#spinner') | ||
fetch('/api/sendy_sync', { | ||
headers: { | ||
'Authorization': `Bearer ${window.apikey}` | ||
} | ||
}) | ||
.then(r => r.json()) | ||
.then(r => { | ||
returnLink.style.display = 'inline' | ||
spinner.style.display = 'none'; | ||
if (r.success) { | ||
location.href = '/dashboard' | ||
statusP.innerText = 'Scrape successful!' | ||
} else { | ||
statusP.innerHTML = `Something went wrong! Here's what we know: <br/> <code> ${r.status} </code>` | ||
} | ||
}) | ||
.catch(() => { | ||
returnLink.style.display = 'inline' | ||
spinner.style.display = 'none'; | ||
statusP.innerText = `Something went wrong! And we don't know what it is, either.` | ||
}) | ||
</script> | ||
</body> | ||
</html> |