Skip to content

Commit dbb6605

Browse files
committed
feat: add transformTelegramChat helper for chat data processing
1 parent f0b432a commit dbb6605

2 files changed

Lines changed: 118 additions & 2 deletions

File tree

Lines changed: 115 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,115 @@
1+
import { Chat } from 'grammy/types';
2+
3+
// List of optional fields to stringify for all chat types
4+
const commonOptionalDict = [
5+
'photo',
6+
'pinned_message',
7+
'permissions',
8+
'active_usernames',
9+
'available_reactions',
10+
'background_custom_emoji_id',
11+
'bio',
12+
'birthdate',
13+
'business_intro',
14+
'business_location',
15+
'business_opening_hours',
16+
'can_set_sticker_set',
17+
'custom_emoji_sticker_set_name',
18+
'description',
19+
'emoji_status_custom_emoji_id',
20+
'has_aggressive_anti_spam_enabled',
21+
'has_hidden_members',
22+
'has_protected_content',
23+
'has_restricted_voice_and_video_messages',
24+
'has_visible_history',
25+
'invite_link',
26+
'join_by_request',
27+
'join_to_send_messages',
28+
'linked_chat_id',
29+
'location',
30+
'message_auto_delete_time',
31+
'personal_chat',
32+
'slow_mode_delay',
33+
'sticker_set_name',
34+
'usernames',
35+
'accepted_gift_types',
36+
];
37+
38+
// Private chat specific fields
39+
const privateChatOptionalDict = [
40+
'chat_photo',
41+
'chat_shared',
42+
];
43+
44+
// Group chat specific fields
45+
const groupChatOptionalDict = [
46+
'chat_photo',
47+
'chat_shared',
48+
];
49+
50+
// Supergroup specific fields
51+
const supergroupOptionalDict = [
52+
'chat_photo',
53+
'chat_shared',
54+
'forum_topic',
55+
'forum_topic_created',
56+
'forum_topic_edited',
57+
'forum_topic_closed',
58+
'forum_topic_reopened',
59+
'general_forum_topic_hidden',
60+
'general_forum_topic_unhidden',
61+
'giveaway_created',
62+
'giveaway',
63+
'giveaway_winners',
64+
'giveaway_completed',
65+
'video_chat_scheduled',
66+
'video_chat_started',
67+
'video_chat_ended',
68+
'video_chat_participants_invited',
69+
'web_app_data',
70+
'reply_markup',
71+
];
72+
73+
// Channel specific fields
74+
const channelOptionalDict = [
75+
'chat_photo',
76+
'chat_shared',
77+
];
78+
79+
export function transformTelegramChat(
80+
chat: Chat,
81+
): Record<string, any> {
82+
// Make a shallow copy of the chat
83+
const obj: Record<string, any> = { ...chat };
84+
85+
// Get the appropriate optional dict based on chat type
86+
let typeSpecificDict: string[] = [];
87+
if ('type' in chat) {
88+
switch (chat.type) {
89+
case 'private':
90+
typeSpecificDict = privateChatOptionalDict;
91+
break;
92+
case 'group':
93+
typeSpecificDict = groupChatOptionalDict;
94+
break;
95+
case 'supergroup':
96+
typeSpecificDict = supergroupOptionalDict;
97+
break;
98+
case 'channel':
99+
typeSpecificDict = channelOptionalDict;
100+
break;
101+
}
102+
}
103+
104+
// Combine common and type-specific fields
105+
const allOptionalFields = [...commonOptionalDict, ...typeSpecificDict];
106+
107+
// Iterate through optional fields and stringify if present
108+
allOptionalFields.forEach((dictName) => {
109+
if (obj[dictName] !== undefined && obj[dictName] !== null) {
110+
obj[dictName] = JSON.stringify(obj[dictName]);
111+
}
112+
});
113+
114+
return obj;
115+
}

src/libs/telegram/services/chat.service.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,14 @@ import { Chat } from 'grammy/types';
22
import { Transaction } from 'neo4j-driver';
33
import { CREATE_OR_UPDATE_CHAT, MIGRATE_CHAT } from '../neo4j/cyphers';
44
import { neo4jService } from '../../../libs/neo4j/Neo4jClient';
5+
import { transformTelegramChat } from '../helpers/transformTelegramChat';
56

67
class ChatService {
78
async createOrUpdate(chat: Chat, tx?: Transaction): Promise<void> {
89
if (tx) {
9-
tx.run(CREATE_OR_UPDATE_CHAT, { chat });
10+
tx.run(CREATE_OR_UPDATE_CHAT, { chat: transformTelegramChat(chat) });
1011
} else {
11-
await neo4jService.run(CREATE_OR_UPDATE_CHAT, { chat });
12+
await neo4jService.run(CREATE_OR_UPDATE_CHAT, { chat: transformTelegramChat(chat) });
1213
}
1314
}
1415

0 commit comments

Comments
 (0)