2011-08-03 20:31:25 +02:00
/ * *
* This module provides all API functions
* /
/ *
2011-08-11 16:26:41 +02:00
* 2011 Peter 'Pita' Martischka ( Primary Technology Ltd )
2011-08-03 20:31:25 +02:00
*
* Licensed under the Apache License , Version 2.0 ( the "License" ) ;
* you may not use this file except in compliance with the License .
* You may obtain a copy of the License at
*
* http : //www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing , software
* distributed under the License is distributed on an "AS-IS" BASIS ,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND , either express or implied .
* See the License for the specific language governing permissions and
* limitations under the License .
* /
2011-12-04 16:50:02 +01:00
var ERR = require ( "async-stacktrace" ) ;
2011-12-10 16:46:47 +01:00
var customError = require ( "../utils/customError" ) ;
2011-08-03 20:31:25 +02:00
var padManager = require ( "./PadManager" ) ;
2011-08-04 18:18:59 +02:00
var padMessageHandler = require ( "../handler/PadMessageHandler" ) ;
2011-08-04 18:40:51 +02:00
var readOnlyManager = require ( "./ReadOnlyManager" ) ;
2011-08-08 17:21:31 +02:00
var groupManager = require ( "./GroupManager" ) ;
2011-08-09 13:09:04 +02:00
var authorManager = require ( "./AuthorManager" ) ;
2011-08-09 17:45:49 +02:00
var sessionManager = require ( "./SessionManager" ) ;
2011-08-03 20:31:25 +02:00
var async = require ( "async" ) ;
2011-10-31 11:18:44 +01:00
var exportHtml = require ( "../utils/ExportHtml" ) ;
2011-11-17 10:18:35 +01:00
var importHtml = require ( "../utils/ImportHtml" ) ;
var cleanText = require ( "./Pad" ) . cleanText ;
2013-01-23 00:06:52 +01:00
var PadDiff = require ( "../utils/padDiff" ) ;
2011-08-03 20:31:25 +02:00
/**********************/
/**GROUP FUNCTIONS*****/
/**********************/
2012-09-17 23:03:56 +02:00
exports . listAllGroups = groupManager . listAllGroups ;
2011-08-08 17:21:31 +02:00
exports . createGroup = groupManager . createGroup ;
2011-08-10 13:38:03 +02:00
exports . createGroupIfNotExistsFor = groupManager . createGroupIfNotExistsFor ;
2011-08-09 21:22:39 +02:00
exports . deleteGroup = groupManager . deleteGroup ;
2011-08-08 18:35:40 +02:00
exports . listPads = groupManager . listPads ;
exports . createGroupPad = groupManager . createGroupPad ;
2011-08-03 20:31:25 +02:00
2013-01-08 20:14:01 +01:00
/**********************/
/**PADLIST FUNCTION****/
/**********************/
2013-01-18 17:13:03 +01:00
exports . listAllPads = padManager . listAllPads ;
2013-01-08 20:14:01 +01:00
2011-08-03 20:31:25 +02:00
/**********************/
/**AUTHOR FUNCTIONS****/
/**********************/
2011-08-09 13:09:04 +02:00
exports . createAuthor = authorManager . createAuthor ;
2011-08-10 13:38:03 +02:00
exports . createAuthorIfNotExistsFor = authorManager . createAuthorIfNotExistsFor ;
2012-09-04 17:23:33 +02:00
exports . getAuthorName = authorManager . getAuthorName ;
2012-06-27 18:23:17 +02:00
exports . listPadsOfAuthor = authorManager . listPadsOfAuthor ;
2012-08-17 20:46:34 +02:00
exports . padUsers = padMessageHandler . padUsers ;
2012-06-29 20:26:12 +02:00
exports . padUsersCount = padMessageHandler . padUsersCount ;
2011-08-03 20:31:25 +02:00
/**********************/
/**SESSION FUNCTIONS***/
/**********************/
2011-08-09 17:45:49 +02:00
exports . createSession = sessionManager . createSession ;
2011-08-09 21:22:39 +02:00
exports . deleteSession = sessionManager . deleteSession ;
2011-08-09 17:45:49 +02:00
exports . getSessionInfo = sessionManager . getSessionInfo ;
2011-08-09 21:14:32 +02:00
exports . listSessionsOfGroup = sessionManager . listSessionsOfGroup ;
exports . listSessionsOfAuthor = sessionManager . listSessionsOfAuthor ;
2011-08-03 20:31:25 +02:00
/************************/
/**PAD CONTENT FUNCTIONS*/
/************************/
/ * *
getText ( padID , [ rev ] ) returns the text of a pad
Example returns :
{ code : 0 , message : "ok" , data : { text : "Welcome Text" } }
{ code : 1 , message : "padID does not exist" , data : null }
* /
exports . getText = function ( padID , rev , callback )
{
//check if rev is set
if ( typeof rev == "function" )
{
callback = rev ;
rev = undefined ;
}
//check if rev is a number
if ( rev !== undefined && typeof rev != "number" )
{
//try to parse the number
if ( ! isNaN ( parseInt ( rev ) ) )
{
rev = parseInt ( rev ) ;
}
else
{
2011-12-10 16:46:47 +01:00
callback ( new customError ( "rev is not a number" , "apierror" ) ) ;
2011-08-03 20:31:25 +02:00
return ;
}
}
//ensure this is not a negativ number
if ( rev !== undefined && rev < 0 )
{
2011-12-10 16:46:47 +01:00
callback ( new customError ( "rev is a negativ number" , "apierror" ) ) ;
2011-08-03 20:31:25 +02:00
return ;
}
//ensure this is not a float value
if ( rev !== undefined && ! is _int ( rev ) )
{
2011-12-10 16:46:47 +01:00
callback ( new customError ( "rev is a float value" , "apierror" ) ) ;
2011-08-03 20:31:25 +02:00
return ;
}
2011-08-04 18:18:59 +02:00
//get the pad
2011-08-04 20:20:14 +02:00
getPadSafe ( padID , true , function ( err , pad )
2011-08-04 18:18:59 +02:00
{
2011-12-04 16:50:02 +01:00
if ( ERR ( err , callback ) ) return ;
2011-08-04 18:18:59 +02:00
//the client asked for a special revision
if ( rev !== undefined )
2011-08-03 20:31:25 +02:00
{
2011-08-04 18:18:59 +02:00
//check if this is a valid revision
if ( rev > pad . getHeadRevisionNumber ( ) )
2011-08-03 20:31:25 +02:00
{
2011-12-10 16:46:47 +01:00
callback ( new customError ( "rev is higher than the head revision of the pad" , "apierror" ) ) ;
2011-08-04 18:18:59 +02:00
return ;
}
//get the text of this revision
pad . getInternalRevisionAText ( rev , function ( err , atext )
2011-08-03 20:31:25 +02:00
{
2011-12-04 16:50:02 +01:00
if ( ERR ( err , callback ) ) return ;
2011-08-03 20:31:25 +02:00
2011-12-04 16:50:02 +01:00
data = { text : atext . text } ;
callback ( null , data ) ;
2011-08-04 18:18:59 +02:00
} )
}
//the client wants the latest text, lets return it to him
else
{
callback ( null , { "text" : pad . text ( ) } ) ;
2011-08-03 20:31:25 +02:00
}
} ) ;
}
/ * *
setText ( padID , text ) sets the text of a pad
Example returns :
{ code : 0 , message : "ok" , data : null }
{ code : 1 , message : "padID does not exist" , data : null }
{ code : 1 , message : "text too long" , data : null }
* /
exports . setText = function ( padID , text , callback )
2011-08-08 18:35:40 +02:00
{
2012-07-10 06:53:55 +02:00
//text is required
if ( typeof text != "string" )
{
callback ( new customError ( "text is no string" , "apierror" ) ) ;
return ;
}
2011-08-04 18:18:59 +02:00
//get the pad
2011-08-04 20:20:14 +02:00
getPadSafe ( padID , true , function ( err , pad )
2011-08-04 18:18:59 +02:00
{
2011-12-04 16:50:02 +01:00
if ( ERR ( err , callback ) ) return ;
2011-08-04 18:18:59 +02:00
//set the text
pad . setText ( text ) ;
//update the clients on the pad
padMessageHandler . updatePadClients ( pad , callback ) ;
} ) ;
2011-08-03 20:31:25 +02:00
}
2011-10-31 11:18:44 +01:00
/ * *
getHTML ( padID , [ rev ] ) returns the html of a pad
Example returns :
{ code : 0 , message : "ok" , data : { text : "Welcome <strong>Text</strong>" } }
{ code : 1 , message : "padID does not exist" , data : null }
* /
exports . getHTML = function ( padID , rev , callback )
{
if ( typeof rev == "function" )
{
callback = rev ;
rev = undefined ;
}
if ( rev !== undefined && typeof rev != "number" )
{
if ( ! isNaN ( parseInt ( rev ) ) )
{
rev = parseInt ( rev ) ;
}
else
{
2011-12-10 16:46:47 +01:00
callback ( new customError ( "rev is not a number" , "apierror" ) ) ;
2011-10-31 11:18:44 +01:00
return ;
}
}
if ( rev !== undefined && rev < 0 )
{
2011-12-10 16:46:47 +01:00
callback ( new customError ( "rev is a negative number" , "apierror" ) ) ;
2011-10-31 11:18:44 +01:00
return ;
}
if ( rev !== undefined && ! is _int ( rev ) )
{
2011-12-10 16:46:47 +01:00
callback ( new customError ( "rev is a float value" , "apierror" ) ) ;
2011-10-31 11:18:44 +01:00
return ;
}
getPadSafe ( padID , true , function ( err , pad )
{
2011-12-04 16:50:02 +01:00
if ( ERR ( err , callback ) ) return ;
2011-10-31 11:18:44 +01:00
//the client asked for a special revision
if ( rev !== undefined )
{
//check if this is a valid revision
if ( rev > pad . getHeadRevisionNumber ( ) )
{
2011-12-10 16:46:47 +01:00
callback ( new customError ( "rev is higher than the head revision of the pad" , "apierror" ) ) ;
2011-10-31 11:18:44 +01:00
return ;
}
//get the html of this revision
exportHtml . getPadHTML ( pad , rev , function ( err , html )
{
2011-12-04 16:50:02 +01:00
if ( ERR ( err , callback ) ) return ;
data = { html : html } ;
callback ( null , data ) ;
2011-10-31 11:18:44 +01:00
} ) ;
}
//the client wants the latest text, lets return it to him
else
{
exportHtml . getPadHTML ( pad , undefined , function ( err , html )
{
2011-12-04 16:50:02 +01:00
if ( ERR ( err , callback ) ) return ;
data = { html : html } ;
callback ( null , data ) ;
2011-10-31 11:18:44 +01:00
} ) ;
}
} ) ;
}
2011-11-17 10:18:35 +01:00
exports . setHTML = function ( padID , html , callback )
{
//get the pad
getPadSafe ( padID , true , function ( err , pad )
{
2011-12-04 16:50:02 +01:00
if ( ERR ( err , callback ) ) return ;
2011-11-17 10:18:35 +01:00
// add a new changeset with the new html to the pad
importHtml . setPadHTML ( pad , cleanText ( html ) ) ;
//update the clients on the pad
padMessageHandler . updatePadClients ( pad , callback ) ;
} ) ;
}
2013-01-26 14:35:26 +01:00
/******************/
/**CHAT FUNCTIONS */
/******************/
/ * *
getChatHistory ( padId , start , end ) , returns a part of or the whole chat - history of this pad
Example returns :
{ "code" : 0 , "message" : "ok" , "data" : { "messages" : [ { "text" : "foo" , "userId" : "a.foo" , "time" : 1359199533759 , "userName" : "test" } ,
{ "text" : "bar" , "userId" : "a.foo" , "time" : 1359199534622 , "userName" : "test" } ] } }
{ code : 1 , message : "start is higher or equal to the current chatHead" , data : null }
{ code : 1 , message : "padID does not exist" , data : null }
* /
exports . getChatHistory = function ( padID , start , end , callback )
{
if ( start && end )
{
if ( start < 0 )
{
callback ( new customError ( "start is below zero" , "apierror" ) ) ;
return ;
}
if ( end < 0 )
{
callback ( new customError ( "end is below zero" , "apierror" ) ) ;
return ;
}
if ( start > end )
{
callback ( new customError ( "start is higher than end" , "apierror" ) ) ;
return ;
}
}
//get the pad
getPadSafe ( padID , true , function ( err , pad )
{
if ( ERR ( err , callback ) ) return ;
var chatHead = pad . chatHead ;
// fall back to getting the whole chat-history if a parameter is missing
if ( ! start || ! end )
{
start = 0 ;
end = pad . chatHead - 1 ;
}
if ( start >= chatHead )
{
callback ( new customError ( "start is higher or equal to the current chatHead" , "apierror" ) ) ;
return ;
}
if ( end >= chatHead )
{
callback ( new customError ( "end is higher or equal to the current chatHead" , "apierror" ) ) ;
return ;
}
// the the whole message-log and return it to the client
pad . getChatMessages ( start , end ,
function ( err , msgs )
{
if ( ERR ( err , callback ) ) return ;
callback ( null , { messages : msgs } ) ;
} ) ;
} ) ;
}
2011-08-03 20:31:25 +02:00
/*****************/
/**PAD FUNCTIONS */
/*****************/
/ * *
getRevisionsCount ( padID ) returns the number of revisions of this pad
Example returns :
{ code : 0 , message : "ok" , data : { revisions : 56 } }
{ code : 1 , message : "padID does not exist" , data : null }
* /
exports . getRevisionsCount = function ( padID , callback )
{
2011-08-04 18:24:36 +02:00
//get the pad
2011-08-04 20:20:14 +02:00
getPadSafe ( padID , true , function ( err , pad )
2011-08-04 18:18:59 +02:00
{
2011-12-04 16:50:02 +01:00
if ( ERR ( err , callback ) ) return ;
2011-08-04 18:24:36 +02:00
callback ( null , { revisions : pad . getHeadRevisionNumber ( ) } ) ;
} ) ;
2011-08-03 20:31:25 +02:00
}
2012-06-27 18:55:03 +02:00
/ * *
getLastEdited ( padID ) returns the timestamp of the last revision of the pad
Example returns :
{ code : 0 , message : "ok" , data : { lastEdited : 1340815946602 } }
{ code : 1 , message : "padID does not exist" , data : null }
* /
exports . getLastEdited = function ( padID , callback )
{
//get the pad
getPadSafe ( padID , true , function ( err , pad )
{
if ( ERR ( err , callback ) ) return ;
2012-07-07 19:22:27 +02:00
pad . getLastEdit ( function ( err , value ) {
if ( ERR ( err , callback ) ) return ;
callback ( null , { lastEdited : value } ) ;
} ) ;
2012-06-27 18:55:03 +02:00
} ) ;
}
2011-08-04 20:20:14 +02:00
/ * *
createPad ( padName [ , text ] ) creates a new pad in this group
Example returns :
{ code : 0 , message : "ok" , data : null }
{ code : 1 , message : "pad does already exist" , data : null }
* /
exports . createPad = function ( padID , text , callback )
2011-08-08 18:35:40 +02:00
{
2011-08-04 20:20:14 +02:00
//ensure there is no $ in the padID
2011-12-22 06:22:36 +01:00
if ( padID && padID . indexOf ( "$" ) != - 1 )
2011-08-04 20:20:14 +02:00
{
2011-12-10 16:46:47 +01:00
callback ( new customError ( "createPad can't create group pads" , "apierror" ) ) ;
2011-08-04 20:20:14 +02:00
return ;
}
//create pad
getPadSafe ( padID , false , text , function ( err )
{
2011-12-04 16:50:02 +01:00
if ( ERR ( err , callback ) ) return ;
callback ( ) ;
2011-08-04 20:20:14 +02:00
} ) ;
}
2011-08-03 20:31:25 +02:00
/ * *
deletePad ( padID ) deletes a pad
Example returns :
{ code : 0 , message : "ok" , data : null }
{ code : 1 , message : "padID does not exist" , data : null }
* /
exports . deletePad = function ( padID , callback )
{
2011-08-04 20:20:14 +02:00
getPadSafe ( padID , true , function ( err , pad )
2011-08-04 18:18:59 +02:00
{
2011-12-04 16:50:02 +01:00
if ( ERR ( err , callback ) ) return ;
2011-08-04 18:24:36 +02:00
2011-08-16 21:02:30 +02:00
pad . remove ( callback ) ;
2011-08-04 18:24:36 +02:00
} ) ;
2011-08-03 20:31:25 +02:00
}
/ * *
getReadOnlyLink ( padID ) returns the read only link of a pad
Example returns :
{ code : 0 , message : "ok" , data : null }
{ code : 1 , message : "padID does not exist" , data : null }
* /
2011-08-04 18:40:51 +02:00
exports . getReadOnlyID = function ( padID , callback )
2011-08-03 20:31:25 +02:00
{
2011-08-04 18:40:51 +02:00
//we don't need the pad object, but this function does all the security stuff for us
2011-08-04 20:20:14 +02:00
getPadSafe ( padID , true , function ( err )
2011-08-04 18:40:51 +02:00
{
2011-12-04 16:50:02 +01:00
if ( ERR ( err , callback ) ) return ;
2011-08-04 18:40:51 +02:00
//get the readonlyId
readOnlyManager . getReadOnlyId ( padID , function ( err , readOnlyId )
{
2011-12-04 16:50:02 +01:00
if ( ERR ( err , callback ) ) return ;
callback ( null , { readOnlyID : readOnlyId } ) ;
2011-08-04 18:40:51 +02:00
} ) ;
} ) ;
2011-08-03 20:31:25 +02:00
}
/ * *
setPublicStatus ( padID , publicStatus ) sets a boolean for the public status of a pad
Example returns :
{ code : 0 , message : "ok" , data : null }
{ code : 1 , message : "padID does not exist" , data : null }
* /
exports . setPublicStatus = function ( padID , publicStatus , callback )
{
2011-08-16 19:17:46 +02:00
//ensure this is a group pad
2011-12-22 06:22:36 +01:00
if ( padID && padID . indexOf ( "$" ) == - 1 )
2011-08-16 19:17:46 +02:00
{
2011-12-10 16:46:47 +01:00
callback ( new customError ( "You can only get/set the publicStatus of pads that belong to a group" , "apierror" ) ) ;
2011-08-16 19:17:46 +02:00
return ;
}
2011-08-04 18:24:36 +02:00
//get the pad
2011-08-04 20:20:14 +02:00
getPadSafe ( padID , true , function ( err , pad )
2011-08-04 18:18:59 +02:00
{
2011-12-04 16:50:02 +01:00
if ( ERR ( err , callback ) ) return ;
2011-08-04 18:24:36 +02:00
2011-08-10 23:33:31 +02:00
//convert string to boolean
if ( typeof publicStatus == "string" )
publicStatus = publicStatus == "true" ? true : false ;
2011-08-04 18:24:36 +02:00
2011-08-10 23:33:31 +02:00
//set the password
pad . setPublicStatus ( publicStatus ) ;
callback ( ) ;
2011-08-04 18:24:36 +02:00
} ) ;
2011-08-03 20:31:25 +02:00
}
/ * *
getPublicStatus ( padID ) return true of false
Example returns :
{ code : 0 , message : "ok" , data : { publicStatus : true } }
{ code : 1 , message : "padID does not exist" , data : null }
* /
exports . getPublicStatus = function ( padID , callback )
{
2011-08-16 19:17:46 +02:00
//ensure this is a group pad
2011-12-22 06:22:36 +01:00
if ( padID && padID . indexOf ( "$" ) == - 1 )
2011-08-16 19:17:46 +02:00
{
2011-12-10 16:46:47 +01:00
callback ( new customError ( "You can only get/set the publicStatus of pads that belong to a group" , "apierror" ) ) ;
2011-08-16 19:17:46 +02:00
return ;
}
2011-08-04 18:24:36 +02:00
//get the pad
2011-08-04 20:20:14 +02:00
getPadSafe ( padID , true , function ( err , pad )
2011-08-04 18:18:59 +02:00
{
2011-12-04 16:50:02 +01:00
if ( ERR ( err , callback ) ) return ;
2011-08-04 18:24:36 +02:00
2011-08-10 23:33:31 +02:00
callback ( null , { publicStatus : pad . getPublicStatus ( ) } ) ;
2011-08-04 18:24:36 +02:00
} ) ;
2011-08-03 20:31:25 +02:00
}
/ * *
setPassword ( padID , password ) returns ok or a error message
Example returns :
{ code : 0 , message : "ok" , data : null }
{ code : 1 , message : "padID does not exist" , data : null }
* /
exports . setPassword = function ( padID , password , callback )
{
2011-08-16 19:17:46 +02:00
//ensure this is a group pad
2011-12-22 06:22:36 +01:00
if ( padID && padID . indexOf ( "$" ) == - 1 )
2011-08-16 19:17:46 +02:00
{
2011-12-10 16:46:47 +01:00
callback ( new customError ( "You can only get/set the password of pads that belong to a group" , "apierror" ) ) ;
2011-08-16 19:17:46 +02:00
return ;
}
2011-08-04 18:24:36 +02:00
//get the pad
2011-08-04 20:20:14 +02:00
getPadSafe ( padID , true , function ( err , pad )
2011-08-04 18:18:59 +02:00
{
2011-12-04 16:50:02 +01:00
if ( ERR ( err , callback ) ) return ;
2011-08-04 18:24:36 +02:00
2011-08-10 23:33:31 +02:00
//set the password
2012-04-07 02:33:24 +02:00
pad . setPassword ( password == "" ? null : password ) ;
2011-08-04 18:24:36 +02:00
2011-08-10 23:33:31 +02:00
callback ( ) ;
2011-08-04 18:24:36 +02:00
} ) ;
2011-08-03 20:31:25 +02:00
}
/ * *
isPasswordProtected ( padID ) returns true or false
Example returns :
{ code : 0 , message : "ok" , data : { passwordProtection : true } }
{ code : 1 , message : "padID does not exist" , data : null }
* /
exports . isPasswordProtected = function ( padID , callback )
{
2011-08-16 19:17:46 +02:00
//ensure this is a group pad
2011-12-22 06:22:36 +01:00
if ( padID && padID . indexOf ( "$" ) == - 1 )
2011-08-16 19:17:46 +02:00
{
2011-12-10 16:46:47 +01:00
callback ( new customError ( "You can only get/set the password of pads that belong to a group" , "apierror" ) ) ;
2011-08-16 19:17:46 +02:00
return ;
}
2011-08-04 18:24:36 +02:00
//get the pad
2011-08-04 20:20:14 +02:00
getPadSafe ( padID , true , function ( err , pad )
2011-08-04 18:18:59 +02:00
{
2011-12-04 16:50:02 +01:00
if ( ERR ( err , callback ) ) return ;
2011-08-04 18:24:36 +02:00
2011-08-10 23:33:31 +02:00
callback ( null , { isPasswordProtected : pad . isPasswordProtected ( ) } ) ;
2011-08-04 18:24:36 +02:00
} ) ;
2011-08-03 20:31:25 +02:00
}
2012-06-27 18:23:17 +02:00
/ * *
listAuthorsOfPad ( padID ) returns an array of authors who contributed to this pad
Example returns :
{ code : 0 , message : "ok" , data : { authorIDs : [ "a.s8oes9dhwrvt0zif" , "a.akf8finncvomlqva" ] }
{ code : 1 , message : "padID does not exist" , data : null }
* /
exports . listAuthorsOfPad = function ( padID , callback )
{
//get the pad
getPadSafe ( padID , true , function ( err , pad )
{
if ( ERR ( err , callback ) ) return ;
callback ( null , { authorIDs : pad . getAllAuthors ( ) } ) ;
} ) ;
}
2012-08-08 19:12:11 +02:00
/ * *
sendClientsMessage ( padID , msg ) sends a message to all clients connected to the
pad , possibly for the purpose of signalling a plugin .
Note , this will only accept strings from the HTTP API , so sending bogus changes
or chat messages will probably not be possible .
The resulting message will be structured like so :
{
type : 'COLLABROOM' ,
data : {
type : < msg > ,
time : < time the message was sent >
}
}
Example returns :
{ code : 0 , message : "ok" }
{ code : 1 , message : "padID does not exist" }
* /
exports . sendClientsMessage = function ( padID , msg , callback ) {
getPadSafe ( padID , true , function ( err , pad ) {
if ( ERR ( err , callback ) ) {
return ;
} else {
padMessageHandler . handleCustomMessage ( padID , msg , callback ) ;
}
} ) ;
}
2012-12-06 21:03:15 +01:00
/ * *
2013-01-02 15:14:46 +01:00
checkToken ( ) returns ok when the current api token is valid
2012-12-06 21:03:15 +01:00
Example returns :
2012-12-06 21:26:51 +01:00
{ "code" : 0 , "message" : "ok" , "data" : null }
{ "code" : 4 , "message" : "no or wrong API Key" , "data" : null }
2012-12-06 21:03:15 +01:00
* /
2012-12-06 21:24:44 +01:00
exports . checkToken = function ( callback )
2012-12-06 21:03:15 +01:00
{
2012-12-06 21:24:44 +01:00
callback ( ) ;
2012-12-06 21:03:15 +01:00
}
2013-01-26 14:35:26 +01:00
/ * *
getChatHead ( padID ) returns the chatHead ( last number of the last chat - message ) of the pad
Example returns :
{ code : 0 , message : "ok" , data : { chatHead : 42 } }
{ code : 1 , message : "padID does not exist" , data : null }
* /
exports . getChatHead = function ( padID , callback )
{
//get the pad
getPadSafe ( padID , true , function ( err , pad )
{
if ( ERR ( err , callback ) ) return ;
callback ( null , { chatHead : pad . chatHead } ) ;
} ) ;
}
2012-06-27 18:23:17 +02:00
2013-01-22 23:33:51 +01:00
/ * *
2013-01-28 17:52:23 +01:00
createDiffHTML ( padID , startRev , endRev ) returns an object of diffs from 2 points in a pad
2013-01-22 23:33:51 +01:00
Example returns :
2013-01-27 18:51:40 +01:00
{ "code" : 0 , "message" : "ok" , "data" : { "html" : "<style>\n.authora_HKIv23mEbachFYfH {background-color: #a979d9}\n.authora_n4gEeMLsv1GivNeh {background-color: #a9b5d9}\n.removed {text-decoration: line-through; -ms-filter:'progid:DXImageTransform.Microsoft.Alpha(Opacity=80)'; filter: alpha(opacity=80); opacity: 0.8; }\n</style>Welcome to Etherpad Lite!<br><br>This pad text is synchronized as you type, so that everyone viewing this page sees the same text. This allows you to collaborate seamlessly on documents!<br><br>Get involved with Etherpad at <a href=\"http://etherpad.org\">http://etherpad.org</a><br><span class=\"authora_HKIv23mEbachFYfH\">aw</span><br><br>" , "authors" : [ "a.HKIv23mEbachFYfH" , "" ] } }
{ "code" : 4 , "message" : "no or wrong API Key" , "data" : null }
2013-01-22 23:33:51 +01:00
* /
2013-01-28 17:52:23 +01:00
exports . createDiffHTML = function ( padID , startRev , endRev , callback ) {
2013-01-22 23:33:51 +01:00
//check if rev is a number
if ( startRev !== undefined && typeof startRev != "number" )
{
//try to parse the number
if ( ! isNaN ( parseInt ( startRev ) ) )
{
startRev = parseInt ( startRev , 10 ) ;
}
else
{
callback ( { stop : "startRev is not a number" } ) ;
return ;
}
}
//check if rev is a number
if ( endRev !== undefined && typeof endRev != "number" )
{
//try to parse the number
if ( ! isNaN ( parseInt ( endRev ) ) )
{
endRev = parseInt ( endRev , 10 ) ;
}
else
{
callback ( { stop : "endRev is not a number" } ) ;
return ;
}
}
//get the pad
getPadSafe ( padID , true , function ( err , pad )
{
if ( err ) {
return callback ( err ) ;
}
try {
var padDiff = new PadDiff ( pad , startRev , endRev ) ;
} catch ( e ) {
return callback ( { stop : e . message } ) ;
}
var html , authors ;
async . series ( [
function ( callback ) {
padDiff . getHtml ( function ( err , _html ) {
if ( err ) {
return callback ( err ) ;
}
html = _html ;
callback ( ) ;
} ) ;
} ,
function ( callback ) {
padDiff . getAuthors ( function ( err , _authors ) {
if ( err ) {
return callback ( err ) ;
}
authors = _authors ;
callback ( ) ;
} ) ;
}
] , function ( err ) {
callback ( err , { html : html , authors : authors } )
} ) ;
} ) ;
}
2011-08-03 20:31:25 +02:00
/******************************/
/** INTERNAL HELPER FUNCTIONS */
/******************************/
//checks if a number is an int
function is _int ( value )
{
return ( parseFloat ( value ) == parseInt ( value ) ) && ! isNaN ( value )
}
2011-08-04 18:18:59 +02:00
//gets a pad safe
2011-08-04 20:20:14 +02:00
function getPadSafe ( padID , shouldExist , text , callback )
2011-08-04 18:18:59 +02:00
{
2011-08-04 20:20:14 +02:00
if ( typeof text == "function" )
{
callback = text ;
text = null ;
}
2011-08-04 18:18:59 +02:00
//check if padID is a string
if ( typeof padID != "string" )
{
2011-12-10 16:46:47 +01:00
callback ( new customError ( "padID is not a string" , "apierror" ) ) ;
2011-08-04 18:18:59 +02:00
return ;
}
//check if the padID maches the requirements
if ( ! padManager . isValidPadId ( padID ) )
{
2011-12-10 16:46:47 +01:00
callback ( new customError ( "padID did not match requirements" , "apierror" ) ) ;
2011-08-04 18:18:59 +02:00
return ;
}
//check if the pad exists
padManager . doesPadExists ( padID , function ( err , exists )
{
2011-12-04 16:50:02 +01:00
if ( ERR ( err , callback ) ) return ;
2011-08-04 20:20:14 +02:00
//does not exist, but should
2011-12-04 16:50:02 +01:00
if ( exists == false && shouldExist == true )
2011-08-04 18:18:59 +02:00
{
2011-12-10 16:46:47 +01:00
callback ( new customError ( "padID does not exist" , "apierror" ) ) ;
2011-08-04 18:18:59 +02:00
}
2011-08-04 20:20:14 +02:00
//does exists, but shouldn't
else if ( exists == true && shouldExist == false )
{
2011-12-10 16:46:47 +01:00
callback ( new customError ( "padID does already exist" , "apierror" ) ) ;
2011-08-04 20:20:14 +02:00
}
2011-08-04 18:18:59 +02:00
//pad exists, let's get it
else
{
2011-08-04 20:20:14 +02:00
padManager . getPad ( padID , text , callback ) ;
2011-08-04 18:18:59 +02:00
}
} ) ;
}