2020-09-21 12:25:50 +00:00
|
|
|
/*
|
|
|
|
* Copyright (c) 2020. The Nextcloud Bookmarks contributors.
|
|
|
|
*
|
|
|
|
* This file is licensed under the Affero General Public License version 3 or later. See the COPYING file.
|
|
|
|
*/
|
|
|
|
|
2020-03-25 12:19:17 +00:00
|
|
|
import axios from '@nextcloud/axios'
|
|
|
|
import { generateUrl } from '@nextcloud/router'
|
2020-09-25 11:12:42 +00:00
|
|
|
import { loadState } from '@nextcloud/initial-state'
|
2019-09-24 15:18:29 +00:00
|
|
|
import AppGlobal from '../mixins/AppGlobal'
|
|
|
|
import { mutations } from './mutations'
|
2020-10-12 20:09:19 +00:00
|
|
|
import { Parallel } from 'async-parallel'
|
2019-08-29 09:05:29 +00:00
|
|
|
|
2019-09-24 15:18:29 +00:00
|
|
|
const BATCH_SIZE = 42
|
2019-08-29 09:05:29 +00:00
|
|
|
|
|
|
|
export const actions = {
|
|
|
|
ADD_ALL_BOOKMARKS: 'ADD_ALL_BOOKMARKS',
|
2020-04-08 14:48:16 +00:00
|
|
|
COUNT_BOOKMARKS: 'COUNT_BOOKMARKS',
|
2020-09-09 15:06:09 +00:00
|
|
|
COUNT_UNAVAILABLE: 'COUNT_UNAVAILABLE',
|
2020-09-14 08:26:53 +00:00
|
|
|
COUNT_ARCHIVED: 'COUNT_ARCHIVED',
|
2019-08-29 09:05:29 +00:00
|
|
|
CREATE_BOOKMARK: 'CREATE_BOOKMARK',
|
|
|
|
FIND_BOOKMARK: 'FIND_BOOKMARK',
|
2020-08-02 06:16:08 +00:00
|
|
|
LOAD_BOOKMARK: 'LOAD_BOOKMARK',
|
2019-08-29 09:05:29 +00:00
|
|
|
DELETE_BOOKMARK: 'DELETE_BOOKMARK',
|
|
|
|
OPEN_BOOKMARK: 'OPEN_BOOKMARK',
|
|
|
|
SAVE_BOOKMARK: 'SAVE_BOOKMARK',
|
|
|
|
MOVE_BOOKMARK: 'MOVE_BOOKMARK',
|
|
|
|
IMPORT_BOOKMARKS: 'IMPORT_BOOKMARKS',
|
2019-10-05 21:24:45 +00:00
|
|
|
DELETE_BOOKMARKS: 'DELETE_BOOKMARKS',
|
2019-08-29 09:05:29 +00:00
|
|
|
|
|
|
|
LOAD_TAGS: 'LOAD_TAGS',
|
|
|
|
RENAME_TAG: 'RENAME_TAG',
|
|
|
|
DELETE_TAG: 'DELETE_TAG',
|
|
|
|
|
|
|
|
LOAD_FOLDERS: 'LOAD_FOLDERS',
|
|
|
|
CREATE_FOLDER: 'CREATE_FOLDER',
|
|
|
|
SAVE_FOLDER: 'SAVE_FOLDER',
|
|
|
|
DELETE_FOLDER: 'DELETE_FOLDER',
|
2020-07-23 13:12:45 +00:00
|
|
|
LOAD_FOLDER_CHILDREN_ORDER: 'LOAD_FOLDER_CHILDREN_ORDER',
|
2020-03-28 17:15:59 +00:00
|
|
|
OPEN_FOLDER_DETAILS: 'OPEN_FOLDER_DETAILS',
|
2019-08-29 09:05:29 +00:00
|
|
|
|
|
|
|
MOVE_SELECTION: 'MOVE_SELECTION',
|
2019-10-11 12:14:49 +00:00
|
|
|
DELETE_SELECTION: 'DELETE_SELECTION',
|
2019-08-29 09:05:29 +00:00
|
|
|
|
|
|
|
RELOAD_VIEW: 'RELOAD_VIEW',
|
|
|
|
|
|
|
|
NO_FILTER: 'NO_FILTER',
|
|
|
|
FILTER_BY_RECENT: 'FILTER_BY_RECENT',
|
|
|
|
FILTER_BY_UNTAGGED: 'FILTER_BY_UNTAGGED',
|
2020-08-07 15:59:49 +00:00
|
|
|
FILTER_BY_UNAVAILABLE: 'FILTER_BY_UNAVAILABLE',
|
2020-09-11 16:57:58 +00:00
|
|
|
FILTER_BY_ARCHIVED: 'FILTER_BY_ARCHIVED',
|
2019-08-29 09:05:29 +00:00
|
|
|
FILTER_BY_TAGS: 'FILTER_BY_TAGS',
|
|
|
|
FILTER_BY_FOLDER: 'FILTER_BY_FOLDER',
|
|
|
|
FILTER_BY_SEARCH: 'FILTER_BY_SEARCH',
|
|
|
|
FETCH_PAGE: 'FETCH_PAGE',
|
2020-08-14 21:27:40 +00:00
|
|
|
FETCH_ALL: 'FETCH_ALL',
|
2019-08-29 09:05:29 +00:00
|
|
|
|
|
|
|
SET_SETTING: 'SET_SETTING',
|
|
|
|
LOAD_SETTING: 'LOAD_SETTING',
|
2019-12-10 21:33:54 +00:00
|
|
|
LOAD_SETTINGS: 'SLOAD_SETTINGS',
|
2020-03-28 17:15:59 +00:00
|
|
|
|
|
|
|
LOAD_SHARES_OF_FOLDER: 'LOAD_SHARES_OF_FOLDER',
|
2020-03-30 12:31:54 +00:00
|
|
|
CREATE_SHARE: 'CREATE_SHARE',
|
|
|
|
EDIT_SHARE: 'EDIT_SHARE',
|
|
|
|
DELETE_SHARE: 'DELETE_SHARE',
|
|
|
|
|
2020-03-28 17:15:59 +00:00
|
|
|
LOAD_PUBLIC_LINK: 'LOAD_PUBLIC_LINK',
|
|
|
|
CREATE_PUBLIC_LINK: 'CREATE_PUBLIC_LINK',
|
|
|
|
DELETE_PUBLIC_LINK: 'DELETE_PUBLIC_LINK',
|
2019-09-24 15:18:29 +00:00
|
|
|
}
|
2019-08-29 09:05:29 +00:00
|
|
|
|
|
|
|
export default {
|
|
|
|
[actions.ADD_ALL_BOOKMARKS]({ commit }, bookmarks) {
|
|
|
|
for (const bookmark of bookmarks) {
|
2019-09-24 15:18:29 +00:00
|
|
|
commit(mutations.ADD_BOOKMARK, bookmark)
|
2019-08-29 09:05:29 +00:00
|
|
|
}
|
|
|
|
},
|
|
|
|
|
2020-09-09 15:06:09 +00:00
|
|
|
async [actions.COUNT_UNAVAILABLE]({ commit, dispatch, state }, folderId) {
|
|
|
|
try {
|
|
|
|
const response = await axios.get(url(state, '/bookmark/unavailable')
|
|
|
|
)
|
|
|
|
const {
|
|
|
|
data: { item: count, data, status },
|
|
|
|
} = response
|
|
|
|
if (status !== 'success') {
|
|
|
|
throw new Error(data)
|
|
|
|
}
|
|
|
|
commit(mutations.SET_UNAVAILABLE_COUNT, count)
|
|
|
|
} catch (err) {
|
|
|
|
console.error(err)
|
|
|
|
commit(
|
|
|
|
mutations.SET_ERROR,
|
|
|
|
AppGlobal.methods.t('bookmarks', 'Failed to count unavailable bookmarks')
|
|
|
|
)
|
|
|
|
throw err
|
|
|
|
}
|
|
|
|
},
|
2020-09-14 08:26:53 +00:00
|
|
|
async [actions.COUNT_ARCHIVED]({ commit, dispatch, state }, folderId) {
|
|
|
|
try {
|
|
|
|
const response = await axios.get(url(state, '/bookmark/archived')
|
|
|
|
)
|
|
|
|
const {
|
|
|
|
data: { item: count, data, status },
|
|
|
|
} = response
|
|
|
|
if (status !== 'success') {
|
|
|
|
throw new Error(data)
|
|
|
|
}
|
|
|
|
commit(mutations.SET_ARCHIVED_COUNT, count)
|
|
|
|
} catch (err) {
|
|
|
|
console.error(err)
|
|
|
|
commit(
|
|
|
|
mutations.SET_ERROR,
|
|
|
|
AppGlobal.methods.t('bookmarks', 'Failed to count archived bookmarks')
|
|
|
|
)
|
|
|
|
throw err
|
|
|
|
}
|
|
|
|
},
|
2020-04-08 14:48:16 +00:00
|
|
|
async [actions.COUNT_BOOKMARKS]({ commit, dispatch, state }, folderId) {
|
|
|
|
try {
|
|
|
|
const response = await axios.get(url(state, `/folder/${folderId}/count`)
|
|
|
|
)
|
|
|
|
const {
|
|
|
|
data: { item: count, data, status },
|
|
|
|
} = response
|
|
|
|
if (status !== 'success') {
|
|
|
|
throw new Error(data)
|
|
|
|
}
|
|
|
|
commit(mutations.SET_BOOKMARK_COUNT, { folderId, count })
|
|
|
|
} catch (err) {
|
|
|
|
console.error(err)
|
|
|
|
commit(
|
|
|
|
mutations.SET_ERROR,
|
|
|
|
AppGlobal.methods.t('bookmarks', 'Failed to count bookmarks')
|
|
|
|
)
|
|
|
|
throw err
|
|
|
|
}
|
|
|
|
},
|
2020-08-02 06:16:08 +00:00
|
|
|
async [actions.LOAD_BOOKMARK]({ commit, dispatch, state }, id) {
|
|
|
|
try {
|
|
|
|
const response = await axios.get(url(state, `/bookmark/${id}`))
|
|
|
|
const {
|
|
|
|
data: { item: bookmark, status },
|
|
|
|
} = response
|
|
|
|
if (status !== 'success') {
|
|
|
|
throw new Error(response.data)
|
|
|
|
}
|
|
|
|
commit(mutations.ADD_BOOKMARK, bookmark)
|
|
|
|
return bookmark
|
|
|
|
} catch (err) {
|
|
|
|
console.error(err)
|
|
|
|
commit(
|
|
|
|
mutations.SET_ERROR,
|
|
|
|
AppGlobal.methods.t('bookmarks', 'Failed to load bookmark')
|
|
|
|
)
|
|
|
|
throw err
|
|
|
|
}
|
|
|
|
},
|
2019-08-29 09:05:29 +00:00
|
|
|
async [actions.FIND_BOOKMARK]({ commit, dispatch, state }, link) {
|
2019-09-24 15:18:29 +00:00
|
|
|
if (state.loading.bookmarks) return
|
2019-08-29 09:05:29 +00:00
|
|
|
try {
|
2020-03-28 17:15:59 +00:00
|
|
|
const response = await axios.get(url(state, '/bookmark'), {
|
2019-09-09 16:17:02 +00:00
|
|
|
params: {
|
2019-12-10 21:33:54 +00:00
|
|
|
url: link,
|
|
|
|
},
|
2019-09-24 15:18:29 +00:00
|
|
|
})
|
2019-08-29 09:05:29 +00:00
|
|
|
const {
|
2019-12-10 21:33:54 +00:00
|
|
|
data: { data: bookmarks, status },
|
2019-09-24 15:18:29 +00:00
|
|
|
} = response
|
2019-08-29 09:05:29 +00:00
|
|
|
if (status !== 'success') {
|
2019-09-24 15:18:29 +00:00
|
|
|
throw new Error(response.data)
|
2019-08-29 09:05:29 +00:00
|
|
|
}
|
2019-09-24 15:18:29 +00:00
|
|
|
if (!bookmarks.length) return
|
|
|
|
commit(mutations.ADD_BOOKMARK, bookmarks[0])
|
|
|
|
return bookmarks[0]
|
2019-08-29 09:05:29 +00:00
|
|
|
} catch (err) {
|
2019-09-24 15:18:29 +00:00
|
|
|
console.error(err)
|
2019-08-29 09:05:29 +00:00
|
|
|
commit(
|
|
|
|
mutations.SET_ERROR,
|
|
|
|
AppGlobal.methods.t('bookmarks', 'Failed to find existing bookmark')
|
2019-09-24 15:18:29 +00:00
|
|
|
)
|
|
|
|
throw err
|
2019-08-29 09:05:29 +00:00
|
|
|
}
|
|
|
|
},
|
2020-05-01 23:19:09 +00:00
|
|
|
async [actions.CREATE_BOOKMARK]({ commit, dispatch, state }, data) {
|
2019-09-24 15:18:29 +00:00
|
|
|
if (state.loading.bookmarks) return
|
2019-10-05 21:13:26 +00:00
|
|
|
commit(mutations.FETCH_START, { type: 'createBookmark' })
|
2020-05-01 23:19:09 +00:00
|
|
|
try {
|
|
|
|
const response = await axios.post(url(state, '/bookmark'), {
|
2019-08-29 09:05:29 +00:00
|
|
|
url: data.url,
|
|
|
|
title: data.title,
|
|
|
|
description: data.description,
|
2019-12-12 15:36:46 +00:00
|
|
|
folders: data.folders && data.folders.map(parseInt),
|
2019-12-10 21:33:54 +00:00
|
|
|
tags: data.tags,
|
2019-08-29 09:05:29 +00:00
|
|
|
})
|
2020-05-01 23:19:09 +00:00
|
|
|
const {
|
|
|
|
data: { item: bookmark, status },
|
|
|
|
} = response
|
|
|
|
if (status !== 'success') {
|
|
|
|
throw new Error(response.data.data.join('\n'))
|
|
|
|
}
|
|
|
|
commit(mutations.DISPLAY_NEW_BOOKMARK, false)
|
|
|
|
commit(mutations.FETCH_END, 'createBookmark')
|
2020-07-23 13:12:45 +00:00
|
|
|
commit(mutations.SET_BOOKMARK_COUNT, { folderId: -1, count: state.countsByFolder[-1] + 1 })
|
|
|
|
dispatch(actions.LOAD_FOLDER_CHILDREN_ORDER, -1)
|
2020-08-19 12:54:46 +00:00
|
|
|
dispatch(actions.RELOAD_VIEW)
|
2020-07-23 13:12:45 +00:00
|
|
|
if (data.folders) {
|
|
|
|
for (const folderId of data.folders) {
|
|
|
|
commit(mutations.SET_BOOKMARK_COUNT, { folderId, count: state.countsByFolder[folderId] + 1 })
|
|
|
|
dispatch(actions.LOAD_FOLDER_CHILDREN_ORDER, folderId)
|
|
|
|
}
|
|
|
|
}
|
2020-05-01 23:19:09 +00:00
|
|
|
return dispatch(actions.OPEN_BOOKMARK, bookmark.id)
|
|
|
|
} catch (err) {
|
|
|
|
console.error(err)
|
|
|
|
commit(mutations.FETCH_END, 'createBookmark')
|
|
|
|
commit(
|
|
|
|
mutations.SET_ERROR,
|
|
|
|
AppGlobal.methods.t('bookmarks', 'Failed to create bookmark')
|
|
|
|
)
|
|
|
|
throw err
|
|
|
|
}
|
|
|
|
|
2019-08-29 09:05:29 +00:00
|
|
|
},
|
2020-05-01 23:19:09 +00:00
|
|
|
async [actions.SAVE_BOOKMARK]({ commit, dispatch, state }, id) {
|
2019-10-05 21:13:26 +00:00
|
|
|
commit(mutations.FETCH_START, { type: 'saveBookmark' })
|
2020-05-01 23:19:09 +00:00
|
|
|
try {
|
|
|
|
const response = await axios.put(url(state, `/bookmark/${id}`), this.getters.getBookmark(id))
|
|
|
|
const {
|
|
|
|
data: { status },
|
|
|
|
} = response
|
|
|
|
if (status !== 'success') {
|
|
|
|
throw new Error(response.data)
|
|
|
|
}
|
|
|
|
commit(mutations.FETCH_END, 'saveBookmark')
|
|
|
|
} catch (err) {
|
|
|
|
console.error(err)
|
|
|
|
commit(mutations.FETCH_END, 'saveBookmark')
|
|
|
|
commit(
|
|
|
|
mutations.SET_ERROR,
|
|
|
|
AppGlobal.methods.t('bookmarks', 'Failed to save bookmark')
|
|
|
|
)
|
|
|
|
throw err
|
|
|
|
}
|
2019-08-29 09:05:29 +00:00
|
|
|
},
|
|
|
|
async [actions.MOVE_BOOKMARK](
|
|
|
|
{ commit, dispatch, state },
|
|
|
|
{ bookmark, oldFolder, newFolder }
|
|
|
|
) {
|
2020-05-20 10:37:21 +00:00
|
|
|
if (Number(oldFolder) === Number(newFolder)) {
|
2020-04-28 10:56:59 +00:00
|
|
|
return
|
|
|
|
}
|
2019-10-05 21:13:26 +00:00
|
|
|
commit(mutations.FETCH_START, { type: 'moveBookmark' })
|
2019-08-29 09:05:29 +00:00
|
|
|
try {
|
2019-12-10 21:33:54 +00:00
|
|
|
const response = await axios.post(
|
2020-03-28 17:15:59 +00:00
|
|
|
url(state, `/folder/${newFolder}/bookmarks/${bookmark}`)
|
2019-09-24 15:18:29 +00:00
|
|
|
)
|
2019-08-29 09:05:29 +00:00
|
|
|
if (response.data.status !== 'success') {
|
2019-09-24 15:18:29 +00:00
|
|
|
throw new Error(response.data)
|
2019-08-29 09:05:29 +00:00
|
|
|
}
|
2019-12-10 21:33:54 +00:00
|
|
|
const response2 = await axios.delete(
|
2020-03-28 17:15:59 +00:00
|
|
|
url(state, `/folder/${oldFolder}/bookmarks/${bookmark}`)
|
2019-09-24 15:18:29 +00:00
|
|
|
)
|
2019-08-29 09:05:29 +00:00
|
|
|
if (response2.data.status !== 'success') {
|
2019-09-24 15:18:29 +00:00
|
|
|
throw new Error(response2.data)
|
2019-08-29 09:05:29 +00:00
|
|
|
}
|
2020-05-02 13:04:16 +00:00
|
|
|
commit(mutations.FETCH_END, 'moveBookmark')
|
2020-07-23 13:12:45 +00:00
|
|
|
dispatch(actions.LOAD_FOLDER_CHILDREN_ORDER, oldFolder)
|
|
|
|
dispatch(actions.LOAD_FOLDER_CHILDREN_ORDER, newFolder)
|
2019-08-29 09:05:29 +00:00
|
|
|
} catch (err) {
|
2019-09-24 15:18:29 +00:00
|
|
|
console.error(err)
|
2020-05-02 13:04:16 +00:00
|
|
|
commit(mutations.FETCH_END, 'moveBookmark')
|
2019-08-29 09:05:29 +00:00
|
|
|
commit(
|
|
|
|
mutations.SET_ERROR,
|
|
|
|
AppGlobal.methods.t('bookmarks', 'Failed to move bookmark')
|
2019-09-24 15:18:29 +00:00
|
|
|
)
|
|
|
|
throw err
|
2019-08-29 09:05:29 +00:00
|
|
|
}
|
|
|
|
},
|
|
|
|
[actions.OPEN_BOOKMARK]({ commit }, id) {
|
2019-09-24 15:18:29 +00:00
|
|
|
commit(mutations.SET_SIDEBAR, { type: 'bookmark', id })
|
2019-08-29 09:05:29 +00:00
|
|
|
},
|
2020-10-12 20:09:19 +00:00
|
|
|
async [actions.DELETE_BOOKMARK]({ commit, dispatch, state }, { id, folder, avoidReload }) {
|
2019-08-29 09:05:29 +00:00
|
|
|
if (folder) {
|
|
|
|
try {
|
|
|
|
const response = await axios.delete(
|
2020-03-28 17:15:59 +00:00
|
|
|
url(state, `/folder/${folder}/bookmarks/${id}`)
|
2019-09-24 15:18:29 +00:00
|
|
|
)
|
2019-08-29 09:05:29 +00:00
|
|
|
if (response.data.status !== 'success') {
|
2019-09-24 15:18:29 +00:00
|
|
|
throw new Error(response.data)
|
2019-08-29 09:05:29 +00:00
|
|
|
}
|
2019-09-24 15:18:29 +00:00
|
|
|
commit(mutations.REMOVE_BOOKMARK, id)
|
2020-10-12 20:09:19 +00:00
|
|
|
if (!avoidReload) {
|
|
|
|
await dispatch(actions.COUNT_BOOKMARKS, -1)
|
|
|
|
await dispatch(actions.LOAD_FOLDER_CHILDREN_ORDER, folder)
|
|
|
|
}
|
2019-08-29 09:05:29 +00:00
|
|
|
} catch (err) {
|
2019-09-24 15:18:29 +00:00
|
|
|
console.error(err)
|
2019-08-29 09:05:29 +00:00
|
|
|
commit(
|
|
|
|
mutations.SET_ERROR,
|
|
|
|
AppGlobal.methods.t('bookmarks', 'Failed to delete bookmark')
|
2019-09-24 15:18:29 +00:00
|
|
|
)
|
|
|
|
throw err
|
2019-08-29 09:05:29 +00:00
|
|
|
}
|
2019-09-24 15:18:29 +00:00
|
|
|
return
|
2019-08-29 09:05:29 +00:00
|
|
|
}
|
|
|
|
try {
|
2020-03-28 17:15:59 +00:00
|
|
|
const response = await axios.delete(url(state, `/bookmark/${id}`))
|
2019-08-29 09:05:29 +00:00
|
|
|
if (response.data.status !== 'success') {
|
2019-09-24 15:18:29 +00:00
|
|
|
throw new Error(response.data)
|
2019-08-29 09:05:29 +00:00
|
|
|
}
|
2020-08-12 11:04:47 +00:00
|
|
|
await dispatch(actions.COUNT_BOOKMARKS, -1)
|
|
|
|
await commit(mutations.REMOVE_BOOKMARK, id)
|
2019-08-29 09:05:29 +00:00
|
|
|
} catch (err) {
|
2019-09-24 15:18:29 +00:00
|
|
|
console.error(err)
|
2019-08-29 09:05:29 +00:00
|
|
|
commit(
|
|
|
|
mutations.SET_ERROR,
|
|
|
|
AppGlobal.methods.t('bookmarks', 'Failed to delete bookmark')
|
2019-09-24 15:18:29 +00:00
|
|
|
)
|
|
|
|
throw err
|
2019-08-29 09:05:29 +00:00
|
|
|
}
|
|
|
|
},
|
2020-09-09 13:46:56 +00:00
|
|
|
async [actions.IMPORT_BOOKMARKS]({ commit, dispatch, state }, { file, folder }) {
|
2020-10-08 10:05:24 +00:00
|
|
|
commit(mutations.FETCH_START, { type: 'importBookmarks' })
|
2019-12-10 21:33:54 +00:00
|
|
|
const data = new FormData()
|
2019-09-24 15:18:29 +00:00
|
|
|
data.append('bm_import', file)
|
2020-09-09 13:46:56 +00:00
|
|
|
try {
|
|
|
|
const response = await axios.post(url(state, `/folder/${folder || -1}/import`), data)
|
|
|
|
if (!response.data || response.data.status !== 'success') {
|
|
|
|
if (response.status === 413) {
|
|
|
|
throw new Error('Selected file is too large')
|
2019-08-29 09:05:29 +00:00
|
|
|
}
|
2020-09-09 13:46:56 +00:00
|
|
|
console.error('Failed to import bookmarks', response)
|
|
|
|
throw new Error(Array.isArray(response.data.data) ? response.data.data.join('. ') : response.data.data)
|
|
|
|
}
|
|
|
|
await dispatch(actions.COUNT_BOOKMARKS, -1)
|
|
|
|
await dispatch(actions.LOAD_FOLDER_CHILDREN_ORDER, -1)
|
|
|
|
await dispatch(actions.RELOAD_VIEW)
|
2020-10-08 10:05:24 +00:00
|
|
|
commit(mutations.FETCH_END, 'importBookmarks')
|
2020-09-09 13:46:56 +00:00
|
|
|
return commit(mutations.SET_NOTIFICATION, AppGlobal.methods.t('bookmarks', 'Import successful'))
|
|
|
|
} catch (err) {
|
|
|
|
console.error(err)
|
2020-10-08 10:05:24 +00:00
|
|
|
commit(mutations.FETCH_END, 'importBookmarks')
|
2020-09-09 13:46:56 +00:00
|
|
|
commit(
|
|
|
|
mutations.SET_ERROR,
|
|
|
|
err.message
|
|
|
|
)
|
|
|
|
throw err
|
|
|
|
}
|
2019-08-29 09:05:29 +00:00
|
|
|
},
|
|
|
|
[actions.DELETE_BOOKMARKS]({ commit, dispatch, state }) {
|
2020-10-08 10:05:24 +00:00
|
|
|
commit(mutations.FETCH_START, { type: 'deleteBookmarks' })
|
2019-08-29 09:05:29 +00:00
|
|
|
return axios
|
2020-08-02 13:53:33 +00:00
|
|
|
.delete(url(state, '/bookmark'))
|
2019-08-29 09:05:29 +00:00
|
|
|
.then(response => {
|
|
|
|
const {
|
2019-12-10 21:33:54 +00:00
|
|
|
data: { status },
|
2019-09-24 15:18:29 +00:00
|
|
|
} = response
|
2019-08-29 09:05:29 +00:00
|
|
|
if (status !== 'success') {
|
2019-09-24 15:18:29 +00:00
|
|
|
throw new Error(response.data)
|
2019-08-29 09:05:29 +00:00
|
|
|
}
|
2020-04-08 14:48:16 +00:00
|
|
|
dispatch(actions.COUNT_BOOKMARKS, -1)
|
2020-07-23 13:12:45 +00:00
|
|
|
dispatch(actions.LOAD_FOLDER_CHILDREN_ORDER, -1)
|
2020-10-08 10:05:24 +00:00
|
|
|
commit(mutations.FETCH_END, 'deleteBookmarks')
|
2020-04-17 11:43:08 +00:00
|
|
|
return dispatch(actions.RELOAD_VIEW)
|
2019-08-29 09:05:29 +00:00
|
|
|
})
|
|
|
|
.catch(err => {
|
2019-09-24 15:18:29 +00:00
|
|
|
console.error(err)
|
2020-10-08 10:05:24 +00:00
|
|
|
commit(mutations.FETCH_END, 'deleteBookmarks')
|
2019-08-29 09:05:29 +00:00
|
|
|
commit(
|
|
|
|
mutations.SET_ERROR,
|
|
|
|
AppGlobal.methods.t('bookmarks', err.message)
|
2019-09-24 15:18:29 +00:00
|
|
|
)
|
|
|
|
throw err
|
|
|
|
})
|
2019-08-29 09:05:29 +00:00
|
|
|
},
|
|
|
|
|
2020-07-22 15:51:02 +00:00
|
|
|
async [actions.RENAME_TAG]({ commit, dispatch, state }, { oldName, newName }) {
|
2019-10-05 21:13:26 +00:00
|
|
|
commit(mutations.FETCH_START, { type: 'tag' })
|
2020-05-02 13:04:16 +00:00
|
|
|
try {
|
2020-07-22 15:51:02 +00:00
|
|
|
const response = await axios
|
2020-05-02 13:04:16 +00:00
|
|
|
.put(url(state, `/tag/${oldName}`), {
|
|
|
|
name: newName,
|
|
|
|
})
|
|
|
|
const {
|
|
|
|
data: { status },
|
|
|
|
} = response
|
|
|
|
if (status !== 'success') {
|
|
|
|
throw new Error(response.data)
|
|
|
|
}
|
|
|
|
commit(mutations.RENAME_TAG, { oldName, newName })
|
|
|
|
commit(mutations.FETCH_END, 'tag')
|
|
|
|
return dispatch(actions.LOAD_TAGS)
|
|
|
|
} catch (err) {
|
|
|
|
console.error(err)
|
|
|
|
commit(mutations.FETCH_END, 'tag')
|
|
|
|
commit(
|
|
|
|
mutations.SET_ERROR,
|
2020-07-22 15:51:02 +00:00
|
|
|
AppGlobal.methods.t('bookmarks', 'Failed to rename tag')
|
2020-05-02 13:04:16 +00:00
|
|
|
)
|
|
|
|
throw err
|
|
|
|
}
|
2019-08-29 09:05:29 +00:00
|
|
|
},
|
2020-04-07 19:38:24 +00:00
|
|
|
[actions.LOAD_TAGS]({ commit, dispatch, state }) {
|
2019-10-05 21:13:26 +00:00
|
|
|
commit(mutations.FETCH_START, { type: 'tags' })
|
2019-08-29 09:05:29 +00:00
|
|
|
return axios
|
2020-03-28 17:15:59 +00:00
|
|
|
.get(url(state, '/tag'), { params: { count: true } })
|
2019-08-29 09:05:29 +00:00
|
|
|
.then(response => {
|
2019-09-24 15:18:29 +00:00
|
|
|
const { data: tags } = response
|
2020-05-02 13:04:16 +00:00
|
|
|
commit(mutations.FETCH_END, 'tags')
|
2019-09-24 15:18:29 +00:00
|
|
|
return commit(mutations.SET_TAGS, tags)
|
2019-08-29 09:05:29 +00:00
|
|
|
})
|
|
|
|
.catch(err => {
|
2019-09-24 15:18:29 +00:00
|
|
|
console.error(err)
|
2020-05-02 13:04:16 +00:00
|
|
|
commit(mutations.FETCH_END, 'tags')
|
2019-08-29 09:05:29 +00:00
|
|
|
commit(
|
|
|
|
mutations.SET_ERROR,
|
|
|
|
AppGlobal.methods.t('bookmarks', 'Failed to load tags')
|
2019-09-24 15:18:29 +00:00
|
|
|
)
|
|
|
|
throw err
|
2019-08-29 09:05:29 +00:00
|
|
|
})
|
|
|
|
},
|
|
|
|
[actions.DELETE_TAG]({ commit, dispatch, state }, tag) {
|
|
|
|
return axios
|
2020-03-28 17:15:59 +00:00
|
|
|
.delete(url(state, `/tag/${tag}`))
|
2019-08-29 09:05:29 +00:00
|
|
|
.then(response => {
|
|
|
|
const {
|
2019-12-10 21:33:54 +00:00
|
|
|
data: { status },
|
2019-09-24 15:18:29 +00:00
|
|
|
} = response
|
2019-08-29 09:05:29 +00:00
|
|
|
if (status !== 'success') {
|
2019-09-24 15:18:29 +00:00
|
|
|
throw new Error(response.data)
|
2019-08-29 09:05:29 +00:00
|
|
|
}
|
2019-09-24 15:18:29 +00:00
|
|
|
dispatch(actions.LOAD_TAGS)
|
2019-08-29 09:05:29 +00:00
|
|
|
})
|
|
|
|
.catch(err => {
|
2019-09-24 15:18:29 +00:00
|
|
|
console.error(err)
|
2019-08-29 09:05:29 +00:00
|
|
|
commit(
|
|
|
|
mutations.SET_ERROR,
|
|
|
|
AppGlobal.methods.t('bookmarks', 'Failed to delete bookmark')
|
2019-09-24 15:18:29 +00:00
|
|
|
)
|
|
|
|
throw err
|
|
|
|
})
|
2019-08-29 09:05:29 +00:00
|
|
|
},
|
|
|
|
|
2020-08-25 18:49:02 +00:00
|
|
|
async [actions.LOAD_FOLDERS]({ commit, dispatch, state }) {
|
2020-09-25 11:12:42 +00:00
|
|
|
if (!state.folders.length) {
|
|
|
|
try {
|
|
|
|
const folders = loadState('bookmarks', 'folders')
|
|
|
|
return commit(mutations.SET_FOLDERS, folders)
|
|
|
|
} catch (e) {
|
|
|
|
console.warn('Could not load initial folder state, continuing with HTTP request')
|
|
|
|
}
|
|
|
|
}
|
2019-10-12 18:00:15 +00:00
|
|
|
let canceled = false
|
|
|
|
commit(mutations.FETCH_START, {
|
|
|
|
type: 'folders',
|
|
|
|
cancel: () => {
|
|
|
|
canceled = true
|
2019-12-10 21:33:54 +00:00
|
|
|
},
|
2019-10-12 18:00:15 +00:00
|
|
|
})
|
2020-08-25 18:49:02 +00:00
|
|
|
try {
|
|
|
|
const response = await axios.get(url(state, '/folder'), { params: {} })
|
|
|
|
if (canceled) return
|
|
|
|
const {
|
|
|
|
data: { data, status },
|
|
|
|
} = response
|
|
|
|
if (status !== 'success') throw new Error(data)
|
|
|
|
const folders = data
|
|
|
|
commit(mutations.FETCH_END, 'folders')
|
|
|
|
return commit(mutations.SET_FOLDERS, folders)
|
|
|
|
} catch (err) {
|
|
|
|
console.error(err)
|
|
|
|
commit(mutations.FETCH_END, 'folders')
|
|
|
|
commit(
|
|
|
|
mutations.SET_ERROR,
|
|
|
|
AppGlobal.methods.t('bookmarks', 'Failed to load folders')
|
|
|
|
)
|
|
|
|
throw err
|
|
|
|
}
|
2019-08-29 09:05:29 +00:00
|
|
|
},
|
2020-10-12 20:09:19 +00:00
|
|
|
async [actions.DELETE_FOLDER]({ commit, dispatch, state }, { id, avoidReload }) {
|
2020-08-12 10:37:37 +00:00
|
|
|
try {
|
|
|
|
const response = await axios.delete(url(state, `/folder/${id}`))
|
|
|
|
const {
|
|
|
|
data: { status },
|
|
|
|
} = response
|
|
|
|
if (status !== 'success') {
|
|
|
|
throw new Error(response.data)
|
|
|
|
}
|
|
|
|
const parentFolder = this.getters.getFolder(id)[0].parent_folder
|
2020-10-12 20:09:19 +00:00
|
|
|
if (!avoidReload) {
|
|
|
|
await dispatch(actions.LOAD_FOLDER_CHILDREN_ORDER, parentFolder)
|
|
|
|
await dispatch(actions.LOAD_FOLDERS)
|
|
|
|
}
|
2020-08-12 10:37:37 +00:00
|
|
|
} catch (err) {
|
|
|
|
console.error(err)
|
|
|
|
commit(
|
|
|
|
mutations.SET_ERROR,
|
|
|
|
AppGlobal.methods.t('bookmarks', 'Failed to delete folder')
|
|
|
|
)
|
|
|
|
throw err
|
|
|
|
}
|
2019-08-29 09:05:29 +00:00
|
|
|
},
|
2020-08-06 10:01:00 +00:00
|
|
|
async [actions.CREATE_FOLDER](
|
2019-08-29 09:05:29 +00:00
|
|
|
{ commit, dispatch, state },
|
|
|
|
{ parentFolder, title }
|
|
|
|
) {
|
2020-08-06 10:01:00 +00:00
|
|
|
try {
|
|
|
|
const response = await axios.post(url(state, '/folder'), {
|
2019-08-29 09:05:29 +00:00
|
|
|
parent_folder: parentFolder,
|
2019-12-10 21:33:54 +00:00
|
|
|
title,
|
2019-08-29 09:05:29 +00:00
|
|
|
})
|
2020-08-06 10:01:00 +00:00
|
|
|
const {
|
|
|
|
data: { status },
|
|
|
|
} = response
|
|
|
|
if (status !== 'success') {
|
|
|
|
throw new Error(response.data)
|
|
|
|
}
|
|
|
|
commit(mutations.DISPLAY_NEW_FOLDER, false)
|
2020-08-12 10:37:37 +00:00
|
|
|
await dispatch(actions.LOAD_FOLDER_CHILDREN_ORDER, parentFolder || -1)
|
|
|
|
await dispatch(actions.LOAD_FOLDERS)
|
2020-08-06 10:01:00 +00:00
|
|
|
} catch (err) {
|
|
|
|
console.error(err)
|
|
|
|
commit(
|
|
|
|
mutations.SET_ERROR,
|
|
|
|
AppGlobal.methods.t('bookmarks', 'Failed to create folder')
|
|
|
|
)
|
|
|
|
throw err
|
|
|
|
}
|
2019-08-29 09:05:29 +00:00
|
|
|
},
|
2020-08-12 11:04:47 +00:00
|
|
|
async [actions.SAVE_FOLDER]({ commit, dispatch, state }, id) {
|
2019-09-24 15:18:29 +00:00
|
|
|
const folder = this.getters.getFolder(id)[0]
|
2019-10-05 21:13:26 +00:00
|
|
|
commit(mutations.FETCH_START, { type: 'saveFolder' })
|
2020-08-12 11:04:47 +00:00
|
|
|
try {
|
|
|
|
const response = await axios.put(url(state, `/folder/${id}`), {
|
2019-08-29 09:05:29 +00:00
|
|
|
parent_folder: folder.parent_folder,
|
2019-12-10 21:33:54 +00:00
|
|
|
title: folder.title,
|
2019-08-29 09:05:29 +00:00
|
|
|
})
|
2020-08-12 11:04:47 +00:00
|
|
|
|
|
|
|
const {
|
|
|
|
data: { status },
|
|
|
|
} = response
|
|
|
|
if (status !== 'success') {
|
|
|
|
throw new Error(response.data)
|
|
|
|
}
|
|
|
|
await dispatch(actions.LOAD_FOLDER_CHILDREN_ORDER, folder.parent_folder)
|
|
|
|
commit(mutations.FETCH_END, 'saveFolder')
|
|
|
|
} catch (err) {
|
|
|
|
console.error(err)
|
|
|
|
commit(mutations.FETCH_END, 'saveFolder')
|
|
|
|
commit(
|
|
|
|
mutations.SET_ERROR,
|
|
|
|
AppGlobal.methods.t('bookmarks', 'Failed to create folder')
|
|
|
|
)
|
|
|
|
throw err
|
|
|
|
}
|
2019-08-29 09:05:29 +00:00
|
|
|
},
|
2020-07-23 13:12:45 +00:00
|
|
|
async [actions.LOAD_FOLDER_CHILDREN_ORDER]({ commit, dispatch, state }, id) {
|
|
|
|
commit(mutations.FETCH_START, { type: 'childrenOrder' })
|
|
|
|
try {
|
|
|
|
const response = await axios.get(url(state, `/folder/${id}/childorder`))
|
|
|
|
const {
|
|
|
|
data: { status },
|
|
|
|
} = response
|
|
|
|
if (status !== 'success') {
|
|
|
|
throw new Error(response.data)
|
|
|
|
}
|
2020-08-12 10:37:37 +00:00
|
|
|
await commit(mutations.FETCH_END, 'childrenOrder')
|
|
|
|
await commit(mutations.SET_FOLDER_CHILDREN_ORDER, { folderId: id, children: response.data.data })
|
2020-07-23 13:12:45 +00:00
|
|
|
} catch (err) {
|
|
|
|
console.error(err)
|
|
|
|
commit(mutations.FETCH_END, 'childrenOrder')
|
|
|
|
commit(
|
|
|
|
mutations.SET_ERROR,
|
|
|
|
AppGlobal.methods.t('bookmarks', 'Failed to load children order')
|
|
|
|
)
|
|
|
|
throw err
|
|
|
|
}
|
|
|
|
},
|
2020-03-28 17:15:59 +00:00
|
|
|
[actions.OPEN_FOLDER_DETAILS]({ commit }, id) {
|
|
|
|
commit(mutations.SET_SIDEBAR, { type: 'folder', id })
|
|
|
|
},
|
2019-08-29 09:05:29 +00:00
|
|
|
|
|
|
|
async [actions.MOVE_SELECTION]({ commit, dispatch, state }, folderId) {
|
2019-10-05 21:13:26 +00:00
|
|
|
commit(mutations.FETCH_START, { type: 'moveSelection' })
|
2019-08-29 09:05:29 +00:00
|
|
|
try {
|
2020-10-12 20:09:19 +00:00
|
|
|
await Parallel.each(state.selection.folders, async folder => {
|
2019-08-29 09:05:29 +00:00
|
|
|
if (folderId === folder.id) {
|
2019-09-24 15:18:29 +00:00
|
|
|
throw new Error('Cannot move folder into itself')
|
2019-08-29 09:05:29 +00:00
|
|
|
}
|
2020-07-23 13:12:45 +00:00
|
|
|
const oldParent = folder.parent_folder
|
2019-09-24 15:18:29 +00:00
|
|
|
folder.parent_folder = folderId
|
2020-08-12 11:22:16 +00:00
|
|
|
await dispatch(actions.SAVE_FOLDER, folder.id) // reloads children order for new parent
|
2020-08-12 11:04:47 +00:00
|
|
|
await dispatch(actions.LOAD_FOLDER_CHILDREN_ORDER, oldParent)
|
2020-10-12 20:09:19 +00:00
|
|
|
}, 10)
|
2020-08-12 11:04:47 +00:00
|
|
|
await dispatch(actions.LOAD_FOLDERS)
|
2019-08-29 09:05:29 +00:00
|
|
|
|
2020-10-12 20:09:19 +00:00
|
|
|
await Parallel.each(state.selection.bookmarks, bookmark =>
|
|
|
|
dispatch(actions.MOVE_BOOKMARK, {
|
2019-08-29 09:05:29 +00:00
|
|
|
oldFolder: bookmark.folders[bookmark.folders.length - 1], // FIXME This is veeeery ugly and will cause issues. Inevitably.
|
|
|
|
newFolder: folderId,
|
2019-12-10 21:33:54 +00:00
|
|
|
bookmark: bookmark.id,
|
2019-09-24 15:18:29 +00:00
|
|
|
})
|
2020-10-12 20:09:19 +00:00
|
|
|
, 10)
|
2020-08-12 11:22:16 +00:00
|
|
|
|
|
|
|
// Because we're possibly moving across share boundaries we need to recount
|
|
|
|
await dispatch(actions.COUNT_BOOKMARKS, -1)
|
|
|
|
|
2020-05-02 13:04:16 +00:00
|
|
|
commit(mutations.FETCH_END, 'moveSelection')
|
2019-08-29 09:05:29 +00:00
|
|
|
} catch (err) {
|
2019-09-24 15:18:29 +00:00
|
|
|
console.error(err)
|
2020-05-02 13:04:16 +00:00
|
|
|
commit(mutations.FETCH_END, 'moveSelection')
|
2019-08-29 09:05:29 +00:00
|
|
|
commit(
|
|
|
|
mutations.SET_ERROR,
|
|
|
|
AppGlobal.methods.t('bookmarks', 'Failed to move parts of selection')
|
2019-09-24 15:18:29 +00:00
|
|
|
)
|
|
|
|
throw err
|
2019-08-29 09:05:29 +00:00
|
|
|
}
|
|
|
|
},
|
2020-05-20 09:42:08 +00:00
|
|
|
async [actions.DELETE_SELECTION]({ commit, dispatch, state }, { folder }) {
|
2019-10-11 12:14:49 +00:00
|
|
|
commit(mutations.FETCH_START, { type: 'deleteSelection' })
|
|
|
|
try {
|
2020-10-12 20:09:19 +00:00
|
|
|
await Parallel.each(state.selection.folders, folder => dispatch(actions.DELETE_FOLDER, { id: folder.id, avoidReload: true }), 10)
|
|
|
|
await Parallel.each(state.selection.bookmarks, bookmark => dispatch(actions.DELETE_BOOKMARK, { id: bookmark.id, folder, avoidReload: true }), 10)
|
|
|
|
dispatch(actions.RELOAD_VIEW)
|
2020-05-02 13:04:16 +00:00
|
|
|
commit(mutations.FETCH_END, 'deleteSelection')
|
2019-10-11 12:14:49 +00:00
|
|
|
} catch (err) {
|
|
|
|
console.error(err)
|
2020-05-02 13:04:16 +00:00
|
|
|
commit(mutations.FETCH_END, 'deleteSelection')
|
2019-10-11 12:14:49 +00:00
|
|
|
commit(
|
|
|
|
mutations.SET_ERROR,
|
|
|
|
AppGlobal.methods.t('bookmarks', 'Failed to delete parts of selection')
|
|
|
|
)
|
|
|
|
throw err
|
|
|
|
}
|
|
|
|
},
|
2019-08-29 09:05:29 +00:00
|
|
|
|
|
|
|
[actions.RELOAD_VIEW]({ state, dispatch, commit }) {
|
2019-09-24 15:18:29 +00:00
|
|
|
commit(mutations.SET_QUERY, state.fetchState.query)
|
|
|
|
dispatch(actions.FETCH_PAGE)
|
|
|
|
dispatch(actions.LOAD_FOLDERS)
|
|
|
|
dispatch(actions.LOAD_TAGS)
|
2020-09-14 08:26:53 +00:00
|
|
|
dispatch(actions.COUNT_BOOKMARKS, -1)
|
|
|
|
dispatch(actions.COUNT_UNAVAILABLE)
|
|
|
|
dispatch(actions.COUNT_ARCHIVED)
|
2019-08-29 09:05:29 +00:00
|
|
|
},
|
|
|
|
|
|
|
|
[actions.NO_FILTER]({ dispatch, commit }) {
|
2019-09-24 15:18:29 +00:00
|
|
|
commit(mutations.SET_QUERY, {})
|
|
|
|
return dispatch(actions.FETCH_PAGE)
|
2019-08-29 09:05:29 +00:00
|
|
|
},
|
|
|
|
[actions.FILTER_BY_RECENT]({ dispatch, commit }, search) {
|
2020-04-18 13:14:32 +00:00
|
|
|
commit(mutations.SET_QUERY, { sortby: 'added' })
|
2019-09-24 15:18:29 +00:00
|
|
|
return dispatch(actions.FETCH_PAGE)
|
2019-08-29 09:05:29 +00:00
|
|
|
},
|
|
|
|
[actions.FILTER_BY_SEARCH]({ dispatch, commit }, search) {
|
2019-09-24 15:18:29 +00:00
|
|
|
commit(mutations.SET_QUERY, { search: search.split(' ') })
|
|
|
|
return dispatch(actions.FETCH_PAGE)
|
2019-08-29 09:05:29 +00:00
|
|
|
},
|
|
|
|
[actions.FILTER_BY_TAGS]({ dispatch, commit }, tags) {
|
2019-09-24 15:18:29 +00:00
|
|
|
commit(mutations.SET_QUERY, { tags, conjunction: 'and' })
|
|
|
|
return dispatch(actions.FETCH_PAGE)
|
2019-08-29 09:05:29 +00:00
|
|
|
},
|
|
|
|
[actions.FILTER_BY_UNTAGGED]({ dispatch, commit }) {
|
2019-09-24 15:18:29 +00:00
|
|
|
commit(mutations.SET_QUERY, { untagged: true })
|
|
|
|
return dispatch(actions.FETCH_PAGE)
|
2019-08-29 09:05:29 +00:00
|
|
|
},
|
2020-08-07 15:59:49 +00:00
|
|
|
[actions.FILTER_BY_UNAVAILABLE]({ dispatch, commit }) {
|
|
|
|
commit(mutations.SET_QUERY, { unavailable: true })
|
|
|
|
return dispatch(actions.FETCH_PAGE)
|
|
|
|
},
|
2020-09-11 16:57:58 +00:00
|
|
|
[actions.FILTER_BY_ARCHIVED]({ dispatch, commit }) {
|
|
|
|
commit(mutations.SET_QUERY, { archived: true })
|
|
|
|
return dispatch(actions.FETCH_PAGE)
|
|
|
|
},
|
2019-08-29 09:05:29 +00:00
|
|
|
[actions.FILTER_BY_FOLDER]({ dispatch, commit }, folder) {
|
2019-09-24 15:18:29 +00:00
|
|
|
commit(mutations.SET_QUERY, { folder })
|
2020-07-23 13:12:45 +00:00
|
|
|
dispatch(actions.LOAD_FOLDER_CHILDREN_ORDER, folder)
|
2019-09-24 15:18:29 +00:00
|
|
|
return dispatch(actions.FETCH_PAGE)
|
2019-08-29 09:05:29 +00:00
|
|
|
},
|
2020-08-14 21:27:40 +00:00
|
|
|
|
2019-08-29 09:05:29 +00:00
|
|
|
[actions.FETCH_PAGE]({ dispatch, commit, state }) {
|
2019-09-24 15:18:29 +00:00
|
|
|
if (state.fetchState.reachedEnd) return
|
2020-07-17 09:23:19 +00:00
|
|
|
if (state.loading.bookmarks) return
|
2019-10-05 21:13:26 +00:00
|
|
|
let canceled = false
|
|
|
|
commit(mutations.FETCH_START, {
|
|
|
|
type: 'bookmarks',
|
|
|
|
cancel() {
|
|
|
|
canceled = true
|
2019-12-10 21:33:54 +00:00
|
|
|
},
|
2019-10-05 21:13:26 +00:00
|
|
|
})
|
|
|
|
axios
|
2020-03-28 17:15:59 +00:00
|
|
|
.get(url(state, '/bookmark'), {
|
2019-08-29 09:05:29 +00:00
|
|
|
params: {
|
|
|
|
limit: BATCH_SIZE,
|
|
|
|
page: state.fetchState.page,
|
|
|
|
sortby: state.settings.sorting,
|
2019-12-10 21:33:54 +00:00
|
|
|
...state.fetchState.query,
|
|
|
|
},
|
2019-08-29 09:05:29 +00:00
|
|
|
})
|
|
|
|
.then(response => {
|
2019-10-05 21:13:26 +00:00
|
|
|
if (canceled) return
|
2019-08-29 09:05:29 +00:00
|
|
|
const {
|
2019-12-10 21:33:54 +00:00
|
|
|
data: { data, status },
|
2019-09-24 15:18:29 +00:00
|
|
|
} = response
|
|
|
|
if (status !== 'success') throw new Error(data)
|
|
|
|
const bookmarks = data
|
|
|
|
commit(mutations.INCREMENT_PAGE)
|
2019-08-29 09:05:29 +00:00
|
|
|
if (bookmarks.length < BATCH_SIZE) {
|
2019-09-24 15:18:29 +00:00
|
|
|
commit(mutations.REACHED_END)
|
2019-08-29 09:05:29 +00:00
|
|
|
}
|
2020-05-02 13:04:16 +00:00
|
|
|
commit(mutations.FETCH_END, 'bookmarks')
|
2019-09-24 15:18:29 +00:00
|
|
|
return dispatch(actions.ADD_ALL_BOOKMARKS, bookmarks)
|
2019-08-29 09:05:29 +00:00
|
|
|
})
|
|
|
|
.catch(err => {
|
2019-09-24 15:18:29 +00:00
|
|
|
console.error(err)
|
2020-05-02 13:04:16 +00:00
|
|
|
commit(mutations.FETCH_END, 'bookmarks')
|
2019-08-29 09:05:29 +00:00
|
|
|
commit(
|
|
|
|
mutations.SET_ERROR,
|
|
|
|
AppGlobal.t('bookmarks', 'Failed to fetch bookmarks.')
|
2019-09-24 15:18:29 +00:00
|
|
|
)
|
|
|
|
throw err
|
2019-08-29 09:05:29 +00:00
|
|
|
})
|
|
|
|
},
|
2020-08-14 21:27:40 +00:00
|
|
|
async [actions.FETCH_ALL]({ dispatch, commit, state }) {
|
|
|
|
if (state.fetchState.reachedEnd) return
|
|
|
|
if (state.loading.bookmarks) return
|
|
|
|
let canceled = false
|
|
|
|
commit(mutations.FETCH_START, {
|
|
|
|
type: 'bookmarks',
|
|
|
|
cancel() {
|
|
|
|
canceled = true
|
|
|
|
},
|
|
|
|
})
|
|
|
|
try {
|
|
|
|
const response = await axios.get(url(state, '/bookmark'), {
|
|
|
|
params: {
|
|
|
|
page: -1,
|
|
|
|
sortby: state.settings.sorting,
|
|
|
|
...state.fetchState.query,
|
|
|
|
},
|
|
|
|
})
|
|
|
|
if (canceled) return
|
|
|
|
const {
|
|
|
|
data: { data, status },
|
|
|
|
} = response
|
|
|
|
if (status !== 'success') throw new Error(data)
|
|
|
|
const bookmarks = data
|
|
|
|
commit(mutations.REACHED_END)
|
|
|
|
commit(mutations.FETCH_END, 'bookmarks')
|
|
|
|
return dispatch(actions.ADD_ALL_BOOKMARKS, bookmarks)
|
|
|
|
|
|
|
|
} catch (err) {
|
|
|
|
console.error(err)
|
|
|
|
commit(mutations.FETCH_END, 'bookmarks')
|
|
|
|
commit(
|
|
|
|
mutations.SET_ERROR,
|
|
|
|
AppGlobal.t('bookmarks', 'Failed to fetch bookmarks.')
|
|
|
|
)
|
|
|
|
throw err
|
|
|
|
}
|
|
|
|
},
|
2019-08-29 09:05:29 +00:00
|
|
|
|
2019-10-12 18:00:15 +00:00
|
|
|
async [actions.SET_SETTING]({ commit, dispatch, state }, { key, value }) {
|
|
|
|
await commit(mutations.SET_SETTING, { key, value })
|
|
|
|
if (key === 'viewMode') {
|
|
|
|
await commit(mutations.SET_VIEW_MODE, value)
|
|
|
|
}
|
|
|
|
if (key === 'sorting') {
|
|
|
|
await commit(mutations.RESET_PAGE)
|
2019-09-09 16:17:02 +00:00
|
|
|
}
|
2020-03-28 17:15:59 +00:00
|
|
|
if (state.public) {
|
|
|
|
return
|
|
|
|
}
|
2019-08-29 09:05:29 +00:00
|
|
|
return axios
|
2020-03-28 17:15:59 +00:00
|
|
|
.post(url(state, `/settings/${key}`), {
|
2019-12-10 21:33:54 +00:00
|
|
|
[key]: value,
|
2019-08-29 09:05:29 +00:00
|
|
|
})
|
|
|
|
.catch(err => {
|
2019-09-24 15:18:29 +00:00
|
|
|
console.error(err)
|
2019-08-29 09:05:29 +00:00
|
|
|
commit(
|
|
|
|
mutations.SET_ERROR,
|
|
|
|
AppGlobal.methods.t('bookmarks', 'Failed to change setting')
|
2019-09-24 15:18:29 +00:00
|
|
|
)
|
|
|
|
throw err
|
|
|
|
})
|
2019-08-29 09:05:29 +00:00
|
|
|
},
|
|
|
|
[actions.LOAD_SETTING]({ commit, dispatch, state }, key) {
|
|
|
|
return axios
|
2020-03-28 17:15:59 +00:00
|
|
|
.get(url(state, `/settings/${key}`))
|
2019-10-12 18:00:15 +00:00
|
|
|
.then(async response => {
|
2019-08-29 09:05:29 +00:00
|
|
|
const {
|
2019-12-10 21:33:54 +00:00
|
|
|
data: { [key]: value },
|
2019-09-24 15:18:29 +00:00
|
|
|
} = response
|
2019-10-12 18:00:15 +00:00
|
|
|
await commit(mutations.SET_SETTING, { key, value })
|
2020-04-08 14:48:16 +00:00
|
|
|
switch (key) {
|
|
|
|
case 'viewMode':
|
2019-10-12 18:00:15 +00:00
|
|
|
await commit(mutations.SET_VIEW_MODE, value)
|
2020-04-08 14:48:16 +00:00
|
|
|
break
|
|
|
|
case 'sorting':
|
2019-10-12 18:00:15 +00:00
|
|
|
await commit(mutations.RESET_PAGE)
|
2020-04-08 14:48:16 +00:00
|
|
|
break
|
2019-08-29 09:05:29 +00:00
|
|
|
}
|
|
|
|
})
|
|
|
|
.catch(err => {
|
2019-09-24 15:18:29 +00:00
|
|
|
console.error(err)
|
2019-08-29 09:05:29 +00:00
|
|
|
commit(
|
|
|
|
mutations.SET_ERROR,
|
2020-04-07 14:49:11 +00:00
|
|
|
AppGlobal.methods.t('bookmarks', 'Failed to load setting {key}', { key })
|
2019-09-24 15:18:29 +00:00
|
|
|
)
|
|
|
|
throw err
|
|
|
|
})
|
2019-08-29 09:05:29 +00:00
|
|
|
},
|
|
|
|
[actions.LOAD_SETTINGS]({ commit, dispatch, state }) {
|
|
|
|
return Promise.all(
|
2020-08-21 15:18:30 +00:00
|
|
|
['sorting', 'viewMode', 'archivePath', 'limit'].map(key => dispatch(actions.LOAD_SETTING, key))
|
2019-09-24 15:18:29 +00:00
|
|
|
)
|
2019-12-10 21:33:54 +00:00
|
|
|
},
|
2020-03-28 17:15:59 +00:00
|
|
|
|
|
|
|
[actions.LOAD_SHARES_OF_FOLDER]({ commit, dispatch, state }, folderId) {
|
2020-04-02 15:06:39 +00:00
|
|
|
if (folderId === -1 || folderId === '-1') {
|
|
|
|
return Promise.resolve()
|
|
|
|
}
|
2020-03-28 17:15:59 +00:00
|
|
|
return axios
|
|
|
|
.get(url(state, `/folder/${folderId}/shares`))
|
|
|
|
.then(async response => {
|
|
|
|
const {
|
|
|
|
data: { data, status },
|
|
|
|
} = response
|
|
|
|
if (status !== 'success') throw new Error(data)
|
|
|
|
const shares = data
|
|
|
|
for (const share of shares) {
|
|
|
|
await commit(mutations.ADD_SHARE, share)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
.catch(err => {
|
|
|
|
console.error(err)
|
2020-04-02 15:37:15 +00:00
|
|
|
// Don't set a notification as this is expected to happen for subfolders of shares that we don't have a RESHAR permission for
|
2020-03-28 17:15:59 +00:00
|
|
|
throw err
|
|
|
|
})
|
|
|
|
},
|
2020-03-30 12:31:54 +00:00
|
|
|
[actions.CREATE_SHARE]({ commit, dispatch, state }, { folderId, type, participant }) {
|
|
|
|
return axios
|
|
|
|
.post(url(state, `/folder/${folderId}/shares`), {
|
|
|
|
folderId,
|
|
|
|
participant,
|
|
|
|
type,
|
|
|
|
})
|
|
|
|
.then(async response => {
|
|
|
|
const {
|
|
|
|
data: { item, data, status },
|
|
|
|
} = response
|
|
|
|
if (status !== 'success') throw new Error(data)
|
|
|
|
await commit(mutations.ADD_SHARE, item)
|
|
|
|
})
|
|
|
|
.catch(err => {
|
|
|
|
console.error(err)
|
|
|
|
commit(
|
|
|
|
mutations.SET_ERROR,
|
2020-04-07 14:49:11 +00:00
|
|
|
AppGlobal.methods.t('bookmarks', 'Failed to create share for folder {folderId}', { folderId })
|
2020-03-30 12:31:54 +00:00
|
|
|
)
|
|
|
|
throw err
|
|
|
|
})
|
|
|
|
},
|
|
|
|
[actions.EDIT_SHARE]({ commit, dispatch, state }, { shareId, canWrite, canShare }) {
|
|
|
|
return axios
|
|
|
|
.put(url(state, `/share/${shareId}`), {
|
|
|
|
canWrite,
|
|
|
|
canShare,
|
|
|
|
})
|
|
|
|
.then(async response => {
|
|
|
|
const {
|
|
|
|
data: { item, data, status },
|
|
|
|
} = response
|
|
|
|
if (status !== 'success') throw new Error(data)
|
|
|
|
await commit(mutations.ADD_SHARE, item)
|
|
|
|
})
|
|
|
|
.catch(err => {
|
|
|
|
console.error(err)
|
|
|
|
commit(
|
|
|
|
mutations.SET_ERROR,
|
2020-04-07 14:49:11 +00:00
|
|
|
AppGlobal.methods.t('bookmarks', 'Failed to update share {shareId}', { shareId })
|
2020-03-30 12:31:54 +00:00
|
|
|
)
|
|
|
|
throw err
|
|
|
|
})
|
|
|
|
},
|
|
|
|
[actions.DELETE_SHARE]({ commit, dispatch, state }, shareId) {
|
|
|
|
return axios
|
|
|
|
.delete(url(state, `/share/${shareId}`))
|
|
|
|
.then(async response => {
|
|
|
|
const {
|
|
|
|
data: { data, status },
|
|
|
|
} = response
|
|
|
|
if (status !== 'success') throw new Error(data)
|
|
|
|
await commit(mutations.REMOVE_SHARE, shareId)
|
|
|
|
})
|
|
|
|
.catch(err => {
|
|
|
|
console.error(err)
|
|
|
|
commit(
|
|
|
|
mutations.SET_ERROR,
|
2020-04-07 14:49:11 +00:00
|
|
|
AppGlobal.methods.t('bookmarks', 'Failed to delete share {shareId}', { shareId })
|
2020-03-30 12:31:54 +00:00
|
|
|
)
|
|
|
|
throw err
|
|
|
|
})
|
|
|
|
},
|
|
|
|
|
2020-03-28 17:15:59 +00:00
|
|
|
[actions.LOAD_PUBLIC_LINK]({ commit, dispatch, state }, folderId) {
|
|
|
|
return axios
|
|
|
|
.get(url(state, `/folder/${folderId}/publictoken`), {
|
|
|
|
validateStatus: (status) => status === 404 || status === 200,
|
|
|
|
})
|
|
|
|
.then(async response => {
|
|
|
|
const {
|
|
|
|
data: { item, data, status },
|
|
|
|
} = response
|
|
|
|
if (response.status === 404) {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
if (status !== 'success') throw new Error(data)
|
|
|
|
const token = item
|
|
|
|
await commit(mutations.ADD_PUBLIC_TOKEN, { folderId, token })
|
|
|
|
})
|
|
|
|
.catch(err => {
|
|
|
|
console.error(err)
|
2020-04-19 19:29:25 +00:00
|
|
|
// Not sending a notification because we might just not have enough permissions to see this
|
2020-03-28 17:15:59 +00:00
|
|
|
})
|
|
|
|
},
|
|
|
|
[actions.CREATE_PUBLIC_LINK]({ commit, dispatch, state }, folderId) {
|
|
|
|
return axios
|
|
|
|
.post(url(state, `/folder/${folderId}/publictoken`))
|
|
|
|
.then(async response => {
|
|
|
|
const {
|
|
|
|
data: { item, data, status },
|
|
|
|
} = response
|
|
|
|
if (status !== 'success') throw new Error(data)
|
|
|
|
const token = item
|
|
|
|
await commit(mutations.ADD_PUBLIC_TOKEN, { folderId, token })
|
|
|
|
})
|
|
|
|
.catch(err => {
|
|
|
|
console.error(err)
|
|
|
|
commit(
|
|
|
|
mutations.SET_ERROR,
|
2020-04-07 14:49:11 +00:00
|
|
|
AppGlobal.methods.t('bookmarks', 'Failed to create public link for folder {folderId}', { folderId })
|
2020-03-28 17:15:59 +00:00
|
|
|
)
|
|
|
|
throw err
|
|
|
|
})
|
|
|
|
},
|
|
|
|
[actions.DELETE_PUBLIC_LINK]({ commit, dispatch, state }, folderId) {
|
|
|
|
return axios
|
|
|
|
.delete(url(state, `/folder/${folderId}/publictoken`))
|
|
|
|
.then(async response => {
|
|
|
|
const {
|
|
|
|
data: { data, status },
|
|
|
|
} = response
|
|
|
|
if (status !== 'success') throw new Error(data)
|
|
|
|
await commit(mutations.REMOVE_PUBLIC_TOKEN, { folderId })
|
|
|
|
})
|
|
|
|
.catch(err => {
|
|
|
|
console.error(err)
|
|
|
|
commit(
|
|
|
|
mutations.SET_ERROR,
|
2020-04-07 14:49:11 +00:00
|
|
|
AppGlobal.methods.t('bookmarks', 'Failed to delete public link for folder {folderId}', { folderId })
|
2020-03-28 17:15:59 +00:00
|
|
|
)
|
|
|
|
throw err
|
|
|
|
})
|
|
|
|
},
|
2019-09-24 15:18:29 +00:00
|
|
|
}
|
2019-08-29 09:05:29 +00:00
|
|
|
|
2020-03-28 17:15:59 +00:00
|
|
|
function url(state, url) {
|
|
|
|
if (state.public) {
|
|
|
|
url = `/apps/bookmarks/public/rest/v2${url}`
|
|
|
|
} else {
|
|
|
|
url = `/apps/bookmarks${url}`
|
|
|
|
}
|
2019-09-24 15:18:29 +00:00
|
|
|
return generateUrl(url)
|
2019-08-29 09:05:29 +00:00
|
|
|
}
|