diff --git a/util/invite-user.js b/util/invite-user.js index d1f8712..7618ca3 100644 --- a/util/invite-user.js +++ b/util/invite-user.js @@ -3,7 +3,7 @@ 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 @@ -11,31 +11,45 @@ async function inviteGuestToSlack({ email, channels, _customMessage }) { // 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) @@ -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 } diff --git a/util/upgrade-user.js b/util/upgrade-user.js index 36e6302..a902d89 100644 --- a/util/upgrade-user.js +++ b/util/upgrade-user.js @@ -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)