-
Notifications
You must be signed in to change notification settings - Fork 662
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add Endpoint.Slack for invitations to the Elm Slack
- Loading branch information
Showing
1 changed file
with
69 additions
and
0 deletions.
There are no files selected for viewing
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,69 @@ | ||
{-# LANGUAGE OverloadedStrings #-} | ||
module Endpoint.Slack | ||
( Token(..) | ||
, endpoint | ||
) | ||
where | ||
|
||
|
||
-- Send invitations to the Elm Slack to whoever asks. | ||
-- | ||
-- NOTE: The API to invite users is not officially documented, but the people | ||
-- here looked in the Network tab of Developer Tools to figure it out: | ||
-- | ||
-- https://levels.io/slack-typeform-auto-invite-sign-ups/ | ||
-- from https://github.com/outsideris/slack-invite-automation | ||
-- | ||
|
||
|
||
|
||
-- ALLOWED ORIGINS | ||
|
||
|
||
allowedOrigins :: [String] | ||
allowedOrigins = | ||
[ "https://elm-lang.org" | ||
] | ||
|
||
|
||
|
||
-- ENDPOINT | ||
|
||
|
||
newtype Token = | ||
Token BS.ByteString | ||
|
||
|
||
endpoint :: Token -> Snap () | ||
endpoint (Token token) = | ||
Cors.allow POST allowedOrigins $ | ||
do req <- getRequest | ||
case Map.lookup "email" (rqQueryParams req) of | ||
[email] -> | ||
withResponse (request email) manager handler | ||
|
||
_ -> | ||
do modifyResponse $ setResponseStatus 400 "Bad Request" | ||
modifyResponse $ setContentType "text/html; charset=utf-8" | ||
writeBS "expecting query parameter like [email protected]" | ||
where | ||
request email = | ||
urlEncodedBody | ||
[ ("email", email) | ||
, ("token", token) | ||
, ("set_active","true") | ||
] | ||
(parseRequest_ "https://elmlang.slack.com/api/users.admin.invite") | ||
|
||
handler response = | ||
do modifyResponse $ setContentType "application/json" | ||
loop (responseBody response) | ||
where | ||
loop body = | ||
do chunk <- brRead body | ||
if BS.null chunk | ||
then return () | ||
else | ||
do writeBS chunk | ||
loop body | ||
|