@@ -8,6 +8,7 @@ const pendingFollowBackPath = path.join(
8
8
"../data/pending_follow_back.json"
9
9
) ;
10
10
const unfollowQueuePath = path . join ( __dirname , "../data/unfollow_queue.json" ) ;
11
+ const configPath = path . join ( __dirname , "../config.json" ) ;
11
12
12
13
// Function to remove users from follow_queue.json if they are present in the followedUsersSet
13
14
async function cleanFollowQueue ( followedUsersSet ) {
@@ -62,6 +63,85 @@ async function nextInFollowQueue() {
62
63
}
63
64
}
64
65
66
+ // Function to check if the user's follow back period has expired and move them to unfollow queue
67
+ async function moveExpiredUsersToUnfollowQueue ( ) {
68
+ try {
69
+ // Read and parse pending_follow_back.json
70
+ const pendingFollowBackData = await fs . readFile (
71
+ pendingFollowBackPath ,
72
+ "utf8"
73
+ ) ;
74
+ const pendingFollowBack = JSON . parse ( pendingFollowBackData ) ;
75
+
76
+ // Read and parse config.json
77
+ const configData = await fs . readFile ( configPath , "utf8" ) ;
78
+ const config = JSON . parse ( configData ) ;
79
+ const pendingFollowBackWaitTimeDays =
80
+ config . configValues . pendingFollowBackWaitTimeDays ;
81
+
82
+ // Read or initialize unfollow_queue.json
83
+ let unfollowQueue = { } ;
84
+ try {
85
+ const unfollowQueueData = await fs . readFile ( unfollowQueuePath , "utf8" ) ;
86
+ unfollowQueue = JSON . parse ( unfollowQueueData ) ;
87
+ } catch ( error ) {
88
+ console . log ( "Unfollow queue file not found, starting a new one." ) ;
89
+ }
90
+
91
+ // Get the current date
92
+ const currentDate = new Date ( ) ;
93
+
94
+ // Loop through pending_follow_back.json and check expiration
95
+ for ( const userId in pendingFollowBack ) {
96
+ if ( pendingFollowBack . hasOwnProperty ( userId ) ) {
97
+ const user = pendingFollowBack [ userId ] ;
98
+
99
+ // Calculate the expiration date based on the followed_on date
100
+ const followedOnDate = new Date ( user . followed_on ) ;
101
+ const expirationDate = new Date ( followedOnDate ) ;
102
+ expirationDate . setDate (
103
+ followedOnDate . getDate ( ) + pendingFollowBackWaitTimeDays
104
+ ) ;
105
+
106
+ // Check if the user has exceeded the expiration date
107
+ if ( currentDate > expirationDate ) {
108
+ console . log (
109
+ `User ${ user . login } exceeded the follow back period, moving to unfollow queue.`
110
+ ) ;
111
+
112
+ // Add the user to the unfollow queue with a timestamp
113
+ unfollowQueue [ user . id ] = {
114
+ ...user ,
115
+ added_to_queue_on : new Date ( ) . toISOString ( ) , // Add a timestamp for when the user was added
116
+ } ;
117
+
118
+ // Remove the user from pending_follow_back.json
119
+ delete pendingFollowBack [ userId ] ;
120
+ }
121
+ }
122
+ }
123
+
124
+ // Write the updated unfollow_queue.json
125
+ await fs . writeFile (
126
+ unfollowQueuePath ,
127
+ JSON . stringify ( unfollowQueue , null , 4 ) ,
128
+ "utf8"
129
+ ) ;
130
+ // Write the updated pending_follow_back.json
131
+ await fs . writeFile (
132
+ pendingFollowBackPath ,
133
+ JSON . stringify ( pendingFollowBack , null , 4 ) ,
134
+ "utf8"
135
+ ) ;
136
+
137
+ console . log (
138
+ "Users successfully moved to the unfollow queue and pending follow back list updated."
139
+ ) ;
140
+ } catch ( error ) {
141
+ console . error ( "Error moving expired users to unfollow queue:" , error ) ;
142
+ }
143
+ }
144
+
65
145
// Function to return the next user object from the unfollow queue
66
146
async function nextInUnfollowQueue ( ) {
67
147
try {
@@ -196,6 +276,7 @@ module.exports = {
196
276
removeUserFromFollowQueue,
197
277
addUserToPendingFollowBack,
198
278
cleanFollowQueue,
279
+ moveExpiredUsersToUnfollowQueue,
199
280
} ;
200
281
// Example usage
201
282
// nextInQueue().then(follower => console.log("Next follower to process:", follower));
0 commit comments