Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
56 changes: 35 additions & 21 deletions util/invite-user.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,39 +3,53 @@ const { defaultInvite } = require('./invite-types/default')
const { onboardInvite } = require('./invite-types/onboard')
const { metrics } = require('./metrics')

async function inviteGuestToSlack({ email, channels, _customMessage }) {
async function inviteGuestToSlack({ email, ip, channels, _customMessage }) {
// This is an undocumented API method found in https://github.com/ErikKalkoken/slackApiDoc/pull/70
// Unlike the documention in that PR, we're driving it not with a legacy token but a browser storage+cookie pair

// The SLACK_COOKIE is a xoxd-* token found in browser cookies under the key 'd'
// The SLACK_BROWSER_TOKEN is a xoxc-* token found in browser local storage using this script: https://gist.github.com/maxwofford/5779ea072a5485ae3b324f03bc5738e1

// I haven't yet found out how to add custom messages, so those are ignored for now
const cookieValue = `d=${process.env.SLACK_COOKIE}`
// const cookieValue = `d=${process.env.SLACK_COOKIE}`

// Create a new Headers object
const headers = new Headers()
// // Create a new Headers object
// const headers = new Headers()

// // Add the cookie to the headers
// headers.append('Cookie', cookieValue)
// headers.append('Content-Type', 'application/json')
// headers.append('Authorization', `Bearer ${process.env.SLACK_BROWSER_TOKEN}`)
// const data = JSON.stringify({
// token: process.env.SLACK_BROWSER_TOKEN,
// invites: [
// {
// email,
// type: 'restricted',
// mode: 'manual',
// },
// ],
// restricted: true,
// channels: channels.join(','),
// })

// Add the cookie to the headers
headers.append('Cookie', cookieValue)
// const res = await fetch(`https://slack.com/api/users.admin.inviteBulk`, {
// headers,
// method: 'POST',
// body: data,
// })

const headers = new Headers()
headers.append('Content-Type', 'application/json')
headers.append('Authorization', `Bearer ${process.env.SLACK_BROWSER_TOKEN}`)
headers.append('Authorization', `Bearer ${process.env.CHARON_API_KEY}`)

const data = JSON.stringify({
token: process.env.SLACK_BROWSER_TOKEN,
invites: [
{
email,
type: 'restricted',
mode: 'manual',
},
],
restricted: true,
channels: channels.join(','),
email,
ip
})

const res = await fetch(`https://slack.com/api/users.admin.inviteBulk`, {
const res = await fetch("https://charon.hackclub.com/user/invite", {
headers,
method: 'POST',
method: "POST",
body: data,
})
metrics.increment('events.flow.invitetoslack', 1)
Expand Down Expand Up @@ -71,7 +85,7 @@ async function inviteUser({
}
const { channels, customMessage } = invite

return await inviteGuestToSlack({ email, channels, customMessage })
return await inviteGuestToSlack({ email, ip, channels, customMessage })
}

module.exports = { inviteUser }
47 changes: 30 additions & 17 deletions util/upgrade-user.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,28 +24,41 @@ async function upgradeUser(user) {
// The SLACK_COOKIE is a xoxd-* token found in browser cookies under the key 'd'
// The SLACK_BROWSER_TOKEN is a xoxc-* token found in browser local storage using this script: https://gist.github.com/maxwofford/5779ea072a5485ae3b324f03bc5738e1

const cookieValue = `d=${process.env.SLACK_COOKIE}`
// const cookieValue = `d=${process.env.SLACK_COOKIE}`

// Create a new Headers object
const headers = new Headers()
// // Create a new Headers object
// const headers = new Headers()

// Add the cookie to the headers
headers.append('Cookie', cookieValue)
headers.append('Content-Type', 'application/json')
headers.append('Authorization', `Bearer ${process.env.SLACK_BROWSER_TOKEN}`)
// headers.append('Cookie', cookieValue)
// headers.append('Content-Type', 'application/json')
// headers.append('Authorization', `Bearer ${process.env.SLACK_BROWSER_TOKEN}`)

const form = JSON.stringify({
user,
team_id,
// const form = JSON.stringify({
// user,
// team_id,
// })
// return await fetch(
// `https://slack.com/api/users.admin.setRegular?slack_route=${team_id}&user=${user}`,
// {
// headers,
// method: 'POST',
// body: form,
// }
// )

const headers = new Headers()
headers.append('Content-Type', 'application/json')
headers.append('Authorization', `Bearer ${process.env.CHARON_API_KEY}`)

const data = JSON.stringify({
id: user
})
return await fetch("https://charon.hackclub.com/user/promote", {
headers,
method: "POST",
body: data,
})
return await fetch(
`https://slack.com/api/users.admin.setRegular?slack_route=${team_id}&user=${user}`,
{
headers,
method: 'POST',
body: form,
}
)
.then((r) => {
r.json()
metrics.increment('events.flow.user_upgrade', 1)
Expand Down