Tags
================================


/*  Complicated tags.
--------------------------------
*/


If tags are ints, in the database:
--------------------------------

  def addRemoveTagsIfAuth(pageId: Option[PageId], postId: Option[UniquePostId],
        labels: Set[TagLabel], who: Who) {
    dieIf(pageId.isDefined == postId.isDefined, "EsE7KUF0W2")
    readWriteTransaction { transaction =>
      val me = transaction.loadTheUser(who.id)
      val pageMeta = pageId.map(transaction.loadThePageMeta)
      val post = postId.map(transaction.loadThePost)

      val oldLabelIds: Set[TagLabelId] =
        pageMeta.map({ meta =>
          throwForbiddenIf(meta.authorId != me.id && !me.isStaff,
              "EsE6JKF0", "Not your page and not staff")
          transaction.loadTagLabelIdsForPage(meta.pageId)
        }).orElse(
          post map { post =>
            throwForbiddenIf(post.createdById != me.id && !me.isStaff,
                "EsE2GKY5", "Not your post and not staff")
            transaction.loadTagLabelIdsForPost(post.uniqueId)
          })
          .getOrDie("EsE7YKPU")

      val labelIdsByText = transaction.loadTagLabelIdsByTextMap()
      val labelTextsById = Map[TagLabelId, TagLabel](labelIdsByText.iterator.map({ textAndId =>
        textAndId._2 -> textAndId._1
      }).toSeq: _*)

      val oldLabelTexts =

      val labelTextsToCreate = labels -- labelIdsByText.keySet
      val labelTextsToDeleteFromPageOrPost = oldLabelIds -- labels
      val labelIdsToDeleteFromPageOrPost = oldLabelIds -- labels
      val labelsToAddToPageOrPost = labels -- oldLabelIds
      transaction.createTagLabels(labelsToCreate)
      pageId.foreach(transaction.addTagLabelsToPage(labelsToAddToPageOrPost, _))
      postId.foreach(transaction.addTagLabelsToPost(labelsToAddToPageOrPost, _))
    }
  }


 public | tag_group_memberships           | table    | vagrant
 public | tag_groups                      | table    | vagrant
 public | tag_users                       | table    | vagrant
 public | tags                            | table    | vagrant




=== Unknown user id from -3 to -1:
-- This'll force a check of all constraints, so we'll know that -1 is gone for sure.
commit;
-- Now change the Unknown user's id from -3 to -1, because I like -1 better.
-- First delete 2 tests posts by mine.
delete from audit_log3 where doer_id = -3 and did_what = 4;  -- 4 = create post
delete from review_tasks3 where caused_by_id = -3;
delete from posts3 where created_by_id = -3;
-- Then change the id.
set constraints all deferred;
update audit_log3 set doer_id = -1 where doer_id = -3;
update post_actions3 set created_by_id = -1 where created_by_id = -3;
update post_read_stats3 set user_id = -1 where user_id = -3;
update users3 set user_id = -1 where user_id = -3;



