Skip to content

Commit

Permalink
Improve graph-store
Browse files Browse the repository at this point in the history
  • Loading branch information
cyri113 committed Apr 10, 2024
1 parent 7cbc4ee commit a33a0e0
Show file tree
Hide file tree
Showing 17 changed files with 230 additions and 103 deletions.
11 changes: 11 additions & 0 deletions apps/graph-store/src/cyphers/chat.cypher.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
export const CREATE_OR_UPDATE_CHAT = `
MERGE(chat:TGChat { id: $chat_id })
ON CREATE
SET chat = apoc.map.merge(chat, $chat_params),
chat.created_at = timestamp(),
chat.updated_at = timestamp()
ON MATCH
SET chat = apoc.map.merge(chat, apoc.map.clean($chat_params, ['id'], [null])),
chat.updated_at = timestamp()
RETURN chat
`;
5 changes: 0 additions & 5 deletions apps/graph-store/src/cyphers/chat.cyphers.ts

This file was deleted.

13 changes: 13 additions & 0 deletions apps/graph-store/src/cyphers/joined.cypher.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
export const CREATE_OR_UPDATE_JOINED = `
MATCH (chat:TGChat { id: $chat_id })
MATCH (user:TGUser { id: $user_id })
MERGE (user)-[joined:JOINED]->(chat)
ON CREATE
SET joined = apoc.map.merge(joined, $joined_params),
joined.created_at = timestamp(),
joined.updated_at = timestamp()
ON MATCH
SET joined = apoc.map.merge(joined, apoc.map.clean($joined_params, ['id'], [null])),
joined.updated_at = timestamp()
RETURN joined
`;
10 changes: 10 additions & 0 deletions apps/graph-store/src/cyphers/mentioned.cypher.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
export const CREATED_OR_UPDATE_MENTION = `
MATCH (message:TGMessage { id: $message_id })-[:SENT_IN]->(chat:TGChat { id: $chat_id })
OPTIONAL MATCH (message)-[r:MENTIONED]->(user:TGUser)
DELETE r
WITH message, $mentions AS mentions
UNWIND mentions AS username
MATCH (user:TGUser { username: username })
MERGE (message)-[r:MENTIONED]->(user)
RETURN collect(r)
`;
12 changes: 12 additions & 0 deletions apps/graph-store/src/cyphers/message.cypher.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
export const CREATE_OR_UPDATE_MESSAGE = `
MATCH (user:TGUser { id: $user_id })-[:JOINED]->(chat:TGChat { id: $chat_id })
MERGE (user)-[:CREATED_MESSAGE]->(message:TGMessage { id: $message_id })-[:SENT_IN]->(chat)
ON CREATE
SET message = apoc.map.merge(message, $params),
message.created_at = timestamp(),
message.updated_at = timestamp()
ON MATCH
SET message = apoc.map.merge(message, apoc.map.clean($params, ['id'], [null])),
message.updated_at = timestamp()
RETURN message
`;
13 changes: 13 additions & 0 deletions apps/graph-store/src/cyphers/reaction.cypher.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
export const CREATE_OR_UPDATE_REACTION = `
MATCH (message:TGMessage { id: $message_id })-[:SENT_IN]->(chat:TGChat { id: $chat_id })
MATCH (user:TGUser { id: $user_id })
MERGE (user)-[r:REACTED_TO]->(message)
ON CREATE
SET r = apoc.map.merge(r, $params),
r.created_at = timestamp(),
r.updated_at = timestamp()
ON MATCH
SET r = apoc.map.merge(r, apoc.map.clean($params, ['id'], [null])),
r.updated_at = timestamp()
RETURN r
`;
12 changes: 12 additions & 0 deletions apps/graph-store/src/cyphers/user.cypher.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
export const CREATE_OR_UPDATE_USER = `
MATCH (chat:TGChat { id: $chat_id })
MERGE (user:TGUser { id: $user_id })
ON CREATE
SET user = apoc.map.merge(user, $user_params),
user.created_at = timestamp(),
user.updated_at = timestamp()
ON MATCH
SET user = apoc.map.merge(user, apoc.map.clean($user_params, ['id'], [null])),
user.updated_at = timestamp()
RETURN user
`;
5 changes: 0 additions & 5 deletions apps/graph-store/src/cyphers/user.cyphers.ts

This file was deleted.

10 changes: 6 additions & 4 deletions apps/graph-store/src/decorators/chat.decorator.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,15 @@
import { createParamDecorator, ExecutionContext } from '@nestjs/common';
import { Context } from 'grammy';

export const Chat = createParamDecorator(
(data: unknown, ctx: ExecutionContext) => {
const { event_data } = ctx.switchToRpc().getData();
const context: Context = ctx.getArgs()[0] as Context;
const { update } = context;

return (
event_data.chat ||
event_data.edited_message?.chat ||
event_data.message_reaction?.chat
update.message?.chat ||
update.edited_message?.chat ||
update.message_reaction?.chat
);
},
);
12 changes: 8 additions & 4 deletions apps/graph-store/src/decorators/mentions.decorator.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
import { createParamDecorator, ExecutionContext } from '@nestjs/common';
import { Context } from 'grammy';

export const Mentions = createParamDecorator(
(data: unknown, ctx: ExecutionContext): string[] => {
const { event_data } = ctx.switchToRpc().getData();
const context: Context = ctx.getArgs()[0] as Context;
const { update } = context;

const message = event_data || event_data.edited_message;
const message = update.message || update.edited_message;

const mentions = message.entities?.filter(
(entity) => entity.type === 'mention',
Expand All @@ -14,11 +16,13 @@ export const Mentions = createParamDecorator(
if (mentions) {
for (const mention of mentions) {
usernames.push(
message.text.slice(mention.offset, mention.offset + mention.length),
message.text.slice(
mention.offset + 1,
mention.offset + mention.length,
),
);
}
}

return usernames;
},
);
10 changes: 10 additions & 0 deletions apps/graph-store/src/decorators/message.decorator.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import { createParamDecorator, ExecutionContext } from '@nestjs/common';
import { Context } from 'grammy';

export const Message = createParamDecorator(
(data: unknown, ctx: ExecutionContext) => {
const context: Context = ctx.getArgs()[0] as Context;
const { update } = context;
return update.message || update.edited_message;
},
);
10 changes: 10 additions & 0 deletions apps/graph-store/src/decorators/reaction.decorator.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import { createParamDecorator, ExecutionContext } from '@nestjs/common';
import { Context } from 'grammy';

export const Reaction = createParamDecorator(
(data: unknown, ctx: ExecutionContext) => {
const context: Context = ctx.getArgs()[0] as Context;
const { update } = context;
return update.message_reaction;
},
);
10 changes: 6 additions & 4 deletions apps/graph-store/src/decorators/user.decorator.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,15 @@
import { createParamDecorator, ExecutionContext } from '@nestjs/common';
import { Context } from 'grammy';

export const User = createParamDecorator(
(data: unknown, ctx: ExecutionContext) => {
const { event_data } = ctx.switchToRpc().getData();
const context: Context = ctx.getArgs()[0] as Context;
const { update } = context;

return (
event_data.from ||
event_data.edited_message?.from ||
event_data.message_reaction?.user
update.message?.from ||
update.edited_message?.from ||
update.message_reaction?.user
);
},
);
46 changes: 27 additions & 19 deletions apps/graph-store/src/graph-store.controller.ts
Original file line number Diff line number Diff line change
@@ -1,40 +1,48 @@
import { Controller, Inject, Logger, UseInterceptors } from '@nestjs/common';
import { Controller, Logger, UseInterceptors } from '@nestjs/common';
import { GraphStoreService } from './graph-store.service';
import { ClientProxy, MessagePattern } from '@nestjs/microservices';
import { Events, Services } from '@app/common';
import { MessagePattern } from '@nestjs/microservices';
import { EventNames } from '@app/common';
import { RmqInterceptor } from './interceptors/rmq.interceptor';
import { User } from './decorators/user.decorator';
// import { Mentions } from './decorators/mentions.decorator';
import { Chat } from './decorators/chat.decorator';
import { Message } from './decorators/message.decorator';
import { Reaction } from './decorators/reaction.decorator';
import { Mentions } from './decorators/mentions.decorator';

@Controller()
@UseInterceptors(RmqInterceptor)
export class GraphStoreController {
private readonly logger = new Logger(GraphStoreController.name);

constructor(
private readonly graphStoreService: GraphStoreService,
@Inject(Services.Bot) private client: ClientProxy,
) {}
constructor(private readonly graphStoreService: GraphStoreService) {}

@MessagePattern(Events.Message)
@MessagePattern(EventNames.MESSAGE)
async message(
@Chat() chat,
@User() user,
@Message() message,
@Mentions() mentions,
// @Chat() chat,
): Promise<void> {
// Get user ids
this.client.emit('get_chat', mentions[0]);
this.logger.log(user);
this.logger.log(mentions);
this.graphStoreService.message(chat, user, message, mentions);
}

@MessagePattern(Events.EditedMessage)
async edited_message(@User() user): Promise<void> {
this.logger.log(user);
@MessagePattern(EventNames.EDITED_MESSAGE)
async edited_message(
@Chat() chat,
@User() user,
@Message() message,
@Mentions() mentions,
): Promise<void> {
this.graphStoreService.message(chat, user, message, mentions);
}

@MessagePattern(Events.MessageReaction)
async message_reaction(@User() user): Promise<void> {
this.logger.log(user);
@MessagePattern(EventNames.MESSAGE_REACTION)
async message_reaction(
@Chat() chat,
@User() user,
@Reaction() reaction,
): Promise<void> {
this.graphStoreService.reaction(chat, user, reaction);
}
}
13 changes: 1 addition & 12 deletions apps/graph-store/src/graph-store.module.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,7 @@
import { Module } from '@nestjs/common';
import { GraphStoreController } from './graph-store.controller';
import { GraphStoreService } from './graph-store.service';
import {
Queues,
RmqModule,
Services,
neo4jConfig,
rmqConfig,
schemaConfig,
} from '@app/common';
import { RmqModule, neo4jConfig, rmqConfig, schemaConfig } from '@app/common';
import { ConfigModule, ConfigService } from '@nestjs/config';
import { Neo4jModule } from 'nest-neo4j';
import { NodeHelper } from './cyphers/helpers/node.helper';
Expand All @@ -21,10 +14,6 @@ import { NodeHelper } from './cyphers/helpers/node.helper';
isGlobal: true,
}),
RmqModule,
RmqModule.register({
name: Services.Bot,
queue: Queues.Bot,
}),
Neo4jModule.forRootAsync({
import: [ConfigModule],
useFactory: (configService: ConfigService) => configService.get('neo4j'),
Expand Down
Loading

0 comments on commit a33a0e0

Please sign in to comment.