Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Filter expired agreements #100

Merged
merged 2 commits into from
Jul 18, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading