From f8d6fe41f0c639ce1c3f8072ef94de8e3419f1cf Mon Sep 17 00:00:00 2001 From: Roger Braun Date: Fri, 28 Oct 2016 14:26:51 +0200 Subject: [PATCH] Start fetching. --- src/modules/users.js | 5 +- src/services/api/api.service.js | 46 +++++-- .../timeline_fetcher.service.js | 120 ++++++++++++------ 3 files changed, 118 insertions(+), 53 deletions(-) diff --git a/src/modules/users.js b/src/modules/users.js index e7ebf305..76a26863 100644 --- a/src/modules/users.js +++ b/src/modules/users.js @@ -1,4 +1,5 @@ import apiService from '../services/api/api.service.js' +import timelineFetcher from '../services/timeline_fetcher/timeline_fetcher.service.js' const users = { state: { @@ -17,13 +18,15 @@ const users = { } }, actions: { - loginUser ({commit, state}, userCredentials) { + loginUser (store, userCredentials) { + const commit = store.commit commit('beginLogin') apiService.verifyCredentials(userCredentials) .then((response) => { if (response.ok) { response.json() .then((user) => commit('setCurrentUser', user)) + .then(() => timelineFetcher.startFetching({store, credentials: userCredentials})) } commit('endLogin') }) diff --git a/src/services/api/api.service.js b/src/services/api/api.service.js index ac27865f..9e7da9de 100644 --- a/src/services/api/api.service.js +++ b/src/services/api/api.service.js @@ -1,8 +1,8 @@ /* eslint-env browser */ const LOGIN_URL = '/api/account/verify_credentials.json' -// const FRIENDS_TIMELINE_URL='/api/statuses/friends_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 FRIENDS_TIMELINE_URL = '/api/statuses/friends_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 CONVERSATION_URL = '/api/statusnet/conversation/'; // const STATUS_UPDATE_URL = '/api/statuses/update.json'; // 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 { merge } from 'lodash'; -const apiService = { - verifyCredentials: (user) => { - const base64 = btoa(`${user.username}:${user.password}`) - const authHeaders = { 'Authorization': `Basic ${base64}` } - return fetch(LOGIN_URL, { - method: 'POST', - headers: authHeaders - }) - // return $http.post(LOGIN_URL, null, { headers: authHeaders }); +const authHeaders = (user) => ({ 'Authorization': `Basic ${btoa(`${user.username}:${user.password}`)}` }) + +const fetchTimeline = ({timeline, credentials, since = false, until = false}) => { + 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, { + method: 'POST', + headers: authHeaders(user) + }) +} + +const apiService = { + verifyCredentials, + fetchTimeline } export default apiService diff --git a/src/services/timeline_fetcher/timeline_fetcher.service.js b/src/services/timeline_fetcher/timeline_fetcher.service.js index 9013c82e..80bcb415 100644 --- a/src/services/timeline_fetcher/timeline_fetcher.service.js +++ b/src/services/timeline_fetcher/timeline_fetcher.service.js @@ -1,53 +1,93 @@ -import { upperFirst, camelCase } from 'lodash'; +import { camelCase } from 'lodash' -const timelineFetcherServiceFactory = ($ngRedux, apiService, $interval) => { - let fetcher; +import apiService from '../api/api.service.js' - const update = ({statuses, timeline, showImmediately}) => { - const ccTimeline = camelCase(timeline); +const update = ({store, statuses, timeline, showImmediately}) => { + const ccTimeline = camelCase(timeline) - const action = { - type: 'ADD_NEW_STATUSES', - data: { - statuses, - timeline: ccTimeline, - showImmediately - } - }; + store.commit('addNewStatuses', { + timeline: ccTimeline, + statuses, + showImmediately + }) - $ngRedux.dispatch(action); - $ngRedux.dispatch({type: 'UPDATE_TIMESTAMPS'}); - }; + // $ngRedux.dispatch({type: 'UPDATE_TIMESTAMPS'}); +} - const fetchAndUpdate = ({timeline = 'friends', older = false, showImmediately = false}) => { - const args = { timeline }; - const timelineData = $ngRedux.getState().statuses.timelines[camelCase(timeline)]; +const fetchAndUpdate = ({store, credentials, timeline = 'friends', older = false, showImmediately = false}) => { + const args = { timeline, credentials } + const timelineData = store.rootState.statuses.timelines[camelCase(timeline)] - if(older) { - args['until'] = timelineData.minVisibleId; - } else { - args['since'] = timelineData.maxId; - } + if (older) { + args['until'] = timelineData.minVisibleId + } else { + args['since'] = timelineData.maxId + } - apiService.fetchTimeline(args). - then((statuses) => update({statuses, timeline, showImmediately})); - }; + apiService.fetchTimeline(args) + .then((statuses) => update({store, statuses, timeline, showImmediately})) +} - const startFetching = ({timeline = 'friends'}) => { - fetchAndUpdate({timeline, showImmediately: true}); +const startFetching = ({ timeline = 'friends', credentials, store }) => { + fetchAndUpdate({timeline, credentials, store, showImmediately: true}) + const boundFetchAndUpdate = () => fetchAndUpdate({ timeline, credentials, store }) - const boundFetchAndUpdate = () => fetchAndUpdate({timeline}); - fetcher = $interval(boundFetchAndUpdate, 10000); - }; + setInterval(boundFetchAndUpdate, 10000) +} +const timelineFetcher = { + startFetching +} - const timelineFetcherService = { - startFetching, - fetchAndUpdate - }; +export default timelineFetcher - 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;