Permalink
Browse files

Now using last seen counter instead of unix epoch which works better.

  • Loading branch information...
1 parent 0f5d96d commit 43776c97492d45179707b72b1ca2acc96ec14c86 @jnunemaker committed Jul 31, 2008
View
@@ -1,10 +1,6 @@
(function($) {
var field = $('#message'), messages = $('#messages'), scroll_to_bottom = true,
- message_url = '/rooms/' + room_id + '/messages',
- text_msg_tpl = $.template('<div id="message_${id}" class="message">' +
- '<div class="message_user">${user}</div>' +
- '<div class="message_body">${body}</div>' +
- '</div>');
+ message_url = '/rooms/' + room_id + '/messages';
function createMessage(body, o) {
var params = {breed:'text', body:body}
@@ -16,16 +12,18 @@
}
function addMessageToList(message) {
- if ($('#message_' + message.id).length == 0) {
- $.log('adding message to list');
- messages.append(text_msg_tpl, {
- id: message.id,
- body: message.body,
- user: message.user.nickname
- });
+ if ($('#message_' + message.evil).length == 0) {
+ l('adding message to list');
+
+ messages.append('<div id="message_' + message.evil +'" class="message">' +
+ '<div class="message_user">' + message.user.nickname + '</div>' +
+ '<div class="message_body"></div>' +
+ '</div>');
+ // escapes the html of the message and appends it
+ $('#message_' + message.evil + ' div.message_body').append(document.createTextNode(message.body));
field.scrollTo();
} else {
- $.log('message already existed');
+ l('message already existed');
}
}
@@ -34,9 +32,8 @@
num = json.length;
if (num > 0) {
var m = json[num-1];
- since = m.created.epoch;
- last_id = m.id
- $.log('setting since to ' + since + ' and last id to ' + last_id);
+ evil = m.evil;
+ l('setting evil to: ' + evil);
}
}
@@ -61,11 +58,11 @@
field.keypress(onKeyPress).scrollTo().focus();
var times = 0;
- $.timer(4000, function(timer) {
- $.get(message_url, {since:since}, addMessagesToList, 'json');
+ $.timer(3000, function(timer) {
+ $.get(message_url, {'evil':evil}, addMessagesToList, 'json');
times += 1;
- if (times == 10) { timer.stop(); }
+ // if (times == 20) { timer.stop(); }
});
$(window).bind('resize', function() {
View
@@ -1,3 +1,3 @@
-jQuery.extend({
- log: function(msg) { if (window.console && console.log) console.log(msg); }
-});
+function l(msg) {
+ if (window.console && console.log) console.log(msg);
+}
@@ -35,9 +35,9 @@ class ApplicationHandler(webapp.RequestHandler):
def render_template(self, template_path, template_values={}):
values = {
'request' : self.request,
- 'current_user' : users.GetCurrentUser(),
- 'login_url' : users.CreateLoginURL(self.request.uri),
- 'logout_url' : users.CreateLogoutURL(self.request.uri),
+ 'current_user' : users.get_current_user(),
+ 'login_url' : users.create_login_url(self.request.uri),
+ 'logout_url' : users.create_logout_url(self.request.uri),
'application_name' : 'Communication',
}
values.update(template_values)
@@ -13,19 +13,19 @@ class MessageCollectionHandler(handlers.ApplicationHandler):
@handlers.find_room_and_authorize
def get(self, room):
"""Returns the latest messages for a given room in json."""
- since = datetime.fromtimestamp(int(self.request.get('since')))
- messages = models.Message.gql("WHERE room = :room AND created > :since", room=room, since=since)
+ messages = models.Message.gql("WHERE room = :room AND evil > :evil", room=room, evil=int(self.request.get('evil')))
self.render_json(messages)
-
+
@handlers.login_required
@handlers.find_room_and_authorize
def post(self, room):
"""Creates a new message."""
form = forms.MessageForm(data=self.request.POST)
if form.is_valid():
- message = form.save(commit=False)
+ message = form.save(commit=False)
message.room = room
message.user = users.get_current_user()
+ message.increment_evil()
message.put()
self.render_json(message)
else:
@@ -24,6 +24,18 @@ class Message(db.Model):
created = db.DateTimeProperty(auto_now_add=True)
room = db.ReferenceProperty(Room, collection_name='messages')
user = db.UserProperty()
+ evil = db.IntegerProperty()
+
+ def increment_evil(self):
+ """docstring for fname"""
+ last = Message.gql("WHERE room = :room ORDER BY evil DESC", room=self.room).fetch(1)
+ if len(last) > 0:
+ self.evil = last[0].evil + 1
+ else:
+ self.evil = 1
+
+
+
class Authorization(db.Model):
room = db.ReferenceProperty(Room, collection_name='authorizations', required=True)
@@ -18,6 +18,7 @@
<div id="header">
<ul>
<li><a href="/rooms">Home</a></li>
+ <li>Logged in as {{ current_user.nickname }}</li>
<li><a href="{{ logout_url }}">Logout</a></li>
</ul>
</div>
@@ -1,15 +1,23 @@
{% extends "../layouts/application.html" %}
{% block primary %}
+ <script type="text/javascript" charset="utf-8">
+ var evil = 0;
+ </script>
<div id="messages">
{% for message in messages %}
- <div id="message_{{ message.key.id }}" class="message">
+ <div id="message_{{ message.evil }}" class="message">
<div class="message_user">
{{ message.user.nickname }}
</div>
<div class="message_body">
- {{ message.body }}
+ {{ message.body|escape }}
</div>
</div>
+ {% if forloop.last %}
+ <script type="text/javascript" charset="utf-8">
+ evil = {{ message.evil }};
+ </script>
+ {% endif %}
{% endfor %}
</div>
<form id="chat_form">
@@ -21,7 +29,7 @@
</form>
<script type="text/javascript" charset="utf-8">
- var since = Math.round(new Date().getTime()/1000.0);
+ // var since = Math.round(new Date().getTime()/1000.0);
var room_id = {{ room.key.id }};
</script>
<script type="text/javascript" src="/assets/js/chat.js"></script>
View
@@ -10,19 +10,32 @@ indexes:
# automatically uploaded to the admin console when you next deploy
# your application using appcfg.py.
-# Used 18 times in query history.
+# Unused in query history -- copied from input.
- kind: Message
properties:
- name: room
- name: ID
-# Used 1316 times in query history.
+# Used 2247 times in query history.
- kind: Message
properties:
- name: room
- name: created
-# Used 209 times in query history.
+# Used 132 times in query history.
+- kind: Message
+ properties:
+ - name: room
+ - name: evil
+
+# Used 18 times in query history.
+- kind: Message
+ properties:
+ - name: room
+ - name: evil
+ direction: desc
+
+# Unused in query history -- copied from input.
- kind: Message
properties:
- name: room

0 comments on commit 43776c9

Please sign in to comment.