2013-02-22 15:41:12 -05:00
|
|
|
/**
|
|
|
|
A data model representing a user on Discourse
|
2013-02-20 13:15:50 -05:00
|
|
|
|
2013-02-22 15:41:12 -05:00
|
|
|
@class User
|
|
|
|
@extends Discourse.Model
|
|
|
|
@namespace Discourse
|
|
|
|
@module Discourse
|
|
|
|
**/
|
|
|
|
Discourse.User = Discourse.Model.extend({
|
|
|
|
|
2014-05-02 18:40:57 -04:00
|
|
|
hasPMs: Em.computed.gt("private_messages_stats.all", 0),
|
|
|
|
hasStartedPMs: Em.computed.gt("private_messages_stats.mine", 0),
|
|
|
|
hasUnreadPMs: Em.computed.gt("private_messages_stats.unread", 0),
|
|
|
|
|
2013-07-26 17:09:54 -04:00
|
|
|
/**
|
|
|
|
The user's stream
|
|
|
|
|
|
|
|
@property stream
|
|
|
|
@type {Discourse.UserStream}
|
|
|
|
**/
|
|
|
|
stream: function() {
|
|
|
|
return Discourse.UserStream.create({ user: this });
|
|
|
|
}.property(),
|
|
|
|
|
2013-07-11 19:35:52 -04:00
|
|
|
/**
|
|
|
|
Is this user a member of staff?
|
|
|
|
|
|
|
|
@property staff
|
|
|
|
@type {Boolean}
|
|
|
|
**/
|
|
|
|
staff: Em.computed.or('admin', 'moderator'),
|
|
|
|
|
2013-05-24 12:21:53 -04:00
|
|
|
searchContext: function() {
|
2013-08-27 17:01:35 -04:00
|
|
|
return {
|
|
|
|
type: 'user',
|
|
|
|
id: this.get('username_lower'),
|
|
|
|
user: this
|
|
|
|
};
|
2013-05-24 12:21:53 -04:00
|
|
|
}.property('username_lower'),
|
|
|
|
|
2013-12-08 09:01:25 -05:00
|
|
|
/**
|
|
|
|
This user's display name. Returns the name if possible, otherwise returns the
|
|
|
|
username.
|
|
|
|
|
|
|
|
@property displayName
|
|
|
|
@type {String}
|
|
|
|
**/
|
|
|
|
displayName: function() {
|
|
|
|
if (Discourse.SiteSettings.enable_names && !this.blank('name')) {
|
|
|
|
return this.get('name');
|
|
|
|
}
|
|
|
|
return this.get('username');
|
|
|
|
}.property('username', 'name'),
|
|
|
|
|
2013-03-13 16:43:16 -04:00
|
|
|
/**
|
|
|
|
This user's website.
|
|
|
|
|
|
|
|
@property websiteName
|
|
|
|
@type {String}
|
|
|
|
**/
|
2013-04-18 03:22:59 -04:00
|
|
|
websiteName: function() {
|
2013-07-11 19:35:52 -04:00
|
|
|
var website = this.get('website');
|
|
|
|
if (Em.isEmpty(website)) { return; }
|
2013-04-18 03:22:59 -04:00
|
|
|
|
2013-07-11 19:35:52 -04:00
|
|
|
return this.get('website').split("/")[2];
|
2013-04-18 03:22:59 -04:00
|
|
|
}.property('website'),
|
2014-03-05 16:11:01 -05:00
|
|
|
|
2014-02-28 15:12:51 -05:00
|
|
|
/**
|
|
|
|
This user's profile background(in CSS).
|
2013-02-22 15:41:12 -05:00
|
|
|
|
2014-02-28 15:12:51 -05:00
|
|
|
@property websiteName
|
|
|
|
@type {String}
|
|
|
|
**/
|
|
|
|
profileBackground: function() {
|
|
|
|
var background = this.get('profile_background');
|
|
|
|
if(Em.isEmpty(background) || !Discourse.SiteSettings.allow_profile_backgrounds) { return; }
|
2014-03-05 16:11:01 -05:00
|
|
|
|
2014-02-28 15:12:51 -05:00
|
|
|
return 'background-image: url(' + background + ')';
|
|
|
|
}.property('profile_background'),
|
2014-03-05 16:11:01 -05:00
|
|
|
|
2013-05-02 03:40:44 -04:00
|
|
|
statusIcon: function() {
|
2014-04-03 11:54:51 -04:00
|
|
|
var name = Handlebars.Utils.escapeExpression(this.get('name')),
|
|
|
|
desc;
|
|
|
|
|
2013-05-02 03:40:44 -04:00
|
|
|
if(this.get('admin')) {
|
2014-04-03 11:54:51 -04:00
|
|
|
desc = I18n.t('user.admin', {user: name});
|
2013-12-09 16:27:49 -05:00
|
|
|
return '<i class="fa fa-trophy" title="' + desc + '" alt="' + desc + '"></i>';
|
2013-05-02 03:40:44 -04:00
|
|
|
}
|
|
|
|
if(this.get('moderator')){
|
2014-04-03 11:54:51 -04:00
|
|
|
desc = I18n.t('user.moderator', {user: name});
|
2013-12-09 16:27:49 -05:00
|
|
|
return '<i class="fa fa-magic" title="' + desc + '" alt="' + desc + '"></i>';
|
2013-05-02 03:40:44 -04:00
|
|
|
}
|
|
|
|
return null;
|
|
|
|
}.property('admin','moderator'),
|
|
|
|
|
2013-03-13 16:43:16 -04:00
|
|
|
/**
|
|
|
|
Path to this user.
|
|
|
|
|
|
|
|
@property path
|
|
|
|
@type {String}
|
|
|
|
**/
|
2013-07-11 19:35:52 -04:00
|
|
|
path: Discourse.computed.url('username_lower', "/users/%@"),
|
2013-03-14 08:01:52 -04:00
|
|
|
|
|
|
|
/**
|
|
|
|
Path to this user's administration
|
|
|
|
|
|
|
|
@property adminPath
|
|
|
|
@type {String}
|
|
|
|
**/
|
2013-07-11 19:35:52 -04:00
|
|
|
adminPath: Discourse.computed.url('username_lower', "/admin/users/%@"),
|
2013-02-22 15:41:12 -05:00
|
|
|
|
2013-03-13 16:43:16 -04:00
|
|
|
/**
|
|
|
|
This user's username in lowercase.
|
|
|
|
|
|
|
|
@property username_lower
|
|
|
|
@type {String}
|
|
|
|
**/
|
2013-05-07 13:30:12 -04:00
|
|
|
username_lower: function() {
|
2013-02-22 15:41:12 -05:00
|
|
|
return this.get('username').toLowerCase();
|
2013-05-07 13:30:12 -04:00
|
|
|
}.property('username'),
|
2013-02-22 15:41:12 -05:00
|
|
|
|
2013-03-13 16:43:16 -04:00
|
|
|
/**
|
|
|
|
This user's trust level.
|
|
|
|
|
|
|
|
@property trustLevel
|
|
|
|
@type {Integer}
|
|
|
|
**/
|
2013-05-07 13:30:12 -04:00
|
|
|
trustLevel: function() {
|
2013-08-08 12:49:58 -04:00
|
|
|
return Discourse.Site.currentProp('trustLevels').findProperty('id', parseInt(this.get('trust_level'), 10));
|
2013-05-07 13:30:12 -04:00
|
|
|
}.property('trust_level'),
|
2013-02-22 15:41:12 -05:00
|
|
|
|
2014-03-17 14:50:28 -04:00
|
|
|
isElder: Em.computed.equal('trust_level', 4),
|
|
|
|
canManageTopic: Em.computed.or('staff', 'isElder'),
|
|
|
|
|
2013-11-07 16:34:18 -05:00
|
|
|
isSuspended: Em.computed.equal('suspended', true),
|
|
|
|
|
2013-12-03 15:53:30 -05:00
|
|
|
suspended: function() {
|
|
|
|
return this.get('suspended_till') && moment(this.get('suspended_till')).isAfter();
|
|
|
|
}.property('suspended_till'),
|
|
|
|
|
2013-11-07 16:34:18 -05:00
|
|
|
suspendedTillDate: function() {
|
|
|
|
return Discourse.Formatter.longDate(this.get('suspended_till'));
|
|
|
|
}.property('suspended_till'),
|
|
|
|
|
2013-03-14 14:45:29 -04:00
|
|
|
/**
|
2013-03-13 16:43:16 -04:00
|
|
|
Changes this user's username.
|
|
|
|
|
|
|
|
@method changeUsername
|
|
|
|
@param {String} newUsername The user's new username
|
|
|
|
@returns Result of ajax call
|
|
|
|
**/
|
2013-02-22 15:41:12 -05:00
|
|
|
changeUsername: function(newUsername) {
|
2013-08-27 17:01:35 -04:00
|
|
|
return Discourse.ajax("/users/" + this.get('username_lower') + "/preferences/username", {
|
2013-02-22 15:41:12 -05:00
|
|
|
type: 'PUT',
|
2013-05-07 13:30:12 -04:00
|
|
|
data: { new_username: newUsername }
|
2013-02-22 15:41:12 -05:00
|
|
|
});
|
|
|
|
},
|
|
|
|
|
2013-03-13 16:43:16 -04:00
|
|
|
/**
|
|
|
|
Changes this user's email address.
|
|
|
|
|
|
|
|
@method changeEmail
|
|
|
|
@param {String} email The user's new email address\
|
|
|
|
@returns Result of ajax call
|
|
|
|
**/
|
2013-02-22 15:41:12 -05:00
|
|
|
changeEmail: function(email) {
|
2013-08-27 17:01:35 -04:00
|
|
|
return Discourse.ajax("/users/" + this.get('username_lower') + "/preferences/email", {
|
2013-02-22 15:41:12 -05:00
|
|
|
type: 'PUT',
|
2013-05-07 13:30:12 -04:00
|
|
|
data: { email: email }
|
2013-02-22 15:41:12 -05:00
|
|
|
});
|
|
|
|
},
|
|
|
|
|
2013-03-13 16:43:16 -04:00
|
|
|
/**
|
|
|
|
Returns a copy of this user.
|
|
|
|
|
|
|
|
@method copy
|
|
|
|
@returns {User}
|
|
|
|
**/
|
|
|
|
copy: function() {
|
2013-02-22 15:41:12 -05:00
|
|
|
return Discourse.User.create(this.getProperties(Ember.keys(this)));
|
|
|
|
},
|
|
|
|
|
2013-03-13 16:43:16 -04:00
|
|
|
/**
|
|
|
|
Save's this user's properties over AJAX via a PUT request.
|
|
|
|
|
|
|
|
@method save
|
2013-04-08 15:04:40 -04:00
|
|
|
@returns {Promise} the result of the operation
|
2013-03-13 16:43:16 -04:00
|
|
|
**/
|
2013-04-08 15:04:40 -04:00
|
|
|
save: function() {
|
|
|
|
var user = this;
|
2014-01-02 01:58:49 -05:00
|
|
|
var data = this.getProperties('auto_track_topics_after_msecs',
|
2013-02-22 15:41:12 -05:00
|
|
|
'bio_raw',
|
|
|
|
'website',
|
|
|
|
'name',
|
2014-02-07 22:24:10 -05:00
|
|
|
'locale',
|
2013-02-22 15:41:12 -05:00
|
|
|
'email_digests',
|
|
|
|
'email_direct',
|
2013-10-01 03:04:02 -04:00
|
|
|
'email_always',
|
2013-02-22 15:41:12 -05:00
|
|
|
'email_private_messages',
|
2013-06-15 02:58:24 -04:00
|
|
|
'dynamic_favicon',
|
2013-02-22 15:41:12 -05:00
|
|
|
'digest_after_days',
|
2013-03-13 16:43:16 -04:00
|
|
|
'new_topic_duration_minutes',
|
2013-03-12 23:06:58 -04:00
|
|
|
'external_links_in_new_tab',
|
2014-02-06 19:06:35 -05:00
|
|
|
'mailing_list_mode',
|
2014-01-02 01:58:49 -05:00
|
|
|
'enable_quoting');
|
2014-01-08 01:10:16 -05:00
|
|
|
|
2014-01-05 19:57:17 -05:00
|
|
|
_.each(['muted','watched','tracked'], function(s){
|
2014-01-08 01:10:16 -05:00
|
|
|
var cats = user.get(s + 'Categories').map(function(c){ return c.get('id')});
|
|
|
|
// HACK: denote lack of categories
|
|
|
|
if(cats.length === 0) { cats = [-1]; }
|
|
|
|
data[s + '_category_ids'] = cats;
|
2014-01-05 19:57:17 -05:00
|
|
|
});
|
2014-01-02 01:58:49 -05:00
|
|
|
|
|
|
|
return Discourse.ajax("/users/" + this.get('username_lower'), {
|
|
|
|
data: data,
|
2013-05-07 10:58:41 -04:00
|
|
|
type: 'PUT'
|
|
|
|
}).then(function(data) {
|
|
|
|
user.set('bio_excerpt',data.user.bio_excerpt);
|
2013-06-15 03:05:55 -04:00
|
|
|
|
|
|
|
_.each([
|
|
|
|
'enable_quoting', 'external_links_in_new_tab', 'dynamic_favicon'
|
|
|
|
], function(preference) {
|
|
|
|
Discourse.User.current().set(preference, user.get(preference));
|
|
|
|
});
|
2013-02-22 15:41:12 -05:00
|
|
|
});
|
|
|
|
},
|
|
|
|
|
2013-03-13 16:43:16 -04:00
|
|
|
/**
|
|
|
|
Changes the password and calls the callback function on AJAX.complete.
|
|
|
|
|
|
|
|
@method changePassword
|
2013-04-08 15:04:40 -04:00
|
|
|
@returns {Promise} the result of the change password operation
|
2013-03-13 16:43:16 -04:00
|
|
|
**/
|
2013-04-08 15:04:40 -04:00
|
|
|
changePassword: function() {
|
2013-05-07 13:30:12 -04:00
|
|
|
return Discourse.ajax("/session/forgot_password", {
|
2013-02-22 15:41:12 -05:00
|
|
|
dataType: 'json',
|
2013-08-27 17:01:35 -04:00
|
|
|
data: { login: this.get('username') },
|
2013-04-08 15:04:40 -04:00
|
|
|
type: 'POST'
|
2013-02-22 15:41:12 -05:00
|
|
|
});
|
|
|
|
},
|
|
|
|
|
2013-03-13 16:43:16 -04:00
|
|
|
/**
|
|
|
|
Loads a single user action by id.
|
|
|
|
|
|
|
|
@method loadUserAction
|
|
|
|
@param {Integer} id The id of the user action being loaded
|
|
|
|
@returns A stream of the user's actions containing the action of id
|
|
|
|
**/
|
2013-02-22 15:41:12 -05:00
|
|
|
loadUserAction: function(id) {
|
2013-04-08 15:04:40 -04:00
|
|
|
var user = this;
|
|
|
|
var stream = this.get('stream');
|
2013-05-07 13:30:12 -04:00
|
|
|
return Discourse.ajax("/user_actions/" + id + ".json", { cache: 'false' }).then(function(result) {
|
2013-04-08 15:04:40 -04:00
|
|
|
if (result) {
|
|
|
|
if ((user.get('streamFilter') || result.action_type) !== result.action_type) return;
|
2013-05-22 11:20:16 -04:00
|
|
|
var action = Discourse.UserAction.collapseStream([Discourse.UserAction.create(result)]);
|
|
|
|
stream.set('itemsLoaded', user.get('itemsLoaded') + 1);
|
|
|
|
stream.insertAt(0, action[0]);
|
2013-02-20 13:15:50 -05:00
|
|
|
}
|
2013-02-22 15:41:12 -05:00
|
|
|
});
|
|
|
|
},
|
|
|
|
|
2013-03-13 16:43:16 -04:00
|
|
|
/**
|
|
|
|
The user's stat count, excluding PMs.
|
|
|
|
|
|
|
|
@property statsCountNonPM
|
|
|
|
@type {Integer}
|
|
|
|
**/
|
2013-05-20 16:52:37 -04:00
|
|
|
statsCountNonPM: function() {
|
|
|
|
if (this.blank('statsExcludingPms')) return 0;
|
2013-06-10 16:48:50 -04:00
|
|
|
var count = 0;
|
|
|
|
_.each(this.get('statsExcludingPms'), function(val) {
|
|
|
|
count += val.count;
|
2013-02-22 15:41:12 -05:00
|
|
|
});
|
2013-06-10 16:48:50 -04:00
|
|
|
return count;
|
2013-05-20 16:52:37 -04:00
|
|
|
}.property('statsExcludingPms.@each.count'),
|
2013-02-22 15:41:12 -05:00
|
|
|
|
2013-03-13 16:43:16 -04:00
|
|
|
/**
|
|
|
|
The user's stats, excluding PMs.
|
|
|
|
|
|
|
|
@property statsExcludingPms
|
|
|
|
@type {Array}
|
|
|
|
**/
|
2013-05-20 16:52:37 -04:00
|
|
|
statsExcludingPms: function() {
|
|
|
|
if (this.blank('stats')) return [];
|
|
|
|
return this.get('stats').rejectProperty('isPM');
|
|
|
|
}.property('stats.@each.isPM'),
|
2013-02-22 15:41:12 -05:00
|
|
|
|
2013-04-04 12:59:44 -04:00
|
|
|
|
2013-05-22 11:20:16 -04:00
|
|
|
findDetails: function() {
|
2013-04-04 12:59:44 -04:00
|
|
|
var user = this;
|
2013-07-26 15:56:29 -04:00
|
|
|
|
2013-05-22 11:20:16 -04:00
|
|
|
return PreloadStore.getAndRemove("user_" + user.get('username'), function() {
|
|
|
|
return Discourse.ajax("/users/" + user.get('username') + '.json');
|
2013-04-04 12:59:44 -04:00
|
|
|
}).then(function (json) {
|
2013-07-12 15:59:35 -04:00
|
|
|
|
|
|
|
if (!Em.isEmpty(json.user.stats)) {
|
|
|
|
json.user.stats = Discourse.User.groupStats(_.map(json.user.stats,function(s) {
|
|
|
|
if (s.count) s.count = parseInt(s.count, 10);
|
|
|
|
return Discourse.UserActionStat.create(s);
|
|
|
|
}));
|
|
|
|
}
|
2013-04-04 12:59:44 -04:00
|
|
|
|
2014-02-06 17:34:48 -05:00
|
|
|
if (!Em.isEmpty(json.user.custom_groups)) {
|
|
|
|
json.user.custom_groups = json.user.custom_groups.map(function (g) {
|
|
|
|
return Discourse.Group.create(g);
|
|
|
|
});
|
|
|
|
}
|
2014-03-28 04:49:30 -04:00
|
|
|
|
2013-05-27 11:05:41 -04:00
|
|
|
if (json.user.invited_by) {
|
|
|
|
json.user.invited_by = Discourse.User.create(json.user.invited_by);
|
|
|
|
}
|
|
|
|
|
2014-03-28 04:49:30 -04:00
|
|
|
if (!Em.isEmpty(json.user.featured_user_badge_ids)) {
|
|
|
|
var userBadgesMap = {};
|
|
|
|
Discourse.UserBadge.createFromJson(json).forEach(function(userBadge) {
|
|
|
|
userBadgesMap[ userBadge.get('id') ] = userBadge;
|
|
|
|
});
|
|
|
|
json.user.featured_user_badges = json.user.featured_user_badge_ids.map(function(id) {
|
|
|
|
return userBadgesMap[id];
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2013-04-04 12:59:44 -04:00
|
|
|
user.setProperties(json.user);
|
2013-05-20 16:52:37 -04:00
|
|
|
return user;
|
2013-04-04 12:59:44 -04:00
|
|
|
});
|
2013-08-25 11:26:44 -04:00
|
|
|
},
|
|
|
|
|
|
|
|
/*
|
|
|
|
Change avatar selection
|
|
|
|
|
|
|
|
@method toggleAvatarSelection
|
2013-08-27 17:01:35 -04:00
|
|
|
@param {Boolean} useUploadedAvatar true if the user is using the uploaded avatar
|
2013-08-25 11:26:44 -04:00
|
|
|
@returns {Promise} the result of the toggle avatar selection
|
|
|
|
*/
|
2013-08-27 17:01:35 -04:00
|
|
|
toggleAvatarSelection: function(useUploadedAvatar) {
|
|
|
|
return Discourse.ajax("/users/" + this.get("username_lower") + "/preferences/avatar/toggle", {
|
|
|
|
type: 'PUT',
|
|
|
|
data: { use_uploaded_avatar: useUploadedAvatar }
|
|
|
|
});
|
2013-09-07 04:49:11 -04:00
|
|
|
},
|
2014-03-05 16:11:01 -05:00
|
|
|
|
2014-02-28 15:12:51 -05:00
|
|
|
/*
|
|
|
|
Clear profile background
|
2014-03-05 16:11:01 -05:00
|
|
|
|
2014-02-28 15:12:51 -05:00
|
|
|
@method clearProfileBackground
|
|
|
|
@returns {Promise} the result of the clear profile background request
|
|
|
|
*/
|
|
|
|
clearProfileBackground: function() {
|
|
|
|
var user = this;
|
|
|
|
return Discourse.ajax("/users/" + this.get("username_lower") + "/preferences/profile_background/clear", {
|
|
|
|
type: 'PUT',
|
|
|
|
data: { }
|
|
|
|
}).then(function() {
|
|
|
|
user.set('profile_background', null);
|
|
|
|
});
|
|
|
|
},
|
2013-09-07 04:49:11 -04:00
|
|
|
|
|
|
|
/**
|
|
|
|
Determines whether the current user is allowed to upload a file.
|
|
|
|
|
|
|
|
@method isAllowedToUploadAFile
|
2013-11-06 12:56:26 -05:00
|
|
|
@param {String} type The type of the upload (image, attachment)
|
2013-09-07 04:49:11 -04:00
|
|
|
@returns true if the current user is allowed to upload a file
|
|
|
|
**/
|
|
|
|
isAllowedToUploadAFile: function(type) {
|
|
|
|
return this.get('staff') ||
|
|
|
|
this.get('trust_level') > 0 ||
|
|
|
|
Discourse.SiteSettings['newuser_max_' + type + 's'] > 0;
|
2013-11-06 12:56:26 -05:00
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
|
|
|
Invite a user to the site
|
|
|
|
|
|
|
|
@method createInvite
|
|
|
|
@param {String} email The email address of the user to invite to the site
|
|
|
|
@returns {Promise} the result of the server call
|
|
|
|
**/
|
2014-05-09 04:22:15 -04:00
|
|
|
createInvite: function(email, groupNames) {
|
2013-11-06 12:56:26 -05:00
|
|
|
return Discourse.ajax('/invites', {
|
|
|
|
type: 'POST',
|
2014-05-09 04:22:15 -04:00
|
|
|
data: {email: email, group_names: groupNames}
|
2013-11-06 12:56:26 -05:00
|
|
|
});
|
2013-12-30 12:46:18 -05:00
|
|
|
},
|
|
|
|
|
2014-01-02 01:58:49 -05:00
|
|
|
updateMutedCategories: function() {
|
2014-01-08 01:10:16 -05:00
|
|
|
this.set("mutedCategories", Discourse.Category.findByIds(this.muted_category_ids));
|
2014-01-02 01:58:49 -05:00
|
|
|
}.observes("muted_category_ids"),
|
|
|
|
|
2014-01-05 19:57:17 -05:00
|
|
|
updateTrackedCategories: function() {
|
2014-01-08 01:10:16 -05:00
|
|
|
this.set("trackedCategories", Discourse.Category.findByIds(this.tracked_category_ids));
|
2014-01-05 19:57:17 -05:00
|
|
|
}.observes("tracked_category_ids"),
|
|
|
|
|
2014-01-02 01:58:49 -05:00
|
|
|
updateWatchedCategories: function() {
|
2014-01-08 01:10:16 -05:00
|
|
|
this.set("watchedCategories", Discourse.Category.findByIds(this.watched_category_ids));
|
2014-02-13 11:42:35 -05:00
|
|
|
}.observes("watched_category_ids"),
|
|
|
|
|
|
|
|
canDeleteAccount: function() {
|
|
|
|
return this.get('can_delete_account') && ((this.get('reply_count')||0) + (this.get('topic_count')||0)) <= 1;
|
|
|
|
}.property('can_delete_account', 'reply_count', 'topic_count'),
|
|
|
|
|
|
|
|
delete: function() {
|
|
|
|
if (this.get('can_delete_account')) {
|
|
|
|
return Discourse.ajax("/users/" + this.get('username'), {
|
|
|
|
type: 'DELETE',
|
|
|
|
data: {context: window.location.pathname}
|
|
|
|
});
|
|
|
|
} else {
|
|
|
|
return Ember.RSVP.reject(I18n.t('user.delete_yourself_not_allowed'));
|
|
|
|
}
|
|
|
|
}
|
2014-01-08 01:10:16 -05:00
|
|
|
|
2013-02-22 15:41:12 -05:00
|
|
|
});
|
2013-02-20 13:15:50 -05:00
|
|
|
|
2013-08-08 12:42:08 -04:00
|
|
|
Discourse.User.reopenClass(Discourse.Singleton, {
|
2013-10-03 12:51:30 -04:00
|
|
|
/**
|
|
|
|
Find a `Discourse.User` for a given username.
|
|
|
|
|
|
|
|
@method findByUsername
|
|
|
|
@returns {Promise} a promise that resolves to a `Discourse.User`
|
|
|
|
**/
|
|
|
|
findByUsername: function(username) {
|
|
|
|
var user = Discourse.User.create({username: username});
|
|
|
|
return user.findDetails();
|
|
|
|
},
|
|
|
|
|
2013-05-28 11:08:32 -04:00
|
|
|
/**
|
2013-08-08 12:42:08 -04:00
|
|
|
The current singleton will retrieve its attributes from the `PreloadStore`
|
|
|
|
if it exists. Otherwise, no instance is created.
|
2013-05-28 11:08:32 -04:00
|
|
|
|
2013-08-08 12:42:08 -04:00
|
|
|
@method createCurrent
|
|
|
|
@returns {Discourse.User} the user, if logged in.
|
2013-05-28 11:08:32 -04:00
|
|
|
**/
|
2013-08-08 12:42:08 -04:00
|
|
|
createCurrent: function() {
|
|
|
|
var userJson = PreloadStore.get('currentUser');
|
|
|
|
if (userJson) { return Discourse.User.create(userJson); }
|
|
|
|
return null;
|
2013-05-28 11:08:32 -04:00
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
|
|
|
Logs out the currently logged in user
|
|
|
|
|
|
|
|
@method logout
|
|
|
|
@returns {Promise} resolved when the logout finishes
|
|
|
|
**/
|
|
|
|
logout: function() {
|
|
|
|
var discourseUserClass = this;
|
2013-08-08 12:42:08 -04:00
|
|
|
return Discourse.ajax("/session/" + Discourse.User.currentProp('username'), {
|
2013-05-28 11:08:32 -04:00
|
|
|
type: 'DELETE'
|
|
|
|
}).then(function () {
|
|
|
|
discourseUserClass.currentUser = null;
|
|
|
|
});
|
|
|
|
},
|
|
|
|
|
2013-03-13 16:43:16 -04:00
|
|
|
/**
|
|
|
|
Checks if given username is valid for this email address
|
2013-02-22 15:41:12 -05:00
|
|
|
|
2013-03-13 16:43:16 -04:00
|
|
|
@method checkUsername
|
|
|
|
@param {String} username A username to check
|
|
|
|
@param {String} email An email address to check
|
2014-03-18 21:19:20 -04:00
|
|
|
@param {Number} forUserId user id - provide when changing username
|
2013-03-13 16:43:16 -04:00
|
|
|
**/
|
2013-07-30 14:13:56 -04:00
|
|
|
checkUsername: function(username, email, forUserId) {
|
2013-05-07 13:30:12 -04:00
|
|
|
return Discourse.ajax('/users/check_username', {
|
2013-07-30 14:13:56 -04:00
|
|
|
data: { username: username, email: email, for_user_id: forUserId }
|
2013-02-22 15:41:12 -05:00
|
|
|
});
|
|
|
|
},
|
|
|
|
|
2013-03-13 16:43:16 -04:00
|
|
|
/**
|
|
|
|
Groups the user's statistics
|
|
|
|
|
|
|
|
@method groupStats
|
2014-03-18 21:19:20 -04:00
|
|
|
@param {Array} stats Given stats
|
2013-03-13 16:43:16 -04:00
|
|
|
@returns {Object}
|
|
|
|
**/
|
2013-02-22 15:41:12 -05:00
|
|
|
groupStats: function(stats) {
|
2013-05-20 16:52:37 -04:00
|
|
|
var responses = Discourse.UserActionStat.create({
|
|
|
|
count: 0,
|
2013-07-16 16:16:37 -04:00
|
|
|
action_type: Discourse.UserAction.TYPES.replies
|
2013-02-22 15:41:12 -05:00
|
|
|
});
|
2013-05-20 16:52:37 -04:00
|
|
|
|
|
|
|
stats.filterProperty('isResponse').forEach(function (stat) {
|
|
|
|
responses.set('count', responses.get('count') + stat.get('count'));
|
2013-02-22 15:41:12 -05:00
|
|
|
});
|
2013-05-20 16:52:37 -04:00
|
|
|
|
|
|
|
var result = Em.A();
|
|
|
|
result.pushObjects(stats.rejectProperty('isResponse'));
|
2013-07-07 22:12:06 -04:00
|
|
|
|
2013-08-30 00:43:54 -04:00
|
|
|
var insertAt = 0;
|
2013-07-07 22:12:06 -04:00
|
|
|
result.forEach(function(item, index){
|
2013-07-16 16:16:37 -04:00
|
|
|
if(item.action_type === Discourse.UserAction.TYPES.topics || item.action_type === Discourse.UserAction.TYPES.posts){
|
2013-07-07 22:12:06 -04:00
|
|
|
insertAt = index + 1;
|
|
|
|
}
|
|
|
|
});
|
2013-07-19 03:21:51 -04:00
|
|
|
if(responses.count > 0) {
|
|
|
|
result.insertAt(insertAt, responses);
|
|
|
|
}
|
2013-05-20 16:52:37 -04:00
|
|
|
return(result);
|
2013-02-22 15:41:12 -05:00
|
|
|
},
|
2013-02-20 13:15:50 -05:00
|
|
|
|
2013-03-13 16:43:16 -04:00
|
|
|
/**
|
|
|
|
Creates a new account over POST
|
|
|
|
|
|
|
|
@method createAccount
|
|
|
|
@param {String} name This user's name
|
|
|
|
@param {String} email This user's email
|
|
|
|
@param {String} password This user's password
|
2014-03-18 21:19:20 -04:00
|
|
|
@param {String} username This user's username
|
2013-03-13 16:43:16 -04:00
|
|
|
@param {String} passwordConfirm This user's confirmed password
|
|
|
|
@param {String} challenge
|
|
|
|
@returns Result of ajax call
|
|
|
|
**/
|
2013-02-22 15:41:12 -05:00
|
|
|
createAccount: function(name, email, password, username, passwordConfirm, challenge) {
|
2013-05-07 13:30:12 -04:00
|
|
|
return Discourse.ajax("/users", {
|
2013-02-22 15:41:12 -05:00
|
|
|
data: {
|
|
|
|
name: name,
|
|
|
|
email: email,
|
|
|
|
password: password,
|
|
|
|
username: username,
|
|
|
|
password_confirmation: passwordConfirm,
|
|
|
|
challenge: challenge
|
|
|
|
},
|
|
|
|
type: 'POST'
|
|
|
|
});
|
|
|
|
}
|
|
|
|
});
|