All files / plugins Plantnet.js

73.84% Statements 96/130
50% Branches 8/16
71.42% Functions 5/7
73.84% Lines 96/130

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 1311x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x     1x 1x 1x 3x 3x 1x 1x 3x 3x 1x 1x     1x 1x     1x 1x 3x 3x 3x 3x 3x 3x 3x 3x     3x 3x     3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 2x 2x 2x 2x 2x 3x 1x 1x 1x 1x               3x     3x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 3x 3x 3x 3x 3x 3x                               3x 1x 1x  
import {arrayIsNotEmpty, clone, isSet, loadJsonResource} from "../lib/Common.js";
import {firstImageOf} from "../domain/post.js";
import {IDENTIFY_RESULT} from "../servicesExternal/PlantnetApiService.js";
import {dataSimulationDirectory} from "../services/BotService.js";
 
export default class Plantnet {
    constructor(config, loggerService, blueskyService, plantnetCommonService, plantnetApiService) {
        this.isAvailable = false;
        this.logger = loggerService.getLogger().child({label: 'Pl@ntNet'});
        this.blueskyService = blueskyService;
        this.plantnetSimulate = (config.bot.plantnetSimulate === true);
        this.plantnetCommonService = plantnetCommonService;
        this.plantnetApiService = plantnetApiService;
        try {
            this.questions = loadJsonResource('src/data/questionsPlantnet.json');
            this.isAvailable = plantnetApiService.isReady();
            this.logger.info((this.isAvailable ? "available" : "not available") +
                " with " + this.questions.length + " questions");
        } catch (exception) {
            plantnetCommonService.logError("init", exception);
        }
    }
 
    getName() {
        return "Plantnet";
    }
 
    getPluginTags() {
        return ["#BeSPlantnet", "#IndentificationDePlantes"].join(' ');
    }
 
    getQuestions() {
        return clone(this.questions);
    }
 
    isReady() {
        return this.isAvailable;
    }
 
    async process(config) {
        const pluginName = this.getName();
        const {plantnetSimulate, plantnetCommonService, plantnetApiService, logger} = this;
        let {doSimulate, simulateIdentifyCase, context} = config;
        const doSimulateIdentify = plantnetSimulate || isSet(simulateIdentifyCase);// if at least one want to simulate then simulate
        let candidate = null;
        try {
            candidate = await this.searchNextCandidate(config);
            if (candidate === null) {
                return plantnetCommonService.resultNoCandidate(pluginName, context);
            }
            const candidatePhoto = firstImageOf(candidate);
            if (!candidatePhoto) {
                return plantnetCommonService.rejectNoCandidateImage(pluginName, candidate, context);
            }
            plantnetCommonService.logCandidate(pluginName, candidate, candidatePhoto, context);
 
            const tags = this.getPluginTags();
            const {
                result,
                plantnetResult = null
            } = await plantnetApiService.plantnetIdentify({
                "imageUrl": candidatePhoto?.fullsize,
                doSimulate,
                doSimulateIdentify,
                simulateIdentifyCase,
                candidate,
                tags,
                context
            });
            if (result === IDENTIFY_RESULT.OK) {
                const {scoredResult, firstImageOriginalUrl, firstImageText} = plantnetResult;
                return await plantnetCommonService.replyToWithIdentificationResult(candidate,
                    {tags, doSimulate, context},
                    {scoredResult, firstImageOriginalUrl, firstImageText}
                );
            } else if (result === IDENTIFY_RESULT.BAD_SCORE) {
                return await plantnetCommonService.handleWithoutScoredResult(
                    {doSimulate, candidate, "replyTo": null, "muteAuthor": true, context}
                );
            } else {
                if (result !== IDENTIFY_RESULT.NONE) {
                    logger.warn(`unable to handle plantnetService.plantnetIdentify result:${result} so consider it as NONE`);
                }
                return await plantnetCommonService.handleWithNoIdentificationResult(
                    {doSimulate, candidate, "replyTo": null, "muteAuthor": true, context}
                );
            }
        } catch (err) {
            return plantnetCommonService.rejectWithIdentifyError(pluginName, candidate, err, context);
        }
    }
 
    /**
     * iterate through plugin.questions to search next candidate
     *
     * Bluesky advanced search may improve this stage :
     * https://github.com/bluesky-social/social-app/issues/3378 (parent of advanced search)
     * https://github.com/bluesky-social/social-app/issues/1522 (Use Boolean search operators AND and OR)
     * https://github.com/bluesky-social/social-app/issues/4093 (ability to know supported keyword/search logic/tips)
     * https://github.com/bluesky-social/social-app/issues/4094 (support has:images to filter only post having image)
     *
     * @param config
     * @param bookmark
     * @returns {Promise<unknown>}
     */
    async searchNextCandidate(config, bookmark = 0) {
        const {questions, blueskyService, logger} = this;
        const {context, doSimulateSearch} = config;
        if (doSimulateSearch) {
            await blueskyService.login();
            return Promise.resolve(loadJsonResource(`${dataSimulationDirectory}/blueskyPostFakeFlower.json`));
        }
        const candidatePosts = await blueskyService.searchPosts({
            searchQuery: questions[bookmark],
            "hasImages": true,
            "hasNoReply": true,
            "isNotMuted": true,
            "maxHoursOld": 24// now-24h ... now
        })
        logger.info(`${candidatePosts.length} candidate(s)`, context);
        if (arrayIsNotEmpty(candidatePosts)) {
            return Promise.resolve(candidatePosts[0]);
        }
        if (bookmark + 1 < questions.length) {
            return await this.searchNextCandidate(config, bookmark + 1)
        }
        return Promise.resolve(null);
    }
 
}