Commit 75175aec authored by Sean Hammond's avatar Sean Hammond

Remove USER_CHANGED event from AnnotationController

When the user logs out or logs in we throw away all the
AnnotationController instances and create new ones. So everything it
needs to do "when the user logs in" (or out) AnnotationController can do
on Initialization, it doesn't need the event.
parent 58d19bf0
......@@ -174,14 +174,12 @@ function AnnotationController(
/** The domain model, contains the currently saved version of the annotation
* from the server. */
var model = $scope.annotationGet();
if (!model.user) {
model.user = session.state.userid;
}
// Set the group of new annotations.
if (!model.group) {
model.group = groups.focused().id;
}
// Set the user of new annotations that don't have a user yet.
model.user = model.user || session.state.userid;
// Set the group of new annotations that don't have a group yet.
model.group = model.group || groups.focused().id;
// Set the permissions of new annotations.
model.permissions = model.permissions || permissions['default'](model.group);
......@@ -639,20 +637,6 @@ function AnnotationController(
vm.render();
}, true);
$scope.$on(events.USER_CHANGED, function() {
if (!model.user) {
model.user = session.state.userid;
}
// Set model.permissions on sign in, if it isn't already set.
// This is because you can create annotations when signed out and they
// will have model.permissions = null, then when you sign in we set the
// permissions correctly here.
if (!model.permissions) {
model.permissions = permissions['default'](model.group);
}
});
// If this annotation is not a highlight and if it's new (has just been
// created by the annotate button) or it has edits not yet saved to the
// server - then open the editor on AnnotationController instantiation.
......
......@@ -560,6 +560,38 @@ describe('annotation.js', function() {
});
describe('AnnotationController() initialization', function() {
it('sets the user of annotations that don\'t have one', function() {
// You can create annotations while logged out and then login.
// When you login a new AnnotationController instance is created for
// each of your annotations, and on initialization it will set the
// annotation's user to your username from the session.
var annotation = newAnnotation();
annotation.user = undefined;
fakeSession.state.userid = 'acct:bill@localhost';
createDirective(annotation);
assert.equal(annotation.user, 'acct:bill@localhost');
});
it(
'sets the permissions of annotations that don\'t have any',
function() {
// You can create annotations while logged out and then login.
// When you login a new AnnotationController instance is created for
// each of your annotations, and on initialization it will set the
// annotation's permissions using your username from the session.
var annotation = newAnnotation();
annotation.user = annotation.permissions = undefined;
fakeSession.state.userid = 'acct:bill@localhost';
fakePermissions.default.returns('default permissions');
createDirective(annotation);
assert.equal(annotation.permissions, 'default permissions');
}
);
it('saves new highlights to the server on initialization', function() {
var annotation = newHighlight();
// The user is logged-in.
......@@ -1730,73 +1762,6 @@ describe('annotation.js', function() {
);
});
describe('when the user signs in', function() {
it('sets the user of unsaved annotations', function() {
// This annotation has no user yet, because that's what happens
// when you create a new annotation while not signed in.
var annotation = {};
var session = {
state: {
userid: null // Not signed in.
}
};
var $rootScope = createAnnotationDirective({
annotation: annotation,
session: session
}).$rootScope;
// At this point we would not expect the user to have been set,
// even though the annotation has been created, because the user isn't
// signed in.
assert(!annotation.user);
// Sign the user in.
session.state.userid = 'acct:fred@hypothes.is';
// The session service would broadcast USER_CHANGED after sign in.
$rootScope.$broadcast(events.USER_CHANGED, {});
assert.equal(annotation.user, session.state.userid);
});
it('sets the permissions of unsaved annotations', function() {
// This annotation has no permissions yet, because that's what happens
// when you create a new annotation while not signed in.
var annotation = {
group: '__world__'
};
var session = {
state: {
userid: null // Not signed in.
}
};
var permissions = {
// permissions.default() would return null, because the user isn't
// signed in.
'default': function() {
return null;
},
isShared: function() {},
isPrivate: function() {}
};
var $rootScope = createAnnotationDirective({
annotation: annotation,
session: session,
permissions: permissions
}).$rootScope;
// At this point we would not expect the permissions to have been set,
// even though the annotation has been created, because the user isn't
// signed in.
assert(!annotation.permissions);
// Sign the user in.
session.state.userid = 'acct:fred@hypothes.is';
// permissions.default() would now return permissions, because the user
// is signed in.
permissions['default'] = function() {
return '__default_permissions__';
};
// The session service would broadcast USER_CHANGED after sign in.
$rootScope.$broadcast(events.USER_CHANGED, {});
assert.equal(annotation.permissions, '__default_permissions__');
});
});
/*
Simulate what happens when the user edits an annotation, clicks Save,
gets an error because the server fails to save the annotation, then clicks
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment