feat(share): record and display share activity (#549)

* feat(share): record and display share activity

* style(share): remove activity entry borders

* docs(share): explain share link activity
This commit is contained in:
Yiling-J
2026-11-25 12:23:11 +08:00
committed by GitHub
parent 9c8eb16259
commit e32445684f
51 changed files with 3208 additions and 227 deletions
+68 -55
View File
@@ -3,9 +3,14 @@ import type { Context } from 'hono'
import { getCookie } from 'hono/cookie'
import { zValidator } from '@hono/zod-validator'
import { shareService } from '@shumai/core/src/share/share'
import { shareActivityService } from '@shumai/core/src/share/share-activity'
import { assetService } from '@shumai/core/src/asset/asset'
import { watermarkService } from '@shumai/core/src/watermark/watermark'
import { paginationParamsSchema, assetTypeFilterSchema } from '@shumai/dtos'
import {
paginationParamsSchema,
assetTypeFilterSchema,
recordShareViewRequestSchema,
} from '@shumai/dtos'
import { z } from 'zod'
import { metadataService } from '@shumai/core/src/metadata/metadata'
import { FieldInfo } from '@shumai/dtos'
@@ -156,7 +161,36 @@ const sharedChildrenRequestSchema = paginationParamsSchema.extend({
assetType: assetTypeFilterSchema.optional().default('file'),
})
async function getPublicShareUserId(c: Context, shareId: string) {
const session = await auth.api.getSession({ headers: c.req.raw.headers })
if (session?.user) return session.user.id
const token = getCookie(c, `shumai_guest_${shareId}`) || getCookie(c, 'shumai_guest')
const verified = token && verifyGuestToken(token, shareId)
if (!verified) return undefined
const guestUser = await userService.getUserById(verified.userId)
return guestUser?.guestEmail ? guestUser.id : undefined
}
const route = app
.post('/shares/:shareId/views', zValidator('json', recordShareViewRequestSchema), async (c) => {
const shareId = c.req.param('shareId')
const password = c.req.header('x-share-password')
const { assetId, isShareEntry } = c.req.valid('json')
try {
const share = await shareService.verifyPublicAccess(shareId, assetId, password)
const targetId =
assetId && assetId !== share.rootFolderId
? await assetService.resolveLatestVersionId(assetId)
: undefined
const userId = await getPublicShareUserId(c, shareId)
await shareActivityService.recordView(shareId, { assetId: targetId, isShareEntry, userId })
return c.body(null, 204)
} catch (err) {
return handlePublicShareError(c, err)
}
})
.get('/shares/:shareId/info', async (c) => {
const shareId = c.req.param('shareId')
const password = c.req.header('x-share-password')
@@ -305,12 +339,18 @@ const route = app
})
.post(
'/shares/:shareId/files/:fileId/download-url',
zValidator('json', z.object({ key: z.string().min(1) })),
zValidator(
'json',
z.object({
key: z.string().min(1),
purpose: z.enum(['download', 'prepare', 'clipboard']).default('download'),
}),
),
async (c) => {
const shareId = c.req.param('shareId')
const fileId = c.req.param('fileId')
const password = c.req.header('x-share-password')
const { key } = c.req.valid('json')
const { key, purpose } = c.req.valid('json')
try {
const shareLink = await shareService.verifyPublicAccess(shareId, fileId, password)
@@ -323,7 +363,16 @@ const route = app
return c.json({ error: 'Invalid key' }, 400)
}
const url = await assetService.getDownloadUrl(fileId, key)
const { url, assetId } = await assetService.getDownloadUrlWithAsset(fileId, key)
if (assetId !== fileId) await shareService.verifyPublicAccess(shareId, assetId, password)
if (purpose === 'download') {
const userId = await getPublicShareUserId(c, shareId)
await shareActivityService.recordEvent(shareId, {
kind: 'asset_downloaded',
assetId,
userId,
})
}
return c.json({ url })
} catch (err) {
return handlePublicShareError(c, err)
@@ -343,25 +392,7 @@ const route = app
await shareService.verifyPublicAccess(shareId, fileId, password)
const targetFileId = await assetService.resolveTargetAssetId(fileId)
let userId: string | undefined
const session = await auth.api.getSession({
headers: c.req.raw.headers,
})
if (session?.user) {
userId = session.user.id
} else {
const cookieName = `shumai_guest_${shareId}`
const token = getCookie(c, cookieName) || getCookie(c, 'shumai_guest')
if (token) {
const verified = verifyGuestToken(token, shareId)
if (verified) {
const guestUser = await userService.getUserById(verified.userId)
if (guestUser && guestUser.guestEmail) {
userId = guestUser.id
}
}
}
}
const userId = await getPublicShareUserId(c, shareId)
const res = await assetService.listComments(targetFileId, req, userId)
return c.json(res)
} catch (err) {
@@ -383,48 +414,30 @@ const route = app
}
try {
const shareLink = await shareService.getShareLink(shareId)
await shareService.verifyPublicAccess(shareId, fileId, password)
const shareLink = await shareService.verifyPublicAccess(shareId, fileId, password)
const targetFileId = await assetService.resolveTargetAssetId(fileId)
const teamId = await projectService.getProjectTeam(shareLink.projectId)
// Identify user
let userId: string | null = null
const session = await auth.api.getSession({
headers: c.req.raw.headers,
})
if (session?.user) {
userId = session.user.id
} else {
const cookieName = `shumai_guest_${shareId}`
const token = getCookie(c, cookieName) || getCookie(c, 'shumai_guest')
if (token) {
const verified = verifyGuestToken(token, shareId)
if (verified) {
const guestUser = await userService.getUserById(verified.userId)
if (guestUser && guestUser.guestEmail) {
userId = guestUser.id
}
}
}
}
const userId = await getPublicShareUserId(c, shareId)
if (!userId) {
return c.json({ error: 'Unauthorized' }, 401)
}
const comment = await assetService.createComment({
assetId: targetFileId,
userId: userId,
replyToId: req.replyToId,
message: req.message,
annotations: req.annotations,
second: req.second,
attachmentIds: req.attachmentIds,
})
const comment = await assetService.createComment(
{
assetId: targetFileId,
userId,
replyToId: req.replyToId,
message: req.message,
annotations: req.annotations,
second: req.second,
attachmentIds: req.attachmentIds,
},
{ shareLinkId: shareId },
)
const notifType = req.replyToId ? 'reply_created' : 'comment_created'
+160 -5
View File
@@ -4,9 +4,15 @@ import shareRoute from './share'
import publicShareRoute from './public-share'
import { authMiddleware } from './middleware/auth'
import { shareService } from '@shumai/core/src/share/share'
import { shareActivityService } from '@shumai/core/src/share/share-activity'
import { HTTPException } from 'hono/http-exception'
import { assetService } from '@shumai/core/src/asset/asset'
import { authzService, Permission, ResourceType } from '@shumai/core/src/authz/authz'
import { ShareLinkPasswordInvalidError, ShareLinkExpiredError } from '@shumai/core/src/share/errors'
import {
ShareLinkPasswordInvalidError,
ShareLinkExpiredError,
ShareLinkNotFoundError,
} from '@shumai/core/src/share/errors'
import { createGuestToken } from '@shumai/core/src/auth/guest-token'
import { userService } from '@shumai/core/src/user/user'
import { notificationService } from '@shumai/core/src/notification/notification'
@@ -93,6 +99,82 @@ describe('Share API', () => {
})
// Public Routes
describe('POST /shares/:shareId/views', () => {
test('attributes an authorized asset view to a verified guest', async () => {
vi.spyOn(shareService, 'verifyPublicAccess').mockResolvedValue({
rootFolderId: 'folder1',
} as Awaited<ReturnType<typeof shareService.verifyPublicAccess>>)
vi.spyOn(assetService, 'resolveLatestVersionId').mockResolvedValue('asset1')
vi.spyOn(userService, 'getUserById').mockResolvedValue({
id: 'guest1',
guestEmail: 'alice@example.com',
} as Awaited<ReturnType<typeof userService.getUserById>>)
const record = vi.spyOn(shareActivityService, 'recordView').mockResolvedValue(undefined)
const token = createGuestToken({ userId: 'guest1', shareId: 'share1' })
const res = await app.request('/shares/share1/views', {
method: 'POST',
headers: {
'Content-Type': 'application/json',
'x-share-password': 'pass',
Cookie: `shumai_guest_share1=${token}`,
},
body: JSON.stringify({ assetId: 'alias1', isShareEntry: true }),
})
expect(res.status).toBe(204)
expect(shareService.verifyPublicAccess).toHaveBeenCalledWith('share1', 'alias1', 'pass')
expect(record).toHaveBeenCalledWith('share1', {
assetId: 'asset1',
isShareEntry: true,
userId: 'guest1',
})
})
test('a rejected view leaves the counter and history alone', async () => {
vi.spyOn(shareService, 'verifyPublicAccess').mockRejectedValue(
new ShareLinkPasswordInvalidError('Incorrect password'),
)
const record = vi.spyOn(shareActivityService, 'recordView').mockResolvedValue(undefined)
const res = await app.request('/shares/share1/views', {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: '{}',
})
expect(res.status).toBe(401)
expect(record).not.toHaveBeenCalled()
})
})
describe('GET /shares/:shareId/activities', () => {
test('returns the requested page to a share reader', async () => {
const list = vi.spyOn(shareActivityService, 'list').mockResolvedValue({
data: [],
pageInfo: { total: 0 },
})
const res = await app.request('/shares/share1/activities?first=20')
expect(res.status).toBe(200)
expect(await res.json()).toEqual({ data: [], pageInfo: { total: 0 } })
expect(authzService.hasPermission).toHaveBeenCalledWith(
expect.objectContaining({
id: 'share1',
permission: Permission.Read,
type: ResourceType.Share,
}),
)
expect(list).toHaveBeenCalledWith('share1', { first: 20 })
})
test('denies access to activity without share read permission', async () => {
vi.mocked(authzService.hasPermission).mockRejectedValueOnce(new HTTPException(403))
const list = vi.spyOn(shareActivityService, 'list')
const res = await app.request('/shares/share1/activities')
expect(res.status).toBe(403)
expect(list).not.toHaveBeenCalled()
})
})
describe('GET /shares/:shareId/info', () => {
test('Success', async () => {
vi.spyOn(shareService, 'getShareLink').mockResolvedValue({
@@ -101,6 +183,8 @@ describe('Share API', () => {
isDisabled: false,
allowDownload: false,
isExpired: false,
views: 0,
lastViewedAt: null,
hasPassword: true,
rootFolderId: 'folder1',
projectId: 'project1',
@@ -134,6 +218,8 @@ describe('Share API', () => {
isDisabled: false,
allowDownload: false,
isExpired: false,
views: 0,
lastViewedAt: null,
hasPassword: true,
rootFolderId: 'folder1',
projectId: 'project1',
@@ -161,6 +247,8 @@ describe('Share API', () => {
isDisabled: false,
allowDownload: false,
isExpired: false,
views: 0,
lastViewedAt: null,
hasPassword: true,
rootFolderId: 'folder1',
projectId: 'project1',
@@ -712,6 +800,8 @@ describe('Share API', () => {
rootFolderId: 'root1',
projectId: 'project1',
isExpired: false,
views: 0,
lastViewedAt: null,
hasPassword: false,
createdAt: new Date().toISOString(),
updatedAt: new Date().toISOString(),
@@ -756,6 +846,8 @@ describe('Share API', () => {
rootFolderId: 'root1',
projectId: 'project1',
isExpired: false,
views: 0,
lastViewedAt: null,
hasPassword: false,
createdAt: new Date().toISOString(),
updatedAt: new Date().toISOString(),
@@ -813,6 +905,8 @@ describe('Share API', () => {
rootFolderId: 'root1',
projectId: 'project1',
isExpired: false,
views: 0,
lastViewedAt: null,
hasPassword: false,
createdAt: new Date().toISOString(),
updatedAt: new Date().toISOString(),
@@ -844,6 +938,8 @@ describe('Share API', () => {
rootFolderId: 'root1',
projectId: 'project1',
isExpired: false,
views: 0,
lastViewedAt: null,
hasPassword: false,
createdAt: new Date().toISOString(),
updatedAt: new Date().toISOString(),
@@ -888,6 +984,8 @@ describe('Share API', () => {
rootFolderId: 'root1',
projectId: 'project1',
isExpired: false,
views: 0,
lastViewedAt: null,
hasPassword: false,
watermarkStatus: 'processing',
watermarkConfigId: 'cfg1',
@@ -976,12 +1074,60 @@ describe('Share API', () => {
})
describe('POST /shares/:shareId/files/:fileId/download-url', () => {
test('requires share access to the concrete asset selected for download', async () => {
vi.spyOn(shareService, 'verifyPublicAccess')
.mockResolvedValueOnce({
allowDownload: true,
} as Awaited<ReturnType<typeof shareService.verifyPublicAccess>>)
.mockRejectedValueOnce(new ShareLinkNotFoundError('Asset is not shared'))
vi.spyOn(assetService, 'getDownloadUrlWithAsset').mockResolvedValue({
url: 'https://s3.example.com/download',
assetId: 'unshared',
})
const record = vi.spyOn(shareActivityService, 'recordEvent').mockResolvedValue(undefined)
const res = await app.request('/shares/share1/files/file1/download-url', {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({ key: 'files/download.png' }),
})
expect(res.status).toBe(404)
expect(await res.json()).toEqual({ error: 'Asset is not shared' })
expect(record).not.toHaveBeenCalled()
})
test.each(['prepare', 'clipboard'])(
'does not count a URL requested for %s',
async (purpose) => {
vi.spyOn(shareService, 'verifyPublicAccess').mockResolvedValue({
allowDownload: true,
} as Awaited<ReturnType<typeof shareService.verifyPublicAccess>>)
vi.spyOn(assetService, 'getDownloadUrlWithAsset').mockResolvedValue({
url: 'https://s3.example.com/download',
assetId: 'target1',
})
const record = vi.spyOn(shareActivityService, 'recordEvent').mockResolvedValue(undefined)
const res = await app.request('/shares/share1/files/symlink1/download-url', {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({ key: 'files/target1/fly.png', purpose }),
})
expect(res.status).toBe(200)
expect(record).not.toHaveBeenCalled()
},
)
test('Success for symlink asset in share', async () => {
vi.spyOn(shareService, 'verifyPublicAccess').mockResolvedValue({
allowDownload: true,
// eslint-disable-next-line @typescript-eslint/no-explicit-any
} as any)
vi.spyOn(assetService, 'getDownloadUrl').mockResolvedValue('https://s3.example.com/download')
vi.spyOn(assetService, 'getDownloadUrlWithAsset').mockResolvedValue({
url: 'https://s3.example.com/download',
assetId: 'target1',
})
const record = vi.spyOn(shareActivityService, 'recordEvent').mockResolvedValue(undefined)
const res = await app.request('/shares/share1/files/symlink1/download-url', {
method: 'POST',
@@ -993,7 +1139,15 @@ describe('Share API', () => {
const body = await res.json()
expect(body.url).toBe('https://s3.example.com/download')
expect(shareService.verifyPublicAccess).toHaveBeenCalledWith('share1', 'symlink1', undefined)
expect(assetService.getDownloadUrl).toHaveBeenCalledWith('symlink1', 'files/target1/fly.png')
expect(assetService.getDownloadUrlWithAsset).toHaveBeenCalledWith(
'symlink1',
'files/target1/fly.png',
)
expect(record).toHaveBeenCalledWith('share1', {
kind: 'asset_downloaded',
assetId: 'target1',
userId: undefined,
})
})
test('Rejects when allowDownload is disabled', async () => {
@@ -1004,8 +1158,8 @@ describe('Share API', () => {
// eslint-disable-next-line @typescript-eslint/no-explicit-any
} as any)
const getDownloadUrlSpy = vi
.spyOn(assetService, 'getDownloadUrl')
.mockResolvedValue('https://s3.example.com/download')
.spyOn(assetService, 'getDownloadUrlWithAsset')
.mockResolvedValue({ url: 'https://s3.example.com/download', assetId: 'target1' })
const res = await app.request('/shares/share1/files/symlink1/download-url', {
method: 'POST',
@@ -1173,6 +1327,7 @@ describe('Share API', () => {
userId: 'guest1',
message: 'hello from alice',
}),
{ shareLinkId: 'share1' },
)
})
+12
View File
@@ -2,11 +2,13 @@ import { Hono } from 'hono'
import { zValidator } from '@hono/zod-validator'
import { authzService, Permission, ResourceType } from '@shumai/core/src/authz/authz'
import { shareService } from '@shumai/core/src/share/share'
import { shareActivityService } from '@shumai/core/src/share/share-activity'
import { projectService } from '@shumai/core/src/project/project'
import {
createShareLinkRequestSchema,
updateShareLinkRequestSchema,
listShareLinksRequestSchema,
paginationParamsSchema,
addAssetToShareRequestSchema,
updateShareLinkWatermarkRequestSchema,
AuditAction,
@@ -90,6 +92,16 @@ const route = new Hono<{ Variables: { user: User } }>()
const shareLink = await shareService.getShareLink(shareId)
return c.json(shareLink)
})
.get('/shares/:shareId/activities', zValidator('query', paginationParamsSchema), async (c) => {
const shareId = c.req.param('shareId')
await authzService.hasPermission({
user: c.get('user'),
permission: Permission.Read,
type: ResourceType.Share,
id: shareId,
})
return c.json(await shareActivityService.list(shareId, c.req.valid('query')))
})
.put('/shares/:shareId', zValidator('json', updateShareLinkRequestSchema), async (c) => {
const shareId = c.req.param('shareId')
const user = c.get('user')