| 80 | }); |
| 81 | } |
| 82 | async function _updateLastParentId(input: { |
| 83 | userId: string; |
| 84 | pageId: string; |
| 85 | parentPageId?: string; |
| 86 | dataAbstraction: DataAbstraction<typeof dataHashes>; |
| 87 | }) { |
| 88 | if (input.parentPageId == null) { |
| 89 | return; |
| 90 | } |
| 91 | |
| 92 | // Check for loop in parent chain |
| 93 | |
| 94 | const visitedPageIds = new Set<string>([input.pageId]); |
| 95 | |
| 96 | let loopCheckPageId: string | undefined = input.parentPageId; |
| 97 | let rootPageId: string | undefined; |
| 98 | |
| 99 | while (loopCheckPageId != null) { |
| 100 | if (visitedPageIds.has(loopCheckPageId)) { |
| 101 | mainLogger.sub('api/pages/bump').info('Loop detected in parent chain'); |
| 102 | return; |
| 103 | } |
| 104 | |
| 105 | visitedPageIds.add(loopCheckPageId); |
| 106 | |
| 107 | rootPageId = loopCheckPageId; |
| 108 | |
| 109 | loopCheckPageId = |
| 110 | (await input.dataAbstraction.hget( |
| 111 | 'user-page', |
| 112 | `${input.userId}:${loopCheckPageId}`, |
| 113 | 'last-parent-id', |
| 114 | )) ?? undefined; |
| 115 | } |
| 116 | |
| 117 | mainLogger.sub('api/pages/bump').info('No loop detected in parent chain'); |
| 118 | |
| 119 | // Check that root page is personal group's main page |
| 120 | |
| 121 | const personalGroupId = await input.dataAbstraction.hget( |
| 122 | 'user', |
| 123 | input.userId, |
| 124 | 'personal-group-id', |
| 125 | ); |
| 126 | |
| 127 | const mainPageId = await input.dataAbstraction.hget( |
| 128 | 'group', |
| 129 | personalGroupId, |
| 130 | 'main-page-id', |
| 131 | ); |
| 132 | |
| 133 | if (rootPageId !== mainPageId) { |
| 134 | throw new TRPCError({ |
| 135 | message: 'Invalid parent page.', |
| 136 | code: 'BAD_REQUEST', |
| 137 | }); |
| 138 | } |
| 139 | |