· 7 years ago · Oct 14, 2018, 08:06 PM
1var Ember = require('/lib/em_ti/ember-runtime');
2 Keychain = require('com.obscure.keychain');
3
4/**
5 @class
6
7 An Appcelerator Cloud Services User.
8
9 @extends Ember.Object
10*/
11var User = Ember.Object.extend({
12 // ..........................................................
13 // Properties
14 //
15
16 /**
17 Unique identifier for the user from ACS.
18
19 @property {String}
20 */
21 id: null,
22
23 /**
24 Email used to log in to ACS.
25
26 @property {String}
27 */
28 email: null,
29
30 /**
31 Access token used to sign in to ACS using Facebook.
32
33 @property {String}
34 */
35 facebookAccessToken: null,
36
37 /**
38 Unique ID for user's Facebook account.
39
40 @property {Number}
41 */
42 facebookId: null,
43
44 /**
45 Full name of the ACS user.
46
47 @property {String}
48 */
49 name: null,
50
51 /**
52 Password used to log in to ACS.
53
54 @property {String}
55 */
56 password: null,
57
58 /**
59 OAuth access token key for authorized Twitter user.
60
61 @property {String}
62 */
63 twitterAccessTokenKey: null,
64
65 /**
66 OAuth access token secret for authorized Twitter user.
67
68 @property {String}
69 */
70 twitterAccessTokenSecret: null,
71
72 /**
73 Unique ID for user's Twitter account.
74
75 @property {Number}
76 */
77 twitterId: null,
78
79 /**
80 The name of the user as obtained from Facebook or Twitter or the username if name is not defined.
81
82 @property {String}
83 */
84 displayName: function() {
85 if (!Ember.none(this.get('name'))) {
86 return this.get('name');
87 } else {
88 return this.get('username');
89 }
90 }.property('name', 'username'),
91
92 // ..........................................................
93 // Methods
94 //
95
96 /**
97 Save the User to the local db.
98
99 @returns {User} The saved version of the object.
100 */
101 save: function() {
102 var db = Titanium.Database.open('mydb');
103
104 Ti.API.info(this.toString());
105
106 db.execute('CREATE TABLE IF NOT EXISTS users (id TEXT, json TEXT)');
107
108 if (Ember.none(User.find(this.get('id')))) {
109 db.execute('INSERT INTO users (id, json) VALUES (?, ?)', this.get('id'), this.toString());
110 } else {
111 db.execute('UPDATE users SET json = ? WHERE id = ?', this.toString(), this.get('id'));
112 }
113 db.close();
114
115 if (!Ember.none(this.get('password'))) {
116 var keychainItem = Keychain.createItem({ identifier: 'login' });
117 keychainItem.account = this.get('username');
118 keychainItem.valueData = this.get('password');
119 }
120
121 return this;
122 },
123
124 /**
125 Returns the User object as a JSON object.
126
127 @returns {Hash}
128 */
129 toJSON: function() {
130 return this.getProperties('id', 'email', 'facebookAccessToken', 'facebookId', 'name', 'twitterAccessTokenKey', 'twitterAccessTokenSecret', 'twitterId');
131 },
132
133 /**
134 Returns the User object as a stringified JSON object.
135
136 @returns {String}
137 */
138 toString: function() {
139 return JSON.stringify(this.toJSON());
140 }
141});
142
143User.reopenClass({
144 /**
145 Override the default create method to handle creating from stringified JSON and arrays.
146
147 @param {Object} options
148 A JSON object, an Array of JSON objects, or a stringified JSON object to create the User object(s).
149
150 @returns {Object} The created User object or an Array of User objects.
151 */
152 create: function(options) {
153 if (Ember.none(options)) return this._super();
154
155 if (typeof options === 'string') {
156 try {
157 var parsedOptions = JSON.parse(options);
158 return this.create(parsedOptions);
159 } catch(e) {
160 Ti.API.warn('User#create could not parse the options string.');
161 }
162 } else if (options.constructor.toString().indexOf('Array') > -1) {
163 var objArray = [];
164 for (var i = 0; i < options.length; i++) {
165 objArray.push(this.create(options[i]));
166 }
167 return objArray;
168 } else {
169 return this._super(options);
170 }
171 },
172
173 /**
174 Search the local db for a User for a specific id.
175
176 @param {String} id
177 The unique id for the User you're looking up as a string.
178
179 @returns {Object} The User object or null if no User is found.
180 */
181 find: function(id) {
182 var db = Titanium.Database.open('mydb'), user = null;
183
184 db.execute('CREATE TABLE IF NOT EXISTS users (id TEXT, json TEXT)');
185
186 var row = db.execute('SELECT * FROM users WHERE id = ?', id);
187 if (row.isValidRow()) {
188 user = this.create(row.fieldByName('json'));
189 }
190
191 row.close();
192 db.close();
193
194 return user;
195 },
196
197 /**
198 Convert one or many User objects to JSON objects.
199
200 @param {Object} user
201 A User object or an array of User objects.
202
203 @returns {Object} The JSON object or an Array of JSON objects.
204 */
205 toJSON: function(user) {
206 if (!Ember.none(user) && user.constructor.toString().indexOf('Array') > -1) {
207 var jsonArray = [];
208 for (var i = 0; i < user.length; i++) {
209 jsonArray.push(user[i].toJSON());
210 }
211 return jsonArray;
212 } else {
213 return user.toJSON();
214 }
215 }
216});
217
218module.exports = User;