filter:post.edit, setTopicFields keeps forgetting data on restart

Plugin Development
  • I'm having a frustrating time with setTopicFields, but only on filter:post.edit. filter:post.create works fine, and the code is pretty similar so I'm not sure what I'm missing. At first, it looks like it works properly, but when I restart NodeBB the data reverts back to how it was on post.create.

    The post data works correctly, but it creates a discrepancy where the post content says the right thing and the custom topicField says something out of date.

    plugin.createPostTruth = function (payload, callback) {
    
        topics.getTopicFields(payload.data.tid, ['truths'], function (err, topicData) {
    
            if (err) return console.log(err);
    
            var topicTruths = topicData.truths;
    
            var parsed = parseColored(payload.post.content);
    
            var truthpost = {
                'pid': payload.post.pid,
                'uid': payload.post.uid,
                'truths': parsed
            };
    
            if (truthpost.truths) {
    
                if (topicTruths && Array.isArray(topicTruths)) {
                    topicTruths.push(truthpost);
    
                } else {
                    topicTruths = [truthpost];
                }
            }
    
            topics.setTopicFields(payload.data.tid, {
                'truths': topicTruths
    
            });
        });
    
        callback(null, payload);
    }
    
    plugin.editPostTruth = function (payload, callback) {
    
        posts.getPostFields(payload.data.pid, ['tid'], function (err, postData) {
            topics.getTopicFields(postData.tid, ['truths'], function (err, topicData) {
    
                if (err) return console.log(err);
    
                var topicTruths = topicData.truths;
    
                var parsed = parseColored(payload.post.content);
    
                var truthpost = {
                    'pid': payload.post.pid,
                    'uid': payload.post.uid,
                    'truths': parsed
                };
    
                if (!truthpost.truths) {
                    console.log("checking for pid " + truthpost.pid);
                    var i = topicTruths.findIndex(function (o) {
                        return o.pid == truthpost.pid;
                    });
                    if (i >= 0) {
                        console.log("removing truths");
                        topicTruths.splice(i, 1);
                    }
                    //else return payload;
                } else {
    
                    if (topicTruths && Array.isArray(topicTruths)) {
    
                        console.log("checking to find pid " + truthpost.pid);
                        var i = topicTruths.findIndex(function (o) {
                            return o.pid == truthpost.pid;
                        });
                        if (i >= 0) {
                            console.log("Found pid. splicing truth");
                            topicTruths.splice(i, 1, truthpost);
    
                        } else {
                            topicTruths.push(truthpost);
                            console.log("No pid found. adding truth to bottom of stack.");
                        }
                    } else {
                        topicTruths = [truthpost];
                        console.log("There were no truths at all. making this the first truth...");
                    }
                }
    
                topics.setTopicFields(payload.data.tid, {
                    'truths': topicTruths
    
                });
            });
        });
        callback(null, payload);
    }
    
    

    The topic json before edit, and after restart:

     "title": "Example Topic",
        "uid": 1,
        "viewcount": 1,
        "truths": [
            {
                "pid": 136,
                "uid": 1,
                "truths": [
                    {
                        "type": "red",
                        "text": "This is a red."
                    }
                ]
            },
            {
                "pid": 137,
                "uid": 1,
                "truths": [
                    {
                        "type": "blue",
                        "text": "This is a blue."
                    }
                ]
            }
        ],
    

    The topic json just after edit, and what it should look like:

        "title": "Example Topic",
        "uid": 1,
        "viewcount": 1,
        "truths": [
            {
                "pid": 136,
                "uid": 1,
                "truths": [
                    {
                        "type": "red",
                        "text": "This is a red."
                    }
                ]
            },
            {
                "pid": 137,
                "uid": 1,
                "truths": [
                    {
                        "type": "gold",
                        "text": "This is not a blue."
                    }
                ]
            }
        ],
    

Suggested Topics


  • 0 Votes
    2 Posts
    404 Views

    @sebastián-cisneros In static:api.routes, you'll want to call controllerHelpers.setupAPIRoute to mount the route to the appropriate /api/v3/plugins mount point.

    You can see an example of how we do it in the quickstart plugin

  • 0 Votes
    5 Posts
    559 Views

    @antosik @baris
    Nice, Thank you all!

  • filter:uploadFile

    Plugin Development
    0 Votes
    1 Posts
    864 Views

    I'm working on a plugin that filters out a specific file type and relocates them to a different folder.

    Everything works as I want except for two issues that I expect are approximately the same solution.

    Since uploadFile fires on all files, I need to do an early filter against the extension/mimetype and exit out on a non-match. When I do so, I am firing the callback with (null, data ) where data is the past object for the filter then returning. The resulting link that shows up in the message has a bad URL.

    Similarly, when it passes the extension check, it throws an exception due to the uploaded temp file being moved by my plugin.

    I assume in both cases this is an issue with what need to get sent back when the filter wraps up its business for the rest of the "upload" flow. Looking at uploads.js hasn't helped me parse it out.

    Looking at the imgur plugin has me more confused than understanding.

    Anyone know what needs to occur here?

  • 0 Votes
    17 Posts
    4k Views

    My YouTube plugin needs updating. But I'm in London tomorrow to Thursday. Then spending alternate weeks in gibraltar. So its hard to find the time. 😉

  • 0 Votes
    3 Posts
    2k Views

    @baris thanks. I realised there's some issues with redactor munging the data.
    When code is insered inside a <pre/> block, switching between HTML and WYSIWYG views will munge the content to HTML entitites.
    You can recreate this issue on this page:
    http://imperavi.com/redactor/examples/typography/

    Switch to code view and add some HTML inside the <pre/> block. I entered:
    <p>test</p>
    Switch to WYSIWYG and back to code and it has been replaced by:

    &lt;p&gt;test&lt;/p&gt;

    This breaks the ability to parse that code later with syntax highlighters.

    I've raised it with Imperavi.