From a9447aff1e37348ae1888406ab55abaa53ab1a87 Mon Sep 17 00:00:00 2001 From: chaojun Date: Wed, 4 Sep 2024 14:42:54 +0800 Subject: [PATCH] fix: check society member, #1034 --- .../services/node.service/getSocietyMember.js | 5 ++++- .../node.service/getSocietyMembers.js | 5 ++++- .../proposal.service/createSocietyProposal.js | 22 +++++++++++++++++++ .../node-api/src/features/society/routes.js | 2 ++ 4 files changed, 32 insertions(+), 2 deletions(-) diff --git a/backend/packages/backend/src/services/node.service/getSocietyMember.js b/backend/packages/backend/src/services/node.service/getSocietyMember.js index 8de284b2..5eb3a00d 100644 --- a/backend/packages/backend/src/services/node.service/getSocietyMember.js +++ b/backend/packages/backend/src/services/node.service/getSocietyMember.js @@ -2,7 +2,10 @@ const { NODE_API_ENDPOINT } = require("../../env"); const { fetchApi } = require("../../utils/fech.api"); async function getSocietyMember(network, address, height) { - const url = `${NODE_API_ENDPOINT}/${network}/society/members/${address}/height/${height}`; + let url = `${NODE_API_ENDPOINT}/${network}/society/members/${address}`; + if (height) { + url = `${NODE_API_ENDPOINT}/${network}/society/members/${address}/height/${height}`; + } return await fetchApi(url); } diff --git a/backend/packages/backend/src/services/node.service/getSocietyMembers.js b/backend/packages/backend/src/services/node.service/getSocietyMembers.js index 0961ec2a..161bd9f3 100644 --- a/backend/packages/backend/src/services/node.service/getSocietyMembers.js +++ b/backend/packages/backend/src/services/node.service/getSocietyMembers.js @@ -2,7 +2,10 @@ const { NODE_API_ENDPOINT } = require("../../env"); const { fetchApi } = require("../../utils/fech.api"); async function getSocietyMembers(network, height) { - const url = `${NODE_API_ENDPOINT}/${network}/society/members/height/${height}`; + let url = `${NODE_API_ENDPOINT}/${network}/society/members`; + if (height) { + url = `${NODE_API_ENDPOINT}/${network}/society/members/height/${height}`; + } return await fetchApi(url); } diff --git a/backend/packages/backend/src/services/proposal.service/createSocietyProposal.js b/backend/packages/backend/src/services/proposal.service/createSocietyProposal.js index 5bfedf20..aedb8410 100644 --- a/backend/packages/backend/src/services/proposal.service/createSocietyProposal.js +++ b/backend/packages/backend/src/services/proposal.service/createSocietyProposal.js @@ -6,6 +6,22 @@ const { HttpError } = require("../../exc"); const { ContentType } = require("../../constants"); const { spaces: spaceServices } = require("../../spaces"); const { pinData, createSpaceNotifications } = require("./common"); +const { Accessibility } = require("../../consts/space"); +const { getSocietyMember } = require("../node.service/getSocietyMember"); + +async function checkSocietyMember({ + networksConfig, + proposerNetwork, + proposer, +}) { + if (networksConfig.accessibility !== Accessibility.SOCIETY) { + return; + } + const societyMember = await getSocietyMember(proposerNetwork, proposer); + if (!societyMember.data) { + throw new HttpError(400, "You are not the society member"); + } +} async function createSocietyProposal({ space, @@ -30,6 +46,12 @@ async function createSocietyProposal({ const proposer = realProposer || address; + await checkSocietyMember({ + networksConfig, + proposerNetwork, + proposer, + }); + const { cid, pinHash } = await pinData({ data, address, signature }); const postUid = await nextPostUid(); diff --git a/backend/packages/node-api/src/features/society/routes.js b/backend/packages/node-api/src/features/society/routes.js index fe17fc1b..2bad9d54 100644 --- a/backend/packages/node-api/src/features/society/routes.js +++ b/backend/packages/node-api/src/features/society/routes.js @@ -7,6 +7,8 @@ router.get( "/society/members/:address/height/:blockHashOrHeight?", getSocietyMember, ); +router.get("/society/members/:address", getSocietyMember); router.get("/society/members/height/:blockHashOrHeight?", getSocietyMembers); +router.get("/society/members", getSocietyMembers); module.exports = router;