Save Nested Object or Array Into Topic Data

Plugin Development
  • Hi community, I am writing a plugin that I want to keep track of a list of users who have replied to a certain topic.

    I wanted to create a list of uids that replied (or object with uid as key and post count inside the topic as value) in the topic data object.

    However when I tried to do it like this

    plugin.createReplyList = function(topicData) {
    	var replyList = [1,2,3]
    
    	db.setObjectField('topic:' + topicData.tid, "replyList", replyList);
    };
    

    I got the below error

    node_redis: Deprecated: The HSET command contains a argument of type Array.
    This is converted to "1,2,3" by using .toString() now and will return an error from v.3.0 on.
    Please handle this in your code to make sure everything works as you intended it to.
    
    

    Wonder what would be the correct way to achieve this? Thanks in advance.

  • Why don't you just create the list when you need it by iterating over the posts in a topic?

    What do you need this list for? There actually might already be a list for this... let me check.

    Edit: ok I was thinking of Topics.getFollowers(tid, callback); which corresponds to the tid:[tid]:followers set. Maybe that could work for you, as most users have it set up so they follow the topic when they reply. However, if that doesn't work, then you probably want to listen to the action:topic.reply hook and add users to a set or sorted set that way.

    function onTopicReply(postData, callback) {
      var tid = postData.tid;
      var uid = postData.uid;
      db.setAdd('topic:tid:' + tid + ':replies', uid, callback);
    }
    
  • Keep in mind we already have 'tid:' + tid + ':posters' It keeps a list of poster uids in that topic sorted by reply count.

  • @baris literally saved my day 👍
    BTW, is there a way to tell the client side code to refresh the page inside an action hook, say action:topic.reply?

  • Client side you can call ajaxify.refresh() to refresh the page.


Suggested Topics


| | | |