Skip to content

Instantly share code, notes, and snippets.

@grantges
Last active October 10, 2015 08:07
Show Gist options
  • Save grantges/3659842 to your computer and use it in GitHub Desktop.
Save grantges/3659842 to your computer and use it in GitHub Desktop.
Appcelerator ACS Wrapper Module
/**
* Appcelerator Cloud Services (ACS) Helper Module
*/
var Cloud = require('ti.cloud');
var $P = Ti.App.Properties;
/**
* Users Helpers
*/
/**
* isLoggedIn
* Retuns whether the user already is installed
*/
exports.isLoggedIn = function() {
return Cloud.hasStoredSession();
};
exports.startAppWithLoginCheck = function(_args) {
_args = _args || {};
try {
(Cloud.hasStoredSession()) ? _args.appstart() : _args.login();
}
catch(e){
Ti.API.error(e.error + "::" + e.message)
}
};
/**
* login
* Creates reference to the Appcelerator Cloud Services Module and attempts to login a user. Prior to logging the user in it will check for an existing session.
* @param {Object} data - object that contains necessary login information (email -OR- username required and password)
* @param {Function} callback - callback function to be called after the login attempt has completed
*/
exports.login = function(_data, _callback){
_data = _data || {};
//Check for existing session first to prevent a unneeded call to the cloud.
if(Cloud.hasStoredSession()) {
if(_callback && (typeof(_callback) === 'function') ){
_callback({success: 1, message: 'USER SESSION ALREADY EXISTS'});
}
return;
}
if( ( _data.email || _data.username ) && _data.password ) {
Cloud.Users.login({
login: _data.email || _data.username,
password: _data.password
}, function (e) {
if (e.success) {
if(_callback && (typeof(_callback) === 'function') ){
_callback(e);
}
} else {
Ti.API.error('ACS Error: ' +((e.error && e.message) || JSON.stringify(e)));
if(_callback){
_callback({error: e.error, message: e.message});
}
}
});
}
else {
if(_callback && (typeof(_callback) === 'function') ){
_callback({success: 0, err: 'BAD_INFO', message: 'Oops! Missing user information to successfully login the user' });
}
}
return;
};
/**
* logout
* @param {Function} _callback - function to be called once the logout action has completed
*/
exports.logout = function(_callback){
var Cloud = require('ti.cloud');
Cloud.Users.logout(function (e) {
if (e.success) {
Ti.API.info('ACS::logout successful');
} else {
Ti.API.error('ACS Error: ' +((e.error && e.message) || JSON.stringify(e)));
}
if(_callback && (typeof(_callback) === 'function') ){
_callback(e);
}
});
}
/**
* createUser
* Creates a new User in the Appcelerator Cloud
* @param {Object} _data - user related information; must contain either email or username, password and password_confirmation properties at minimum.
* @param {Function} _callback - callback function to be called once the user creation has completed. This is called for success or failure
*/
exports.createUser = function(_data, _callback){
if( ( _data.email || _data.username ) && _data.password && _data.password_confirmation) {
var Cloud = require('ti.cloud');
Cloud.Users.create({
username: _data.username,
email: _data.email,
first_name: _data.first_name,
last_name: _data.last_name,
password: _data.password,
password_confirmation: _data.password_confirmation,
role: _data.role || 'USER',
photo: _data.photo
}, function (e) {
if (e.success) {
var user = e.users[0];
Ti.API.debug('Success:\\n id: ' + user.id + '\\n first name: ' + user.first_name + '\\n last name: ' + user.last_name);
$P.setBool('loggedin', true);
} else {
Ti.API.error('ACS Error: ' +((e.error && e.message) || JSON.stringify(e)));
$P.setBool('loggedin', false);
}
if(_callback && (typeof(_callback) === 'function') ){
_callback(e);
}
});
}
};
/**
*
*/
exports.showMe = function(_callback){
Cloud.Users.showMe(function(e){
if(e.success){
Ti.API.info('ACS::showMe successful');
}
else{
Ti.API.error('ACS Error: ' +((e.error && e.message) || JSON.stringify(e)));
}
if(_callback && (typeof(_callback) === 'function') ){
_callback(e);
}
});
}
/**
* Status Helpers
*/
exports.getStatus = function(sid, callback) {
if(sid) {
var Cloud = require('ti.cloud');
Ti.API.info('getStatus: '+ sid);
Cloud.Statuses.search({status_id: sid}, function(e){
if(e.success) {
Ti.API.info('Success! - length'+e.statuses.length);
if(callback)
{
for(var i=0; i<e.statuses.length; i++){
if(e.statuses[i].id === sid){
callback(e.statuses[i]);
}
}
}
};
});
}
else {
Ti.API.error('StatusWindow.getStatusInfo - id parameter is required');
alert('Unable to show this Users status, id is missing or invalid');
}
};
exports.postStatus = function(_data, _callback) { //data = {message, photo, tags}
var Cloud = require('ti.cloud');
Cloud.Statuses.create(_data, function(e){
if(e.success) {
}
else {
Ti.API.error('StatusWindow.postStatus - id parameter is required');
alert('Unable to show this Users status, id is missing or invalid');
}
if(_callback && (typeof(_callback) === 'function') ){
_callback(e);
}
});
}
/*
* ACS Photo Helper Functions
*
* Example:
* var ACS = require('acs');
* ACS.capturePhotoAndUpload('CAMERA',{
success: function(photo){ alert(photo.id)}),
error: function(e){ alert(e.message)}
});
*/
exports.capturePhotoAndUpload = function( _source, _o) {
var onSuccess = function(e){
if(e.media){
Cloud.Photos.create({
photo: e.media
}, function (e) {
if (e.success) {
var photo = e.photos[0];
_o.success && _o.success(photo);
/*
alert('Success:\\n' +
'id: ' + photo.id + '\\n' +
'filename: ' + photo.filename + '\\n' +
'size: ' + photo.size,
'updated_at: ' + photo.updated_at);
*/
} else {
_o.error && _o.error(e)
}
});
}
}
switch(_source){
case "CAMERA":
Ti.Media.showCamera({
animated: true,
allowEditing: true,
autohide: true,
mediaTypes: [Ti.Media.MEDIA_TYPE_PHOTO],
success: onSuccess,
error: function(e){ alert('Error:\\n' +
((e.error && e.message) || JSON.stringify(e)));}
});
break;
case "GALLERY":
Ti.Media.openPhotoGallery({
animated: true,
allowEditing: true,
autohide: true,
mediaTypes: [Ti.Media.MEDIA_TYPE_PHOTO],
success: onSuccess,
error: function(e){ alert('Error:\\n' +
((e.error && e.message) || JSON.stringify(e)));}
});
break;
default:
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment