-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
17 changed files
with
230 additions
and
103 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
`; |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
`; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
`; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
`; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
`; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
`; |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
); | ||
}, | ||
); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
}, | ||
); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
}, | ||
); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
); | ||
}, | ||
); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.