share.tsx 984 B

1234567891011121314151617181920212223242526272829303132333435
  1. import {
  2. checkToken,
  3. getCryptoString,
  4. makeErrorResponse,
  5. makeSuccessResponse,
  6. } from "utils/server.ts";
  7. import { define } from "utils/state.ts";
  8. import { find, update } from "utils/db.ts";
  9. export const handler = define.handlers({
  10. async POST(ctx) {
  11. const req = ctx.req;
  12. const reqJson = await req.json();
  13. const id = reqJson.id;
  14. const shared = reqJson.shared;
  15. const password = reqJson.password || "";
  16. const tokenUserId = checkToken(req);
  17. if (tokenUserId && id) {
  18. const post = find("Post", { id, user_id: tokenUserId });
  19. if (post.length > 0) {
  20. const hashedPassword = shared && password
  21. ? await getCryptoString(password, "MD5")
  22. : "";
  23. const newPost = update("Post", id, {
  24. shared: shared ? 1 : 0,
  25. share_password: hashedPassword,
  26. });
  27. if (newPost.length > 0) {
  28. return makeSuccessResponse(true);
  29. }
  30. }
  31. }
  32. return makeErrorResponse();
  33. },
  34. });