diff --git a/api/resolvers/notifications.js b/api/resolvers/notifications.js index 59b752fb..9a449f76 100644 --- a/api/resolvers/notifications.js +++ b/api/resolvers/notifications.js @@ -44,38 +44,38 @@ export default { let notifications = await models.$queryRaw(` SELECT ${ITEM_FIELDS}, "Item".created_at as "sortTime", NULL as "earnedSats", - false as mention - From "Item" - JOIN "Item" p ON "Item"."parentId" = p.id - WHERE p."userId" = $1 - AND "Item"."userId" <> $1 AND "Item".created_at <= $2 + false as mention + FROM "Item" + JOIN "Item" p ON "Item"."parentId" = p.id + WHERE p."userId" = $1 + AND "Item"."userId" <> $1 AND "Item".created_at <= $2 UNION ALL (SELECT ${ITEM_SUBQUERY_FIELDS}, max(subquery.voted_at) as "sortTime", - sum(subquery.sats) as "earnedSats", false as mention - FROM - (SELECT ${ITEM_FIELDS}, "Vote".created_at as voted_at, "Vote".sats, - ROW_NUMBER() OVER(ORDER BY "Vote".created_at) - - ROW_NUMBER() OVER(PARTITION BY "Item".id ORDER BY "Vote".created_at) as island - FROM "Vote" - JOIN "Item" on "Vote"."itemId" = "Item".id - WHERE "Vote"."userId" <> $1 - AND "Vote".created_at <= $2 - AND "Vote".boost = false - AND "Item"."userId" = $1) subquery - GROUP BY ${ITEM_SUBQUERY_FIELDS}, subquery.island ORDER BY max(subquery.voted_at) desc) + sum(subquery.sats) as "earnedSats", false as mention + FROM + (SELECT ${ITEM_FIELDS}, "Vote".created_at as voted_at, "Vote".sats, + ROW_NUMBER() OVER(ORDER BY "Vote".created_at) - + ROW_NUMBER() OVER(PARTITION BY "Item".id ORDER BY "Vote".created_at) as island + FROM "Vote" + JOIN "Item" on "Vote"."itemId" = "Item".id + WHERE "Vote"."userId" <> $1 + AND "Vote".created_at <= $2 + AND "Vote".boost = false + AND "Item"."userId" = $1) subquery + GROUP BY ${ITEM_SUBQUERY_FIELDS}, subquery.island ORDER BY max(subquery.voted_at) desc) UNION ALL (SELECT ${ITEM_FIELDS}, "Mention".created_at as "sortTime", NULL as "earnedSats", - true as mention - FROM "Mention" - JOIN "Item" on "Mention"."itemId" = "Item".id - JOIN "Item" p on "Item"."parentId" = p.id - WHERE "Mention"."userId" = $1 - AND "Mention".created_at <= $2 - AND "Item"."userId" <> $1 - AND p."userId" <> $1) - ORDER BY "sortTime" DESC - OFFSET $3 - LIMIT ${LIMIT}`, me.id, decodedCursor.time, decodedCursor.offset) + true as mention + FROM "Mention" + JOIN "Item" on "Mention"."itemId" = "Item".id + JOIN "Item" p on "Item"."parentId" = p.id + WHERE "Mention"."userId" = $1 + AND "Mention".created_at <= $2 + AND "Item"."userId" <> $1 + AND p."userId" <> $1) + ORDER BY "sortTime" DESC + OFFSET $3 + LIMIT ${LIMIT}`, me.id, decodedCursor.time, decodedCursor.offset) notifications = notifications.map(n => { n.item = { ...n } diff --git a/api/resolvers/user.js b/api/resolvers/user.js index 4a7369a7..1104ed1a 100644 --- a/api/resolvers/user.js +++ b/api/resolvers/user.js @@ -14,7 +14,7 @@ export default { throw new AuthenticationError('you must be logged in') } - return me.name === name || !(await models.user.findUnique({ where: { name } })) + return me.name?.toUpperCase() === name?.toUpperCase() || !(await models.user.findUnique({ where: { name } })) }, recentlyStacked: async (parent, args, { models, me }) => { if (!me) { diff --git a/prisma/migrations/20210902220036_case_insensitive_names/migration.sql b/prisma/migrations/20210902220036_case_insensitive_names/migration.sql new file mode 100644 index 00000000..f69d081d --- /dev/null +++ b/prisma/migrations/20210902220036_case_insensitive_names/migration.sql @@ -0,0 +1,10 @@ +/* + Warnings: + + - A unique constraint covering the columns `[name]` on the table `users` will be added. If there are existing duplicate values, this will fail. + +*/ +CREATE EXTENSION IF NOT EXISTS citext; + +-- AlterTable +ALTER TABLE users ALTER COLUMN name TYPE citext; diff --git a/prisma/schema.prisma b/prisma/schema.prisma index 3de4471c..9468a97c 100644 --- a/prisma/schema.prisma +++ b/prisma/schema.prisma @@ -14,7 +14,7 @@ model User { id Int @id @default(autoincrement()) createdAt DateTime @default(now()) @map(name: "created_at") updatedAt DateTime @default(now()) @updatedAt @map(name: "updated_at") - name String? @unique + name String? @unique @db.Citext email String? @unique emailVerified DateTime? @map(name: "email_verified") image String?