1 //
  2 // The Antville Project
  3 // http://code.google.com/p/antville
  4 //
  5 // Copyright 2001-2007 by The Antville People
  6 //
  7 // Licensed under the Apache License, Version 2.0 (the ``License'');
  8 // you may not use this file except in compliance with the License.
  9 // You may obtain a copy of the License at
 10 //
 11 //    http://www.apache.org/licenses/LICENSE-2.0
 12 //
 13 // Unless required by applicable law or agreed to in writing, software
 14 // distributed under the License is distributed on an ``AS IS'' BASIS,
 15 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 16 // See the License for the specific language governing permissions and
 17 // limitations under the License.
 18 //
 19 // $Revision$
 20 // $LastChangedBy$
 21 // $LastChangedDate$
 22 // $URL$
 23 //
 24 
 25 /**
 26  * @fileOverview Defines the Api prototype.
 27  */
 28 
 29 /**
 30  * 
 31  * @param {Site} site
 32  * @param {User} user
 33  */
 34 Api.constrain = function(site, user) {
 35    res.handlers.site = site;
 36    res.handlers.membership = Membership.getByName(user.name);
 37    return;
 38 }
 39 
 40 /** @ignore */
 41 Api.dispatch = function() {
 42 }
 43 
 44 /**
 45  * 
 46  * @param {String} name
 47  * @param {String} password
 48  * @throws {Error}
 49  * @returns {User} 
 50  */
 51 Api.getUser = function(name, password) {
 52    var user = User.getByName(name);
 53    if (!user) {
 54       throw Error("User " + name + " does not exist on this server");
 55    } else if (user.hash !== String(password + user.salt).md5()) {
 56       throw Error("Authentication failed for user " + name);
 57    } else if (user.status === User.BLOCKED) {
 58       throw Error("The user account " + name + " is currently blocked");
 59    }
 60    session.login(user);
 61    return user;
 62 }
 63 
 64 /**
 65  * 
 66  * @param {String} name
 67  * @throws {Error}
 68  * @returns {Site}
 69  */
 70 Api.getSite = function(name) {
 71    var site = Site.getByName(String(name));
 72    if (!site) {
 73       throw Error("Site " + name + " does not exist on this server");
 74    } else if (site.status === Site.BLOCKED) {
 75       throw Error("The site " + name + " is blocked");
 76    }
 77    return site;
 78 }
 79 
 80 /**
 81  * 
 82  * @param {Number} id
 83  * @throws {Error}
 84  * @returns {Story}
 85  */
 86 Api.getStory = function(id) {
 87    var story = Story.getById(id);
 88    if (!story) {
 89       throw Error("Story #" + id + " does not exist on this server");
 90    }
 91    return story;
 92 }
 93 
 94 /**
 95  * @name Api
 96  * @constructor
 97  * @extends HopObject
 98  */
 99 
100 /**
101  * @returns {Boolean}
102  */
103 Api.prototype.getPermission = function(){
104    return true;
105 }
106 
107 Api.prototype.main_action = function() {
108    res.data.title = gettext("Application Programming Interfaces");
109    res.data.body = this.renderSkinAsString("$Api#main");
110    res.handlers.site.renderSkin("Site#page");
111    return;
112 }
113 
114 Api.prototype.callback_action = function() {
115    var ping = function(data) {
116       if (data.type !== "Story" && data.type !== "Comment") {
117          return;
118       }
119       var remote = new Remote("http://rpc.weblogs.com/RPC2");
120       var call = remote.weblogUpdates.ping(data.site, data.origin);
121       if (call.error || call.result.flerror) {
122          app.debug("Error invoking weblogs.com ping() method for " + 
123                data.site + ": " + call.error || call.result.message);
124       } else {
125          app.debug(call.result);
126       }
127       return;
128    };
129 
130    if (req.isGet()) {
131       res.data.title = gettext("Default Callback Url");
132       res.data.body = this.renderSkinAsString("$Api#callback", 
133             {name: req.action, code: ping.toString()});
134       res.handlers.site.renderSkin("Site#page");
135    } else if (req.isPost()) {
136       app.debug("Invoked default callback with POST params: " + req.postParams);
137       app.invokeAsync(this, ping, [req.postParams], 1000);
138    }
139    return;
140 }
141 
142 /**
143  * 
144  * @param {String} methodName
145  * @throws {Error}
146  */
147 Api.prototype.main_action_xmlrpc = function(methodName) {
148    if (!methodName) {
149       return false;
150    } 
151    var parts = methodName.split(".");
152    var method = parts[1];
153    if (method && !method.startsWith("_")) {
154       var handler = Api[parts[0]];
155       if (handler && handler[method]) {
156          var args = Array.prototype.splice.call(arguments, 1);
157          return handler[method].apply(null, args);
158       }
159    }
160    throw Error("Method " + methodName + "() is not implemented");
161    return;
162 }
163