1
1
import { Events , ChannelType } from "discord.js" ;
2
- import type { Client , Message , PartialMessage , TextChannel } from "discord.js" ;
2
+ import type { Client , Message , TextChannel } from "discord.js" ;
3
3
import db from "#~/db.server" ;
4
- import {
5
- parseMarkdownBlocks ,
6
- getChars ,
7
- getWords ,
8
- } from "#~/helpers/messageParsing" ;
9
- import { partition } from "lodash-es" ;
10
4
import { log , trackPerformance } from "#~/helpers/observability" ;
11
5
import { threadStats } from "#~/helpers/metrics" ;
12
-
13
- export type CodeStats = {
14
- chars : number ;
15
- words : number ;
16
- lines : number ;
17
- lang : string | undefined ;
18
- } ;
6
+ import { getMessageStats } from "#~/helpers/discord.js" ;
19
7
20
8
export async function startActivityTracking ( client : Client ) {
21
9
log ( "info" , "ActivityTracker" , "Starting activity tracking" , {
@@ -100,6 +88,8 @@ export async function startActivityTracking(client: Client) {
100
88
. insertInto ( "message_stats" )
101
89
. values ( {
102
90
...info ,
91
+ code_stats : JSON . stringify ( info . code_stats ) ,
92
+ link_stats : JSON . stringify ( info . link_stats ) ,
103
93
message_id : msg . id ,
104
94
author_id : msg . author . id ,
105
95
guild_id : msg . guildId ,
@@ -116,8 +106,8 @@ export async function startActivityTracking(client: Client) {
116
106
channelId : msg . channelId ,
117
107
charCount : info . char_count ,
118
108
wordCount : info . word_count ,
119
- hasCode : info . code_stats !== "[]" ,
120
- hasLinks : info . link_stats !== "[]" ,
109
+ hasCode : info . code_stats . length > 0 ,
110
+ hasLinks : info . link_stats . length > 0 ,
121
111
} ) ;
122
112
123
113
// Track message in business analytics
@@ -131,7 +121,13 @@ export async function startActivityTracking(client: Client) {
131
121
const info = await getMessageStats ( msg ) ;
132
122
if ( ! info ) return ;
133
123
134
- await updateStatsById ( msg . id ) . set ( info ) . execute ( ) ;
124
+ await updateStatsById ( msg . id )
125
+ . set ( {
126
+ ...info ,
127
+ code_stats : JSON . stringify ( info . code_stats ) ,
128
+ link_stats : JSON . stringify ( info . link_stats ) ,
129
+ } )
130
+ . execute ( ) ;
135
131
136
132
log ( "debug" , "ActivityTracker" , "Message stats updated" , {
137
133
messageId : msg . id ,
@@ -206,75 +202,6 @@ function updateStatsById(id: string) {
206
202
return db . updateTable ( "message_stats" ) . where ( "message_id" , "=" , id ) ;
207
203
}
208
204
209
- async function getMessageStats ( msg : Message | PartialMessage ) {
210
- return trackPerformance (
211
- "startActivityTracking: getMessageStats" ,
212
- async ( ) => {
213
- const { content } = await msg . fetch ( ) ;
214
-
215
- const blocks = parseMarkdownBlocks ( content ) ;
216
-
217
- // TODO: groupBy would be better here, but this was easier to keep typesafe
218
- const [ textblocks , nontextblocks ] = partition (
219
- blocks ,
220
- ( b ) => b . type === "text" ,
221
- ) ;
222
- const [ links , codeblocks ] = partition (
223
- nontextblocks ,
224
- ( b ) => b . type === "link" ,
225
- ) ;
226
-
227
- const linkStats = links . map ( ( link ) => link . url ) ;
228
-
229
- const { wordCount, charCount } = [ ...links , ...textblocks ] . reduce (
230
- ( acc , block ) => {
231
- const content =
232
- block . type === "link" ? ( block . label ?? "" ) : block . content ;
233
- const words = getWords ( content ) . length ;
234
- const chars = getChars ( content ) . length ;
235
- return {
236
- wordCount : acc . wordCount + words ,
237
- charCount : acc . charCount + chars ,
238
- } ;
239
- } ,
240
- { wordCount : 0 , charCount : 0 } ,
241
- ) ;
242
-
243
- const codeStats = codeblocks . map ( ( block ) : CodeStats => {
244
- switch ( block . type ) {
245
- case "fencedcode" : {
246
- const content = block . code . join ( "\n" ) ;
247
- return {
248
- chars : getChars ( content ) . length ,
249
- words : getWords ( content ) . length ,
250
- lines : block . code . length ,
251
- lang : block . lang ,
252
- } ;
253
- }
254
- case "inlinecode" : {
255
- return {
256
- chars : getChars ( block . code ) . length ,
257
- words : getWords ( block . code ) . length ,
258
- lines : 1 ,
259
- lang : undefined ,
260
- } ;
261
- }
262
- }
263
- } ) ;
264
-
265
- const values = {
266
- char_count : charCount ,
267
- word_count : wordCount ,
268
- code_stats : JSON . stringify ( codeStats ) ,
269
- link_stats : JSON . stringify ( linkStats ) ,
270
- react_count : msg . reactions . cache . size ,
271
- sent_at : msg . createdTimestamp ,
272
- } ;
273
- return values ;
274
- } ,
275
- ) ;
276
- }
277
-
278
205
export async function reportByGuild ( guildId : string ) {
279
206
return trackPerformance (
280
207
"reportByGuild" ,
0 commit comments