Update modules.

This commit is contained in:
Roger Braun 2016-10-27 18:03:14 +02:00
parent 43eece2539
commit 945ea5e69f
3 changed files with 144 additions and 91 deletions

View file

@ -82,7 +82,7 @@ const addStatusesToTimeline = (addedStatuses, showImmediately, { statuses, visib
visibleStatuses: newVisibleStatuses, visibleStatuses: newVisibleStatuses,
newStatusCount: newNewStatusCount, newStatusCount: newNewStatusCount,
maxId: newStatuses[0].id, maxId: newStatuses[0].id,
minVisibleId: last(newVisibleStatuses).id, minVisibleId: (last(newVisibleStatuses) || { id: undefined }).id,
faves: unionBy(faves, addedFaves, 'id') faves: unionBy(faves, addedFaves, 'id')
} }
} }

38
src/modules/users.js Normal file
View file

@ -0,0 +1,38 @@
import apiService from '../services/api/api.service.js'
const users = {
state: {
currentUser: false,
loggingIn: false
},
mutations: {
setCurrentUser (state, user) {
state.currentUser = user
},
beginLogin (state) {
state.loggingIn = true
},
endLogin (state) {
state.loggingIn = false
}
},
actions: {
loginUser ({commit, state}, userCredentials) {
commit('beginLogin')
apiService.verifyCredentials(userCredentials)
.then((response) => {
if (response.ok) {
response.json()
.then((user) => commit('setCurrentUser', user))
}
commit('endLogin')
})
.catch((error) => {
console.log(error)
commit('endLogin')
})
}
}
}
export default users

View file

@ -1,101 +1,116 @@
const LOGIN_URL='/api/account/verify_credentials.json'; /* eslint-env browser */
const FRIENDS_TIMELINE_URL='/api/statuses/friends_timeline.json'; const LOGIN_URL = '/api/account/verify_credentials.json'
const PUBLIC_TIMELINE_URL='/api/statuses/public_timeline.json'; // const FRIENDS_TIMELINE_URL='/api/statuses/friends_timeline.json';
const PUBLIC_AND_EXTERNAL_TIMELINE_URL='/api/statuses/public_and_external_timeline.json'; // const PUBLIC_TIMELINE_URL='/api/statuses/public_timeline.json';
const CONVERSATION_URL = '/api/statusnet/conversation/'; // const PUBLIC_AND_EXTERNAL_TIMELINE_URL='/api/statuses/public_and_external_timeline.json';
const STATUS_UPDATE_URL = '/api/statuses/update.json'; // const CONVERSATION_URL = '/api/statusnet/conversation/';
const MEDIA_UPLOAD_URL = '/api/statusnet/media/upload'; // const STATUS_UPDATE_URL = '/api/statuses/update.json';
const FAVORITE_URL = '/api/favorites/create'; // const MEDIA_UPLOAD_URL = '/api/statusnet/media/upload';
const UNFAVORITE_URL = '/api/favorites/destroy'; // const FAVORITE_URL = '/api/favorites/create';
// const UNFAVORITE_URL = '/api/favorites/destroy';
const FORM_CONTENT_TYPE = {'Content-Type': 'application/x-www-form-urlencoded'}; // const FORM_CONTENT_TYPE = {'Content-Type': 'application/x-www-form-urlencoded'};
import { param, ajax } from 'jquery'; // import { param, ajax } from 'jquery';
import { merge } from 'lodash'; // import { merge } from 'lodash';
// TODO: This should probably be in redux.
let authHeaders = {};
const apiServiceFactory = ($http) => {
// Public
const fetchConversation = (id) => {
return $http.get(`${CONVERSATION_URL}/${id}.json?count=100`);
};
const fetchTimeline = ({timeline, 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 }).then((data) => data.json());
};
// Need credentials
const verifyCredentials = (user) => {
const base64 = btoa(`${user.username}:${user.password}`);
authHeaders = { "Authorization": `Basic ${base64}` };
return $http.post(LOGIN_URL, null, { headers: authHeaders });
};
const postStatus = ({status, mediaIds, in_reply_to_status_id}) => {
const idsText = mediaIds.join(',');
const form = new FormData();
form.append('status', status);
form.append('source', 'The Wired FE');
form.append('media_ids', idsText);
if(in_reply_to_status_id) {
form.append('in_reply_to_status_id', in_reply_to_status_id);
};
return fetch(STATUS_UPDATE_URL, {
body: form,
method: 'POST',
headers: authHeaders
});
};
const favorite = (id) => $http.post(`${FAVORITE_URL}/${id}.json`, null, {headers: authHeaders});
const unfavorite = (id) => $http.post(`${UNFAVORITE_URL}/${id}.json`, null, {headers: authHeaders});
// This was impossible to get to work with $http. You're supposed to set Content-Type
// undefined in the header so it sends the correct header. It would always send a json
// content type. This method from jQuery worked right away...
// Also, this method is only available as XML output. OLOLOLOLO
const uploadMedia = (formData) => ajax({
url: MEDIA_UPLOAD_URL,
data: formData,
type: 'POST',
processData: false,
contentType: false,
headers: authHeaders
});
const apiService = { const apiService = {
verifyCredentials, verifyCredentials: (user) => {
fetchConversation, const base64 = btoa(`${user.username}:${user.password}`)
postStatus, const authHeaders = { 'Authorization': `Basic ${base64}` }
uploadMedia, return fetch(LOGIN_URL, {
favorite, method: 'POST',
unfavorite, headers: authHeaders
fetchTimeline })
}; // return $http.post(LOGIN_URL, null, { headers: authHeaders });
}
}
return apiService; export default apiService
};
apiServiceFactory.$inject = ['$http']; // // TODO: This should probably be in redux.
// let authHeaders = {};
export default apiServiceFactory; // const apiServiceFactory = ($http) => {
// // Public
// const fetchConversation = (id) => {
// return $http.get(`${CONVERSATION_URL}/${id}.json?count=100`);
// };
// const fetchTimeline = ({timeline, 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 }).then((data) => data.json());
// };
// // Need credentials
// const verifyCredentials = (user) => {
// const base64 = btoa(`${user.username}:${user.password}`);
// authHeaders = { "Authorization": `Basic ${base64}` };
// return $http.post(LOGIN_URL, null, { headers: authHeaders });
// };
// const postStatus = ({status, mediaIds, in_reply_to_status_id}) => {
// const idsText = mediaIds.join(',');
// const form = new FormData();
// form.append('status', status);
// form.append('source', 'The Wired FE');
// form.append('media_ids', idsText);
// if(in_reply_to_status_id) {
// form.append('in_reply_to_status_id', in_reply_to_status_id);
// };
// return fetch(STATUS_UPDATE_URL, {
// body: form,
// method: 'POST',
// headers: authHeaders
// });
// };
// const favorite = (id) => $http.post(`${FAVORITE_URL}/${id}.json`, null, {headers: authHeaders});
// const unfavorite = (id) => $http.post(`${UNFAVORITE_URL}/${id}.json`, null, {headers: authHeaders});
// // This was impossible to get to work with $http. You're supposed to set Content-Type
// // undefined in the header so it sends the correct header. It would always send a json
// // content type. This method from jQuery worked right away...
// // Also, this method is only available as XML output. OLOLOLOLO
// const uploadMedia = (formData) => ajax({
// url: MEDIA_UPLOAD_URL,
// data: formData,
// type: 'POST',
// processData: false,
// contentType: false,
// headers: authHeaders
// });
// const apiService = {
// verifyCredentials,
// fetchConversation,
// postStatus,
// uploadMedia,
// favorite,
// unfavorite,
// fetchTimeline
// };
// return apiService;
// };
// apiServiceFactory.$inject = ['$http'];
// export default apiServiceFactory;