Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP

Comparing changes

Choose two branches to see what's changed or to start a new pull request. If you need to, you can also compare across forks.

Open a pull request

Create a new pull request by comparing changes across two branches. If you need to, you can also compare across forks.
base fork: tenderlove/chatty
base: f968aa1ac5
...
head fork: tenderlove/chatty
compare: 8769589565
  • 2 commits
  • 4 files changed
  • 0 commit comments
  • 1 contributor
View
39 app/assets/javascripts/application.js
@@ -13,3 +13,42 @@
//= require jquery
//= require jquery_ujs
//= require_tree .
+
+function makeSafe(text) {
+ return text.replace(/[&<>"'`]/g, function (chr) {
+ return '&#' + chr.charCodeAt(0) + ';';
+ });
+};
+
+// parseUri 1.2.2
+// (c) Steven Levithan <stevenlevithan.com>
+// MIT License
+
+function parseUri(str) {
+ var o = parseUri.options,
+ m = o.parser[o.strictMode ? "strict" : "loose"].exec(str),
+ uri = {},
+ i = 14;
+
+ while (i--) uri[o.key[i]] = m[i] || "";
+
+ uri[o.q.name] = {};
+ uri[o.key[12]].replace(o.q.parser, function ($0, $1, $2) {
+ if ($1) uri[o.q.name][$1] = $2;
+ });
+
+ return uri;
+};
+
+parseUri.options = {
+ strictMode: false,
+ key: ["source","protocol","authority","userInfo","user","password","host","port","relative","path","directory","file","query","anchor"],
+ q: {
+ name: "queryKey",
+ parser: /(?:^|&)([^&=]*)=?([^&]*)/g
+ },
+ parser: {
+ strict: /^(?:([^:\/?#]+):)?(?:\/\/((?:(([^:@]*)(?::([^:@]*))?)?@)?([^:\/?#]*)(?::(\d*))?))?((((?:[^?#\/]*\/)*)([^?#]*))(?:\?([^#]*))?(?:#(.*))?)/,
+ loose: /^(?:(?![^:@]+:[^:@\/]*@)([^:\/?#.]+):)?(?:\/\/)?((?:(([^:@]*)(?::([^:@]*))?)?@)?([^:\/?#]*)(?::(\d*))?)(((\/(?:[^?#](?![^?#\/]*\.[^?#\/.]+(?:[?#]|$)))*\/?)?([^?#\/]*))(?:\?([^#]*))?(?:#(.*))?)/
+ }
+};
View
9 app/controllers/messages_controller.rb
@@ -50,12 +50,17 @@ def show
Message.add_observer Listener.new(Message.maximum(:id), queue)
Message.limit(30).order("id desc").all.reverse_each do |m|
- queue.push m
+ hash = m.event_hash
+ hash['me'] = m.uid == session[:id] ? 'yes' : 'no'
+ ss.write hash
end
while event = queue.pop
hash = event.event_hash
- hash['me'] = event.uid == session[:id] ? 'yes' : 'no'
+
+ next if event.uid == session[:id]
+
+ hash['me'] = 'no'
ss.write hash
end
ensure
View
10 app/models/message.rb
@@ -28,13 +28,17 @@ def event_hash
def imgval
begin
uri = URI(value)
- if uri.scheme == 'http' && uri.path =~ /\.(jpg|png|gif)$/
- "<img src=\"#{value}\" />"
+ if uri.scheme == 'http'
+ if uri.path =~ /\.(jpg|png|gif)$/
+ "<img src=\"#{value}\" />"
+ else
+ "<a href=\"#{value}\">#{h(value)}</a>"
+ end
else
h(value)
end
rescue
- value
+ h(value)
end
end
View
26 app/views/messages/show.html.erb
@@ -4,10 +4,36 @@ Type "/nick yourname" to change your nickname.
</ul>
<script>
+ var nickname = '<%= h(session[:name]) %>';
+
jQuery(document).ready(function() {
$("#messages-form").bind('ajax:success', function(data, status, xhr) {
$("#messages-form").find(':input').each(function() {
if(this.type == 'text') {
+ var room = $('#chatroom');
+ var message = $(this).val();
+ var name = nickname;
+ var match = /^\/nick\s*(.*)$/.exec(message);
+
+ if (match) {
+ message = "changed name to " + match[1];
+ nickname = match[1];
+ }
+
+ var uri = parseUri(message);
+ if (uri && uri.protocol == 'http') {
+ if (/\.(png|jpg|gif)$/.exec(uri.path)) {
+ message = "<img src=\"" + message + "\" />";
+ } else {
+ message = "<a href=\"" + message + "\">" + makeSafe(message) + "</a>";
+ }
+ } else {
+ message = makeSafe(message);
+ }
+
+ room.append($("<li class=\"me\">" +
+ makeSafe(name) + ": " + message + "</li>"));
+
$(this).val('');
}
});

No commit comments for this range

Something went wrong with that request. Please try again.