Some tests for the user stream

This commit is contained in:
Robin Ward 2013-07-26 17:09:54 -04:00
parent 5d66f6ed8e
commit 385b2fbde2
5 changed files with 110 additions and 42 deletions

View File

@ -8,6 +8,16 @@
**/
Discourse.User = Discourse.Model.extend({
/**
The user's stream
@property stream
@type {Discourse.UserStream}
**/
stream: function() {
return Discourse.UserStream.create({ user: this });
}.property(),
/**
Is this user a member of staff?
@ -32,9 +42,9 @@ Discourse.User = Discourse.Model.extend({
@property avatarSmall
@type {String}
**/
avatarSmall: (function() {
avatarSmall: function() {
return Discourse.Utilities.avatarUrl(this.get('username'), 'small', this.get('avatar_template'));
}).property('username'),
}.property('username'),
searchContext: function() {
return ({ type: 'user', id: this.get('username_lower'), user: this });
@ -269,26 +279,6 @@ Discourse.User = Discourse.Model.extend({
user.setProperties(json.user);
return user;
});
},
findStream: function(filter) {
// When filtering for replies, include mentions and quotes too
if (filter === Discourse.UserAction.TYPES.replies) {
filter = [Discourse.UserAction.TYPES.replies,
Discourse.UserAction.TYPES.mentions,
Discourse.UserAction.TYPES.quotes].join(",");
}
var stream = Discourse.UserStream.create({
itemsLoaded: 0,
content: [],
filter: filter,
user: this
});
stream.findItems();
return stream;
}
});

View File

@ -7,38 +7,64 @@
@module Discourse
**/
Discourse.UserStream = Discourse.Model.extend({
loaded: false,
filterChanged: function() {
this.setProperties({
content: Em.A(),
itemsLoaded: 0
});
this.findItems();
}.observes('filter'),
init: function() {
this.setProperties({ itemsLoaded: 0, content: [] });
},
filterParam: function() {
var filter = this.get('filter');
if (filter === Discourse.UserAction.TYPES.replies) {
return [Discourse.UserAction.TYPES.replies,
Discourse.UserAction.TYPES.mentions,
Discourse.UserAction.TYPES.quotes].join(",");
}
return filter;
}.property('filter'),
baseUrl: Discourse.computed.url('itemsLoaded', 'user.username_lower', '/user_actions.json?offset=%@&username=%@'),
filterBy: function(filter) {
if (this.get('loaded') && (this.get('filter') === filter)) { return Ember.RSVP.resolve(); }
this.set('filter', filter);
return this.findItems();
},
findItems: function() {
var userStream = this;
if(this.get("loading")) { return Ember.RSVP.reject(); }
this.set("loading",true);
if(this.get('loading')) { return Ember.RSVP.reject(); }
var url = Discourse.getURL("/user_actions.json?offset=") + this.get('itemsLoaded') + "&username=" + (this.get('user.username_lower'));
if (this.get('filter')) {
url += "&filter=" + (this.get('filter'));
this.setProperties({
loading: true,
itemsLoaded: 0,
content: []
});
var url = this.get('baseUrl');
if (this.get('filterParam')) {
url += "&filter=" + this.get('filterParam');
}
var stream = this;
var loadingFinished = function() {
userStream.set('loading', false);
};
return Discourse.ajax(url, {cache: 'false'}).then( function(result) {
userStream.set("loading",false);
if (result && result.user_actions) {
var copy = Em.A();
_.each(result.user_actions,function(action) {
result.user_actions.forEach(function(action) {
copy.pushObject(Discourse.UserAction.create(action));
});
copy = Discourse.UserAction.collapseStream(copy);
stream.get('content').pushObjects(copy);
stream.set('itemsLoaded', stream.get('itemsLoaded') + result.user_actions.length);
userStream.get('content').pushObjects(Discourse.UserAction.collapseStream(copy));
userStream.setProperties({
loaded: true,
itemsLoaded: userStream.get('itemsLoaded') + result.user_actions.length
});
}
}, function(){ userStream.set("loading", false); });
loadingFinished();
}, loadingFinished);
}
});

View File

@ -135,7 +135,11 @@ Discourse.UserIndexRoute = Discourse.UserActivityRoute.extend({
**/
Discourse.UserActivityStreamRoute = Discourse.Route.extend({
model: function() {
return this.modelFor('user').findStream(this.get('userActionType'));
return this.modelFor('user').get('stream');
},
afterModel: function() {
return this.modelFor('user').get('stream').filterBy(this.get('userActionType'));
},
renderTemplate: function() {

View File

@ -0,0 +1,29 @@
module("Discourse.User");
test('basics', function(){
var user = Discourse.User.create({id: 1, username: 'eviltrout'});
var stream = user.get('stream');
present(stream, "a user has a stream by default");
equal(stream.get('user'), user, "the stream points back to the user");
equal(stream.get('itemsLoaded'), 0, "no items are loaded by default");
blank(stream.get('content'), "no content by default");
blank(stream.get('filter'), "no filter by default");
ok(!stream.get('loaded'), "the stream is not loaded by default");
});
test('filterParam', function() {
var user = Discourse.User.create({id: 1, username: 'eviltrout'});
var stream = user.get('stream');
blank(stream.get('filterParam'), "it's blank by default");
stream.set('filter', Discourse.UserAction.TYPES.likes_given);
equal(stream.get('filterParam'), Discourse.UserAction.TYPES.likes_given);
stream.set('filter', Discourse.UserAction.TYPES.replies);
equal(stream.get('filterParam'), '6,7,9');
});

View File

@ -0,0 +1,19 @@
module("Discourse.User");
test('staff', function(){
var user = Discourse.User.create({id: 1, username: 'eviltrout'});
ok(!user.get('staff'), "user is not staff");
user.toggleProperty('moderator');
ok(user.get('staff'), "moderators are staff");
user.setProperties({moderator: false, admin: true});
ok(user.get('staff'), "admins are staff");
});
test('searchContext', function() {
var user = Discourse.User.create({id: 1, username: 'EvilTrout'});
deepEqual(user.get('searchContext'), {type: 'user', id: 'eviltrout', user: user}, "has a search context");
});