cloud-py-api / nc_py_api

Nextcloud Python Framework
https://cloud-py-api.github.io/nc_py_api/
Other
84 stars 4 forks source link

get tags assigned to a file or a folder #259

Closed bigcat88 closed 3 months ago

bigcat88 commented 3 months ago

Discussed in https://github.com/cloud-py-api/nc_py_api/discussions/258

Originally posted by **zhongzishi** May 29, 2024 Hi, I passed through the code and found that it seems there is no method to get list of tags assigned to an object. There are methods such as assign_tag and unassign_tag, but I'm not able to find a list of tags already assigned to a file. Regards.
bigcat88 commented 3 months ago

This can be achieved by calling separate endpoint:

export const fetchTagsForFile = async (fileId: number): Promise<TagWithId[]> => {
    const path = '/systemtags-relations/files/' + fileId
    try {
        const { data: tags } = await davClient.getDirectoryContents(path, {
            data: fetchTagsPayload,
            details: true,
            glob: '/systemtags-relations/files/*/*', // Filter out first empty tag
        }) as ResponseDataDetailed<Required<FileStat>[]>
        return parseTags(tags)
    } catch (error) {
        logger.error(t('systemtags', 'Failed to load tags for file'), { error })
        throw new Error(t('systemtags', 'Failed to load tags for file'))
    }
}

Ref: https://github.com/nextcloud/server/blob/a5fd623469b40c44881e4635de073d334b371bae/apps/systemtags/src/services/files.ts#L31-L44