Skip to content
This repository has been archived by the owner on Apr 12, 2023. It is now read-only.

Commit

Permalink
Refactor deserialization of Account
Browse files Browse the repository at this point in the history
  • Loading branch information
zcbenz committed Jan 11, 2019
1 parent 2abd3dc commit 9cdb01b
Show file tree
Hide file tree
Showing 6 changed files with 27 additions and 23 deletions.
3 changes: 2 additions & 1 deletion lib/controller/account-manager.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,8 @@ class AccountsManager {
deserialize(config) {
for (const c of config) {
const service = require(path.join(this.servicesPath, c.service))
service.deserializeAccount(c)
const account = service.createAccount(c.id, c.name, c.token)
account.deserialize(c)
}
}

Expand Down
2 changes: 1 addition & 1 deletion lib/controller/config-store.js
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ class ConfigStore {
this.locked = true
if (config.version != CONFIG_VERSION)
config = {} // TODO Report error here
if (config.accounts && Array.isArray(config.accounts))
if (Array.isArray(config.accounts))
accountManager.deserialize(config.accounts)
if (config.window)
windowManager.initWithConfig(config.window)
Expand Down
26 changes: 16 additions & 10 deletions lib/model/account.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,27 +3,23 @@ const Signal = require('mini-signals')
const accountManager = require('../controller/account-manager')

class Account {
constructor(service, config) {
constructor(service, id, name) {
this.service = service
this.id = config.id
this.name = config.name
this.icon = config.icon ? config.icon : null
this.url = `https://${this.id}.${this.service.id}.com`
this.id = id
this.name = name
this.icon = null
this.url = `https://${this.id}.${service.id}.com`
this.authorizationHeader = null
this.channels = []
this.dms = []
this.emoji = {}
this.currentChannelId = config.currentChannelId ? config.currentChannelId : null
this.currentChannelId = null
this.currentUserId = null
this.currentUserName = null
this.users = {}
this.isRead = true
this.mentions = 0

// Save the config of channels, since channels are loaded later.
if (config.channels)
this.channelsConfig = config.channels.reduce((r, i) => { r[i.id] = i; return r }, {})

this.status = 'connecting'
this.isChannelsReady = false

Expand Down Expand Up @@ -57,6 +53,16 @@ class Account {
return config
}

deserialize(config) {
if (config.icon)
this.icon = config.icon
if (config.currentChannelId)
this.currentChannelId = config.currentChannelId
// Save the config of channels, since channels are loaded later.
if (config.channels)
this.channelsConfig = config.channels.reduce((r, i) => { r[i.id] = i; return r }, {})
}

channelsLoaded() {
if (this.channelsConfig) {
for (const messageList of this.channels.concat(this.dms)) {
Expand Down
2 changes: 1 addition & 1 deletion lib/model/service.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ class Service {
throw new Error('This method should be implemented by clients')
}

deserializeAccount(config) {
createAccount(id, name, token) {
throw new Error('This method should be implemented by clients')
}
}
Expand Down
7 changes: 4 additions & 3 deletions lib/service/slack/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,8 @@ class SlackService extends Service {
this.loginWindow.activate()
}

deserializeAccount(config) {
return new SlackAccount(this, null, null, config)
createAccount(id, name, token) {
return new SlackAccount(this, id, name, token)
}

async loginWithToken(token, button) {
Expand All @@ -40,7 +40,8 @@ class SlackService extends Service {
try {
// Test the token.
const client = new WebClient(token)
new SlackAccount(this, await client.auth.test(), token)
const data = await client.auth.test()
this.createAccount(data.team_id, data.team, token)
// Succeeded.
if (button) {
this.loginWindow.getContentView().removeChildView(button)
Expand Down
10 changes: 3 additions & 7 deletions lib/service/slack/slack-account.js
Original file line number Diff line number Diff line change
Expand Up @@ -40,13 +40,9 @@ function filterDM(c) {
}

class SlackAccount extends Account {
constructor(service, data, token, config=null) {
if (config)
super(service, config)
else
super(service, {id: data.team_id, name: data.team})

this.setupRTMClient(config ? config.token : token)
constructor(service, id, name, token) {
super(service, id, name)
this.setupRTMClient(token)
}

setupRTMClient(token) {
Expand Down

0 comments on commit 9cdb01b

Please sign in to comment.