From f5fe013657ced6920e3fe794a2a8d4122a85e377 Mon Sep 17 00:00:00 2001 From: Evan Prodromou Date: Fri, 29 Aug 2008 16:03:52 -0400 Subject: many jabber queue management changes Added a method to QueueManager to let subclasses do stuff when idle. Needed so that XMPP queue manager can service its message queue. Cleaned up jabber_broadcast_message quite a bit. Use custom joins instead of loop-and-query, should fix some problems with users who are getting messages even after turning off notification. Only build $msg and $entry once, and use the XMPPHP function for messages with a payload, rather than rolling our own. darcs-hash:20080829200352-84dde-427e4ca8c81d4222a36f78e7c580b611ff0bf765.gz --- lib/jabber.php | 126 ++++++++++++++++++++++----------------------------- lib/queuehandler.php | 14 ++++-- 2 files changed, 66 insertions(+), 74 deletions(-) (limited to 'lib') diff --git a/lib/jabber.php b/lib/jabber.php index 171dff4df..b943b6f78 100644 --- a/lib/jabber.php +++ b/lib/jabber.php @@ -25,25 +25,6 @@ require_once('XMPPHP/XMPP.php'); class Laconica_XMPP extends XMPPHP_XMPP { - function messageplus($to, $body, $type = 'chat', $subject = null, $payload = null) { - $to = htmlspecialchars($to); - $body = htmlspecialchars($body); - $subject = htmlspecialchars($subject); - - $jid = jabber_daemon_address(); - - $out = ""; - if($subject) $out .= "$subject"; - $out .= "$body"; - if($payload) $out .= $payload; - $out .= ""; - - $cnt = strlen($out); - common_log(LOG_DEBUG, "Sending $cnt chars to $to"); - $this->send($out); - common_log(LOG_DEBUG, 'Done.'); - } - public function presence($status = null, $show = 'available', $to = null, $type='available', $priority=NULL) { if($type == 'available') $type = ''; $to = htmlspecialchars($to); @@ -134,7 +115,7 @@ function jabber_send_notice($to, $notice) { } $msg = jabber_format_notice($profile, $notice); $entry = jabber_format_entry($profile, $notice); - $conn->messageplus($to, $msg, 'chat', NULL, $entry); + $conn->message($to, $msg, 'chat', NULL, $entry); return true; } @@ -142,6 +123,8 @@ function jabber_send_notice($to, $notice) { function jabber_format_entry($profile, $notice) { + # FIXME: notice url might be remote + $noticeurl = common_local_url('shownotice', array('notice' => $notice->id)); $msg = jabber_format_notice($profile, $notice); @@ -168,6 +151,10 @@ function jabber_format_entry($profile, $notice) { $html .= "\n\n"; $html .= "\n\n"; + $address = "\n"; + $address .= "
\n"; + $address .= "\n"; + $event = "\n"; $event .= "\n"; @@ -175,7 +162,7 @@ function jabber_format_entry($profile, $notice) { $event .= "\n"; $event .= "\n"; # FIXME: include the pubsub event, too. - return $html . $entry; + return $html . $entry . $address; # return $entry . "\n" . $event; } @@ -234,63 +221,54 @@ function jabber_broadcast_notice($notice) { return true; } $profile = Profile::staticGet($notice->profile_id); + if (!$profile) { common_log(LOG_WARNING, 'Refusing to broadcast notice with ' . 'unknown profile ' . common_log_objstring($notice), __FILE__); return false; } + + $msg = jabber_format_notice($profile, $notice); + $entry = jabber_format_entry($profile, $notice); + $sent_to = array(); - # First, get users who this is a direct reply to - $reply = new Reply(); - $reply->notice_id = $notice->id; - if ($reply->find()) { - while ($reply->fetch()) { - $user = User::staticGet($reply->profile_id); - if ($user && $user->jabber && $user->jabbernotify && $user->jabberreplies) { - common_log(LOG_INFO, - 'Sending reply notice ' . $notice->id . ' to ' . $user->jabber, - __FILE__); - $success = jabber_send_notice($user->jabber, $notice); - if ($success) { - # Remember so we don't send twice - $sent_to[$user->id] = true; - } else { - # XXX: Not sure, but I think that's the right thing to do - common_log(LOG_WARNING, - 'Sending reply notice ' . $notice->id . ' to ' . $user->jabber . ' FAILED, cancelling.', - __FILE__); - return false; - } - } - } + $conn = jabber_connect(); + + # First, get users to whom this is a direct reply + $user = new User(); + $user->query('SELECT user.id, user.jabber ' . + 'FROM user JOIN reply ON user.id = reply.profile_id ' . + 'WHERE reply.notice_id = ' . $notice->id . ' ' . + 'AND user.jabber is not null ' . + 'AND user.jabbernotify = 1 ' . + 'AND user.jabberreplies = 1 '); + + while ($user->fetch()) { + common_log(LOG_INFO, + 'Sending reply notice ' . $notice->id . ' to ' . $user->jabber, + __FILE__); + $conn->message($user->jabber, $msg, 'chat', NULL, $entry); } + # Now, get users subscribed to this profile - # XXX: use a join here rather than looping through results - $sub = new Subscription(); - $sub->subscribed = $notice->profile_id; - - if ($sub->find()) { - while ($sub->fetch()) { - $user = User::staticGet($sub->subscriber); - if ($user && $user->jabber && $user->jabbernotify && !array_key_exists($user->id,$sent_to)) { - common_log(LOG_INFO, - 'Sending notice ' . $notice->id . ' to ' . $user->jabber, - __FILE__); - $success = jabber_send_notice($user->jabber, $notice); - if ($success) { - $sent_to[$user->id] = true; - } else { - # XXX: Not sure, but I think that's the right thing to do - common_log(LOG_WARNING, - 'Sending notice ' . $notice->id . ' to ' . $user->jabber . ' FAILED, cancelling.', - __FILE__); - return false; - } - } + + $user = new User(); + $user->query('SELECT user.id, user.jabber ' . + 'FROM user JOIN subscription ON user.id = subscription.subscriber ' . + 'WHERE subscription.subscribed = ' . $notice->profile_id . ' ' . + 'AND user.jabber is not null ' . + 'AND user.jabbernotify = 1 '); + + while ($user->fetch()) { + if (!array_key_exists($user->id, $sent_to)) { + common_log(LOG_INFO, + 'Sending notice ' . $notice->id . ' to ' . $user->jabber, + __FILE__); + $conn->message($user->jabber, $msg, 'chat', NULL, $entry); } } - + return true; } @@ -305,11 +283,17 @@ function jabber_public_notice($notice) { # = false? I think not if ($public && $notice->is_local) { + $msg = jabber_format_notice($profile, $notice); + $entry = jabber_format_entry($profile, $notice); + + $sent_to = array(); + $conn = jabber_connect(); + foreach ($public as $address) { - common_log(LOG_INFO, - 'Sending notice ' . $notice->id . ' to public listener ' . $address, - __FILE__); - jabber_send_notice($address, $notice); + common_log(LOG_INFO, + 'Sending notice ' . $notice->id . ' to public listener ' . $address, + __FILE__); + $conn->message($address, $msg, 'chat', NULL, $entry); } } diff --git a/lib/queuehandler.php b/lib/queuehandler.php index 747e7b493..470b595ba 100644 --- a/lib/queuehandler.php +++ b/lib/queuehandler.php @@ -57,7 +57,6 @@ class QueueHandler { function handle_queue() { $this->log(LOG_INFO, 'checking for queued notices'); - $cnt = 0; $transport = $this->transport(); do { $qi = Queue_item::top($transport); @@ -82,14 +81,23 @@ class QueueHandler { $this->log(LOG_WARNING, 'queue item for notice that does not exist'); } $qi->delete(); - $cnt++; + $this->idle(); } else { $this->clear_old_claims(); - sleep(10); + $start = microtime(); + $this->idle(); + $used = microtime() - $start; + if ($used < 5000000) { + usleep(5000000 - $used); + } } } while (true); } + function idle() { + return true; + } + function clear_old_claims() { $qi = new Queue_item(); $qi->transport = $this->transport(); -- cgit v1.2.3-54-g00ecf