Skip to content

Commit

Permalink
Merge pull request #891 from Journaly/add-posting-ip-field
Browse files Browse the repository at this point in the history
🛜 Implement Solution For Storing Post & Comment Author's IP Address in Respective Tables
  • Loading branch information
robin-macpherson authored Sep 10, 2024
2 parents 62abf68 + 53b2d3c commit 4a9654e
Show file tree
Hide file tree
Showing 11 changed files with 53 additions and 16 deletions.
2 changes: 1 addition & 1 deletion packages/j-db-client/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@journaly/j-db-client",
"version": "13.20.0",
"version": "13.22.0",
"description": "Journaly's internal database client.",
"main": "dist/index",
"scripts": {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
-- AlterTable
ALTER TABLE "Comment" ADD COLUMN "postingIpAddress" TEXT;

-- AlterTable
ALTER TABLE "Post" ADD COLUMN "postingIpAddress" TEXT;

-- AlterTable
ALTER TABLE "PostComment" ADD COLUMN "postingIpAddress" TEXT;
3 changes: 3 additions & 0 deletions packages/j-db-client/prisma/schema.prisma
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,7 @@ model Post {
savedUsers User[] @relation("UserSavedPosts", references: [id])
privateShareId String? @unique
newPostNotifications NewPostNotification[]
postingIpAddress String?
}

model Language {
Expand Down Expand Up @@ -247,6 +248,7 @@ model Comment {
authorLanguageLevel LanguageLevel @default(BEGINNER)
threadCommentNotifications ThreadCommentNotification[]
mentionNotifications MentionNotification[]
postingIpAddress String?
}

model PostComment {
Expand All @@ -263,6 +265,7 @@ model PostComment {
postCommentNotifications PostCommentNotification[]
mentionNotifications MentionNotification[]
authorLanguageLevel LanguageLevel @default(BEGINNER)
postingIpAddress String?
}

model Thread {
Expand Down
2 changes: 1 addition & 1 deletion packages/j-mail/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@
"typescript": "4.2.4"
},
"dependencies": {
"@journaly/j-db-client": "^13.20.0",
"@journaly/j-db-client": "^13.22.0",
"@types/nodemailer": "^6.4.0",
"aws-sdk": "^2.737.0",
"date-fns": "^2.16.1",
Expand Down
14 changes: 7 additions & 7 deletions packages/web/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion packages/web/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@
"dependencies": {
"@apollo/client": "3.7.14",
"@github/text-expander-element": "^2.5.0",
"@journaly/j-db-client": "^13.20.0",
"@journaly/j-db-client": "^13.22.0",
"@prisma/client": "^3.15.2",
"@stripe/react-stripe-js": "^1.2.2",
"@stripe/stripe-js": "^1.12.1",
Expand Down
15 changes: 12 additions & 3 deletions packages/web/resolvers/comment.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ import {
ThreadWithRels,
sendNewBadgeEmail,
assignCountBadges,
getPostingIpAddress,
} from './utils'
import { NotFoundError } from './errors'

Expand All @@ -41,7 +42,7 @@ const assignCommentCountBadges = async (db: PrismaClient, userId: number): Promi
FROM "Comment"
WHERE "authorId" = ${userId}
`

const postCommentCountQuery = Prisma.sql`
SELECT COUNT(*) AS count
FROM "PostComment"
Expand Down Expand Up @@ -184,10 +185,11 @@ type CreateCommentArg = {
}>
author: UserWithRels<{ languages: true }>
body: string
postingIpAddress: string | undefined
db: PrismaClient
}

const createComment = async ({ db, thread, author, body }: CreateCommentArg) => {
const createComment = async ({ db, thread, author, body, postingIpAddress }: CreateCommentArg) => {
const authorHasPostLanguage =
author && author.languages.find((language) => language.languageId === thread.post.languageId)

Expand All @@ -199,6 +201,7 @@ const createComment = async ({ db, thread, author, body }: CreateCommentArg) =>
data: {
body,
authorLanguageLevel,
postingIpAddress,
author: {
connect: { id: author.id },
},
Expand Down Expand Up @@ -363,11 +366,13 @@ const CommentMutations = extendType({
},
})

const authorPostingIpAddress = getPostingIpAddress(ctx.request)
await createComment({
db: ctx.db,
thread,
author,
body,
postingIpAddress: authorPostingIpAddress,
})

await assignCommentCountBadges(ctx.db, userId)
Expand Down Expand Up @@ -453,13 +458,14 @@ const CommentMutations = extendType({
if (!author) {
throw new NotFoundError('user')
}

const authorPostingIpAddress = getPostingIpAddress(ctx.request)
const [comment, _] = await Promise.all([
createComment({
db: ctx.db,
thread,
author,
body: args.body,
postingIpAddress: authorPostingIpAddress,
}),
assignCommentCountBadges(ctx.db, userId),
] as const)
Expand Down Expand Up @@ -603,10 +609,13 @@ const CommentMutations = extendType({

const mentionedUserHandles = parseCommentBodyForMentions(args.body)

const authorPostingIpAddress = getPostingIpAddress(ctx.request)

const postComment = await ctx.db.postComment.create({
data: {
body: args.body,
authorLanguageLevel,
postingIpAddress: authorPostingIpAddress,
author: {
connect: { id: userId },
},
Expand Down
4 changes: 2 additions & 2 deletions packages/web/resolvers/context.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import { PrismaClient } from '@journaly/j-db-client'
import { IncomingMessage, ServerResponse } from 'http';

interface Request extends IncomingMessage {
userId: number,
export interface Request extends IncomingMessage {
userId: number
}


Expand Down
3 changes: 2 additions & 1 deletion packages/web/resolvers/post.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,9 @@ import {
generatePostPrivateShareId,
createInAppNotification,
sendReportSpamPostEmail,
getPostingIpAddress,
} from './utils'


import { NotFoundError, NotAuthorizedError, ResolverError } from './errors'
import {
Prisma,
Expand Down Expand Up @@ -486,6 +486,7 @@ const PostMutations = extendType({
publishedAt: isPublished ? new Date() : null,
bumpedAt: isPublished ? new Date() : null,
publishedLanguageLevel: userLanguageLevel,
postingIpAddress: getPostingIpAddress(ctx.request),
postCommentSubscriptions: {
create: [
{
Expand Down
15 changes: 15 additions & 0 deletions packages/web/resolvers/utils/getPostingIpAddress.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import { NextRequest } from 'next/server'
import { Request } from '../context'

const isNextRequest = (req: NextRequest | Request): req is NextRequest => {
return !!(req as NextRequest).ip
}

export const getPostingIpAddress = (req: NextRequest | Request) => {
if (isNextRequest(req)) return req.ip

const headers = req?.headers['X-Forwarded-For']
if (typeof headers === 'string') return headers.split(',')?.[0]

return undefined
}
1 change: 1 addition & 0 deletions packages/web/resolvers/utils/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -305,4 +305,5 @@ export * from './db'
export * from './notifications'
export * from './types'
export * from './badges'
export * from './getPostingIpAddress'
export type { NodeType }

0 comments on commit 4a9654e

Please sign in to comment.