From aadea033078e46601a0f3367d7ece64959a694ef Mon Sep 17 00:00:00 2001
From: Pierre-Yves Chibon <pingou@pingoured.fr>
Date: Mar 29 2018 16:58:49 +0000
Subject: [PATCH 1/3] Be consistent on how we get external scripts


Signed-off-by: Pierre-Yves Chibon <pingou@pingoured.fr>

---

diff --git a/pagure/templates/pull_request.html b/pagure/templates/pull_request.html
index c297985..258d2f3 100644
--- a/pagure/templates/pull_request.html
+++ b/pagure/templates/pull_request.html
@@ -970,9 +970,12 @@
 <script type="text/javascript"
     src="{{ url_for('static', filename='emoji/emojicomplete.js') }}">
 </script>
-<script src="{{ url_for('static', filename='vendor/selectize/selectize.min.js') }}" type="text/javascript"> </script>
-<script type="text/javascript" src="{{ url_for('static', filename='vendor/jquery.caret/jquery.caret.min.js') }}"></script>
-<script type="text/javascript" src="{{ url_for('static', filename='vendor/jquery.atwho/jquery.atwho.min.js') }}"></script>
+<script type="text/javascript"
+    src="{{ url_for('static', filename='vendor/selectize/selectize.min.js') }}"> </script>
+<script type="text/javascript"
+    src="{{ url_for('static', filename='vendor/jquery.caret/jquery.caret.min.js') }}"></script>
+<script type="text/javascript"
+    src="{{ url_for('static', filename='vendor/jquery.atwho/jquery.atwho.min.js') }}"></script>
 
 <script type="text/javascript">
 function cancel_edit_btn() {

From df52cc62c533a596dab910408a90b638fd204aec Mon Sep 17 00:00:00 2001
From: Pierre-Yves Chibon <pingou@pingoured.fr>
Date: Mar 29 2018 17:23:39 +0000
Subject: [PATCH 2/3] Support showing comment submitted by ajax when the SSE is down/not set


Fixes https://pagure.io/pagure/issue/2077

Signed-off-by: Pierre-Yves Chibon <pingou@pingoured.fr>

---

diff --git a/pagure/templates/pull_request.html b/pagure/templates/pull_request.html
index 258d2f3..f155200 100644
--- a/pagure/templates/pull_request.html
+++ b/pagure/templates/pull_request.html
@@ -976,6 +976,8 @@
     src="{{ url_for('static', filename='vendor/jquery.caret/jquery.caret.min.js') }}"></script>
 <script type="text/javascript"
     src="{{ url_for('static', filename='vendor/jquery.atwho/jquery.atwho.min.js') }}"></script>
+<script type="text/javascript"
+  src="{{ url_for('static', filename='request_ev.js') }}"></script>
 
 <script type="text/javascript">
 function cancel_edit_btn() {
@@ -1309,7 +1311,6 @@ $( ".editmetadatatoggle" ).click(
 );
 
 function set_ui_for_comment(setting){
-  console.log(setting)
   if (setting == false) {
     $(document.body).find('input[type="submit"]').removeAttr("disabled");
     document.body.style.cursor = 'default';
@@ -1328,17 +1329,27 @@ function try_async_comment(form, inline) {
     set_ui_for_comment(false);
     return true;
   }
-  if (!sse) {
-    console.log('no sse, submitting');
-    form.submit();
-    return false;
-  }
   var _url = form.action;
   if (_url.indexOf('?') != -1){
       _url += "&js=1";
   } else {
     _url += "?js=1";
   }
+
+  /* Keep some variable in memory before sending them in case the SSE is down */
+  var _comment = $(form).find('#comment').val();
+  var _commit_id = null;
+  var _line = null;
+  var _token = "{{ mergeform.csrf_token.current_token }}";
+  var _base_url = _url.split('?')[0];
+  if (!_base_url.match(/comment$/)){
+    _commit_id = _url.split('/comment/')[1];
+    _commit_id = _commit_id.split('/')[0];
+
+    items = _url.split('/');
+    _line = items[items.length -1];
+  }
+
   $.post( _url, $(form).serialize() )
     .done(function(data) {
       if(data == 'ok') {
@@ -1354,7 +1365,38 @@ function try_async_comment(form, inline) {
           $(item.parent().children()[1]).show()
         }
         item.remove();
-        set_ui_for_comment(false);
+        if (!sse) {
+            console.log('no sse, adding the comment manually');
+            $.ajax({
+              url: "{{ url_for('ui_ns.markdown_preview') }}" ,
+              type: 'POST',
+              data: {
+                content: _comment,
+                csrf_token: _token,
+              },
+              dataType: 'html',
+              success: function(res) {
+                var _comment = emojione.toImage(res);
+                var data = {
+                    comment_added: _comment,
+                    comment_user: "{{ g.fas_user.username }}",
+                    comment_date: Date.now(),
+                    avatar_url: "{{ g.fas_user.email | avatar_url(16) }}",
+                    commit_id: _commit_id,
+                    line: _line,
+                }
+                process_event(
+                    data,
+                    "{{ request.uid }}",
+                    "{{ g.fas_user.username if g.authenticated or '' }}");
+                set_ui_for_comment(false);
+                return false;
+              }
+            });
+            return false;
+        } else {
+            set_ui_for_comment(false);
+        }
       } else {
         // Make the browser submit the form sync
         form.submit();
@@ -1637,8 +1679,6 @@ $(document).on("mouseleave", "td.cell2", function() {
 
 
 {% if config['EVENTSOURCE_SOURCE'] and pull_request %}
-<script type="text/javascript"
-  src="{{ url_for('static', filename='request_ev.js') }}"></script>
 
 <script type="text/javascript">
 var source = null;

From f2ab7521aa942645388ae6a6841cb1ec3817b62d Mon Sep 17 00:00:00 2001
From: Pierre-Yves Chibon <pingou@pingoured.fr>
Date: Mar 29 2018 17:23:39 +0000
Subject: [PATCH 3/3] Do not show the delete, reply or update buttons if there is not comment_id


This is the case when the comment are shown in the UI without going
through the SSE (for example because the SSE is overloaded or simply
down).

Signed-off-by: Pierre-Yves Chibon <pingou@pingoured.fr>

---

diff --git a/pagure/static/request_ev.js b/pagure/static/request_ev.js
index 58219c5..4f5aa43 100644
--- a/pagure/static/request_ev.js
+++ b/pagure/static/request_ev.js
@@ -1,12 +1,16 @@
 add_comment = function(data, username) {
   console.log('Adding comment ' + data.comment_added);
   var field = $('#comments');
-  var edit_btn = '<a class="btn btn-secondary btn-sm edit_btn" \
+
+  var edit_btn = ''
+  if (data.request_id && data.project){
+    edit_btn = '<a class="btn btn-secondary btn-sm edit_btn" \
         href="/' + data.project + '/pull-request/' + data.request_id + '/comment/' + data.comment_id + '/edit" \
         data-comment="' + data.comment_id + '" \
         data-objid="' + data.request_id + '"> \
         <span class="oi" data-glyph="pencil"></span> \
     </a>';
+  }
   var inline = false;
   if (data.commit_id){
     inline = true;
@@ -43,12 +47,15 @@ add_comment = function(data, username) {
         </div> \
         </section> \
         <div class="issue_actions m-t-2"> \
-        <aside class="btn-group issue_action icon pull-xs-right p-b-1"> \
-            <div class="btn-group" role="group" aria-label="Basic example"> \
+          <aside class="btn-group issue_action icon pull-xs-right p-b-1"> \
+          <div class="btn-group" role="group" aria-label="Basic example">';
+        if (data.comment_id) {
+            _data += '\
               <a class="reply btn btn-secondary btn-sm" data-toggle="tooltip" title="Reply to this comment - lose formatting"> \
                 <span class="oi" data-glyph="share-boxed"></span> \
               </a>';
-        if ( data.comment_user == username) {
+        }
+        if ( data.comment_user == username && data.comment_id) {
           _data += edit_btn +
               '<button class="btn btn-secondary btn-sm" type="submit" name="drop_comment" value="' + data.comment_id + '" \
                   onclick="return confirm(\'Do you really want to remove this comment?\');" \
@@ -69,10 +76,13 @@ add_comment = function(data, username) {
             <img class="avatar circle" src="' + data.avatar_url + '"/> \
             <a href="/user/' + data.comment_user + '"> ' + data.comment_user + '\
               </a> \
-              <a class="headerlink pull-xs-right" title="Permalink to this headline" \
-                href="#comment-' + data.comment_id + '"> \
+              <a class="headerlink pull-xs-right" title="Permalink to this headline"';
+            if (data.comment_id) {
+                _data += 'href="#comment-' + data.comment_id + '">';
+            }
+            _data += '\
                 <span title="">seconds ago</span> \
-               </a>\
+               </a> \
             </div>\
             <div class="card-block">\
               <section class="issue_comment"> \
@@ -84,11 +94,14 @@ add_comment = function(data, username) {
               </section> \
               <div class="issue_actions m-t-2"> \
                 <div class="issue_action icon pull-xs-right p-b-1"> \
-                  <div class="btn-group" role="group" aria-label="Basic example"> \
+                  <div class="btn-group" role="group" aria-label="Basic example">';
+            if (data.comment_id) {
+                _data += '\
                     <a class="reply btn btn-secondary btn-sm" data-toggle="tooltip" title="Reply to this comment - lose formatting"> \
                       <span class="oi" data-glyph="share-boxed"></span> \
                     </a>';
-            if ( data.comment_user == username) {
+            }
+            if ( data.comment_user == username && data.comment_id) {
                 _data += edit_btn +
                     '<button class="btn btn-secondary btn-sm" type="submit" name="drop_comment" value="' + data.comment_id + '" \
                         onclick="return confirm(\'Do you really want to remove this comment?\');" \

