import storage from '../../common/storage' ; import { dateDiff } from '../../common/date' ; import Device from '../../common/device' ; import graphql from '../utils/graphql' ; import To from '../../common/to' ; import loadList from '../utils/new-graphql-load-list' ; const processPostsList = ( list , store ? , id ? ) => { list . map ( function ( posts ) { if ( posts . device ) { posts . _device = Device . getNameByDeviceId ( posts . device ) ; } if ( posts . content_html && posts . content_html == '<p><br></p>' ) { posts . content_html = '' ; } if ( posts . content_html ) { posts . images = abstractImages ( posts . content_html ) ; if ( posts . images && posts . images . length > 0 ) { posts . _coverImage = posts . images [ 0 ] . split ( '?' ) [ 0 ] + '?imageView2/2/w/300/auto-orient/format/jpg' ; } let textContent = posts . content_html ; let imgReg = /<img(.*?)>/gi ; let imgs = [ ] ; let img ; while ( img = imgReg . exec ( textContent ) ) { imgs . push ( img [ 0 ] ) ; } imgs . map ( item => { textContent = textContent . replace ( item , '[图片] ' ) ; } ) ; textContent = textContent . replace ( /<[^>]+>/g , '' ) ; textContent = textContent . replace ( /\r\n/g , '' ) ; textContent = textContent . replace ( /\n/g , '' ) ; if ( textContent . length > 137 ) textContent = textContent . slice ( 0 , 137 ) + '...' ; posts . content_summary = textContent ; posts . content_html = imageOptimization ( posts . content_html ) ; } if ( posts . create_at ) posts . _create_at = dateDiff ( posts . create_at ) ; if ( posts . sort_by_date ) posts . _sort_by_date = dateDiff ( posts . sort_by_date ) ; if ( posts . last_comment_at ) posts . _last_comment_at = dateDiff ( posts . last_comment_at ) ; if ( posts . update_at ) posts . _update_at = dateDiff ( posts . update_at ) ; } ) ; return list ; } ; interface AddPosts { title : string ; contentHTML : string ; topicId : string ; device : number ; type : number ; } export function addPosts ( { title , contentHTML , topicId , device , type } ) { return ( dispatch , getState ) => { return new Promise ( async ( resolve ) => { let [ err , res ] = await graphql ( { type : 'mutation' , apis : [ { api : 'addPosts' , args : { title : encodeURIComponent ( title ) , content_html : encodeURIComponent ( contentHTML ) , topic_id : topicId , device_id : device , type } , fields : `
        success
        _id
        ` } ] , headers : { accessToken : getState ( ) . user . accessToken } } ) ; resolve ( [ err , res ] ) ; } ) ; } ; } export const loadPostsList = loadList ( { reducerName : 'posts' , actionType : 'SET_POSTS_LIST_BY_ID' , processList : processPostsList , api : 'posts' , fields : `
  _id
  comment_count
  reply_count
  content_html
  create_at
  deleted
  device
  follow_count
  last_comment_at
  like_count
  recommend
  sort_by_date
  title
  ip
  topic_id{
    _id
    name
    parent_id {
      _id
      name
    }
  }
  type
  user_id{
    _id
    nickname
    brief
    avatar_url
    posts_count
    comment_count
    fans_count
    follow_people_count
    follow
    user_cover
  }
  verify
  view_count
  weaken
  follow
  like
  update_at
  ` } ) ; export const removePostsListById = ( id ) => { return ( dispatch , getState ) => { dispatch ( { type : 'REMOVE_POSTS_LIST_BY_ID' , id } ) ; } ; } ; export const refreshPostsListById = ( id ) => { return ( dispatch , getState ) => { let list = getState ( ) . posts [ id ] || null ; if ( ! list || ! list . filters ) return ; delete list . filters . page_size ; delete list . filters . page_number ; return loadPostsList ( { id , args : list . filters , restart : true } ) ( dispatch , getState ) . then ( ( [ err , res ] ) => { if ( res && res . data && res . data . length > 0 ) { let postList = { } ; res . data . map ( ( item ) => { postList [ item . _id ] = item ; } ) ; let postsState = getState ( ) . posts ; Reflect . ownKeys ( postsState ) . map ( item => { getState ( ) . posts [ item ] . data . map ( ( item ) => { if ( postList [ item . _id ] ) { if ( postList [ item . _id ] . comment_count ) item . comment_count = postList [ item . _id ] . comment_count ; if ( postList [ item . _id ] . reply_count ) item . reply_count = postList [ item . _id ] . reply_count ; if ( postList [ item . _id ] . follow_count ) item . follow_count = postList [ item . _id ] . follow_count ; if ( postList [ item . _id ] . like_count ) item . like_count = postList [ item . _id ] . like_count ; } } ) ; } ) ; dispatch ( { type : 'SET_POSTS' , state : postsState } ) ; } } ) ; } ; } ; export function viewPostsById ( { id } ) { return async ( dispatch , getState ) => { let [ err , res ] = await To ( storage . load ( { key : 'view-posts' } ) ) ; let viewPosts = res || '' ; viewPosts = viewPosts . split ( ',' ) ; if ( ! viewPosts [ 0 ] ) viewPosts = [ ] ; if ( viewPosts . indexOf ( id ) == - 1 ) { viewPosts . push ( id ) ; storage . save ( { key : 'view-posts' , data : viewPosts . join ( ',' ) , expires : 1000 * 3600 , } ) ; let [ err , res ] = await graphql ( { type : 'mutation' , apis : [ { api : 'viewPosts' , args : { posts_id : id } , fields : `success` } ] } ) ; if ( res && res . success ) { dispatch ( { type : 'UPDATE_POSTS_VIEW' , id : id } ) ; } } } ; } interface UpdatePosts { id : string ; title : string ; contentHTML : string ; topicId : string ; } export function updatePosts ( { id , title , contentHTML , topicId } ) { return async ( dispatch , getState ) => { return new Promise ( async ( resolve , reject ) => { let args = { _id : id , title , content_html : encodeURIComponent ( contentHTML ) , topic_id : topicId } ; let [ err , res ] = await graphql ( { type : 'mutation' , apis : [ { api : 'updatePosts' , args : JSON . parse ( JSON . stringify ( args ) ) , fields : `success` } ] , headers : { accessToken : getState ( ) . user . accessToken } } ) ; if ( err ) { return reject ( err ) ; } loadPostsList ( { id , args : { _id : id } , restart : true } ) ( dispatch , getState ) . then ( ( [ err , res ] ) => { let posts = res && res . data && res . data [ 0 ] ? res . data [ 0 ] : null ; if ( ! posts ) { resolve ( res ) ; } else { dispatch ( { type : 'UPDATE_POST' , id : id , update : posts } ) ; } resolve ( res ) ; } ) ; } ) ; } ; } const abstractImages = ( str ) => { let imgReg = /<img(?:(?:".*?")|(?:'.*?')|(?:[^>]*?))*>/gi ; let srcReg = /src=[\'\"]?([^\'\"]*)[\'\"]?/i ; let result = [ ] ; let img ; while ( img = imgReg . exec ( str ) ) { let _img = img [ 0 ] . match ( srcReg ) ; if ( _img && _img [ 1 ] ) { _img = _img [ 1 ] ; if ( _img [ 1 ] . indexOf ( 'xiaoduyu.com' ) != - 1 ) { _img = _img [ 1 ] + '?imageView2/2/w/800/auto-orient/format/jpg' ; } result . push ( _img ) ; } } return result ; } ; const imageOptimization = ( str ) => { let imgReg = /<img(?:(?:".*?")|(?:'.*?')|(?:[^>]*?))*>/gi ; let srcReg = /src=[\'\"]?([^\'\"]*)[\'\"]?/i ; let result = [ ] ; let img ; while ( img = imgReg . exec ( str ) ) { let oldImgDom = img [ 0 ] ; if ( oldImgDom ) { let _img = oldImgDom . match ( srcReg ) ; if ( _img && _img [ 1 ] && _img [ 1 ] . indexOf ( 'xiaoduyu.com' ) != - 1 ) { let newImg = oldImgDom . replace ( _img [ 1 ] , _img [ 1 ] + '?imageView2/2/w/680/auto-orient/format/jpg' ) ; str = str . replace ( oldImgDom , newImg ) ; } } } return str ; } ;