2023-01-16 09:14:51 +00:00
|
|
|
/**
|
2018-10-10 07:46:26 +00:00
|
|
|
* @copyright Copyright (c) 2018 Julius Härtl <jus@bitgrid.net>
|
|
|
|
*
|
2019-10-15 10:16:38 +00:00
|
|
|
* @file Timeline related store
|
|
|
|
*
|
2018-10-10 07:46:26 +00:00
|
|
|
* @author Julius Härtl <jus@bitgrid.net>
|
2019-04-26 15:15:21 +00:00
|
|
|
* @author Jonas Sulzer <jonas@violoncello.ch>
|
2018-10-10 07:46:26 +00:00
|
|
|
*
|
2023-01-16 09:14:51 +00:00
|
|
|
* @license AGPL-3.0-or-later
|
2018-10-10 07:46:26 +00:00
|
|
|
*
|
|
|
|
* 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/>.
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
2022-10-27 13:07:12 +00:00
|
|
|
import logger from '../services/logger.js'
|
2019-11-05 15:41:02 +00:00
|
|
|
import axios from '@nextcloud/axios'
|
2018-11-21 17:27:33 +00:00
|
|
|
import Vue from 'vue'
|
2020-10-09 16:16:40 +00:00
|
|
|
import { generateUrl } from '@nextcloud/router'
|
2018-11-13 14:20:43 +00:00
|
|
|
|
2019-10-15 10:16:38 +00:00
|
|
|
/**
|
2022-10-27 13:07:12 +00:00
|
|
|
* @property {object} timeline - The posts' collection
|
2023-01-16 09:14:51 +00:00
|
|
|
* @property {number} since - Time (EPOCH) of the most recent post
|
2022-10-27 13:07:12 +00:00
|
|
|
* @property {string} type - Timeline's type: 'home', 'single-post',...
|
|
|
|
* @property {object} params - Timeline's parameters
|
|
|
|
* @property {string} account -
|
2019-10-15 10:16:38 +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-10-15 10:16:38 +00:00
|
|
|
/**
|
|
|
|
* @namespace params
|
2022-10-27 13:07:12 +00:00
|
|
|
* @property {string} account ???
|
|
|
|
* @property {string} id
|
|
|
|
* @property {string} localId
|
|
|
|
* @property {string} type ???
|
2019-10-15 10:16:38 +00:00
|
|
|
*/
|
2019-01-11 08:36:49 +00:00
|
|
|
params: {},
|
2019-10-15 10:16:38 +00:00
|
|
|
account: '',
|
2019-10-15 14:00:43 +00:00
|
|
|
/* Tells whether the composer should be displayed or not.
|
|
|
|
* It's up to the view to honor this status or not.
|
2019-10-15 10:16:38 +00:00
|
|
|
* @member {boolean}
|
|
|
|
*/
|
2022-10-27 13:07:12 +00:00
|
|
|
composerDisplayStatus: false,
|
2018-10-26 08:33:27 +00:00
|
|
|
}
|
2018-10-10 07:46:26 +00:00
|
|
|
const mutations = {
|
|
|
|
addToTimeline(state, data) {
|
2022-10-27 13:07:12 +00:00
|
|
|
for (const 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
|
|
|
|
},
|
2019-10-15 10:16:38 +00:00
|
|
|
setComposerDisplayStatus(state, status) {
|
|
|
|
state.composerDisplayStatus = status
|
|
|
|
},
|
2018-12-05 09:50:37 +00:00
|
|
|
setAccount(state, account) {
|
|
|
|
state.account = account
|
2019-05-04 11:11:39 +00:00
|
|
|
},
|
2019-07-10 15:39:49 +00:00
|
|
|
likePost(state, { post, parentAnnounce }) {
|
|
|
|
if (typeof state.timeline[post.id] !== 'undefined') {
|
|
|
|
Vue.set(state.timeline[post.id].action.values, 'liked', true)
|
|
|
|
}
|
|
|
|
if (typeof parentAnnounce.id !== 'undefined') {
|
|
|
|
Vue.set(state.timeline[parentAnnounce.id].cache[parentAnnounce.object].object.action.values, 'liked', true)
|
|
|
|
}
|
|
|
|
},
|
|
|
|
unlikePost(state, { post, parentAnnounce }) {
|
|
|
|
if (typeof state.timeline[post.id] !== 'undefined') {
|
|
|
|
Vue.set(state.timeline[post.id].action.values, 'liked', false)
|
|
|
|
}
|
|
|
|
if (typeof parentAnnounce.id !== 'undefined') {
|
|
|
|
Vue.set(state.timeline[parentAnnounce.id].cache[parentAnnounce.object].object.action.values, 'liked', false)
|
|
|
|
}
|
|
|
|
},
|
2019-05-28 14:08:09 +00:00
|
|
|
boostPost(state, { post, parentAnnounce }) {
|
2019-05-29 21:58:16 +00:00
|
|
|
if (typeof state.timeline[post.id] !== 'undefined') {
|
2019-05-29 17:51:16 +00:00
|
|
|
Vue.set(state.timeline[post.id].action.values, 'boosted', true)
|
|
|
|
}
|
2019-05-29 21:58:16 +00:00
|
|
|
if (typeof parentAnnounce.id !== 'undefined') {
|
2019-05-29 21:35:30 +00:00
|
|
|
Vue.set(state.timeline[parentAnnounce.id].cache[parentAnnounce.object].object.action.values, 'boosted', true)
|
2019-05-28 14:08:09 +00:00
|
|
|
}
|
2019-05-04 11:11:39 +00:00
|
|
|
},
|
2019-05-28 14:08:09 +00:00
|
|
|
unboostPost(state, { post, parentAnnounce }) {
|
2019-05-29 21:58:16 +00:00
|
|
|
if (typeof state.timeline[post.id] !== 'undefined') {
|
2019-05-29 17:51:16 +00:00
|
|
|
Vue.set(state.timeline[post.id].action.values, 'boosted', false)
|
|
|
|
}
|
2019-05-29 21:58:16 +00:00
|
|
|
if (typeof parentAnnounce.id !== 'undefined') {
|
2019-05-29 21:35:30 +00:00
|
|
|
Vue.set(state.timeline[parentAnnounce.id].cache[parentAnnounce.object].object.action.values, 'boosted', false)
|
2019-05-28 14:08:09 +00:00
|
|
|
}
|
2022-10-27 13:07:12 +00:00
|
|
|
},
|
2018-10-26 08:33:27 +00:00
|
|
|
}
|
2018-10-10 07:46:26 +00:00
|
|
|
const getters = {
|
2019-10-15 10:16:38 +00:00
|
|
|
getComposerDisplayStatus(state) {
|
|
|
|
return state.composerDisplayStatus
|
|
|
|
},
|
2018-10-10 07:46:26 +00:00
|
|
|
getTimeline(state) {
|
2018-11-21 17:27:33 +00:00
|
|
|
return Object.values(state.timeline).sort(function(a, b) {
|
|
|
|
return b.publishedTime - a.publishedTime
|
|
|
|
})
|
2019-09-15 10:51:13 +00:00
|
|
|
},
|
|
|
|
getPostFromTimeline(state) {
|
|
|
|
return (postId) => {
|
|
|
|
if (typeof state.timeline[postId] !== 'undefined') {
|
|
|
|
return state.timeline[postId]
|
|
|
|
} else {
|
2022-10-27 13:07:12 +00:00
|
|
|
logger.warn('Could not find post in timeline', { postId })
|
2019-09-15 10:51:13 +00:00
|
|
|
}
|
|
|
|
}
|
2022-10-27 13:07:12 +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
|
|
|
},
|
2022-09-14 10:37:18 +00:00
|
|
|
async post(context, post) {
|
|
|
|
try {
|
2022-10-27 11:49:47 +00:00
|
|
|
const { data } = await axios.post(generateUrl('apps/social/api/v1/post'), post, {
|
2022-09-14 10:37:18 +00:00
|
|
|
headers: {
|
2022-10-27 13:07:12 +00:00
|
|
|
'Content-Type': 'multipart/form-data',
|
|
|
|
},
|
2018-12-20 15:34:55 +00:00
|
|
|
})
|
2022-10-27 13:07:12 +00:00
|
|
|
logger.info('Post created with token ' + data.result.token)
|
|
|
|
} catch (error) {
|
|
|
|
OC.Notification.showTemporary('Failed to create a post')
|
|
|
|
logger.error('Failed to create a post', { error: error.response })
|
2022-09-14 10:37:18 +00:00
|
|
|
}
|
2018-11-13 14:20:43 +00:00
|
|
|
},
|
2019-01-11 09:15:31 +00:00
|
|
|
postDelete(context, post) {
|
2020-10-09 16:16:40 +00:00
|
|
|
return axios.delete(generateUrl(`apps/social/api/v1/post?id=${post.id}`)).then((response) => {
|
2019-01-11 09:15:31 +00:00
|
|
|
context.commit('removePost', post)
|
2022-10-27 13:07:12 +00:00
|
|
|
logger.info('Post deleted with token ' + response.data.result.token)
|
2019-01-11 09:15:31 +00:00
|
|
|
}).catch((error) => {
|
|
|
|
OC.Notification.showTemporary('Failed to delete the post')
|
2022-10-27 13:07:12 +00:00
|
|
|
logger.error('Failed to delete the post', { error })
|
2019-01-11 09:15:31 +00:00
|
|
|
})
|
|
|
|
},
|
2019-07-10 15:39:49 +00:00
|
|
|
postLike(context, { post, parentAnnounce }) {
|
|
|
|
return new Promise((resolve, reject) => {
|
2020-10-09 16:16:40 +00:00
|
|
|
axios.post(generateUrl(`apps/social/api/v1/post/like?postId=${post.id}`)).then((response) => {
|
2019-07-10 15:39:49 +00:00
|
|
|
context.commit('likePost', { post, parentAnnounce })
|
|
|
|
resolve(response)
|
|
|
|
}).catch((error) => {
|
|
|
|
OC.Notification.showTemporary('Failed to like post')
|
2022-10-27 13:07:12 +00:00
|
|
|
logger.error('Failed to like post', { error: error.response })
|
2019-07-10 15:39:49 +00:00
|
|
|
reject(error)
|
|
|
|
})
|
|
|
|
})
|
|
|
|
},
|
|
|
|
postUnlike(context, { post, parentAnnounce }) {
|
2020-10-09 16:16:40 +00:00
|
|
|
return axios.delete(generateUrl(`apps/social/api/v1/post/like?postId=${post.id}`)).then((response) => {
|
2019-07-10 15:39:49 +00:00
|
|
|
context.commit('unlikePost', { post, parentAnnounce })
|
2019-07-26 15:11:00 +00:00
|
|
|
// Remove post from list if we are in the 'liked' timeline
|
|
|
|
if (state.type === 'liked') {
|
|
|
|
context.commit('removePost', post)
|
|
|
|
}
|
2019-07-10 15:39:49 +00:00
|
|
|
}).catch((error) => {
|
|
|
|
OC.Notification.showTemporary('Failed to unlike post')
|
2022-10-27 13:07:12 +00:00
|
|
|
logger.error('Failed to unlike post', { error })
|
2019-07-10 15:39:49 +00:00
|
|
|
})
|
|
|
|
},
|
2019-05-28 14:08:09 +00:00
|
|
|
postBoost(context, { post, parentAnnounce }) {
|
2019-03-12 15:38:15 +00:00
|
|
|
return new Promise((resolve, reject) => {
|
2020-10-09 16:16:40 +00:00
|
|
|
axios.post(generateUrl(`apps/social/api/v1/post/boost?postId=${post.id}`)).then((response) => {
|
2019-05-28 14:08:09 +00:00
|
|
|
context.commit('boostPost', { post, parentAnnounce })
|
2022-10-27 13:07:12 +00:00
|
|
|
logger.info('Post boosted with token ' + response.data.result.token)
|
2019-03-12 15:38:15 +00:00
|
|
|
resolve(response)
|
|
|
|
}).catch((error) => {
|
|
|
|
OC.Notification.showTemporary('Failed to create a boost post')
|
2022-10-27 13:07:12 +00:00
|
|
|
logger.error('Failed to create a boost post', { error: error.response })
|
2019-03-12 15:38:15 +00:00
|
|
|
reject(error)
|
|
|
|
})
|
|
|
|
})
|
|
|
|
},
|
2019-05-28 14:08:09 +00:00
|
|
|
postUnBoost(context, { post, parentAnnounce }) {
|
2020-10-09 16:16:40 +00:00
|
|
|
return axios.delete(generateUrl(`apps/social/api/v1/post/boost?postId=${post.id}`)).then((response) => {
|
2019-05-28 14:08:09 +00:00
|
|
|
context.commit('unboostPost', { post, parentAnnounce })
|
2022-10-27 13:07:12 +00:00
|
|
|
logger.info('Boost deleted with token ' + response.data.result.token)
|
2019-04-22 17:56:40 +00:00
|
|
|
}).catch((error) => {
|
|
|
|
OC.Notification.showTemporary('Failed to delete the boost')
|
2022-10-27 13:07:12 +00:00
|
|
|
logger.error('Failed to delete the boost', { error })
|
2019-04-22 17:56:40 +00:00
|
|
|
})
|
|
|
|
},
|
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 }) {
|
2019-09-14 12:23:34 +00:00
|
|
|
|
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
|
|
|
}
|
2019-09-14 12:23:34 +00:00
|
|
|
|
2019-09-14 13:23:24 +00:00
|
|
|
// Compute URl to get the data
|
2019-09-15 10:51:13 +00:00
|
|
|
let url = ''
|
2018-12-05 09:50:37 +00:00
|
|
|
if (state.type === 'account') {
|
2020-10-09 16:16:40 +00:00
|
|
|
url = 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') {
|
2020-10-09 16:16:40 +00:00
|
|
|
url = generateUrl(`apps/social/api/v1/stream/tag/${state.params.tag}?limit=25&since=` + sinceTimestamp)
|
2019-09-14 12:23:34 +00:00
|
|
|
} else if (state.type === 'single-post') {
|
2020-10-09 16:16:40 +00:00
|
|
|
url = generateUrl(`apps/social/local/v1/post/replies?id=${state.params.id}&limit=5&since=` + sinceTimestamp)
|
2018-12-05 09:50:37 +00:00
|
|
|
} else {
|
2020-10-09 16:16:40 +00:00
|
|
|
url = generateUrl(`apps/social/api/v1/stream/${state.type}?limit=25&since=` + sinceTimestamp)
|
2018-12-05 09:50:37 +00:00
|
|
|
}
|
2019-09-14 12:23:34 +00:00
|
|
|
|
2019-09-15 10:51:13 +00:00
|
|
|
// Get the data and add them to the timeline
|
2018-12-05 09:50:37 +00:00
|
|
|
return axios.get(url).then((response) => {
|
2019-09-14 12:23:34 +00:00
|
|
|
|
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
|
|
|
}
|
2019-09-14 12:23:34 +00:00
|
|
|
|
|
|
|
// Add results to timeline
|
2019-10-11 12:45:11 +00:00
|
|
|
context.commit('addToTimeline', response.data.result)
|
2019-09-14 12:23:34 +00:00
|
|
|
|
2018-11-21 11:49:31 +00:00
|
|
|
return response.data
|
2018-11-13 14:20:43 +00:00
|
|
|
})
|
2019-08-15 13:56:29 +00:00
|
|
|
},
|
|
|
|
addToTimeline(context, data) {
|
|
|
|
context.commit('addToTimeline', data)
|
2022-10-27 13:07:12 +00:00
|
|
|
},
|
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 }
|