Forum Discussion

ttadej's avatar
ttadej
Advisor
8 years ago

Subscription REST call fails as admin

I'm creating a custom component for a subscribe button. The button needs to exist on a QandAQuestionPage and subscribe users to topic changes (side note: still unclear on thread vs message subscriptions - there seems to be no documentation on the difference). The button needs to do the following on page load:

 

  • For anonymous users, add disabled class to button
  • For logged in users, check if a subscription already exists
    • If so, assign custom text to the button i.e. "unsubscribe" and assign click event to unsubscribe from thread
    • If not subscribed, button will have the text "subscribe" and click event will subscribe the user

 

I run into problems when I try and add a subscription. Here is the rough code:

 

 

<#assign message = page.context.thread.topicMessage! />
<#assign topicId = message.uniqueId! />

<#assign subscription = rest("/threads/id/${topicId!}/subscriptions/users/self/type/email").subscription[0]!/>
<#assign userIsAnonymous = user.anonymous! />

<#assign buttonStateClass = "button-default" />
<#assign buttonText = text.format("label.subscribe_to_label") />
<#assign buttonSubscribeAction = "add" />

<#--Check user logged in state and subscription status-->
<#if userIsAnonymous!>
  <#assign buttonStateClass = "button-disabled" />
<#else>
  <#if !subscription.@null??>
    <#assign buttonStateClass = "button-active" />
    <#assign buttonText = text.format("label.custom.subscribed_to_label") />
    <#assign buttonSubscribeAction = "remove" />
  </#if>
</#if>

<button id="watch-topic-button" class="${buttonStateClass!}" data-action="${buttonSubscribeAction!}">${buttonText!}</button>

<@liaAddScript>
  ;(function($) {
    $(function() {

      $("#watch-topic-button").on("click", function () {
        var action = $(this).data("action");
        post(action);
      });

      function post(action) {
        $.post("/restapi/vc/threads/id/"+${topicId}+"/subscriptions/users/self/"+action+"?subscription.type=email",
        function(data){
          console.log("success?: ", action, data);
        });
      }

    });
  })(LITHIUM.jQuery);
</@liaAddScript>

 

Checking the subscription is successful, but adding a subscription results in a Error.. in the XML response is an error code 303. When I try to hit the URL directly I get a 504 error "Method 'get' is not supported off of node 'thread_subscription_context.users.self.add'"

 

I was looking at code from discussions like:

https://community.lithium.com/t5/Developers-Discussion/I-need-a-way-to-add-a-subription-to-a-board-for-many-users-at/m-p/171651#M6837

 

I'm an admin user and copied this code from discussions here.

 

Any ideas? Is there a better way to do this?

 

Thanks

  • Hi,

     

    Did you try cloning the 'Subscribe' option from the 'Topic Options' drop-down and display as a button?

     

    Try this - 

    //Add Subscribe and unsubscribe button in ForumPage, for example:
    if(LITHIUM.CommunityJsonObject.Page.name == "ForumPage"){

    var element = $(".lia-link-navigation.addBoardUserEmailSubscription").clone();
    if(element[0] != undefined){
    element = '<div class="custom_board_Subscription">'+ element[0].outerHTML +'</div>';
    $(".lia-menu-bar-buttons .lia-menu-navigation").prepend(element);
    }
    else{
    var element = $(".lia-link-navigation.removeBoardUserEmailSubscription").clone();
    element = '<div class="custom_board_Subscription">'+ element[0].outerHTML +'</div>';
    $(".lia-menu-bar-buttons .lia-menu-navigation").prepend(element);
    }
    }

     

     

    Hope this helps!

     

    Thanks,

    Srujana.

    • ttadej's avatar
      ttadej
      Advisor

      Forgot I had posted this question. I ended up creating an endpoint to handle subscriptions for both boards and individual threads. The endpoint takes in the following parameters:

      • id
      • action (add/remove)
      • type (threads/board)

      Then it goes something like this:

          <#if actionParam == "add">
            <#assign response = rest("/${typeParam!?url('UTF-8')}/id/${idParam!?url('UTF-8')}/subscriptions/users/self/add?subscription.type=email") />
          <#elseif actionParam == "remove">
              <#assign response = rest("/${typeParam!?url('UTF-8')}/id/${idParam!?url('UTF-8')}/subscriptions/users/self/type/email/remove") />
          <#else>
            <#-- handle error -->

      My original issue had to do with permissions. In general we've found it is best to do any type of "POST" in an endpoint using ajax.

      • gstelang's avatar
        gstelang
        Mentor

        Hey ttadej That's useful.

         

        Is this for subscribing to a label? For example, I create a post and give it a label "Customer Research" and I want to subscribe to this label. Is that something I can do?