@@ -4,12 +4,12 @@ import axios from 'axios'
44import Snoowrap from 'snoowrap'
55import { PrismaClient } from '@prisma/client'
66import { CommentStream } from 'snoostorm'
7-
8- const prisma = new PrismaClient ( )
7+ import { sendNotification } from './notifications'
8+ import prisma from '@/lib/db'
99
1010// Initialize Reddit API client with environment variables
1111export const redditClient = new Snoowrap ( {
12- userAgent : 'your-app-name by /u/your-username ' ,
12+ userAgent : 'CodeBuilder by /u/taofullstack ' ,
1313 clientId : process . env . REDDIT_CLIENT_ID ,
1414 clientSecret : process . env . REDDIT_CLIENT_SECRET ,
1515 username : process . env . REDDIT_USERNAME ,
@@ -25,6 +25,9 @@ export async function storeMessages(items: Array<Snoowrap.PrivateMessage | Snoow
2525 const newMessages = [ ]
2626 console . debug ( `Processing ${ items . length } message(s)` )
2727
28+ // Retrieve all subscriptions for sending notifications
29+ const subscriptions = await prisma . subscription . findMany ( )
30+
2831 for ( const item of items ) {
2932 try {
3033 const isMessage = item instanceof Snoowrap . PrivateMessage
@@ -64,12 +67,29 @@ export async function storeMessages(items: Array<Snoowrap.PrivateMessage | Snoow
6467 contextUrl : item . permalink ,
6568 }
6669
67- const created = await prisma . redditMessage . create ( {
70+ const createdMsg = await prisma . redditMessage . create ( {
6871 data : { ...baseData , ...additionalData } ,
6972 } )
7073
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 )
7393 } catch ( error ) {
7494 console . error ( `Error processing message ${ item . name } :` , error . message )
7595 }
@@ -161,3 +181,54 @@ export const fetchRedditPosts = async (subreddits: string[]) => {
161181
162182 return posts
163183}
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