fetch_search_results: function(options){
                var that = this;
                // Return attachments that meet search criteria

                // Doesn't do anything complex yet, just a regex search
                // - todo: links from contact, etc.

                // options.text

                var text = options.text;

                // escape regex characters
                var tmp_text = text.replace(/[-\/\\^$*+?.()|[\]{}]/g, '\\$&')

                Api.search({
                    data: {
                        model: 'Email',
                        conditions: {
                            'original.attachments.name' : {
                                '$regex' : '(' + tmp_text + ')',
                                '$options' : 'i'
                            }
                        },
                        fields: ['original.attachments','attributes.thread_id','common.date','original.headers.Subject'],
                        limit: 10,
                        sort: {"_id" : -1}
                    },
                    success: function(response, code, data, msg){
                        // response = JSON.parse(response);

                        // Parse out the attachments for each email
                        var attachments = [];
                        $.each(response.data,function(i,e){
                            $.each(e.Email.original.attachments,function(k,attachment){
                                // test the regex again
                                var tmp = new RegExp('(' + tmp_text + ')', 'i');
                                if(tmp.test(attachment.name)){
                                    attachments.push({
                                        id: e.Email._id + '-' + k.toString(),
                                        attachment: attachment,
                                        thread_id: e.Email.attributes.thread_id,
                                        email_id: e.Email._id,
                                        date: e.Email.common.date,
                                        subject: e.Email.original.headers.Subject
                                    });
                                }

                            });
                        });

                        // Call success
                        that.reset(attachments);

                        if(options.success) options.success(attachments);

                    }
                });

            }
Пример #2
0
            fetch_search_results: function(options){
                // Conducts a search against Threads and Emails, and returns a collection of related Thread and Emails
                var that = this;

                var text = options.text;

                var deferredSearches = [];

                var and_fields = {}; // todo...

                // escape regex characters
                // text = text.replace(/[#-}]/g, '\\$&'); // escape regex characters from search string
                text = text.replace(/[-\/\\^$*+?.()|[\]{}]/g, '\\$&')

                // Email searching::

                // Normal Email fields to search for text
                var email_filter_fields = [
                    'app.AppPkgDevMinimail.note', // only on Thread level? (no, on Email level too...)
                    'original.ParsedData.0.Data',
                    // 'original.HtmlBody',
                    'original.HtmlTextSearchable', // - strip html (for searching HTML views)
                    'original.headers.Subject',
                    'original.headers.From',
                    'original.headers.To',
                    'original.headers.Reply-To',
                    'original.attachments.name' // array
                ];
                // Normal Email fields to search for text
                var thread_filter_fields = [
                    'app.AppPkgDevMinimail.note'
                ];

                var tmp_email_filter_fields = [];
                $.each(email_filter_fields,function(k,val){
                    var d = {};
                    d[val] = {
                        "$regex" : '(' + text + ')',
                        "$options" : 'i'
                    };
                    tmp_email_filter_fields.push(d);
                });

                var email_search_conditions = {
                    "$or" : tmp_email_filter_fields
                };

                deferredSearches.push( Api.search({
                    data: {
                        model: 'Email',
                        conditions: email_search_conditions,
                        fields : ['_id','attributes.thread_id','_modified'],
                        limit : 50,
                        sort: {"_id" : -1} // most recently received (better way of sorting?)
                    },
                    success: function(response){
                        // responded OK
                        console.log('FINISHED SEARCHING EMAILS');
                    }
                }) );


                // Thread Searching::

                // Normal Thread fields to search for text
                var thread_filter_fields = [
                    'app.AppPkgDevMinimail.note'
                ];
                // Normal Thread fields to search for text
                var thread_filter_fields = [
                    'app.AppPkgDevMinimail.note'
                ];

                var tmp_thread_filter_fields = [];
                $.each(thread_filter_fields,function(k,val){
                    var d = {};
                    d[val] = {
                        "$regex" : '(' + text + ')',
                        "$options" : 'i'
                    };
                    tmp_thread_filter_fields.push(d);
                });

                var thread_search_conditions = {
                    "$or" : tmp_thread_filter_fields
                };

                deferredSearches.push( Api.search({
                    data: {
                        model: 'Thread',
                        conditions: thread_search_conditions,
                        fields : ['_id','_modified'],
                        limit : 50,
                        sort: {"_id" : -1} // most recently received (better way of sorting?)
                    },
                    success: function(response){
                        // responded OK
                        // console.log('FINISHED SEARCHING EMAILS');
                    }
                }) );

                $.when.apply(this, deferredSearches).then(function(emailResponse, threadResponse){ // expecting two arguments
                    // Completed all our preliminary searches
                    // - now actually gathering the Threads and Emails

                    var thread_ids = [];

                    // // arguments passed in
                    // _.each(arguments, function(response, code, data){
                    //     // Each returned argument is actually a response object
                    //     if(response.code != 200){
                    //         // Shoot
                    //         return;
                    //     }

                    //     thread_ids.push( _.map(response.data, function(){
                    //         return email.
                    //     }) );

                    // });

                    _.each(emailResponse.data, function(email){
                        thread_ids.push(email.Email.attributes.thread_id);
                    });

                    _.each(threadResponse.data, function(thread){
                        thread_ids.push(thread.Thread._id);
                    });


                    // remove duplicates
                    thread_ids = _.uniq(thread_ids);

                    // Conduct collection fetch (and fetchRelated)
                    that.search_conditions = {
                        _id: {
                            '$in' : thread_ids
                        }
                    };
                    that.search_limit = 10;

                    // actually run the .fetch
                    console.log(that.search_conditions);
                    // debugger;
                    that.fetch(options.options);

                });

            }
 albumSearch: function(artist, title, release) {
     console.log("collection.albumSearch");
     Api.search(artist, title, release, collection.startPollSearchResponse);
 },