Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add OpenCollective provider #10

Merged
merged 3 commits into from
Aug 29, 2022

Conversation

GeopJr
Copy link
Contributor

@GeopJr GeopJr commented Mar 7, 2022

This PR adds an OpenCollective provider.

Blocking

OpenCollective returns an avatar url even if the user doesn't have one (which fails on resolveAvatars), a default one should be provided instead.

Concerns

  • OpenCollective suppots multiple currencies which might confuse users since the key is called monthlyDollars
  • OpenCollective graphql api is not fully documented (eg. pagination)

Output (on babel with a 10 backers hard limit):

sponsors of babel on opencollective

closes: #8

@JounQin
Copy link

JounQin commented Aug 24, 2022

@GeopJr Is this PR ready? I'd love to use it!

@GeopJr
Copy link
Contributor Author

GeopJr commented Aug 24, 2022

@GeopJr Is this PR ready? I'd love to use it!
@JounQin

Kinda - I just fixed the conflicts. The main problem still is the lack of default avatar (#11). As per the original post, OpenCollective returns an avatar url even if the user doesn't have one (e.g. https://images.opencollective.com/kentcdodds/d219cb9/avatar/460.png) which causes sponsorkit to fail during the avatar downloading portion:

FetchError: 404 Not Found (https://images.opencollective.com/kentcdodds/d219cb9/avatar/460.png)

However here's a quick (and dirty) workaround (it's a git patch):

diff --git a/src/image.ts b/src/image.ts
index 911f31a..faf7e23 100644
--- a/src/image.ts
+++ b/src/image.ts
@@ -5,8 +5,15 @@ import sharp from 'sharp'
 import type { Sponsorship } from './types'
 
 export async function resolveAvatars(ships: Sponsorship[]) {
-  return Promise.all(ships.map(async(ship) => {
-    const data = await $fetch(ship.sponsor.avatarUrl, { responseType: 'arrayBuffer' })
+  const defaultAvatar = await $fetch("https://i.imgur.com/nk8ksF0.png", { responseType: 'arrayBuffer' })
+  return Promise.all(ships.map(async (ship) => {
+    let data;
+    try {
+      data = await $fetch(ship.sponsor.avatarUrl, { responseType: 'arrayBuffer' })
+    } catch {
+      data = defaultAvatar
+    }
+
     const radius = ship.sponsor.type === 'User' ? 0.5 : 0.15
     ship.sponsor.avatarUrlHighRes = await imageDataURI.encode(await round(data, radius, 120), 'PNG')
     ship.sponsor.avatarUrlMediumRes = await imageDataURI.encode(await round(data, radius, 80), 'PNG')

This will use a quick default avatar I made from the GitHub "private sponsor" icon when an avatar 404s.

Here's the full babel list (1212 members - too big of an svg but ok for the sake of testing)

example usage: SPONSORKIT_OPENCOLLECTIVE_KEY=<YOUR_API_KEY> SPONSORKIT_OPENCOLLECTIVE_GH_HANDLE=babel pnpm run dev

@antfu
Copy link
Member

antfu commented Aug 25, 2022

I think we could have an option to configure the fallback avatar (could be a function for generated ones). Using GH's fallback one as the default sounds good to me

@GeopJr GeopJr mentioned this pull request Aug 25, 2022
@GeopJr
Copy link
Contributor Author

GeopJr commented Aug 25, 2022

Should SPONSORKIT_OPENCOLLECTIVE_KEY be renamed to SPONSORKIT_OPENCOLLECTIVE_TOKEN to match the other providers?

OpenCollective does call it API Key however:
API Keys Use API Keys to interact with the Open Collective GraphQL API with your own account.

@GeopJr
Copy link
Contributor Author

GeopJr commented Aug 26, 2022

Since #11 is done, I think this PR is ready for review!

@GeopJr GeopJr marked this pull request as ready for review August 26, 2022 09:48
docs: GitHub slug -> GitHub handle
@antfu antfu merged commit 237c9b3 into antfu-collective:main Aug 29, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Open Collective Support
3 participants