Skip to content

Commit 0b62f8f

Browse files
committed
Starting to make objects for all the other classes we will need to get chat running
1 parent 0675ec0 commit 0b62f8f

9 files changed

+95
-8
lines changed

index.coffee

+1-6
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,3 @@
11
Client = require './src/client'
22

3-
module.exports = {
4-
Client
5-
}
6-
7-
module.exports.SlackClient = (token) ->
8-
new Client token
3+
module.exports = Client

src/bot.coffee

Whitespace-only changes.

src/channel.coffee

+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
class Channel
2+
constructor: (@id, options = {}) ->
3+
for k of (options or {})
4+
@[k] = options[k]
5+
@['name'] ||= @id
6+
7+
module.exports = Channel

src/client.coffee

+62-2
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,82 @@
11
https = require 'https'
22
querystring = require 'querystring'
33

4+
User = require './user'
5+
Team = require './team'
6+
Channel = require './channel'
7+
Group = require './group'
8+
DM = require './dm'
9+
Message = require './message'
10+
Bot = require './bot'
11+
412
class Client
513

614
host: 'api.slack.com'
715

816
constructor: (@token) ->
17+
@authenticated = false
18+
@connected = false
19+
20+
@self = null
21+
@team = null
22+
23+
@channels = {}
24+
@dms = {}
25+
@groups = {}
26+
@users = {}
27+
@bots = {}
28+
29+
@socketUrl = null
930

1031
login: ->
1132
@_apiCall 'users.login', {token: @token, agent: 'node-slack'}, @onLogin
1233

13-
onLogin: (data) ->
34+
onLogin: (data) =>
1435
if data
1536
if not data.ok
1637
console.error 'Cannot login: '+data.error
38+
@authenticated = false
1739
else
18-
console.log 'Logged in to '+data.team.name+' as '+data.self.name
40+
@authenticated = true
41+
42+
# Important information about ourselves
43+
@self = new User data.self.id, data.self
44+
@team = new Team data.team.id, data.team.name, data.team.domain
45+
46+
# Stash our websocket url away for later -- must be used within 30 seconds!
47+
@socketUrl = data.url
48+
49+
# Stash our list of channels
50+
for k of data.channels
51+
c = data.channels[k]
52+
@channels[c.id] = new Channel c.id, c
53+
54+
# Stash our list of dms
55+
for k of data.ims
56+
i = data.ims[k]
57+
@dms[i.id] = new DM i.id, i
58+
59+
# Stash our list of private groups
60+
for k of data.groups
61+
g = data.groups[k]
62+
@groups[g.id] = new Group g.id, g
63+
64+
# Stash our list of other users
65+
for k of data.users
66+
u = data.users[k]
67+
@users[u.id] = new User u.id, u
68+
69+
# TODO: Process bots
70+
71+
console.log 'Logged in to '+@team.name+' as '+@self.name
72+
else
73+
console.error 'Invalid login response received (network down?)'
1974

75+
connect: ->
76+
if not @socketUrl
77+
console.error 'Cannot connect without a url. Login first?'
78+
else
79+
console.log 'We would connect here'
2080

2181
_apiCall: (method, params, callback) ->
2282
params['token'] = @token

src/dm.coffee

+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
class DM
2+
constructor: (@id, options = {}) ->
3+
for k of (options or {})
4+
@[k] = options[k]
5+
@['name'] ||= @id
6+
7+
module.exports = DM

src/group.coffee

+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
class Group
2+
constructor: (@id, options = {}) ->
3+
for k of (options or {})
4+
@[k] = options[k]
5+
@['name'] ||= @id
6+
7+
module.exports = Group

src/message.coffee

Whitespace-only changes.

src/team.coffee

+4
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
class Team
2+
constructor: (@id, @name, @domain) ->
3+
4+
module.exports = Team

src/user.coffee

+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
class User
2+
constructor: (@id, options = {}) ->
3+
for k of (options or {})
4+
@[k] = options[k]
5+
@['name'] ||= @id
6+
7+
module.exports = User

0 commit comments

Comments
 (0)