Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add google news #1

Merged
merged 28 commits into from
Nov 24, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
28 commits
Select commit Hold shift + click to select a range
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions admin/src/components/List/Collection/Row.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,9 @@ const CustomRow = ({ openModal, entry }) => {

return (
<Tr key={entry.id}>
<Td>
<Typography variant="omega" textColor="neutral800">{entry.title}</Typography>
</Td>
<Td>
<Typography variant="omega" textColor="neutral800">{contentTypes[entry.name] && contentTypes[entry.name].displayName}</Typography>
</Td>
Expand Down
4 changes: 4 additions & 0 deletions admin/src/components/List/Collection/index.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ const ListComponent = (props) => {
items.map((item, key) => {
item.get('languages').map((langItem, langKey) => {
const formattedItem = {};
formattedItem.title = langItem.get('title');
formattedItem.name = key;
formattedItem.langcode = langKey;
formattedItem.pattern = langItem.get('pattern');
Expand All @@ -51,6 +52,9 @@ const ListComponent = (props) => {
<Table colCount={4} rowCount={formattedItems.length + 1} footer={<TFooter onClick={() => openModal()} icon={<Plus />}>{formatMessage({ id: 'sitemap.Button.AddURLBundle', defaultMessage: 'Add another URL bundle' })}</TFooter>}>
<Thead>
<Tr>
<Th>
<Typography variant="sigma">Name</Typography>
</Th>
<Th>
<Typography variant="sigma">Type</Typography>
</Th>
Expand Down
64 changes: 64 additions & 0 deletions admin/src/components/ModalForm/Collection/index.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import {
Checkbox,
Combobox,
ComboboxOption,
TextInput,
} from '@strapi/design-system';

import SelectContentTypes from '../../SelectContentTypes';
Expand Down Expand Up @@ -73,6 +74,16 @@ const CollectionForm = (props) => {
<Grid gap={6}>
<GridItem col={6} s={12}>
<Grid gap={4}>
<GridItem col={12}>
<TextInput
label={formatMessage({ id: 'sitemap.Settings.Field.Title.Label', defaultMessage: 'Title' })}
name="title"
required
value={modifiedState.getIn([uid, 'languages', langcode, 'title'], '')}
hint={formatMessage({ id: 'sitemap.Settings.Field.Title.Description', defaultMessage: 'Title of the bundle.' })}
onChange={(e) => onChange(uid, langcode, 'title', e.target.value)}
/>
</GridItem>
<GridItem col={12}>
<SelectContentTypes
contentTypes={contentTypes}
Expand All @@ -89,6 +100,36 @@ const CollectionForm = (props) => {
value={langcode}
/>
</GridItem>
<GridItem col={12}>

<TextInput
label={formatMessage({ id: 'sitemap.Settings.Field.Filter.Label', defaultMessage: 'Additional filters' })}
name="filter"
value={modifiedState.getIn([uid, 'languages', langcode, 'filter'], '')}
hint={formatMessage({ id: 'sitemap.Settings.Field.Filter.Description', defaultMessage: 'Add additional filters to the content type.' })}
onChange={(e) => onChange(uid, langcode, 'filter', e.target.value)}
/>
</GridItem>

<GridItem col={12}>
<TextInput
label={formatMessage({ id: 'sitemap.Settings.Field.MaxAge.Label', defaultMessage: 'Max age of items (ms)' })}
name="maxAge"
value={modifiedState.getIn([uid, 'languages', langcode, 'maxAge'], '')}
hint={formatMessage({ id: 'sitemap.Settings.Field.MaxAge.Description', defaultMessage: 'Define the maximum age of an item in milliseconds.' })}
onChange={(e) => onChange(uid, langcode, 'maxAge', e.target.value)}
/>
</GridItem>

<GridItem col={12}>
<TextInput
label={formatMessage({ id: 'sitemap.Settings.Field.MinAge.Label', defaultMessage: 'Min age of items (ms)' })}
name="minAge"
value={modifiedState.getIn([uid, 'languages', langcode, 'minAge'], '')}
hint={formatMessage({ id: 'sitemap.Settings.Field.MinAge.Description', defaultMessage: 'Define the minimum age of an item in milliseconds.' })}
onChange={(e) => onChange(uid, langcode, 'minAge', e.target.value)}
/>
</GridItem>
</Grid>
</GridItem>
<GridItem col={6} s={12}>
Expand Down Expand Up @@ -166,6 +207,29 @@ const CollectionForm = (props) => {
{formatMessage({ id: 'sitemap.Settings.Field.IncludeLastmod.Label', defaultMessage: 'Lastmod' })}
</Checkbox>
</GridItem>

<GridItem col={12}>
<Checkbox
onValueChange={(cbValue) => {
onChange(uid, langcode, 'addNews', cbValue);
}}
value={modifiedState.getIn([uid, 'languages', langcode, 'addNews'], false)}
disabled={!uid || (contentTypes[uid].locales && !langcode)}
hint={formatMessage({ id: 'sitemap.Settings.Field.AddNews.Description', defaultMessage: 'Adds a News object to all the URLs of this type.' })}
>
{formatMessage({ id: 'sitemap.Settings.Field.AddNews.Label', defaultMessage: 'Add News' })}
</Checkbox>
</GridItem>

<GridItem col={12}>
<TextInput
label={formatMessage({ id: 'sitemap.Settings.Field.NewsTitleField.Label', defaultMessage: 'Title field for News' })}
name="newsTitleField"
value={modifiedState.getIn([uid, 'languages', langcode, 'newsTitleField'], '')}
hint={formatMessage({ id: 'sitemap.Settings.Field.NewsTitleField.Description', defaultMessage: 'Field to use to set the field title in news.publication.' })}
onChange={(e) => onChange(uid, langcode, 'newsTitleField', e.target.value)}
/>
</GridItem>
</Grid>
</GridItem>
</Grid>
Expand Down
10 changes: 10 additions & 0 deletions admin/src/tabs/Settings/index.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,16 @@ const Settings = () => {
onChange={(e) => dispatch(onChangeSettings('hostname', e.target.value))}
/>
</GridItem>
<GridItem col={6} s={12}>
<TextInput
placeholder="My Website"
label={formatMessage({ id: 'sitemap.Settings.Field.WebsiteName.Label', defaultMessage: 'Website name' })}
name="websiteName"
value={settings.get('websiteName')}
hint={formatMessage({ id: 'sitemap.Settings.Field.WebsiteName.Description', defaultMessage: 'The name of your website' })}
onChange={(e) => dispatch(onChangeSettings('websiteName', e.target.value))}
/>
</GridItem>
{languages.length > 1 && (
<GridItem col={12} s={12}>
<Typography variant="pi" fontWeight="bold">
Expand Down
4 changes: 4 additions & 0 deletions admin/src/translations/en.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@
"Settings.SettingsTitle": "Settings",
"Settings.Field.Hostname.Label": "Hostname",
"Settings.Field.Hostname.Description": "The URL of your website",
"Settings.Field.WebsiteName.Label": "Website name",
"Settings.Field.WebsiteName.Description": "The name of your website",
"Settings.Field.HostnameOverrides.Label": "Hostname overrides",
"Settings.Field.HostnameOverrides.Button": "Configure",
"Settings.Field.HostnameOverrides.Description": "Specify hostname per language",
Expand All @@ -34,6 +36,8 @@
"Settings.Field.Changefreq.Description": "The changefreq of the pages.",
"Settings.Field.IncludeLastmod.Label": "Lastmod",
"Settings.Field.IncludeLastmod.Description": "Adds a <lastmod> tag to all the URLs of this type.",
"Settings.Field.AddNews.Label": "Add News",
"Settings.Field.AddNews.Description": "Adds a News object to all the URLs of this type.",
"Settings.Field.Pattern.Label": "Pattern",
"Settings.Field.Pattern.DescriptionPart1": "Create a dynamic URL pattern",
"Settings.Field.Pattern.DescriptionPart2": "using",
Expand Down
26 changes: 21 additions & 5 deletions server/services/core.js
Original file line number Diff line number Diff line change
Expand Up @@ -88,13 +88,29 @@ const getSitemapPageData = async (config, page, contentType) => {
const url = `${hostnameOverride}${path}`;

const pageData = {
lastmod: page.updatedAt,
url: url,
links: await getLanguageLinks(config, page, contentType, url),
changefreq: config.contentTypes[contentType]['languages'][locale].changefreq || 'monthly',
priority: parseFloat(config.contentTypes[contentType]['languages'][locale].priority) || 0.5,
url,
};

if (config.contentTypes[contentType]['languages'][locale].addNews === true) {
const titleField = config.contentTypes[contentType]['languages'][locale].titleField || 'title';
pageData.news = {
publication: {
name: config.websiteName,
language: page.locale,
},
title: page[titleField],
publication_date: page.publishedAt || page.createdAt,
};
} else {
pageData = {
...pageData,
lastmod: page.updatedAt,
links: await getLanguageLinks(config, page, contentType, url),
changefreq: config.contentTypes[contentType]['languages'][locale].changefreq || 'monthly',
priority: parseFloat(config.contentTypes[contentType]['languages'][locale].priority) || 0.5,
}
}

if (config.contentTypes[contentType]['languages'][locale].includeLastmod === false) {
delete pageData.lastmod;
}
Expand Down
39 changes: 37 additions & 2 deletions server/services/query.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,12 +33,16 @@ const getFieldsFromConfig = (contentType, topLevel = false, isLocalized = false,
});
}



if (topLevel) {
if (isLocalized) {
fields.push('locale');
}

fields.push('updatedAt');
fields.push('createdAt');
fields.push('publishedAt');
}

// Remove duplicates
Expand Down Expand Up @@ -87,8 +91,36 @@ const getPages = async (config, contentType, ids) => {

const relations = getRelationsFromConfig(config.contentTypes[contentType]);
const fields = getFieldsFromConfig(config.contentTypes[contentType], true, isLocalized);
let filters = {}

for (const language in config.contentTypes[contentType].languages) {
const newsTitleField = config.contentTypes[contentType].languages[language].newsTitleField;
if (newsTitleField) fields.push(newsTitleField);

if (config.contentTypes[contentType].languages[language]['filter']) {
const filter = config.contentTypes[contentType].languages[language]['filter'];
filters = { ...filters, ...filter.value }
}

if (config.contentTypes[contentType].languages[language]['maxAge']) {
const maxAge = config.contentTypes[contentType].languages[language]['maxAge'];
const date = new Date(new Date().getTime() - (maxAge));
filters = { ...filters, publishedAt: { $gte: date } }
}

const pages = await noLimit(strapi, contentType, {
if (config.contentTypes[contentType].languages[language]['minAge']) {
const minAge = config.contentTypes[contentType].languages[language]['minAge'];
const date = new Date(new Date().getTime() - (minAge));
if(filters.publishedAt) {
filters.publishedAt = { ...filters.publishedAt, $lte: date }
} else {
filters = { ...filters, publishedAt: { $lte: date } }
}
}
}


const params = {
filters: {
$or: [
{
Expand All @@ -105,6 +137,7 @@ const getPages = async (config, contentType, ids) => {
id: ids ? {
$in: ids,
} : {},
...filters,
},
locale: 'all',
fields,
Expand All @@ -117,7 +150,9 @@ const getPages = async (config, contentType, ids) => {
},
orderBy: 'id',
publicationState: excludeDrafts ? 'live' : 'preview',
});
};

const pages = await noLimit(strapi, contentType, params);

return pages;
};
Expand Down
1 change: 1 addition & 0 deletions server/services/settings.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ const createDefaultConfig = async () => {
hostname_overrides: {},
contentTypes: Map({}),
customEntries: Map({}),
websiteName: '',
};

await pluginStore.set({ key: 'settings', value });
Expand Down