/** * @param string $screenname screenname or array of screennames to pull from * If not specified, checks all queues in the system. */ public static function top($screenname = null) { $wm = new Msn_waiting_message(); if ($screenname) { if (is_array($screenname)) { // @fixme use safer escaping $list = implode("','", array_map('addslashes', $screenname)); $wm->whereAdd("screenname in ('{$list}')"); } else { $wm->screenname = $screenname; } } $wm->orderBy('created'); $wm->whereAdd('claimed is null'); $wm->limit(1); $cnt = $wm->find(true); if ($cnt) { // XXX: potential race condition // can we force it to only update if claimed is still null // (or old)? common_log(LOG_INFO, 'claiming msn waiting message id = ' . $wm->id); $orig = clone $wm; $wm->claimed = common_sql_now(); $result = $wm->update($orig); if ($result) { common_log(LOG_INFO, 'claim succeeded.'); return $wm; } else { common_log(LOG_INFO, 'claim failed.'); } } $wm = null; return null; }
/** * Enters a message into the database for sending via a callback * when the session is established * * @param string $to Intended recipient * @param string $message Message */ protected function enqueue_waiting_message($to, $message) { $wm = new Msn_waiting_message(); $wm->screenname = $to; $wm->message = $message; $wm->created = common_sql_now(); $result = $wm->insert(); if (!$result) { common_log_db_error($wm, 'INSERT', __FILE__); // TRANS: Server exception thrown when a message to be sent through MSN cannot be added to the database queue. throw new ServerException(_m('Database error inserting queue item.')); } return true; }