-
Notifications
You must be signed in to change notification settings - Fork 235
feat: Backend API for the Activity Log #10368
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
arvid-e
merged 10 commits into
feat/171499-activity-log
from
feat/171501-api-for-activity-log
Oct 14, 2025
Merged
Changes from 8 commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
91516ab
Add user actions for the activity log
arvid-e 73a3991
Copy user activity api from last branch
arvid-e b97512d
Reinstall using node 20
arvid-e 1c85929
Merge branch 'feat/171499-activity-log' into feat/171501-api-for-acti…
yuki-takei 39db017
Revert "Reinstall using node 20"
yuki-takei 0e4d3f9
Merge branch 'feat/171499-activity-log' into feat/171501-api-for-acti…
yuki-takei 84946c3
fix biome error
yuki-takei 9c68ca8
Update error code
arvid-e 3362901
Remove the need of type casting using request interfaces
arvid-e a2aa6c1
Make use of generic custom request to guarantee limit and offset types
arvid-e File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,258 @@ | ||
| import type { IUserHasId } from '@growi/core'; | ||
| import { serializeUserSecurely } from '@growi/core/dist/models/serializers'; | ||
| import type { Request, Router } from 'express'; | ||
| import express from 'express'; | ||
| import { query } from 'express-validator'; | ||
| import type { PipelineStage } from 'mongoose'; | ||
| import { Types } from 'mongoose'; | ||
|
|
||
| import type { IActivity } from '~/interfaces/activity'; | ||
| import { ActivityLogActions } from '~/interfaces/activity'; | ||
| import Activity from '~/server/models/activity'; | ||
| import { configManager } from '~/server/service/config-manager'; | ||
| import loggerFactory from '~/utils/logger'; | ||
|
|
||
| import type Crowi from '../../crowi'; | ||
| import { apiV3FormValidator } from '../../middlewares/apiv3-form-validator'; | ||
|
|
||
| import type { ApiV3Response } from './interfaces/apiv3-response'; | ||
|
|
||
| const logger = loggerFactory('growi:routes:apiv3:activity'); | ||
|
|
||
|
|
||
| const validator = { | ||
| list: [ | ||
| query('limit').optional().isInt({ max: 100 }).withMessage('limit must be a number less than or equal to 100'), | ||
| query('offset').optional().isInt().withMessage('page must be a number'), | ||
| query('searchFilter').optional().isString().withMessage('query must be a string'), | ||
| ], | ||
| }; | ||
|
|
||
| interface AuthorizedRequest extends Request { | ||
| user?: IUserHasId; | ||
| } | ||
|
|
||
| /** | ||
| * @swagger | ||
| * | ||
| * components: | ||
| * schemas: | ||
| * ActivityResponse: | ||
| * type: object | ||
| * properties: | ||
| * serializedPaginationResult: | ||
| * type: object | ||
| * properties: | ||
| * docs: | ||
| * type: array | ||
| * items: | ||
| * type: object | ||
| * properties: | ||
| * _id: | ||
| * type: string | ||
| * example: "67e33da5d97e8d3b53e99f95" | ||
| * targetModel: | ||
| * type: string | ||
| * example: "Page" | ||
| * target: | ||
| * type: string | ||
| * example: "675547e97f208f8050a361d4" | ||
| * action: | ||
| * type: string | ||
| * example: "PAGE_UPDATE" | ||
| * createdAt: | ||
| * type: string | ||
| * format: date-time | ||
| * example: "2025-03-25T23:35:01.584Z" | ||
| * user: | ||
| * type: object | ||
| * properties: | ||
| * _id: | ||
| * type: string | ||
| * example: "669a5aa48d45e62b521d00e4" | ||
| * name: | ||
| * type: string | ||
| * example: "Taro" | ||
| * username: | ||
| * type: string | ||
| * example: "growi" | ||
| * imageUrlCached: | ||
| * type: string | ||
| * example: "/images/icons/user.svg" | ||
| * totalDocs: | ||
| * type: integer | ||
| * example: 3 | ||
| * offset: | ||
| * type: integer | ||
| * example: 0 | ||
| * limit: | ||
| * type: integer | ||
| * example: 10 | ||
| * totalPages: | ||
| * type: integer | ||
| * example: 1 | ||
| * page: | ||
| * type: integer | ||
| * example: 1 | ||
| * pagingCounter: | ||
| * type: integer | ||
| * example: 1 | ||
| * hasPrevPage: | ||
| * type: boolean | ||
| * example: false | ||
| * hasNextPage: | ||
| * type: boolean | ||
| * example: false | ||
| * prevPage: | ||
| * type: integer | ||
| * nullable: true | ||
| * example: null | ||
| * nextPage: | ||
| * type: integer | ||
| * nullable: true | ||
| * example: null | ||
| */ | ||
|
|
||
| module.exports = (crowi: Crowi): Router => { | ||
| const loginRequiredStrictly = require('../../middlewares/login-required')(crowi); | ||
|
|
||
| const router = express.Router(); | ||
|
|
||
| /** | ||
| * @swagger | ||
| * | ||
| * /activity: | ||
| * get: | ||
| * summary: /activity | ||
| * tags: [Activity] | ||
| * security: | ||
| * - cookieAuth: [] | ||
| * - bearer: [] | ||
| * - accessTokenInQuery: [] | ||
| * parameters: | ||
| * - name: limit | ||
| * in: query | ||
| * required: false | ||
| * schema: | ||
| * type: integer | ||
| * - name: offset | ||
| * in: query | ||
| * required: false | ||
| * schema: | ||
| * type: integer | ||
| * - name: searchFilter | ||
| * in: query | ||
| * required: false | ||
| * schema: | ||
| * type: string | ||
| * responses: | ||
| * 200: | ||
| * description: Activity fetched successfully | ||
| * content: | ||
| * application/json: | ||
| * schema: | ||
| * $ref: '#/components/schemas/ActivityResponse' | ||
| */ | ||
| router.get('/', | ||
| loginRequiredStrictly, validator.list, apiV3FormValidator, async(req: AuthorizedRequest, res: ApiV3Response) => { | ||
|
|
||
| const defaultLimit = String(configManager.getConfig('customize:showPageLimitationS')); | ||
| const limit: number = parseInt(req.query.limit as string || defaultLimit, 10) || 10; | ||
| const offset: number = parseInt(req.query.offset as string || '0', 10) || 0; | ||
|
||
|
|
||
| const user = req.user; | ||
|
|
||
| if (!user || !user._id) { | ||
| logger.error('Authentication failure: req.user is missing after loginRequiredStrictly.'); | ||
| return res.apiv3Err('Authentication failed.', 401); | ||
| } | ||
|
|
||
| const userId = user._id; | ||
|
|
||
| try { | ||
| const userObjectId = new Types.ObjectId(userId); | ||
|
|
||
| const userActivityPipeline: PipelineStage[] = [ | ||
| { | ||
| $match: { | ||
| user: userObjectId, | ||
| action: { $in: Object.values(ActivityLogActions) }, | ||
| }, | ||
| }, | ||
| { | ||
| $facet: { | ||
| totalCount: [ | ||
| { $count: 'count' }, | ||
| ], | ||
| docs: [ | ||
| { $sort: { createdAt: -1 } }, | ||
| { $skip: offset }, | ||
| { $limit: limit }, | ||
|
|
||
| { | ||
| $lookup: { | ||
| from: 'users', | ||
| localField: 'user', | ||
| foreignField: '_id', | ||
| as: 'user', | ||
| }, | ||
| }, | ||
| { | ||
| $unwind: { | ||
| path: '$user', | ||
| preserveNullAndEmptyArrays: true, | ||
| }, | ||
| }, | ||
| { | ||
| $project: { | ||
| _id: 1, | ||
| 'user._id': 1, | ||
| 'user.username': 1, | ||
| 'user.name': 1, | ||
| 'user.imageUrlCached': 1, | ||
| action: 1, | ||
| createdAt: 1, | ||
| target: 1, | ||
| targetModel: 1, | ||
| }, | ||
| }, | ||
| ], | ||
| }, | ||
| }, | ||
| ]; | ||
|
|
||
| const [activityResults] = await Activity.aggregate(userActivityPipeline); | ||
|
|
||
| const serializedResults = activityResults.docs.map((doc: IActivity) => { | ||
| const { user, ...rest } = doc; | ||
| return { | ||
| user: serializeUserSecurely(user), | ||
| ...rest, | ||
| }; | ||
| }); | ||
|
|
||
| const totalDocs = activityResults.totalCount.length > 0 ? activityResults.totalCount[0].count : 0; | ||
| const totalPages = Math.ceil(totalDocs / limit); | ||
| const page = Math.floor(offset / limit) + 1; | ||
|
|
||
| const serializedPaginationResult = { | ||
| docs: serializedResults, | ||
| totalDocs, | ||
| limit, | ||
| offset, | ||
| page, | ||
| totalPages, | ||
| hasPrevPage: page > 1, | ||
| hasNextPage: page < totalPages, | ||
| }; | ||
|
|
||
| return res.apiv3({ serializedPaginationResult }); | ||
| } | ||
| catch (err) { | ||
| logger.error('Failed to get paginated activity', err); | ||
| return res.apiv3Err(err, 500); | ||
| } | ||
| }); | ||
|
|
||
| return router; | ||
| }; | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
キャストしなくても返り値は string 型のはず