@@ -4,12 +4,12 @@ import axios from 'axios'
4
4
import Snoowrap from 'snoowrap'
5
5
import { PrismaClient } from '@prisma/client'
6
6
import { CommentStream } from 'snoostorm'
7
-
8
- const prisma = new PrismaClient ( )
7
+ import { sendNotification } from './notifications'
8
+ import prisma from '@/lib/db'
9
9
10
10
// Initialize Reddit API client with environment variables
11
11
export const redditClient = new Snoowrap ( {
12
- userAgent : 'your-app-name by /u/your-username ' ,
12
+ userAgent : 'CodeBuilder by /u/taofullstack ' ,
13
13
clientId : process . env . REDDIT_CLIENT_ID ,
14
14
clientSecret : process . env . REDDIT_CLIENT_SECRET ,
15
15
username : process . env . REDDIT_USERNAME ,
@@ -25,6 +25,9 @@ export async function storeMessages(items: Array<Snoowrap.PrivateMessage | Snoow
25
25
const newMessages = [ ]
26
26
console . debug ( `Processing ${ items . length } message(s)` )
27
27
28
+ // Retrieve all subscriptions for sending notifications
29
+ const subscriptions = await prisma . subscription . findMany ( )
30
+
28
31
for ( const item of items ) {
29
32
try {
30
33
const isMessage = item instanceof Snoowrap . PrivateMessage
@@ -64,12 +67,29 @@ export async function storeMessages(items: Array<Snoowrap.PrivateMessage | Snoow
64
67
contextUrl : item . permalink ,
65
68
}
66
69
67
- const created = await prisma . redditMessage . create ( {
70
+ const createdMsg = await prisma . redditMessage . create ( {
68
71
data : { ...baseData , ...additionalData } ,
69
72
} )
70
73
71
- console . debug ( `Stored new ${ type } [${ created . redditId } ] from /u/${ created . author } ` )
72
- newMessages . push ( created )
74
+ // Build your unified payload object
75
+ const notificationPayload = {
76
+ title : `${ createdMsg . author } (${ createdMsg . subreddit } )` ,
77
+ body : `${ createdMsg . content } ` ,
78
+ url : createdMsg . contextUrl ,
79
+ icon : 'https://new.codebuilder.org/images/logo2.png' ,
80
+ badge : 'https://new.codebuilder.org/images/logo2.png' ,
81
+ }
82
+
83
+ // Loop and send notifications concurrently
84
+ const notificationPromises = subscriptions . map ( ( sub ) =>
85
+ sendNotification ( sub , notificationPayload )
86
+ )
87
+
88
+ // Wait for all notifications to complete
89
+ await Promise . all ( notificationPromises )
90
+
91
+ console . debug ( `Stored new ${ type } [${ createdMsg . redditId } ] from /u/${ createdMsg . author } ` )
92
+ newMessages . push ( createdMsg )
73
93
} catch ( error ) {
74
94
console . error ( `Error processing message ${ item . name } :` , error . message )
75
95
}
@@ -161,3 +181,54 @@ export const fetchRedditPosts = async (subreddits: string[]) => {
161
181
162
182
return posts
163
183
}
184
+
185
+ /**
186
+ * Stores Reddit posts in database with deduplication and sends notifications
187
+ * @param posts Array of Reddit post objects
188
+ * @returns Array of newly created database records
189
+ */
190
+ export async function storePosts ( posts : Array < any > ) {
191
+ const newPosts = [ ]
192
+ console . debug ( `Processing ${ posts . length } post(s)` )
193
+
194
+ // Retrieve all subscriptions for sending notifications
195
+ const subscriptions = await prisma . subscription . findMany ( )
196
+
197
+ for ( const post of posts ) {
198
+ try {
199
+ // Check for existing record
200
+ const existing = await prisma . post . findUnique ( {
201
+ where : { url : post . url } ,
202
+ } )
203
+
204
+ if ( existing ) {
205
+ console . debug ( `Skipping duplicate post [${ post . url } ]` )
206
+ continue
207
+ }
208
+
209
+ const createdPost = await prisma . post . create ( { data : post } )
210
+
211
+ // Build notification payload
212
+ const notificationPayload = {
213
+ title : `${ createdPost . title } (${ createdPost . subreddit } - ${ createdPost . author } )` ,
214
+ body : `${ createdPost . title } ` ,
215
+ url : createdPost . url ,
216
+ icon : 'https://new.codebuilder.org/images/logo2.png' ,
217
+ badge : 'https://new.codebuilder.org/images/logo2.png' ,
218
+ }
219
+
220
+ // Send notifications concurrently
221
+ const notificationPromises = subscriptions . map ( ( sub ) =>
222
+ sendNotification ( sub , notificationPayload )
223
+ )
224
+ await Promise . all ( notificationPromises )
225
+
226
+ console . debug ( `Stored new post [${ createdPost . url } ] from /u/${ createdPost . author } ` )
227
+ newPosts . push ( createdPost )
228
+ } catch ( error ) {
229
+ console . error ( `Error processing post ${ post . url } :` , error . message )
230
+ }
231
+ }
232
+
233
+ return newPosts
234
+ }
0 commit comments