Skip to content

Commit

Permalink
Merge pull request #40409 from nextcloud/feat/f2v-unread-comments-action
Browse files Browse the repository at this point in the history
  • Loading branch information
skjnldsv authored Sep 16, 2023
2 parents f8ccaf8 + 03ece12 commit 006f5d0
Show file tree
Hide file tree
Showing 30 changed files with 339 additions and 304 deletions.
193 changes: 193 additions & 0 deletions apps/comments/src/actions/inlineUnreadCommentsAction.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,193 @@
/**
* @copyright Copyright (c) 2023 Lucas Azevedo <lhs_azevedo@hotmail.com>
*
* @author Lucas Azevedo <lhs_azevedo@hotmail.com>
*
* @license AGPL-3.0-or-later
*
* 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/>.
*
*/
import { action } from './inlineUnreadCommentsAction'
import { expect } from '@jest/globals'
import { File, Permission, View, FileAction } from '@nextcloud/files'
import logger from '../logger'

const view = {
id: 'files',
name: 'Files',
} as View

describe('Inline unread comments action display name tests', () => {
test('Default values', () => {
const file = new File({
id: 1,
source: 'https://cloud.domain.com/remote.php/dav/files/admin/foobar.txt',
owner: 'admin',
mime: 'text/plain',
permissions: Permission.ALL,
attributes: {
'comments-unread': 1,
},
})

expect(action).toBeInstanceOf(FileAction)
expect(action.id).toBe('comments-unread')
expect(action.displayName([file], view)).toBe('1 new comment')
expect(action.iconSvgInline([], view)).toBe('<svg>SvgMock</svg>')
expect(action.enabled!([file], view)).toBe(true)
expect(action.inline!(file, view)).toBe(true)
expect(action.default).toBeUndefined()
expect(action.order).toBe(-140)
})

test('Display name when file has two new comments', () => {
const file = new File({
id: 1,
source: 'https://cloud.domain.com/remote.php/dav/files/admin/foobar.txt',
owner: 'admin',
mime: 'text/plain',
permissions: Permission.ALL,
attributes: {
'comments-unread': 2,
},
})

expect(action.displayName([file], view)).toBe('2 new comments')
})
})

describe('Inline unread comments action enabled tests', () => {
test('Action is disabled when comments-unread attribute is missing', () => {
const file = new File({
id: 1,
source: 'https://cloud.domain.com/remote.php/dav/files/admin/foobar.txt',
owner: 'admin',
mime: 'text/plain',
permissions: Permission.ALL,
attributes: { },
})

expect(action.enabled!([file], view)).toBe(false)
})

test('Action is disabled when file does not have unread comments', () => {
const file = new File({
id: 1,
source: 'https://cloud.domain.com/remote.php/dav/files/admin/foobar.txt',
owner: 'admin',
mime: 'text/plain',
permissions: Permission.ALL,
attributes: {
'comments-unread': 0,
},
})

expect(action.enabled!([file], view)).toBe(false)
})

test('Action is enabled when file has a single unread comment', () => {
const file = new File({
id: 1,
source: 'https://cloud.domain.com/remote.php/dav/files/admin/foobar.txt',
owner: 'admin',
mime: 'text/plain',
permissions: Permission.ALL,
attributes: {
'comments-unread': 1,
},
})

expect(action.enabled!([file], view)).toBe(true)
})

test('Action is enabled when file has a two unread comments', () => {
const file = new File({
id: 1,
source: 'https://cloud.domain.com/remote.php/dav/files/admin/foobar.txt',
owner: 'admin',
mime: 'text/plain',
permissions: Permission.ALL,
attributes: {
'comments-unread': 2,
},
})

expect(action.enabled!([file], view)).toBe(true)
})
})

describe('Inline unread comments action execute tests', () => {
test('Action opens sidebar', async () => {
const openMock = jest.fn()
const setActiveTabMock = jest.fn()
window.OCA = {
Files: {
Sidebar: {
open: openMock,
setActiveTab: setActiveTabMock,
},
},
}

const file = new File({
id: 1,
source: 'https://cloud.domain.com/remote.php/dav/files/admin/foobar.txt',
owner: 'admin',
mime: 'text/plain',
permissions: Permission.ALL,
attributes: {
'comments-unread': 1,
},
})

const result = await action.exec!(file, view, '/')

expect(result).toBe(null)
expect(setActiveTabMock).toBeCalledWith('comments')
expect(openMock).toBeCalledWith('/foobar.txt')
})

test('Action handles sidebar open failure', async () => {
const openMock = jest.fn(() => { throw new Error('Mock error') })
const setActiveTabMock = jest.fn()
window.OCA = {
Files: {
Sidebar: {
open: openMock,
setActiveTab: setActiveTabMock,
},
},
}
jest.spyOn(logger, 'error').mockImplementation(() => jest.fn())

const file = new File({
id: 1,
source: 'https://cloud.domain.com/remote.php/dav/files/admin/foobar.txt',
owner: 'admin',
mime: 'text/plain',
permissions: Permission.ALL,
attributes: {
'comments-unread': 1,
},
})

const result = await action.exec!(file, view, '/')

expect(result).toBe(false)
expect(setActiveTabMock).toBeCalledWith('comments')
expect(openMock).toBeCalledWith('/foobar.txt')
expect(logger.error).toBeCalledTimes(1)
})
})
61 changes: 61 additions & 0 deletions apps/comments/src/actions/inlineUnreadCommentsAction.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
/**
* @copyright Copyright (c) 2023 Lucas Azevedo <lhs_azevedo@hotmail.com>
*
* @author Lucas Azevedo <lhs_azevedo@hotmail.com>
*
* @license AGPL-3.0-or-later
*
* 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/>.
*
*/
import { FileAction, Node, registerFileAction } from '@nextcloud/files'
import { translate as t, translatePlural as n } from '@nextcloud/l10n'
import commentProcessingSvg from '@mdi/svg/svg/comment-processing.svg?raw'
import logger from '../logger'

export const action = new FileAction({
id: 'comments-unread',

displayName(nodes: Node[]) {
const unread = nodes[0].attributes['comments-unread'] as number
if (unread >= 0) {
return n('comments', '1 new comment', '{unread} new comments', unread, { unread })
}
return t('comments', 'Comment')
},

iconSvgInline: () => commentProcessingSvg,

enabled(nodes: Node[]) {
const unread = nodes[0].attributes['comments-unread'] as number|undefined
return typeof unread === 'number' && unread > 0
},

async exec(node: Node) {
try {
window.OCA.Files.Sidebar.setActiveTab('comments')
await window.OCA.Files.Sidebar.open(node.path)
return null
} catch (error) {
logger.error('Error while opening sidebar', { error })
return false
}
},

inline: () => true,

order: -140,
})

registerFileAction(action)
2 changes: 1 addition & 1 deletion apps/comments/src/comments.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@

import './app.js'
import './templates.js'
import './filesplugin.js'
import './activitytabviewplugin.js'
import './actions/inlineUnreadCommentsAction.ts'

window.OCA.Comments = OCA.Comments
Loading

0 comments on commit 006f5d0

Please sign in to comment.