Last active
March 13, 2017 13:28
-
-
Save bezysoftware/47a569e080d34e71059ba1a4d212d124 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
| // shorten invite link | |
| export let shortenLink = functions | |
| .database | |
| .ref('/groups/{groupId}/inviteLink') | |
| .onWrite(event => shortenInviteLink(event)); | |
| // Shortens invite link and saves it in place of the original long link. | |
| async function shortenInviteLink(event: Event<DeltaSnapshot>) | |
| { | |
| let link: string = event.data.val(); | |
| // link is already short | |
| if (!isLongLink(link)) | |
| { | |
| return; | |
| } | |
| var config = functions.config() as Config; | |
| var options = | |
| { | |
| method: 'POST', | |
| uri: 'https://firebasedynamiclinks.googleapis.com/v1/shortLinks', | |
| qs: { | |
| key: config.local.apikey | |
| }, | |
| headers: { | |
| 'Content-Type': 'application/json' | |
| }, | |
| body: { | |
| 'longDynamicLink': link, | |
| 'suffix': { 'option': 'UNGUESSABLE' } | |
| }, | |
| json: true | |
| }; | |
| let result = await request(options); | |
| await event.data.ref.set(result.shortLink); | |
| } | |
| // Checks whether the link is long or already shortened. | |
| function isLongLink(link: string) | |
| { | |
| return link.indexOf('?link') > 0; | |
| } | |
| interface Config { | |
| local: { | |
| apikey: string; | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment