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

chore: remove noteFromQuote mutations #1034

Merged
merged 2 commits into from
Jan 28, 2025
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
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 @@ -2,11 +2,11 @@
import {
Note,
CreateNoteInput,
CreateNoteFromQuoteInput,

Check failure on line 5 in servers/notes-api/src/models/Note.ts

View workflow job for this annotation

GitHub Actions / lint

'CreateNoteFromQuoteInput' is defined but never used
EditNoteTitleInput,
EditNoteContentInput,
DeleteNoteInput,
CreateNoteFromQuoteMarkdownInput,

Check failure on line 9 in servers/notes-api/src/models/Note.ts

View workflow job for this annotation

GitHub Actions / lint

'CreateNoteFromQuoteMarkdownInput' is defined but never used
CreateNoteMarkdownInput,
EditNoteContentMarkdownInput,
ArchiveNoteInput,
Expand All @@ -16,11 +16,7 @@
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 @@
};
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
@@ -1,14 +1,14 @@
import { Node, Schema } from 'prosemirror-model';
import { EditorState, AllSelection } from 'prosemirror-state';

Check failure on line 2 in servers/notes-api/src/models/ProseMirrorDoc.ts

View workflow job for this annotation

GitHub Actions / lint

'EditorState' is defined but never used

Check failure on line 2 in servers/notes-api/src/models/ProseMirrorDoc.ts

View workflow job for this annotation

GitHub Actions / lint

'AllSelection' is defined but never used
import { findWrapping } from 'prosemirror-transform';

Check failure on line 3 in servers/notes-api/src/models/ProseMirrorDoc.ts

View workflow job for this annotation

GitHub Actions / lint

'findWrapping' is defined but never used
import {
defaultMarkdownSerializer,
defaultMarkdownParser,
schema as commonMarkSchema,
} from 'prosemirror-markdown';
import { UserInputError } from '@pocket-tools/apollo-utils';

Check failure on line 9 in servers/notes-api/src/models/ProseMirrorDoc.ts

View workflow job for this annotation

GitHub Actions / lint

'UserInputError' is defined but never used
import { serverLogger } from '@pocket-tools/ts-logger';

Check failure on line 10 in servers/notes-api/src/models/ProseMirrorDoc.ts

View workflow job for this annotation

GitHub Actions / lint

'serverLogger' is defined but never used
import * as Sentry from '@sentry/node';

Check failure on line 11 in servers/notes-api/src/models/ProseMirrorDoc.ts

View workflow job for this annotation

GitHub Actions / lint

'Sentry' is defined but never used

/**
* Class for handling ProseMirror documents
Expand Down Expand Up @@ -47,77 +47,3 @@
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
Loading