@@ -59,48 +59,60 @@ function qa_mailing_stop()
5959function qa_mailing_perform_step ()
6060{
6161 require_once QA_INCLUDE_DIR . 'db/users.php ' ;
62+ require_once QA_INCLUDE_DIR . 'app/users.php ' ;
6263
6364 $ lastuserid = qa_opt ('mailing_last_userid ' );
6465
65- if (strlen ($ lastuserid )) {
66- $ thistime = time ();
67- $ lasttime = qa_opt ('mailing_last_timestamp ' );
68- $ perminute = qa_opt ('mailing_per_minute ' );
66+ if (strlen ($ lastuserid ) == 0 ) {
67+ return ;
68+ }
69+
70+ $ thistime = time ();
71+ $ lasttime = qa_opt ('mailing_last_timestamp ' );
72+ $ perminute = qa_opt ('mailing_per_minute ' );
6973
70- if (($ lasttime - $ thistime ) > 60 ) // if it's been a while, we assume there hasn't been continuous mailing...
71- $ lasttime = $ thistime - 1 ; // ... so only do 1 second's worth
72- else // otherwise...
73- $ lasttime = max ($ lasttime , $ thistime - 6 ); // ... don't do more than 6 seconds' worth
74+ if (($ lasttime - $ thistime ) > 60 ) // if it's been a while, we assume there hasn't been continuous mailing...
75+ $ lasttime = $ thistime - 1 ; // ... so only do 1 second's worth
76+ else // otherwise...
77+ $ lasttime = max ($ lasttime , $ thistime - 6 ); // ... don't do more than 6 seconds' worth
7478
75- $ count = min (floor (($ thistime - $ lasttime ) * $ perminute / 60 ), 100 ); // don't do more than 100 messages at a time
79+ $ count = min (floor (($ thistime - $ lasttime ) * $ perminute / 60 ), 100 ); // don't do more than 100 messages at a time
7680
77- if ($ count > 0 ) {
78- qa_opt ( ' mailing_last_timestamp ' , $ thistime + 30 ) ;
79- // prevents a parallel call to qa_mailing_perform_step() from sending messages, unless we're very unlucky with timing (poor man's mutex)
81+ if ($ count == 0 ) {
82+ return ;
83+ }
8084
81- $ sentusers = 0 ;
82- $ users = qa_db_users_get_mailing_next ( $ lastuserid , $ count );
85+ qa_opt ( ' mailing_last_timestamp ' , $ thistime + 30 ) ;
86+ // prevents a parallel call to qa_mailing_perform_step() from sending messages, unless we're very unlucky with timing (poor man's mutex)
8387
84- if (count ($ users )) {
85- foreach ($ users as $ user ) {
86- $ lastuserid = max ($ lastuserid , $ user ['userid ' ]);
87- }
88+ $ sentusers = 0 ;
89+ $ users = qa_db_users_get_mailing_next ($ lastuserid , $ count );
8890
89- qa_opt ('mailing_last_userid ' , $ lastuserid );
90- qa_opt ('mailing_done_users ' , qa_opt ('mailing_done_users ' ) + count ($ users ));
91+ if (count ($ users )) {
92+ foreach ($ users as $ user ) {
93+ $ lastuserid = max ($ lastuserid , $ user ['userid ' ]);
94+ }
9195
92- foreach ($ users as $ user ) {
93- if (!($ user ['flags ' ] & QA_USER_FLAGS_NO_MAILINGS )) {
94- qa_mailing_send_one ($ user ['userid ' ], $ user ['handle ' ], $ user ['email ' ], $ user ['emailcode ' ]);
95- $ sentusers ++;
96- }
97- }
96+ qa_opt ('mailing_last_userid ' , $ lastuserid );
97+ qa_opt ('mailing_done_users ' , qa_opt ('mailing_done_users ' ) + count ($ users ));
9898
99- qa_opt ('mailing_last_timestamp ' , $ lasttime + $ sentusers * 60 / $ perminute ); // can be floating point result, based on number of mails actually sent
99+ $ isModeratingUsers = qa_opt ('moderate_users ' );
100100
101- } else
102- qa_mailing_stop ();
101+ foreach ($ users as $ user ) {
102+ if (($ user ['flags ' ] & QA_USER_FLAGS_NO_MAILINGS ) || // exclude users who don't want to get the mailings
103+ ($ user ['flags ' ] & QA_USER_FLAGS_USER_BLOCKED ) || // exclude blocked users
104+ ($ isModeratingUsers && ($ user ['level ' ] < QA_USER_LEVEL_APPROVED ))) { // if moderating users exclude unapproved users
105+ continue ;
106+ }
107+
108+ qa_mailing_send_one ($ user ['userid ' ], $ user ['handle ' ], $ user ['email ' ], $ user ['emailcode ' ]);
109+ $ sentusers ++;
103110 }
111+
112+ qa_opt ('mailing_last_timestamp ' , $ lasttime + $ sentusers * 60 / $ perminute ); // can be floating point result, based on number of mails actually sent
113+
114+ } else {
115+ qa_mailing_stop ();
104116 }
105117}
106118
0 commit comments