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.xmlrpc.com/metaWeblogApi for further details.
 28  */
 29 
 30 /** @namespace */
 31 Api.metaWeblog = {}
 32 
 33 /**
 34  * @param {Story} story
 35  * @returns {Object}
 36  */
 37 Api.metaWeblog._getStruct = function(story) {
 38    return {
 39       userid: story.creator.name,
 40       postid: story._id,
 41       dateCreated: story.created,
 42       title: story.title,
 43       description: story.text,
 44       categories: story.getTags(),
 45       flNotOnHomePage: story.mode === Story.HIDDEN ? true : false,
 46       link: story.href(),
 47       permaLink: story.href(),
 48       mt_excerpt: null, // FIXME: What are these "mt_" prefixed properties?
 49       mt_text_more: null,
 50       mt_allow_comments: story.commentMode === Story.OPEN ? 1 : 0,
 51       mt_allow_pings: 0,
 52       mt_convert_breaks: null,
 53       mt_keywords: null
 54    }
 55 }
 56 
 57 /**
 58  * 
 59  * @param {Number} id
 60  * @param {String} name
 61  * @param {String} password
 62  * @param {Number} limit
 63  * @throws {Error}
 64  * @returns {Object[]}
 65  */
 66 Api.metaWeblog.getRecentPosts = function(id, name, password, limit) {
 67    var site = Api.getSite(id);
 68    var user = Api.getUser(name, password);
 69 
 70    Api.constrain(site, user);
 71    if (!site.stories.getPermission("main")) {
 72       throw Error("Permission denied for user " + user.name + 
 73             " to get recent posts from site " + site.name);
 74    }
 75 
 76    var result = [];
 77    var stories = res.handlers.membership.stories;
 78    var max = Math.min(stories.size(), Number(limit) || Infinity, 20);
 79    for each (var story in stories.list(0, max)) {
 80       result.push(Api.metaWeblog._getStruct(story));
 81    }
 82    return result;
 83 }
 84 
 85 /**
 86  * 
 87  * @param {Number} id
 88  * @param {String} name
 89  * @param {String} password
 90  * @throws {Error}
 91  * @returns {Object}
 92  */
 93 Api.metaWeblog.getPost = function(id, name, password) {
 94    var story = Api.getStory(id);
 95    var user = Api.getUser(name, password);
 96    Api.constrain(story.site, user);
 97    if (!story.getPermission("main")) { 
 98       throw Error("Permission denied for user " + name + 
 99             " to get post #" + id);
100    }
101    return Api.metaWeblog._getStruct(story);
102 }
103 
104 /**
105  * 
106  * @param {Number} id
107  * @param {String} name
108  * @param {String} password
109  * @param {String} content
110  * @param {Boolean} publish
111  * @throws {Error}
112  * @returns {Number}
113  */
114 Api.metaWeblog.newPost = function(id, name, password, content, publish) {
115    var site = Api.getSite(id);
116    var user = Api.getUser(name, password);
117    
118    Api.constrain(site, user);
119    if (!site.stories.getPermission("create")) {
120       throw Error("Permission denied for user " + user.name + 
121             " to add a post to site " + site.name);
122    }
123    
124    var story = new Story;
125    story.site = site;
126    story.creator = user;
127    story.update({
128       title: content.title,
129       text: content.description,
130       status: publish ? Story.PUBLIC : Story.CLOSED,
131       mode: content.flNotOnHomePage ? Story.HIDDEN : Story.FEATURED,
132       commentMode: content.discussions === 0 ? Story.CLOSED : Story.OPEN,
133       tags: content.categories
134    });
135    site.stories.add(story);
136    return story._id;
137 }
138 
139 /**
140  * 
141  * @param {Number} id
142  * @param {String} name
143  * @param {String} password
144  * @param {String} content
145  * @param {Boolean} publish
146  * @throws {Error}
147  * @returns {Boolean}
148  */
149 Api.metaWeblog.editPost = function(id, name, password, content, publish) {
150    var story = Api.getStory(id);
151    var user = Api.getUser(name, password);
152 
153    Api.constrain(story.site, user);
154    if (!story.getPermission("edit")) {
155       throw Error("Permission denied for user " + name + 
156             " to edit post #" + id);         
157    }
158 
159    story.update({
160       title: content.title,
161       text: content.description,
162       status: publish ? Story.PUBLIC : Story.CLOSED,
163       mode: content.flNotOnHomePage ? Story.HIDDEN : Story.FEATURED,
164       commentMode: content.discussions ? Story.OPEN : Story.CLOSED,
165       tags: content.categories
166    });
167    return true;
168 }
169 
170 /**
171  * 
172  * @param {Number} id
173  * @param {String} name
174  * @param {String} password
175  * @throws {Error}
176  * @returns {Object[]}
177  */
178 Api.metaWeblog.getCategories = function(id, name, password) {
179    var site = Api.getSite(id);
180    var user = Api.getUser(name, password);
181 
182    Api.constrain(site, user);
183    if (!site.stories.getPermission("main")) {
184       throw Error("Permission denied for user " + user.name + 
185             " to get categories from site " + site.name);
186    }
187 
188    var result = [];
189    var tags = site.getTags("tags", Tags.ALL).list();
190    for each (var tag in tags) {
191      result.push({
192         description: tag.name,
193         htmlUrl: tag.href(),
194         rssUrl: tag.href("rss")
195      });
196    }
197    return result;
198 }
199 
200 /**
201  * 
202  * @param {Number} id
203  * @param {String} name
204  * @param {String} password
205  * @param {String} media
206  * @throws {Error}
207  * @returns {Object}
208  */
209 Api.metaWeblog.newMediaObject = function(id, name, password, media) {
210    var site = Api.getSite(id);
211    var user = Api.getUser(name, password);
212 
213    Api.constrain(site, user);
214 
215    var result = {};
216    var data = {};
217    if (media.type.toLowerCase().startsWith("image/")) {
218       if (!site.images.getPermission("create")) {
219          throw Error("Permission denied for user " + user.name + 
220                " to add a media object to site " + site.name);
221       }
222       data.file = new Packages.helma.util.MimePart(media.name, 
223             media.bits, media.type);
224       data.file_origin = media.name;
225       data.description = media.description;   
226       var image = new Image;
227       image.site = site;
228       image.creator = user;
229       image.update(data);
230       site.images.add(image);
231       result.url = image.getUrl();
232    } else {
233       if (!site.files.getPermission("create")) {
234          throw Error("Permission denied for user " + user.name + 
235                " to add a media object to site " + site.name);
236       }
237       data.file = new Packages.helma.util.MimePart(media.name, 
238             media.bits, media.type);
239       data.file_origin = media.name;
240       data.description = media.description;
241       var file = new File;
242       file.site = site;
243       file.creator = file.modifier = user;
244       file.update(data);
245       site.files.add(file);
246       result.url = file.getUrl();
247    }
248    
249    return result;
250 }
251