Add labels and state to savePage API
This commit is contained in:
@ -86,7 +86,7 @@ export interface Label {
|
||||
id: string
|
||||
name: string
|
||||
color: string
|
||||
description?: string
|
||||
description?: string | null
|
||||
createdAt?: Date
|
||||
}
|
||||
|
||||
|
||||
@ -24,7 +24,7 @@ export class Label {
|
||||
color!: string
|
||||
|
||||
@Column('text', { nullable: true })
|
||||
description?: string
|
||||
description?: string | null
|
||||
|
||||
@CreateDateColumn()
|
||||
createdAt!: Date
|
||||
|
||||
@ -402,7 +402,7 @@ export enum CreateLabelErrorCode {
|
||||
}
|
||||
|
||||
export type CreateLabelInput = {
|
||||
color: Scalars['String'];
|
||||
color?: InputMaybe<Scalars['String']>;
|
||||
description?: InputMaybe<Scalars['String']>;
|
||||
name: Scalars['String'];
|
||||
};
|
||||
@ -2187,6 +2187,7 @@ export type SaveError = {
|
||||
|
||||
export enum SaveErrorCode {
|
||||
EmbeddedHighlightFailed = 'EMBEDDED_HIGHLIGHT_FAILED',
|
||||
EmbeddedLabelFailed = 'EMBEDDED_LABEL_FAILED',
|
||||
Unauthorized = 'UNAUTHORIZED',
|
||||
Unknown = 'UNKNOWN'
|
||||
}
|
||||
@ -2225,7 +2226,7 @@ export type SaveFilterSuccess = {
|
||||
|
||||
export type SavePageInput = {
|
||||
clientRequestId: Scalars['ID'];
|
||||
labels?: InputMaybe<Array<InputMaybe<Scalars['String']>>>;
|
||||
labels?: InputMaybe<Array<CreateLabelInput>>;
|
||||
originalContent: Scalars['String'];
|
||||
parseResult?: InputMaybe<ParseResult>;
|
||||
source: Scalars['String'];
|
||||
|
||||
@ -350,7 +350,7 @@ enum CreateLabelErrorCode {
|
||||
}
|
||||
|
||||
input CreateLabelInput {
|
||||
color: String!
|
||||
color: String
|
||||
description: String
|
||||
name: String!
|
||||
}
|
||||
@ -1584,6 +1584,7 @@ type SaveError {
|
||||
|
||||
enum SaveErrorCode {
|
||||
EMBEDDED_HIGHLIGHT_FAILED
|
||||
EMBEDDED_LABEL_FAILED
|
||||
UNAUTHORIZED
|
||||
UNKNOWN
|
||||
}
|
||||
@ -1620,7 +1621,7 @@ type SaveFilterSuccess {
|
||||
|
||||
input SavePageInput {
|
||||
clientRequestId: ID!
|
||||
labels: [String]
|
||||
labels: [CreateLabelInput!]
|
||||
originalContent: String!
|
||||
parseResult: ParseResult
|
||||
source: String!
|
||||
|
||||
@ -1,4 +1,4 @@
|
||||
import { authorized } from '../../utils/helpers'
|
||||
import { authorized, generateRandomColor } from '../../utils/helpers'
|
||||
import {
|
||||
CreateLabelError,
|
||||
CreateLabelErrorCode,
|
||||
@ -114,7 +114,7 @@ export const createLabelResolver = authorized<
|
||||
const label = await getRepository(Label).save({
|
||||
user,
|
||||
name,
|
||||
color,
|
||||
color: color || generateRandomColor(),
|
||||
description: description || '',
|
||||
})
|
||||
|
||||
|
||||
@ -513,6 +513,7 @@ const schema = gql`
|
||||
UNKNOWN
|
||||
UNAUTHORIZED
|
||||
EMBEDDED_HIGHLIGHT_FAILED
|
||||
EMBEDDED_LABEL_FAILED
|
||||
}
|
||||
|
||||
type SaveError {
|
||||
@ -555,7 +556,7 @@ const schema = gql`
|
||||
originalContent: String!
|
||||
parseResult: ParseResult
|
||||
state: ArticleSavingRequestStatus
|
||||
labels: [String]
|
||||
labels: [CreateLabelInput!]
|
||||
}
|
||||
|
||||
input SaveUrlInput {
|
||||
@ -1430,7 +1431,7 @@ const schema = gql`
|
||||
|
||||
input CreateLabelInput {
|
||||
name: String! @sanitize(maxLength: 64)
|
||||
color: String! @sanitize(pattern: "^#([A-Fa-f0-9]{6}|[A-Fa-f0-9]{3})$")
|
||||
color: String @sanitize(pattern: "^#([A-Fa-f0-9]{6}|[A-Fa-f0-9]{3})$")
|
||||
description: String @sanitize(maxLength: 100)
|
||||
}
|
||||
|
||||
|
||||
@ -2,7 +2,7 @@ import { Label } from '../entity/label'
|
||||
import { ILike, In } from 'typeorm'
|
||||
import { PageContext } from '../elastic/types'
|
||||
import { User } from '../entity/user'
|
||||
import { addLabelInPage } from '../elastic/labels'
|
||||
import { addLabelInPage, updateLabelsInPage } from '../elastic/labels'
|
||||
import { getRepository } from '../entity/utils'
|
||||
import { Link } from '../entity/link'
|
||||
import DataLoader from 'dataloader'
|
||||
@ -103,3 +103,43 @@ export const createLabel = async (
|
||||
user: { id: userId },
|
||||
})
|
||||
}
|
||||
|
||||
export const addLabelsToNewPage = async (
|
||||
ctx: PageContext,
|
||||
pageId: string,
|
||||
labels: {
|
||||
name: string
|
||||
color?: string | null
|
||||
description?: string | null
|
||||
}[]
|
||||
): Promise<boolean> => {
|
||||
const user = await getRepository(User).findOneBy({
|
||||
id: ctx.uid,
|
||||
})
|
||||
if (!user) {
|
||||
console.log('user not found')
|
||||
return false
|
||||
}
|
||||
|
||||
const labelEntities = await getRepository(Label).findBy({
|
||||
user: { id: user.id },
|
||||
name: In(labels.map((l) => l.name)),
|
||||
})
|
||||
|
||||
const existingLabels = labelEntities.map((l) => l.name)
|
||||
const newLabels = labels.filter((l) => !existingLabels.includes(l.name))
|
||||
// create new labels
|
||||
const newLabelEntities = await getRepository(Label).save(
|
||||
newLabels.map((l) => ({
|
||||
...l,
|
||||
color: l.color || generateRandomColor(),
|
||||
user,
|
||||
}))
|
||||
)
|
||||
// add all labels to page
|
||||
return updateLabelsInPage(
|
||||
pageId,
|
||||
[...newLabelEntities, ...labelEntities],
|
||||
ctx
|
||||
)
|
||||
}
|
||||
|
||||
@ -22,6 +22,7 @@ import {
|
||||
} from '../utils/helpers'
|
||||
import { parsePreparedContent } from '../utils/parser'
|
||||
import { createPageSaveRequest } from './create_page_save_request'
|
||||
import { addLabelsToNewPage } from './labels'
|
||||
|
||||
type SaveContext = {
|
||||
pubsub: PubsubClient
|
||||
@ -174,21 +175,22 @@ export const savePage = async (
|
||||
type: HighlightType.Highlight,
|
||||
}
|
||||
|
||||
if (
|
||||
!(await addHighlightToPage(pageId, highlight, {
|
||||
pubsub: ctx.pubsub,
|
||||
uid: ctx.uid,
|
||||
}))
|
||||
) {
|
||||
if (!(await addHighlightToPage(pageId, highlight, ctx))) {
|
||||
return {
|
||||
errorCodes: [SaveErrorCode.EmbeddedHighlightFailed],
|
||||
message: 'Failed to save highlight',
|
||||
}
|
||||
}
|
||||
}
|
||||
// TODO: add labels to page
|
||||
// if (pageId && input.labels) {
|
||||
// }
|
||||
// add labels to page
|
||||
if (pageId && input.labels) {
|
||||
if (!(await addLabelsToNewPage(ctx, pageId, input.labels))) {
|
||||
return {
|
||||
errorCodes: [SaveErrorCode.EmbeddedLabelFailed],
|
||||
message: 'Failed to save labels',
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return {
|
||||
clientRequestId: pageId,
|
||||
|
||||
@ -90,7 +90,9 @@ const importURL = async (
|
||||
url: url.toString(),
|
||||
saveRequestId: uuid(),
|
||||
state,
|
||||
labels,
|
||||
labels: labels?.map((l) => {
|
||||
return { name: l }
|
||||
}),
|
||||
})
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user