@@ -12,7 +12,7 @@ const momentTZ = require('moment-timezone');
12
12
const cheerio = require ( 'cheerio' ) ;
13
13
const fetch = require ( 'node-fetch' ) ;
14
14
const readingTime = require ( 'reading-time' ) ;
15
-
15
+ const loadSiteContext = require ( '../../../site-context/load' ) ;
16
16
const newrelic = require ( '../../../newrelic' ) ;
17
17
const defaults = require ( '../../defaults' ) ;
18
18
const validateRest = require ( '../../utils/validate-rest' ) ;
@@ -41,6 +41,27 @@ const { validateYoutubePlaylistId, validateYoutubeChannelId, validateYoutubeUser
41
41
const { MOST_POPULAR_CONTENT_API_URL } = require ( '../../../env' ) ;
42
42
const optimizeForRss = require ( '../../utils/optimize-for-rss' ) ;
43
43
44
+ /**
45
+ * @typedef {import('../../../routes/graphql').GraphQLServerContext } GraphQLServerContext
46
+ * @typedef {import("../../../site-context") } SiteContext
47
+ */
48
+
49
+ /**
50
+ *
51
+ * @param {string } siteId
52
+ * @param {GraphQLServerContext } context
53
+ * @returns {SiteContext }
54
+ */
55
+ const loadSiteContextFrom = async ( siteId , context ) => {
56
+ const site = await loadSiteContext ( {
57
+ basedb : context . basedb ,
58
+ siteId,
59
+ tenant : context . tenant ,
60
+ enableCache : true ,
61
+ } ) ;
62
+ return site ;
63
+ } ;
64
+
44
65
const retrieveYoutubePlaylistId = async ( { youtube } ) => {
45
66
const playlistId = get ( youtube , 'playlistId' ) ;
46
67
if ( playlistId ) return playlistId ;
@@ -320,15 +341,24 @@ module.exports = {
320
341
return links ;
321
342
} ,
322
343
344
+ /**
345
+ *
346
+ * @param {object } content
347
+ * @param {object } variables
348
+ * @param {GraphQLServerContext } ctx
349
+ * @returns
350
+ */
323
351
siteContext : async ( content , { input } , ctx ) => {
324
- const { enableLinkUrl } = input ;
325
- const { site, load, basedb } = ctx ;
352
+ const { enableLinkUrl, siteId } = input ;
353
+ const { load, basedb } = ctx ;
354
+ const site = siteId ? await loadSiteContextFrom ( siteId , ctx ) : ctx . site ;
326
355
if ( ! site . exists ( ) ) throw new UserInputError ( 'A website context must be set to generate `Content.siteContext` fields.' ) ;
356
+ const context = { ...ctx , site } ;
327
357
328
358
return {
329
- path : ( ) => canonicalPathFor ( content , ctx , { enableLinkUrl } ) ,
359
+ path : ( ) => canonicalPathFor ( content , context , { enableLinkUrl } ) ,
330
360
url : async ( ) => {
331
- const path = await canonicalPathFor ( content , ctx , { enableLinkUrl } ) ;
361
+ const path = await canonicalPathFor ( content , context , { enableLinkUrl } ) ;
332
362
if ( / ^ h t t p / i. test ( path ) ) return path ;
333
363
return `${ site . get ( 'origin' ) } ${ path } ` ;
334
364
} ,
0 commit comments