{"size": 2308, "ext": "js", "max_stars_repo_path": "backend/js/posting/services/TwitterSocialPostingService.js", "max_stars_repo_name": "jjak0b/vue-tweet-tracker", "max_stars_repo_stars_event_min_datetime": null, "max_stars_repo_stars_event_max_datetime": null, "max_issues_repo_path": "backend/js/posting/services/TwitterSocialPostingService.js", "max_issues_repo_name": "jjak0b/vue-tweet-tracker", "max_issues_repo_issues_event_min_datetime": null, "max_issues_repo_issues_event_max_datetime": null, "max_forks_repo_path": "backend/js/posting/services/TwitterSocialPostingService.js", "max_forks_repo_name": "jjak0b/vue-tweet-tracker", "max_forks_repo_forks_event_min_datetime": "2020-12-19T13:44:46.000Z", "max_forks_repo_forks_event_max_datetime": "2020-12-19T13:44:46.000Z"}
{"max_stars_count": null, "max_issues_count": null, "max_forks_count": 1, "avg_line_length": 28.1463414634, "max_line_length": 138, "alphanum_fraction": 0.5017331023}
const ISocialPostingService = require("./ISocialPostingService");

const uploadClient = require("../../clients/1.1").getUserContextClient("upload");
const postClient = require("../../clients/1.1").getUserContextClient("api");

class TwitterSocialPostingService extends ISocialPostingService{
    constructor() {
        super();
        this.client = {
            uploading:  uploadClient,
            posting: postClient
        }
    }


    async addPublicPost(text, media) {

        let mediaIds = [];
        for (let i = 0; i < media.length; i++) {
            let mediaInfo = media[ i ];
            try {
                let media_id = await this.uploadMedia(mediaInfo);
                mediaIds.push( media_id );
            }
            catch (e) {
                console.error( `[${this.constructor.name}]`, "Error uploading media", mediaInfo.mime, mediaInfo.metaInfo, "cause:\n", e );
            }
        }

        try {
            return await this.client.posting.post(
                "/statuses/update",
                {
                    status: text,
                    media_ids: mediaIds
                }
            )
        }
        catch (e) {
            console.error( `[${this.constructor.name}]`, "Error posting status", text, "cause:\n", e );
            return e
        }


    }

    /**
     *
     * @param media {MediaInfo}
     *
     */
    async uploadMedia( media) {
        let mediaCategory = null;
        if( media.mime.includes("image") ) {
            if( media.mime.includes("gif") ) {
                mediaCategory = "TweetGif";
            }
            else {
                mediaCategory = "TweetImage";
            }
        }
        else if( media.mime.includes("video") ) {
            mediaCategory = "TweetVideo";
        }
        else {
            console.warn( `[${this.constructor.name}]`, "Warning: attempting to upload unhandled media", media.mime );
        }

        let response = await this.client.uploading.post(
            "media/upload",
            {
                media_data: media.data.toString("base64"),
                media_type: media.mime,
                media_category: mediaCategory
            }
        )

        return response[ "media_id_string" ];
    }
}

module.exports = TwitterSocialPostingService;