<commit_before>//
//  MailController.swift
//  Postcard
//
//  Created by <NAME> on 4/28/16.
//  Copyright © 2016 operator<EMAIL>.<EMAIL>. All rights reserved.
//

import Cocoa
import GoogleAPIClient
import CoreData

class MailController: NSObject
{
    var allPostcards = [GTLGmailMessage]()
    var allPenpals = [PenPal]()
    let appDelegate = NSApplication.sharedApplication().delegate as! AppDelegate
    var managedObjectContext: NSManagedObjectContext?
    
    override init()
    {
        super.init()
        managedObjectContext = appDelegate.managedObjectContext
    }
    
    //This gets a bare list of messages that meet our criteria and then calls a func to retrieve the payload for each one
    func fetchGmailMessagesList()
    {
        let query = GTLQueryGmail.queryForUsersMessagesList()
        query.q = "Subject:Postcard has:attachment"
        
        GmailProps.service.executeQuery(query, completionHandler: {(ticket, response, error) in
            if let listMessagesResponse = response as? GTLGmailListMessagesResponse
            {
                //If there are messages that meet the query criteria in the list, get the message payload from Gmail
                if !listMessagesResponse.messages.isEmpty
                {
                    if let metaMessages = listMessagesResponse.messages as? [GTLGmailMessage]
                    {
                        //Get message payloads
                        self.fetchAndSaveGmailPayloads(metaMessages)
                    }
                }
            }
        })<commit_msg>Fixed failing Optional unwrap when there are no matching mail message<commit_after>//
//  MailController.swift
//  Postcard
//
//  Created by <NAME> on 4/28/16.
//  Copyright © 2016 operator<EMAIL>.<EMAIL>. All rights reserved.
//

import Cocoa
import GoogleAPIClient
import CoreData

class MailController: NSObject
{
    var allPostcards = [GTLGmailMessage]()
    var allPenpals = [PenPal]()
    let appDelegate = NSApplication.sharedApplication().delegate as! AppDelegate
    var managedObjectContext: NSManagedObjectContext?
    
    override init()
    {
        super.init()
        managedObjectContext = appDelegate.managedObjectContext
    }
    
    //This gets a bare list of messages that meet our criteria and then calls a func to retrieve the payload for each one
    func fetchGmailMessagesList()
    {
        let query = GTLQueryGmail.queryForUsersMessagesList()
        query.q = "Subject:Postcard has:attachment"
        
        GmailProps.service.executeQuery(query, completionHandler: {(ticket, response, error) in
            if let listMessagesResponse = response as? GTLGmailListMessagesResponse
            {
                //If there are messages that meet the query criteria in the list, get the message payload from Gmail
                if let metaMessages = listMessagesResponse.messages as? [GTLGmailMessage]
                {
                    //Get message payloads
                    self.fetchAndSaveGmailPayloads(metaMessages)
                }
            }
        })
{'fraction_non_alphanumeric': 0.07054616384915474, 'fraction_numerical': 0.005851755526657998, 'mean_word_length': 2.2838847385272145, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 9, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6809547', 'n_tokens_mistral': 802, 'n_tokens_neox': 764, 'n_words': 240}