How can I add a space to a users favourites?

Hello there!
I’m using confluence server 7.0.1.
Within one of my plugin’s listeners, I would like to mark a space as favourite for some users.
Can you point me to the right call to achieve that?

I tried SpaceLabelManager.addLabel(space, "my:favourite") but no luck, it just adds this as a normal label.

Am I supposed to use the LabelManager.addLabel(Labelable content, Label label) method? If so, how do I get the Labelable object for my Space and how can I get the right Label to add?

Any help is greatly appreciated!
Cwittenb

Ok, so by digging into the confluence source I found, I could use FavouriteManager.addSpaceToFavourites(space, user)

However when I try this in my LoginEvent Listener, it throws an NotAuthorizedException and tells me that the user is still anonymous.

Any hints on how to deal with this?

If this is in the event listener then you probably should set the user in the current thread with com.atlassian.confluence.user.AuthenticatedUserThreadLocal.set(user)

Have not tried this particular API but we do this in our background threads to change the current user in the current thread. And after your code better reset the current user with AuthenticatedUserThreadLocal.reset()

Kind regards,
Raimonds

Thanks Raimonds, that did work indeed!

I also found that I could get around the permission check, by just calling the LabelManager directly (which does not check permissions :blush: ), as the FavouriteManager does:

            Label label = new Label(LabelManager.FAVOURITE_LABEL, Namespace.PERSONAL, user);
            labelmanager.addLabel((Labelable) s.getDescription(), label);