Browse Source

Made links clickable in chat/news.

KrisVos130 9 years ago
parent
commit
1fe9af7812

+ 46 - 3
app/client/scripts/helpers.js

@@ -228,7 +228,12 @@ Template.queues.helpers({
 
 Template.news.helpers({
     articles: function() {
-        return News.find().fetch().reverse();
+        var articles =  News.find().fetch().reverse();
+        articles = articles.map(function(article) {
+            article.content = replaceURLWithHTMLLinks(article.content);
+            return article;
+        });
+        return articles;
     }
 });
 
@@ -347,7 +352,12 @@ Template.room.helpers({
                 elem.scrollTop = elem.scrollHeight;
             }
         }, 100);
-        return Chat.find({type: "global"}, {sort: {time: -1}, limit: 50}).fetch().reverse();
+        var messages = Chat.find({type: "global"}, {sort: {time: -1}, limit: 50}).fetch().reverse();
+        messages = messages.map(function(message) {
+            message.message = replaceURLWithHTMLLinks(message.message);
+            return message;
+        });
+        return messages;
     },
     likes: function () {
         var playlist = Songs.find({"genres": Session.get("type")}).fetch();
@@ -556,7 +566,12 @@ Template.privateRoom.helpers({
                 elem.scrollTop = elem.scrollHeight;
             }
         }, 100);
-        return Chat.find({type: "global"}, {sort: {time: -1}, limit: 50}).fetch().reverse();
+        var messages = Chat.find({type: "global"}, {sort: {time: -1}, limit: 50}).fetch().reverse();
+        messages = messages.map(function(message) {
+            message.message = replaceURLWithHTMLLinks(message.message);
+            return message;
+        });
+        return messages;
     },
     privateRoomDisplayName: function () {
         var parts = location.href.split('/');
@@ -640,3 +655,31 @@ Template.settings.helpers({
         }
     }
 });
+
+function replaceURLWithHTMLLinks(text) {
+    var re = /(\(.*?)?\b((?:https?|ftp|file):\/\/[-a-z0-9+&@#\/%?=~_()|!:,.;]*[-a-z0-9+&@#\/%=~_()|])/ig;
+    return text.replace(re, function(match, lParens, url) {
+        var rParens = '';
+        lParens = lParens || '';
+
+        // Try to strip the same number of right parens from url
+        // as there are left parens.  Here, lParenCounter must be
+        // a RegExp object.  You cannot use a literal
+        //     while (/\(/g.exec(lParens)) { ... }
+        // because an object is needed to store the lastIndex state.
+        var lParenCounter = /\(/g;
+        while (lParenCounter.exec(lParens)) {
+            var m;
+            // We want m[1] to be greedy, unless a period precedes the
+            // right parenthesis.  These tests cannot be simplified as
+            //     /(.*)(\.?\).*)/.exec(url)
+            // because if (.*) is greedy then \.? never gets a chance.
+            if (m = /(.*)(\.\).*)/.exec(url) ||
+                    /(.*)(\).*)/.exec(url)) {
+                url = m[1];
+                rParens = m[2] + rParens;
+            }
+        }
+        return lParens + "<a style='font-size: 18px; padding: 0; display: inline;' target='_blank' href='" + url + "'>" + url + "</a>" + rParens;
+    });
+}

+ 1 - 1
app/client/templates/news.html

@@ -8,7 +8,7 @@
                         <h3 class="center">{{title}}</h3>
                         <hr style="margin: 15px;">
                         <p id="news-content" style="word-wrap:break-word;" class="black-text">
-                            {{content}}
+                            {{{content}}}
                         </p>
                     </div>
                     <div class="card-action">

+ 1 - 1
app/client/templates/privateRoom.html

@@ -55,7 +55,7 @@
                     <li class="chat-message" style="line-height: 30px">
                         <span title="{{time}}" style="float: right; margin-top: 15px">{{rtime time}}</span>
                         <a style="text-decoration: none; font-size: 0.9em; height: 0.9em; font-weight: 500" href="/u/{{this.username}}" target="_blank"><span class="rank-{{this.rawrank}}">{{this.rank}}</span>{{this.username}}</a>
-                        <p style="clear: both; line-height: 1.2em; margin-left: 13px; margin-bottom: 0; font-size: 1.2em">{{this.message}}</p>
+                        <p style="clear: both; line-height: 1.2em; margin-left: 13px; margin-bottom: 0; font-size: 1.2em">{{{this.message}}}</p>
                     </li>
                 {{/emojione}}
                 <div class="divider" style="margin-top: 15px;"></div>

+ 1 - 1
app/client/templates/room.html

@@ -70,7 +70,7 @@
                     <li class="chat-message" style="line-height: 30px">
                         <span title="{{time}}" style="float: right; margin-top: 15px">{{rtime time}}</span>
                         <a style="text-decoration: none; font-size: 0.9em; height: 0.9em; font-weight: 500" href="/u/{{this.username}}" target="_blank"><span class="rank-{{this.rawrank}}">{{this.rank}}</span>{{this.username}}</a>
-                        <p style="clear: both; line-height: 1.2em; margin-left: 13px; margin-bottom: 0; font-size: 1.2em">{{this.message}}</p>
+                        <p style="clear: both; line-height: 1.2em; margin-left: 13px; margin-bottom: 0; font-size: 1.2em">{{{this.message}}}</p>
                     </li>
                 {{/emojione}}
                 <div class="divider" style="margin-top: 15px;"></div>

+ 7 - 1
app/server/server.js

@@ -1119,7 +1119,8 @@ Meteor.methods({
             var time = new Date();
             var rawrank = user.profile.rank;
             var username = user.profile.username;
-            var profanity = false
+            var profanity = false;
+            message = htmlEntities(message);
             if (!message.replace(/\s/g, "").length > 0) {
                 throw new Meteor.Error(406, "Message length cannot be 0.");
             }
@@ -1441,6 +1442,7 @@ Meteor.methods({
                 }
                 delete data.anonymous;
                 data.time =  new Date();
+                data.content = htmlEntities(data.content);
                 News.insert(data, function(err, res) {
                     if (err) {
                         console.log(err);
@@ -1745,3 +1747,7 @@ Meteor.setInterval(function () {
 Meteor.users.after.insert(function (err, user) {
     Accounts.sendVerificationEmail(user._id);
 });
+
+function htmlEntities(str) {
+    return String(str).replace(/&/g, '&amp;').replace(/</g, '&lt;').replace(/>/g, '&gt;').replace(/"/g, '&quot;');
+}