Index: /trunk/Bookmarks.pm
===================================================================
--- /trunk/Bookmarks.pm	(revision 27)
+++ /trunk/Bookmarks.pm	(revision 28)
@@ -154,5 +154,4 @@
     my $uri = $bookmark->{uri};
     my $title = $bookmark->{title};
-    #TODO: accept a ctime or mtime
     my $ctime = $bookmark->{ctime} || time;
     my $mtime = $bookmark->{mtime} || $ctime;
@@ -176,4 +175,5 @@
 
     # create the bookmark
+    my $bookmark_exists = 0;
     my ($sql_bookmark, @bind_bookmark) = sql_interp(
         'insert into bookmarks', { ($id ? (id => $id) : ()), uri => $uri, ctime => $ctime, mtime => $mtime }
@@ -186,8 +186,6 @@
         if ($@ =~ /column uri is not unique/) {
             # this is not truly an error condition; the bookmark was already there
-            # update the mtime instead
-            # TODO: only update mtime if the tag list is changed?
-            my $sth_update = $self->dbh->prepare('update bookmarks set mtime = ? where uri = ?');
-            $sth_update->execute($mtime, $uri);
+            # set this flag so that later we can update the mtime if tags change
+            $bookmark_exists = 1;
         } else {
             die $@;
@@ -195,4 +193,5 @@
     }
 
+    my $changed_tags = 0;
     my %new_tags = map { $_ => 1 } @{ $bookmark->{tags} };
     my $sth_delete_tag = $self->dbh->prepare('delete from tags where uri = ? and tag = ?');
@@ -204,4 +203,5 @@
             # if a current tag is not in the new tags, remove it from the database
             $sth_delete_tag->execute($uri, $tag);
+            $changed_tags++;
         } else {
             # if a new tag is already in the database, remove it from the list of tags to add
@@ -211,29 +211,12 @@
     for my $tag (keys %new_tags) {
         $sth_insert_tag->execute($uri, $tag);
-    }
-
-=begin
-
-    # clear all tags
-    my $sth_delete_tag = $self->dbh->prepare('delete from tags where uri = ?');
-    $sth_delete_tag->execute($uri);
-    my $sth_tag = $self->dbh->prepare('insert into tags (uri, tag) values (?, ?)');
-    for my $tag (@{ $bookmark->{tags} }) {
-        #print $tag, "\n";
-        # prevent duplicate (uri,tag) pairs in the database
-        # TODO: should POST with a set of tags ever remove tags?
-        eval {
-            $sth_tag->execute($uri, $tag);
-        };
-        if ($@) {
-            if ($@ =~ /columns uri, tag are not unique/) {
-                # this is not truly an error condition; the tag was already there
-            } else {
-                die $@;
-            }
-        }
-    }
-
-=cut
+        $changed_tags++;
+    }
+
+    if ($bookmark_exists && $changed_tags) {
+        # update the mtime if the bookmark already existed but the tags were changed
+        my $sth_update = $self->dbh->prepare('update bookmarks set mtime = ? where uri = ?');
+        $sth_update->execute($mtime, $uri);
+    }
 
     # return the newly created or updated bookmark
