Forum Discussion

jaread83's avatar
jaread83
Champion
8 years ago
Solved

Post a new topic to a specific board via API

Hi all,

Is it possible to post a new topic with a subject and body content into a specific board via the API? I would like the user who initialises the api call to be the author of the new topic too. I am having trouble finding anything other than viewing or editting existing content.

Cheers,

Jack

  • VikasB's avatar
    VikasB
    8 years ago

    HI jaread83

    It works perfectly except one thing that endpoint superuserescalationcode trying to return ${addPost} which is not feasible.  Either you have to create a custom response as you created in superuserescalation endpoint or you have to return ${addPost.message.root} or you can remove this freemarker variable.

     

    I think you are trying to return the status of addPost call???

    If yes, you can use ajax call with error/success methods to call the endpoint as below.  

     

     

    $.ajax({
    	url: '/aptgm87544/plugins/custom/plusnetplc/plusnetplc/superuserescalationencode',
    	error: function(error) {
    		console.log('error in call', error.responseText)
    	},
    	success: function(responseText) {
    		console.log(responseText)
       },
       type: 'GET',
    data: {subject: messagesubject,body: messagebody} });

    Hope it would resolve it. 

     

15 Replies

  • jaread83's avatar
    jaread83
    Champion
    8 years ago

    Thanks VikasB, this works now!

    The only issue I am having now is that when testing this it returns a 500 error... but it still works anyway but without a response. My admin account works perfectly so its tied to the user account permissions (i think?). I checked the permissions but as it turns out, changing the API permission to allow modify doesn't change anything. The user definately has permission to post in the chosen board too.

    Any ideas? Thanks for your help so far!

  • VikasB's avatar
    VikasB
    Boss
    8 years ago

    jaread83  

    No, it's not the permission issue.  

    If it returns 500 means there is a syntax error in your endpoint. 

    If it returns 501 means there is something wrong with API you are using. 

     

    Can I have a look at your code if you want me to resolve it? 

  • jaread83's avatar
    jaread83
    Champion
    8 years ago

    Hi VikasB, sure thing.

    The JS code for getting and posting the data:

     

    <@liaAddScript>
    ;(function($) {
    
    $(document).ready(function() {
    
        // hide the validation message
        $('.lia-form-validation-help-text').hide();
    
        $(document).on('click', '.lia-button-Cancel-action', function() {
            window.history.back();
        }); 
    
        // process everything when the submit button is clicked
        $(document).on('click','.lia-button-Submit-action',function() {
            // postid is in querystring of URL so get it with this function
            var postid = getUrlParameter('postid'),
                error = true,
                reasontext = $('#lia-additionalInformation').val();
    
            if(reasontext.length) {
                error = false;
            };
            
            if(error === false) {
                $.get('/aptgm87544/plugins/custom/plusnetplc/plusnetplc/superuserescalation', {post:postid}, function(data,status){
    
                }).done(function(data){
                    // get everything from the data object endpoint
                    var messageviewhref = $(data).find('messageviewhref').text(),
                        messagesubject = $(data).find('messagesubject').text(),
                        messagebody = $(data).find('messagebody').html(),
                        messageauthor = $(data).find('messageauthor').text(),
                        messageauthorid = $(data).find('messageauthorid').text(),
                        messageboard = $(data).find('messageboard').text(),
                        escalatesubject = '[ESCALATED] By: '+ messageauthor +' / Board: '+ messageboard,
                        escalatebodypre =  reasontext + '<p><br>Link to post: <a href="'+ messageviewhref +'">('+ messagesubject +')</a><br>by <li-user uid="'+ messageauthorid +'" login="'+ messageauthor +'"></li-user></p><p><br><a href="'+ messageviewhref +'">'+ messageviewhref +'</a></p><br>',
                        escalatebody = messagebody,
                        successMessage = '<div class="InfoMessage lia-panel-feedback-inline-note"><div class="lia-text"><p ng-non-bindable="" tabindex="0" role="informational">Success! This post has now been escalated</p><p><a href="'+ messageviewhref +'">Return to the post.</a></p></div></div>';
    
                    // pump the above variables into the endpoint so it can post
                    $.get('/aptgm87544/plugins/custom/plusnetplc/plusnetplc/superuserescalationencode', {escalatebody:escalatebody,escalatesubject:escalatesubject,escalatebodypre:escalatebodypre}, function(postdata,status){
                    // When finished, remove the form and display successMessage
                    }).done(function(){
                        $('.escalationform').remove();
                        $('.escalationform--feedback').append(successMessage);
                    })
                });
            } else {
                $('.lia-form-validation-help-text').show();
            };
    
        });
    
        function getUrlParameter(name) {
            name = name.replace(/[\[]/, '\\[').replace(/[\]]/, '\\]');
            var regex = new RegExp('[\\?&]' + name + '=([^&#]*)');
            var results = regex.exec(location.search);
            return results === null ? '' : decodeURIComponent(results[1].replace(/\+/g, ' '));
        };
    
    });
    
    })(jQuery);
    </@liaAddScript>

     

    Endpoint for get method (superuserescalation.ftl):

     

    <#compress>
    <#assign messageId = http.request.parameters.name.get("post","")?html?js_string />
    <#if messageId?length gt 0>
        <#attempt>
            <#assign message = rest("/messages/id/${messageId}").message />
            <#assign author = rest(message.author.@href).user />
            <#assign board = rest(message.board.@href).board />
        <response>
            <status>success</status>
            <messageviewhref>${message.@view_href}</messageviewhref>
            <messagesubject>${message.subject}</messagesubject>
            <messagebody>${message.body}</messagebody>
            <messageauthor>${author.login}</messageauthor>
            <messageauthorid>${author.id}</messageauthorid>
            <messageboard>${board.title}</messageboard>
        </response>
    
        <#recover>
            Error retrieving message id ${messageId}.
        </#attempt>
    <#else>
        Missing required parameter "id".
    </#if>
    </#compress>

    Endpoint for the post method (superuserescalationencode.ftl):

    <#assign escalatesubject = http.request.parameters.name.get("escalatesubject","")>
    <#assign escalatebodypre = http.request.parameters.name.get("escalatebodypre","")>
    <#assign escalatebody = http.request.parameters.name.get("escalatebody","")>
    <#assign addPost = rest("/boards/id/Superuser-escalations/messages/post?message.subject=${escalatesubject?url}&message.body=${escalatebodypre?url}${escalatebody?html?url}") />
    
    ${addPost}

    (with the above I tried adding the /> to the assigns but that was not the issue)

    The form as a custom component added to a custom page (I have stripped this down to the bare bones as there are a lot of wrapper divs):

    <div class="escalationform--feedback"></div>
    <div class="escalationform lia-form-vertical">
        <textarea id="lia-additionalInformation"></textarea>
        <div class="lia-form-validation-help-text">Tell us why you believe this content should be escalated.</div>
        <div class="lia-form-footer">
            <button class="lia-button lia-button-primary lia-button-Submit-action">Submit</button>
            <button class="lia-button lia-button-primary lia-button-Cancel-action">Cancel</button>
        </div>
    </div>
  • VikasB's avatar
    VikasB
    Boss
    8 years ago

    HI jaread83

    It works perfectly except one thing that endpoint superuserescalationcode trying to return ${addPost} which is not feasible.  Either you have to create a custom response as you created in superuserescalation endpoint or you have to return ${addPost.message.root} or you can remove this freemarker variable.

     

    I think you are trying to return the status of addPost call???

    If yes, you can use ajax call with error/success methods to call the endpoint as below.  

     

     

    $.ajax({
    	url: '/aptgm87544/plugins/custom/plusnetplc/plusnetplc/superuserescalationencode',
    	error: function(error) {
    		console.log('error in call', error.responseText)
    	},
    	success: function(responseText) {
    		console.log(responseText)
       },
       type: 'GET',
    data: {subject: messagesubject,body: messagebody} });

    Hope it would resolve it. 

     

  • jaread83's avatar
    jaread83
    Champion
    8 years ago
    I just tested it with a non admin account and that did it! Amazing, thank you so much for your help with this!