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 Movable Type's XML-RPC API.
 27  * See http://www.sixapart.com/developers/xmlrpc/movable_type_api for details.
 28  */
 29 
 30 /** @namespace */
 31 Api.mt = {};
 32 
 33 /**
 34  * 
 35  * @param {Number} id
 36  * @param {String} name
 37  * @param {String} password
 38  * @param {Number} limit
 39  * @throws {Error}
 40  * @returns {Object[]}
 41  */
 42 Api.mt.getRecentPostTitles = function(id, name, password, limit) {
 43    var site = Api.getSite(id);
 44    var user = Api.getUser(name, password);
 45 
 46    Api.constrain(site, user);
 47    if (!site.stories.getPermission("main")) {
 48       throw Error("Permission denied for user " + user.name + 
 49             " to get recent post titles from site " + site.name);
 50    }
 51 
 52    var result = [];
 53    var stories = res.handlers.membership.stories;
 54    var max = Math.min(stories.size(), Number(limit) || Infinity, 20);
 55    for each (var story in stories.list(0, max)) {
 56       result.push({
 57          postid: story._id,
 58          username: story.creator.name,
 59          dateCreated: story.created,
 60          title: story.getTitle()	
 61       });
 62    }
 63    return result;
 64 }
 65 
 66 /**
 67  * 
 68  * @param {Number} id
 69  * @param {String} name
 70  * @param {String} password
 71  * @throws {Error}
 72  * @returns {Object[]}
 73  */
 74 Api.mt.getCategoryList = function(id, name, password) {
 75    var site = Api.getSite(id);
 76    var user = Api.getUser(name, password);
 77 
 78    Api.constrain(site, user);
 79    if (!site.stories.getPermission("main")) {
 80       throw Error("Permission denied for user " + user.name + 
 81             " to access site " + site.name);
 82    }
 83    
 84    var result = [];
 85    var tags = site.getTags("tags", Tags.ALL).list();
 86    for each (var tag in tags) {
 87      result.push({
 88         categoryId: tag.name, // FIXME: tag._id,
 89         categoryName: tag.name
 90      });
 91    }
 92    return result;
 93 }
 94 
 95 /**
 96  * 
 97  * @param {Number} id
 98  * @param {String} name
 99  * @param {String} password
100  * @throws {Error}
101  * @returns {Object[]}
102  */
103 Api.mt.getPostCategories = function(id, name, password) {
104    var story = Api.getStory(id);
105    var user = Api.getUser(name, password);
106 
107    Api.constrain(story.site, user);
108    if (!story.getPermission("main")) {
109       throw Error("Permission denied for user " + name + 
110             " to access story #" + id);
111    }
112    
113    var result = [];
114    for each (var tag in story.getTags()) {
115       result.push({
116          categoryId: tag,
117          categoryName: tag,
118          isPrimary: true
119       });
120    }
121    return result;
122 }
123 
124 // FIXME: What kind of stupid API is this?
125 /**
126  * 
127  * @param {Number} id
128  * @param {String} name
129  * @param {String} password
130  * @throws {Error}
131  * @returns {Boolean}
132  */
133 Api.mt.publishPost = function(id, name, password) {
134    var story = Api.getStory(id);
135    var user = Api.getUser(name, password);
136 
137    Api.constrain(story.site, user);
138    if (!story.getPermission("edit")) {
139       throw Error("Permission denied for user " + name + 
140             " to edit story #" + id);
141    }
142 
143    story.mode = Story.FEATURED;
144    return true;   
145 }
146 
147 /**
148  * 
149  * @param {Number} id
150  * @param {String} name
151  * @param {String} password
152  * @param {String[]} categories
153  * @throws {Error}
154  * @returns {Boolean}
155  */
156 Api.mt.setPostCategories = function(id, name, password, categories) {
157    if (!categories || !categories.length) {
158       return;
159    }
160 
161    var story = Api.getStory(id);
162    var user = Api.getUser(name, password);
163 
164    Api.constrain(story.site, user);
165    if (!story.getPermission("edit")) {
166       throw Error("Permission denied for user " + name + 
167             " to edit story #" + id);
168    }
169 
170    story.setTags(categories);
171    return true;
172 }
173 
174 /**
175  * 
176  * @returns {Array}
177  */
178 Api.mt.supportedTextFilters = function() {
179    return [];
180 }
181 
182 /**
183  * 
184  * @returns {Array}
185  */
186 Api.mt.getTrackbackPings = function() {
187    return [];
188 }
189 
190 /**
191  * 
192  * @returns {String[]}
193  */
194 Api.mt.supportedMethods = function() {
195    var result = [];
196    for (var method in Api.mt) {
197       result.push(method);
198    }
199    return result.sort();
200 }
201