Start updating to new API

This commit is contained in:
Calvin Montgomery 2013-08-11 23:10:55 -04:00
parent 0bf80a375d
commit 4aa0e7a4ef
7 changed files with 63 additions and 314 deletions

View file

@ -11,17 +11,16 @@ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLI
var uname = readCookie("cytube_uname") || "";
var session = readCookie("cytube_session") || "";
var api = WEB_URL + "/api/json/";
var loggedin = false;
if(uname && session) {
var loginstr = "name=" + encodeURIComponent(uname)
+ "&session=" + session;
var url = api + "login?" + loginstr + "&callback=?";
$.getJSON(url, function(data) {
if(data.success) {
var data = {
name: uname,
session: session
};
$.post(WEB_URL + "/api/login?callback=?", data, function (data) {
if(data.success)
onLogin();
}
});
}
@ -57,7 +56,8 @@ $("#email").click(makeTabCallback("#email", "#changeemailpane"));
$("#profile").click(makeTabCallback("#profile", "#profilepane"));
$("#profile").click(function() {
if(uname != "") {
$.getJSON(api + "getprofile?name=" + encodeURIComponent(uname) + "&callback=?", function(data) {
$.getJSON(WEB_URL+"/api/users/"+uname+"/profile?callback=?",
function (data) {
if(data.success) {
$("#profiletext").val(data.profile_text);
$("#profileimg").val(data.profile_image);
@ -82,8 +82,10 @@ $("#channels").click(function () {
return;
}
$.getJSON(api + "listuserchannels?name=" + encodeURIComponent(uname) +
"&session=" + session + "&callback=?", function(data) {
var auth = "name=" + encodeURIComponent(uname) + "&session=" +
encodeURIComponent(session);
$.getJSON(WEB_URL+"/api/account/mychannels?"+auth+"&callback=?",
function (data) {
$("#channellist tbody").remove();
data.channels.forEach(function (chan) {
var tr = $("<tr/>").appendTo($("#channellist"));
@ -134,12 +136,12 @@ $("#registerbtn").click(function() {
}
// Input valid, try registering
var url = api + "register?" + [
"name=" + encodeURIComponent(name),
"pw=" + encodeURIComponent(pw)
].join("&") + "&callback=?";
$.getJSON(url, function(data) {
var data = {
name: name,
pw: pw
};
$.pos(WEB_URL + "/api/register?callback=?", data, function (data) {
if(data.success) {
uname = name;
session = data.session;
@ -170,10 +172,11 @@ $("#loginbtn").click(function() {
return;
}
uname = $("#loginusername").val();
var loginstr = "name=" + encodeURIComponent(uname)
+ "&pw=" + encodeURIComponent($("#loginpw").val());
var url = api + "login?" + loginstr + "&callback=?";
$.getJSON(url, function(data) {
var data = {
name: uname,
pw: $("#loginpw").val()
};
$.getJSON(WEB_URL+"/api/login?callback=?", data, function(data) {
if(data.success) {
session = data.session;
onLogin();
@ -230,12 +233,13 @@ $("#cpwbtn").click(function() {
}
// Input valid, try changing password
var url = api + "changepass?" + [
"name=" + encodeURIComponent(name),
"oldpw=" + encodeURIComponent(oldpw),
"newpw=" + encodeURIComponent(newpw)
].join("&") + "&callback=?";
$.getJSON(url, function(data) {
var data = {
name: name,
oldpw: oldpw,
newpw: newpw
};
$.post(WEB_URL + "/api/account/passwordchange?callback=?", data,
function (data) {
if(data.success) {
$("<div/>").addClass("alert alert-success")
.text("Password changed.")
@ -266,7 +270,7 @@ $("#cebtn").click(function() {
return;
}
if(!email.match(/^[a-z0-9_\.]+@[a-z0-9_\.]+[a-z]+$/)) {
if(!email.match(/^[\w_\.]+@[\w_\.]+[a-zA-Z]+$/)) {
$("<div/>").addClass("alert alert-error")
.text("Invalid email")
.insertAfter($("#ceemail").parent().parent());
@ -282,12 +286,13 @@ $("#cebtn").click(function() {
return;
}
var url = api + "setemail?" + [
"name=" + encodeURIComponent(name),
"pw=" + encodeURIComponent(pw),
"email=" + encodeURIComponent(email)
].join("&") + "&callback=?";
$.getJSON(url, function(data) {
var data = {
name: name,
pw: pw,
email: email
};
$.post(WEB_URL + "/api/account/email?callback=?", data,
function (data) {
if(data.success) {
$("<div/>").addClass("alert alert-success")
.text("Email updated")
@ -312,11 +317,12 @@ $("#rpbtn").click(function() {
var name = $("#rpusername").val();
var email = $("#rpemail").val();
var url = api + "resetpass?" + [
"name=" + encodeURIComponent(name),
"email=" + encodeURIComponent(email)
].join("&") + "&callback=?";
$.getJSON(url, function(data) {
var data = {
name: name,
email: email
};
$.post(WEB_URL + "/api/account/passwordreset?callback=?", data,
function (data) {
$("#rpbtn").text("Send Reset");
if(data.success) {
$("<div/>").addClass("alert alert-success")
@ -336,20 +342,16 @@ $("#profilesave").click(function() {
$("#profilepane").find(".alert-error").remove();
$("#profilepane").find(".alert-success").remove();
var img = $("#profileimg").val();
/*
img = escape(img).replace(/\//g, "%2F")
.replace(/&/g, "%26")
.replace(/=/g, "%3D")
.replace(/\?/g, "%3F");
*/
var url = api + "setprofile?" + [
"name=" + encodeURIComponent(uname),
"session=" + session,
"profile_image=" + encodeURIComponent(img),
"profile_text=" + encodeURIComponent($("#profiletext").val())
].join("&") + "&callback=?";
var text = $("#profiletext").val();
var data = {
name: uname,
session: session,
profile_image: img,
profile_text: text
};
$.getJSON(url, function(data) {
$.post(WEB_URL+"/api/account/profile?callback=?", data,
function (data) {
if(data.success) {
$("<div/>").addClass("alert alert-success")
.text("Profile updated.")