public function get_attachments_for_posts(context $context, array $posts) {
        $itemids = array_map(function($post) {
            return $post->get_id();
        }, $posts);

        $files = $this->filestorage->get_area_files(
            $context->id,
            self::COMPONENT,
            self::FILE_AREA,
            $itemids,
            self::SORT,
            self::INCLUDE_DIRECTORIES
        );

        $filesbyid = array_reduce($posts, function($carry, $post) {
            $carry[$post->get_id()] = [];
            return $carry;
        }, []);

        return array_reduce($files, function($carry, $file) {
            $itemid = $file->get_itemid();
            $carry[$itemid] = array_merge($carry[$itemid], [$file]);
            return $carry;
        }, $filesbyid);
    }