destinygg / website

Destiny.gg
https://www.destiny.gg
Other
210 stars 121 forks source link

Enable YouTube membership sync #240

Closed 11k closed 3 years ago

11k commented 4 years ago

This PR closes #222.

config.local.php changes

Old

'oauth_providers' => [
    ...
    'youtube' => [
        'client_id' => '<client_id_1>',
        'client_secret' => '<client_secret_1>',
        'redirect_uri' => 'https://<host>/admin/youtube/auth'
    ]
    ...
]

'youtube' => [
    'apikey' => '<api_key>',
    'playlistId' => '<channel_id>',
    'user' => '<channel_name>',
]

New

'oauth_providers' => [
    ...
    'youtubebroadcaster' => [
        'client_id' => '<client_id_1>',
        'client_secret' => '<client_secret_1>',
        'redirect_uri' => 'https://<host>/admin/youtube/auth'
    ],
    'youtube' => [
        'client_id' => '<client_id_2>',
        'client_secret' => '<client_secret_2>',
        'redirect_uri' => 'https://<host>/profile/auth/youtube/cb'
    ]
    ...
]

`youtubebroadcaster` => [
    'sync_memberships' => false,
    'channelId' => '<channel_id>'
]

How it works

Toggle the youtubebroadcaster => 'sync_memberships' setting in your config.local.php to enable membership syncing. If disabled, no membership-related cron tasks will run and authing on /admin/youtube won't include the youtube.channel-memberships.creator scope. Remember that this scope requires special approval from Google.

After the streamer auths on /admin/youtube, we run the "full sync" task to fetch all available membership levels and all active members.

To fetch new members, we use a separate cron task that runs every five minutes. This task passes mode=updates as a parameter when pinging the /members endpoint, which means only new members since the last call will be returned.

Knowing the exact expiration date of a YouTube membership is difficult. To handle expirations easily, we simply run the "full sync" task every 24 hours to fetch all members again. We then compare this list of members to those in our database and prune the ones that no longer exist. This means memberships expire only once per day.

YouTube channel IDs are the only way to uniquely identify members, therefore, we need to know a user's channel ID to determine if they have an active membership. In the Subscription section of their profile page, users will now see a subsection titled YouTube with a button that reads Log in with YouTube. By clicking the button, users can auth with the youtube.readonly scope and let us fetch the IDs of their YouTube channels. These IDs are cross-referenced with the IDs of active members to check their membership status. Note that youtube.readonly is a "sensitive" scope, so users will be greeted with a very spooky warning if your app isn't verified.

Screen Shot 2020-11-26 at 6 28 18 PM

Additional things