diff --git a/src/modules/statuses.js b/src/modules/statuses.js
index d6bdba65..eee368a3 100644
--- a/src/modules/statuses.js
+++ b/src/modules/statuses.js
@@ -1,5 +1,6 @@
import { map, slice, last, intersectionBy, sortBy, unionBy, toInteger, groupBy, differenceBy, each, find } from 'lodash'
import moment from 'moment'
+import apiService from '../services/api/api.service.js'
const defaultState = {
allStatuses: [],
@@ -104,6 +105,18 @@ const updateTimestampsInStatuses = (statuses) => {
const statuses = {
state: defaultState,
+ actions: {
+ favorite ({ rootState, commit }, status) {
+ // Optimistic favoriting...
+ commit('setFavorited', { status, value: true })
+ apiService.favorite({ id: status.id, credentials: rootState.users.currentUser.credentials })
+ },
+ unfavorite ({ rootState, commit }, status) {
+ // Optimistic favoriting...
+ commit('setFavorited', { status, value: false })
+ apiService.unfavorite({ id: status.id, credentials: rootState.users.currentUser.credentials })
+ }
+ },
mutations: {
addNewStatuses (state, { statuses, showImmediately = false, timeline }) {
state.timelines[timeline] = addStatusesToTimeline(statuses, showImmediately, state.timelines[timeline])
@@ -118,6 +131,10 @@ const statuses = {
updateTimestamps (state) {
updateTimestampsInStatuses(state.allStatuses)
},
+ setFavorited (state, { status, value }) {
+ const newStatus = find(state.allStatuses, status)
+ newStatus.favorited = value
+ },
setNsfw (state, { id, nsfw }) {
// For now, walk through all the statuses because the stuff might be in the replied_to_status
// TODO: Save the replied_tos as references.
diff --git a/src/modules/users.js b/src/modules/users.js
index 3f6864a2..cf2b16f0 100644
--- a/src/modules/users.js
+++ b/src/modules/users.js
@@ -25,7 +25,10 @@ const users = {
.then((response) => {
if (response.ok) {
response.json()
- .then((user) => commit('setCurrentUser', user))
+ .then((user) => {
+ user.credentials = userCredentials
+ 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 9e7da9de..5a46451b 100644
--- a/src/services/api/api.service.js
+++ b/src/services/api/api.service.js
@@ -3,11 +3,11 @@ 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 FAVORITE_URL = '/api/favorites/create'
+const UNFAVORITE_URL = '/api/favorites/destroy'
// const CONVERSATION_URL = '/api/statusnet/conversation/';
// const STATUS_UPDATE_URL = '/api/statuses/update.json';
// const MEDIA_UPLOAD_URL = '/api/statusnet/media/upload';
-// const FAVORITE_URL = '/api/favorites/create';
-// const UNFAVORITE_URL = '/api/favorites/destroy';
// const FORM_CONTENT_TYPE = {'Content-Type': 'application/x-www-form-urlencoded'};
@@ -43,9 +43,25 @@ const verifyCredentials = (user) => {
})
}
+const favorite = ({ id, credentials }) => {
+ return fetch(`${FAVORITE_URL}/${id}.json`, {
+ headers: authHeaders(credentials),
+ method: 'POST'
+ })
+}
+
+const unfavorite = ({ id, credentials }) => {
+ return fetch(`${UNFAVORITE_URL}/${id}.json`, {
+ headers: authHeaders(credentials),
+ method: 'POST'
+ })
+}
+
const apiService = {
verifyCredentials,
- fetchTimeline
+ fetchTimeline,
+ favorite,
+ unfavorite
}
export default apiService
@@ -104,7 +120,6 @@ export default apiService
// });
// };
-// 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