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:
parent
d33a904207
commit
9f4b15bbb7
5 changed files with 21 additions and 12 deletions
|
@ -1335,10 +1335,10 @@ Zotero.DataObject.prototype.toResponseJSON = function (options = {}) {
|
||||||
href: Zotero.URI.toAPIURL(uri, options.apiURL),
|
href: Zotero.URI.toAPIURL(uri, options.apiURL),
|
||||||
type: 'application/json'
|
type: 'application/json'
|
||||||
},
|
},
|
||||||
alternate: {
|
alternate: Zotero.Users.getCurrentUserID() ? {
|
||||||
href: Zotero.URI.toWebURL(uri),
|
href: Zotero.URI.toWebURL(uri),
|
||||||
type: 'text/html'
|
type: 'text/html'
|
||||||
}
|
} : undefined
|
||||||
},
|
},
|
||||||
meta: {},
|
meta: {},
|
||||||
data: this.toJSON(options)
|
data: this.toJSON(options)
|
||||||
|
|
|
@ -136,7 +136,7 @@ Zotero.defineProperty(Zotero.Library.prototype, 'libraryTypeID', {
|
||||||
get: function () {
|
get: function () {
|
||||||
switch (this._libraryType) {
|
switch (this._libraryType) {
|
||||||
case 'user':
|
case 'user':
|
||||||
return Zotero.Users.getCurrentUserID();
|
return Zotero.Users.getCurrentUserID() || 0;
|
||||||
|
|
||||||
case 'group':
|
case 'group':
|
||||||
return Zotero.Groups.getGroupIDFromLibraryID(this._libraryID);
|
return Zotero.Groups.getGroupIDFromLibraryID(this._libraryID);
|
||||||
|
@ -689,10 +689,10 @@ Zotero.Library.prototype.toResponseJSON = function (options = {}) {
|
||||||
href: Zotero.URI.toAPIURL(uri, options.apiURL),
|
href: Zotero.URI.toAPIURL(uri, options.apiURL),
|
||||||
type: 'application/json'
|
type: 'application/json'
|
||||||
},
|
},
|
||||||
alternate: {
|
alternate: Zotero.Users.getCurrentUserID() ? {
|
||||||
href: Zotero.URI.toWebURL(uri),
|
href: Zotero.URI.toWebURL(uri),
|
||||||
type: 'text/html'
|
type: 'text/html'
|
||||||
}
|
} : undefined
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
};
|
};
|
||||||
|
|
|
@ -253,10 +253,10 @@ Zotero.Tags = new function() {
|
||||||
href: Zotero.URI.toAPIURL(uri),
|
href: Zotero.URI.toAPIURL(uri),
|
||||||
type: 'application/json'
|
type: 'application/json'
|
||||||
},
|
},
|
||||||
alternate: {
|
alternate: Zotero.Users.getCurrentUserID() ? {
|
||||||
href: uri, // No toWebURL - match dataserver behavior
|
href: uri, // No toWebURL - match dataserver behavior
|
||||||
type: 'text/html'
|
type: 'text/html'
|
||||||
}
|
} : undefined
|
||||||
},
|
},
|
||||||
meta: {
|
meta: {
|
||||||
type: tag.type || 0,
|
type: tag.type || 0,
|
||||||
|
|
|
@ -101,7 +101,12 @@ class LocalAPIEndpoint {
|
||||||
if (userID !== undefined
|
if (userID !== undefined
|
||||||
&& userID != 0
|
&& userID != 0
|
||||||
&& userID != Zotero.Users.getCurrentUserID()) {
|
&& 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
|
requestData.libraryID = requestData.pathParams.groupID
|
||||||
|
@ -273,10 +278,11 @@ class LocalAPIEndpoint {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// alternate: cut off '/api/', replace userID 0 with current user's ID
|
// alternate: only include if logged in, cut off '/api/', replace userID 0 with current userID
|
||||||
links.alternate = ZOTERO_CONFIG.WWW_BASE_URL
|
if (Zotero.Users.getCurrentUserID()) {
|
||||||
+ requestData.pathname.substring(5)
|
links.alternate = ZOTERO_CONFIG.WWW_BASE_URL + requestData.pathname.substring(5)
|
||||||
.replace('users/0/', `users/${Zotero.Users.getCurrentUserID()}/`);
|
.replace('users/0/', `users/${Zotero.Users.getCurrentUserID()}/`);
|
||||||
|
}
|
||||||
|
|
||||||
return links;
|
return links;
|
||||||
}
|
}
|
||||||
|
|
|
@ -359,7 +359,10 @@ Zotero.URI = new function () {
|
||||||
if (!apiURL) {
|
if (!apiURL) {
|
||||||
apiURL = ZOTERO_CONFIG.API_URL;
|
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');
|
||||||
};
|
};
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|
Loading…
Reference in a new issue