Forum Discussion

radhika_kundana's avatar
3 years ago

Join and leave grouphub through API

Hi,

Has anyone tried to create endpoint to join/leave group hub using rest API? 

I am new to Khoros , any inputs will be useful. 

Thankyou

  • Hi,

    We have a bunch of functions so I can't edit them to make more user friendly, but should give you enough. We separated all the functions to do separate tasks (join open node, send request to join closed node, accept invite, decline invite + leave group (there isn't functionality to reject an invite to groups so we went to accept invite and leave group immediately).

    function joinOpenNode(id, div, href, title) {
    
      $("#" + div).html("<span id='" + div + "' class='df df-justify-center'><img src='${settings.paths.bucket}/Assets/others/spinner.svg' class='group-button-loading'></span>");
    
      $.ajax({
        url: "/api/2.0/nodes/grouphub:"+div+"/membership_requests",
        headers: {
          accept: 'application/json',
          'content-type': 'application/json'
        },
        type: "POST",
        data: '{"data":{"type":"membership_request"}}',
        success: function(result) {
          $("#" + div).remove();
          window.location.href = href;
        },
        error: function(result) {
          if(result.responseText.split('"message":"')[1].split('","data"')[0].indexOf("The specified node is not a membership node") == 0) {
            createAndOpenModal(id, "${text.format('groups.modals-open-error-join-hidden')}");
            $("#" + div).remove();
          }
          else if(result.responseText.split('"message":"')[1].split('","data"')[0].indexOf("error.api.membership.join.user_not_found_id") == 0) {
            createAndOpenModal(id, "${text.format('groups.modals-open-error-join-anonymous')}");
            $("#" + div).remove();
          }
          else {
            createAndOpenModal(id, "${text.format('groups.modals-open-error-join-default')}");
          }
        },
        async: true,
        cache: false
      });
      
    }
    
    function joinClosedNode(id, div, href, title) {
    
      $("#" + div).html("<span id='" + div + "' class='df df-justify-center'><img src='${settings.paths.bucket}/Assets/others/spinner.svg' class='group-button-loading'></span>");
      
      $.ajax({
        url: '/api/2.0/nodes/grouphub:' + div + '/membership_requests',
        method: "POST",
        headers: {
          accept: 'application/json',
          'content-type': 'application/json'
        },
        data: '{"data":{"type":"membership_request"}}',
        success: function(request) {
          createAndOpenModal(id, "${text.format('groups.modals-closed-success', '"+ title +"')}");
          $("#" + div).html('<span class="cg-group-info-join-requested">${text.format("groups.tile-buttons-join-closed-node-requested")}</span>');
        },
        error: function(request) {
          if(request.responseText.split('"message":"')[1].split('","data"')[0].indexOf("User has already requested to join node") == 0) {
            createAndOpenModal(id, "${text.format('groups.modals-closed-error-already-requested')}");
            $("#" + div).remove();
          }
          else if(request.responseText.split('"message":"')[1].split('","data"')[0].indexOf("The specified node is not a membership node") == 0) {
            createAndOpenModal(id, "${text.format('groups.modals-open-error-join-hidden')}");
            $("#" + div).remove();
          }
          else if(request.responseText.split('"message":"')[1].split('","data"')[0].indexOf("error.api.membership.join.user_not_found_id") == 0) {
            createAndOpenModal(id, "${text.format('groups.modals-closed-error-anonymous')}");
            $("#" + div).remove();
          }
          else {
            createAndOpenModal(id, "${text.format('groups.modals-closed-error-default', '"+ title +"')}");
            $("#" + div).html("<span onclick=\"joinClosedNode('" + id + "', '" + div + "', '" + href + "', '" + title.replace("'","").replace('"','') + "')\" class='community-button community-button-up-black'>${text.format('groups.tile-buttons-join-closed-node')}</span>");
          }
        },
        async: true,
        cache: false
      });
      
    }
    
    function acceptInvite(id, div, href, title) {
    
      $("#" + div).html("<span id='" + div + "' class='df df-justify-center'><img src='${settings.paths.bucket}/Assets/others/spinner.svg' class='group-button-loading'></span>");
      
      $.ajax({
        url: '/api/2.0/nodes/grouphub:' + div + '/invites',
        method: "PUT",
        headers: {
          accept: 'application/json',
          'content-type': 'application/json'
        },
        data: '{"invite":{"accept":true}}',
        success: function(request) {
          window.location.href = href;
          $("#" + div).remove();
        },
        error: function(request) {
          if(request.responseText.split('"message":"')[1].split('","data"')[0].indexOf("Invite not found for the specified user and node") == 0) {
            createAndOpenModal(id, "${text.format('groups.modals-accept-invite-no-invite')}");
            $("#" + div).html("<span onclick=\"declineInvite('" + id + "', '" + div + "', '" + href + "', '" + title.replace("'","").replace('"','') + "')\" class='community-button community-button-link'>${text.format('groups.tile-buttons-invite-decline')}</span><span onclick=\"acceptInvite('" + id + "', '" + div + "', '" + href + "', '" + title.replace("'","").replace('"','') + "')\" class='community-button community-button-up-black'>${text.format('groups.tile-buttons-invite-accept')}</span>");
          }
          else {
            createAndOpenModal(id, "${text.format('groups.modals-accept-invite-error-default')}");
            $("#" + div).html("<span onclick=\"declineInvite('" + id + "', '" + div + "', '" + href + "', '" + title.replace("'","").replace('"','') + "')\" class='community-button community-button-link'>${text.format('groups.tile-buttons-invite-decline')}</span><span onclick=\"acceptInvite('" + id + "', '" + div + "', '" + href + "', '" + title.replace("'","").replace('"','') + "')\" class='community-button community-button-up-black'>${text.format('groups.tile-buttons-invite-accept')}</span>");
          }
          
        },
        async: true,
        cache: false
      });
      
    }
    
    function declineInvite(id, div, href, title) {
    
      $("#" + div).html("<span id='" + div + "' class='df df-justify-center'><img src='${settings.paths.bucket}/Assets/others/spinner.svg' class='group-button-loading'></span>");
      
      $.ajax({
        url: '/api/2.0/nodes/grouphub:' + div + '/invites',
        method: "PUT",
        headers: {
          accept: 'application/json',
          'content-type': 'application/json'
        },
        data: '{"invite":{"accept":true}}',
        success: function(request) {
          leaveGroup(id, div, href, title);
        },
        error: function(request) {
          if(request.responseText.split('"message":"')[1].split('","data"')[0].indexOf("Invite not found for the specified user and node") == 0) {
            createAndOpenModal(id, "${text.format('groups.modals-decline-invite-no-invite')}");
            $("#" + div).html("<span onclick=\"declineInvite('" + id + "', '" + div + "', '" + href + "', '" + title.replace("'","").replace('"','') + "')\" class='community-button community-button-link'>${text.format('groups.tile-buttons-invite-decline')}</span><span onclick=\"acceptInvite('" + id + "', '" + div + "', '" + href + "', '" + title.replace("'","").replace('"','') + "')\" class='community-button community-button-up-black'>${text.format('groups.tile-buttons-invite-accept')}</span>");
          }
          else {
            createAndOpenModal(id, "${text.format('groups.modals-decline-invite-error-default')}");
            $("#" + div).html("<span onclick=\"declineInvite('" + id + "', '" + div + "', '" + href + "', '" + title.replace("'","").replace('"','') + "')\" class='community-button community-button-link'>${text.format('groups.tile-buttons-invite-decline')}</span><span onclick=\"acceptInvite('" + id + "', '" + div + "', '" + href + "', '" + title.replace("'","").replace('"','') + "')\" class='community-button community-button-up-black'>${text.format('groups.tile-buttons-invite-accept')}</span>");
          }
          
        },
        async: true,
        cache: false
      });
    
    }
    
    function leaveGroup(id, div, href, title) {
    
      $.ajax({
        url: "/api/2.0/nodes/grouphub:"+div+"/membership_requests",
        headers: {
          accept: 'application/json',
          'content-type': 'application/json'
        },
        type: "DELETE",
        data: '{"data":{"type":"membership_request"}}',
        success: function(result) {
          if($("#" + div).parent().parent().parent().find(".custom-groups-group").length == 1) {
            $("#" + div).parent().parent().parent().parent().remove();
          }
          else {
            $("#" + div).parent().parent().remove();
          }
          createAndOpenModal(id, "${text.format('groups.modals-decline-invite-success')}");
        },
        error: function(result) {
          createAndOpenModal(id, "${text.format('groups.modals-decline-invite-error-default')}");
          $("#" + div).html("<span onclick=\"declineInvite('" + id + "', '" + div + "', '" + href + "', '" + title.replace("'","").replace('"','') + "')\" class='community-button community-button-link'>${text.format('groups.tile-buttons-invite-decline')}</span><span onclick=\"acceptInvite('" + id + "', '" + div + "', '" + href + "', '" + title.replace("'","").replace('"','') + "')\" class='community-button community-button-up-black'>${text.format('groups.tile-buttons-invite-accept')}</span>");
        },
        async: true,
        cache: false
      });
    }

    Hope it helps. It does look extremely chaotic xD

    • Hi ChhamaJ,

      I was able to implement Leave node successfully using membership API(Javascript).

      But Join node API is returning 301 error and I don't see error details, can you guide me on the issue.

      Code:  I am calling below function on button click. 

      function joinNode()
      {
      const options = {
      method: 'POST',
      headers: {Accept: 'application/json', 'Content-Type': 'application/json'}
      };
      console.log(options);
      fetch('https://frnmb74945.stage.lithium.com/api/2.0/nodes/grouphub:'+ groupID + '/membership_requests', options)
      .then(response => response.json())
      .then(response => console.log(response))
      .catch(err => console.error(err));

      }

      Error details from console: 

      {status: 'error', message: 'An unexpected error occurred.', data: {…}, metadata: {…}data:code: 301

      developer_message: ""

      more_info: ""

      type: "error_data"

      [[Prototype]]: Object

      message: "An unexpected error occurred."

      metadata: {}

      status: "error"

      [[Prototype]]: Object

      • MartinD's avatar
        MartinD
        Khoros Staff

        Hi radhika_kundana you forgot the body

         

        {
             "data" : {
                   "type" : "membership_request"
               }
          }

         

        This call worked for me

         

        curl --location --request POST 'https://frnmb74945.stage.lithium.com/api/2.0/nodes/grouphub:region-east/membership_requests?restapi.session_key=9Lu7OU_KvuuPOMw9FqHaOZ33KVBAJnkpTKcQxVNufHQ.' \
        --header 'Authorization: Basic aGNsZGV2OlN6YWJyTmRwaw==' \
        --header 'Content-Type: application/json' \
        --data-raw '{
             "data" : {
                   "type" : "membership_request"
               }
          }'