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 return user; 61 } 62 63 /** 64 * 65 * @param {String} name 66 * @throws {Error} 67 * @returns {Site} 68 */ 69 Api.getSite = function(name) { 70 var site = Site.getByName(String(name)); 71 if (!site) { 72 throw Error("Site " + name + " does not exist on this server"); 73 } else if (site.status === Site.BLOCKED) { 74 throw Error("The site " + name + " is blocked"); 75 } 76 return site; 77 } 78 79 /** 80 * 81 * @param {Number} id 82 * @throws {Error} 83 * @returns {Story} 84 */ 85 Api.getStory = function(id) { 86 var story = Story.getById(id); 87 if (!story) { 88 throw Error("Story #" + id + " does not exist on this server"); 89 } 90 return story; 91 } 92 93 /** 94 * @name Api 95 * @constructor 96 * @extends HopObject 97 */ 98 99 /** 100 * @returns {Boolean} 101 */ 102 Api.prototype.getPermission = function(){ 103 return true; 104 } 105 106 Api.prototype.main_action = function() { 107 res.data.title = "Supported Application Programming Interfaces"; 108 res.data.body = this.renderSkinAsString("$Api#main"); 109 res.handlers.site.renderSkin("Site#page"); 110 return; 111 } 112 113 Api.prototype.callback_action = function() { 114 var ping = function(data) { 115 if (data.type !== "Story" && data.type !== "Comment") { 116 return; 117 } 118 var remote = new Remote("http://rpc.weblogs.com/RPC2"); 119 var call = remote.weblogUpdates.ping(data.site, data.origin); 120 if (call.error || call.result.flerror) { 121 app.debug("Error invoking weblogs.com ping() method for " + 122 data.site + ": " + call.error || call.result.message); 123 } else { 124 app.debug(call.result); 125 } 126 return; 127 }; 128 129 if (req.isGet()) { 130 res.data.title = "Default callback"; 131 res.data.body = this.renderSkinAsString("$Api#callback", 132 {name: req.action, code: ping.toString()}); 133 res.handlers.site.renderSkin("Site#page"); 134 } else if (req.isPost()) { 135 app.debug("Invoked default callback with POST params: " + req.postParams); 136 app.invokeAsync(this, ping, [req.postParams], 1000); 137 } 138 return; 139 } 140 141 /** 142 * 143 * @param {String} methodName 144 * @throws {Error} 145 */ 146 Api.prototype.main_action_xmlrpc = function(methodName) { 147 if (!methodName) { 148 return false; 149 } 150 var parts = methodName.split("."); 151 var method = parts[1]; 152 if (method && !method.startsWith("_")) { 153 var handler = Api[parts[0]]; 154 if (handler && handler[method]) { 155 var args = Array.prototype.splice.call(arguments, 1); 156 return handler[method].apply(null, args); 157 } 158 } 159 throw Error("Method " + methodName + "() is not implemented"); 160 return; 161 } 162