Set user display name from /keys/current

So that it can be available for annotations created locally before
they're uploaded
This commit is contained in:
Dan Stillman 2022-02-19 12:32:33 -05:00
parent a2458614df
commit 4a914c26db
4 changed files with 42 additions and 11 deletions

View file

@ -169,8 +169,14 @@ Zotero_Preferences.Sync = {
);
return;
}
if (!(yield Zotero.Sync.Data.Local.checkUser(window, json.userID, json.username))) {
var ok = yield Zotero.Sync.Data.Local.checkUser(
window,
json.userID,
json.username,
json.displayName
);
if (!ok) {
// createAPIKeyFromCredentials will have created an API key,
// but user decided not to use it, so we remove it here.
Zotero.Sync.Runner.deleteAPIKey();

View file

@ -114,12 +114,14 @@ Zotero.Sync.Data.Local = {
*
* @param {Window|null}
* @param {Integer} userID - New userID
* @param {Integer} username - New username
* @param {String} username - New username
* @param {String} [name] - New display name
* @return {Boolean} - True to continue, false to cancel
*/
checkUser: Zotero.Promise.coroutine(function* (win, userID, username) {
checkUser: Zotero.Promise.coroutine(function* (win, userID, username, name) {
var lastUserID = Zotero.Users.getCurrentUserID();
var lastUsername = Zotero.Users.getCurrentUsername();
var lastName = Zotero.Users.getCurrentName();
if (lastUserID && lastUserID != userID) {
Zotero.debug(`Last user id ${lastUserID}, current user id ${userID}, `
@ -165,17 +167,21 @@ Zotero.Sync.Data.Local = {
return false;
}
yield Zotero.DB.executeTransaction(function* () {
yield Zotero.DB.executeTransaction(async function () {
if (lastUsername != username) {
yield Zotero.Users.setCurrentUsername(username);
}
await Zotero.Users.setCurrentUsername(username);
}
if (!lastUserID) {
yield Zotero.Users.setCurrentUserID(userID);
await Zotero.Users.setCurrentUserID(userID);
// Replace local user key with libraryID, in case duplicates were merged before the
// first sync
yield Zotero.Relations.updateUser(null, userID);
yield Zotero.Notes.updateUser(null, userID);
await Zotero.Relations.updateUser(null, userID);
await Zotero.Notes.updateUser(null, userID);
}
var newName = name || username;
if (lastName != newName) {
await Zotero.Users.setCurrentName(newName);
}
});

View file

@ -203,7 +203,13 @@ Zotero.Sync.Runner_Module = function (options = {}) {
let wm = Components.classes["@mozilla.org/appshell/window-mediator;1"]
.getService(Components.interfaces.nsIWindowMediator);
let lastWin = wm.getMostRecentWindow("navigator:browser");
if (!(yield Zotero.Sync.Data.Local.checkUser(lastWin, keyInfo.userID, keyInfo.username))) {
let ok = yield Zotero.Sync.Data.Local.checkUser(
lastWin,
keyInfo.userID,
keyInfo.username,
keyInfo.displayName
);
if (!ok) {
Zotero.debug("User cancelled sync on username mismatch");
return false;
}

View file

@ -90,6 +90,19 @@ Zotero.Users = new function () {
});
this.getCurrentName = function () {
var userID = this.getCurrentUserID();
return userID ? this.getName(userID) : '';
};
this.setCurrentName = async function (name) {
var userID = this.getCurrentUserID();
if (!userID) {
throw new Error("Current user ID must be set before setting name");
}
await this.setName(userID, name);
};
this.getLocalUserKey = function () {
return _localUserKey;
};