Start fetching.

This commit is contained in:
Roger Braun 2016-10-28 14:26:51 +02:00
parent 7ce6bbcf30
commit f8d6fe41f0
3 changed files with 118 additions and 53 deletions

View File

@ -1,4 +1,5 @@
import apiService from '../services/api/api.service.js' import apiService from '../services/api/api.service.js'
import timelineFetcher from '../services/timeline_fetcher/timeline_fetcher.service.js'
const users = { const users = {
state: { state: {
@ -17,13 +18,15 @@ const users = {
} }
}, },
actions: { actions: {
loginUser ({commit, state}, userCredentials) { loginUser (store, userCredentials) {
const commit = store.commit
commit('beginLogin') commit('beginLogin')
apiService.verifyCredentials(userCredentials) apiService.verifyCredentials(userCredentials)
.then((response) => { .then((response) => {
if (response.ok) { if (response.ok) {
response.json() response.json()
.then((user) => commit('setCurrentUser', user)) .then((user) => commit('setCurrentUser', user))
.then(() => timelineFetcher.startFetching({store, credentials: userCredentials}))
} }
commit('endLogin') commit('endLogin')
}) })

View File

@ -1,8 +1,8 @@
/* eslint-env browser */ /* eslint-env browser */
const LOGIN_URL = '/api/account/verify_credentials.json' const LOGIN_URL = '/api/account/verify_credentials.json'
// const FRIENDS_TIMELINE_URL='/api/statuses/friends_timeline.json'; const FRIENDS_TIMELINE_URL = '/api/statuses/friends_timeline.json'
// const PUBLIC_TIMELINE_URL='/api/statuses/public_timeline.json'; const PUBLIC_TIMELINE_URL = '/api/statuses/public_timeline.json'
// const PUBLIC_AND_EXTERNAL_TIMELINE_URL='/api/statuses/public_and_external_timeline.json'; const PUBLIC_AND_EXTERNAL_TIMELINE_URL = '/api/statuses/public_and_external_timeline.json'
// const CONVERSATION_URL = '/api/statusnet/conversation/'; // const CONVERSATION_URL = '/api/statusnet/conversation/';
// const STATUS_UPDATE_URL = '/api/statuses/update.json'; // const STATUS_UPDATE_URL = '/api/statuses/update.json';
// const MEDIA_UPLOAD_URL = '/api/statusnet/media/upload'; // const MEDIA_UPLOAD_URL = '/api/statusnet/media/upload';
@ -14,16 +14,38 @@ const LOGIN_URL = '/api/account/verify_credentials.json'
// import { param, ajax } from 'jquery'; // import { param, ajax } from 'jquery';
// import { merge } from 'lodash'; // import { merge } from 'lodash';
const apiService = { const authHeaders = (user) => ({ 'Authorization': `Basic ${btoa(`${user.username}:${user.password}`)}` })
verifyCredentials: (user) => {
const base64 = btoa(`${user.username}:${user.password}`) const fetchTimeline = ({timeline, credentials, since = false, until = false}) => {
const authHeaders = { 'Authorization': `Basic ${base64}` } const timelineUrls = {
public: PUBLIC_TIMELINE_URL,
friends: FRIENDS_TIMELINE_URL,
'public-and-external': PUBLIC_AND_EXTERNAL_TIMELINE_URL
}
let url = timelineUrls[timeline]
if (since) {
url += `?since_id=${since}`
}
if (until) {
url += `?max_id=${until}`
}
return fetch(url, { headers: authHeaders(credentials) }).then((data) => data.json())
}
const verifyCredentials = (user) => {
return fetch(LOGIN_URL, { return fetch(LOGIN_URL, {
method: 'POST', method: 'POST',
headers: authHeaders headers: authHeaders(user)
}) })
// return $http.post(LOGIN_URL, null, { headers: authHeaders }); }
}
const apiService = {
verifyCredentials,
fetchTimeline
} }
export default apiService export default apiService

View File

@ -1,53 +1,93 @@
import { upperFirst, camelCase } from 'lodash'; import { camelCase } from 'lodash'
const timelineFetcherServiceFactory = ($ngRedux, apiService, $interval) => { import apiService from '../api/api.service.js'
let fetcher;
const update = ({statuses, timeline, showImmediately}) => { const update = ({store, statuses, timeline, showImmediately}) => {
const ccTimeline = camelCase(timeline); const ccTimeline = camelCase(timeline)
const action = { store.commit('addNewStatuses', {
type: 'ADD_NEW_STATUSES',
data: {
statuses,
timeline: ccTimeline, timeline: ccTimeline,
statuses,
showImmediately showImmediately
} })
};
$ngRedux.dispatch(action); // $ngRedux.dispatch({type: 'UPDATE_TIMESTAMPS'});
$ngRedux.dispatch({type: 'UPDATE_TIMESTAMPS'}); }
};
const fetchAndUpdate = ({timeline = 'friends', older = false, showImmediately = false}) => { const fetchAndUpdate = ({store, credentials, timeline = 'friends', older = false, showImmediately = false}) => {
const args = { timeline }; const args = { timeline, credentials }
const timelineData = $ngRedux.getState().statuses.timelines[camelCase(timeline)]; const timelineData = store.rootState.statuses.timelines[camelCase(timeline)]
if(older) { if (older) {
args['until'] = timelineData.minVisibleId; args['until'] = timelineData.minVisibleId
} else { } else {
args['since'] = timelineData.maxId; args['since'] = timelineData.maxId
} }
apiService.fetchTimeline(args). apiService.fetchTimeline(args)
then((statuses) => update({statuses, timeline, showImmediately})); .then((statuses) => update({store, statuses, timeline, showImmediately}))
}; }
const startFetching = ({timeline = 'friends'}) => { const startFetching = ({ timeline = 'friends', credentials, store }) => {
fetchAndUpdate({timeline, showImmediately: true}); fetchAndUpdate({timeline, credentials, store, showImmediately: true})
const boundFetchAndUpdate = () => fetchAndUpdate({ timeline, credentials, store })
const boundFetchAndUpdate = () => fetchAndUpdate({timeline}); setInterval(boundFetchAndUpdate, 10000)
fetcher = $interval(boundFetchAndUpdate, 10000); }
}; const timelineFetcher = {
startFetching
}
const timelineFetcherService = { export default timelineFetcher
startFetching,
fetchAndUpdate
};
return timelineFetcherService; // const timelineFetcherServiceFactory = ($ngRedux, apiService, $interval) => {
}; // let fetcher;
timelineFetcherServiceFactory.$inject = ['$ngRedux', 'apiService', '$interval']; // const update = ({statuses, timeline, showImmediately}) => {
// const ccTimeline = camelCase(timeline);
export default timelineFetcherServiceFactory; // const action = {
// type: 'ADD_NEW_STATUSES',
// data: {
// statuses,
// timeline: ccTimeline,
// showImmediately
// }
// };
// $ngRedux.dispatch(action);
// $ngRedux.dispatch({type: 'UPDATE_TIMESTAMPS'});
// };
// const fetchAndUpdate = ({timeline = 'friends', older = false, showImmediately = false}) => {
// const args = { timeline };
// const timelineData = $ngRedux.getState().statuses.timelines[camelCase(timeline)];
// if(older) {
// args['until'] = timelineData.minVisibleId;
// } else {
// args['since'] = timelineData.maxId;
// }
// apiService.fetchTimeline(args).
// then((statuses) => update({statuses, timeline, showImmediately}));
// };
// const startFetching = ({timeline = 'friends'}) => {
// fetchAndUpdate({timeline, showImmediately: true});
// const boundFetchAndUpdate = () => fetchAndUpdate({timeline});
// fetcher = $interval(boundFetchAndUpdate, 10000);
// };
// const timelineFetcherService = {
// startFetching,
// fetchAndUpdate
// };
// return timelineFetcherService;
// };
// timelineFetcherServiceFactory.$inject = ['$ngRedux', 'apiService', '$interval'];
// export default timelineFetcherServiceFactory;