|
|
@@ -35,6 +35,8 @@ import { getPageSchema, extractToAncestorsPaths, populateDataToShowRevision } fr
|
|
|
import type { UserGroupDocument } from './user-group';
|
|
|
import UserGroupRelation from './user-group-relation';
|
|
|
|
|
|
+type ObjectId = mongoose.Types.ObjectId;
|
|
|
+
|
|
|
const logger = loggerFactory('growi:models:page');
|
|
|
/*
|
|
|
* define schema
|
|
|
@@ -80,11 +82,16 @@ export type FindRecentUpdatedPagesOption = {
|
|
|
}
|
|
|
|
|
|
export type CreateMethod = (path: string, body: string, user, options: IOptionsForCreate) => Promise<HydratedDocument<PageDocument>>
|
|
|
+type FindByPathAndViewerMethod =
|
|
|
+ (this: PageModel, id: string | ObjectId, user, userGroups?, includeEmpty?: boolean) => Promise<HydratedDocument<PageDocument> | null>
|
|
|
+type CountByPathAndViewerMethod =
|
|
|
+ (this: PageModel, id: string | ObjectId, user, userGroups?, includeEmpty?: boolean) => Promise<number>
|
|
|
|
|
|
export interface PageModel extends Model<PageDocument> {
|
|
|
[x: string]: any; // for obsolete static methods
|
|
|
createEmptyPage(path: string, parent, descendantCount?: number): Promise<HydratedDocument<PageDocument>>
|
|
|
- findByIdAndViewer(pageId: ObjectIdLike, user, userGroups?, includeEmpty?: boolean): Promise<HydratedDocument<PageDocument> | null>
|
|
|
+ findByIdAndViewer: FindByPathAndViewerMethod
|
|
|
+ countByIdAndViewer: CountByPathAndViewerMethod
|
|
|
findByIdsAndViewer(
|
|
|
pageIds: ObjectIdLike[], user, userGroups?, includeEmpty?: boolean, includeAnyoneWithTheLink?: boolean,
|
|
|
): Promise<HydratedDocument<PageDocument>[]>
|
|
|
@@ -589,6 +596,36 @@ schema.statics.createEmptyPage = async function(
|
|
|
return page.save();
|
|
|
};
|
|
|
|
|
|
+const findByIdAndViewer: FindByPathAndViewerMethod = async function(this, id, user, userGroups = null, includeEmpty = false) {
|
|
|
+ const baseQuery = this.findOne({ _id: id });
|
|
|
+
|
|
|
+ const relatedUserGroups = (user != null && userGroups == null) ? [
|
|
|
+ ...(await UserGroupRelation.findAllUserGroupIdsRelatedToUser(user)),
|
|
|
+ ...(await ExternalUserGroupRelation.findAllUserGroupIdsRelatedToUser(user)),
|
|
|
+ ] : userGroups;
|
|
|
+
|
|
|
+ const queryBuilder = new this.PageQueryBuilder(baseQuery, includeEmpty);
|
|
|
+ queryBuilder.addConditionToFilteringByViewer(user, relatedUserGroups, true);
|
|
|
+
|
|
|
+ return queryBuilder.query.exec();
|
|
|
+};
|
|
|
+schema.statics.findByIdAndViewer = findByIdAndViewer;
|
|
|
+
|
|
|
+const countByIdAndViewer: CountByPathAndViewerMethod = async function(this, id, user, userGroups = null, includeEmpty = false) {
|
|
|
+ const baseQuery = this.countDocuments({ _id: id });
|
|
|
+
|
|
|
+ const relatedUserGroups = (user != null && userGroups == null) ? [
|
|
|
+ ...(await UserGroupRelation.findAllUserGroupIdsRelatedToUser(user)),
|
|
|
+ ...(await ExternalUserGroupRelation.findAllUserGroupIdsRelatedToUser(user)),
|
|
|
+ ] : userGroups;
|
|
|
+
|
|
|
+ const queryBuilder = new this.PageQueryBuilder(baseQuery, includeEmpty);
|
|
|
+ queryBuilder.addConditionToFilteringByViewer(user, relatedUserGroups, true);
|
|
|
+
|
|
|
+ return queryBuilder.query.exec();
|
|
|
+};
|
|
|
+schema.statics.countByIdAndViewer = countByIdAndViewer;
|
|
|
+
|
|
|
/**
|
|
|
* Replace an existing page with an empty page.
|
|
|
* It updates the children's parent to the new empty page's _id.
|