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 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')
})

View file

@ -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}` }
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
headers: authHeaders(user)
})
// return $http.post(LOGIN_URL, null, { headers: authHeaders });
}
}
const apiService = {
verifyCredentials,
fetchTimeline
}
export default apiService

View file

@ -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,
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;
if (older) {
args['until'] = timelineData.minVisibleId
} else {
args['since'] = timelineData.maxId;
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;