1 // The Antville Project
  2 // http://code.google.com/p/antville
  3 //
  4 // Copyright 2007-2011 by Tobi Schäfer.
  5 //
  6 // Copyright 2001–2007 Robert Gaggl, Hannes Wallnöfer, Tobi Schäfer,
  7 // Matthias & Michael Platzer, Christoph Lincke.
  8 //
  9 // Licensed under the Apache License, Version 2.0 (the ``License'');
 10 // you may not use this file except in compliance with the License.
 11 // You may obtain a copy of the License at
 12 //
 13 //    http://www.apache.org/licenses/LICENSE-2.0
 14 //
 15 // Unless required by applicable law or agreed to in writing, software
 16 // distributed under the License is distributed on an ``AS IS'' BASIS,
 17 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 18 // See the License for the specific language governing permissions and
 19 // limitations under the License.
 20 //
 21 // $Revision$
 22 // $Author$
 23 // $Date$
 24 // $URL$
 25 
 26 /**
 27  * @fileOverview Methods that implement Blogger's XML-RPC API.
 28  * See http://goo.gl/u8lZZ for further details.
 29  * The blogger.getTemplate and blogger.setTemplate methods are not supported
 30  */
 31 
 32 /** @namespace */
 33 Api.blogger = {};
 34 
 35 /**
 36  * 
 37  * @param {String} content
 38  */
 39 Api.blogger._getContentParts = function(content) {
 40    content && (content = content.trim());
 41    content || (content = String.EMPTY);
 42    var result = {};
 43    if (!content.startsWith("<title>")) {
 44       result.text = content;
 45    } else {
 46       var pos = content.lastIndexOf("</title>");
 47       if (pos > 0) {
 48          result.title = content.substring(7, pos);
 49          result.text = content.substring (pos + 8); 
 50       } else {
 51          result.text = content;
 52       }
 53    }
 54    return result;
 55 }
 56 
 57 /**
 58  * 
 59  * @param {String} appKey
 60  * @param {String} name
 61  * @param {String} password
 62  * @throws {Error}
 63  * @returns {Object} Properties: userid, nickname and url
 64  */
 65 Api.blogger.getUserInfo = function(appKey, name, password) {
 66    var user = User.getByName(name);
 67    if (!user) {
 68       throw Error("User " + name + " does not exist on this server");
 69    }
 70    return {
 71       userid: name,
 72       nickname: name,
 73       url: user.url
 74    }
 75 }
 76 
 77 /**
 78  * 
 79  * @param {String} appKey
 80  * @param {String} name
 81  * @param {String} password
 82  * @returns {Object[]} A list of objects with the properties blogid, blogName and 
 83  * url
 84  */
 85 Api.blogger.getUsersBlogs = function(appKey, name, password) {
 86    var user = Api.getUser(name, password);
 87    var result = [];
 88    user.forEach(function() {
 89       Api.constrain(this.site, user);
 90       if (this.site.stories.getPermission("create")) {
 91          result.push({
 92             blogid: this.site.name,
 93             blogName: this.site.title,
 94             url: this.site.href()
 95          });
 96       }
 97       return;
 98    });
 99    return result;
100 }
101 
102 /**
103  * 
104  * @param {String} appKey
105  * @param {Number} id
106  * @param {String} name
107  * @param {String} password
108  * @param {Number} limit
109  * @throws {Error}
110  * @returns {Object[]} A list of objects with the properties postid, userid,
111  * dateCreated and content
112  */
113 Api.blogger.getRecentPosts = function(appKey, id, name, password, limit) {
114    var site = Api.getSite(id);
115    var user = Api.getUser(name, password);
116 
117    Api.constrain(site, user);
118    if (!site.stories.getPermission("main")) {
119       throw Error("Permission denied for user " + user.name + 
120             " to get recent posts of site " + site.name);
121    }
122 
123    var result = [];
124    var stories = res.handlers.membership.stories;
125    var max = Math.min(stories.size(), Number(limit) || Infinity, 20);
126    for each (var story in stories.list(0, max)) {
127       result.push({
128          postid: story._id,
129          userid: story.creator.name,
130          dateCreated: story.created,
131          content: story.title ? "<title>" + story.title + 
132                "</title>" + story.text : story.text
133       });
134    }
135    return result;
136 }
137 
138 /**
139  * 
140  * @param {String} appKey
141  * @param {Number} id
142  * @param {String} name
143  * @param {String} password
144  * @throws {Error}
145  * @returns {Object} Properties: content, userid, postid, dateCreated
146  */
147 Api.blogger.getPost = function(appKey, id, name, password) {
148    var story = Api.getStory(id);
149    var user = Api.getUser(name, password);
150    
151    Api.constrain(story.site, user);
152    if (!story.getPermission("main")) {
153       throw Error("Permission denied for user " + name + 
154             " to get post #" + id);
155    }
156 
157    return {
158       content: story.title ? html.elementAsString("title", story.title) + 
159             story.text : story.text,
160       userid: story.creator.name,
161       postid: story._id,
162       dateCreated: story.created
163    }
164 }
165 
166 /**
167  * 
168  * @param {String} appKey
169  * @param {Number} id
170  * @param {String} name
171  * @param {String} password
172  * @param {String} content
173  * @param {Boolean} publish
174  * @throws {Error}
175  * @returns {Number} The ID of the new story
176  */
177 Api.blogger.newPost = function(appKey, id, name, password, content, publish) {
178    var site = Api.getSite(id);
179    var user = Api.getUser(name, password);
180    
181    Api.constrain(site, user);
182    if (!site.stories.getPermission("create")) {
183       throw Error("Permission denied for user " + user.name + 
184             " to add a post to site " + site.name);
185    }
186 
187    var parts = Api.blogger._getContentParts(content);
188 
189    var story = Story.add({
190       title: parts.title,
191       text: parts.text,
192       status: publish ? Story.PUBLIC : Story.CLOSED,
193       mode: Story.FEATURED
194    }, site, user);
195 
196    return story._id;
197 }
198 
199 /**
200  * 
201  * @param {String} appKey
202  * @param {Number} id
203  * @param {String} name
204  * @param {String} password
205  * @param {String} content
206  * @param {Boolean} publish
207  * @throws {Error}
208  * @returns {Boolean} Always true
209  */
210 Api.blogger.editPost = function(appkey, id, name, password, content, publish) {
211    var story = Api.getStory(id);
212    var user = Api.getUser(name, password);
213 
214    Api.constrain(story.site, user);
215    if (!story.getPermission("edit")) {
216       throw Error("Permission denied for user " + name + 
217             " to edit post #" + id);
218    }
219 
220    var parts = Api.blogger._getContentParts(content);
221    story.update({
222       title: parts.title,
223       text: parts.text,
224       status: publish ? Story.PUBLIC : Story.CLOSED,
225       modifier: user,
226       modified: new Date
227    });
228    return true;
229 }
230 
231 /**
232  * 
233  * @param {String} appKey
234  * @param {Number} id
235  * @param {String} name
236  * @param {String} password
237  * @throws {Error}
238  * @returns {Boolean} Always true
239  */
240 Api.blogger.deletePost = function(appKey, id, name, password) {
241    var story = Api.getStory(id);
242    var user = Api.getUser(name, password);
243 
244    Api.constrain(story.site, user);
245    if (!story.getPermission("delete")) {
246       throw Error("Permission denied for user " + name + 
247             " to delete story #" + id);
248    }
249 
250    Story.remove.call(story);
251    return true;
252 }
253