|
| 1 | +/** |
| 2 | + * GraphQL API mutations related to stories. |
| 3 | + * |
| 4 | + * @copyright 2016-present Kriasoft (https://git.io/vMINh) |
| 5 | + */ |
| 6 | + |
| 7 | +import slugify from "slugify"; |
| 8 | +import validator from "validator"; |
| 9 | +import { v4 as uuid } from "uuid"; |
| 10 | +import { mutationWithClientMutationId } from "graphql-relay"; |
| 11 | +import { |
| 12 | + GraphQLNonNull, |
| 13 | + GraphQLID, |
| 14 | + GraphQLString, |
| 15 | + GraphQLBoolean, |
| 16 | + GraphQLList, |
| 17 | +} from "graphql"; |
| 18 | + |
| 19 | +import db, { Story } from "../db"; |
| 20 | +import { Context } from "../context"; |
| 21 | +import { StoryType } from "../types"; |
| 22 | +import { fromGlobalId, validate } from "../utils"; |
| 23 | + |
| 24 | +function slug(text: string) { |
| 25 | + return slugify(text, { lower: true }); |
| 26 | +} |
| 27 | + |
| 28 | +export const upsertStory = mutationWithClientMutationId({ |
| 29 | + name: "UpsertStory", |
| 30 | + description: "Creates or updates a story.", |
| 31 | + |
| 32 | + inputFields: { |
| 33 | + id: { type: GraphQLID }, |
| 34 | + title: { type: GraphQLString }, |
| 35 | + text: { type: GraphQLString }, |
| 36 | + approved: { type: GraphQLBoolean }, |
| 37 | + validateOnly: { type: GraphQLBoolean }, |
| 38 | + }, |
| 39 | + |
| 40 | + outputFields: { |
| 41 | + story: { type: StoryType }, |
| 42 | + errors: { |
| 43 | + // TODO: Extract into a custom type. |
| 44 | + type: new GraphQLList( |
| 45 | + new GraphQLNonNull(new GraphQLList(new GraphQLNonNull(GraphQLString))), |
| 46 | + ), |
| 47 | + }, |
| 48 | + }, |
| 49 | + |
| 50 | + async mutateAndGetPayload(input, ctx: Context) { |
| 51 | + const id = input.id ? fromGlobalId(input.id, "Story") : null; |
| 52 | + const newId = uuid(); |
| 53 | + |
| 54 | + let story: Story | undefined; |
| 55 | + |
| 56 | + if (id) { |
| 57 | + story = await db.table<Story>("stories").where({ id }).first(); |
| 58 | + |
| 59 | + if (!story) { |
| 60 | + throw new Error(`Cannot find the story # ${id}.`); |
| 61 | + } |
| 62 | + |
| 63 | + // Only the author of the story or admins can edit it |
| 64 | + ctx.ensureAuthorized( |
| 65 | + (user) => story?.author_id === user.id || user.admin, |
| 66 | + ); |
| 67 | + } else { |
| 68 | + ctx.ensureAuthorized(); |
| 69 | + } |
| 70 | + |
| 71 | + // Validate and sanitize user input |
| 72 | + const { data, errors } = validate(input, (x) => |
| 73 | + x |
| 74 | + .field("title", { trim: true }) |
| 75 | + .isRequired() |
| 76 | + .isLength({ min: 5, max: 80 }) |
| 77 | + |
| 78 | + .field("text", { alias: "URL or text", trim: true }) |
| 79 | + .isRequired() |
| 80 | + .isLength({ min: 10, max: 1000 }) |
| 81 | + |
| 82 | + .field("text", { |
| 83 | + trim: true, |
| 84 | + as: "is_url", |
| 85 | + transform: (x) => |
| 86 | + validator.isURL(x, { protocols: ["http", "https"] }), |
| 87 | + }) |
| 88 | + |
| 89 | + .field("approved") |
| 90 | + .is(() => Boolean(ctx.user?.admin), "Only admins can approve a story."), |
| 91 | + ); |
| 92 | + |
| 93 | + if (errors.length > 0) { |
| 94 | + return { errors }; |
| 95 | + } |
| 96 | + |
| 97 | + if (data.title) { |
| 98 | + data.slug = `${slug(data.title)}-${(id || newId).substr(29)}`; |
| 99 | + } |
| 100 | + |
| 101 | + if (id && Object.keys(data).length) { |
| 102 | + [story] = await db |
| 103 | + .table<Story>("stories") |
| 104 | + .where({ id }) |
| 105 | + .update({ |
| 106 | + ...(data as Partial<Story>), |
| 107 | + updated_at: db.fn.now(), |
| 108 | + }) |
| 109 | + .returning("*"); |
| 110 | + } else { |
| 111 | + [story] = await db |
| 112 | + .table<Story>("stories") |
| 113 | + .insert({ |
| 114 | + id: newId, |
| 115 | + ...(data as Partial<Story>), |
| 116 | + author_id: ctx.user?.id, |
| 117 | + approved: ctx.user?.admin ? true : false, |
| 118 | + }) |
| 119 | + .returning("*"); |
| 120 | + } |
| 121 | + |
| 122 | + return { story }; |
| 123 | + }, |
| 124 | +}); |
| 125 | + |
| 126 | +export const likeStory = mutationWithClientMutationId({ |
| 127 | + name: "LikeStory", |
| 128 | + description: 'Marks the story as "liked".', |
| 129 | + |
| 130 | + inputFields: { |
| 131 | + id: { type: new GraphQLNonNull(GraphQLID) }, |
| 132 | + }, |
| 133 | + |
| 134 | + outputFields: { |
| 135 | + story: { type: StoryType }, |
| 136 | + }, |
| 137 | + |
| 138 | + async mutateAndGetPayload(input, ctx: Context) { |
| 139 | + // Check permissions |
| 140 | + ctx.ensureAuthorized(); |
| 141 | + |
| 142 | + const id = fromGlobalId(input.id, "Story"); |
| 143 | + const keys = { story_id: id, user_id: ctx.user.id }; |
| 144 | + |
| 145 | + const points = await db |
| 146 | + .table("story_points") |
| 147 | + .where(keys) |
| 148 | + .select(db.raw("1")); |
| 149 | + |
| 150 | + if (points.length) { |
| 151 | + await db.table("story_points").where(keys).del(); |
| 152 | + } else { |
| 153 | + await db.table("story_points").insert(keys); |
| 154 | + } |
| 155 | + |
| 156 | + const story = db.table("stories").where({ id }).first(); |
| 157 | + |
| 158 | + return { story }; |
| 159 | + }, |
| 160 | +}); |
0 commit comments