Skip to content

Commit

Permalink
Event queries optimalizations, fix gitignored v1 and v2 schemas
Browse files Browse the repository at this point in the history
  • Loading branch information
Lezek123 committed Feb 22, 2023
1 parent 314ca8b commit 87f3ccb
Show file tree
Hide file tree
Showing 16 changed files with 48,138 additions and 6,649 deletions.
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,4 @@
# IDE files
/.idea
src/model/generated
schema.graphql
/schema.graphql

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

61 changes: 53 additions & 8 deletions db/migrations/2000000000000-Views.js
Original file line number Diff line number Diff line change
Expand Up @@ -173,17 +173,62 @@ module.exports = class Views2000000000000 {
AND ("data"->>'bid' IS NULL OR EXISTS(SELECT 1 FROM "bid" WHERE "id"="data"->>'bid'))
AND ("data"->>'comment' IS NULL OR EXISTS(SELECT 1 FROM "comment" WHERE "id"="data"->>'comment'))
`)
// Notifications view: All notifications except those related to events
// that are not part of the `events` view
await db.query(`ALTER TABLE "notification" SET SCHEMA "processor"`)
await db.query(`
CREATE VIEW "notification" AS
SELECT *
FROM "processor"."notification"
WHERE
EXISTS(SELECT 1 FROM "event" WHERE "id"="event_id")
`)
// Nft history entry view: All nft history entries except those related to events
// that are not part of the `events` view
await db.query(`ALTER TABLE "nft_history_entry" SET SCHEMA "processor"`)
await db.query(`
CREATE VIEW "nft_history_entry" AS
SELECT *
FROM "processor"."nft_history_entry"
WHERE
EXISTS(SELECT 1 FROM "event" WHERE "id"="event_id")
`)
// Nft activity view: All nft activities except those related to events
// that are not part of the `events` view
await db.query(`ALTER TABLE "nft_activity" SET SCHEMA "processor"`)
await db.query(`
CREATE VIEW "nft_activity" AS
SELECT *
FROM "processor"."nft_activity"
WHERE
EXISTS(SELECT 1 FROM "event" WHERE "id"="event_id")
`)
}

async down(db) {
await db.query(`DROP VIEW "channel"`)
await db.query(`ALTER TABLE "processor"."channel" SET SCHEMA "public"`)
await db.query(`DROP VIEW "video"`)
await db.query(`ALTER TABLE "processor"."video" SET SCHEMA "public"`)
await db.query(`DROP VIEW "video_category"`)
await db.query(`ALTER TABLE "processor"."video_category" SET SCHEMA "public"`)
await db.query(`DROP VIEW "owned_nft"`)
await db.query(`ALTER TABLE "processor"."owned_nft" SET SCHEMA "public"`)
const views = [
"channel",
"video",
"video_category",
"owned_nft",
"auction",
"bid",
"comment",
"comment_reaction",
"license",
"video_media_metadata",
"video_media_encoding",
"video_reaction",
"video_subtitle",
"event",
"notification",
"nft_history_entry",
"nft_activity"
]
for (const viewName of views) {
await db.query(`DROP VIEW "${viewName}"`)
await db.query(`ALTER TABLE "processor"."${viewName}" SET SCHEMA "public"`)
}
await db.query(`DROP SCHEMA "processor"`)
}
}
33 changes: 33 additions & 0 deletions schema/events.graphql
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,39 @@ type Event @entity {
data: EventData!
}

type Notification @entity {
"Autoincremented"
id: ID!

"Member that should recieve the notification"
member: Membership!

"The notification event"
event: Event!
}

type NftHistoryEntry @entity {
"Autoincremented"
id: ID!

"The NFT the event relates to"
nft: OwnedNft!

"Nft-related event"
event: Event!
}

type NftActivity @entity {
"Autoincremented"
id: ID!

"The member the activity relates to"
member: Membership!

"Nft-related activity"
event: Event!
}

union EventData =
CommentCreatedEventData
| CommentTextUpdatedEventData
Expand Down
13 changes: 12 additions & 1 deletion src/mappings/content/commentsAndReactions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,10 +38,12 @@ import {
import { config, ConfigVariable } from '../../utils/config'
import { EntityManagerOverlay, Flat } from '../../utils/overlay'
import {
addNotification,
backwardCompatibleMetaID,
genericEventFields,
metaprotocolTransactionFailure,
} from '../utils'
import { getChannelOwnerMemberByChannelId } from './utils'

function parseVideoReaction(reaction: ReactVideo.Reaction): VideoReactionOptions {
const protobufReactionToGraphqlReaction = {
Expand Down Expand Up @@ -403,14 +405,23 @@ export async function processCreateCommentMessage(
})

// add CommentCreated event
overlay.getRepository(Event).new({
const event = overlay.getRepository(Event).new({
...genericEventFields(overlay, block, indexInBlock, txHash),
data: new CommentCreatedEventData({
comment: comment.id,
text: body,
}),
})

if (parentComment) {
// Notify parent comment author
addNotification(overlay, [parentComment.authorId], event.id)
} else {
// Notify channel owner
const channelOwnerMemberId = await getChannelOwnerMemberByChannelId(overlay, channelId)
addNotification(overlay, [channelOwnerMemberId], event.id)
}

return new MetaprotocolTransactionResultCommentCreated({ commentCreated: comment.id })
}

Expand Down
Loading

0 comments on commit 87f3ccb

Please sign in to comment.