Skip to content

Commit

Permalink
Merge branch 'main' into em/preview
Browse files Browse the repository at this point in the history
  • Loading branch information
3mcd authored Mar 3, 2025
2 parents e507897 + 843aeb3 commit 4eae5f1
Show file tree
Hide file tree
Showing 19 changed files with 734 additions and 52 deletions.
5 changes: 4 additions & 1 deletion core/actions/googleDriveImport/formatDriveData.ts
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,10 @@ const processAssets = async (html: string, pubId: string): Promise<string> => {
assetData.buffer,
{ contentType: assetData.mimetype }
);
assetUrls[originalAssetUrl] = uploadedUrl;
assetUrls[originalAssetUrl] = uploadedUrl.replace(
"assets.app.pubpub.org.s3.us-east-1.amazonaws.com",
"assets.app.pubpub.org"
);
} else {
assetUrls[originalAssetUrl] = originalAssetUrl;
}
Expand Down
2 changes: 2 additions & 0 deletions core/actions/googleDriveImport/gdocPlugins.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -659,6 +659,7 @@ test("Structure InlineMath", async () => {
<body>
<p>I am just writing a lovely $10 equation like this $y=2x + 5$</p>
<p>Should also work as long as styling doesn't <b>change throughout, such as $z= 25x + 2$ and <i>so</i> on.</b></p>
<p>Now consider two different genes, $A$ and $B$, with variation in allelic state across a population of diploid organisms. One gene $A$ has two alleles $A$ and $a$, resulting in three allelic states, </p>
</body>
</html>
`;
Expand All @@ -668,6 +669,7 @@ test("Structure InlineMath", async () => {
<body>
<p>I am just writing a lovely $10 equation like this <span class="math-block"><span class="katex"><span class="katex-mathml"><math xmlns="http://www.w3.org/1998/Math/MathML"><semantics><mrow><mi>y</mi><mo>=</mo><mn>2</mn><mi>x</mi><mo>+</mo><mn>5</mn></mrow><annotation encoding="application/x-tex">y=2x + 5</annotation></semantics></math></span><span class="katex-html" aria-hidden="true"><span class="base"><span class="strut" style="height:0.625em;vertical-align:-0.1944em;"></span><span class="mord mathnormal" style="margin-right:0.03588em;">y</span><span class="mspace" style="margin-right:0.2778em;"></span><span class="mrel">=</span><span class="mspace" style="margin-right:0.2778em;"></span></span><span class="base"><span class="strut" style="height:0.7278em;vertical-align:-0.0833em;"></span><span class="mord">2</span><span class="mord mathnormal">x</span><span class="mspace" style="margin-right:0.2222em;"></span><span class="mbin">+</span><span class="mspace" style="margin-right:0.2222em;"></span></span><span class="base"><span class="strut" style="height:0.6444em;"></span><span class="mord">5</span></span></span></span></span></p>
<p>Should also work as long as styling doesn't <b>change throughout, such as <span class="math-block"><span class="katex"><span class="katex-mathml"><math xmlns="http://www.w3.org/1998/Math/MathML"><semantics><mrow><mi>z</mi><mo>=</mo><mn>25</mn><mi>x</mi><mo>+</mo><mn>2</mn></mrow><annotation encoding="application/x-tex">z= 25x + 2</annotation></semantics></math></span><span class="katex-html" aria-hidden="true"><span class="base"><span class="strut" style="height:0.4306em;"></span><span class="mord mathnormal" style="margin-right:0.04398em;">z</span><span class="mspace" style="margin-right:0.2778em;"></span><span class="mrel">=</span><span class="mspace" style="margin-right:0.2778em;"></span></span><span class="base"><span class="strut" style="height:0.7278em;vertical-align:-0.0833em;"></span><span class="mord">25</span><span class="mord mathnormal">x</span><span class="mspace" style="margin-right:0.2222em;"></span><span class="mbin">+</span><span class="mspace" style="margin-right:0.2222em;"></span></span><span class="base"><span class="strut" style="height:0.6444em;"></span><span class="mord">2</span></span></span></span></span> and <i>so</i> on.</b></p>
<p>Now consider two different genes, <span class="math-block"><span class="katex"><span class="katex-mathml"><math xmlns="http://www.w3.org/1998/Math/MathML"><semantics><mrow><mi>A</mi></mrow><annotation encoding="application/x-tex">A</annotation></semantics></math></span><span class="katex-html" aria-hidden="true"><span class="base"><span class="strut" style="height:0.6833em;"></span><span class="mord mathnormal">A</span></span></span></span></span> and <span class="math-block"><span class="katex"><span class="katex-mathml"><math xmlns="http://www.w3.org/1998/Math/MathML"><semantics><mrow><mi>B</mi></mrow><annotation encoding="application/x-tex">B</annotation></semantics></math></span><span class="katex-html" aria-hidden="true"><span class="base"><span class="strut" style="height:0.6833em;"></span><span class="mord mathnormal" style="margin-right:0.05017em;">B</span></span></span></span></span>, with variation in allelic state across a population of diploid organisms. One gene <span class="math-block"><span class="katex"><span class="katex-mathml"><math xmlns="http://www.w3.org/1998/Math/MathML"><semantics><mrow><mi>A</mi></mrow><annotation encoding="application/x-tex">A</annotation></semantics></math></span><span class="katex-html" aria-hidden="true"><span class="base"><span class="strut" style="height:0.6833em;"></span><span class="mord mathnormal">A</span></span></span></span></span> has two alleles <span class="math-block"><span class="katex"><span class="katex-mathml"><math xmlns="http://www.w3.org/1998/Math/MathML"><semantics><mrow><mi>A</mi></mrow><annotation encoding="application/x-tex">A</annotation></semantics></math></span><span class="katex-html" aria-hidden="true"><span class="base"><span class="strut" style="height:0.6833em;"></span><span class="mord mathnormal">A</span></span></span></span></span> and <span class="math-block"><span class="katex"><span class="katex-mathml"><math xmlns="http://www.w3.org/1998/Math/MathML"><semantics><mrow><mi>a</mi></mrow><annotation encoding="application/x-tex">a</annotation></semantics></math></span><span class="katex-html" aria-hidden="true"><span class="base"><span class="strut" style="height:0.4306em;"></span><span class="mord mathnormal">a</span></span></span></span></span>, resulting in three allelic states, </p>
</body>
</html>
`;
Expand Down
2 changes: 1 addition & 1 deletion core/actions/googleDriveImport/gdocPlugins.ts
Original file line number Diff line number Diff line change
Expand Up @@ -590,7 +590,7 @@ export const structureBlockMath = () => (tree: Root) => {
export const structureInlineMath = () => (tree: Root) => {
visit(tree, "text", (node: any, index: any, parent: any) => {
if (typeof node.value === "string") {
const regex = /\$(\S[^$]*\S)\$/g;
const regex = /\$(\S(?:[^$]*\S)?)\$/g;
let match;
const elements: any[] = [];
let lastIndex = 0;
Expand Down
55 changes: 47 additions & 8 deletions core/actions/googleDriveImport/run.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import type { PubsId } from "db/public";
import { logger } from "logger";

import { doPubsExist, getPubTypesForCommunity, updatePub, upsertPubRelations } from "~/lib/server";
Expand Down Expand Up @@ -41,6 +42,23 @@ export const run = defineRun<typeof action>(
existingPubs.forEach((pub) => existingDiscussionPubIds.push(pub.id));
}

const existingVersionIdPairs = pub.values
.filter(
(values) =>
values.fieldSlug === `${communitySlug}:versions` &&
values.relatedPubId &&
values.relatedPub
)
.map((values) => {
const publicationDateField = values.relatedPub!.values.filter(
(value) => value.fieldSlug === `${communitySlug}:publication-date`
)[0];
const publicationDate: Date = publicationDateField
? (publicationDateField.value as Date)
: new Date(values.relatedPub!.createdAt);
return { [`${publicationDate.toISOString()}`]: values.relatedPubId };
});

// Versions don't have IDs so we compare timestamps
const existingVersionDates = pub.values
.filter(
Expand Down Expand Up @@ -85,6 +103,7 @@ export const run = defineRun<typeof action>(
relatedPubId: discussion.id,
};
}),
/* Create new versions from gdrive if they don't exist */
...formattedData.versions
.filter(
(version) =>
Expand All @@ -106,6 +125,27 @@ export const run = defineRun<typeof action>(
}),
];

/* Lazily update all existing old versions (TODO: Check for changed content) */
formattedData.versions
.filter((version) =>
existingVersionDates.includes(version[`${communitySlug}:publication-date`])
)
.forEach(async (version) => {
const versionDate = version[`${communitySlug}:publication-date`];
const relatedVersionId = existingVersionIdPairs.filter(
(pair) => pair[versionDate]
)[0][versionDate] as PubsId;
await updatePub({
pubId: relatedVersionId,
communityId,
lastModifiedBy,
continueOnValidationError: false,
pubValues: {
...version,
},
});
});

/* NON-MIGRATION */
/* If the main doc is updated, make a new version */
const orderedVersions = pub.values
Expand All @@ -120,16 +160,15 @@ export const run = defineRun<typeof action>(
const fooDateField = foo.relatedPub!.values.filter(
(value: any) => value.fieldSlug === `${communitySlug}:publication-date`
)[0];
const barDateField = foo.relatedPub!.values.filter(
const barDateField = bar.relatedPub!.values.filter(
(value: any) => value.fieldSlug === `${communitySlug}:publication-date`
)[0];

const fooDate: Date = fooDateField
? fooDateField.value
: foo.relatedPub!.createdAt;
const barDate: Date = barDateField
? barDateField.value
: foo.relatedPub!.createdAt;
const fooDate = new Date(
fooDateField ? fooDateField.value : foo.relatedPub!.createdAt
);
const barDate = new Date(
barDateField ? barDateField.value : bar.relatedPub!.createdAt
);
return barDate.getTime() - fooDate.getTime();
});

Expand Down
34 changes: 33 additions & 1 deletion core/lib/server/user.ts
Original file line number Diff line number Diff line change
Expand Up @@ -237,13 +237,45 @@ export const createUserWithMembership = async (data: {
};
}

if (!user?.isSuperAdmin && isSuperAdmin) {
if (!user.isSuperAdmin && isSuperAdmin) {
return {
title: "Failed to add member",
error: "You cannot add members as super admins",
};
}

// If they're adding a community member, make sure their role is equivalent or higher than
// the new member's. If they're adding a different type of membership, the community
// membership is always a contributor, so we can skip this check.
if (membership.type === MembershipType.community) {
const rolesRanking = {
[MemberRole.admin]: 2,
[MemberRole.editor]: 1,
[MemberRole.contributor]: 0,
};
const highestRole = user.memberships.reduce(
(highestRole, m) => {
if (m.communityId === community.id) {
if (!highestRole || rolesRanking[m.role] > rolesRanking[highestRole]) {
return m.role;
}
}
return highestRole;
},
undefined as MemberRole | undefined
);

const roleIsHighEnough =
highestRole && rolesRanking[highestRole] >= rolesRanking[membership.role];

if (!roleIsHighEnough) {
return {
title: "Failed to add member",
error: "You cannot add members with a higher role than your own",
};
}
}

let nameQuery: (trx: Transaction<Database>) => Promise<string>;
let membershipQuery: (trx: Transaction<Database>, userId: UsersId) => Promise<unknown>;
let target: CapabilityTarget;
Expand Down
Loading

0 comments on commit 4eae5f1

Please sign in to comment.