2014-02-06 13:40:04 -05:00
|
|
|
/**
|
|
|
|
The data model for a Group
|
|
|
|
|
|
|
|
@class Group
|
|
|
|
@extends Discourse.Model
|
|
|
|
@namespace Discourse
|
|
|
|
@module Discourse
|
|
|
|
**/
|
2013-04-17 17:08:21 +10:00
|
|
|
Discourse.Group = Discourse.Model.extend({
|
2015-01-05 18:51:45 +01:00
|
|
|
limit: 50,
|
|
|
|
offset: 0,
|
|
|
|
user_count: 0,
|
2013-05-09 17:37:34 +10:00
|
|
|
|
2013-05-08 15:20:38 +10:00
|
|
|
userCountDisplay: function(){
|
|
|
|
var c = this.get('user_count');
|
|
|
|
// don't display zero its ugly
|
2015-01-05 18:51:45 +01:00
|
|
|
if (c > 0) { return c; }
|
2013-05-08 15:20:38 +10:00
|
|
|
}.property('user_count'),
|
|
|
|
|
2014-02-06 16:14:32 -05:00
|
|
|
findMembers: function() {
|
2015-01-05 18:51:45 +01:00
|
|
|
if (Em.isEmpty(this.get('name'))) { return ; }
|
2014-04-23 13:25:02 -04:00
|
|
|
|
2015-01-05 18:51:45 +01:00
|
|
|
var self = this, offset = Math.min(this.get("user_count"), Math.max(this.get("offset"), 0));
|
|
|
|
|
|
|
|
return Discourse.ajax('/groups/' + this.get('name') + '/members.json', {
|
|
|
|
data: {
|
|
|
|
limit: this.get("limit"),
|
|
|
|
offset: offset
|
|
|
|
}
|
|
|
|
}).then(function(result) {
|
|
|
|
self.setProperties({
|
|
|
|
user_count: result.meta.total,
|
|
|
|
limit: result.meta.limit,
|
|
|
|
offset: result.meta.offset,
|
|
|
|
members: result.members.map(function(member) { return Discourse.User.create(member); })
|
|
|
|
});
|
2014-02-06 16:14:32 -05:00
|
|
|
});
|
|
|
|
},
|
|
|
|
|
2015-01-05 18:51:45 +01:00
|
|
|
removeMember: function(member) {
|
|
|
|
var self = this;
|
|
|
|
return Discourse.ajax('/admin/groups/' + this.get('id') + '/members.json', {
|
|
|
|
type: "DELETE",
|
|
|
|
data: { user_id: member.get("id") }
|
|
|
|
}).then(function() {
|
|
|
|
// reload member list
|
|
|
|
self.findMembers();
|
|
|
|
});
|
2013-05-09 17:37:34 +10:00
|
|
|
},
|
|
|
|
|
2015-01-05 18:51:45 +01:00
|
|
|
addMembers: function(usernames) {
|
|
|
|
var self = this;
|
|
|
|
return Discourse.ajax('/admin/groups/' + this.get('id') + '/members.json', {
|
|
|
|
type: "PUT",
|
|
|
|
data: { usernames: usernames }
|
|
|
|
}).then(function() {
|
|
|
|
// reload member list
|
|
|
|
self.findMembers();
|
|
|
|
})
|
2014-04-22 16:43:46 -04:00
|
|
|
},
|
|
|
|
|
2015-01-05 18:51:45 +01:00
|
|
|
asJSON: function() {
|
|
|
|
return {
|
|
|
|
name: this.get('name'),
|
|
|
|
alias_level: this.get('alias_level'),
|
|
|
|
visible: !!this.get('visible')
|
|
|
|
};
|
|
|
|
},
|
2013-05-09 17:37:34 +10:00
|
|
|
|
2015-01-05 18:51:45 +01:00
|
|
|
create: function(){
|
|
|
|
var self = this;
|
|
|
|
return Discourse.ajax("/admin/groups", { type: "POST", data: this.asJSON() }).then(function(resp) {
|
2014-04-23 13:25:02 -04:00
|
|
|
self.set('id', resp.basic_group.id);
|
2013-05-09 17:37:34 +10:00
|
|
|
});
|
2013-05-24 21:04:26 +10:00
|
|
|
},
|
|
|
|
|
2015-01-05 18:51:45 +01:00
|
|
|
save: function(){
|
|
|
|
return Discourse.ajax("/admin/groups/" + this.get('id'), { type: "PUT", data: this.asJSON() });
|
|
|
|
},
|
|
|
|
|
|
|
|
destroy: function(){
|
|
|
|
if (!this.get('id')) { return };
|
|
|
|
return Discourse.ajax("/admin/groups/" + this.get('id'), {type: "DELETE"});
|
2014-02-07 10:44:03 -05:00
|
|
|
},
|
|
|
|
|
|
|
|
findPosts: function(opts) {
|
|
|
|
opts = opts || {};
|
|
|
|
|
|
|
|
var data = {};
|
|
|
|
if (opts.beforePostId) { data.before_post_id = opts.beforePostId; }
|
2013-04-17 17:08:21 +10:00
|
|
|
|
2014-02-07 10:44:03 -05:00
|
|
|
return Discourse.ajax("/groups/" + this.get('name') + "/posts.json", { data: data }).then(function (posts) {
|
|
|
|
return posts.map(function (p) {
|
|
|
|
p.user = Discourse.User.create(p.user);
|
|
|
|
return Em.Object.create(p);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
}
|
2013-04-17 17:08:21 +10:00
|
|
|
});
|
|
|
|
|
|
|
|
Discourse.Group.reopenClass({
|
2014-05-09 18:22:15 +10:00
|
|
|
findAll: function(opts){
|
|
|
|
return Discourse.ajax("/admin/groups.json", { data: opts }).then(function(groups){
|
2014-04-23 13:25:02 -04:00
|
|
|
return groups.map(function(g) { return Discourse.Group.create(g); });
|
2013-05-08 15:20:38 +10:00
|
|
|
});
|
2014-02-06 13:40:04 -05:00
|
|
|
},
|
|
|
|
|
2014-02-18 16:17:04 -05:00
|
|
|
findGroupCounts: function(name) {
|
|
|
|
return Discourse.ajax("/groups/" + name + "/counts.json").then(function (result) {
|
|
|
|
return Em.Object.create(result.counts);
|
2014-02-12 14:00:45 -05:00
|
|
|
});
|
|
|
|
},
|
|
|
|
|
2014-02-06 16:14:32 -05:00
|
|
|
find: function(name) {
|
|
|
|
return Discourse.ajax("/groups/" + name + ".json").then(function(g) {
|
|
|
|
return Discourse.Group.create(g.basic_group);
|
|
|
|
});
|
2013-04-17 17:08:21 +10:00
|
|
|
}
|
|
|
|
});
|