Local API: Better handle users who have never logged in (#4270)

- Don't include `alternate` links
- Replace `users/local/:localKey` with `users/:userID` in `self` links
- Use `0` for id in `library` block
This commit is contained in:
Dan Stillman 2024-06-12 04:54:21 -04:00
parent d33a904207
commit 9f4b15bbb7
5 changed files with 21 additions and 12 deletions

View file

@ -1335,10 +1335,10 @@ Zotero.DataObject.prototype.toResponseJSON = function (options = {}) {
href: Zotero.URI.toAPIURL(uri, options.apiURL),
type: 'application/json'
},
alternate: {
alternate: Zotero.Users.getCurrentUserID() ? {
href: Zotero.URI.toWebURL(uri),
type: 'text/html'
}
} : undefined
},
meta: {},
data: this.toJSON(options)

View file

@ -136,7 +136,7 @@ Zotero.defineProperty(Zotero.Library.prototype, 'libraryTypeID', {
get: function () {
switch (this._libraryType) {
case 'user':
return Zotero.Users.getCurrentUserID();
return Zotero.Users.getCurrentUserID() || 0;
case 'group':
return Zotero.Groups.getGroupIDFromLibraryID(this._libraryID);
@ -689,10 +689,10 @@ Zotero.Library.prototype.toResponseJSON = function (options = {}) {
href: Zotero.URI.toAPIURL(uri, options.apiURL),
type: 'application/json'
},
alternate: {
alternate: Zotero.Users.getCurrentUserID() ? {
href: Zotero.URI.toWebURL(uri),
type: 'text/html'
}
} : undefined
}
};
};

View file

@ -253,10 +253,10 @@ Zotero.Tags = new function() {
href: Zotero.URI.toAPIURL(uri),
type: 'application/json'
},
alternate: {
alternate: Zotero.Users.getCurrentUserID() ? {
href: uri, // No toWebURL - match dataserver behavior
type: 'text/html'
}
} : undefined
},
meta: {
type: tag.type || 0,

View file

@ -101,7 +101,12 @@ class LocalAPIEndpoint {
if (userID !== undefined
&& userID != 0
&& userID != Zotero.Users.getCurrentUserID()) {
return this.makeResponse(400, 'text/plain', 'Only data for the logged-in user is available locally - use userID 0');
let suffix = "";
let currentUserID = Zotero.Users.getCurrentUserID();
if (currentUserID) {
suffix += " or " + currentUserID;
}
return this.makeResponse(400, 'text/plain', 'Only data for the logged-in user is available locally -- use userID 0' + suffix);
}
requestData.libraryID = requestData.pathParams.groupID
@ -273,10 +278,11 @@ class LocalAPIEndpoint {
}
}
// alternate: cut off '/api/', replace userID 0 with current user's ID
links.alternate = ZOTERO_CONFIG.WWW_BASE_URL
+ requestData.pathname.substring(5)
// alternate: only include if logged in, cut off '/api/', replace userID 0 with current userID
if (Zotero.Users.getCurrentUserID()) {
links.alternate = ZOTERO_CONFIG.WWW_BASE_URL + requestData.pathname.substring(5)
.replace('users/0/', `users/${Zotero.Users.getCurrentUserID()}/`);
}
return links;
}

View file

@ -359,7 +359,10 @@ Zotero.URI = new function () {
if (!apiURL) {
apiURL = ZOTERO_CONFIG.API_URL;
}
return uri.replace(ZOTERO_CONFIG.BASE_URI, apiURL);
return uri
.replace(ZOTERO_CONFIG.BASE_URI, apiURL)
// Replace local user key with users/0
.replace(/(http:\/\/localhost:\d+\/api\/users)\/local\/\w+/, '$1/0');
};
/**