<commit_before>/// <reference path="../../../lib2/types/angular.d.ts"/>
/// <reference path="../../../lib2/types/lodash.d.ts"/>

import * as _ from "lodash";

import * as AdhConfig from "../Config/Config";
import * as AdhCredentials from "../User/Credentials";
import * as AdhHttp from "../Http/Http";
import * as AdhPermissions from "../Permissions/Permissions";
import * as AdhPreliminaryNames from "../PreliminaryNames/PreliminaryNames";
import * as AdhMovingColumns from "../MovingColumns/MovingColumns";
import * as AdhUtil from "../Util/Util";

import RIBadgeAssignment from "../../Resources_/adhocracy_core/resources/badge/IBadgeAssignment";
import * as SIBadge from "../../Resources_/adhocracy_core/sheets/badge/IBadge";
import * as SIBadgeable from "../../Resources_/adhocracy_core/sheets/badge/IBadgeable";
import * as SIBadgeAssignment from "../../Resources_/adhocracy_core/sheets/badge/IBadgeAssignment";
import * as SIDescription from "../../Resources_/adhocracy_core/sheets/description/IDescription";
import * as SIName from "../../Resources_/adhocracy_core/sheets/name/IName";
import * as SITitle from "../../Resources_/adhocracy_core/sheets/title/ITitle";

var pkgLocation = "/Badge";

export interface IBadge {
    title : string;
    description : string;
    name : string;
    path : string;
    badgePath : string;
}

export interface IGetBadges {
    (resource : SIBadgeable.HasSheet) : angular.IPromise<IBadge[]>;
}

export var getBadgesFactory = (
    adhHttp : AdhHttp.Service<any>,
    $q : angular.IQService
) : IGetBadges => (
    resource : SIBadgeable.HasSheet,
    includeParent? : boolean
) : angular.IPromise<IBadge[]> => {
    if (typeof includeParent === "undefined") {
        includeParent = !!resource.content_type.match(/Version$/);
    }

    var assignmentPaths = resource.data[SIBadgeable.nick].assignments;

    var getBadge = (assignmentPath : string) => {
        return adhHttp.get(assignmentPath).then((assignment : RIBadgeAssignment) => {
            var badgePath = assignment.data[SIBadgeAssignment.nick].badge;
            return adhHttp.get(badgePath).then((badge) => {
                return {
                    title: badge.data[SITitle.nick].title,
                    name: badge.data[SIName.nick].name,
                    description: assignment.data[SIDescription.nick].description,
                    path: assignmentPath,
                    badgePath: badgePath
                };
            });
        });
    };

    if (includeParent) {
        var parentPath = AdhUtil.parentPath(resource.path);

        return adhHttp.get(parentPath).then((parentResource) => {
            var parentAssignments = parentResource.data[SIBadgeable.nick].assignments;
            assignmentPaths = assignmentPaths.concat(parentAssignments);
            return $q.all(_.map(assignmentPaths, getBadge));
        });
    } else {
        return $q.all(_.map(assignmentPaths, getBadge));
    }
};


var bindPath = (
    adhHttp : AdhHttp.Service<any>,
    adhPermissions : AdhPermissions.Service,
    $q : angular.IQService
) => (
    scope
) : void => {
    scope.data = {
        badge: "",
        description: ""
    };

    var extractBadge = (badge) => {
        return {
            title: badge.data[SITitle.nick].title,
            path: badge.path,
            groups: badge.data[SIBadge.nick].groups
        };
    };

    var extractGroup = (group) => {
        return {
            title: group.data[SITitle.nick].title,
            path: group.path
        };
    };

    var collectBadgesByGroup = (groupPaths, badges) => {
        var badgesByGroup = {};
        for (var groupPath of groupPaths) {
            badgesByGroup[groupPath] = [];
            for (var badge of badges) {
                if (_.includes(badge.groups, groupPath)) {
                    badgesByGroup[groupPath].push(badge.path);
                }
            }
        }
        return badgesByGroup;
    };

    var getAssignableBadges = (rawOptions) => {
        if (!rawOptions) {
            return;
        }

        var requestBodyOptions : {content_type : string}[] = AdhUtil.deepPluck(rawOptions, [
            "data", "POST", "request_body"
        ]);
<commit_msg>use _.forOwn instead of for-of<commit_after>/// <reference path="../../../lib2/types/angular.d.ts"/>
/// <reference path="../../../lib2/types/lodash.d.ts"/>

import * as _ from "lodash";

import * as AdhConfig from "../Config/Config";
import * as AdhCredentials from "../User/Credentials";
import * as AdhHttp from "../Http/Http";
import * as AdhPermissions from "../Permissions/Permissions";
import * as AdhPreliminaryNames from "../PreliminaryNames/PreliminaryNames";
import * as AdhMovingColumns from "../MovingColumns/MovingColumns";
import * as AdhUtil from "../Util/Util";

import RIBadgeAssignment from "../../Resources_/adhocracy_core/resources/badge/IBadgeAssignment";
import * as SIBadge from "../../Resources_/adhocracy_core/sheets/badge/IBadge";
import * as SIBadgeable from "../../Resources_/adhocracy_core/sheets/badge/IBadgeable";
import * as SIBadgeAssignment from "../../Resources_/adhocracy_core/sheets/badge/IBadgeAssignment";
import * as SIDescription from "../../Resources_/adhocracy_core/sheets/description/IDescription";
import * as SIName from "../../Resources_/adhocracy_core/sheets/name/IName";
import * as SITitle from "../../Resources_/adhocracy_core/sheets/title/ITitle";

var pkgLocation = "/Badge";

export interface IBadge {
    title : string;
    description : string;
    name : string;
    path : string;
    badgePath : string;
}

export interface IGetBadges {
    (resource : SIBadgeable.HasSheet) : angular.IPromise<IBadge[]>;
}

export var getBadgesFactory = (
    adhHttp : AdhHttp.Service<any>,
    $q : angular.IQService
) : IGetBadges => (
    resource : SIBadgeable.HasSheet,
    includeParent? : boolean
) : angular.IPromise<IBadge[]> => {
    if (typeof includeParent === "undefined") {
        includeParent = !!resource.content_type.match(/Version$/);
    }

    var assignmentPaths = resource.data[SIBadgeable.nick].assignments;

    var getBadge = (assignmentPath : string) => {
        return adhHttp.get(assignmentPath).then((assignment : RIBadgeAssignment) => {
            var badgePath = assignment.data[SIBadgeAssignment.nick].badge;
            return adhHttp.get(badgePath).then((badge) => {
                return {
                    title: badge.data[SITitle.nick].title,
                    name: badge.data[SIName.nick].name,
                    description: assignment.data[SIDescription.nick].description,
                    path: assignmentPath,
                    badgePath: badgePath
                };
            });
        });
    };

    if (includeParent) {
        var parentPath = AdhUtil.parentPath(resource.path);

        return adhHttp.get(parentPath).then((parentResource) => {
            var parentAssignments = parentResource.data[SIBadgeable.nick].assignments;
            assignmentPaths = assignmentPaths.concat(parentAssignments);
            return $q.all(_.map(assignmentPaths, getBadge));
        });
    } else {
        return $q.all(_.map(assignmentPaths, getBadge));
    }
};


var bindPath = (
    adhHttp : AdhHttp.Service<any>,
    adhPermissions : AdhPermissions.Service,
    $q : angular.IQService
) => (
    scope
) : void => {
    scope.data = {
        badge: "",
        description: ""
    };

    var extractBadge = (badge) => {
        return {
            title: badge.data[SITitle.nick].title,
            path: badge.path,
            groups: badge.data[SIBadge.nick].groups
        };
    };

    var extractGroup = (group) => {
        return {
            title: group.data[SITitle.nick].title,
            path: group.path
        };
    };

    var collectBadgesByGroup = (groupPaths, badges) => {
        var badgesByGroup = {};
        _.forOwn(groupPaths, (groupPath) => {
            badgesByGroup[groupPath] = [];
            _.forOwn(badges, (badge) => {
                if (_.includes(badge.groups, groupPath)) {
                    badgesByGroup[groupPath].push(badge.path);
                }
            });
        });
        return badgesByGroup;
    };

    var getAssignableBadges = (rawOptions) => {
        if (!rawOptions) {
            return;
        }

        var requestBodyOptions : {content_type : string}[] = AdhUtil.deepPluck(rawOptions, [
            "data", "POST", "request_body"
        ]);

{'fraction_non_alphanumeric': 0.13837965700768776, 'fraction_numerical': 0.0004730928444707274, 'mean_word_length': 2.4083031035872633, 'pattern_counts': {'":': 0, '<': 15, '<?xml version=': 0, '>': 41, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3405355', 'n_tokens_mistral': 2579, 'n_tokens_neox': 2519, 'n_words': 462}