import * as moment from "moment" ; import { graph } from "@pnp/graph" ; import { WebPartContext } from "@microsoft/sp-webpart-base" ; import { ApplicationCustomizerContext } from "@microsoft/sp-application-base" ; import { MSGraphClient , SPHttpClient , SPHttpClientResponse , HttpClient , HttpClientResponse } from "@microsoft/sp-http" ; import { IChat } from "../entities/IChat" ; import { ISubAddResult } from "../entities/ISubAddResult" ; import { IChatMember } from "../entities/IChatMember" ; import * as constants from "../common/constants" ; import { sp } from "@pnp/pnpjs" ; import { SPComponentLoader } from "@microsoft/sp-loader" ; import * as $ from "jquery" ; import * as MicrosoftGraph from "@microsoft/microsoft-graph-types" ; import { IChatMessage } from "../entities/IChatMessage" ; export default class spservices { private static _graphClient : MSGraphClient = null ; private static _currentUser : string = undefined ; private static _context : ApplicationCustomizerContext = undefined ; private static _functionAppUrl : string ; public static init ( context , functionAppUrl ) { this . _context = context ; this . _functionAppUrl = functionAppUrl ; console . log ( "context" , this . _context ) ; sp . setup ( { spfxContext : this . _context } ) ; graph . setup ( { spfxContext : this . _context } ) ; this . _currentUser = this . _context . pageContext . user . email ; } param { string } chatId returns { Promise < IChatMember [ ] > } memberof public static async getChatMembers ( chatId ) { let chatMembers = [ ] ; try { this . _graphClient = await this . _context . msGraphClientFactory . getClient ( ) ; const results = await this . _graphClient . api ( `me/chats/${ chatId }/members` ) . version ( "beta" ) . get ( ) ; return ( chatMembers = results . value ) ; } catch ( error ) { console . log ( error ) ; throw new Error ( `Error Get Chat Members: ${ error . message }` ) ; } return null ; } param { string } URI memberof public static async getChatMessage ( URI ) { try { this . _graphClient = await this . _context . msGraphClientFactory . getClient ( ) ; const chatMessage = await this . _graphClient . api ( `me/${ URI }` ) . version ( "beta" ) . get ( ) ; return chatMessage ; } catch ( error ) { console . log ( error ) ; throw new Error ( `Error Getting Chat Message ${ error . message }` ) ; } return ; } param { string } chatId memberof public static async createMessageSubs ( chatId ) { try { const restUrl = `${ this . _functionAppUrl }/api/CreateMessageSubscription?chatId=${ chatId }` ; const response = await this . _context . httpClient . fetch ( restUrl , HttpClient . configurations . v1 , { method : "GET" } ) ; const results = await response . json ( ) ; return results . subscriptionId ; } catch ( error ) { console . log ( 'error' ) ; throw new Error ( `Error creating Chat Message Subscriptions : ${ error . message }` ) ; } } returns { Promise < IChat [ ] > } memberof public static async getChats ( ) { let chats = [ ] ; let currentDate = moment ( ) . toISOString ( ) ; let startDate = moment ( ) . subtract ( 60 , "days" ) . toISOString ( ) ; try { this . _graphClient = await this . _context . msGraphClientFactory . getClient ( ) ; let returnChats = await this . _graphClient . api ( `me/chats` ) . filter ( `lastUpdatedDateTime ge ${ startDate } and lastUpdatedDateTime le ${ currentDate }` ) . version ( "beta" ) . get ( ) ; chats = returnChats . value ; } catch ( error ) { console . log ( error ) ; throw new Error ( `Error getting users Chats: ${ error . message }` ) ; } return chats ; } public static async getHostedContentImage ( uri ) { return new Promise ( async ( resolve , rejected ) => { try { const accesstoken = await this . _context . aadTokenProviderFactory . getTokenProvider ( ) ; const token = await accesstoken . getToken ( "https://graph.microsoft.com" ) ; const body = await $ . ajax ( { url : uri , method : "GET" , crossDomain : false , headers : { "Content-Type" : "application/json" , Authorization : `Bearer ${ token }` , } , xhrFields : { responseType : "blob" } } ) ; let reader = new FileReader ( ) ; reader . onload = ( ) => { resolve ( reader . result ) ; } ; reader . readAsDataURL ( body ) ; } catch ( error ) { console . log ( error ) ; resolve ( null ) ; } } ) ; } param userId returns public static async getUser ( userId ) { try { const user = await graph . users . getById ( userId ) . get ( ) ; return user ; } catch ( error ) { if ( error . status == '404' ) { console . log ( "(Get User Info) Info: user dont exist in AAD:" , userId ) ; throw new Error ( `(Get User Info) Info: user dont exist in AAD:, ${ userId }` ) ; } else { console . log ( "error get user info" , error ) ; throw new Error ( `error get user info, ${ error . message }` ) ; } } } param userId returns public static async getUserPhoto ( userId ) { const personaImgUrl = constants . PROFILE_IMAGE_URL + userId ; const url = await this . getImageBase64 ( personaImgUrl ) ; const newHash = await this . getMd5HashForUrl ( url ) ; if ( newHash !== constants . DEFAULT_PERSONA_IMG_HASH && newHash !== constants . DEFAULT_IMAGE_PLACEHOLDER_HASH ) { return "data:image/png;base64," + url ; } else { return "undefined" ; } } param url private static getMd5HashForUrl ( url ) { return new Promise ( async ( resolve , reject ) => { const library = await this . loadSPComponentById ( constants . MD5_MODULE_ID ) ; try { const md5Hash = library . Md5Hash ; if ( md5Hash ) { const convertedHash = md5Hash ( url ) ; resolve ( convertedHash ) ; } } catch ( error ) { resolve ( url ) ; } } ) ; } param componentId private static loadSPComponentById ( componentId ) { return new Promise ( ( resolve , reject ) => { SPComponentLoader . loadComponentById ( componentId ) . then ( ( component ) => { resolve ( component ) ; } ) . catch ( error => { console . log ( "Error load component from library" , error ) ; } ) ; } ) ; } param pictureUrl returns private static getImageBase64 ( pictureUrl ) { return new Promise ( ( resolve , reject ) => { let image = new Image ( ) ; image . addEventListener ( "load" , ( ) => { let tempCanvas = document . createElement ( "canvas" ) ; ( tempCanvas . width = image . width ) , ( tempCanvas . height = image . height ) , tempCanvas . getContext ( "2d" ) . drawImage ( image , 0 , 0 ) ; let base64Str ; try { base64Str = tempCanvas . toDataURL ( "image/png" ) ; } catch ( e ) { return "" ; } base64Str = base64Str . replace ( /^data:image\/png;base64,/ , "" ) ; resolve ( base64Str ) ; } ) ; image . src = pictureUrl ; } ) ; } }