Commit 7d31d565 authored by Rene Saarsoo's avatar Rene Saarsoo
Browse files

Implement delete and undo_delete requests.

To be able to query the deleted comments, the Comments#showDeleted
method can be called to define whether deleted comments should be
included to the query.  This is implemented through having two
views in the database: full_comments and full_visible_comments.
parent 3621df8d
Loading
Loading
Loading
Loading
+38 −0
Original line number Diff line number Diff line
@@ -188,6 +188,44 @@ app.post('/auth/:sdk/:version/comments/:commentId', services.requireLogin, servi
    });
});

// Deletes a comment
app.post('/auth/:sdk/:version/comments/:commentId/delete', services.requireLogin, services.comments, services.users, function(req, res) {
    req.comments.getById(req.params.commentId, function(err, comment) {
        if (!req.users.canModify(req.session.user, comment)) {
            res.json({ success: false, reason: 'Forbidden' }, 403);
            return;
        }

        var action = {
            id: req.params.commentId,
            user_id: req.session.user.id,
            deleted: true
        };
        req.comments.setDeleted(action, function(err) {
            res.send({ success: true });
        });
    });
});

// Restores a deleted comment
app.post('/auth/:sdk/:version/comments/:commentId/undo_delete', services.requireLogin, services.comments, services.users, function(req, res) {
    req.comments.showDeleted(true);
    req.comments.getById(req.params.commentId, function(err, comment) {
        if (!req.users.canModify(req.session.user, comment)) {
            res.json({ success: false, reason: 'Forbidden' }, 403);
            return;
        }

        var action = {
            id: req.params.commentId,
            user_id: req.session.user.id,
            deleted: false
        };
        req.comments.setDeleted(action, function(err) {
            res.send({ success: true, comment: ApiAdapter.commentToJson(comment) });
        });
    });
});

// Returns all subscriptions for logged in user
// For now does nothing.
+18 −10
Original line number Diff line number Diff line
@@ -17,12 +17,23 @@ function Comments(db, domain) {
    this.db = db;
    this.domain = domain;
    this.targets = new Targets(db, domain);
    this.view = "full_visible_comments";
}

Comments.prototype = {
    /**
     * Toggles between showing and hiding deleted comments.  By
     * default all the #get* #find* and #count* methods will exclude
     * the deleted comments.  But by first calling showDeleted(true)
     * the deleted comments will also be included.
     * @param {Boolean} show
     */
    showDeleted: function(show) {
        this.view = show ? "full_comments" : "full_visible_comments";
    },

    /**
     * Finds a single comment by ID in the current domain.
     * Does not fine deleted comments.
     *
     * @param {Number} id The ID of the comment to find.
     * @param {Function} callback Called with the result.
@@ -32,7 +43,7 @@ Comments.prototype = {
    getById: function(id, callback) {
        var sql = [
            'SELECT *',
            'FROM full_visible_comments',
            'FROM', this.view,
            'WHERE domain = ? AND id = ?'
        ];

@@ -41,7 +52,6 @@ Comments.prototype = {

    /**
     * Finds list of all comments for a particular target.
     * Excludes deleted comments.
     *
     * @param {Object} target The target:
     * @param {String} target.type One of: class, guide, video.
@@ -55,7 +65,7 @@ Comments.prototype = {
    find: function(target, callback) {
        var sql = [
            'SELECT *',
            'FROM full_visible_comments',
            'FROM', this.view,
            'WHERE domain = ? AND type = ? AND cls = ? AND member = ?',
            'ORDER BY created_at'
        ];
@@ -65,7 +75,6 @@ Comments.prototype = {

    /**
     * Returns all comments sorted in reverse chronological order.
     * Excludes deleted comments.
     *
     * @param {Object} opts Options for the query:
     * @param {Number} [opts.limit=100] Number of rows to return.
@@ -78,7 +87,7 @@ Comments.prototype = {
    findRecent: function(opts, callback) {
        var sql = [
            'SELECT *',
            'FROM full_visible_comments',
            'FROM', this.view,
            'WHERE domain = ?',
            'ORDER BY created_at DESC',
            'LIMIT ? OFFSET ?'
@@ -89,7 +98,6 @@ Comments.prototype = {

    /**
     * Counts number of comments in the current domain.
     * Excludes deleted comments.
     *
     * @param {Object} opts Reserved for future.
     *
@@ -100,7 +108,7 @@ Comments.prototype = {
    count: function(opts, callback) {
        var sql = [
            'SELECT COUNT(*) as count',
            'FROM full_visible_comments',
            'FROM', this.view,
            'WHERE domain = ?'
        ];

@@ -111,7 +119,7 @@ Comments.prototype = {

    /**
     * Returns number of comments for each target in the current
     * domain.  Excludes deleted comments.
     * domain.
     *
     * @param {Function} callback Called with the result.
     * @param {Error} callback.err The error object.
@@ -128,7 +136,7 @@ Comments.prototype = {
            'SELECT',
            "    CONCAT(type, '__', cls, '__', member) AS _id,",
            "    count(*) AS value",
            'FROM full_visible_comments',
            'FROM', this.view,
            'WHERE domain = ?',
            'GROUP BY target_id'
        ];
+12 −0
Original line number Diff line number Diff line
@@ -47,6 +47,18 @@ describe("Comments", function() {
        });
    });

    describe("after calling showDeleted(true)", function() {
        beforeEach(function() {
            comments.showDeleted(true);
        });
        it("#getById also finds the deleted comment", function(done) {
            comments.getById(6, function(err, com) {
                expect(com.id).toEqual(6);
                done();
            });
        });
    });

    it("#find returns all undeleted comments for a target", function(done) {
        comments.find({type: "class", cls: "Ext", member: ""}, function(err, rows) {
            expect(rows.length).toEqual(5);
+15 −0
Original line number Diff line number Diff line
@@ -89,3 +89,18 @@ CREATE OR REPLACE VIEW full_visible_comments AS SELECT
FROM visible_comments AS c
    LEFT JOIN users ON c.user_id = users.id
    LEFT JOIN targets ON c.target_id = targets.id;

-- the same as above, but including deleted comments
CREATE OR REPLACE VIEW full_comments AS SELECT
    c.*,
    users.username,
    users.external_id,
    users.email,
    users.moderator,
    targets.domain,
    targets.type,
    targets.cls,
    targets.member
FROM comments AS c
    LEFT JOIN users ON c.user_id = users.id
    LEFT JOIN targets ON c.target_id = targets.id;