2018-10-10 07:46:26 +00:00
|
|
|
/*
|
|
|
|
* @copyright Copyright (c) 2018 Julius Härtl <jus@bitgrid.net>
|
|
|
|
*
|
|
|
|
* @author Julius Härtl <jus@bitgrid.net>
|
|
|
|
*
|
|
|
|
* @license GNU AGPL version 3 or any later version
|
|
|
|
*
|
|
|
|
* This program is free software: you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU Affero General Public License as
|
|
|
|
* published by the Free Software Foundation, either version 3 of the
|
|
|
|
* License, or (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU Affero General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU Affero General Public License
|
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
2018-11-13 14:20:43 +00:00
|
|
|
import axios from 'nextcloud-axios'
|
2018-11-21 17:27:33 +00:00
|
|
|
import Vue from 'vue'
|
2018-11-13 14:20:43 +00:00
|
|
|
|
2018-10-10 07:46:26 +00:00
|
|
|
const state = {
|
2018-11-21 17:27:33 +00:00
|
|
|
timeline: {},
|
2018-11-22 14:42:10 +00:00
|
|
|
since: Math.floor(Date.now() / 1000) + 1,
|
2018-12-05 09:50:37 +00:00
|
|
|
type: 'home',
|
2019-01-11 08:36:49 +00:00
|
|
|
params: {},
|
2018-12-05 09:50:37 +00:00
|
|
|
account: ''
|
2018-10-26 08:33:27 +00:00
|
|
|
}
|
2018-10-10 07:46:26 +00:00
|
|
|
const mutations = {
|
|
|
|
addToTimeline(state, data) {
|
|
|
|
for (let item in data) {
|
2018-11-22 14:42:10 +00:00
|
|
|
state.since = data[item].publishedTime
|
2018-11-21 17:27:33 +00:00
|
|
|
Vue.set(state.timeline, data[item].id, data[item])
|
2018-10-10 07:46:26 +00:00
|
|
|
}
|
2018-11-21 14:23:25 +00:00
|
|
|
},
|
2019-01-11 09:15:31 +00:00
|
|
|
removePost(state, post) {
|
|
|
|
Vue.delete(state.timeline, post.id)
|
|
|
|
},
|
2018-11-22 14:42:10 +00:00
|
|
|
resetTimeline(state) {
|
|
|
|
state.timeline = {}
|
2018-11-28 10:28:11 +00:00
|
|
|
state.since = Math.floor(Date.now() / 1000) + 1
|
2018-11-22 14:42:10 +00:00
|
|
|
},
|
|
|
|
setTimelineType(state, type) {
|
|
|
|
state.type = type
|
2018-12-05 09:50:37 +00:00
|
|
|
},
|
2019-01-11 08:36:49 +00:00
|
|
|
setTimelineParams(state, params) {
|
|
|
|
state.params = params
|
|
|
|
},
|
2018-12-05 09:50:37 +00:00
|
|
|
setAccount(state, account) {
|
|
|
|
state.account = account
|
2018-10-10 07:46:26 +00:00
|
|
|
}
|
2018-10-26 08:33:27 +00:00
|
|
|
}
|
2018-10-10 07:46:26 +00:00
|
|
|
const getters = {
|
|
|
|
getTimeline(state) {
|
2018-11-21 17:27:33 +00:00
|
|
|
return Object.values(state.timeline).sort(function(a, b) {
|
|
|
|
return b.publishedTime - a.publishedTime
|
|
|
|
})
|
2018-10-10 07:46:26 +00:00
|
|
|
}
|
2018-10-26 08:33:27 +00:00
|
|
|
}
|
2018-11-13 14:20:43 +00:00
|
|
|
const actions = {
|
2019-01-11 08:36:49 +00:00
|
|
|
changeTimelineType(context, { type, params }) {
|
2018-11-22 14:42:10 +00:00
|
|
|
context.commit('resetTimeline')
|
|
|
|
context.commit('setTimelineType', type)
|
2019-01-11 08:36:49 +00:00
|
|
|
context.commit('setTimelineParams', params)
|
2018-12-05 09:50:37 +00:00
|
|
|
context.commit('setAccount', '')
|
|
|
|
},
|
|
|
|
changeTimelineTypeAccount(context, account) {
|
|
|
|
context.commit('resetTimeline')
|
|
|
|
context.commit('setTimelineType', 'account')
|
|
|
|
context.commit('setAccount', account)
|
2018-11-22 14:42:10 +00:00
|
|
|
},
|
2018-11-13 14:20:43 +00:00
|
|
|
post(context, post) {
|
2018-12-20 15:34:55 +00:00
|
|
|
return new Promise((resolve, reject) => {
|
|
|
|
axios.post(OC.generateUrl('apps/social/api/v1/post'), { data: post }).then((response) => {
|
|
|
|
// eslint-disable-next-line no-console
|
|
|
|
console.log('Post created with token ' + response.data.result.token)
|
|
|
|
resolve(response)
|
|
|
|
}).catch((error) => {
|
|
|
|
OC.Notification.showTemporary('Failed to create a post')
|
|
|
|
console.error('Failed to create a post', error.response)
|
|
|
|
reject(error)
|
|
|
|
})
|
2018-11-13 14:20:43 +00:00
|
|
|
})
|
|
|
|
},
|
2019-01-11 09:15:31 +00:00
|
|
|
postDelete(context, post) {
|
|
|
|
return axios.delete(OC.generateUrl(`apps/social/api/v1/post?id=${post.id}`)).then((response) => {
|
|
|
|
context.commit('removePost', post)
|
|
|
|
// eslint-disable-next-line no-console
|
|
|
|
console.log('Post deleted with token ' + response.data.result.token)
|
|
|
|
}).catch((error) => {
|
|
|
|
OC.Notification.showTemporary('Failed to delete the post')
|
|
|
|
console.error('Failed to delete the post', error)
|
|
|
|
})
|
|
|
|
},
|
2019-03-12 15:38:15 +00:00
|
|
|
postBoost(context, post) {
|
|
|
|
return new Promise((resolve, reject) => {
|
|
|
|
axios.post(OC.generateUrl(`apps/social/api/v1/boost?postId=${post.id}`)).then((response) => {
|
|
|
|
// eslint-disable-next-line no-console
|
|
|
|
console.log('Post boosted with token ' + response.data.result.token)
|
|
|
|
resolve(response)
|
|
|
|
}).catch((error) => {
|
|
|
|
OC.Notification.showTemporary('Failed to create a boost post')
|
|
|
|
console.error('Failed to create a boost post', error.response)
|
|
|
|
reject(error)
|
|
|
|
})
|
|
|
|
})
|
|
|
|
},
|
2018-12-05 09:50:37 +00:00
|
|
|
refreshTimeline(context) {
|
|
|
|
return this.dispatch('fetchTimeline', { sinceTimestamp: Math.floor(Date.now() / 1000) + 1 })
|
2018-11-21 17:27:33 +00:00
|
|
|
},
|
2018-12-05 09:50:37 +00:00
|
|
|
fetchTimeline(context, { sinceTimestamp }) {
|
2018-11-21 17:27:33 +00:00
|
|
|
if (typeof sinceTimestamp === 'undefined') {
|
2018-11-22 14:42:10 +00:00
|
|
|
sinceTimestamp = state.since - 1
|
2018-11-21 17:27:33 +00:00
|
|
|
}
|
2018-12-05 09:50:37 +00:00
|
|
|
let url
|
|
|
|
if (state.type === 'account') {
|
|
|
|
url = OC.generateUrl(`apps/social/api/v1/account/${state.account}/stream?limit=25&since=` + sinceTimestamp)
|
2019-01-11 08:36:49 +00:00
|
|
|
} else if (state.type === 'tags') {
|
|
|
|
url = OC.generateUrl(`apps/social/api/v1/stream/tag/${state.params.tag}?limit=25&since=` + sinceTimestamp)
|
2018-12-05 09:50:37 +00:00
|
|
|
} else {
|
|
|
|
url = OC.generateUrl(`apps/social/api/v1/stream/${state.type}?limit=25&since=` + sinceTimestamp)
|
|
|
|
}
|
|
|
|
return axios.get(url).then((response) => {
|
2018-11-19 20:54:26 +00:00
|
|
|
if (response.status === -1) {
|
2018-11-21 11:49:31 +00:00
|
|
|
throw response.message
|
2018-11-19 20:54:26 +00:00
|
|
|
}
|
2018-11-21 11:49:31 +00:00
|
|
|
context.commit('addToTimeline', response.data.result)
|
|
|
|
return response.data
|
2018-11-13 14:20:43 +00:00
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
2018-10-10 07:46:26 +00:00
|
|
|
|
2018-10-26 08:33:27 +00:00
|
|
|
export default { state, mutations, getters, actions }
|