From: Greg Burri Date: Fri, 8 Aug 2008 20:29:46 +0000 (+0000) Subject: ADD fin de 'communication.js' X-Git-Tag: 1.1.0~11 X-Git-Url: http://git.euphorik.ch/?p=euphorik.git;a=commitdiff_plain;h=15f8b5143c6b9dcfe86eda84c22c31826a7f3d1c ADD fin de 'communication.js' --- diff --git a/index.yaws b/index.yaws index 9f318dc..767a0b7 100755 --- a/index.yaws +++ b/index.yaws @@ -42,11 +42,11 @@ - - + + diff --git a/js/client.js b/js/client.js index 93e5dae..e01be9e 100644 --- a/js/client.js +++ b/js/client.js @@ -161,40 +161,6 @@ euphorik.Client.prototype.supprimerConversation = function(num) { } }; -euphorik.Client.prototype.getJSONLogin = function(login, password) { - return { - "header" : { "action" : "authentification", "version" : euphorik.conf.versionProtocole }, - "login" : login, - "password" : password - }; -}; - -euphorik.Client.prototype.getJSONLoginCookie = function() { - return { - "header" : { "action" : "authentification", "version" : euphorik.conf.versionProtocole }, - "cookie" : this.cookie - }; -}; - -/** - * le couple (login, password) est facultatif. S'il n'est pas fournit alors il ne sera pas possible - * de s'autentifier avec (login, password). - */ -euphorik.Client.prototype.getJSONEnregistrement = function(login, password) { - var mess = { - "header" : { "action" : "register","version" : euphorik.conf.versionProtocole } - }; - - if (login && password) { - mess.login = login; - mess.password = password; - } - - mess.profile = this.getJSONProfileInfos(); - - return mess; -}; - euphorik.Client.prototype.getJSONConversations = function() { var conversations = []; this.conversations.each(function(i, conv) { @@ -205,7 +171,6 @@ euphorik.Client.prototype.getJSONConversations = function() { euphorik.Client.prototype.getJSONProfile = function() { return { - "header" : { "action" : "set_profile", "version" : euphorik.conf.versionProtocole }, "cookie" : this.cookie, "login" : this.login, "password" : this.password, @@ -291,11 +256,11 @@ euphorik.Client.prototype.connexionCookie = function() { if (!this.cookie) { return false; } - return this.connexion(this.getJSONLoginCookie()); + return this.connexion("authentification", { "cookie" : this.cookie }); }; euphorik.Client.prototype.connexionLogin = function(login, password) { - return this.connexion(this.getJSONLogin(login, password)); + return this.connexion("authentification", {"login" : login, "password" : password }); }; euphorik.Client.prototype.enregistrement = function(login, password) { @@ -308,10 +273,27 @@ euphorik.Client.prototype.enregistrement = function(login, password) { } return false; } else { - return this.connexion(this.getJSONEnregistrement(login, password)); + return this.connexion("register", this.getJSONEnregistrement(login, password)); } }; +/** + * le couple (login, password) est facultatif. S'il n'est pas fournit alors il ne sera pas possible + * de s'autentifier avec (login, password). + */ +euphorik.Client.prototype.getJSONEnregistrement = function(login, password) { + var mess = {}; + + if (login && password) { + mess.login = login; + mess.password = password; + } + + mess.profile = this.getJSONProfileInfos(); + + return mess; +}; + /** * Connexion. Réalisée de manière synchrone. */ @@ -324,10 +306,11 @@ euphorik.Client.prototype.connexion = function(action, messageJson) { function(data) { thisClient.chargerDonnees(data); }, - function() { + function(data) { thisClient.util.messageDialogue(data.error_message); thisClient.delCookie(); // suppression du cookie actuel, cas où le cookie du client ne permet pas une authentification - } + }, + false ); return this.authentifie(); }; @@ -389,22 +372,19 @@ euphorik.Client.prototype.flush = function(async) { var thisClient = this; var ok = true; - jQuery.ajax({ - async: async, - type: "POST", - url: "request", - dataType: "json", - data: this.util.jsonVersAction(this.getJSONProfile()), - success: - function(data) { - if (data.reply === "error") { - thisClient.util.messageDialogue(data.error_message); - ok = false; - } else { - thisClient.majBulle(); - } - } - }); + + this.communication.requete( + "set_profile", + this.getJSONProfile(), + function(data) { + thisClient.majBulle(); + }, + function(data) { + thisClient.util.messageDialogue(data.error_message); + ok = false; + }, + async + ); return ok; }; @@ -462,51 +442,15 @@ euphorik.Client.prototype.majLogo = function() { euphorik.Client.prototype.slap = function(userId, raison) { var thisClient = this; - - jQuery.ajax({ - type: "POST", - url: "request", - dataType: "json", - data: this.util.jsonVersAction({ - "header" : { "action" : "slap", "version" : euphorik.conf.versionProtocole }, - "cookie" : thisClient.cookie, - "user_id" : userId, - "reason" : raison - }), - success: - function(data) { - if (data.reply === "error") { - thisClient.util.messageDialogue(data.error_message); - } - } - }); + this.communication.requete("slap", { "cookie" : thisClient.cookie, "user_id" : userId, "reason" : raison }); }; -euphorik.Client.prototype.ban = function(userId, raison, minutes) -{ +euphorik.Client.prototype.ban = function(userId, raison, minutes) { var thisClient = this; // par défaut un ban correspond à 3 jours minutes = minutes || euphorik.conf.tempsBan; - - jQuery.ajax({ - type: "POST", - url: "request", - dataType: "json", - data: this.util.jsonVersAction({ - "header" : { "action" : "ban", "version" : euphorik.conf.versionProtocole }, - "cookie" : thisClient.cookie, - "duration" : minutes, - "user_id" : userId, - "reason" : raison - }), - success: - function(data) { - if (data.reply === "error") { - thisClient.util.messageDialogue(data.error_message); - } - } - }); + this.communication.requete("ban", { "cookie" : thisClient.cookie, "duration" : minutes, "user_id" : userId, "reason" : raison }); }; euphorik.Client.prototype.kick = function(userId, raison) { diff --git a/js/communication.js b/js/communication.js index db9beb8..e472ccb 100644 --- a/js/communication.js +++ b/js/communication.js @@ -16,47 +16,58 @@ // You should have received a copy of the GNU General Public License // along with Euphorik. If not, see . -// regroupe la partie communication client -> sevreur de euphorik. +// Regroupe la partie communication JSON client -> serveur de euphorik. +// Voir : http://dev.euphorik.ch/wiki/euk/Protocole /** * @param funError un fonction executé lors d'un réponse 'error' de la part du serveur, peut être redéfinit pour une requête. */ -euphorik.Communication = function(util, funError) { +euphorik.Communication = function(funError) { + this.funError = funError; } -euphorik.Communication.prototype.getBase = function(action) { - this.base = { - "header" : { "action" : action, "version" : euphorik.conf.versionProtocole } - }; -} - -euphorik.Communication.prototype.requete = function(action, json, funOk, funError, asynchrone) { +euphorik.Communication.prototype.requete = function(action, json, funOk, funError, asynchrone, paramsSupp) { + var thisCommunication = this; if (asynchrone === undefined) { asynchrone = true; // asynchrone par défaut } - var mess = this.getbase(action); + var mess = this.getBase(action); objectEach(json, function(nom, val) { mess[nom] = val; }); - - jQuery.ajax({ + + paramsAjax = { async: asynchrone, type: "POST", url: "request", dataType: "json", - data: { action : JSON.stringify(mess) } + data: { action : JSON.stringify(mess) }, success: function(data) { if (data.reply === "error") { if (funError) { funError(data); - } else if (this.funError) { - this.funError(data); - } + } else if (thisCommunication.funError) { + thisCommunication.funError(data); + } } else if (funOk) { funOk(data) } } - }); + }; + + if (paramsSupp) { + objectEach(paramsSupp, function(nom, val) { + paramsAjax[nom] = val; + }); + } + + jQuery.ajax(paramsAjax); +} + +euphorik.Communication.prototype.getBase = function(action) { + return { + "header" : { "action" : action, "version" : euphorik.conf.versionProtocole } + }; } diff --git a/js/euphorik.js b/js/euphorik.js index 4a9261a..00227cd 100755 --- a/js/euphorik.js +++ b/js/euphorik.js @@ -26,8 +26,8 @@ $(document).ready( function() { var fragment = new Fragment(); var formateur = new euphorik.Formateur(); - var util = new euphorik.Util(formateur); - var communication = new Communication(); + var util = new euphorik.Util(formateur); + var communication = new euphorik.Communication(function(data) { util.messageDialogue(data.error_message); }); var client = new euphorik.Client(util, communication); var pages = new euphorik.Pages(fragment); @@ -61,7 +61,7 @@ $(document).ready( // TODO : simplifier et pouvoir créer des liens par exemple : Conditions d'utilisation $("#footer .conditions").click(function(){ pages.afficherPage("conditions_utilisation"); }); - pages.ajouterPage(new euphorik.PageMinichat(client, formateur, util), true); + pages.ajouterPage(new euphorik.PageMinichat(client, formateur, util, communication), true); pages.ajouterPage(new euphorik.PageAdmin(client, formateur, util)); pages.ajouterPage(new euphorik.PageProfile(client, formateur, util)); pages.ajouterPage(new euphorik.PageRegister(client, formateur, util)); diff --git a/js/pageMinichat/conversation.js b/js/pageMinichat/conversation.js index 645c4d5..70f8d2b 100644 --- a/js/pageMinichat/conversation.js +++ b/js/pageMinichat/conversation.js @@ -318,7 +318,7 @@ euphorik.Conversation.prototype.attacherEventsSurMessage = function(element) { var top = $(this).offset().top var left = $(this).offset().left + $(this).width() - thisConversation.util.outilsMessage.width() thisConversation.util.outilsMessage.css("top", top + 1).css("left", left).prependTo(this).show(); - TODO + //TODO // // extraction d'une conversation /*if ($(event.target).is(".extraire")) { diff --git a/js/pageMinichat/pageMinichat.js b/js/pageMinichat/pageMinichat.js index d289a99..e952281 100755 --- a/js/pageMinichat/pageMinichat.js +++ b/js/pageMinichat/pageMinichat.js @@ -18,12 +18,13 @@ /*jslint laxbreak:true */ -euphorik.PageMinichat = function(client, formateur, util) { +euphorik.PageMinichat = function(client, formateur, util, communication) { this.nom = "minichat"; this.client = client; this.formateur = formateur; this.util = util; + this.communication = communication; this.commandes = new euphorik.Commandes(this.client); // permet d'éviter d'envoyer plusieurs messages simultanément en pressant @@ -226,22 +227,6 @@ euphorik.PageMinichat.prototype.decharger = function() { this.fragment.delVal("conv") }; - -euphorik.PageMinichat.prototype.getJSONMessage = function(pseudo, message) { - var repondA = []; - objectEach(this.conversations.messagesRepond, function(id) { - repondA.push(parseInt(id, 10)); - }); - - return { - "header" : { "action" : "put_message", "version" : euphorik.conf.versionProtocole }, - "cookie" : this.client.cookie, - "nick" : pseudo, - "content" : message, - "answer_to" : repondA - }; -}; - euphorik.PageMinichat.prototype.envoyerMessage = function(pseudo, message) { var thisPageMinichat = this; @@ -275,38 +260,52 @@ euphorik.PageMinichat.prototype.envoyerMessage = function(pseudo, message) { } this.envoieMessageEnCours = true; - jQuery.ajax({ - url : "request", - type: "POST", - data : this.util.jsonVersAction(this.getJSONMessage(pseudo, message)), - dataType : "json", - beforeSend : function(xmlHttpRequest) { - // TODO : ça marche ça ?? - xmlHttpRequest.setRequestHeader("X-Requested-With", ""); - }, - success : function(data, textStatus) { - if(data.reply === "ok") { - // TODO : revoir cette partie - // met à jour la classe des messages auquel repond celui ci (c'est un peu de la triche) TODO : ya mieux ? - objectEach(thisPageMinichat.conversations.messagesRepond, function(messId) { - thisPageMinichat.conversations.conversations.each(function(i, conv) { - var mess = conv.messagesParId[messId]; - if (mess) { - mess.clientARepondu = true; - $("#conversations #" + mess.getId(conv.getId())).addClass("repondu") - } - }); + this.communication.requete( + "put_message", + this.getJSONMessage(pseudo, message), + function() { + // TODO : revoir cette partie + // met à jour la classe des messages auquel repond celui ci (c'est un peu de la triche) TODO : ya mieux ? + objectEach(thisPageMinichat.conversations.messagesRepond, function(messId) { + thisPageMinichat.conversations.conversations.each(function(i, conv) { + var mess = conv.messagesParId[messId]; + if (mess) { + mess.clientARepondu = true; + $("#conversations #" + mess.getId(conv.getId())).addClass("repondu") + } }); - - $("form#posterMessage input.message").val(""); - thisPageMinichat.conversations.enleverMessagesRepond(); - } else if (data.reply === "error") { - thisPageMinichat.util.messageDialogue(data.error_message); - } + }); + $("form#posterMessage input.message").val(""); + thisPageMinichat.conversations.enleverMessagesRepond(); thisPageMinichat.envoieMessageEnCours = false; }, - error : function() { + function(data) { + thisPageMinichat.util.messageDialogue(data.error_message); thisPageMinichat.envoieMessageEnCours = false; + }, + true, + { + beforeSend : function(xmlHttpRequest) { + // TODO : ça marche ça ?? + xmlHttpRequest.setRequestHeader("X-Requested-With", ""); + }, + error : function() { + thisPageMinichat.envoieMessageEnCours = false; + } } + ); +}; + +euphorik.PageMinichat.prototype.getJSONMessage = function(pseudo, message) { + var repondA = []; + objectEach(this.conversations.messagesRepond, function(id) { + repondA.push(parseInt(id, 10)); }); + + return { + "cookie" : this.client.cookie, + "nick" : pseudo, + "content" : message, + "answer_to" : repondA + }; }; diff --git a/js/pageRegister.js b/js/pageRegister.js index 37f7705..e28e3d3 100755 --- a/js/pageRegister.js +++ b/js/pageRegister.js @@ -69,7 +69,7 @@ euphorik.PageRegister.prototype.charger = function() { thisPage.util.messageDialogue("Un mot de passe est obligatoire"); } else if (password !== passwordRe) { thisPage.util.messageDialogue("Les mots de passes ne correspondent pas"); - } else if(thisPage.client.enregistrement(login, thisPage.util.md5(password))) { + } else if (thisPage.client.enregistrement(login, thisPage.util.md5(password))) { thisPage.util.messageDialogue("Enregistrement réussi"); thisPage.pages.afficherPage("minichat"); } diff --git a/modules/erl/euphorik_bd_admin.erl b/modules/erl/euphorik_bd_admin.erl index 20da1fa..616ac4d 100644 --- a/modules/erl/euphorik_bd_admin.erl +++ b/modules/erl/euphorik_bd_admin.erl @@ -141,7 +141,8 @@ reset() -> User = #user{id = 0, profile = #profile{pseudo = "Sys"}, login = "Sys", date_creation = now(), date_derniere_connexion = now(), ek_master = true}, mnesia:write(User), User - end). + end), + peupler_texte(). peupler_texte() -> @@ -153,9 +154,9 @@ peupler_texte() -> mnesia:write(#texte{ id = 50, fr = "Impossible de mettre à jour le profile"}), mnesia:write(#texte{ id = 60, fr = "timeout"}), mnesia:write(#texte{ id = 70, fr = "Page inconnue"}), - mnesia:write(#texte{ id = 80, fr = "Vous êtes banni pour encore ~w"}), + mnesia:write(#texte{ id = 80, fr = "Vous êtes banni pour encore ~s"}), mnesia:write(#texte{ id = 90, fr = "Message vide"}), - mnesia:write(#texte{ id = 100, fr = "Impossible d'ajouter un nouveau message. Raison : ~w"}), + mnesia:write(#texte{ id = 100, fr = "Impossible d'ajouter un nouveau message. Raison : ~s"}), mnesia:write(#texte{ id = 110, fr = "Utilisateur inconnu"}), mnesia:write(#texte{ id = 120, fr = "Il n'est pas possible de s'auto bannir"}), mnesia:write(#texte{ id = 130, fr = "L'utilisateur est lui même un ekMaster"}), diff --git a/modules/erl/euphorik_protocole.erl b/modules/erl/euphorik_protocole.erl index 82a4fce..4f30ae7 100755 --- a/modules/erl/euphorik_protocole.erl +++ b/modules/erl/euphorik_protocole.erl @@ -426,7 +426,7 @@ put_message( {ok, User} -> case euphorik_bd:est_banni(User#user.id) of {true, Temps_restant} -> - erreur(80, format_minutes(Temps_restant)); + erreur(80, [format_minutes(Temps_restant)]); _ -> Strip_content = string:strip(Content), if Strip_content =:= [] -> @@ -435,7 +435,7 @@ put_message( % attention : non-atomique (update_pseudo+nouveau_message) euphorik_bd:update_pseudo_user(User#user.id, Nick), case euphorik_bd:nouveau_message(Strip_content, User#user.id, Answer_to) of - {erreur, R} -> erreur(100, R); + {erreur, R} -> erreur(100, [R]); _ -> json_reponse_ok() end @@ -531,9 +531,9 @@ put_troll( {ok, User = #user{ek_master = true}} -> case euphorik_bd:put_troll(User#user.id, Content) of max_troll_reached_per_user -> - erreur(180, ?NB_MAX_TROLL_WAITING_BY_USER); + erreur(180, [?NB_MAX_TROLL_WAITING_BY_USER]); max_troll_reached -> - erreur(190, ?NB_MAX_TROLL_WAITING); + erreur(190, [?NB_MAX_TROLL_WAITING]); _Id -> json_reponse_ok() end;