1. Benoît Allard
  2. msgboard-meteor

Commits

Benoit Allard  committed d65c8cf

keepalive, ago implemented

  • Participants
  • Parent commits cc2df41
  • Branches default

Comments (0)

Files changed (3)

File msgboard.coffee

View file
  • Ignore whitespace
   window.Users = Users
   window.Messages = Messages
 
-  update_user = ->
-    Users.update(
-          {name: name},
-          {$set: {last_visit: Date.now()}})
-
   logged_on = ->
-    Session.get('user_name') != undefined
+    not Session.equals('user_name', undefined)
 
   Template.login.user = ->
     Session.get('user_name')
           console.log "Added user", name, id
         Session.set('user_name', name)
         $('#username').val('')
+
     'click #logout': (event) ->
       Session.set('user_name')
 
         last_modified_on: now)
       Session.set('add_msg',false)
 
+  seen = ->
+    Message.update(
+      {_id: this._id},
+      {$push: {seen: Session.get('user_name')}})
+
   Template.news.news = ->
-    usr = Users.findOne({name: Session.get('user_name')})
-    console.log usr, Session.get('user_name')
-    Messages.find({
+    name = Session.get('user_name')
+    usr = Users.findOne({name: name})
+    if not usr
+      # during refresh, on live update, the DB fails
+      console.log "escaping ", usr, name
+      return {}
+    msgs = Messages.find({
         user: {$ne: usr.name},
-        created_on: {$gt: usr.last_visit}},{
-        sort: {created_on: -1}})
+        created_on: {$gt: usr.last_keepalive}},{
+        sort: {created_on: -1}}).fetch()
+    _.each msgs, (msg) -> msg.printauthor = true
+    msgs
 
   Template.news.mods = ->
-    usr = Users.findOne({name: Session.get('user_name')})
-    console.log usr, Session.get('user_name')
-    Messages.find({
+    name = Session.get('user_name')
+    usr = Users.findOne({name: name})
+    if not usr
+      console.log "escaping ", usr, name
+      return {}
+    msgs = Messages.find({
         user: {$ne: usr.name},
-        last_modified_on: {$gt: usr.last_visit}
-        created_on: {$lte: usr.last_visit}},{
-        sort: {last_modified_on: -1}})
+        last_modified_on: {$gt: usr.last_keepalive}
+        created_on: {$lte: usr.last_keepalive}},{
+        sort: {last_modified_on: -1}}).fetch()
+    _.each msgs, (msg) -> msg.printauthor = true
+    msgs
 
   Template.users.users = ->
     Users.find(
       {user: Session.get('user_name')},
       sort:
         last_modified_on: -1
-      )
+    )
 
   Template.message.show = ->
-    this.user == Session.get('user_name') or not this.archived
+    Session.equals('user_name', this.user) or not this.archived
 
   Template.message.loggedonuser = ->
-    this.user == Session.get('user_name')
-
-  Template.message.printauthor = ->
-    this.user != Session.get('user_name')
+    Session.equals('user_name', this.user)
 
   Template.message.ago = (date) ->
-    '3 years ago'
+
+    scales =
+      year:  365 * 24 * 60 * 60,
+      month:  30 * 24 * 60 * 60,
+      week:    7 * 24 * 60 * 60,
+      day:    24 * 60 * 60,
+      hour:   60 * 60,
+      minute: 60,
+      second: 1
+
+    format = (count, string) ->
+      ret = count + ' ' + string;
+      if count > 1
+        ret = ret + 's'
+      ret
+
+     shortdate = (date) ->
+       ret = date.getFullYear() + '-'
+       # getMonth() gives a 0-11 result
+       month = date.getMonth() + 1
+       if month <= 9
+         ret += '0' + month
+       else
+         ret += month
+       ret += '-'
+       day = date.getDate();
+       if day <= 9
+         ret += '0' + day
+       else
+         ret += day;
+       ret
+
+    now = new Date
+    delta = Math.floor (now.getTime() - date) / 1000
+
+
+    if delta > (5 * scales.year)
+      return shortdate(once);
+
+    for unit, s of scales
+      n = Math.floor delta / s
+      if (n >= 2) or (s == 1)
+        return format(n, unit) + ' ago'
 
   Template.message.edited = ->
     this.created_on != this.last_modified_on
     "click #archive": ->
       Messages.update({_id: this._id},{$set: {archived: true}})
     "click #unarchive": ->
-      Messages.update({_id: this._id},{$set: {archived: false}})
+      Messages.update({_id: this._id},{$set: {archived: false}})
+    "click #edit": ->
+      console.log "edit", this
+
+  Meteor.startup ->
+    # send keepalives so the server can tell when we go away.
+    #
+    # XXX this is not a great idiom. meteor server does not yet have a
+    # way to expose connection status to user code. Once it does, this
+    # code can go away.
+    Meteor.setInterval ->
+      if Meteor.status().connected
+        Meteor.call 'keepalive', Session.get('user_name')
+    , 20*1000
+
+if Meteor.is_server
+
+  Meteor.methods
+    keepalive: (user_name) ->
+      Users.update(
+        {name: user_name},
+        $set:
+          last_keepalive: Date.now()
+      )

File msgboard.html

View file
  • Ignore whitespace
   {{> login}}
   {{> new}}
   <div class="content">
-  <div id="block">
+  <div class="block">
     {{> news}}
   </div>
-  <div id="block">
+  <div class="block">
     {{> users}}
   </div>
-  <div id="block">
+  <div class="block">
     {{> mines}}
   </div>
   </div>
   {{# if show}}
   <section class="message{{#if archived}} archived{{/if}}" id="{{_id}}">
     <p class=info>Posted&nbsp;
-    {{#if printauthor }}by {{ user }} {{/if}}
+    {{#if printauthor}}by {{ user }} {{/if}}
     {{ ago created_on }}
     {{#if edited }}
       <span title="last modified&nbsp;{{ ago last_modified_on }}">*</span>

File msgboard.less

View file
  • Ignore whitespace
-/* CSS declarations go here */
-
 body {
   font-family: 'Georgia', serif;
   font-size: 17px;
   color: black;
 }
 
-#block {
+.rounded-corners (@radius: 10px) {
+  border-radius: @radius;
+  -webkit-border-radius: @radius;
+  -moz-border-radius: @radius;
+}
+
+.block {
+  .rounded-corners(15px);
+  border: 3px solid lighten(#DDD, 5%);
   float: left;
   width: 620px;
 }
   }
 }
 
-.page {
-  width: 540px;
-  margin: 40px auto;
-}
-
 #logout {
   float:right;
   font-size: 70%;
 
 .message {
 
+  .rounded-corners(10px);
   border: 3px solid #DDD;
-  border-radius: 10px;
   margin-left: 30px;
   padding: 5px;