stComment
    :: (UTCTime
        -> DiscussionId
        -> Maybe CommentId
        -> UserId
        -> Markdown
        -> Int
        -> Visibility
        -> Language
        -> SDB CommentId)
    -> UserId
    -> Maybe CommentId
    -> DiscussionId
    -> Markdown
    -> Visibility
    -> Language
    -> SDB (Either Text CommentId)
postComment
        insert_comment
        user_id
        mparent_id
        discussion_id
        contents
        visibility
        language = do
    case parseTicket $ unMarkdown contents of
        Left err -> return $ Left err
        Right mnew_ticket -> do
            (now, depth) <- lift $ (,)
                <$> liftIO getCurrentTime
                <*> fetchCommentDepthFromMaybeParentIdDB mparent_id

            let content_with_tags    = unMarkdown contents
                content_without_tags = Markdown $ stripTags content_with_tags

            comment_id <- insert_comment now discussion_id mparent_id user_id
                              content_without_tags depth visibility language

            lift $ do
                F.forM_ mnew_ticket $ \new_ticket ->
                    insert_ $ Ticket now now (unTicketName new_ticket) comment_id
                insertTagsDB user_id comment_id $ parseTags content_with_tags

                case mparent_id of
                    Nothing -> return ()
                    Just parent_id ->
                        mapM_ (insert_ . CommentAncestor comment_id)
                        =<< (parent_id:) <$> fetchCommentAncestorsDB parent_id

                update $ \t -> do
                 set t [TicketUpdatedTs =. val now]
                 where_ (t ^. TicketComment `in_` subFetchCommentAncestorsDB comment_id)

            return $ Right comment_id

-- | Filter a list of comments per the provided permission filter.
f
postComment
    :: (UTCTime
        -> DiscussionId
        -> Maybe CommentId
        -> UserId
        -> Markdown
        -> Int
        -> Visibility
        -> Language
        -> SDB CommentId)
    -> UserId
    -> Maybe CommentId
    -> DiscussionId
    -> Markdown
    -> Visibility
    -> Language
    -> SDB (Either Text CommentId)
postComment
        insert_comment
        user_id
        mparent_id
        discussion_id
        contents
        visibility
        language = do
    case parseTicket $ unMarkdown contents of
        Left err -> return $ Left err
        Right mnew_ticket -> do
            (now, depth) <- lift $ (,)
                <$> liftIO getCurrentTime
                <*> fetchCommentDepthFromMaybeParentIdDB mparent_id

            let content_with_tags    = unMarkdown contents
                content_without_tags = Markdown $ stripTags content_with_tags

            comment_id <- insert_comment now discussion_id mparent_id user_id
                              content_without_tags depth visibility language

            lift $ do
                F.forM_ mnew_ticket $ \new_ticket ->
                    insert_ $ Ticket now now (unTicketName new_ticket) comment_id
                insertTagsDB user_id comment_id $ parseTags content_with_tags

                case mparent_id of
                    Nothing -> return ()
                    Just parent_id ->
                        mapM_ (insert_ . CommentAncestor comment_id)
                        =<< (parent_id:) <$> fetchCommentAncestorsDB parent_id

                update $ \t -> do
                 set t [TicketUpdatedTs =. val now]
                 where_ (t ^. TicketComment `in_` subFetchCommentAncestorsDB comment_id)

            return $ Right comment_id

-- | Filter a list of comments per the provided permission filter.
postComment
        insert_comment
        user_id
        mparent_id
        discussion_id
        contents
        visibility
        language = do
    case parseTicket $ unMarkdown contents of
        Left err -> return $ Left err
        Right mnew_ticket -> do
            (now, depth) <- lift $ (,)
                <$> liftIO getCurrentTime
                <*> fetchCommentDepthFromMaybeParentIdDB mparent_id

            let content_with_tags    = unMarkdown contents
                content_without_tags = Markdown $ stripTags content_with_tags

            comment_id <- insert_comment now discussion_id mparent_id user_id
                              content_without_tags depth visibility language

            lift $ do
                F.forM_ mnew_ticket $ \new_ticket ->
                    insert_ $ Ticket now now (unTicketName new_ticket) comment_id
                insertTagsDB user_id comment_id $ parseTags content_with_tags

                case mparent_id of
                    Nothing -> return ()
                    Just parent_id ->
                        mapM_ (insert_ . CommentAncestor comment_id)
                        =<< (parent_id:) <$> fetchCommentAncestorsDB parent_id

                update $ \t -> do
                 set t [TicketUpdatedTs =. val now]
                 where_ (t ^. TicketComment `in_` subFetchCommentAncestorsDB comment_id)

            return $ Right comment_id

-- | Filter a list of comments per the provided permission filter.