Patching onmessage from ReactDev tools; Genius auth

This commit is contained in:
James Barnsley
2018-08-16 08:05:06 +12:00
parent b7eb12b4f5
commit 240ebf9391
12 changed files with 958 additions and 3856 deletions

File diff suppressed because it is too large Load Diff

File diff suppressed because one or more lines are too long

View File

@ -98,11 +98,7 @@
// Release details
// These are automatically injected to built HTML
<<<<<<< HEAD
var build = "1534307216";
=======
var build = "1534236383";
>>>>>>> 54fb8a0266382e453de2eb9d9f88adee5d3c4f29
var build = "1534318748";
var version = "3.23.2";
// Construct the script tag

12
src/js/bootstrap.js vendored
View File

@ -63,14 +63,14 @@ var initialState = {
mute: false,
volume: 0,
progress: 0,
play_state: false,
play_state: null,
uri_schemes: [],
library_albums_uri: 'local:directory?type=album',
library_artists_uri: 'local:directory?type=artist'
},
pusher: {
connected: false,
username: false,
username: null,
connections: {},
version: {
current: '0.0.0'
@ -78,14 +78,15 @@ var initialState = {
config: {}
},
lastfm: {
me: false,
me: null,
authorization_url: 'https://jamesbarnsley.co.nz/iris/auth_lastfm.php'
},
genius: {
provider_url: 'https://james.barnsley.nz/iris/provider_genius.php'
me: null,
authorization_url: 'https://jamesbarnsley.co.nz/iris/auth_genius.php'
},
spotify: {
me: false,
me: null,
autocomplete_results: {},
authorization_url: 'https://jamesbarnsley.co.nz/iris/auth_spotify.php'
}
@ -98,6 +99,7 @@ initialState.mopidy = Object.assign({}, initialState.mopidy, helpers.getStorage(
initialState.pusher = Object.assign({}, initialState.pusher, helpers.getStorage('pusher'));
initialState.spotify = Object.assign({}, initialState.spotify, helpers.getStorage('spotify'));
initialState.lastfm = Object.assign({}, initialState.lastfm, helpers.getStorage('lastfm'));
initialState.genius = Object.assign({}, initialState.genius, helpers.getStorage('genius'));
console.log('Bootstrapping', initialState)

View File

@ -24,10 +24,15 @@ class GeniusAuthenticationFrame extends React.Component{
// Listen for incoming messages from the authorization popup
window.addEventListener('message', function(event){
var data = JSON.parse(event.data);
try {
var data = JSON.parse(event.data);
} catch (e){
console.error("Failed to parse JSON", e, event);
return;
}
// Only digest messages relevant to us
if (data.origin == 'auth_lastfm'){
if (data.origin == 'auth_genius'){
self.handleMessage(event, data);
}
}, false);
@ -62,7 +67,7 @@ class GeniusAuthenticationFrame extends React.Component{
this.setState({authorizing: true})
// Open an authentication request window
var url = this.props.authorization_url+'?action=authorize'
var url = this.props.authorization_url+'?action=authorize&scope=me'
var popup = window.open(url,"popup","height=580,width=350");
popup.name = "GeniusAuthenticationWindow";
@ -108,7 +113,7 @@ class GeniusAuthenticationFrame extends React.Component{
const mapStateToProps = (state, ownProps) => {
return {
authorization_url: state.genius.authorization_url,
authorized: state.genius.session,
authorized: state.genius.authorization,
authorizing: state.genius.authorizing
}
}

View File

@ -24,7 +24,12 @@ class LastfmAuthenticationFrame extends React.Component{
// Listen for incoming messages from the authorization popup
window.addEventListener('message', function(event){
var data = JSON.parse(event.data);
try {
var data = JSON.parse(event.data);
} catch (e){
console.error("Failed to parse JSON", e, event);
return;
}
// Only digest messages relevant to us
if (data.origin == 'auth_lastfm'){

View File

@ -24,7 +24,12 @@ class SpotifyAuthenticationFrame extends React.Component{
// Listen for incoming messages from the authorization popup
window.addEventListener('message', function(event){
var data = JSON.parse(event.data);
try {
var data = JSON.parse(event.data);
} catch (e){
console.error("Failed to parse JSON", e, event);
return;
}
// Only digest messages relevant to us
if (data.origin == 'auth_spotify'){

View File

@ -202,13 +202,13 @@ class Services extends React.Component{
}
renderGenius(){
var user_object = (this.props.genius.session ? this.props.core.users["genius:user:"+this.props.genius.session.name] : null);
var user_object = (this.props.genius.user ? this.props.core.users["genius:user:"+this.props.genius.user.id] : null);
if (user_object){
var user = (
<span className="user">
<Thumbnail circle={true} size="small" images={user_object.image} />
<Thumbnail circle={true} size="small" images={user_object.avatar} />
<span className="user-name">
{user_object.realname ? user_object.realname : user_object.name}
{user_object.name}
</span>
</span>
)
@ -225,7 +225,7 @@ class Services extends React.Component{
return (
<div>
{this.props.genius.session ? <div className="field current-user">
{this.props.genius.user ? <div className="field current-user">
<div className="name">Current user</div>
<div className="input">
<div className="text">
@ -292,7 +292,11 @@ class Services extends React.Component{
var lastfm_icon = <Icon type="fontawesome" name="lastfm" />
}
var genius_icon = <Icon type="fontawesome" name="genius" />
if (this.props.genius.user && this.props.core.users["lastfm:user:"+this.props.genius.user.id]){
var genius_icon = <Thumbnail circle={true} size="small" images={this.props.core.users["genius:user:"+this.props.genius.user.id].avatar} />
} else {
var genius_icon = <Icon name="extension" />
}
return (
<div className="menu">

View File

@ -3,6 +3,13 @@ var coreActions = require('../core/actions')
var uiActions = require('../ui/actions')
var helpers = require('../../helpers')
export function set(data){
return {
type: 'GENIUS_SET',
data: data
}
}
/**
* Send an ajax request to the Genius API
*
@ -16,54 +23,56 @@ var helpers = require('../../helpers')
const sendRequest = (dispatch, getState, endpoint, method = 'GET', data = false) => {
return new Promise((resolve, reject) => {
getToken(dispatch, getState )
.then(
// create our ajax request config
var config = {
method: method,
url: 'https://api.genius.com/'+endpoint,
cached: true,
timeout: 30000,
headers: {
Authorization: 'Bearer '+ response,
Accept: 'application/json'
}
}
// only if we've got data do we add it to the request (this prevents appending of "&false" to the URL)
if (data){
if (typeof(data) === 'string'){
config.data = data
} else {
config.data = JSON.stringify(data)
}
}
// add reference to loader queue
var loader_key = helpers.generateGuid()
dispatch(uiActions.startLoading(loader_key, 'genius_'+endpoint))
$.ajax(config).then(
response => {
dispatch(uiActions.stopLoading(loader_key));
// create our ajax request config
var config = {
method: method,
url: 'https://api.genius.com/'+endpoint,
cached: true,
timeout: 30000,
headers: {
Authorization: 'Bearer '+ response,
Accept: 'application/json'
}
if (response.meta.status >= 200 && response.meta.status < 300 && response.response){
resolve(response.response);
} else {
reject({
config: config,
xhr: xhr,
status: status,
error: error
});
}
// only if we've got data do we add it to the request (this prevents appending of "&false" to the URL)
if (data){
if (typeof(data) === 'string'){
config.data = data
} else {
config.data = JSON.stringify(data)
}
}
// add reference to loader queue
var loader_key = helpers.generateGuid()
dispatch(uiActions.startLoading(loader_key, 'genius_'+endpoint))
$.ajax(config).then(
response => {
dispatch(uiActions.stopLoading(loader_key))
resolve(response)
},
(xhr, status, error) => {
dispatch(uiActions.stopLoading(loader_key));
reject({
config: config,
xhr: xhr,
status: status,
error: error
});
}
)
},
error => {
reject(error)
(xhr, status, error) => {
dispatch(uiActions.stopLoading(loader_key));
reject({
config: config,
xhr: xhr,
status: status,
error: error
});
}
);
}
@ -94,17 +103,17 @@ export function revokeAuthorization(){
**/
export function getMe(){
return (dispatch, getState) => {
sendRequest(dispatch, getState, 'me')
sendRequest(dispatch, getState, 'account')
.then(
response => {
dispatch({
type: 'GENIUS_ME_LOADED',
data: response
user: response.user
});
},
error => {
dispatch(coreActions.handleException(
'Could not load your profile',
'Could not load your Genius profile',
error
));
}

View File

@ -1,6 +1,28 @@
export default function reducer(genius = {}, action){
switch (action.type){
case 'GENIUS_SET':
return Object.assign({}, genius, action.data)
case 'GENIUS_ME_LOADED':
return Object.assign({}, genius, {user: action.user})
case 'GENIUS_AUTHORIZATION_GRANTED':
return Object.assign({}, genius, {
authorizing: false,
authorization_code: action.data.authorization_code,
access_token: action.data.access_token
})
case 'GENIUS_AUTHORIZATION_REVOKED':
return Object.assign({}, genius, {
authorizing: false,
authorization_code: false,
access_token: false,
me: false
});
default:
return genius
}

View File

@ -117,6 +117,27 @@ const localstorageMiddleware = (function(){
);
break;
case 'GENIUS_AUTHORIZATION_GRANTED':
helpers.setStorage(
'genius',
{
authorization_code: action.data.authorization_code,
access_token: action.data.access_token
}
);
break;
case 'GENIUS_AUTHORIZATION_REVOKED':
helpers.setStorage(
'genius',
{
me: null,
authorization_code: null,
access_token: null
}
);
break;
case 'CORE_SET':
helpers.setStorage(
'core',

View File

@ -578,6 +578,10 @@ const PusherMiddleware = (function(){
store.dispatch(lastfmActions.set({
authorization_url: (action.config.lastfm_authorization_url ? action.config.lastfm_authorization_url : null)
}));
store.dispatch(geniusActions.set({
authorization_url: (action.config.genius_authorization_url ? action.config.genius_authorization_url : null)
}));
next(action);
break