discourse/app/assets/javascripts/discourse/models/user.js
Sam 38f185355d added options to disable quoting and open links in new tabs
fixed a some regressions
removed some dead code
fixed messages about constants being re-defined
2013-03-12 20:06:58 -07:00

329 lines
8.5 KiB
JavaScript

/**
A data model representing a user on Discourse
@class User
@extends Discourse.Model
@namespace Discourse
@module Discourse
**/
Discourse.User = Discourse.Model.extend({
avatarLarge: (function() {
return Discourse.Utilities.avatarUrl(this.get('username'), 'large', this.get('avatar_template'));
}).property('username'),
avatarSmall: (function() {
return Discourse.Utilities.avatarUrl(this.get('username'), 'small', this.get('avatar_template'));
}).property('username'),
websiteName: (function() {
return this.get('website').split("/")[2];
}).property('website'),
path: (function() {
return "/users/" + (this.get('username_lower'));
}).property('username'),
username_lower: (function() {
return this.get('username').toLowerCase();
}).property('username'),
trustLevel: (function() {
return Discourse.get('site.trust_levels').findProperty('id', this.get('trust_level'));
}).property('trust_level'),
changeUsername: function(newUsername) {
return $.ajax({
url: "/users/" + (this.get('username_lower')) + "/preferences/username",
type: 'PUT',
data: {
new_username: newUsername
}
});
},
changeEmail: function(email) {
return $.ajax({
url: "/users/" + (this.get('username_lower')) + "/preferences/email",
type: 'PUT',
data: {
email: email
}
});
},
copy: function(deep) {
return Discourse.User.create(this.getProperties(Ember.keys(this)));
},
save: function(finished) {
var _this = this;
return $.ajax("/users/" + this.get('username').toLowerCase(), {
data: this.getProperties('auto_track_topics_after_msecs',
'bio_raw',
'website',
'name',
'email_digests',
'email_direct',
'email_private_messages',
'digest_after_days',
'new_topic_duration_minutes',
'external_links_in_new_tab',
'enable_quoting'),
type: 'PUT',
success: function() {
Discourse.set('currentUser.enable_quoting', _this.get('enable_quoting'));
Discourse.set('currentUser.external_links_in_new_tab', _this.get('external_links_in_new_tab'));
return finished(true);
},
error: function() { return finished(false); }
});
},
changePassword: function(callback) {
var good;
good = false;
return $.ajax({
url: '/session/forgot_password',
dataType: 'json',
data: {
username: this.get('username')
},
type: 'POST',
success: function() { good = true; },
complete: function() {
var message;
message = "error";
if (good) {
message = "email sent";
}
return callback(message);
}
});
},
filterStream: function(filter) {
if (Discourse.UserAction.statGroups[filter]) {
filter = Discourse.UserAction.statGroups[filter].join(",");
}
this.set('streamFilter', filter);
this.set('stream', Em.A());
return this.loadMoreUserActions();
},
loadUserAction: function(id) {
var stream,
_this = this;
stream = this.get('stream');
return $.ajax({
url: "/user_actions/" + id + ".json",
dataType: 'json',
cache: 'false',
success: function(result) {
if (result) {
var action;
if ((_this.get('streamFilter') || result.action_type) !== result.action_type) {
return;
}
action = Em.A();
action.pushObject(Discourse.UserAction.create(result));
action = Discourse.UserAction.collapseStream(action);
return stream.insertAt(0, action[0]);
}
}
});
},
loadMoreUserActions: function(callback) {
var stream, url,
_this = this;
stream = this.get('stream');
if (!stream) return;
url = "/user_actions?offset=" + stream.length + "&user_id=" + (this.get("id"));
if (this.get('streamFilter')) {
url += "&filter=" + (this.get('streamFilter'));
}
return $.ajax({
url: url,
dataType: 'json',
cache: 'false',
success: function(result) {
var copy;
if (result && result.user_actions && result.user_actions.each) {
copy = Em.A();
result.user_actions.each(function(i) {
return copy.pushObject(Discourse.UserAction.create(i));
});
copy = Discourse.UserAction.collapseStream(copy);
stream.pushObjects(copy);
_this.set('stream', stream);
}
if (callback) {
return callback();
}
}
});
},
statsCountNonPM: (function() {
var stats, total;
total = 0;
if (!(stats = this.get('stats'))) return 0;
this.get('stats').each(function(s) {
if (!s.get("isPM")) {
total += parseInt(s.count, 10);
}
});
return total;
}).property('stats.@each'),
statsExcludingPms: (function() {
var r;
r = [];
if (this.blank('stats')) return r;
this.get('stats').each(function(s) {
if (!s.get('isPM')) {
return r.push(s);
}
});
return r;
}).property('stats.@each'),
statsPmsOnly: (function() {
var r;
r = [];
if (this.blank('stats')) return r;
this.get('stats').each(function(s) {
if (s.get('isPM')) return r.push(s);
});
return r;
}).property('stats.@each'),
inboxCount: (function() {
var r;
r = 0;
this.get('stats').each(function(s) {
if (s.action_type === Discourse.UserAction.GOT_PRIVATE_MESSAGE) {
r = s.count;
return false;
}
});
return r;
}).property('stats.@each'),
sentItemsCount: (function() {
var r;
r = 0;
this.get('stats').each(function(s) {
if (s.action_type === Discourse.UserAction.NEW_PRIVATE_MESSAGE) {
r = s.count;
return false;
}
});
return r;
}).property('stats.@each')
});
Discourse.User.reopenClass({
checkUsername: function(username, email) {
return $.ajax({
url: '/users/check_username',
type: 'GET',
data: {
username: username,
email: email
}
});
},
groupStats: function(stats) {
var g,
_this = this;
g = {};
stats.each(function(s) {
var c, found, k, v, _ref;
found = false;
_ref = Discourse.UserAction.statGroups;
for (k in _ref) {
v = _ref[k];
if (v.contains(s.action_type)) {
found = true;
if (!g[k]) {
g[k] = Em.Object.create({
description: Em.String.i18n("user_action_descriptions." + k),
count: 0,
action_type: parseInt(k, 10)
});
}
g[k].count += parseInt(s.count, 10);
c = g[k].count;
if (s.action_type === k) {
g[k] = s;
s.count = c;
}
}
}
if (!found) {
g[s.action_type] = s;
}
});
return stats.map(function(s) {
return g[s.action_type];
}).exclude(function(s) {
return !s;
});
},
/**
Find a user by username
@method find
@param {String} username the username of the user we want to find
**/
find: function(username) {
// Check the preload store first
return PreloadStore.get("user_" + username, function() {
return $.ajax({ url: "/users/" + username + '.json' });
}).then(function (json) {
// Create a user from the resulting JSON
json.user.stats = Discourse.User.groupStats(json.user.stats.map(function(s) {
var stat = Em.Object.create(s);
stat.set('isPM', stat.get('action_type') === Discourse.UserAction.NEW_PRIVATE_MESSAGE ||
stat.get('action_type') === Discourse.UserAction.GOT_PRIVATE_MESSAGE);
return stat;
}));
if (json.user.stream) {
json.user.stream = Discourse.UserAction.collapseStream(json.user.stream.map(function(ua) {
return Discourse.UserAction.create(ua);
}));
}
return Discourse.User.create(json.user);
});
},
createAccount: function(name, email, password, username, passwordConfirm, challenge) {
return $.ajax({
url: '/users',
dataType: 'json',
data: {
name: name,
email: email,
password: password,
username: username,
password_confirmation: passwordConfirm,
challenge: challenge
},
type: 'POST'
});
}
});