summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authoruckelman <uckelman@nomic.net>2010-04-30 16:41:26 +0000
committeruckelman <uckelman@nomic.net>2010-04-30 16:41:26 +0000
commit07ad87efa0f7f5ff0e503efb16c26314ffb1d31f (patch)
treeb96636855e9fba5dfa55af67cb78764b15dd4855
parent03c94fad7650f4075ee2d289f8b985cd8f8daef7 (diff)
Refactored message registration.
git-svn-id: https://vassalengine.svn.sourceforge.net/svnroot/vassalengine/site-src/trunk@6783 67b53d14-2c14-4ace-a08f-0dab2b34000c
-rw-r--r--src/Bridge.php36
-rw-r--r--src/F2M.php12
-rw-r--r--test/BridgeTest.php65
3 files changed, 77 insertions, 36 deletions
diff --git a/src/Bridge.php b/src/Bridge.php
index 7f8996c..719e91c 100644
--- a/src/Bridge.php
+++ b/src/Bridge.php
@@ -88,43 +88,41 @@ class Bridge {
return $this->db->lastInsertId();
}
- public function registerMessage($postId, $messageId, $inReplyTo) {
+ public function registerByEditId($editId, $messageId, $inReplyTo) {
throw_if_null($messageId);
- $sql = 'INSERT IGNORE INTO posts ' .
- '(post_id, message_id, in_reply_to) ' .
- 'VALUES (' . ($postId === null ? 'NULL' : $postId) . ', '
- . $this->db->quote($messageId) . ', '
- . $this->quote($inReplyTo) . ')';
+ $sql = 'UPDATE posts SET ' .
+ 'message_id = ' . $this->db->quote($messageId) . ', ' .
+ 'in_reply_to = ' . $this->quote($inReplyTo) . ') ' .
+ 'WHERE edit_id = ' . $editId;
$count = $this->db->exec($sql);
return $count == 1;
}
- public function unregisterMessage($messageId) {
+ public function registerByMessageId($messageId, $inReplyTo) {
throw_if_null($messageId);
- $sql = 'DELETE FROM posts WHERE message_id = ' .
- $this->db->quote($messageId);
+ $sql = 'INSERT IGNORE INTO posts ' .
+ '(message_id, in_reply_to) ' .
+ 'VALUES (' .
+ $this->db->quote($messageId) . ', ' .
+ $this->quote($inReplyTo) .
+ ')'
$count = $this->db->exec($sql);
-
- if ($count != 1) {
- throw new Exception('Failed to delete message id: ' . $messageId);
- }
+ return $count == 1 ? $this->db->lastInsertId() : false;
}
- public function unregisterPost($postId) {
- throw_if_null($postId);
+ public function unregisterMessage($editId) {
+ throw_if_null($messageId);
-# FIXME: this might need adjusting now that there can be more than one
-# message id per post id.
- $sql = 'DELETE FROM posts WHERE post_id = ' . $postId;
+ $sql = 'DELETE FROM posts WHERE edit_id = ' . $editId;
$count = $this->db->exec($sql);
if ($count != 1) {
- throw new Exception('Failed to delete post id: ' . $postId);
+ throw new Exception('Failed to delete edit id: ' . $editId);
}
}
diff --git a/src/F2M.php b/src/F2M.php
index 74d0725..68d2a12 100644
--- a/src/F2M.php
+++ b/src/F2M.php
@@ -53,7 +53,6 @@ function send_post_to_lists($config, $user, $mode, $data, $post_data) {
}
$date = date(DATE_RFC2822, $time);
- $messageId = build_message_id($time, $postId, $_SERVER['SERVER_NAME']);
$inReplyTo = null;
$references = null;
@@ -172,13 +171,14 @@ EOF;
$body = $msg['body'];
}
- $mailer = Mail::factory('sendmail');
+ $editId = $bridge->reserveEditId($postId);
+ $messageId = build_message_id($postId, $editId,
+ $time, $_SERVER['SERVER_NAME']);
-# FIXME: Message-id should include the edit_id. To do that, we have to
-# register BEFORE creating the Message-id
+ $mailer = Mail::factory('sendmail');
# Register the message
- $seen = !$bridge->registerMessage($postId, $messageId, $inReplyTo);
+ $seen = !$bridge->registerByEditId($editId, $messageId, $inReplyTo);
if ($seen) {
throw new Exception('message id already seen: ' . $messageId);
}
@@ -192,7 +192,7 @@ EOF;
}
catch (Exception $e) {
# Bridging failed, unregister message.
- $bridge->unregisterMessage($messageId);
+ $bridge->unregisterMessage($editId);
throw $e;
}
}
diff --git a/test/BridgeTest.php b/test/BridgeTest.php
index 413a99e..bf13ee0 100644
--- a/test/BridgeTest.php
+++ b/test/BridgeTest.php
@@ -106,25 +106,26 @@ class BridgeTest extends PHPUnit_Framework_TestCase {
public function providerReserveEditId() {
return array(
+ array(null, null, 'Exception'),
array(1, 2, null),
array(2, 2, null)
);
}
-
+
/**
- * @dataProvider providerRegisterMessage
+ * @dataProvider providerRegisterByEditId
*/
- public function testRegisterMessage($postId, $messageId, $inReplyTo,
- $expected, $ex) {
+ public function testRegisterByEditId($editId, $messageId, $inReplyTo,
+ $expected, $ex) {
if ($ex) $this->setExpectedException($ex);
$bridge = new Bridge($this->db);
$this->assertEquals(
$expected,
- $bridge->registerMessage($postId, $messageId, $inReplyTo)
+ $bridge->registerMessage($editId, $messageId, $inReplyTo)
);
}
- public function providerRegisterMessage() {
+ public function providerRegisterByEditId() {
return array(
array(null, null, null, null, 'Exception'),
array(
@@ -159,20 +160,62 @@ class BridgeTest extends PHPUnit_Framework_TestCase {
}
/**
+ * @dataProvider providerRegisterByMessageId
+ */
+ public function testRegisterByMessageId($messageId, $inReplyTo,
+ $expected, $ex) {
+ if ($ex) $this->setExpectedException($ex);
+ $bridge = new Bridge($this->db);
+ $this->assertEquals(
+ $expected,
+ $bridge->registerByMessageId($messageId, $inReplyTo)
+ );
+ }
+
+ public function providerRegisterMessageId() {
+ return array(
+ array(null, null, null, 'Exception'),
+ array(
+ '<20100302094228.33F0310091@charybdis.ellipsis.cx>',
+ null,
+ false,
+ null
+ ),
+ array(
+ '<20100302094228.33F0310091@charybdis.ellipsis.cx>',
+ null,
+ false,
+ null
+ ),
+ array(
+ '<10100302094228.33F0310091@charybdis.ellipsis.cx>',
+ null,
+ 2,
+ null
+ ),
+ array(
+ '<10100302094228.33F0310091@charybdis.ellipsis.cx>',
+ null,
+ 2,
+ null
+ )
+ );
+ }
+
+ /**
* @dataProvider providerUnregisterMessage
*/
- public function testUnregisterMessage($messageId, $ex) {
+ public function testUnregisterMessage($editId, $ex) {
if ($ex) $this->setExpectedException($ex);
$bridge = new Bridge($this->db);
- $bridge->unregisterMessage($messageId);
- $this->assertEquals(false, $bridge->getPostId($messageId));
+ $bridge->unregisterMessage($editId);
}
public function providerUnregisterMessage() {
return array(
array(null, 'Exception'),
- array('<20100302094228.33F0310091@charybdis.ellipsis.cx>', null),
- array('<10100302094228.33F0310091@charybdis.ellipsis.cx>', 'Exception')
+ array(1, null),
+ array(2, 'Exception')
);
}