Skip to content

Commit

Permalink
chore: remove noteFromQuote mutations
Browse files Browse the repository at this point in the history
Clients will handle formatting (or not) quoted notes
using the createNote mutations instead.
  • Loading branch information
kschelonka committed Jan 28, 2025
1 parent 6d11e36 commit 0cc3428
Show file tree
Hide file tree
Showing 9 changed files with 2 additions and 734 deletions.
76 changes: 0 additions & 76 deletions servers/notes-api/schema.graphql
Original file line number Diff line number Diff line change
Expand Up @@ -277,72 +277,6 @@ input CreateNoteMarkdownInput {
createdAt: ISOString
}

"""
Input to create a new Note seeded with copied content from a page.
The entire content becomes editable and is not able to be "reattached"
like a traditional highlight.
"""
input CreateNoteFromQuoteInput {
"""Optional title for this Note"""
title: String
"""
Client-provided UUID for the new Note.
If not provided, will be generated on the server.
"""
id: ID
"""
The Web Resource where the quote is taken from.
This should always be sent by the client where possible,
but in some cases (e.g. copying from mobile apps) there may
not be an accessible source url.
"""
source: ValidUrl
"""
JSON representation of a ProseMirror document, which
contains the formatted snipped text. This is used to seed
the initial Note document state, and will become editable.
"""
quote: ProseMirrorJson!
"""
When this note was created. If not provided, defaults to server time upon
receiving request.
"""
createdAt: ISOString
}

"""
Input to create a new Note seeded with copied content from a page.
The entire content becomes editable and is not able to be "reattached"
like a traditional highlight.
"""
input CreateNoteFromQuoteMarkdownInput {
"""Optional title for this Note"""
title: String
"""
Client-provided UUID for the new Note.
If not provided, will be generated on the server.
"""
id: ID
"""
The Web Resource where the quote is taken from.
This should always be sent by the client where possible,
but in some cases (e.g. copying from mobile apps) there may
not be an accessible source url.
"""
source: ValidUrl
"""
Commonmark Markdown document, which contains the formatted
snipped text. This is used to seed the initial Note
document state, and will become editable.
"""
quote: Markdown!
"""
When this note was created. If not provided, defaults to server time upon
receiving request.
"""
createdAt: ISOString
}

input EditNoteTitleInput {
"""The ID of the note to edit"""
id: ID!
Expand Down Expand Up @@ -427,16 +361,6 @@ type Mutation {
Create a new note, optionally with title and markdown content
"""
createNoteMarkdown(input: CreateNoteMarkdownInput!): Note! @requiresScopes(scopes: [["ROLE_USER"]])
"""
Create a new note, with a pre-populated block that contains the quoted and cited text
selected by a user.
"""
createNoteFromQuote(input: CreateNoteFromQuoteInput!): Note! @requiresScopes(scopes: [["ROLE_USER"]])
"""
Create a new note, with a pre-populated block that contains the quoted and cited text
selected by a user.
"""
createNoteFromQuoteMarkdown(input: CreateNoteFromQuoteMarkdownInput!): Note! @requiresScopes(scopes: [["ROLE_USER"]])

"""
Edit the title of a Note.
Expand Down
6 changes: 0 additions & 6 deletions servers/notes-api/src/apollo/resolvers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -69,12 +69,6 @@ export const resolvers: Resolvers = {
createNoteMarkdown(root, { input }, context) {
return context.NoteModel.createFromMarkdown(input);
},
createNoteFromQuote(root, { input }, context) {
return context.NoteModel.fromQuote(input);
},
createNoteFromQuoteMarkdown(root, { input }, context) {
return context.NoteModel.fromMarkdownQuote(input);
},
editNoteTitle(root, { input }, context) {
return context.NoteModel.editTitle(input);
},
Expand Down
46 changes: 1 addition & 45 deletions servers/notes-api/src/models/Note.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,7 @@ import { Insertable, NoResultError, Selectable } from 'kysely';
import { orderAndMap } from '../utils/dataloader';
import { IContext } from '../apollo/context';
import { NotesService } from '../datasources/NoteService';
import {
docFromMarkdown,
ProseMirrorDoc,
wrapDocInBlockQuote,
} from './ProseMirrorDoc';
import { docFromMarkdown, ProseMirrorDoc } from './ProseMirrorDoc';
import { DatabaseError } from 'pg';
import {
NoteFilterInput,
Expand Down Expand Up @@ -192,46 +188,6 @@ export class NoteModel {
};
return await this.create(createInput);
}
/**
* Create a new Note seeded with a blockquote (optionally with
* an additional paragraph with the source link).
*/
async fromQuote(input: CreateNoteFromQuoteInput) {
try {
const docContent = JSON.parse(input.quote);
const options =
input.source != null ? { source: input.source.toString() } : undefined;
const quotedDoc = wrapDocInBlockQuote(docContent, options);
const createInput: CreateNoteInput = {
docContent: JSON.stringify(quotedDoc),
createdAt: input.createdAt,
id: input.id,
title: input.title,
source: input.source,
};
return this.create(createInput);
} catch (error) {
if (error instanceof SyntaxError) {
throw new UserInputError(
`Received malformed JSON for docContent: ${error.message}`,
);
} else {
throw error;
}
}
}
/**
* Create a new Note seeded with a blockquote (optionally with
* an additional paragraph with the source link).
*/
async fromMarkdownQuote(input: CreateNoteFromQuoteMarkdownInput) {
const quote = docFromMarkdown(input.quote);
const createInput: CreateNoteFromQuoteInput = {
...input,
quote: JSON.stringify(quote.toJSON()),
};
return await this.fromQuote(createInput);
}
/**
* Edit a note's title
*/
Expand Down
40 changes: 1 addition & 39 deletions servers/notes-api/src/models/ProseMirrorDoc.spec.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,7 @@
import basicText from '../test/documents/basicText.json';
import {
docFromMarkdown,
ProseMirrorDoc,
wrapDocInBlockQuote,
} from './ProseMirrorDoc';
import { docFromMarkdown, ProseMirrorDoc } from './ProseMirrorDoc';
import { schema } from 'prosemirror-markdown';
import fromQuote from '../test/documents/fromQuote.json';
import badMd from '../test/documents/badMd.json';
import { UserInputError } from '@pocket-tools/apollo-utils';
import * as fs from 'fs';
import path from 'path';

Expand All @@ -30,38 +24,6 @@ describe('ProseMirrorDoc', () => {
expect(doc.markdown).toBeString();
});
});
describe('quote constructor', () => {
it('wraps quote in blockquote and adds attribution', () => {
const { input, expectedSource } = fromQuote;
const actual = wrapDocInBlockQuote(input, { source: 'localhost:3001' });
expect(actual).toEqual(expectedSource);
});
it('wraps quote in blockquote without attribution', () => {
const { input, expectedNoSource } = fromQuote;
const actual = wrapDocInBlockQuote(input);
expect(actual).toEqual(expectedNoSource);
});
it('throws error if an invalid node is encountered', () => {
const bad = {
type: 'doc',
content: [
{
type: 'invalid',
content: [
{
type: 'text',
text: '',
},
],
},
],
};
expect(() => wrapDocInBlockQuote(bad)).toThrowWithMessage(
UserInputError,
/.*Invalid Document.*/,
);
});
});
describe('markdown parser', () => {
it('parses plain text paragraphs', () => {
const doc = docFromMarkdown(basicTextMd);
Expand Down
74 changes: 0 additions & 74 deletions servers/notes-api/src/models/ProseMirrorDoc.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,77 +47,3 @@ export class ProseMirrorDoc {
export function docFromMarkdown(doc: string) {
return defaultMarkdownParser.parse(doc);
}

/**
* Wrap a JSON representation of a ProseMirror document in a blockquote,
* optionally with an additional paragraph that references the source
* link from where the document was copied.
* Returns a JSON-serializable representation of the new formatted document.
*/
export function wrapDocInBlockQuote<
S extends Schema<'blockquote' | 'paragraph' | 'text', 'link'>,
>(quoteDoc: any, options?: { source?: string; schema?: S }): any {
const opts = options ?? {};
Sentry.addBreadcrumb({
message: 'wrapDocInBlockQuote: input document',
type: 'log',
timestamp: Date.now(),
data: { source: opts.source, doc: quoteDoc },
});
const schema = opts.schema ?? commonMarkSchema;
const source = opts.source;
let initialState: EditorState;
try {
initialState = EditorState.create({ doc: Node.fromJSON(schema, quoteDoc) });
} catch (error) {
if (error instanceof RangeError) {
serverLogger.warn({
message: 'Attempted to parse document with unknown node type',
errorData: error,
document: quoteDoc,
});
throw new UserInputError(`Invalid Document: ${error.message}`);
} else {
throw error;
}
}
// Kind of a silly closure, but helps keep this more organized
// without having to pass along everything to a new function for
// error reporting/logging
const wrapDoc = (state: EditorState) => {
// Logic for wrapping in blockquote
const docSelect = new AllSelection(state.doc);
const range = docSelect.$from.blockRange(docSelect.$to);
if (range == null) {
const message = `Could not generate range from document`;
serverLogger.error({ message, document: quoteDoc, source });
throw new UserInputError(`${message} -- is the document malformed?`);
} else {
const wrapping = findWrapping(range, schema.nodes.blockquote, {});
if (wrapping == null) {
const message = `Could not wrap document selection`;
serverLogger.error({ message, document: quoteDoc, source });
throw new UserInputError(`${message} -- is the document malformed?`);
} else {
const transaction = state.tr.wrap(range, wrapping);
const trxResult = state.applyTransaction(transaction);
return trxResult.state;
}
}
};
// Wrap document in blockquote
const state = wrapDoc(initialState);
// Insert paragraph with source link if provided
if (source != null) {
const node = schema.node('paragraph', {}, [
schema.text('Source: '),
schema.text(source, [schema.mark('link', { href: source })]),
]);
const transaction = state.tr.insert(state.tr.doc.content.size, node);
const trxResult = state.applyTransaction(transaction);
return trxResult.state.doc.toJSON();
} else {
// Just return the blockquoted-doc
return state.doc.toJSON();
}
}
Loading

0 comments on commit 0cc3428

Please sign in to comment.