Skip to content

Commit

Permalink
use refList for messages
Browse files Browse the repository at this point in the history
  • Loading branch information
cpetzold committed Apr 27, 2012
1 parent 227862c commit ee48567
Show file tree
Hide file tree
Showing 4 changed files with 83 additions and 25 deletions.
21 changes: 14 additions & 7 deletions src/bot.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,24 @@

module.exports = class Bot
constructor: (opts = {}) ->
@store = opts.store
store = opts.store

if !@store
if !store
throw new Error 'Must pass in a store'

opts.network ?= 'the0th'
opts.server ?= 'irc.the0th.com';
opts.channels ?= [ '#derbyjs' ]
opts.channels ?= [ '#derbyjs', '#pwn' ]
opts.port ?= 6667
opts.nick ?= 'derbybot'

@path = opts.path ? 'channels.'
@path += '.' if not @path.match '.'
refPath = opts.refPath ? '_messages'
toPath = opts.toPath ? 'messages'
keyPath = opts.keyPath ? '_messageIds'

model = store.createModel()
model.refList refPath, toPath, keyPath
@messages = model.at refPath

@client = new Client opts.server, opts.nick, opts
@client.on 'message', @onMessage
Expand All @@ -37,6 +43,7 @@ module.exports = class Bot
from: from

push: (to, message) ->
path = @path + to.replace '#', ''
message.channel = to.replace '#', ''
message.timestamp = Date.now()
@store.push path + '.messages', message, null
console.log message
@messages.push message
20 changes: 14 additions & 6 deletions src/orca/index.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -6,22 +6,30 @@ orca = derby.createApp module
orca.view.fn 'prettyDate', (d) ->
moment(d).format 'hh:mm:ss a'

orca.get '/', (page) ->
page.redirect '/derbyjs'

orca.get '/:channel', (page, model, params) ->
path = 'channels.' + params.channel
model.subscribe path, (e, channel) ->
messageQuery = model.query 'messages',
where:
channel: params.channel
messageQuery.sort 'timestamp', 'desc'

model.subscribe "channels.#{params.channel}", messageQuery, (e, channel) ->
model.ref '_channel', channel
model.setNull '_channel.messages', []
model.refList '_messages', 'messages', '_messageIds'
page.render()

orca.ready (model) ->
window.model = model

input = document.getElementById 'input'
messages = document.getElementById 'messages'
# toggleActions = document.getElementById 'toggleActions'


document.body.scrollTop = messages.offsetHeight
input.focus()

# toggleActions = document.getElementById 'toggleActions'
# toggleActions.addEventListener 'click', (e) ->
# text = toggleActions.innerText
# if text is 'Hide actions'
Expand All @@ -31,5 +39,5 @@ orca.ready (model) ->
# toggleActions.innerText = 'Hide actions'
# messages.className = ''

model.on 'push', '_channel.messages', (message, len, isLocal) ->
model.on 'push', '_messages', (message, len, isLocal) ->
document.body.scrollTop = messages.offsetHeight
54 changes: 48 additions & 6 deletions styles/orca.styl
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
@import 'nib'

body
background: #2c2b2c
color: #fff
Expand All @@ -11,19 +13,59 @@ input
&:focus
outline: none

#input
#controls
position: fixed
bottom: 0
left: 0
width: 100%

#input
display: none
width: 100%
padding: 10px 5px
margin: 1px 0 0
background: #222
color: #eee
border-top: 1px solid #3e3e3e
box-shadow: 0 -1px 0 #222
background: #2c2b2c
border-top: 1px solid #222
box-shadow: inset 0 5px 15px -5px #222
&:focus
box-shadow: 0 -1px 0 #222, inset 0 5px 15px -5px #111
box-shadow: inset 0 5px 15px -5px #111

#channels
padding: 0 4px
background: #111
color: #eee
border-top: 5px solid #222
box-shadow: 0 -1px 0 #3e3e3e, inset 0 1px 0 #3e3e3e, inset 0 1px 6px #000

.channel
display: inline-block
vertical-align: top
padding: 6px 8px
margin-top: 1px
cursor: default
user-select: none
&:hover
background: #151515

.channel-name
color: #999

&.active
padding: 5px 8px 6px
margin-top: 0
background: #222
border: 1px solid #3e3e3e
border-top: none

.channel-name
color: #eee

.channel-name
color: #777

.channel-unread
display: none

ul
list-style: none
Expand Down Expand Up @@ -57,7 +99,7 @@ ul
&:after
content: ' quit'

.message
.message-message
.message-from
color: #bec4a4
&:before
Expand Down
13 changes: 7 additions & 6 deletions views/orca/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -7,18 +7,19 @@
<Body:>
<div id='controls'>
<input id='input' type='text' />
<ul id='servers'>
{#each _channels}<app:channelTab>{/}
<ul id='channels'>
{#each channels}<app:channelTab>{/}
</ul>
</div>
<ul id='messages'>
{#each _channel.messages}<app:message>{/}
{#each _messages}<app:message>{/}
</ul>

<channelTab:>
<li class='channel'>
<span class='channel-name'>{{name}}</span>
<span class='channel-unread'>{unread}
<li><a href='/{{_id}}' class='channel'>
<span class='channel-name'>{{_id}}</span>
<span class='channel-unread'>{._unread}</span>
</a>

<message:>
<li class='message message-{{type}}'>
Expand Down

0 comments on commit ee48567

Please sign in to comment.