Skip to content

Commit 4746c92

Browse files
author
Duke
authored
Merge pull request #232 from uhuraapp/refactor-api
refactor: api changes
2 parents fbb4267 + 95b41e1 commit 4746c92

18 files changed

+37
-257
lines changed

.jshintrc

+1-2
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,7 @@
22
"predef": [
33
"server",
44
"document",
5-
"window",
6-
"-Promise"
5+
"window"
76
],
87
"browser": true,
98
"boss": true,

app/authenticators/uhura.js

+8-47
Original file line numberDiff line numberDiff line change
@@ -2,42 +2,10 @@ import Ember from 'ember';
22
import Base from 'ember-simple-auth/authenticators/base';
33
import ENV from '../config/environment';
44

5-
let { RSVP: { Promise } } = Ember;
6-
75
export default Base.extend({
86
__authURLForProvider(provider) {
97
return `${ENV.API_URL}/v2/auth/${provider}`;
108
},
11-
__getUserData() {
12-
return Ember.$.ajax({
13-
url: `${ENV.API_URL}/v2/user`,
14-
type: 'GET',
15-
xhrFields: {
16-
withCredentials: true
17-
}
18-
});
19-
},
20-
__checkLogin(loginWindow, resolve, reject) {
21-
return () => {
22-
try {
23-
if (loginWindow.closed) {
24-
this.checkCredentials(resolve, reject);
25-
} else {
26-
window.setTimeout(this.__checkLogin(loginWindow, resolve, reject), 500);
27-
}
28-
} catch(e) {
29-
Ember.run(() => reject(e));
30-
}
31-
};
32-
},
33-
checkCredentials(resolve, reject) {
34-
this.__getUserData().then((data) => {
35-
Ember.run(() => resolve(data));
36-
this._setUser(data);
37-
}, function() {
38-
Ember.run(() => reject('Error: Could not authenticate using external service'));
39-
});
40-
},
419

4210
restore(properties) {
4311
let propertiesObject = Ember.Object.create(properties);
@@ -53,16 +21,10 @@ export default Base.extend({
5321

5422
authenticate(data) {
5523
if (data.provider) {
56-
return new Promise((resolve, reject) => {
57-
let loginWindow = window.open(this.__authURLForProvider(data.provider), '_blank', 'location=no,toolbar=no');
58-
window.setTimeout(this.__checkLogin(loginWindow, resolve, reject), 500);
59-
60-
loginWindow.addEventListener('loadstop', (event) => {
61-
if (event.url.indexOf(`${this.__authURLForProvider(data.provider)}/callback`) === 0) {
62-
loginWindow.close();
63-
this.checkCredentials(resolve, reject);
64-
}
65-
});
24+
return new Promise(() => {
25+
let url = this.__authURLForProvider(data.provider);
26+
url = `${url}?redirect_to=${window.location.href}`;
27+
window.location = url;
6628
});
6729
} else if (data.email && data.password) {
6830
return this.request('POST', '/v2/users/sign_in', data).catch(()=> {
@@ -72,6 +34,8 @@ export default Base.extend({
7234
return Promise.reject('Error: password is required');
7335
} else if (data.password && !data.email) {
7436
return Promise.reject('Error: email is required');
37+
} else if (data.token) {
38+
return this.request('GET', '/v2/user', data);
7539
}
7640

7741
return Promise.reject('Error: email and password is required');
@@ -91,14 +55,11 @@ export default Base.extend({
9155
};
9256
},
9357

94-
request(type, path, data) {
95-
data = JSON.stringify(data);
58+
request(type, path, data = {}) {
59+
data = type === 'POST' ? JSON.stringify(data) : data;
9660
return new Promise((resolve, reject) => {
9761
Ember.$.ajax({
9862
url: ENV.API_URL + path,
99-
xhrFields: {
100-
withCredentials: true
101-
},
10263
contentType: 'application/json; charset=utf-8',
10364
dataType: 'json',
10465
type,

app/components/channel-search.js

-33
This file was deleted.

app/components/episode-list-item.js

+14-8
Original file line numberDiff line numberDiff line change
@@ -25,13 +25,13 @@ export default Ember.Component.extend({
2525
},
2626

2727
played() {
28-
let episode = this.get('episode');
29-
let isPlayed = !!episode.get('played');
30-
let method = isPlayed ? 'DELETE' : 'POST';
28+
const episode = this.get('episode');
29+
const isPlayed = !!episode.get('played');
30+
const method = isPlayed ? 'DELETE' : 'POST';
3131

3232
episode.set('played', !isPlayed); // early visual response
3333

34-
this.get('client').request('episode', episode.id, 'played', method).catch(() => {
34+
this.get('client').request(this._episodeEndpoint(), episode.id, 'played', method).catch(() => {
3535
episode.set('played', isPlayed); // rollback
3636
});
3737

@@ -40,20 +40,26 @@ export default Ember.Component.extend({
4040

4141
download() {
4242
const episodeID = this.get('episode.id');
43-
const downloadURL = this.get('client').buildURL('episode', episodeID, 'download');
43+
44+
const downloadURL = this.get('client').buildURL(this._episodeEndpoint(), episodeID, 'download');
4445
window.open(downloadURL);
4546
},
4647

4748
more() {
4849
Ember.$('.itens.open').removeClass('open');
4950
this.$('.itens').addClass('open');
50-
Ember.run.later(function() {
51-
Ember.$(document).on('click.out-itens', 'body', function(e) {
51+
Ember.run.later(function _later() {
52+
Ember.$(document).on('click.out-itens', 'body', function _clickOutItens(e) {
5253
Ember.$('.itens.open').removeClass('open');
5354
Ember.$(document).off('click.out-itens');
5455
e.stopPropagation();
5556
});
5657
}, 500);
57-
}
58+
},
59+
},
60+
61+
_episodeEndpoint() {
62+
const episode = this.get('episode');
63+
return `channels/${episode.get('channel_id')}/episode`;
5864
}
5965
});

app/components/episodes-list.js

+1-35
Original file line numberDiff line numberDiff line change
@@ -1,41 +1,7 @@
1-
/* global moment */
21
import Ember from 'ember';
32

43
export default Ember.Component.extend({
54
classNames: ['episodes'],
65
tagName: 'ul',
7-
hasMore: true,
8-
page: 1,
9-
infiniteScroll: true,
10-
hasRightActions: true,
11-
actions: {
12-
fetchMore(callback) {
13-
let promise = Ember.RSVP.Promise.reject();
14-
15-
if (this.get('infiniteScroll')) {
16-
let episodes = this.get('episodes');
17-
let lastEpisode = episodes.get('lastObject');
18-
let date = lastEpisode ? lastEpisode.get('published_at') : 1999;
19-
let since = moment.utc(date).format();
20-
21-
// TODO: use meta
22-
promise = this.get('store').query('episode', {
23-
since,
24-
channel_id: this.get('channel.raw_id'),
25-
per_page: 20
26-
}).then(function(response) {
27-
// Fix error with _internalModel
28-
let items = [];
29-
response.get('content').forEach(function(internalModel) {
30-
items.push({ _internalModel: internalModel });
31-
});
32-
response.set('content', items);
33-
return response;
34-
});
35-
36-
}
37-
38-
callback(promise);
39-
}
40-
}
6+
hasRightActions: true
417
});

app/mixins/models/channel.js

-1
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@ export default Ember.Mixin.create({
77
description: DS.attr('string'),
88
copyrigth: DS.attr('string'),
99
episodes: DS.hasMany('episode', { async: false }),
10-
profile: DS.belongsTo('profile', { async: false }),
1110
profile_id: DS.attr('string'),
1211
raw_id: DS.attr('string'),
1312
subscribed: DS.attr('boolean'),

app/mixins/routes/authentication.js

+8
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,17 @@ import MaterialDesignMixin from './material-design';
44
export default Ember.Mixin.create(MaterialDesignMixin, {
55
session: Ember.inject.service('session'),
66
beforeModel(transition) {
7+
if (transition.queryParams.token) {
8+
this.authenticationViaToken(transition.queryParams.token);
9+
}
10+
711
if (this.get('session').get('isAuthenticated')) {
812
this.transitionTo('subscriptions');
913
}
1014
return this._super(transition);
15+
},
16+
17+
authenticationViaToken(token) {
18+
this.get('session').authenticate('authenticator:uhura', { token });
1119
}
1220
});

app/models/episode.js

-4
Original file line numberDiff line numberDiff line change
@@ -19,10 +19,6 @@ export default DS.Model.extend({
1919
source: computed('source_url', function() {
2020
return this.get('source_url');
2121
}),
22-
persisted: computed('id', function() {
23-
// true when the channel id is not a URL
24-
return !this.get('channel_id').match(/http:\/\//);
25-
}),
2622
publishedAt: computed('published_at', function() {
2723
return moment(this.get('published_at')).format('MMM Do YY');
2824
}),

app/services/filesystem.js

-31
This file was deleted.

app/services/player.js

+5-3
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ export default Ember.Service.extend({
1313

1414
PLAYED_PERCENT: 95,
1515

16-
observerAutoplay: observer('autoplay', function() {
16+
observerAutoplay: observer('autoplay', function _observerAutoplay() {
1717
window.ahoy.track('player', { action: 'autoplay', enabled: this.get('autoplay') });
1818
}),
1919

@@ -105,10 +105,12 @@ export default Ember.Service.extend({
105105
if (at === this.get('currentTime')) {
106106
return;
107107
}
108+
const episodeEndpoint = `channels/${episode.get('channel_id')}/episode`;
109+
let data = { at };
110+
108111
this.set('currentTime', at);
109112

110-
let data = { at };
111-
this.get('client').request('episode', episode.id, 'listen', 'PUT', { data }).then(() => {
113+
this.get('client').request(episodeEndpoint, episode.id, 'listen', 'PUT', { data }).then(() => {
112114
episode.set('stopped_at', at);
113115
});
114116
},

app/services/search.js

-43
This file was deleted.

app/templates/components/channel-search.hbs

-25
This file was deleted.

0 commit comments

Comments
 (0)