select( [ 'id', 'author_id' ] ) ->where( 'id', '=', $note->id ) ->when( $note->is_thread(), function ( Note_Query_Builder $q ) use ( $note ) { $q->or_where( 'parent_id', '=', $note->id ); } ) ->when( $note->is_reply(), function ( Note_Query_Builder $q ) use ( $note ) { $q->or_where( 'parent_id', '=', $note->parent_id ) ->or_where( 'id', '=', $note->parent_id ); } ) ->get(); return $this->where_exists( function ( Query_Builder $q ) use ( $notes ) { // User is mentioned in thread or in one of the replies. $q->select_raw( [ 1 ] ) ->table( Module::TABLE_NOTES_USERS_RELATIONS, 'relations' ) ->where_in( 'relations.note_id', $notes->pluck( 'id' )->all() ) ->where_column( 'relations.user_id', '=', 'users.id' ) ->where( 'relations.type', '=', Note::USER_RELATION_MENTION ); } ) // User created the thread or one of the replies. ->or_where_in( 'users.id', $notes->pluck( 'author_id' )->all() ); } }