Skip to content

Commit

Permalink
Use new utility package for contributor field
Browse files Browse the repository at this point in the history
  • Loading branch information
futa-ikeda committed Nov 4, 2024
1 parent 7269b4d commit a6318d1
Show file tree
Hide file tree
Showing 3 changed files with 77 additions and 20 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import Intl from 'ember-intl/services/intl';
import InstitutionModel from 'ember-osf-web/models/institution';
import SearchResultModel from 'ember-osf-web/models/search-result';
import { AttributionRoleIris } from 'ember-osf-web/models/index-card';
import { getOsfmapObjects, getSingleOsfmapValue, hasOsfmapValue } from 'ember-osf-web/packages/osfmap/jsonld';

interface ContributorsFieldArgs {
searchResult: SearchResultModel;
Expand All @@ -24,32 +25,43 @@ export default class InstitutionalObjectListContributorsField extends Component<
// Return two contributors affiliated with the institution given with highest permission levels
get topInstitutionAffiliatedContributors() {
const { searchResult, institution } = this.args;
const attributions: any[] = searchResult.resourceMetadata.qualifiedAttribution;
const {resourceMetadata} = searchResult;
const attributions: any[] = getOsfmapObjects(resourceMetadata, ['qualifiedAttribution']);
const contributors = getOsfmapObjects(resourceMetadata, ['creator']);
const institutionIris = institution.iris;
const affiliatedAttributions = attributions
.filter((attribution: any) => hasInstitutionAffiliation(attribution, institutionIris));
const adminAttributions = affiliatedAttributions
.filter(attribution => attribution.hadRole[0]['@id'] === AttributionRoleIris.Admin);
const writeAttributions = affiliatedAttributions
.filter(attribution => attribution.hadRole[0]['@id'] === AttributionRoleIris.Write);
const readAttributions = affiliatedAttributions
.filter(attribution => attribution.hadRole[0]['@id'] === AttributionRoleIris.Read);
.filter((attribution: any) => hasInstitutionAffiliation(contributors, attribution, institutionIris));
const adminAttributions = attributions.filter(
attribution => hasOsfmapValue(attribution, ['hadRole'], AttributionRoleIris.Admin),
);
const writeAttributions = affiliatedAttributions.filter(
attribution => hasOsfmapValue(attribution, ['hadRole'], AttributionRoleIris.Write),
);
const readAttributions = affiliatedAttributions.filter(
attribution => hasOsfmapValue(attribution, ['hadRole'], AttributionRoleIris.Read),
);

const prioritizedAttributions = adminAttributions.concat(writeAttributions, readAttributions);

return prioritizedAttributions.slice(0, 2).map((attribution: any) => {
const roleIri: AttributionRoleIris = attribution.hadRole[0]['@id'];
return prioritizedAttributions.slice(0, 2).map(attribution => {
const attributedContributor = contributors.find(
(contributor: any) => contributor['@id'] === getSingleOsfmapValue(attribution,['agent']),
);
const roleIri: AttributionRoleIris = getSingleOsfmapValue(attribution, ['hadRole']);
return {
name: attribution.agent[0].name[0]['@value'],
url: attribution.agent[0]['@id'],
name: getSingleOsfmapValue(attributedContributor,['name']),
url: getSingleOsfmapValue(attributedContributor, ['identifier']),
permissionLevel: this.intl.t(roleIriToTranslationKey[roleIri]),
};
});
}
}

function hasInstitutionAffiliation(contributor: any, institutionIris: string[]) {
return contributor.agent[0].affiliation.some(
function hasInstitutionAffiliation(contributors: any[], attribution: any, institutionIris: string[]) {
const attributedContributor = contributors
.filter((contributor: any) => contributor['@id'] === attribution.agent[0]['@id']);

return attributedContributor[0].affiliation.some(
(affiliation: any) => affiliation.identifier.some(
(affiliationIdentifier: any) => institutionIris.includes(affiliationIdentifier['@value']),
),
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
{{#each this.topInstitutionAffiliatedContributors as |contributor|}}
<OsfLink
@href={{contributor.url}}
>
{{contributor.name}}
</OsfLink>
{{t 'institutions.dashboard.object-list.table-items.permission-level' permissionLevel=contributor.permissionLevel}}
<div>
<OsfLink
@href={{contributor.url}}
>
{{contributor.name}}
</OsfLink>
{{t 'institutions.dashboard.object-list.table-items.permission-level' permissionLevel=contributor.permissionLevel}}
</div>
{{/each}}
43 changes: 43 additions & 0 deletions app/packages/osfmap/jsonld.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
export function *iterOsfmapObjects(osfmapObject: any, propertyPath: string[]): IterableIterator<any> {
const [property, ...remainingPath] = propertyPath;
const innerObjArray = osfmapObject[property] || [];
if (remainingPath.length) {
for (const innerObj of innerObjArray) {
yield* iterOsfmapObjects(innerObj, remainingPath);
}
} else {
yield* innerObjArray;
}
}

export function *iterOsfmapValues(osfmapObject: any, propertyPath: string[]): IterableIterator<any> {
for (const obj of iterOsfmapObjects(osfmapObject, propertyPath)) {
yield (Object.hasOwn(obj, '@id') ? obj['@id'] : obj['@value']);
}
}

export function getOsfmapValues(osfmapObject: any, propertyPath: string[]) {
return Array.from(iterOsfmapValues(osfmapObject, propertyPath));
}

export function getSingleOsfmapValue(osfmapObject: any, propertyPath: string[]) {
return iterOsfmapValues(osfmapObject, propertyPath).next().value;
}

export function getOsfmapObjects(osfmapObject: any, propertyPath: string[]) {
return Array.from(iterOsfmapObjects(osfmapObject, propertyPath));
}

export function getSingleOsfmapObject(osfmapObject: any, propertyPath: string[]) {
return iterOsfmapObjects(osfmapObject, propertyPath).next().value;
}

export function hasOsfmapValue(osfmapObject: any, propertyPath: string[], expectedValue: any) {
// could use `Iterator.prototype.some()` instead, if polyfilled
for (const value of iterOsfmapValues(osfmapObject, propertyPath)) {
if (value === expectedValue) {
return true;
}
}
return false;
}

0 comments on commit a6318d1

Please sign in to comment.