All files / lib/models page.ts

55.69% Statements 284/510
44.39% Branches 83/187
47.96% Functions 47/98
55.34% Lines 280/506

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148            16x 16x 16x 16x 16x 16x 16x 16x 16x 16x 16x 16x                                                                                                                                                                                                                               16x 16x     10x       5x       2x     16x                                     14x 14x           2x                       16x 16x   16x       16x         16x       16x       16x 9x 2x     7x     16x       16x 6x 1x 5x 2x     3x     16x 5x 3x     2x       2x     16x                 16x               16x           16x       16x       16x                                           16x                                             16x                               16x               16x                                         16x 2x 2x       2x     16x 1x 1x   1x     16x 1x 1x     16x 3x 3x     3x 3x   3x                 16x                           16x                                       16x 3x     16x                 16x                 16x 1x 1x 1x   1x   1x     16x 4x 2x     4x   4x     16x 1x     16x 2x 1x   2x     16x 3x     16x 5x       5x 5x 5x 2x       3x     16x 59x                           59x 59x 649x 649x 55x       59x     16x       16x           16x 5x 5x     16x               16x 3x   3x       3x     16x 2x   2x 1x     1x       16x 1x   1x 1x       1x 1x 1x                     16x 3x 3x       3x     16x 6x 6x       6x     16x                 16x                             16x                   16x                                     16x                                               16x                                           16x 5x 5x   5x     5x           5x 5x 5x     5x 5x 5x 5x       5x                                   5x 5x         5x     16x 5x 5x     16x               16x       16x         16x 3x     16x                                 16x               16x 6x 6x 6x     6x   6x   6x 6x 6x   6x   6x       6x     16x 7x 7x 7x 7x     7x       7x 7x       7x                       6x 6x 6x 6x 6x             16x                                     16x                                 16x                                               16x                                           16x                             16x           16x                               16x                                   16x 3x 3x 3x 3x   3x 3x     3x   3x 3x   3x       3x 3x     16x 2x 2x 5x   5x     16x 1x 1x 1x 2x 2x 2x   2x 2x           2x 1x   2x   1x     16x 1x 1x   3x                   1x   3x 3x       3x 3x 3x               16x       16x 9x 9x   9x 9x 9x   9x 9x   9x 9x 9x 36x 36x 27x 27x     9x   9x     16x 84x 84x   6x   78x     16x 16x 16x 16x 16x 16x 16x 16x   16x   16x    
import Crowi from 'server/crowi'
import { Types, Document, Model, Schema, model } from 'mongoose'
import Debug from 'debug'
import { RevisionDocument } from './revision'
import { UserDocument } from './user'
 
const GRANT_PUBLIC = 1
const GRANT_RESTRICTED = 2
const GRANT_SPECIFIED = 3
const GRANT_OWNER = 4
const PAGE_GRANT_ERROR = 1
const STATUS_WIP = 'wip'
const STATUS_PUBLISHED = 'published'
const STATUS_DELETED = 'deleted'
const STATUS_DEPRECATED = 'deprecated'
const TYPE_PORTAL = 'portal'
const TYPE_USER = 'user'
const TYPE_PUBLIC = 'public'
 
export interface PageDocument extends Document {
  _id: Types.ObjectId
  path: string
  revision: Types.ObjectId
  redirectTo: string
  status: string
  grant: number
  grantedUsers: Types.ObjectId[]
  creator: Types.ObjectId
  lastUpdateUser: Types.ObjectId
  liker: Types.ObjectId[]
  seenUsers: Types.ObjectId[]
  commentCount: number
  extended: object
  createdAt: Date
  updatedAt: Date
 
  // dynamic fields
  latestRevision?: Types.ObjectId
  likerCount?: number
  seenUsersCount?: number
 
  isPublished(): boolean
  isDeleted(): boolean
  isDeprecated(): boolean
  isPublic(): boolean
  isPortal(): boolean
  isCreator(user: any): boolean
  isGrantedFor(user: any): boolean
  isLatestRevision(): boolean
  isUpdatable(previousRevision): boolean
  isLiked(user: any): boolean
  isRedirectOriginPage(): boolean
  isUnlinkable(user: any): boolean
  isWIP(): boolean
  like(user: any): any
  unlike(user: any): any
  unlink(user: any): any
  isSeenUser(user: any): any
  seen(user: any): any
  getSlackChannel(): any
  updateSlackChannel(slackChannel): any
  updateExtended(extended: object): any
  getNotificationTargetUsers(): any
}
 
export interface PageModel extends Model<PageDocument> {
  GRANT_PUBLIC: number
  GRANT_RESTRICTED: number
  GRANT_SPECIFIED: number
  GRANT_OWNER: number
  PAGE_GRANT_ERROR: number
  TYPE_PORTAL: string
  TYPE_PUBLIC: string
  TYPE_USER: string
 
  populatePageData(pageData, revisionId?: Types.ObjectId | null): Promise<PageDocument>
  populatePagesRevision(pages, revisions): any
  populatePageListToAnyObjects(pageIdObjectArray): any
  updateCommentCount(page, num): any
  hasPortalPage(path, user, revisionId?): Promise<boolean>
  findPortalPage(path, user, revisionId?): Promise<PageDocument | null>
  getGrantLabels(): any
  normalizePath(path): any
  getUserPagePath(user): any
  getDeletedPageName(path): any
  getRevertDeletedPageName(path): any
  isDeletableName(path): any
  isCreatableName(name): any
  fixToCreatableName(path): any
  updateRevision(pageId, revisionId, cb): any
  exists(query): any
  findUpdatedList(offset, limit, cb): any
  findPageById(id): Promise<PageDocument>
  findPageByIdAndGrantedUser(id, userData): Promise<PageDocument>
  findPage(path, userData, revisionId?, ignoreNotFound?): Promise<PageDocument | null>
  findPageByPath(path): Promise<PageDocument>
  isExistByPath(path): any
  isExistById(id): any
  findListByPageIds(ids, options): any
  findPageByRedirectTo(path): any
  findPagesByIds(ids): any
  findListByCreator(user, option, currentUser): any
  getStreamOfFindAll(options): any
  findListByStartWith(path, userData, option): Promise<PageDocument[]>
  findChildrenByPath(path, userData, option): any
  findUnfurlablePages(type, array, grants?: number[]): any
  findUnfurlablePagesByIds(ids): any
  findUnfurlablePagesByPaths(paths): any
  updatePageProperty(page, updateData): any
  updateGrant(page, grant, userData): any
  pushToGrantedUsers(page, userData): any
  pushRevision(pageData, newRevision, user): any
  createPage(path, body, user, options): any
  updatePage(pageData: PageDocument, body, user, options: any): any
  deletePage(pageData: PageDocument, user): any
  revertDeletedPage(pageData: PageDocument, user): Promise<PageDocument>
  completelyDeletePage(pageData: PageDocument, user?): Promise<PageDocument>
  removePage(pageData: PageDocument): any
  removePageById(pageId): any
  removePageByPath(pagePath): any
  removeRedirectOriginPageByPath(pagePath): any
  rename(pageData, newPagePath, user, options): any
  getPathMap(paths, search, replace): any
  checkPagesRenamable(paths, user): any
  renameTree(pathMap, user, options): any
  allPageCount(): any
}
 
export default (crowi: Crowi) => {
  const debug = Debug('crowi:models:page')
  const pageEvent = crowi.event('Page')
 
  function isPortalPath(path) {
    return path.endsWith('/')
  }
 
  function addTrailingSlash(path) {
    return path.endsWith('/') ? path : `${path}/`
  }
 
  function removeTrailingSlash(string) {
    return string.endsWith('/') ? string.substring(0, string.length - 1) : string
  }
 
  const pageSchema = new Schema<PageDocument, PageModel>(
    {
      path: { type: String, required: true, index: true, unique: true },
      revision: { type: Schema.Types.ObjectId, ref: 'Revision' },
      redirectTo: { type: String, index: true },
      status: { type: String, default: STATUS_PUBLISHED, index: true },
      grant: { type: Number, default: GRANT_PUBLIC, index: true },
      grantedUsers: [{ type: Schema.Types.ObjectId, ref: 'User' }],
      creator: { type: Schema.Types.ObjectId, ref: 'User', index: true },
      // lastUpdateUser: this schema is from 1.5.x (by deletion feature), and null is default.
      // the last update user on the screen is by revesion.author for B.C.
      lastUpdateUser: { type: Schema.Types.ObjectId, ref: 'User', index: true },
      liker: [{ type: Schema.Types.ObjectId, ref: 'User', index: true }],
      seenUsers: [{ type: Schema.Types.ObjectId, ref: 'User', index: true }],
      commentCount: { type: Number, default: 0 },
      extended: {
        type: String,
        default: '{}',
        get: function(data) {
          try {
            return JSON.parse(data)
          } catch (e) {
            return data
          }
        },
        set: function(data) {
          return JSON.stringify(data)
        },
      },
      createdAt: { type: Date, default: Date.now },
      updatedAt: Date,
    },
    {
      toJSON: { getters: true },
      toObject: { getters: true },
    },
  )
 
  pageEvent.on('create', pageEvent.onCreate)
  pageEvent.on('update', pageEvent.onUpdate)
 
  pageSchema.methods.isWIP = function() {
    return this.status === STATUS_WIP
  }
 
  pageSchema.methods.isPublished = function() {
    // null: this is for B.C.
    return this.status === null || this.status === STATUS_PUBLISHED
  }
 
  pageSchema.methods.isDeleted = function() {
    return this.status === STATUS_DELETED
  }
 
  pageSchema.methods.isDeprecated = function() {
    return this.status === STATUS_DEPRECATED
  }
 
  pageSchema.methods.isPublic = function() {
    if (!this.grant || this.grant == GRANT_PUBLIC) {
      return true
    }
 
    return false
  }
 
  pageSchema.methods.isPortal = function() {
    return isPortalPath(this.path)
  }
 
  pageSchema.methods.isCreator = function(userData) {
    if (this.populated('creator') && ((this.creator as any) as UserDocument)._id.toString() === userData._id.toString()) {
      return true
    } else if (this.creator.toString() === userData._id.toString()) {
      return true
    }
 
    return false
  }
 
  pageSchema.methods.isGrantedFor = function(userData) {
    if (this.isPublic() || this.isCreator(userData)) {
      return true
    }
 
    Iif (this.grantedUsers.indexOf(userData._id) >= 0) {
      return true
    }
 
    return false
  }
 
  pageSchema.methods.isLatestRevision = function() {
    // populate されていなくて判断できない
    if (!this.latestRevision || !this.revision) {
      return true
    }
 
    return this.latestRevision == (((this.revision as any) as RevisionDocument)._id.toString() as any)
  }
 
  pageSchema.methods.isUpdatable = function(previousRevision) {
    var revision = this.latestRevision || this.revision
    if (revision != previousRevision) {
      return false
    }
    return true
  }
 
  pageSchema.methods.isLiked = function(userData) {
    return this.liker.some(function(likedUser) {
      return likedUser == userData._id.toString()
    })
  }
 
  pageSchema.methods.isRedirectOriginPage = function() {
    return this.redirectTo !== null
  }
 
  pageSchema.methods.isUnlinkable = function(userData) {
    return this.isRedirectOriginPage() && this.isGrantedFor(userData)
  }
 
  pageSchema.methods.like = async function(userData) {
    var Activity = crowi.model('Activity')
 
    const added = ((this.liker as any) as Types.Array<UserDocument>).addToSet(userData._id)
    if (added.length > 0) {
      const data = await this.save()
 
      debug('liker updated!', added)
 
      try {
        const activityLog = await Activity.createByPageLike(data, userData)
        debug('Activity created', activityLog)
      } catch (err) {
        debug('Activity err', err)
      }
 
      return data
    } else {
      debug('liker not updated')
    }
  }
 
  pageSchema.methods.unlike = async function(userData) {
    const Activity = crowi.model('Activity')
 
    const liker = (this.liker as any) as Types.Array<UserDocument>
    const beforeCount = liker.length
    liker.pull(userData._id)
    if (liker.length != beforeCount) {
      const data = await this.save()
 
      try {
        await Activity.removeByPageUnlike(data, userData)
        debug('Activity removed')
      } catch (err) {
        debug('Activity remove err', err)
      }
 
      return data
    } else {
      debug('liker not updated')
    }
  }
 
  // Unlink: Remove redirect origin page
  pageSchema.methods.unlink = async function(userData) {
    const Page = crowi.model('Page')
    if (this.isUnlinkable(userData)) {
      debug('Unlink page', this._id, this.path)
      try {
        const redirectPage = await Page.removePageById(this._id)
        debug('Redirect Page deleted', redirectPage.path)
      } catch (err) {
        debug('Error occured while get setting', err, err.stack)
        throw new Error(`Failed to delete redirect page (${this.path}).`)
      }
    } else {
      throw new Error('Page is not unlinkable')
    }
  }
 
  pageSchema.methods.isSeenUser = function(userData) {
    const seenUsers = (this.seenUsers as any) as UserDocument[]
 
    return seenUsers.some(function(seenUser) {
      return seenUser.equals(userData._id)
    })
  }
 
  pageSchema.methods.seen = async function(userData) {
    const seenUsers = (this.seenUsers as any) as Types.Array<UserDocument>
 
    if (this.isSeenUser(userData)) {
      debug('seenUsers not updated')
      return this
    }
 
    if (!userData || !userData._id) {
      throw new Error('User data is not valid')
    }
 
    const added = seenUsers.addToSet(userData)
 
    await this.save()
 
    debug('seenUsers updated!', added)
 
    return this
  }
 
  pageSchema.methods.getSlackChannel = function() {
    var extended = this.get('extended')
    Iif (!extended) {
      return ''
    }
 
    return extended.slack || ''
  }
 
  pageSchema.methods.updateSlackChannel = function(slackChannel) {
    var extended = this.extended as any
    extended.slack = slackChannel
 
    return this.updateExtended(extended)
  }
 
  pageSchema.methods.updateExtended = function(extended) {
    this.extended = extended
    return this.save()
  }
 
  pageSchema.statics.populatePageData = function(pageData: PageDocument, revisionId) {
    pageData.latestRevision = pageData.revision
    Iif (revisionId) {
      pageData.revision = revisionId
    }
    pageData.likerCount = pageData.liker.length || 0
    pageData.seenUsersCount = pageData.seenUsers.length || 0
 
    return pageData
      .populate([
        { path: 'lastUpdateUser', model: 'User' },
        { path: 'creator', model: 'User' },
        { path: 'revision', model: 'Revision', populate: { path: 'author', model: 'User' } },
      ])
      .execPopulate()
  }
 
  pageSchema.statics.populatePagesRevision = async function(pages, revisions) {
    if (pages.length !== revisions.length) {
      throw new TypeError('page.length must be equal revisions.length')
    }
    pages = pages.map((page, i) => {
      const revision = revisions[i]
      if (revision) {
        page.revision = revision
      }
      return page
    })
    return Page.populate(pages, { path: 'revision', model: 'Revision' })
  }
 
  pageSchema.statics.populatePageListToAnyObjects = async function(pageIdObjectArray) {
    const pageIdMappings = {}
    const pageIds = pageIdObjectArray.map(function(page, idx) {
      if (!page._id) {
        throw new Error('Pass the arg of populatePageListToAnyObjects() must have _id on each element.')
      }
 
      pageIdMappings[String(page._id)] = idx
      return page._id
    })
 
    const pages = await Page.findListByPageIds(pageIds, { limit: 100 }) // limit => if the pagIds is greater than 100, ignore
 
    for (const p of pages) {
      Object.assign(pageIdObjectArray[pageIdMappings[String(p._id)]], p._doc)
    }
 
    return pageIdObjectArray
  }
 
  pageSchema.statics.updateCommentCount = function(page, num) {
    return Page.updateOne({ _id: page }, { commentCount: num }, {})
  }
 
  pageSchema.statics.hasPortalPage = async function(path, user, revisionId) {
    try {
      const page = await Page.findPage(path, user, revisionId)
      return !!page
    } catch (err) {
      return false
    }
  }
 
  pageSchema.statics.findPortalPage = async function(path, user, revisionId) {
    try {
      const page = await Page.findPage(path, user, revisionId)
      return page
    } catch (err) {
      return null
    }
  }
 
  pageSchema.statics.getGrantLabels = function() {
    var grantLabels = {}
    grantLabels[GRANT_PUBLIC] = 'Public' // 公開
    grantLabels[GRANT_RESTRICTED] = 'Anyone with the link' // リンクを知っている人のみ
    // grantLabels[GRANT_SPECIFIED]  = 'Specified users only'; // 特定ユーザーのみ
    grantLabels[GRANT_OWNER] = 'Just me' // 自分のみ
 
    return grantLabels
  }
 
  pageSchema.statics.normalizePath = function(path) {
    if (!path.match(/^\//)) {
      path = '/' + path
    }
 
    path = path.replace(/\/\s+?/g, '/').replace(/\s+\//g, '/')
 
    return path
  }
 
  pageSchema.statics.getUserPagePath = function(user) {
    return '/user/' + user.username
  }
 
  pageSchema.statics.getDeletedPageName = function(path) {
    if (path.match('/')) {
      path = path.substr(1)
    }
    return '/trash/' + path
  }
 
  pageSchema.statics.getRevertDeletedPageName = function(path) {
    return path.replace('/trash', '')
  }
 
  pageSchema.statics.isDeletableName = function(path) {
    var notDeletable = [
      /^\/user\/[^/]+$/, // user page
    ]
 
    for (var i = 0; i < notDeletable.length; i++) {
      var pattern = notDeletable[i]
      if (path.match(pattern)) {
        return false
      }
    }
 
    return true
  }
 
  pageSchema.statics.isCreatableName = function(name) {
    var forbiddenPages = [
      /\^|\$|\*|\+|\?|#/,
      /^\/_.*/, // /_api/* and so on
      /^\/-\/.*/,
      /^\/_r\/.*/,
      /^\/user\/[^/]+\/(bookmarks|comments|activities|pages|recent-create|recent-edit)/, // reserved
      /^\/?https?:\/\/.+$/, // avoid miss in renaming
      /\/{2,}/, // avoid miss in renaming
      /\s+\/\s+/, // avoid miss in renaming
      /.+\/edit$/,
      /.+\.md$/,
      /^\/(installer|register|login|logout|admin|me|files|trash|paste|comments)(\/.*|$)/,
    ]
 
    var isCreatable = true
    forbiddenPages.forEach(function(page) {
      var pageNameReg = new RegExp(page)
      if (name.match(pageNameReg)) {
        isCreatable = false
      }
    })
 
    return isCreatable
  }
 
  pageSchema.statics.fixToCreatableName = function(path) {
    return path.replace(/\/\//g, '/')
  }
 
  pageSchema.statics.updateRevision = function(pageId, revisionId, cb) {
    Page.updateOne({ _id: pageId }, { revision: revisionId }, {}, function(err, data) {
      cb(err, data)
    })
  }
 
  pageSchema.statics.exists = async function(query) {
    const count = await Page.countDocuments(query)
    return count > 0
  }
 
  pageSchema.statics.findUpdatedList = function(offset, limit, cb) {
    Page.find({})
      .sort({ updatedAt: -1 })
      .skip(offset)
      .limit(limit)
      .exec()
  }
 
  pageSchema.statics.findPageById = async function(id) {
    const pageData = await Page.findOne({ _id: id })
 
    Iif (pageData === null) {
      throw new Error('Page not found')
    }
 
    return Page.populatePageData(pageData, null)
  }
 
  pageSchema.statics.findPageByIdAndGrantedUser = async function(id, userData) {
    const pageData = await Page.findPageById(id)
 
    if (userData && !pageData.isGrantedFor(userData)) {
      throw new Error('Page is not granted for the user') // PAGE_GRANT_ERROR, null);
    }
 
    return pageData
  }
 
  // find page and check if granted user
  pageSchema.statics.findPage = async function(path, userData, revisionId, ignoreNotFound) {
    const pageData = await Page.findOne({ path })
 
    Eif (pageData === null) {
      Iif (ignoreNotFound) {
        return null
      }
 
      const pageNotFoundError = new Error('Page Not Found')
      pageNotFoundError.name = 'Crowi:Page:NotFound'
      throw pageNotFoundError
    }
 
    if (!pageData.isGrantedFor(userData)) {
      throw new Error('Page is not granted for the user') // PAGE_GRANT_ERROR, null);
    }
 
    return Page.populatePageData(pageData, revisionId || null)
  }
 
  // find page by path
  pageSchema.statics.findPageByPath = async function(path) {
    const pageData = await Page.findOne({ path })
    Iif (pageData === null) {
      throw new Error('Page not found')
    }
 
    return pageData
  }
 
  pageSchema.statics.isExistByPath = async function(path) {
    const pageData = await Page.findOne({ path })
    Iif (pageData === null) {
      return false
    }
 
    return pageData._id
  }
 
  pageSchema.statics.isExistById = async function(id) {
    const pageData = await Page.findOne({ _id: id })
    if (pageData === null) {
      return false
    }
 
    return pageData._id
  }
 
  pageSchema.statics.findListByPageIds = function(ids, options) {
    options = options || {}
    const limit = options.limit || 50
    const offset = options.skip || 0
 
    return (
      Page.find({ _id: { $in: ids } })
        // .sort({createdAt: -1}) // TODO optionize
        .skip(offset)
        .limit(limit)
        .populate([{ path: 'creator', model: 'User' }, { path: 'revision', model: 'Revision', populate: { path: 'author' } }])
        .exec()
    )
  }
 
  pageSchema.statics.findPageByRedirectTo = async function(path) {
    const pageData = await Page.findOne({ redirectTo: path })
 
    if (pageData === null) {
      throw new Error('Page not found')
    }
 
    return pageData
  }
 
  pageSchema.statics.findPagesByIds = function(ids) {
    return Page.find({
      _id: { $in: ids },
      redirectTo: null,
    })
      .populate([
        { path: 'creator', model: 'User' },
        {
          path: 'revision',
          model: 'Revision',
          populate: {
            path: 'author',
            model: 'User',
          },
        },
      ])
      .exec()
  }
 
  pageSchema.statics.findListByCreator = function(user, option, currentUser) {
    var limit = option.limit || 50
    var offset = option.offset || 0
    var conditions: any = {
      creator: user._id,
      redirectTo: null,
      $or: [{ status: null }, { status: STATUS_PUBLISHED }],
    }
 
    if (!user.equals(currentUser._id)) {
      conditions.grant = GRANT_PUBLIC
    }
 
    return Page.find(conditions)
      .sort({ createdAt: -1 })
      .skip(offset)
      .limit(limit)
      .populate({ path: 'revision', populate: { path: 'author' } })
      .exec()
  }
 
  /**
   * Bulk get (for internal only)
   */
  pageSchema.statics.getStreamOfFindAll = function(options) {
    var options = options || {}
    var publicOnly = options.publicOnly !== false
    var criteria: any = { redirectTo: null }
 
    if (publicOnly) {
      criteria.grant = GRANT_PUBLIC
    }
 
    return Page.find(criteria)
      .populate([{ path: 'creator', model: 'User' }, { path: 'revision', model: 'Revision' }])
      .lean()
      .cursor()
  }
 
  /**
   * findListByStartWith
   *
   * If `path` has `/` at the end, returns '{path}/*' and '{path}' self.
   * If `path` doesn't have `/` at the end, returns '{path}*'
   * e.g.
   */
  pageSchema.statics.findListByStartWith = function(path, userData, option) {
    const pathCondition: Record<string, string | RegExp>[] = []
    const includeDeletedPage = option.includeDeletedPage || false
 
    Iif (!option) {
      option = { sort: 'updatedAt', desc: -1, offset: 0, limit: 50 }
    }
    const opt = {
      sort: option.sort || 'updatedAt',
      desc: option.desc || -1,
      offset: option.offset || 0,
      limit: option.limit === 0 ? 0 : option.limit || 50,
    }
    const sortOpt = {}
    sortOpt[opt.sort] = opt.desc
    const queryReg = new RegExp('^' + path)
    // var sliceOption = option.revisionSlice || { $slice: 1 }
 
    pathCondition.push({ path: queryReg })
    Eif (path.match(/\/$/)) {
      debug('Page list by ending with /, so find also upper level page')
      pathCondition.push({ path: path.substr(0, path.length - 1) })
    }
 
    // FIXME: might be heavy
    const q = Page.find({
      redirectTo: null,
      $or: [
        { grant: null },
        { grant: GRANT_PUBLIC },
        { grant: GRANT_RESTRICTED, grantedUsers: userData._id },
        { grant: GRANT_SPECIFIED, grantedUsers: userData._id },
        { grant: GRANT_OWNER, grantedUsers: userData._id },
      ],
    })
      .populate({ path: 'revision', populate: { path: 'revision.author', model: 'User' } })
      .and({
        $or: pathCondition,
      } as any)
      .sort(sortOpt)
      .skip(opt.offset)
      .limit(opt.limit)
 
    Eif (!includeDeletedPage) {
      q.and({
        $or: [{ status: null }, { status: STATUS_PUBLISHED }],
      } as any)
    }
 
    return q.exec()
  }
 
  pageSchema.statics.findChildrenByPath = async function(path, userData, option) {
    path = addTrailingSlash(path)
    return Page.findListByStartWith(path, userData, { limit: 0, ...option })
  }
 
  pageSchema.statics.findUnfurlablePages = async function(type, array, grants = [GRANT_PUBLIC, GRANT_RESTRICTED]) {
    const page = await Page.find({
      [type]: { $in: array },
      $or: grants.map(grant => ({ grant })),
    })
    return page
  }
 
  pageSchema.statics.findUnfurlablePagesByIds = async function(ids) {
    return Page.findUnfurlablePages('_id', ids)
  }
 
  pageSchema.statics.findUnfurlablePagesByPaths = async function(paths) {
    // `GRANT_RESTRICTED` pages can not be accessed using path
    return Page.findUnfurlablePages('path', paths, [GRANT_PUBLIC])
  }
 
  pageSchema.statics.updatePageProperty = function(page, updateData) {
    return Page.updateOne({ _id: page._id }, { $set: updateData })
  }
 
  pageSchema.statics.updateGrant = async function(page, grant, userData) {
    page.grant = grant
    if (grant == GRANT_PUBLIC) {
      page.grantedUsers = []
    } else {
      page.grantedUsers = []
      page.grantedUsers.addToSet(userData._id)
    }
 
    const data = await page.save()
 
    debug('Page.updateGrant, saved grantedUsers.', (data && data.path) || {})
 
    return data
  }
 
  // Instance method でいいのでは
  pageSchema.statics.pushToGrantedUsers = function(page, userData) {
    if (!page.grantedUsers || !Array.isArray(page.grantedUsers)) {
      page.grantedUsers = []
    }
    page.grantedUsers.addToSet(userData)
    return page.save()
  }
 
  pageSchema.statics.pushRevision = async function(pageData, newRevision, user) {
    const isCreate = pageData.revision === undefined
    Eif (isCreate) {
      debug('pushRevision on Create')
    }
 
    await newRevision.save()
 
    debug('Successfully saved new revision', newRevision)
 
    pageData.revision = newRevision
    pageData.lastUpdateUser = user
    pageData.updatedAt = Date.now()
 
    const data = pageData.save()
 
    Iif (!isCreate) {
      debug('pushRevision on Update')
    }
 
    return data
  }
 
  pageSchema.statics.createPage = async function(path, body, user, options) {
    const Revision = crowi.model('Revision')
    const format = options.format || 'markdown'
    let grant = options.grant || GRANT_PUBLIC
    const redirectTo = options.redirectTo || null
 
    // force public
    Iif (isPortalPath(path)) {
      grant = GRANT_PUBLIC
    }
 
    const pageData = await Page.findOne({ path })
    Iif (pageData) {
      throw new Error('Cannot create new page to existed path')
    }
 
    const newPage = await Page.create({
      path,
      creator: user,
      lastUpdateUser: user,
      createdAt: Date.now(),
      updatedAt: Date.now(),
      redirectTo: redirectTo,
      grant: grant,
      status: STATUS_PUBLISHED,
      grantedUsers: user ? [user] : [],
    })
 
    const newRevision = Revision.prepareRevision(newPage, body, user, { format })
    try {
      const revisionData = await Page.pushRevision(newPage, newRevision, user)
      pageEvent.emit('create', revisionData, user)
      return revisionData
    } catch (err) {
      debug('Push Revision Error on create page', err)
      throw err
    }
  }
 
  pageSchema.statics.updatePage = async function(pageData, body, user, options = {}) {
    const Revision = crowi.model('Revision')
    const Bookmark = crowi.model('Bookmark')
    const grant = options.grant || null
    // update existing page
    const newRevision = Revision.prepareRevision(pageData, body, user)
 
    await Page.pushRevision(pageData, newRevision, user)
    const bookmarkCount = await Bookmark.countByPageId(pageData._id)
 
    if (grant != pageData.grant) {
      const data = await Page.updateGrant(pageData, grant, user)
      pageEvent.emit('update', data, user, bookmarkCount)
      return data
    }
    pageEvent.emit('update', pageData, user, bookmarkCount)
    return pageData
  }
 
  pageSchema.statics.deletePage = async function(pageData, user) {
    const Share = crowi.model('Share')
    const newPath = Page.getDeletedPageName(pageData.path)
    if (Page.isDeletableName(pageData.path)) {
      await Page.updatePageProperty(pageData, { status: STATUS_DELETED, lastUpdateUser: user })
      await Share.deleteByPageId(pageData._id)
      pageData.status = STATUS_DELETED
 
      // ページ名が /trash/ 以下に存在する場合、おかしなことになる
      // が、 /trash 以下にページが有るのは、個別に作っていたケースのみ。
      // 一応しばらく前から uncreatable pages になっているのでこれでいいことにする
      debug('Deleted the page, and rename it', pageData.path, newPath)
      return Page.rename(pageData, newPath, user, { createRedirectPage: true })
    }
    throw new Error('Page is not deletable.')
  }
 
  pageSchema.statics.revertDeletedPage = async function(pageData, user) {
    const newPath = Page.getRevertDeletedPageName(pageData.path)
 
    // 削除時、元ページの path には必ず redirectTo 付きで、ページが作成される。
    // そのため、そいつは削除してOK
    // が、redirectTo ではないページが存在している場合それは何かがおかしい。(データ補正が必要)
    const originPageData = await Page.findPageByPath(newPath)
    if (originPageData.redirectTo !== pageData.path) {
      throw new Error('The new page of to revert is exists and the redirect path of the page is not the deleted page.')
    }
 
    await Page.completelyDeletePage(originPageData)
    await Page.updatePageProperty(pageData, { status: STATUS_PUBLISHED, lastUpdateUser: user })
    pageData.status = STATUS_PUBLISHED
 
    debug('Revert deleted the page, and rename again it', pageData, newPath)
    await Page.rename(pageData, newPath, user, {})
    pageData.path = newPath
    return pageData
  }
 
  /**
   * This is danger.
   */
  pageSchema.statics.completelyDeletePage = async function(pageData, user) {
    // Delete Bookmarks, Attachments, Revisions, Pages and emit delete
    const Bookmark = crowi.model('Bookmark')
    const Attachment = crowi.model('Attachment')
    const Comment = crowi.model('Comment')
    const Activity = crowi.model('Activity')
    const pageId = pageData._id
 
    debug('Completely delete', pageData.path)
 
    await Bookmark.removeBookmarksByPageId(pageId)
    await Attachment.removeAttachmentsByPageId(pageId)
    await Comment.removeCommentsByPageId(pageId)
    await Page.removePageById(pageId)
    await Page.removeRedirectOriginPageByPath(pageData.path)
    await Activity.removeByPage(pageId)
 
    pageEvent.emit('delete', pageData, user) // update as renamed page
 
    return pageData
  }
 
  pageSchema.statics.removePage = async function(pageData) {
    const Revision = crowi.model('Revision')
    const { _id } = pageData
 
    debug('Remove phisically, the page', _id)
    try {
      await Page.deleteOne({ _id })
    } catch (err) {
      debug(' --> error', _id)
      throw err
    }
    await Revision.removeRevisionsByPath(pageData.path)
    return pageData
  }
 
  pageSchema.statics.removePageById = async function(pageId) {
    const pageData = await Page.findPageById(pageId)
    await Page.removePage(pageData)
    return pageData
  }
 
  pageSchema.statics.removePageByPath = async function(pagePath) {
    const pageData = await Page.findPageByPath(pagePath)
    await Page.removePage(pageData)
    return pageData
  }
 
  /**
   * remove the page that is redirecting to specified `pagePath` recursively
   *  ex: when
   *    '/page1' redirects to '/page2' and
   *    '/page2' redirects to '/page3'
   *    and given '/page3',
   *    '/page1' and '/page2' will be removed
   *
   * @param {string} pagePath
   */
  pageSchema.statics.removeRedirectOriginPageByPath = function(pagePath) {
    return Page.findPageByRedirectTo(pagePath)
      .then(redirectOriginPageData => {
        // remove
        return (
          Page.removePageById(redirectOriginPageData.id)
            // remove recursive
            .then(() => {
              return Page.removeRedirectOriginPageByPath(redirectOriginPageData.path)
            })
        )
      })
      .catch(err => {
        // do nothing if origin page doesn't exist
        return Promise.resolve()
      })
  }
 
  pageSchema.statics.rename = async function(pageData, newPagePath, user, options) {
    const Revision = crowi.model('Revision')
    const path = pageData.path
    const createRedirectPage = options.createRedirectPage || false
    const preserveUpdatedAt = options.preserveUpdatedAt || false
 
    const updatedAt = preserveUpdatedAt ? {} : { updatedAt: Date.now() }
    const updateData = { path: newPagePath, lastUpdateUser: user, ...updatedAt }
 
    // pageData の path を変更
    await Page.updatePageProperty(pageData, updateData)
    // reivisions の path を変更
    const data = await Revision.updateRevisionListByPath(path, { path: newPagePath })
    pageData.path = newPagePath
 
    Iif (createRedirectPage) {
      var body = 'redirect ' + newPagePath
      return Page.createPage(path, body, user, { redirectTo: newPagePath })
    }
    pageEvent.emit('update', pageData, user) // update as renamed page
    return data
  }
 
  pageSchema.statics.getPathMap = function(paths, search, replace) {
    search = removeTrailingSlash(search)
    replace = this.normalizePath(replace)
    const renamePath = path => path.replace(search, replace)
    // { [oldPath]: newPath }
    return paths.map(({ path }) => [path, renamePath(path)]).reduce((l, [k, v]) => Object.assign(l, { [k]: v }), {})
  }
 
  pageSchema.statics.checkPagesRenamable = async function(paths, user) {
    let error = false
    let errors = {}
    for (const path of paths) {
      const e: string[] = []
      Eif (!Page.isCreatableName(path)) {
        e.push('rename_tree.error.can_not_use_this_name')
      }
      const isAlreadyExists = await Page.exists({ path })
      Iif (isAlreadyExists) {
        const newPage = await Page.findPageByPath(path)
        if (!newPage.isUnlinkable(user)) {
          e.push('rename_tree.error.already_exists')
        }
      }
      if (!error && e.length > 0) {
        error = true
      }
      errors = Object.assign(errors, { [path]: e })
    }
    return [error, errors]
  }
 
  pageSchema.statics.renameTree = async function(pathMap, user, options) {
    const { createRedirectPage = false, preserveUpdatedAt = true } = options
    await Promise.all(
      Object.values(pathMap).map(async newPath => {
        Iif (await Page.exists({ path: newPath })) {
          const newPage = await Page.findPageByPath(newPath)
          if (newPage.isUnlinkable(user)) {
            await newPage.unlink(user)
          } else {
            throw new Error(`Failed to create this page (${newPage.path}). It already exists.`)
          }
        }
      }),
    )
    return Promise.all(
      Object.entries(pathMap).map(async ([oldPath, newPath]) => {
        try {
          const options = {
            createRedirectPage: !isPortalPath(newPath) && createRedirectPage,
            preserveUpdatedAt,
          }
          const oldPage = await Page.findPageByPath(oldPath)
          await Page.rename(oldPage, newPath, user, options)
          return oldPage
        } catch (err) {
          throw new Error(`Failed to update page (${oldPath}).`)
        }
      }),
    )
  }
 
  pageSchema.statics.allPageCount = function() {
    return Page.countDocuments({ redirectTo: null, grant: GRANT_PUBLIC }) // TODO: option にする
  }
 
  pageSchema.methods.getNotificationTargetUsers = async function() {
    var Comment = crowi.model('Comment')
    var Revision = crowi.model('Revision')
 
    const [commentCreators, revisionAuthors] = await Promise.all([Comment.findCreatorsByPage(this), Revision.findAuthorsByPage(this)])
    debug('commentCreators', commentCreators)
    debug('revisionAuthors', revisionAuthors)
 
    const targetUsers = [this.creator].concat(commentCreators, revisionAuthors)
    debug('targetUsers', targetUsers)
 
    var uniqueChecker = {}
    var uniqueUsers: Types.ObjectId[] = []
    targetUsers.forEach(function(user) {
      var userId = user.toString()
      if (uniqueChecker[userId] !== 1) {
        uniqueUsers.push(user)
        uniqueChecker[userId] = 1
      }
    })
    debug('uniqueUsers', uniqueUsers)
 
    return uniqueUsers
  }
 
  pageSchema.post('save', savedPage => {
    var Backlink = crowi.model('Backlink')
    Backlink.createBySavedPage(savedPage)
      .then(result => {
        debug(result)
      })
      .catch(err => err)
  })
 
  pageSchema.statics.GRANT_PUBLIC = GRANT_PUBLIC
  pageSchema.statics.GRANT_RESTRICTED = GRANT_RESTRICTED
  pageSchema.statics.GRANT_SPECIFIED = GRANT_SPECIFIED
  pageSchema.statics.GRANT_OWNER = GRANT_OWNER
  pageSchema.statics.PAGE_GRANT_ERROR = PAGE_GRANT_ERROR
  pageSchema.statics.TYPE_PORTAL = TYPE_PORTAL
  pageSchema.statics.TYPE_PUBLIC = TYPE_PUBLIC
  pageSchema.statics.TYPE_USER = TYPE_USER
 
  const Page = model<PageDocument, PageModel>('Page', pageSchema)
 
  return Page
}