rqstComment = function(task, thread, s, e, cmtData){
$.getJSON('comments.php', {task: task}, function(data){
var baseurl = 'https://ourapiurl';
var site = data.site;
var url = site + '/' + thread;
var token = data.token;
if(task == 'get'){
$.ajax({
type: 'get',
url: baseurl + url,
data: {env: data.env, token: token, userId: data.username},
success: function(commentsArray) {
s(commentsArray);
},
error: function(XHR, text){
$('#' + thread).html('
Comments are not able to load at this time.
');
e
}
});
}
else if(task == 'download'){
var downlink = baseurl + 'export/' + url + '?env=' + data.env + '&token=' + token;
$('#cmtsExport').find('.modal-body').html(' Download Comments');
$('#downlink').click(function(){
$('#cmtsExport').modal('hide');
});
$('#cmtsExport').modal('show');
}
else if(data.username){//requires username to be returned
if(task == 'post'){//new comment to add to thread
var cmtstr = cmtData.content;
if(cmtstr.length < 3000){
$.ajax({
type: 'post',
url: baseurl + url + '?env=' + data.env + '&token=' + token + '&userId=' + data.username,
data: cmtData,
success: function(comment) {
s(comment)
},
error: function(XHR, text){
alert('New comments are not able to post at this time.');
e
}
});
}
else{
alert('You have exceeded the maximum character limit of 3000 characters.');
}
}
else if(task == 'put'){//updated comment in thread
if(cmtData.created_by_current_user && cmtData.content.length < 3000){
$.ajax({
type: 'put',
url: baseurl + site + '/' + cmtData.id + '?env=' + data.env + '&token=' + token + '&userId=' + data.username,
data: cmtData,
success: function(comment) {
s(comment)
},
error: function(XHR, text){
alert('Comments are not able to be updated at this time.');
e
}
});
}
else if(cmtData.content.length >= 3000){
alert('You have exceeded the maximum character limit of 3000 characters.');
}
else{
alert('Only the comment author can edit the comment text.');
}
}
else if(task == 'delete'){//delete comment from thread
if(confirm(confirmmsg)){
$.ajax({
type: 'delete',
url: baseurl + site + '/' + cmtData.id + '?env=' + data.env + '&token=' + token + '&userId=' + data.username,
success: s,
error: function(XHR, text){
alert('Comments are not able to be deleted at this time.');
e
},
dataType: 'html'
});
}
}
else if(task == 'upvote'){
if(cmtData.user_has_upvoted) {// these mean that the user just upvoted
$.ajax({
type: 'post',
url: baseurl + 'vote/' + site + '/' + cmtData.id + '?env=' + data.env + '&token=' + token + '&userId=' + data.username,
success: function() {
s(cmtData)
},
error: function(XHR, text){
alert('Comments are not able to receive votes at this time.');
e
}
});
} else {
$.ajax({
type: 'delete',
url: baseurl + 'vote/' + site + '/' + cmtData.id + '?env=' + data.env + '&token=' + token + '&userId=' + data.username,
success: function() {
s(cmtData)
console.log('says success');
},
error: function(XHR, text){
alert('Comments are not able to clear votes at this time.');
e
},
dataType: 'html'
});
}
}
}
else{
alert('Your session has timed out. Please login again to submit comments.');
}
});
}