import loadList from '../utils/new-graphql-load-list' ; const processNotificationList = ( list , { dispatch , getState } ) => { list . map ( item => { if ( item . comment_id ) { var text = item . comment_id . content_html ; text = text . replace ( /<[^>]+>/g , "" ) ; if ( text . length > 100 ) text = text . substring ( 0 , 100 ) + '...' ; item . comment_id . content_trim = text ; } if ( item . comment_id && item . comment_id . parent_id ) { var text = item . comment_id . parent_id . content_html ; text = text . replace ( /<[^>]+>/g , "" ) ; if ( text . length > 100 ) text = text . substring ( 0 , 100 ) + '...' ; item . comment_id . parent_id . content_trim = text ; } if ( item . comment_id && item . comment_id . reply_id ) { var text = item . comment_id . reply_id . content_html ; text = text . replace ( /<[^>]+>/g , "" ) ; if ( text . length > 100 ) text = text . substring ( 0 , 100 ) + '...' ; item . comment_id . reply_id . content_trim = text ; } if ( item . comment_id && item . comment_id . answer_id ) { var text = item . comment_id . answer_id . content_html ; text = text . replace ( /<[^>]+>/g , "" ) ; if ( text . length > 100 ) text = text . substring ( 0 , 100 ) + '...' ; item . comment_id . answer_id . content_html = text ; } return item ; } ) ; updateData ( list , { dispatch , getState } ) ; return list ; } ; const updateData = function ( list , { dispatch , getState } ) { const state = getState ( ) ; let unreadNotice = state . website . unreadNotice , comment = state . comment , posts = state . posts , followPeople = state . follow , me = state . user . userInfo ; comment = updateCommentState ( comment , list ) ; posts = updatePosts ( posts , list ) ; followPeople = updateFollowPeople ( followPeople , me . _id , list ) ; list . map ( item => { let _index = unreadNotice . indexOf ( item . _id ) ; if ( _index != - 1 ) unreadNotice . splice ( _index , 1 ) ; } ) ; if ( followPeople . count > 0 ) { me . fans_count = me . fans_count + followPeople . count ; dispatch ( { type : 'SET_USER' , userinfo : me } ) ; dispatch ( { type : 'SET_FOLLOW_PEOPLE' , state : followPeople . state } ) ; } dispatch ( { type : 'SET_POSTS' , state : posts } ) ; dispatch ( { type : 'SET_COMMENT' , state : comment } ) ; dispatch ( { type : 'SET_UNREAD_NOTICE' , unreadNotice } ) ; } ; export const loadNotifications = loadList ( { reducerName : 'notification' , actionType : 'SET_NOTIFICATION_LIST_BY_ID' , api : 'userNotifications' , fields : `
    has_read
    deleted
    create_at
    _id
    type
    comment_id {
      _id
      content_html
      posts_id {
        _id
        title
        content_html
      }
      reply_id {
        _id
        content_html
      }
      parent_id {
        _id
        content_html
      }
    }
    sender_id {
      create_at
      avatar
      _id
      nickname
      avatar_url
      id
    }
    addressee_id {
      create_at
      avatar
      _id
      nickname
      avatar_url
      id
    }
    posts_id {
      title
      content_html
      _id
    }
  ` , processList : processNotificationList } ) ; let updateCommentState = ( comment , notices ) => { notices . map ( ( item ) => { if ( item . has_read ) return ; if ( item . type == 'comment' || item . type == 'like-comment' || item . type == 'new-comment' ) { let posts_id = item . comment_id . posts_id . _id ; if ( comment [ posts_id ] ) delete comment [ posts_id ] ; } else if ( item . type == 'reply' || item . type == 'like-reply' ) { let posts_id = item . comment_id . posts_id . _id ; let parent_id = item . comment_id . parent_id . _id ; if ( comment [ posts_id ] ) delete comment [ posts_id ] ; if ( comment [ parent_id ] ) delete comment [ parent_id ] ; } } ) ; return comment ; } ; const updatePosts = ( state , notices ) => { notices . map ( item => { if ( item . has_read ) return ; if ( item . type == 'follow-posts' ) { for ( let i in state ) { let data = state [ i ] . data ; if ( data . length > 0 ) { for ( let n = 0 , max = data . length ; n < max ; n ++ ) { if ( data [ n ] . _id == item . posts_id . _id ) { state [ i ] . data [ n ] . follow_count += 1 ; } } } } } } ) ; return state ; } ; const updateFollowPeople = ( state , selfId , notices ) => { let count = 0 ; notices . map ( item => { if ( item . has_read ) return ; if ( item . type == 'follow-you' ) { count += 1 ; if ( state [ 'fans-' + selfId ] ) delete state [ 'fans-' + selfId ] ; } } ) ; return { state : state , count : count } ; } ; export const loadNewNotifications = ( { name } ) => { return async ( dispatch , getState ) => { const state = getState ( ) ; let list = state . notification [ name ] || null , unreadNotice = state . website . unreadNotice ; if ( unreadNotice . length == 0 || ! list || ! list . data ) { return ; } let err , res ; let result = await loadNotifications ( { id : 'new' , args : { page_size : 20 , _id : unreadNotice . join ( ',' ) , sort_by : 'create_at:1' } , restart : true } ) ( dispatch , getState ) ; [ err , res ] = result ; res . data . map ( ( item ) => { list . data . unshift ( item ) ; } ) ; dispatch ( { type : 'SET_NOTIFICATION_LIST_BY_ID' , name , data : list } ) ; updateData ( list . data , { dispatch , getState } ) ; } ; } ;