Skip to content

Commit

Permalink
Merge pull request #100 from influenceth/filter-expired-agreements
Browse files Browse the repository at this point in the history
Filter expired agreements
  • Loading branch information
clexmond authored Jul 18, 2024
2 parents c127c44 + e217fe8 commit f13ae32
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 10 deletions.
4 changes: 2 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@influenceth/sdk",
"version": "2.1.2",
"version": "2.1.3",
"description": "Influence SDK",
"type": "module",
"module": "./build/index.js",
Expand Down
22 changes: 15 additions & 7 deletions src/lib/permission.js
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,8 @@ const POLICY_TYPES = {
},
};

const getPermissionPolicy = (entity, rawPermId, crew) => {
const getPermissionPolicy = (entity, rawPermId, crew, blockTime = null) => {
const nowTime = blockTime || Math.floor(Date.now() / 1000);
const permId = Number(rawPermId);

// default perm policy to private
Expand All @@ -146,7 +147,11 @@ const getPermissionPolicy = (entity, rawPermId, crew) => {

// agreement could be from previous policy, so need to attach agreements from any policy type (even if not active)
if (agreementKey) {
permPolicy.agreements.push(...(entity[agreementKey] || []).filter((a) => a.permission === permId));
permPolicy.agreements.push(
...(entity[agreementKey] || [])
.filter((a) => a.permission === permId)
.filter((a) => !a.endTime || a.endTime > nowTime)
);
}
}
});
Expand Down Expand Up @@ -175,18 +180,21 @@ const getPermissionPolicy = (entity, rawPermId, crew) => {
};

// TODO: put this in Crew?
const isPermitted = (crew, permission, hydratedTarget) => {
const policy = getPermissionPolicy(hydratedTarget, permission, crew);
return policy.crewStatus === 'controller' || policy.crewStatus === 'granted';
const isPermitted = (crew, permission, hydratedTarget, blockTime = null) => {
try {
const policy = getPermissionPolicy(hydratedTarget, permission, crew, blockTime);
return policy.crewStatus === 'controller' || policy.crewStatus === 'granted';
} catch {}
return false;
}

// get the applicable policies, agreements, and allowlists for this entity
const getPolicyDetails = (entity, crew = null) => {
const getPolicyDetails = (entity, crew = null, blockTime = null) => {
return Object.keys(TYPES)
.filter((id) => TYPES[id].isApplicable(entity))
.reduce((acc, permId) => ({
...acc,
[permId]: getPermissionPolicy(entity, permId, crew)
[permId]: getPermissionPolicy(entity, permId, crew, blockTime)
}), {});
};
Entity.getPolicyDetails = getPolicyDetails;
Expand Down

0 comments on commit f13ae32

Please sign in to comment.