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 Methods that implement Blogger's XML-RPC API. 27 * See http://www.blogger.com/developers/api/1_docs/ for further details. 28 * The blogger.getTemplate and blogger.setTemplate methods are not supported 29 */ 30 31 /** @namespace */ 32 Api.blogger = {}; 33 34 /** 35 * 36 * @param {String} content 37 */ 38 Api.blogger._getContentParts = function(content) { 39 content && (content = content.trim()); 40 content || (content = String.EMPTY); 41 var result = {}; 42 if (!content.startsWith("<title>")) { 43 result.text = content; 44 } else { 45 var pos = content.lastIndexOf("</title>"); 46 if (pos > 0) { 47 result.title = content.substring(7, pos); 48 result.text = content.substring (pos + 8); 49 } else { 50 result.text = content; 51 } 52 } 53 return result; 54 } 55 56 /** 57 * 58 * @param {String} appKey 59 * @param {String} name 60 * @param {String} password 61 * @throws {Error} 62 * @returns {Object} Properties: userid, nickname and url 63 */ 64 Api.blogger.getUserInfo = function(appKey, name, password) { 65 var user = User.getByName(name); 66 if (!user) { 67 throw Error("User " + name + " does not exist on this server"); 68 } 69 return { 70 userid: name, 71 nickname: name, 72 url: user.url 73 } 74 } 75 76 /** 77 * 78 * @param {String} appKey 79 * @param {String} name 80 * @param {String} password 81 * @returns {Object[]} A list of objects with the properties blogid, blogName and 82 * url 83 */ 84 Api.blogger.getUsersBlogs = function(appKey, name, password) { 85 var user = Api.getUser(name, password); 86 var result = []; 87 user.forEach(function() { 88 Api.constrain(this.site, user); 89 if (this.site.stories.getPermission("create")) { 90 result.push({ 91 blogid: this.site.name, 92 blogName: this.site.title, 93 url: this.site.href() 94 }); 95 } 96 return; 97 }); 98 return result; 99 } 100 101 /** 102 * 103 * @param {String} appKey 104 * @param {Number} id 105 * @param {String} name 106 * @param {String} password 107 * @param {Number} limit 108 * @throws {Error} 109 * @returns {Object[]} A list of objects with the properties postid, userid, 110 * dateCreated and content 111 */ 112 Api.blogger.getRecentPosts = function(appKey, id, name, password, limit) { 113 var site = Api.getSite(id); 114 var user = Api.getUser(name, password); 115 116 Api.constrain(site, user); 117 if (!site.stories.getPermission("main")) { 118 throw Error("Permission denied for user " + user.name + 119 " to get recent posts of site " + site.name); 120 } 121 122 var result = []; 123 var stories = res.handlers.membership.stories; 124 var max = Math.min(stories.size(), Number(limit) || Infinity, 20); 125 for each (var story in stories.list(0, max)) { 126 result.push({ 127 postid: story._id, 128 userid: story.creator.name, 129 dateCreated: story.created, 130 content: story.title ? "<title>" + story.title + 131 "</title>" + story.text : story.text 132 }); 133 } 134 return result; 135 } 136 137 /** 138 * 139 * @param {String} appKey 140 * @param {Number} id 141 * @param {String} name 142 * @param {String} password 143 * @throws {Error} 144 * @returns {Object} Properties: content, userid, postid, dateCreated 145 */ 146 Api.blogger.getPost = function(appKey, id, name, password) { 147 var story = Api.getStory(id); 148 var user = Api.getUser(name, password); 149 150 Api.constrain(story.site, user); 151 if (!story.getPermission("main")) { 152 throw Error("Permission denied for user " + name + 153 " to get post #" + id); 154 } 155 156 return { 157 content: story.title ? html.elementAsString("title", story.title) + 158 story.text : story.text, 159 userid: story.creator.name, 160 postid: story._id, 161 dateCreated: story.created 162 } 163 } 164 165 /** 166 * 167 * @param {String} appKey 168 * @param {Number} id 169 * @param {String} name 170 * @param {String} password 171 * @param {String} content 172 * @param {Boolean} publish 173 * @throws {Error} 174 * @returns {Number} The ID of the new story 175 */ 176 Api.blogger.newPost = function(appKey, id, name, password, content, publish) { 177 var site = Api.getSite(id); 178 var user = Api.getUser(name, password); 179 180 Api.constrain(site, user); 181 if (!site.stories.getPermission("create")) { 182 throw Error("Permission denied for user " + user.name + 183 " to add a post to site " + site.name); 184 } 185 186 var parts = Api.blogger._getContentParts(content); 187 var story = new Story; 188 story.site = site; 189 story.creator = user; 190 story.update({ 191 title: parts.title, 192 text: parts.text, 193 status: publish ? Story.PUBLIC : Story.CLOSED, 194 mode: Story.FEATURED 195 }); 196 site.stories.add(story); 197 return story._id; 198 } 199 200 /** 201 * 202 * @param {String} appKey 203 * @param {Number} id 204 * @param {String} name 205 * @param {String} password 206 * @param {String} content 207 * @param {Boolean} publish 208 * @throws {Error} 209 * @returns {Boolean} Always true 210 */ 211 Api.blogger.editPost = function(appkey, id, name, password, content, publish) { 212 var story = Api.getStory(id); 213 var user = Api.getUser(name, password); 214 215 Api.constrain(story.site, user); 216 if (!story.getPermission("edit")) { 217 throw Error("Permission denied for user " + name + 218 " to edit post #" + id); 219 } 220 221 var parts = Api.blogger._getContentParts(content); 222 story.update({ 223 title: parts.title, 224 text: parts.text, 225 status: publish ? Story.PUBLIC : Story.CLOSED, 226 modifier: user, 227 modified: new Date 228 }); 229 return true; 230 } 231 232 /** 233 * 234 * @param {String} appKey 235 * @param {Number} id 236 * @param {String} name 237 * @param {String} password 238 * @throws {Error} 239 * @returns {Boolean} Always true 240 */ 241 Api.blogger.deletePost = function(appKey, id, name, password) { 242 var story = Api.getStory(id); 243 var user = Api.getUser(name, password); 244 245 Api.constrain(story.site, user); 246 if (!story.getPermission("delete")) { 247 throw Error("Permission denied for user " + name + 248 " to delete story #" + id); 249 } 250 251 Story.remove.call(story); 252 return true; 253 } 254