@teleporthq/teleport-project-generator-next 0.45.21 → 0.45.23
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/__tests__/code-copy-runtime.test.ts +31 -24
- package/__tests__/forum-files-route.test.ts +147 -0
- package/__tests__/forum-markdown.test.ts +136 -0
- package/__tests__/forum-page-data.test.ts +1329 -0
- package/__tests__/forum-project-plugin.test.ts +789 -0
- package/__tests__/forum-replies-route.test.ts +82 -0
- package/__tests__/forum-topics-only-end2end.test.ts +124 -0
- package/__tests__/i18n-app-runtime.test.ts +187 -0
- package/__tests__/page-revalidate-plugin.test.ts +111 -0
- package/__tests__/rich-text-editor-frame.test.ts +25 -0
- package/dist/cjs/forum/files-route.d.ts +26 -0
- package/dist/cjs/forum/files-route.d.ts.map +1 -0
- package/dist/cjs/forum/files-route.js +42 -0
- package/dist/cjs/forum/files-route.js.map +1 -0
- package/dist/cjs/forum/markdown-source.d.ts +17 -0
- package/dist/cjs/forum/markdown-source.d.ts.map +1 -0
- package/dist/cjs/forum/markdown-source.js +23 -0
- package/dist/cjs/forum/markdown-source.js.map +1 -0
- package/dist/cjs/forum/notifications-route.d.ts +17 -0
- package/dist/cjs/forum/notifications-route.d.ts.map +1 -0
- package/dist/cjs/forum/notifications-route.js +30 -0
- package/dist/cjs/forum/notifications-route.js.map +1 -0
- package/dist/cjs/forum/page-data-source.d.ts +58 -0
- package/dist/cjs/forum/page-data-source.d.ts.map +1 -0
- package/dist/cjs/forum/page-data-source.js +168 -0
- package/dist/cjs/forum/page-data-source.js.map +1 -0
- package/dist/cjs/forum/page-rewrite.d.ts +50 -0
- package/dist/cjs/forum/page-rewrite.d.ts.map +1 -0
- package/dist/cjs/forum/page-rewrite.js +473 -0
- package/dist/cjs/forum/page-rewrite.js.map +1 -0
- package/dist/cjs/forum/project-plugin.d.ts +34 -0
- package/dist/cjs/forum/project-plugin.d.ts.map +1 -0
- package/dist/cjs/forum/project-plugin.js +272 -0
- package/dist/cjs/forum/project-plugin.js.map +1 -0
- package/dist/cjs/forum/replies-route.d.ts +21 -0
- package/dist/cjs/forum/replies-route.d.ts.map +1 -0
- package/dist/cjs/forum/replies-route.js +33 -0
- package/dist/cjs/forum/replies-route.js.map +1 -0
- package/dist/cjs/forum/seo.d.ts +29 -0
- package/dist/cjs/forum/seo.d.ts.map +1 -0
- package/dist/cjs/forum/seo.js +61 -0
- package/dist/cjs/forum/seo.js.map +1 -0
- package/dist/cjs/forum/settings.d.ts +97 -0
- package/dist/cjs/forum/settings.d.ts.map +1 -0
- package/dist/cjs/forum/settings.js +162 -0
- package/dist/cjs/forum/settings.js.map +1 -0
- package/dist/cjs/index.d.ts +1 -0
- package/dist/cjs/index.d.ts.map +1 -1
- package/dist/cjs/index.js +87 -80
- package/dist/cjs/index.js.map +1 -1
- package/dist/cjs/instant-search/sections.d.ts.map +1 -1
- package/dist/cjs/instant-search/sections.js +18 -0
- package/dist/cjs/instant-search/sections.js.map +1 -1
- package/dist/cjs/instant-search/settings.d.ts +1 -1
- package/dist/cjs/instant-search/settings.d.ts.map +1 -1
- package/dist/cjs/instant-search/settings.js +2 -0
- package/dist/cjs/instant-search/settings.js.map +1 -1
- package/dist/cjs/internationalization/app-i18n-runtime.d.ts +39 -0
- package/dist/cjs/internationalization/app-i18n-runtime.d.ts.map +1 -0
- package/dist/cjs/internationalization/app-i18n-runtime.js +126 -0
- package/dist/cjs/internationalization/app-i18n-runtime.js.map +1 -0
- package/dist/cjs/internationalization/project.d.ts.map +1 -1
- package/dist/cjs/internationalization/project.js +6 -0
- package/dist/cjs/internationalization/project.js.map +1 -1
- package/dist/cjs/page-revalidate/project-plugin.d.ts.map +1 -1
- package/dist/cjs/page-revalidate/project-plugin.js +5 -1
- package/dist/cjs/page-revalidate/project-plugin.js.map +1 -1
- package/dist/cjs/rich-text-editor/content-support-source.d.ts +2 -1
- package/dist/cjs/rich-text-editor/content-support-source.d.ts.map +1 -1
- package/dist/cjs/rich-text-editor/content-support-source.js +9 -2
- package/dist/cjs/rich-text-editor/content-support-source.js.map +1 -1
- package/dist/cjs/tsconfig.tsbuildinfo +1 -1
- package/dist/esm/forum/files-route.d.ts +26 -0
- package/dist/esm/forum/files-route.d.ts.map +1 -0
- package/dist/esm/forum/files-route.js +38 -0
- package/dist/esm/forum/files-route.js.map +1 -0
- package/dist/esm/forum/markdown-source.d.ts +17 -0
- package/dist/esm/forum/markdown-source.d.ts.map +1 -0
- package/dist/esm/forum/markdown-source.js +19 -0
- package/dist/esm/forum/markdown-source.js.map +1 -0
- package/dist/esm/forum/notifications-route.d.ts +17 -0
- package/dist/esm/forum/notifications-route.d.ts.map +1 -0
- package/dist/esm/forum/notifications-route.js +26 -0
- package/dist/esm/forum/notifications-route.js.map +1 -0
- package/dist/esm/forum/page-data-source.d.ts +58 -0
- package/dist/esm/forum/page-data-source.d.ts.map +1 -0
- package/dist/esm/forum/page-data-source.js +164 -0
- package/dist/esm/forum/page-data-source.js.map +1 -0
- package/dist/esm/forum/page-rewrite.d.ts +50 -0
- package/dist/esm/forum/page-rewrite.d.ts.map +1 -0
- package/dist/esm/forum/page-rewrite.js +446 -0
- package/dist/esm/forum/page-rewrite.js.map +1 -0
- package/dist/esm/forum/project-plugin.d.ts +34 -0
- package/dist/esm/forum/project-plugin.d.ts.map +1 -0
- package/dist/esm/forum/project-plugin.js +269 -0
- package/dist/esm/forum/project-plugin.js.map +1 -0
- package/dist/esm/forum/replies-route.d.ts +21 -0
- package/dist/esm/forum/replies-route.d.ts.map +1 -0
- package/dist/esm/forum/replies-route.js +29 -0
- package/dist/esm/forum/replies-route.js.map +1 -0
- package/dist/esm/forum/seo.d.ts +29 -0
- package/dist/esm/forum/seo.d.ts.map +1 -0
- package/dist/esm/forum/seo.js +56 -0
- package/dist/esm/forum/seo.js.map +1 -0
- package/dist/esm/forum/settings.d.ts +97 -0
- package/dist/esm/forum/settings.d.ts.map +1 -0
- package/dist/esm/forum/settings.js +158 -0
- package/dist/esm/forum/settings.js.map +1 -0
- package/dist/esm/index.d.ts +1 -0
- package/dist/esm/index.d.ts.map +1 -1
- package/dist/esm/index.js +6 -0
- package/dist/esm/index.js.map +1 -1
- package/dist/esm/instant-search/sections.d.ts.map +1 -1
- package/dist/esm/instant-search/sections.js +18 -0
- package/dist/esm/instant-search/sections.js.map +1 -1
- package/dist/esm/instant-search/settings.d.ts +1 -1
- package/dist/esm/instant-search/settings.d.ts.map +1 -1
- package/dist/esm/instant-search/settings.js +2 -0
- package/dist/esm/instant-search/settings.js.map +1 -1
- package/dist/esm/internationalization/app-i18n-runtime.d.ts +39 -0
- package/dist/esm/internationalization/app-i18n-runtime.d.ts.map +1 -0
- package/dist/esm/internationalization/app-i18n-runtime.js +120 -0
- package/dist/esm/internationalization/app-i18n-runtime.js.map +1 -0
- package/dist/esm/internationalization/project.d.ts.map +1 -1
- package/dist/esm/internationalization/project.js +6 -0
- package/dist/esm/internationalization/project.js.map +1 -1
- package/dist/esm/page-revalidate/project-plugin.d.ts.map +1 -1
- package/dist/esm/page-revalidate/project-plugin.js +5 -1
- package/dist/esm/page-revalidate/project-plugin.js.map +1 -1
- package/dist/esm/rich-text-editor/content-support-source.d.ts +2 -1
- package/dist/esm/rich-text-editor/content-support-source.d.ts.map +1 -1
- package/dist/esm/rich-text-editor/content-support-source.js +9 -2
- package/dist/esm/rich-text-editor/content-support-source.js.map +1 -1
- package/dist/esm/tsconfig.tsbuildinfo +1 -1
- package/package.json +19 -19
- package/src/forum/files-route.ts +133 -0
- package/src/forum/markdown-source.ts +177 -0
- package/src/forum/notifications-route.ts +87 -0
- package/src/forum/page-data-source.ts +810 -0
- package/src/forum/page-rewrite.ts +567 -0
- package/src/forum/project-plugin.ts +328 -0
- package/src/forum/replies-route.ts +91 -0
- package/src/forum/seo.ts +245 -0
- package/src/forum/settings.ts +261 -0
- package/src/index.ts +6 -0
- package/src/instant-search/sections.ts +20 -0
- package/src/instant-search/settings.ts +2 -0
- package/src/internationalization/app-i18n-runtime.ts +165 -0
- package/src/internationalization/project.ts +7 -0
- package/src/page-revalidate/project-plugin.ts +5 -1
- package/src/rich-text-editor/content-support-source.ts +14 -2
|
@@ -0,0 +1,789 @@
|
|
|
1
|
+
import { parse } from '@babel/parser'
|
|
2
|
+
import { FileType, ProjectPluginStructure, UIDLPluginRecord } from '@teleporthq/teleport-types'
|
|
3
|
+
import { NextForumProjectPlugin, FORUM_FILE_KEYS } from '../src/forum/project-plugin'
|
|
4
|
+
import { FORUM_MAIN_COMMUNITY_SLUG, resolveForumConfig } from '../src/forum/settings'
|
|
5
|
+
import { rewriteForumPage } from '../src/forum/page-rewrite'
|
|
6
|
+
import { resolveDbSearchSections } from '../src/instant-search/sections'
|
|
7
|
+
import { resolveInstantSearchConfig } from '../src/instant-search/settings'
|
|
8
|
+
|
|
9
|
+
const TELEPORT_DATA_SOURCE = {
|
|
10
|
+
id: 'ds-teleport',
|
|
11
|
+
name: 'Teleport',
|
|
12
|
+
type: 'teleport',
|
|
13
|
+
config: { tables: ['teleport_forum_topics', 'teleport_forum_communities'] },
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
/** A details page as the generator leaves it: `record` is the prop its row is read as. */
|
|
17
|
+
const pageSource = (name: string, record: string, states: string[]): string =>
|
|
18
|
+
`import React, { useState } from 'react'
|
|
19
|
+
import Head from 'next/head'
|
|
20
|
+
import ${record}Resource from '../../../utils/data-sources/teleport-teleport-forum-${record}-ds-telep'
|
|
21
|
+
|
|
22
|
+
const ${name} = (props) => {
|
|
23
|
+
${states.map((state) => ` ${state}`).join('\n')}
|
|
24
|
+
return (
|
|
25
|
+
<>
|
|
26
|
+
<Head>
|
|
27
|
+
<title>{props.${record}?.title}</title>
|
|
28
|
+
<meta name="robots" content="index, follow" />
|
|
29
|
+
</Head>
|
|
30
|
+
<div data-tq-forum-page="${record}">{props.${record}?.title}</div>
|
|
31
|
+
</>
|
|
32
|
+
)
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
export default ${name}
|
|
36
|
+
|
|
37
|
+
export async function getStaticProps(context) {
|
|
38
|
+
const { params = {} } = context
|
|
39
|
+
try {
|
|
40
|
+
const response = await ${record}Resource({ ...context?.params }, { as: 'visitor' })
|
|
41
|
+
if (!response?.data) {
|
|
42
|
+
return { notFound: true, revalidate: 60 }
|
|
43
|
+
}
|
|
44
|
+
return { props: { ...context?.params, ${record}: response.data, ...response?.meta }, revalidate: 60 }
|
|
45
|
+
} catch (error) {
|
|
46
|
+
console.log(error)
|
|
47
|
+
return { notFound: true, revalidate: 60 }
|
|
48
|
+
}
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
export async function getStaticPaths() {
|
|
52
|
+
return { paths: [], fallback: 'blocking' }
|
|
53
|
+
}
|
|
54
|
+
`
|
|
55
|
+
|
|
56
|
+
/**
|
|
57
|
+
* The same page when it asks for a sign-in, as the page-access guard leaves
|
|
58
|
+
* it: rendered per request, the guard first, its row still read as a visitor.
|
|
59
|
+
*/
|
|
60
|
+
const guarded = (page: string): string =>
|
|
61
|
+
page
|
|
62
|
+
.replace(
|
|
63
|
+
'export async function getStaticProps(context) {\n const { params = {} } = context',
|
|
64
|
+
'export async function getServerSideProps(context) {\n const __tqAccessDenied = await __tqPageAccess.guardPageAccess(context, { requiresAuth: true })\n if (__tqAccessDenied) return __tqAccessDenied\n const { params = {} } = context'
|
|
65
|
+
)
|
|
66
|
+
.replace(
|
|
67
|
+
"import Head from 'next/head'",
|
|
68
|
+
"import Head from 'next/head'\nimport * as __tqPageAccess from '../../../utils/auth/page-access'"
|
|
69
|
+
)
|
|
70
|
+
.replace(/\nexport async function getStaticPaths\(\) \{[^}]*\}\n\}\n/, '')
|
|
71
|
+
|
|
72
|
+
const TOPIC_PAGE = pageSource('Topic', 'topic', [
|
|
73
|
+
'const [viewer, setViewer] = useState({})',
|
|
74
|
+
'const [topicDetails, setTopicDetails] = useState({})',
|
|
75
|
+
'const [replies, setReplies] = useState([])',
|
|
76
|
+
'const [score, setScore] = useState(0)',
|
|
77
|
+
"const [replySort, setReplySort] = useState('best')",
|
|
78
|
+
'const [isSubmitting, setIsSubmitting] = useState(false)',
|
|
79
|
+
])
|
|
80
|
+
|
|
81
|
+
const COMMUNITY_PAGE = pageSource('Community', 'community', [
|
|
82
|
+
'const [viewer, setViewer] = useState({})',
|
|
83
|
+
'const [topics, setTopics] = useState([])',
|
|
84
|
+
'const [rules, setRules] = useState([])',
|
|
85
|
+
'const [moderators, setModerators] = useState([])',
|
|
86
|
+
'const [pendingJoinCount, setPendingJoinCount] = useState(0)',
|
|
87
|
+
'const [isSubmitting, setIsSubmitting] = useState(false)',
|
|
88
|
+
])
|
|
89
|
+
|
|
90
|
+
// The community's two other pages read the same table under a name of their own.
|
|
91
|
+
const MANAGE_PAGE = guarded(
|
|
92
|
+
pageSource('Manage', 'managed', [
|
|
93
|
+
'const [viewer, setViewer] = useState({})',
|
|
94
|
+
"const [rulesText, setRulesText] = useState('')",
|
|
95
|
+
'const [members, setMembers] = useState([])',
|
|
96
|
+
])
|
|
97
|
+
)
|
|
98
|
+
|
|
99
|
+
const NEW_TOPIC_PAGE = guarded(
|
|
100
|
+
pageSource('NewTopic', 'postingIn', [
|
|
101
|
+
'const [viewer, setViewer] = useState({})',
|
|
102
|
+
"const [bodyHtml, setBodyHtml] = useState('')",
|
|
103
|
+
])
|
|
104
|
+
)
|
|
105
|
+
|
|
106
|
+
/**
|
|
107
|
+
* The composer of a forum without communities, as the generator leaves it: a
|
|
108
|
+
* static page (`pages/forum/new.js`) behind the sign-in guard, with no row to read.
|
|
109
|
+
*/
|
|
110
|
+
const STATIC_COMPOSER_PAGE = `import React, { useState } from 'react'
|
|
111
|
+
import Head from 'next/head'
|
|
112
|
+
import * as __tqPageAccess from '../../utils/auth/page-access'
|
|
113
|
+
|
|
114
|
+
const NewTopic = (props) => {
|
|
115
|
+
const [viewer, setViewer] = useState({ signed_in: false, can_post: false })
|
|
116
|
+
const [bodyHtml, setBodyHtml] = useState('')
|
|
117
|
+
return (
|
|
118
|
+
<>
|
|
119
|
+
<Head>
|
|
120
|
+
<title>New topic</title>
|
|
121
|
+
<meta name="robots" content="index, follow" />
|
|
122
|
+
</Head>
|
|
123
|
+
<div data-tq-forum-page="new-topic">New topic</div>
|
|
124
|
+
</>
|
|
125
|
+
)
|
|
126
|
+
}
|
|
127
|
+
|
|
128
|
+
export default NewTopic
|
|
129
|
+
|
|
130
|
+
export async function getServerSideProps(context) {
|
|
131
|
+
const __tqAccessDenied = await __tqPageAccess.guardPageAccess(context, { requiresAuth: true })
|
|
132
|
+
if (__tqAccessDenied) return __tqAccessDenied
|
|
133
|
+
return { props: {} }
|
|
134
|
+
}
|
|
135
|
+
`
|
|
136
|
+
|
|
137
|
+
const routeValue = (navLink: string, tableName?: string, exposeAs?: string, column = 'slug') => ({
|
|
138
|
+
value: navLink.slice(1) || 'home',
|
|
139
|
+
pageOptions: {
|
|
140
|
+
navLink,
|
|
141
|
+
...(tableName
|
|
142
|
+
? {
|
|
143
|
+
dynamicRouteAttribute: column,
|
|
144
|
+
detailsPageInfo: {
|
|
145
|
+
dataSourceId: 'ds-teleport',
|
|
146
|
+
dataSourceName: 'Teleport',
|
|
147
|
+
dataSourceType: 'teleport',
|
|
148
|
+
tableName,
|
|
149
|
+
differentiatorColumn: column,
|
|
150
|
+
featureIdentifier: '',
|
|
151
|
+
},
|
|
152
|
+
initialPropsData: {
|
|
153
|
+
exposeAs: { name: exposeAs, valuePath: ['data'] },
|
|
154
|
+
resource: { id: 'r1' },
|
|
155
|
+
},
|
|
156
|
+
}
|
|
157
|
+
: {}),
|
|
158
|
+
},
|
|
159
|
+
})
|
|
160
|
+
|
|
161
|
+
/** `uidl.plugins.forum.pages` as the editor sends it: a details page by its static base. */
|
|
162
|
+
const FORUM_PAGES = {
|
|
163
|
+
home: '/forum',
|
|
164
|
+
topic: '/forum/t',
|
|
165
|
+
community: '/forum/c',
|
|
166
|
+
manage: '/forum/manage',
|
|
167
|
+
'new-topic': '/forum/new',
|
|
168
|
+
}
|
|
169
|
+
|
|
170
|
+
const pageRecord = (folder: string[], content: string) => ({
|
|
171
|
+
path: ['pages', ...folder],
|
|
172
|
+
files: [{ name: '[slug]', fileType: FileType.JS, content }],
|
|
173
|
+
})
|
|
174
|
+
|
|
175
|
+
// The three pages of a community: one table, three addresses.
|
|
176
|
+
const COMMUNITY_ROUTES = [
|
|
177
|
+
routeValue('/forum/c/[slug]', 'teleport_forum_communities', 'community'),
|
|
178
|
+
routeValue('/forum/manage/[slug]', 'teleport_forum_communities', 'managed'),
|
|
179
|
+
routeValue('/forum/new/[slug]', 'teleport_forum_communities', 'postingIn'),
|
|
180
|
+
]
|
|
181
|
+
const ROUTE_ORDERS = [
|
|
182
|
+
[0, 1, 2],
|
|
183
|
+
[0, 2, 1],
|
|
184
|
+
[1, 0, 2],
|
|
185
|
+
[1, 2, 0],
|
|
186
|
+
[2, 0, 1],
|
|
187
|
+
[2, 1, 0],
|
|
188
|
+
]
|
|
189
|
+
|
|
190
|
+
function makeStructure(options: {
|
|
191
|
+
record?: Partial<UIDLPluginRecord> | null
|
|
192
|
+
auth?: boolean
|
|
193
|
+
/** The order the community's three routes come in (`ROUTE_ORDERS`). */
|
|
194
|
+
order?: number[]
|
|
195
|
+
extraRoutes?: unknown[]
|
|
196
|
+
}): ProjectPluginStructure {
|
|
197
|
+
const files = new Map()
|
|
198
|
+
files.set('page-topic', pageRecord(['forum', 't'], TOPIC_PAGE))
|
|
199
|
+
files.set('page-community', pageRecord(['forum', 'c'], COMMUNITY_PAGE))
|
|
200
|
+
files.set('page-manage', pageRecord(['forum', 'manage'], MANAGE_PAGE))
|
|
201
|
+
files.set('page-new-topic', pageRecord(['forum', 'new'], NEW_TOPIC_PAGE))
|
|
202
|
+
const routes = [
|
|
203
|
+
routeValue('/'),
|
|
204
|
+
routeValue('/forum'),
|
|
205
|
+
...(options.extraRoutes ?? []),
|
|
206
|
+
routeValue('/forum/t/[slug]', 'teleport_forum_topics', 'topic'),
|
|
207
|
+
...(options.order ?? ROUTE_ORDERS[0]).map((index) => COMMUNITY_ROUTES[index]),
|
|
208
|
+
]
|
|
209
|
+
return {
|
|
210
|
+
uidl: {
|
|
211
|
+
name: 'test-project',
|
|
212
|
+
globals: { settings: { title: 'Test', language: 'en' }, assets: [] },
|
|
213
|
+
dataSources: { 'ds-teleport': TELEPORT_DATA_SOURCE },
|
|
214
|
+
...(options.auth ? { authentication: { enabled: true } } : {}),
|
|
215
|
+
...(options.record === null
|
|
216
|
+
? {}
|
|
217
|
+
: {
|
|
218
|
+
plugins: {
|
|
219
|
+
forum: {
|
|
220
|
+
settings: {},
|
|
221
|
+
pages: FORUM_PAGES,
|
|
222
|
+
...(options.record ?? {}),
|
|
223
|
+
},
|
|
224
|
+
},
|
|
225
|
+
}),
|
|
226
|
+
root: { name: 'App', stateDefinitions: { route: { type: 'string', values: routes } } },
|
|
227
|
+
},
|
|
228
|
+
files,
|
|
229
|
+
dependencies: {},
|
|
230
|
+
devDependencies: {},
|
|
231
|
+
template: { files: [], subFolders: [] },
|
|
232
|
+
} as unknown as ProjectPluginStructure
|
|
233
|
+
}
|
|
234
|
+
|
|
235
|
+
/** The pages of a forum without communities: the feed, a topic and the composer, a static page. */
|
|
236
|
+
const TOPICS_ONLY_PAGES = { home: '/forum', topic: '/forum/t', 'new-topic': '/forum/new' }
|
|
237
|
+
|
|
238
|
+
/** A project whose forum has no communities, as the editor exports it and the generator writes its pages. */
|
|
239
|
+
function makeTopicsOnlyStructure(record: Partial<UIDLPluginRecord> = {}): ProjectPluginStructure {
|
|
240
|
+
const structure = makeStructure({
|
|
241
|
+
record: { settings: { structure: 'topics' }, pages: TOPICS_ONLY_PAGES, ...record },
|
|
242
|
+
auth: true,
|
|
243
|
+
})
|
|
244
|
+
structure.files.clear()
|
|
245
|
+
structure.files.set('page-topic', pageRecord(['forum', 't'], TOPIC_PAGE))
|
|
246
|
+
structure.files.set('page-new-topic', {
|
|
247
|
+
path: ['pages', 'forum'],
|
|
248
|
+
files: [{ name: 'new', fileType: FileType.JS, content: STATIC_COMPOSER_PAGE }],
|
|
249
|
+
})
|
|
250
|
+
structure.uidl.root.stateDefinitions!.route.values = [
|
|
251
|
+
routeValue('/'),
|
|
252
|
+
routeValue('/forum'),
|
|
253
|
+
routeValue('/forum/t/[slug]', 'teleport_forum_topics', 'topic'),
|
|
254
|
+
routeValue('/forum/new'),
|
|
255
|
+
] as never
|
|
256
|
+
return structure
|
|
257
|
+
}
|
|
258
|
+
|
|
259
|
+
const parses = (source: string) => parse(source, { sourceType: 'module', plugins: ['jsx'] })
|
|
260
|
+
const fileContent = (structure: ProjectPluginStructure, key: string): string =>
|
|
261
|
+
structure.files.get(key)!.files[0].content as string
|
|
262
|
+
/** Every file of the project by its key, as the plugin left it. */
|
|
263
|
+
const generatedFiles = (structure: ProjectPluginStructure): Record<string, unknown> =>
|
|
264
|
+
Object.fromEntries(Array.from(structure.files.entries()))
|
|
265
|
+
|
|
266
|
+
describe('NextForumProjectPlugin', () => {
|
|
267
|
+
it('leaves a project without the plugin exactly as it was', async () => {
|
|
268
|
+
const structure = makeStructure({ record: null })
|
|
269
|
+
await new NextForumProjectPlugin().runAfter(structure)
|
|
270
|
+
|
|
271
|
+
expect(structure.files.get(FORUM_FILE_KEYS.pageData)).toBeUndefined()
|
|
272
|
+
expect(structure.files.get(FORUM_FILE_KEYS.repliesRoute)).toBeUndefined()
|
|
273
|
+
expect(fileContent(structure, 'page-topic')).toBe(TOPIC_PAGE)
|
|
274
|
+
expect(structure.dependencies.pg).toBeUndefined()
|
|
275
|
+
})
|
|
276
|
+
|
|
277
|
+
it('reads the settings clamped, the page routes and the data source', () => {
|
|
278
|
+
const structure = makeStructure({
|
|
279
|
+
record: {
|
|
280
|
+
settings: { votes: 'up', topicsPerDay: 9999, moderation: 'nonsense', reactions: true },
|
|
281
|
+
},
|
|
282
|
+
})
|
|
283
|
+
const config = resolveForumConfig(structure.uidl)!
|
|
284
|
+
expect(config.settings.votes).toBe('up')
|
|
285
|
+
expect(config.settings.topicsPerDay).toBe(200)
|
|
286
|
+
// An unknown value falls back on the default: every post shows at once.
|
|
287
|
+
expect(config.settings.moderation).toBe('none')
|
|
288
|
+
expect(config.settings.holdLinks).toBe(false)
|
|
289
|
+
expect(config.settings.reactions).toBe(true)
|
|
290
|
+
expect(config.pages).toEqual(FORUM_PAGES)
|
|
291
|
+
expect(config.routes).toEqual({
|
|
292
|
+
topic: { staticBase: '/forum/t', column: 'slug' },
|
|
293
|
+
community: { staticBase: '/forum/c', column: 'slug' },
|
|
294
|
+
manage: { staticBase: '/forum/manage', column: 'slug' },
|
|
295
|
+
newTopic: { staticBase: '/forum/new', column: 'slug' },
|
|
296
|
+
})
|
|
297
|
+
expect(config.dataSource).toEqual({ dataSourceId: 'ds-teleport', dataSourceType: 'teleport' })
|
|
298
|
+
})
|
|
299
|
+
|
|
300
|
+
it('resolves each page of a community by its own recorded base, whatever the routes’ order', async () => {
|
|
301
|
+
for (const order of ROUTE_ORDERS) {
|
|
302
|
+
const structure = makeStructure({ record: {}, order })
|
|
303
|
+
const { routes } = resolveForumConfig(structure.uidl)!
|
|
304
|
+
expect(routes.community).toEqual({ staticBase: '/forum/c', column: 'slug' })
|
|
305
|
+
expect(routes.manage).toEqual({ staticBase: '/forum/manage', column: 'slug' })
|
|
306
|
+
expect(routes.newTopic).toEqual({ staticBase: '/forum/new', column: 'slug' })
|
|
307
|
+
|
|
308
|
+
await new NextForumProjectPlugin().runAfter(structure)
|
|
309
|
+
const community = fileContent(structure, 'page-community')
|
|
310
|
+
expect(community).toContain('__tqForumData.loadCommunityPage({')
|
|
311
|
+
expect(community).not.toContain('loadCommunityGate')
|
|
312
|
+
expect(community).toContain('"community": data.community')
|
|
313
|
+
const manage = fileContent(structure, 'page-manage')
|
|
314
|
+
expect(manage).toContain("need: 'moderate'")
|
|
315
|
+
expect(manage).not.toContain('loadCommunityPage')
|
|
316
|
+
expect(manage).toContain('"managed": data.community')
|
|
317
|
+
const newTopic = fileContent(structure, 'page-new-topic')
|
|
318
|
+
expect(newTopic).toContain("need: 'post'")
|
|
319
|
+
expect(newTopic).toContain('"postingIn": data.community')
|
|
320
|
+
}
|
|
321
|
+
})
|
|
322
|
+
|
|
323
|
+
it('takes only the address column from the route at a page’s base', () => {
|
|
324
|
+
const config = (uidl: ProjectPluginStructure['uidl']) => resolveForumConfig(uidl)!
|
|
325
|
+
// A page of the same table under a base that merely starts alike is another page.
|
|
326
|
+
const lookalike = makeStructure({
|
|
327
|
+
record: {},
|
|
328
|
+
extraRoutes: [routeValue('/forum/c-old/[id]', 'teleport_forum_communities', 'old', 'id')],
|
|
329
|
+
})
|
|
330
|
+
expect(config(lookalike.uidl).routes.community).toEqual({
|
|
331
|
+
staticBase: '/forum/c',
|
|
332
|
+
column: 'slug',
|
|
333
|
+
})
|
|
334
|
+
// The column is the route's own.
|
|
335
|
+
const byId = makeStructure({ record: { pages: { ...FORUM_PAGES, manage: '/forum/c-old' } } })
|
|
336
|
+
byId.uidl.root.stateDefinitions!.route.values!.push(
|
|
337
|
+
routeValue('/forum/c-old/[id]', 'teleport_forum_communities', 'old', 'id') as never
|
|
338
|
+
)
|
|
339
|
+
expect(config(byId.uidl).routes.manage).toEqual({ staticBase: '/forum/c-old', column: 'id' })
|
|
340
|
+
// A recorded page with no route keeps its base; one that is not recorded is not served.
|
|
341
|
+
const bare = makeStructure({
|
|
342
|
+
record: { pages: { home: '/forum', topic: '/forum/topics/[slug]' } },
|
|
343
|
+
})
|
|
344
|
+
expect(config(bare.uidl).routes).toEqual({
|
|
345
|
+
topic: { staticBase: '/forum/topics', column: 'slug' },
|
|
346
|
+
community: null,
|
|
347
|
+
manage: null,
|
|
348
|
+
newTopic: null,
|
|
349
|
+
})
|
|
350
|
+
})
|
|
351
|
+
|
|
352
|
+
it('emits the renderer, the data module, the routes, the index pages and the driver', async () => {
|
|
353
|
+
const structure = makeStructure({ record: {}, auth: true })
|
|
354
|
+
await new NextForumProjectPlugin().runAfter(structure)
|
|
355
|
+
|
|
356
|
+
const markdown = structure.files.get(FORUM_FILE_KEYS.markdown)!
|
|
357
|
+
expect(markdown.path).toEqual(['utils'])
|
|
358
|
+
expect(markdown.files[0].name).toBe('forum-markdown')
|
|
359
|
+
const data = fileContent(structure, FORUM_FILE_KEYS.pageData)
|
|
360
|
+
expect(structure.files.get(FORUM_FILE_KEYS.pageData)!.path).toEqual(['utils'])
|
|
361
|
+
expect(data).toContain("require('./forum-markdown')")
|
|
362
|
+
expect(data).toContain('function loadTopicPage')
|
|
363
|
+
expect(data).toContain('function loadCommunityPage')
|
|
364
|
+
expect(data).toContain('function loadReplies')
|
|
365
|
+
expect(data).toContain('function readSession')
|
|
366
|
+
// The settings are baked, the session reader checks revocation on a project with sign-in.
|
|
367
|
+
expect(data).toContain('"moderation":"none"')
|
|
368
|
+
expect(data).toContain('session-revocation')
|
|
369
|
+
|
|
370
|
+
const replies = structure.files.get(FORUM_FILE_KEYS.repliesRoute)!
|
|
371
|
+
expect(replies.path).toEqual(['pages', 'api', 'plugins', 'forum'])
|
|
372
|
+
expect(replies.files[0].name).toBe('replies')
|
|
373
|
+
expect(replies.files[0].content).toContain('require("../../../../utils/forum-page-data")')
|
|
374
|
+
expect(replies.files[0].content).toContain('rateLimitRequest')
|
|
375
|
+
const notifications = structure.files.get(FORUM_FILE_KEYS.notificationsRoute)!
|
|
376
|
+
expect(notifications.files[0].name).toBe('notifications')
|
|
377
|
+
const files = structure.files.get(FORUM_FILE_KEYS.filesRoute)!
|
|
378
|
+
expect(files.path).toEqual(['pages', 'api', 'plugins', 'forum'])
|
|
379
|
+
expect(files.files[0].name).toBe('files')
|
|
380
|
+
expect(files.files[0].content).toContain('forum.filesStillShown')
|
|
381
|
+
expect(() => parses(files.files[0].content)).not.toThrow()
|
|
382
|
+
expect(structure.files.get(FORUM_FILE_KEYS.sitemap)!.files[0].name).toBe('sitemap-forum.xml')
|
|
383
|
+
expect(structure.files.get(FORUM_FILE_KEYS.llms)!.files[0].name).toBe('llms-forum.txt')
|
|
384
|
+
expect(() => parses(fileContent(structure, FORUM_FILE_KEYS.sitemap))).not.toThrow()
|
|
385
|
+
expect(structure.dependencies.pg).toBeDefined()
|
|
386
|
+
expect(
|
|
387
|
+
structure.files.get('security-runtime') || structure.files.has('utils/security-runtime')
|
|
388
|
+
).toBeTruthy()
|
|
389
|
+
// The page-arrival wrapper the rewritten pages import.
|
|
390
|
+
expect(structure.files.get(FORUM_FILE_KEYS.pageArrival)!.files[0].name).toBe('page-arrival')
|
|
391
|
+
})
|
|
392
|
+
|
|
393
|
+
it('skips the notifications route when the bell is off', async () => {
|
|
394
|
+
const structure = makeStructure({ record: { settings: { notifications: false } } })
|
|
395
|
+
await new NextForumProjectPlugin().runAfter(structure)
|
|
396
|
+
expect(structure.files.get(FORUM_FILE_KEYS.notificationsRoute)).toBeUndefined()
|
|
397
|
+
expect(structure.files.get(FORUM_FILE_KEYS.repliesRoute)).toBeDefined()
|
|
398
|
+
})
|
|
399
|
+
|
|
400
|
+
it('rewrites the topic and community pages to render per request from the module', async () => {
|
|
401
|
+
const structure = makeStructure({ record: {} })
|
|
402
|
+
await new NextForumProjectPlugin().runAfter(structure)
|
|
403
|
+
|
|
404
|
+
const topic = fileContent(structure, 'page-topic')
|
|
405
|
+
expect(() => parses(topic)).not.toThrow()
|
|
406
|
+
expect(topic).not.toContain('getStaticProps')
|
|
407
|
+
expect(topic).not.toContain('getStaticPaths')
|
|
408
|
+
expect(topic).not.toContain('topicResource')
|
|
409
|
+
expect(topic).toContain("import React, { useState } from 'react'")
|
|
410
|
+
expect(topic).toContain('import __tqForumData from "../../../utils/forum-page-data"')
|
|
411
|
+
expect(topic).toContain('export async function getServerSideProps(context)')
|
|
412
|
+
expect(topic).toContain('__tqForumData.readSession(req)')
|
|
413
|
+
expect(topic).toContain('__tqForumData.loadTopicPage({')
|
|
414
|
+
expect(topic).toContain('slug = one(params["slug"])')
|
|
415
|
+
expect(topic).toContain('notFound: true')
|
|
416
|
+
expect(topic).toContain(
|
|
417
|
+
"'private, no-store' : 'public, s-maxage=30, stale-while-revalidate=300'"
|
|
418
|
+
)
|
|
419
|
+
// The row keeps the prop the page binds, and every forum state starts from the module.
|
|
420
|
+
expect(topic).toContain('"topic": data.topic')
|
|
421
|
+
// The shaped topic is also the `topicDetails` state: what only the module derives.
|
|
422
|
+
expect(topic).toContain('topicDetails: data.topic')
|
|
423
|
+
expect(topic).toContain('useState(props.topicDetails !== undefined ? props.topicDetails : {})')
|
|
424
|
+
expect(topic).toContain('useState(props.viewer !== undefined ? props.viewer : {})')
|
|
425
|
+
expect(topic).toContain('useState(props.replies !== undefined ? props.replies : [])')
|
|
426
|
+
expect(topic).toContain('useState(props.score !== undefined ? props.score : 0)')
|
|
427
|
+
expect(topic).toContain("useState(props.replySort !== undefined ? props.replySort : 'best')")
|
|
428
|
+
expect(topic).toContain('useState(false)')
|
|
429
|
+
expect(topic).toContain('<meta name="robots" content="noindex" />')
|
|
430
|
+
expect(topic).toContain('application/ld+json')
|
|
431
|
+
expect(topic).toContain('export default remountOnPageArrival(Topic)')
|
|
432
|
+
expect(topic).toContain('import remountOnPageArrival from "../../../utils/page-arrival"')
|
|
433
|
+
|
|
434
|
+
const community = fileContent(structure, 'page-community')
|
|
435
|
+
expect(() => parses(community)).not.toThrow()
|
|
436
|
+
expect(community).toContain('__tqForumData.loadCommunityPage({')
|
|
437
|
+
expect(community).toContain('"community": data.community')
|
|
438
|
+
expect(community).toContain('useState(props.topics !== undefined ? props.topics : [])')
|
|
439
|
+
// The About card's lists and the moderator bar's counts are states of the page.
|
|
440
|
+
expect(community).toContain('useState(props.rules !== undefined ? props.rules : [])')
|
|
441
|
+
expect(community).toContain('useState(props.moderators !== undefined ? props.moderators : [])')
|
|
442
|
+
expect(community).toContain(
|
|
443
|
+
'useState(props.pendingJoinCount !== undefined ? props.pendingJoinCount : 0)'
|
|
444
|
+
)
|
|
445
|
+
expect(community).toContain('rules: data.rules')
|
|
446
|
+
expect(community).toContain('moderators: data.moderators')
|
|
447
|
+
expect(community).toContain('openReportCount: data.openReportCount')
|
|
448
|
+
// The list is the server's: the address says which order and which page, never how many a page holds.
|
|
449
|
+
expect(community).toContain('sortBy: data.sortBy')
|
|
450
|
+
expect(community).toContain('paging: data.paging')
|
|
451
|
+
expect(community).toMatch(
|
|
452
|
+
/loadCommunityPage\(\{\s*slug,\s*session,\s*sort: one\(query\.sort\),\s*page: /
|
|
453
|
+
)
|
|
454
|
+
expect(community).not.toContain('perPage')
|
|
455
|
+
expect(community.slice(community.indexOf('__tqForum: {'))).not.toContain('moderators')
|
|
456
|
+
})
|
|
457
|
+
|
|
458
|
+
it('serves Manage and New topic through the membership gate, never cached nor indexed', async () => {
|
|
459
|
+
const structure = makeStructure({ record: {}, auth: true })
|
|
460
|
+
await new NextForumProjectPlugin().runAfter(structure)
|
|
461
|
+
expect(fileContent(structure, FORUM_FILE_KEYS.pageData)).toContain(
|
|
462
|
+
'async function loadCommunityGate(args)'
|
|
463
|
+
)
|
|
464
|
+
|
|
465
|
+
const pages = [
|
|
466
|
+
{ key: 'page-manage', record: 'managed', need: 'moderate', component: 'Manage' },
|
|
467
|
+
{ key: 'page-new-topic', record: 'postingIn', need: 'post', component: 'NewTopic' },
|
|
468
|
+
]
|
|
469
|
+
for (const { key, record, need, component } of pages) {
|
|
470
|
+
const page = fileContent(structure, key)
|
|
471
|
+
expect(() => parses(page)).not.toThrow()
|
|
472
|
+
// The row is no longer read under the visitor's row policy.
|
|
473
|
+
expect(page).not.toContain(`${record}Resource`)
|
|
474
|
+
expect(page).not.toContain("as: 'visitor'")
|
|
475
|
+
const body = page.slice(page.indexOf('export async function getServerSideProps'))
|
|
476
|
+
// The sign-in guard stays in front of the gate.
|
|
477
|
+
expect(body.indexOf('__tqPageAccess.guardPageAccess')).toBeGreaterThan(0)
|
|
478
|
+
expect(body.indexOf('__tqPageAccess.guardPageAccess')).toBeLessThan(
|
|
479
|
+
body.indexOf('loadCommunityGate')
|
|
480
|
+
)
|
|
481
|
+
expect(body).toContain('__tqForumData.loadCommunityGate({')
|
|
482
|
+
expect(body).toContain(`need: '${need}'`)
|
|
483
|
+
expect(body).toContain('notFound: true')
|
|
484
|
+
expect(body).toContain("res.setHeader('Cache-Control', 'private, no-store')")
|
|
485
|
+
expect(body).not.toContain('s-maxage')
|
|
486
|
+
expect(body).not.toContain('pageUrl')
|
|
487
|
+
expect(body).toContain(`"${record}": data.community`)
|
|
488
|
+
expect(body).toContain('viewer: data.viewer')
|
|
489
|
+
expect(body).toMatch(/__tqForum: \{\s*seo: \{\s*noindex: true\s*\}\s*\}/)
|
|
490
|
+
expect(page).toContain('useState(props.viewer !== undefined ? props.viewer : {})')
|
|
491
|
+
expect(page).toContain('<meta name="robots" content="noindex" />')
|
|
492
|
+
expect(page).toContain(`export default remountOnPageArrival(${component})`)
|
|
493
|
+
}
|
|
494
|
+
|
|
495
|
+
// The Manage form's rules field reads its state once, when it mounts: the rules arrive with the page.
|
|
496
|
+
const manage = fileContent(structure, 'page-manage')
|
|
497
|
+
expect(manage).toContain('rulesText: data.community.rules_text')
|
|
498
|
+
expect(manage).toContain("useState(props.rulesText !== undefined ? props.rulesText : '')")
|
|
499
|
+
})
|
|
500
|
+
|
|
501
|
+
it('keeps the access guard of a protected page in front of the forum read', () => {
|
|
502
|
+
const result = rewriteForumPage(guarded(TOPIC_PAGE), {
|
|
503
|
+
kind: 'topic',
|
|
504
|
+
column: 'slug',
|
|
505
|
+
exposeAs: 'topic',
|
|
506
|
+
rootPrefix: '../../../',
|
|
507
|
+
dataModulePath: 'utils/forum-page-data',
|
|
508
|
+
pageArrivalPath: 'utils/page-arrival',
|
|
509
|
+
})!
|
|
510
|
+
expect(() => parses(result.content)).not.toThrow()
|
|
511
|
+
expect(result.content).toContain(
|
|
512
|
+
"import * as __tqPageAccess from '../../../utils/auth/page-access'"
|
|
513
|
+
)
|
|
514
|
+
const body = result.content.slice(
|
|
515
|
+
result.content.indexOf('export async function getServerSideProps')
|
|
516
|
+
)
|
|
517
|
+
expect(body.indexOf('__tqPageAccess.guardPageAccess')).toBeGreaterThan(0)
|
|
518
|
+
expect(body.indexOf('__tqPageAccess.guardPageAccess')).toBeLessThan(
|
|
519
|
+
body.indexOf('loadTopicPage')
|
|
520
|
+
)
|
|
521
|
+
expect(result.seededStates).toEqual(['viewer', 'topicDetails', 'replies', 'score', 'replySort'])
|
|
522
|
+
})
|
|
523
|
+
|
|
524
|
+
it('keeps the locale’s messages of a project with internationalization', () => {
|
|
525
|
+
const rewrite = {
|
|
526
|
+
kind: 'topic' as const,
|
|
527
|
+
column: 'slug',
|
|
528
|
+
exposeAs: 'topic',
|
|
529
|
+
rootPrefix: '../../../',
|
|
530
|
+
dataModulePath: 'utils/forum-page-data',
|
|
531
|
+
pageArrivalPath: 'utils/page-arrival',
|
|
532
|
+
}
|
|
533
|
+
// What the locale fetcher leaves in the page's getStaticProps.
|
|
534
|
+
const localizedPage = TOPIC_PAGE.replace(
|
|
535
|
+
'const { params = {} } = context',
|
|
536
|
+
"const messages = (await import('/locales/' + context.locale + '.json')).default\n const { params = {} } = context"
|
|
537
|
+
).replace(
|
|
538
|
+
'return { props: { ...context?.params,',
|
|
539
|
+
'return { props: { messages, ...context?.params,'
|
|
540
|
+
)
|
|
541
|
+
const localized = rewriteForumPage(localizedPage, rewrite)!
|
|
542
|
+
expect(() => parses(localized.content)).not.toThrow()
|
|
543
|
+
const body = localized.content.slice(
|
|
544
|
+
localized.content.indexOf('export async function getServerSideProps')
|
|
545
|
+
)
|
|
546
|
+
expect(body).toContain("(await import('/locales/' + context.locale + '.json')).default")
|
|
547
|
+
expect(body).toMatch(/props: \{\s*\.\.\.params,\s*messages,/)
|
|
548
|
+
// The messages are read only for a page that renders: never before a refusal.
|
|
549
|
+
expect(body.indexOf('/locales/')).toBeGreaterThan(body.indexOf('notFound: true'))
|
|
550
|
+
|
|
551
|
+
// A project without internationalization has no messages file to read.
|
|
552
|
+
const plain = rewriteForumPage(TOPIC_PAGE, rewrite)!
|
|
553
|
+
expect(plain.content).not.toContain('/locales/')
|
|
554
|
+
expect(plain.content).not.toMatch(/\bmessages\b/)
|
|
555
|
+
})
|
|
556
|
+
|
|
557
|
+
it('keeps what the module seeded: no copy of the record over a seeded state on mount', () => {
|
|
558
|
+
// What the workflows plugin adds to a details page: the record copied into its first object state.
|
|
559
|
+
const recordCopy = (statements: string[]): string =>
|
|
560
|
+
` useEffect(() => {\n if (props.topic) {\n${statements
|
|
561
|
+
.map((statement) => ` ${statement}`)
|
|
562
|
+
.join('\n')}\n }\n }, [])`
|
|
563
|
+
const rewrite = {
|
|
564
|
+
kind: 'topic' as const,
|
|
565
|
+
column: 'slug',
|
|
566
|
+
exposeAs: 'topic',
|
|
567
|
+
rootPrefix: '../../../',
|
|
568
|
+
dataModulePath: 'utils/forum-page-data',
|
|
569
|
+
pageArrivalPath: 'utils/page-arrival',
|
|
570
|
+
}
|
|
571
|
+
const copyOverViewer =
|
|
572
|
+
'setViewer(__normalizeAdminFormRow(props.topic, { signed_in: false, can_post: false }))'
|
|
573
|
+
const page = (statements: string[]): string =>
|
|
574
|
+
pageSource('Topic', 'topic', [
|
|
575
|
+
'const [viewer, setViewer] = useState({ signed_in: false, can_post: false })',
|
|
576
|
+
"const [draft, setDraft] = useState({ title: '' })",
|
|
577
|
+
recordCopy(statements).trimStart(),
|
|
578
|
+
]).replace('{ useState }', '{ useState, useEffect }')
|
|
579
|
+
|
|
580
|
+
// The only statement of the effect: the effect goes with it.
|
|
581
|
+
const alone = rewriteForumPage(page([copyOverViewer]), rewrite)!
|
|
582
|
+
expect(() => parses(alone.content)).not.toThrow()
|
|
583
|
+
expect(alone.content).toContain('useState(props.viewer !== undefined ? props.viewer : {')
|
|
584
|
+
expect(alone.content).not.toContain('setViewer(')
|
|
585
|
+
expect(alone.content).not.toContain('useEffect(')
|
|
586
|
+
|
|
587
|
+
// Beside a state the module does not seed: that copy and its effect stay.
|
|
588
|
+
const copyOverDraft = "setDraft(__normalizeAdminFormRow(props.topic, { title: '' }))"
|
|
589
|
+
const beside = rewriteForumPage(page([copyOverViewer, copyOverDraft]), rewrite)!
|
|
590
|
+
expect(() => parses(beside.content)).not.toThrow()
|
|
591
|
+
expect(beside.content).not.toContain('setViewer(')
|
|
592
|
+
expect(beside.content).toContain('setDraft(__normalizeAdminFormRow(props.topic')
|
|
593
|
+
expect(beside.content).toContain('useEffect(')
|
|
594
|
+
})
|
|
595
|
+
|
|
596
|
+
describe('a forum without communities', () => {
|
|
597
|
+
it('reads how the forum is organised: in communities unless the record says topics', () => {
|
|
598
|
+
const structureOf = (settings: Record<string, unknown>) =>
|
|
599
|
+
resolveForumConfig(makeStructure({ record: { settings: settings as never } }).uidl)!
|
|
600
|
+
.settings.structure
|
|
601
|
+
expect(structureOf({})).toBe('communities')
|
|
602
|
+
expect(structureOf({ structure: 'communities' })).toBe('communities')
|
|
603
|
+
expect(structureOf({ structure: 'topics' })).toBe('topics')
|
|
604
|
+
for (const junk of ['Topics', 'topics ', 'forum', '', 0, true, null, ['topics']]) {
|
|
605
|
+
expect(structureOf({ structure: junk })).toBe('communities')
|
|
606
|
+
}
|
|
607
|
+
expect(FORUM_MAIN_COMMUNITY_SLUG).toBe('main')
|
|
608
|
+
})
|
|
609
|
+
|
|
610
|
+
it('has the topic page and a static composer, and none of a community’s pages', () => {
|
|
611
|
+
const config = resolveForumConfig(makeTopicsOnlyStructure().uidl)!
|
|
612
|
+
expect(config.pages).toEqual(TOPICS_ONLY_PAGES)
|
|
613
|
+
// The composer is a static page: no details route, no fallback column.
|
|
614
|
+
expect(config.routes).toEqual({
|
|
615
|
+
topic: { staticBase: '/forum/t', column: 'slug' },
|
|
616
|
+
community: null,
|
|
617
|
+
manage: null,
|
|
618
|
+
newTopic: null,
|
|
619
|
+
})
|
|
620
|
+
expect(config.composerPath).toBe('/forum/new')
|
|
621
|
+
|
|
622
|
+
// A page of the other structure is not served, even when the record still names it.
|
|
623
|
+
const stale = resolveForumConfig(
|
|
624
|
+
makeTopicsOnlyStructure({
|
|
625
|
+
pages: { ...FORUM_PAGES, communities: '/forum/communities' },
|
|
626
|
+
}).uidl
|
|
627
|
+
)!
|
|
628
|
+
expect(stale.pages).toEqual(TOPICS_ONLY_PAGES)
|
|
629
|
+
expect(stale.routes.community).toBeNull()
|
|
630
|
+
expect(stale.routes.manage).toBeNull()
|
|
631
|
+
expect(stale.routes.newTopic).toBeNull()
|
|
632
|
+
// A forum with no composer page has none to serve.
|
|
633
|
+
const bare = resolveForumConfig(
|
|
634
|
+
makeTopicsOnlyStructure({ pages: { home: '/forum', topic: '/forum/t' } }).uidl
|
|
635
|
+
)!
|
|
636
|
+
expect(bare.composerPath).toBeNull()
|
|
637
|
+
|
|
638
|
+
// With communities the composer is a community's details page.
|
|
639
|
+
const communities = resolveForumConfig(makeStructure({ record: {} }).uidl)!
|
|
640
|
+
expect(communities.composerPath).toBeNull()
|
|
641
|
+
expect(communities.routes.newTopic).toEqual({ staticBase: '/forum/new', column: 'slug' })
|
|
642
|
+
})
|
|
643
|
+
|
|
644
|
+
it('serves the static composer through the gate of the main community, never cached nor indexed', async () => {
|
|
645
|
+
const structure = makeTopicsOnlyStructure()
|
|
646
|
+
await new NextForumProjectPlugin().runAfter(structure)
|
|
647
|
+
|
|
648
|
+
const composer = fileContent(structure, 'page-new-topic')
|
|
649
|
+
expect(() => parses(composer)).not.toThrow()
|
|
650
|
+
expect(composer).toContain('import __tqForumData from "../../utils/forum-page-data"')
|
|
651
|
+
const body = composer.slice(composer.indexOf('export async function getServerSideProps'))
|
|
652
|
+
expect(composer.match(/export async function getServerSideProps/g)).toHaveLength(1)
|
|
653
|
+
// The sign-in guard stays in front of the gate.
|
|
654
|
+
expect(body.indexOf('__tqPageAccess.guardPageAccess')).toBeGreaterThan(0)
|
|
655
|
+
expect(body.indexOf('__tqPageAccess.guardPageAccess')).toBeLessThan(
|
|
656
|
+
body.indexOf('loadCommunityGate')
|
|
657
|
+
)
|
|
658
|
+
// The community is the main one, whatever the address: the page has no route column.
|
|
659
|
+
expect(body).toContain('const slug = "main"')
|
|
660
|
+
expect(body).not.toContain('params["slug"]')
|
|
661
|
+
expect(body).toMatch(
|
|
662
|
+
/__tqForumData\.loadCommunityGate\(\{\s*slug,\s*session,\s*need: 'post'\s*\}\)/
|
|
663
|
+
)
|
|
664
|
+
expect(body).toContain('__tqForumData.readSession(req)')
|
|
665
|
+
expect(body).toContain('notFound: true')
|
|
666
|
+
expect(body).toContain("res.setHeader('Cache-Control', 'private, no-store')")
|
|
667
|
+
expect(body).not.toContain('s-maxage')
|
|
668
|
+
expect(body).not.toContain('pageUrl')
|
|
669
|
+
// No record prop: the viewer is all the page is given, beside the head's noindex.
|
|
670
|
+
expect(body).toContain('viewer: data.viewer')
|
|
671
|
+
expect(body).not.toContain('data.community')
|
|
672
|
+
expect(body).toMatch(/__tqForum: \{\s*seo: \{\s*noindex: true\s*\}\s*\}/)
|
|
673
|
+
expect(composer).toContain(
|
|
674
|
+
'useState(props.viewer !== undefined ? props.viewer : {\n signed_in: false,\n can_post: false\n })'
|
|
675
|
+
)
|
|
676
|
+
expect(composer).toContain("useState('')")
|
|
677
|
+
expect(composer).toContain('<meta name="robots" content="noindex" />')
|
|
678
|
+
expect(composer).toContain('export default remountOnPageArrival(NewTopic)')
|
|
679
|
+
expect(composer).toContain('import remountOnPageArrival from "../../utils/page-arrival"')
|
|
680
|
+
expect(structure.files.get(FORUM_FILE_KEYS.pageArrival)!.files[0].name).toBe('page-arrival')
|
|
681
|
+
|
|
682
|
+
// The topic page is served as in any forum.
|
|
683
|
+
const topic = fileContent(structure, 'page-topic')
|
|
684
|
+
expect(() => parses(topic)).not.toThrow()
|
|
685
|
+
expect(topic).toContain('__tqForumData.loadTopicPage({')
|
|
686
|
+
expect(topic).toContain('slug = one(params["slug"])')
|
|
687
|
+
})
|
|
688
|
+
|
|
689
|
+
it('finds the composer wherever its address puts the file', async () => {
|
|
690
|
+
const at = async (route: string, path: string[], name: string) => {
|
|
691
|
+
const structure = makeTopicsOnlyStructure({
|
|
692
|
+
pages: { ...TOPICS_ONLY_PAGES, 'new-topic': route },
|
|
693
|
+
})
|
|
694
|
+
structure.files.set('page-new-topic', {
|
|
695
|
+
path,
|
|
696
|
+
files: [{ name, fileType: FileType.JS, content: STATIC_COMPOSER_PAGE }],
|
|
697
|
+
})
|
|
698
|
+
await new NextForumProjectPlugin().runAfter(structure)
|
|
699
|
+
return fileContent(structure, 'page-new-topic')
|
|
700
|
+
}
|
|
701
|
+
expect(await at('/ask', ['pages'], 'ask')).toContain(
|
|
702
|
+
'import __tqForumData from "../utils/forum-page-data"'
|
|
703
|
+
)
|
|
704
|
+
expect(await at('/talk/topics/new', ['pages', 'talk', 'topics'], 'new')).toContain(
|
|
705
|
+
'import __tqForumData from "../../../utils/forum-page-data"'
|
|
706
|
+
)
|
|
707
|
+
// A file at another address is not the composer.
|
|
708
|
+
expect(await at('/forum/new', ['pages', 'forum'], 'compose')).toBe(STATIC_COMPOSER_PAGE)
|
|
709
|
+
})
|
|
710
|
+
|
|
711
|
+
it('rewrites no page it lacks and names no community in its index pages', async () => {
|
|
712
|
+
// The files of a forum with communities, in a project whose record says topics.
|
|
713
|
+
const structure = makeStructure({ record: { settings: { structure: 'topics' } } })
|
|
714
|
+
await new NextForumProjectPlugin().runAfter(structure)
|
|
715
|
+
expect(fileContent(structure, 'page-community')).toBe(COMMUNITY_PAGE)
|
|
716
|
+
expect(fileContent(structure, 'page-manage')).toBe(MANAGE_PAGE)
|
|
717
|
+
// A details page under the composer's address is not the static composer.
|
|
718
|
+
expect(fileContent(structure, 'page-new-topic')).toBe(NEW_TOPIC_PAGE)
|
|
719
|
+
expect(fileContent(structure, 'page-topic')).toContain('__tqForumData.loadTopicPage({')
|
|
720
|
+
|
|
721
|
+
for (const key of [FORUM_FILE_KEYS.sitemap, FORUM_FILE_KEYS.llms]) {
|
|
722
|
+
const source = fileContent(structure, key)
|
|
723
|
+
expect(() => parses(source)).not.toThrow()
|
|
724
|
+
expect(source).toContain('var TOPIC_BASE = "/forum/t"')
|
|
725
|
+
expect(source).not.toMatch(/communit/i)
|
|
726
|
+
expect(source).not.toContain('/forum/c')
|
|
727
|
+
}
|
|
728
|
+
// The data module is the same one, told its structure, with no community page to link to.
|
|
729
|
+
const data = fileContent(structure, FORUM_FILE_KEYS.pageData)
|
|
730
|
+
expect(data).toContain('"structure":"topics"')
|
|
731
|
+
expect(data).toContain('var URLS = {"topic":"/forum/t","community":""};')
|
|
732
|
+
expect(data).not.toContain('/forum/c')
|
|
733
|
+
})
|
|
734
|
+
})
|
|
735
|
+
|
|
736
|
+
it('generates a forum with communities the same whether or not the record names its structure', async () => {
|
|
737
|
+
const generate = async (settings: Record<string, unknown>) => {
|
|
738
|
+
const structure = makeStructure({ record: { settings: settings as never }, auth: true })
|
|
739
|
+
await new NextForumProjectPlugin().runAfter(structure)
|
|
740
|
+
return { files: generatedFiles(structure), dependencies: structure.dependencies }
|
|
741
|
+
}
|
|
742
|
+
const unnamed = await generate({})
|
|
743
|
+
expect(await generate({ structure: 'communities' })).toEqual(unnamed)
|
|
744
|
+
expect(await generate({ structure: 'nonsense' })).toEqual(unnamed)
|
|
745
|
+
|
|
746
|
+
// Its index pages list the communities; the module links a topic to its community's page.
|
|
747
|
+
const project = makeStructure({ record: {} })
|
|
748
|
+
await new NextForumProjectPlugin().runAfter(project)
|
|
749
|
+
const sitemap = fileContent(project, FORUM_FILE_KEYS.sitemap)
|
|
750
|
+
expect(sitemap).toContain('var COMMUNITY_BASE = "/forum/c"')
|
|
751
|
+
expect(sitemap).toContain('index.communities.forEach(')
|
|
752
|
+
const llms = fileContent(project, FORUM_FILE_KEYS.llms)
|
|
753
|
+
expect(llms).toContain("var lines = ['# Forum', '', '## Communities', '']")
|
|
754
|
+
expect(llms).toContain('index.communities.forEach(')
|
|
755
|
+
const data = fileContent(project, FORUM_FILE_KEYS.pageData)
|
|
756
|
+
expect(data).toContain('"structure":"communities"')
|
|
757
|
+
expect(data).toContain('var URLS = {"topic":"/forum/t","community":"/forum/c"};')
|
|
758
|
+
})
|
|
759
|
+
|
|
760
|
+
it('offers the topics to Instant search only while the forum is on', () => {
|
|
761
|
+
const on = makeStructure({ record: {} }).uidl
|
|
762
|
+
const config = resolveInstantSearchConfig({
|
|
763
|
+
...on,
|
|
764
|
+
plugins: { ...on.plugins, instantSearch: { settings: {} } },
|
|
765
|
+
} as never)!
|
|
766
|
+
const sections = resolveDbSearchSections(
|
|
767
|
+
{ ...on, plugins: { ...on.plugins, instantSearch: { settings: {} } } } as never,
|
|
768
|
+
config
|
|
769
|
+
)
|
|
770
|
+
expect(sections.map((section) => section.key)).toEqual(['topics'])
|
|
771
|
+
expect(sections[0]).toMatchObject({
|
|
772
|
+
tableName: 'teleport_forum_topics',
|
|
773
|
+
route: { staticBase: '/forum/t', column: 'slug' },
|
|
774
|
+
searchColumns: ['title', 'body_text', 'tags'],
|
|
775
|
+
activeColumn: null,
|
|
776
|
+
})
|
|
777
|
+
const off = makeStructure({ record: null }).uidl
|
|
778
|
+
const offConfig = resolveInstantSearchConfig({
|
|
779
|
+
...off,
|
|
780
|
+
plugins: { instantSearch: { settings: {} } },
|
|
781
|
+
} as never)!
|
|
782
|
+
expect(
|
|
783
|
+
resolveDbSearchSections(
|
|
784
|
+
{ ...off, plugins: { instantSearch: { settings: {} } } } as never,
|
|
785
|
+
offConfig
|
|
786
|
+
)
|
|
787
|
+
).toEqual([])
|
|
788
|
+
})
|
|
789
|
+
})
|