From 409704fe8f5506210f37eaa5e1cd1afaca54a225 Mon Sep 17 00:00:00 2001 From: Evan Morikawa Date: Wed, 4 May 2016 16:23:19 -0700 Subject: [PATCH] feat(babel6): Convert account.coffee to account.es6 --- spec/models/contact-spec.coffee | 2 +- spec/models/query-spec.coffee | 2 +- spec/nylas-api-spec.coffee | 2 +- spec/stores/account-store-spec.coffee | 2 +- src/flux/models/account.coffee | 161 ---------------------- src/flux/models/account.es6 | 188 ++++++++++++++++++++++++++ src/flux/nylas-api.coffee | 4 +- src/flux/stores/account-store.coffee | 4 +- src/flux/stores/category-store.coffee | 2 +- 9 files changed, 197 insertions(+), 170 deletions(-) delete mode 100644 src/flux/models/account.coffee create mode 100644 src/flux/models/account.es6 diff --git a/spec/models/contact-spec.coffee b/spec/models/contact-spec.coffee index c6a383def..1d17554e2 100644 --- a/spec/models/contact-spec.coffee +++ b/spec/models/contact-spec.coffee @@ -1,6 +1,6 @@ Contact = require "../../src/flux/models/contact" AccountStore = require "../../src/flux/stores/account-store" -Account = require "../../src/flux/models/account" +Account = require("../../src/flux/models/account").default contact_1 = name: "Evan Morikawa" diff --git a/spec/models/query-spec.coffee b/spec/models/query-spec.coffee index 376699496..2e397b7ba 100644 --- a/spec/models/query-spec.coffee +++ b/spec/models/query-spec.coffee @@ -2,7 +2,7 @@ ModelQuery = require '../../src/flux/models/query' {Matcher} = require '../../src/flux/attributes' Message = require '../../src/flux/models/message' Thread = require('../../src/flux/models/thread').default -Account = require '../../src/flux/models/account' +Account = require('../../src/flux/models/account').default describe "ModelQuery", -> beforeEach -> diff --git a/spec/nylas-api-spec.coffee b/spec/nylas-api-spec.coffee index ae2901a82..8ddaae3a2 100644 --- a/spec/nylas-api-spec.coffee +++ b/spec/nylas-api-spec.coffee @@ -306,7 +306,7 @@ describe "NylasAPI", -> "folder": require('../src/flux/models/folder') "thread": require('../src/flux/models/thread').default "draft": require('../src/flux/models/message') - "account": require('../src/flux/models/account') + "account": require('../src/flux/models/account').default "message": require('../src/flux/models/message') "contact": require('../src/flux/models/contact') "calendar": require('../src/flux/models/calendar') diff --git a/spec/stores/account-store-spec.coffee b/spec/stores/account-store-spec.coffee index bf7ad8c85..b120078ba 100644 --- a/spec/stores/account-store-spec.coffee +++ b/spec/stores/account-store-spec.coffee @@ -1,7 +1,7 @@ _ = require 'underscore' keytar = require 'keytar' AccountStore = require '../../src/flux/stores/account-store' -Account = require '../../src/flux/models/account' +Account = require('../../src/flux/models/account').default Actions = require '../../src/flux/actions' diff --git a/src/flux/models/account.coffee b/src/flux/models/account.coffee deleted file mode 100644 index 935cd916e..000000000 --- a/src/flux/models/account.coffee +++ /dev/null @@ -1,161 +0,0 @@ -ModelWithMetadata = require('./model-with-metadata').default -Attributes = require '../attributes' -_ = require 'underscore' -CategoryStore = null -Contact = null - -### -Public: The Account model represents a Account served by the Nylas Platform API. -Every object on the Nylas platform exists within a Account, which typically represents -an email account. - -For more information about Accounts on the Nylas Platform, read the -[Account API Documentation](https://nylas.com/docs/api#Account) - -## Attributes - -`name`: {AttributeString} The name of the Account. - -`provider`: {AttributeString} The Account's mail provider (ie: `gmail`) - -`emailAddress`: {AttributeString} The Account's email address -(ie: `ben@nylas.com`). Queryable. - -`organizationUnit`: {AttributeString} Either "label" or "folder". -Depending on the provider, the account may be organized by folders or -labels. - -This class also inherits attributes from {Model} - -Section: Models -### -class Account extends ModelWithMetadata - - @SYNC_STATE_RUNNING = "running" - @SYNC_STATE_STOPPED = "stopped" - @SYNC_STATE_AUTH_FAILED = "invalid" - @SYNC_STATE_ERROR = "sync_error" - - @attributes: _.extend {}, ModelWithMetadata.attributes, - 'name': Attributes.String - modelKey: 'name' - - 'provider': Attributes.String - modelKey: 'provider' - - 'emailAddress': Attributes.String - queryable: true - modelKey: 'emailAddress' - jsonKey: 'email_address' - - 'organizationUnit': Attributes.String - modelKey: 'organizationUnit' - jsonKey: 'organization_unit' - - 'label': Attributes.String - queryable: false - modelKey: 'label' - - 'aliases': Attributes.Object - queryable: false - modelKey: 'aliases' - - 'defaultAlias': Attributes.Object - queryable: false - modelKey: 'defaultAlias' - jsonKey: 'default_alias' - - 'syncState': Attributes.String - queryable: false - modelKey: 'syncState' - jsonKey: 'sync_state' - - constructor: -> - super - @aliases ||= [] - @label ||= @emailAddress - @syncState ||= "running" - - fromJSON: (json) -> - json["label"] ||= json[@constructor.attributes['emailAddress'].jsonKey] - super - - # Returns a {Contact} model that represents the current user. - me: -> - Contact ?= require './contact' - return new Contact - accountId: @id - name: @name - email: @emailAddress - - meUsingAlias: (alias) -> - Contact ?= require './contact' - return @me() unless alias - return Contact.fromString(alias, accountId: @id) - - defaultMe: -> - if @defaultAlias - return @meUsingAlias(@defaultAlias) - else - return @me() - - usesLabels: -> - @organizationUnit is "label" - - usesFolders: -> - @organizationUnit is "folder" - - categoryLabel: -> - if @usesFolders() - 'Folders' - else if @usesLabels() - 'Labels' - else - 'Unknown' - - categoryCollection: -> - "#{@organizationUnit}s" - - categoryIcon: -> - if @usesFolders() - 'folder.png' - else if @usesLabels() - 'tag.png' - else - 'folder.png' - - # Public: Returns the localized, properly capitalized provider name, - # like Gmail, Exchange, or Outlook 365 - displayProvider: -> - if @provider is 'eas' - return 'Exchange' - else if @provider is 'gmail' - return 'Gmail' - else - return @provider - - canArchiveThreads: -> - CategoryStore ?= require '../stores/category-store' - CategoryStore.getArchiveCategory(@)? - - canTrashThreads: -> - CategoryStore ?= require '../stores/category-store' - CategoryStore.getTrashCategory(@)? - - defaultFinishedCategory: -> - CategoryStore ?= require '../stores/category-store' - preferDelete = NylasEnv.config.get('core.reading.backspaceDelete') - archiveCategory = CategoryStore.getArchiveCategory(@) - trashCategory = CategoryStore.getTrashCategory(@) - - if preferDelete or not archiveCategory - trashCategory - else - archiveCategory - - hasSyncStateError: -> - # TODO: ignoring "stopped" until it's no longer overloaded on API - return @syncState != Account.SYNC_STATE_RUNNING && - @syncState != Account.SYNC_STATE_STOPPED - -module.exports = Account diff --git a/src/flux/models/account.es6 b/src/flux/models/account.es6 new file mode 100644 index 000000000..a4c4c0efb --- /dev/null +++ b/src/flux/models/account.es6 @@ -0,0 +1,188 @@ +import Attributes from '../attributes' +import ModelWithMetadata from './model-with-metadata' + +let CategoryStore = null +let Contact = null + +/** + * Public: The Account model represents a Account served by the Nylas Platform API. + * Every object on the Nylas platform exists within a Account, which typically represents + * an email account. + * + * For more information about Accounts on the Nylas Platform, read the + * [Account API Documentation](https://nylas.com/docs/api#Account) + * + * ## Attributes + * + * `name`: {AttributeString} The name of the Account. + * + * `provider`: {AttributeString} The Account's mail provider (ie: `gmail`) + * + * `emailAddress`: {AttributeString} The Account's email address + * (ie: `ben@nylas.com`). Queryable. + * + * `organizationUnit`: {AttributeString} Either "label" or "folder". + * Depending on the provider, the account may be organized by folders or + * labels. + * + * This class also inherits attributes from {Model} + * + * Section: Models + */ +export default class Account extends ModelWithMetadata { + + static SYNC_STATE_RUNNING = "running" + static SYNC_STATE_STOPPED = "stopped" + static SYNC_STATE_AUTH_FAILED = "invalid" + static SYNC_STATE_ERROR = "sync_error" + + static attributes = Object.assign({}, ModelWithMetadata.attributes, { + name: Attributes.String({ + modelKey: 'name', + }), + + provider: Attributes.String({ + modelKey: 'provider', + }), + + emailAddress: Attributes.String({ + queryable: true, + modelKey: 'emailAddress', + jsonKey: 'email_address', + }), + + organizationUnit: Attributes.String({ + modelKey: 'organizationUnit', + jsonKey: 'organization_unit', + }), + + label: Attributes.String({ + queryable: false, + modelKey: 'label', + }), + + aliases: Attributes.Object({ + queryable: false, + modelKey: 'aliases', + }), + + defaultAlias: Attributes.Object({ + queryable: false, + modelKey: 'defaultAlias', + jsonKey: 'default_alias', + }), + + syncState: Attributes.String({ + queryable: false, + modelKey: 'syncState', + jsonKey: 'sync_state', + }), + }) + + constructor() { + super() + this.aliases = this.aliases || []; + this.label = this.label || this.emailAddress; + this.syncState = this.syncState || "running"; + } + + fromJSON(inJSON) { + const json = inJSON; + json.label = json.label || json[Account.attributes.emailAddress.jsonKey] + return super.fromJSON(json) + } + + // Returns a {Contact} model that represents the current user. + me() { + Contact = Contact || require('./contact') + return new Contact({ + accountId: this.id, + name: this.name, + email: this.emailAddress, + }) + } + + meUsingAlias(alias) { + Contact = Contact || require('./contact') + if (!alias) { + return this.me() + } + return Contact.fromString(alias, {accountId: this.id}) + } + + defaultMe() { + if (this.defaultAlias) { + return this.meUsingAlias(this.defaultAlias) + } + return this.me() + } + + usesLabels() { + return this.organizationUnit === "label" + } + + usesFolders() { + return this.organizationUnit === "folder" + } + + categoryLabel() { + if (this.usesFolders()) { + return 'Folders' + } else if (this.usesLabels()) { + return 'Labels' + } + return 'Unknown' + } + + categoryCollection() { + return "#{this.organizationUnit}s" + } + + categoryIcon() { + if (this.usesFolders()) { + return 'folder.png' + } else if (this.usesLabels()) { + return 'tag.png' + } + return 'folder.png' + } + + // Public: Returns the localized, properly capitalized provider name, + // like Gmail, Exchange, or Outlook 365 + displayProvider() { + if (this.provider === 'eas') { + return 'Exchange' + } else if (this.provider === 'gmail') { + return 'Gmail' + } + return this.provider + } + + canArchiveThreads() { + CategoryStore = CategoryStore || require('../stores/category-store') + return CategoryStore.getArchiveCategory(this) + } + + canTrashThreads() { + CategoryStore = CategoryStore || require('../stores/category-store') + return CategoryStore.getTrashCategory(this) + } + + defaultFinishedCategory() { + CategoryStore = CategoryStore || require('../stores/category-store') + const preferDelete = NylasEnv.config.get('core.reading.backspaceDelete') + const archiveCategory = CategoryStore.getArchiveCategory(this) + const trashCategory = CategoryStore.getTrashCategory(this) + + if (preferDelete || !archiveCategory) { + return trashCategory + } + return archiveCategory + } + + hasSyncStateError() { + // TODO: ignoring "stopped" until it's no longer overloaded on API + return this.syncState !== Account.SYNC_STATE_RUNNING && + this.syncState !== Account.SYNC_STATE_STOPPED + } +} diff --git a/src/flux/nylas-api.coffee b/src/flux/nylas-api.coffee index 2ecd0adde..4f38113d4 100644 --- a/src/flux/nylas-api.coffee +++ b/src/flux/nylas-api.coffee @@ -3,7 +3,7 @@ _ = require 'underscore' request = require 'request' NylasLongConnection = require('./nylas-long-connection').default Utils = require './models/utils' -Account = require './models/account' +Account = require('./models/account').default Message = require './models/message' Actions = require './actions' {APIError} = require './errors' @@ -351,7 +351,7 @@ class NylasAPI "folder": require('./models/folder') "thread": require('./models/thread').default "draft": require('./models/message') - "account": require('./models/account') + "account": require('./models/account').default "message": require('./models/message') "contact": require('./models/contact') "calendar": require('./models/calendar') diff --git a/src/flux/stores/account-store.coffee b/src/flux/stores/account-store.coffee index 2d4c0262c..12de1c9de 100644 --- a/src/flux/stores/account-store.coffee +++ b/src/flux/stores/account-store.coffee @@ -1,7 +1,7 @@ _ = require 'underscore' NylasStore = require 'nylas-store' Actions = require '../actions' -Account = require '../models/account' +Account = require('../models/account').default Utils = require '../models/utils' DatabaseStore = require './database-store' keytar = require 'keytar' @@ -237,7 +237,7 @@ class AccountStore extends NylasStore fs = require 'fs-plus' path = require 'path' Message = require '../models/message' - Account = require '../models/account' + Account = require('../models/account').default Thread = require('../models/thread').default Label = require '../models/label' diff --git a/src/flux/stores/category-store.coffee b/src/flux/stores/category-store.coffee index 07f8463a1..91c6ab7e1 100644 --- a/src/flux/stores/category-store.coffee +++ b/src/flux/stores/category-store.coffee @@ -3,7 +3,7 @@ Rx = require 'rx-lite' NylasStore = require 'nylas-store' AccountStore = require './account-store' NylasSyncStatusStore = require './nylas-sync-status-store' -Account = require '../models/account' +Account = require('../models/account').default {StandardCategoryNames} = require '../models/category' {Categories} = require 'nylas-observables'