import cache from '../cache' ; import { name , domainName , AMP , favicon , googleAdSense } from '@config' ; import utils from '@app/redux/utils/html' ; import graphql from '@app/redux/utils/graphql' ; import { dateDiff } from '@app/common/date' ; const { abstractImagesFromHTML } = utils ; export const show = async ( req , res ) => { const { id } = req . params ; let [ err , comment ] = await graphql ( { apis : [ { api : 'comments' , args : { _id : id , deleted : false , weaken : false } , fields : `
        posts_id{
          _id
          title
          content_html
        }
        content_html
        create_at
        reply_count
        like_count
        _id
        user_id {
          _id
          nickname
          avatar_url
        }
      ` } ] } ) ; if ( comment && comment [ 0 ] ) { comment = comment [ 0 ] ; } else { res . status ( 404 ) ; res . send ( '404 Not Found' ) ; return ; } comment = JSON . parse ( JSON . stringify ( comment ) ) ; comment . images = abstractImagesFromHTML ( comment . content_html ) ; if ( comment . images && comment . images . length == 0 ) { comment . images = [ domainName + "/512x512.png" ] ; } comment . description = comment . content_html || '' ; comment . description = comment . description . replace ( /<[^>]+>/g , '' ) ; comment . description = comment . description . replace ( /\r\n/g , '' ) ; comment . description = comment . description . replace ( /\n/g , '' ) ; comment . description = comment . description . replace ( /(\\|\"|\n|\r)/g , "\\$1" ) ; if ( comment . description . length > 100 ) comment . description = comment . description . slice ( 0 , 100 ) + '...' ; comment . description = `${ comment . user_id . nickname }的评论: ${ comment . description }` ; comment . posts_id . content_html = comment . posts_id . content_html . replace ( /<[^>]+>/g , '' ) ; comment . posts_id . content_html = comment . posts_id . content_html . replace ( /\r\n/g , '' ) ; comment . posts_id . content_html = comment . posts_id . content_html . replace ( /\n/g , '' ) ; comment . posts_id . content_html = comment . posts_id . content_html . replace ( /(\\|\"|\n|\r)/g , "\\$1" ) ; if ( comment . posts_id . content_html . length > 100 ) comment . posts_id . content_html = comment . posts_id . content_html . slice ( 0 , 100 ) + '...' ; let arr = comment . content_html . match ( /<img.*?(?:>|\/>)/gi ) ; if ( arr && arr . length > 0 ) { arr . map ( ( item ) => { let arr = item . match ( /src=[\'\"]?([^\'\"]*)[\'\"]?/i ) ; if ( arr && arr [ 1 ] ) { comment . content_html = comment . content_html . replace ( item , `<amp-img width="16" height="9" layout="responsive" src="${ arr [ 1 ] }"></amp-img>` ) ; } } ) ; } comment . content_html = comment . content_html . replace ( /style\s*=(['\"\s]?)[^'\"]*?\1/gi , '' ) ; comment . content_html = comment . content_html . replace ( /style/gi , '' ) ; comment . _create_at = dateDiff ( comment . create_at ) ; let [ error , commentList ] = await graphql ( { apis : [ { api : 'comments' , args : { parent_id : comment . _id , page_size : 50 , weaken : false } , fields : `
        content_html
        create_at
        reply_count
        like_count
        _id
        user_id {
          _id
          nickname
          avatar_url
        }
      ` } ] } ) ; commentList = JSON . parse ( JSON . stringify ( commentList ) ) ; if ( commentList . length > 0 ) { commentList . map ( ( comment ) => { comment . _create_at = dateDiff ( comment . create_at ) ; if ( comment . content_html ) { let arr = comment . content_html . match ( /<img.*?(?:>|\/>)/gi ) ; if ( arr && arr . length > 0 ) { arr . map ( ( item ) => { let arr = item . match ( /src=[\'\"]?([^\'\"]*)[\'\"]?/i ) ; if ( arr && arr [ 1 ] ) { comment . content_html = comment . content_html . replace ( item , `<amp-img width="16" height="9" layout="responsive" src="${ arr [ 1 ] }"></amp-img>` ) ; } else { comment . content_html = comment . content_html . replace ( item , `` ) ; } } ) ; } } } ) ; } res . render ( '../dist/server/views/pages/comment-detail.ejs' , { AMP , website_name : name , domainName , comment , commentList , favicon , googleAdSense } , function ( err , html ) { cache . set ( req . originalUrl , html ) ; res . send ( html ) ; } ) ; } ;