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

add replyToComment #57

Merged
merged 4 commits into from
Dec 7, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
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
25 changes: 13 additions & 12 deletions src/controllers/notifications.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
import { CommentModel } from '@/models/Comment'
import { Controller, Flow, Get, Post, Query } from 'amala'
import { PostModel, defaultPostProjection } from '@/models/Post'
import { TokenModel } from '@/models/Token'
import { getComments } from '@/models/Comment'
import { getLastTimeSentFromStorage } from '@/helpers/lastTimeSent'
import authenticate from '@/helpers/authenticate'
import defaultProvider from '@/helpers/defaultProvider'
import docsToObject from '@/helpers/docsToObject'
import ketlAttestationContract from '@/helpers/getKetlAttestation'
import sendFirebaseNotification from '@/helpers/sendFirebaseNotification'

Expand Down Expand Up @@ -37,7 +36,10 @@ export default class NotificationsController {
}

@Get('/')
async getData(@Query('currentBlockNumber') currentBlockNumber?: number) {
async getData(
@Query('currentBlockNumber') currentBlockNumber?: number,
@Query('isDev') isDev?: boolean
) {
const currentBlock = await defaultProvider.getBlockNumber()

if (Number.isNaN(currentBlockNumber))
Expand All @@ -47,13 +49,12 @@ export default class NotificationsController {
modifiedPostsSinceLastCheck: [],
}

const commentSinceLastCheck = await CommentModel.find(
{
blockNumber: { $gt: currentBlockNumber, $lte: currentBlock },
},
defaultPostProjection
)
const postsSinceLastCheck = await PostModel.find(
const modifiedCommentSinceLastCheck = await getComments({
fromBlock: currentBlockNumber,
isDev,
toBlock: currentBlock,
})
const modifiedPostsSinceLastCheck = await PostModel.find(
{
blockNumber: { $gt: currentBlockNumber, $lte: currentBlock },
},
Expand All @@ -62,8 +63,8 @@ export default class NotificationsController {

return {
currentBlock,
modifiedCommentSinceLastCheck: docsToObject(commentSinceLastCheck),
modifiedPostsSinceLastCheck: docsToObject(postsSinceLastCheck),
modifiedCommentSinceLastCheck,
modifiedPostsSinceLastCheck,
}
}

Expand Down
5 changes: 0 additions & 5 deletions src/helpers/docsToObject.ts

This file was deleted.

2 changes: 2 additions & 0 deletions src/helpers/parse.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import {
PostAddedEvent,
PostStructOutput,
} from '@big-whale-labs/obss-storage-contract/dist/typechain/contracts/Feeds'
import { generateRandomName } from '@big-whale-labs/backend-utils'
import structToCid from '@/helpers/structToCid'

function transform(post: PostStructOutput) {
Expand All @@ -16,6 +17,7 @@ function transform(post: PostStructOutput) {
sender,
threadId,
timestamp,
username: generateRandomName(sender),
}
}

Expand Down
30 changes: 27 additions & 3 deletions src/helpers/saveBlockchainEvents.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,13 @@ import { parseComment, parsePost } from '@/helpers/parse'
import getEvents from '@/helpers/getEventst'
import getFeedsContract from '@/helpers/getFeedsContract'

export async function savePostEvent(event: PostAddedEvent) {
export async function savePostEvent(event: PostAddedEvent, isDev = false) {
try {
const post = parsePost(event)
await PostModel.findOneAndUpdate(
{
feedId: post.feedId,
isDev,
postId: post.postId,
transactionHash: post.transactionHash,
},
Expand All @@ -28,17 +29,40 @@ export async function savePostEvent(event: PostAddedEvent) {
}
}

export async function saveCommentEvent(event: CommentAddedEvent) {
export async function saveCommentEvent(
event: CommentAddedEvent,
isDev = false
) {
try {
const comment = parseComment(event)
let replyToPost, replyToComment
if (comment.replyTo === 0) {
replyToPost = await PostModel.findOne({
feedId: comment.feedId,
isDev,
postId: comment.postId,
})
} else {
replyToComment = await CommentModel.findOne({
commentId: comment.replyTo,
feedId: comment.feedId,
isDev,
postId: comment.postId,
})
}
await CommentModel.findOneAndUpdate(
{
commentId: comment.commentId,
feedId: comment.feedId,
isDev,
postId: comment.postId,
transactionHash: comment.transactionHash,
},
comment,
{
...comment,
replyToComment,
replyToPost,
},
{ new: true, upsert: true }
)
} catch (e) {
Expand Down
39 changes: 37 additions & 2 deletions src/models/Comment.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,46 @@
import { Post } from '@/models/Post'
import { getModelForClass, prop } from '@typegoose/typegoose'
import { Post, defaultPostProjection } from '@/models/Post'
import { Ref, getModelForClass, prop } from '@typegoose/typegoose'

export class Comment extends Post {
@prop({ required: true })
public commentId!: number
@prop({ required: true })
public replyTo!: number
@prop({ ref: () => Comment })
replyToComment?: Ref<Comment>
@prop({ ref: () => Post })
replyToPost?: Ref<Post>
}

export const CommentModel = getModelForClass(Comment)

export function getComments({
fromBlock,
isDev = false,
toBlock,
}: {
fromBlock?: number
toBlock: number
isDev?: boolean
}) {
return CommentModel.find(
{
blockNumber: { $gt: fromBlock, $lte: toBlock },
isDev,
},
{
...defaultPostProjection,
replyToComment: 1,
replyToPost: 1,
}
).populate([
{
path: 'replyToComment',
select: defaultPostProjection,
},
{
path: 'replyToPost',
select: defaultPostProjection,
},
])
}
9 changes: 3 additions & 6 deletions src/models/Post.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
import { ModelOptions, getModelForClass, prop } from '@typegoose/typegoose'
import { TimeStamps } from '@typegoose/typegoose/lib/defaultClasses'
import { generateRandomName } from '@big-whale-labs/backend-utils'

@ModelOptions({
schemaOptions: { id: false, timestamps: true, virtuals: true },
schemaOptions: { id: false, timestamps: true },
})
export class Post extends TimeStamps {
@prop({ required: true })
Expand All @@ -26,10 +25,8 @@ export class Post extends TimeStamps {
public sender!: string
@prop({ required: true })
public numberOfComments!: number

public get username() {
return generateRandomName(this.sender)
}
@prop({ required: true })
public username!: string
}

export const PostModel = getModelForClass(Post)
Expand Down
Loading