Skip to content
This repository was archived by the owner on Mar 2, 2021. It is now read-only.

WIP feat: each track.url update matching providerTrack #45

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@
# config
.env

# secrets
.runtimeconfig.json

# compiled output
/dist
/tmp
Expand Down
1 change: 1 addition & 0 deletions .nvmrc
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
6.11.5
8 changes: 0 additions & 8 deletions .runtimeconfig.json

This file was deleted.

13 changes: 13 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -195,3 +195,16 @@ Firebase allowed this project to come to life without having the need to spend t

Do you want your project to appear here? Send a pull request or get in touch.

## Developement

`nvm use` will tell nvm to use the right `nodejs` version for this
Firebase Function project. It reads it from the `.nvmrc` file.

`firebase functions:config:get > .runtimeconfig.json` will download
locally the secret keys saved in the project environment. You will
need them to run the API.

`yarn start` will run the API.

`yarn firebase:shell` will run the Firebase shell, with which can
locally be testes the non-https functions. See the [documentation](https://firebase.google.com/docs/functions/local-emulator).
7 changes: 7 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,11 @@
const functions = require('firebase-functions')
const app = require('./src/app')
const onTrackUrlChange = require('./src/on-track-url-change')

// Public API for Radio4000
exports.api = functions.https.onRequest(app)

// Listen to every track change
exports.onTrackUrlChange =
functions.database.ref('/tracks/{trackId}/url')
.onWrite(onTrackUrlChange)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

does onWrite mean create+update? Does it also include delete?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

onWrite(), which triggers when data is created, updated, or deleted in the Realtime Database.
Source: https://firebase.google.com/docs/functions/database-events

11 changes: 7 additions & 4 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
"scripts": {
"start": "firebase serve --only hosting,functions --port 4001",
"test": "ava",
"firebase:shell": "firebase functions:shell",
"deploy-api": "firebase use staging; firebase deploy --only functions",
"deploy-api-production": "firebase use production; firebase deploy --only functions",
"deploy-rules": "firebase use staging; firebase deploy --only database",
Expand All @@ -16,14 +17,16 @@
"body-parser": "^1.17.2",
"cors": "^2.8.4",
"express": "^4.15.3",
"firebase-admin": "^5.10.0",
"firebase-functions": "^0.8.2",
"firebase-admin": "^5.12.1",
"firebase-functions": "^1.1.0",
"got": "^6.7.1",
"radio4000-sdk": "^0.0.5",
"stripe": "^4.24.0"
"stripe": "^4.24.0",
"youtube-regex": "^1.0.5"
},
"devDependencies": {
"ava": "^0.21.0",
"firebase-tools": "^3.17.7"
"firebase-tools": "^3.17.7",
"@google-cloud/functions-emulator": "^1.0.0-beta.4"
}
}
39 changes: 39 additions & 0 deletions src/on-track-url-change.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
const functions = require('firebase-functions')
const admin = require('firebase-admin');
const getYoutubeId = require('./utils/youtube-url-to-id.js')

/*
To test the execution of this function
you can run `yarn firebase:shell`
and use the following mockup call

onTrackUrlChange({before: 'youtu.be/xIaco5AQrUQ', after: 'https://www.youtube.com/watch?v=OkR7UNnQU6c' })
*/

const onTracksChange = (snapshot, context) => {
// console.log('snapshot: ', snapshot)
// console.log('context: ', context)

let db = admin.database();

let newUrl = snapshot.after.val()
let providerId = getYoutubeId(newUrl)
let providerName = 'youtube'

// abort if no ID in provider URL
if(!providerId) {
return false
}

const ref = db.ref(`/providerTracks/${providerName}:${providerId}/tracks/${context.params.trackId}`);
Copy link
Member

@oskarrough oskarrough Jun 23, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

How about splitting providerName and providerId with a slash as well? Instead of the :. So you don't have to parse the string to figure out which provider it is. Easier to query with firebase I assume.

ala

https://media.now.sh/youtube/YyI52_FEYgY
https://media.now.sh/vimeo/121814744
https://media.now.sh/discogs/1728315

Copy link
Author

@4www 4www Jun 23, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Right now the DB looks like this

- providerTracks
- - youtube:OkR7UNnQU6c

So if you want to query it with ember your just do store.find('providerTrack', 'youtube:OkR7UNnQU6c').

If you separate the provider with a slash how would you query the providerTrack that has the youtube id OkR7UNnQU6c?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

True. Not sure how to query that either. Maybe different models. Each extend providerTrack so the adapter can specify the endpoints: this.store.find('youtube-track', id)? Ala what we do on the Explorer for Discogs.


If we have discogs:123 and youtube:456. How do you query all Discogs tracks?

Another idea is to move the provider to to a property? {id: '123', provider: 'youtube'}. Then it's easy to filter and query. Trying to think of the pros and cons..

Copy link
Author

@4www 4www Jun 23, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It seems that there is a misunderstanding on the point of this new model.

I will try to make a summary to explain the idea better.


return ref.set(true);
}

module.exports = onTracksChange

/*
Model `providerTrack`:
- tracks: list of tracks that reference this providerTrack
- discogsReleaseId: id of this track's release on discogs
*/
11 changes: 11 additions & 0 deletions src/utils/youtube-url-to-id.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
const youtubeRegex = require('youtube-regex')

const youtubeUrlToId = function(url) {
const results = youtubeRegex().exec(url);
if (!results) {
return false;
}
return results[1];
}

module.exports = youtubeUrlToId
Loading