setRating.js 1.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546
  1. /*
  2. setRating.js
  3. Set (or clear) the user's star rating for a single photo. Ratings are stored
  4. per-user in the photo_ratings table (see imagedb.js) keyed by the photo's
  5. virtual path, so they persist across re-indexing and schema rebuilds.
  6. Request body (JSON):
  7. { "filepath": "user:/Photo/a.jpg", "rating": 0..5 }
  8. A rating of 0 (or less) clears the rating.
  9. Response (JSON):
  10. { ok: true, filepath, rating } // rating is the stored value (0 = cleared)
  11. */
  12. includes("imagedb.js");
  13. function main() {
  14. var rawBody = (typeof POST_data !== "undefined") ? POST_data : "{}";
  15. var payload = {};
  16. try {
  17. payload = JSON.parse(rawBody) || {};
  18. } catch (e) {
  19. payload = {};
  20. }
  21. var filepath = payload.filepath || "";
  22. if (!filepath) {
  23. sendJSONResp(JSON.stringify({ error: "missing filepath" }));
  24. return;
  25. }
  26. var db = openIndexDB();
  27. if (db == null) {
  28. sendJSONResp(JSON.stringify({ error: "index unavailable" }));
  29. return;
  30. }
  31. var stored = db_setRating(db, filepath, payload.rating);
  32. db.close();
  33. sendJSONResp(JSON.stringify({ ok: true, filepath: filepath, rating: stored }));
  34. }
  35. main();