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

Fix new author creation error on socials #3578

Merged
merged 5 commits into from
May 9, 2024
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
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
27 changes: 24 additions & 3 deletions db/model/Gdoc/GdocAuthor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import {
DEFAULT_GDOC_FEATURED_IMAGE,
OwidGdocBaseInterface,
excludeNullish,
defaults,
} from "@ourworldindata/utils"
import { GdocBase } from "./GdocBase.js"
import { htmlToEnrichedTextBlock } from "./htmlToEnriched.js"
Expand All @@ -26,9 +27,29 @@ export class GdocAuthor extends GdocBase implements OwidGdocAuthorInterface {
}

static create(obj: OwidGdocBaseInterface): GdocAuthor {
const gdoc = new GdocAuthor()
Object.assign(gdoc, obj)
return gdoc
const author = new GdocAuthor()

// We need to prevent obj methods from overriding GdocAuthor methods.
// This happens when createGdocAndInsertIntoDb() passes a GdocBase
// instance to loadGdocFromGdocBase(), instead of a simple object. In
// this case, simply assigning obj to gdoc would override GdocAuthor
// methods with GdocBase methods, in particular the
// _enrichedSubclassContent. When creating a new author,
// _enrichedSubclassContent would run from the base GdocBase class
// instead of the GdocAuthor subclass, and the socials block would not
// be shaped as an ArchieML block, thus throwing an error.
// Object.assign(gdoc, omitBy(obj, isFunction))

// However this approach still lets the parent class override properties
// of the subclass, which can be conceptually surprising.

// So instead, we choose to only override the properties and methods
// that are not defined in the subclass. Note that methods are also
// technically overriden if not defined on the subclass, although we can
// ignore it as this is redundant with inheritance mechanisms.
defaults(author, obj)

return author
}
protected typeSpecificFilenames(): string[] {
return excludeNullish([this.content["featured-image"]])
Expand Down
13 changes: 8 additions & 5 deletions db/model/Gdoc/GdocFactory.ts
Original file line number Diff line number Diff line change
Expand Up @@ -93,11 +93,10 @@ export async function createGdocAndInsertIntoDb(
knex: KnexReadWriteTransaction,
id: string
): Promise<OwidGdoc> {
// Fetch the data from Google Docs and save it to the database
// Fetch the data from Google Docs.
// We have to fetch it here because we need to know the type of the Gdoc in load()
const base = new GdocBase(id)
await base.fetchAndEnrichGdoc()
await upsertGdoc(knex, base)

// Load its metadata and state so that subclass parsing & validation is also done.
// This involves a second call to the DB and Google, which makes me sad, but it'll do for now.
Expand All @@ -106,9 +105,13 @@ export async function createGdocAndInsertIntoDb(
base,
GdocsContentSource.Gdocs
)

// 2024-03-12 Daniel: We used to save here before the knex refactor but I think that was redundant?
// await gdoc.save()
// Save the enriched Gdoc to the database (including subclass-specific
// enrichments, cf. _enrichSubclassContent()). Otherwise subclass
// enrichments are not present on the Gdoc* subclass when loading from the
// DB (GdocsContentSource.Internal), since subclass enrichements are only
// done while fetching the live gdocs (GdocsContentSource.Gdocs) in
// loadFromGdocBase().
await upsertGdoc(knex, gdoc)

return gdoc
}
Expand Down
2 changes: 2 additions & 0 deletions packages/@ourworldindata/utils/src/Util.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import {
compact,
countBy,
debounce,
defaults,
difference,
drop,
dropRightWhile,
Expand Down Expand Up @@ -79,6 +80,7 @@ export {
compact,
countBy,
debounce,
defaults,
difference,
drop,
dropRightWhile,
Expand Down
1 change: 1 addition & 0 deletions packages/@ourworldindata/utils/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -151,6 +151,7 @@ export {
compact,
countBy,
debounce,
defaults,
difference,
drop,
dropRightWhile,
Expand Down