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:
I'm an admin user and copied this code from discussions here.
Any ideas? Is there a better way to do this?
Thanks