You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@whimsical.apache.org by Sam Ruby <ru...@apache.org> on 2015/12/30 20:25:02 UTC

[whimsy.git] [2/2] Commit 7c7e82f: match signature to selected if there are only two attachments

Commit 7c7e82fe626b783b483ca78cbc5149638085b181:
    match signature to selected if there are only two attachments
    in the process, consolidate the logic to find the signature file into one
    place.


Branch: refs/heads/secmail
Author: Sam Ruby <ru...@intertwingly.net>
Committer: Sam Ruby <ru...@intertwingly.net>
Pusher: rubys <ru...@apache.org>

------------------------------------------------------------
www/secmail/views/check-signature.js.rb                      | +++++++ -
www/secmail/views/parts.js.rb                                | +++++ ------
------------------------------------------------------------
42 changes: 33 additions, 9 deletions.
------------------------------------------------------------


diff --git a/www/secmail/views/check-signature.js.rb b/www/secmail/views/check-signature.js.rb
index 0e1e611..a2bae6b 100644
--- a/www/secmail/views/check-signature.js.rb
+++ b/www/secmail/views/check-signature.js.rb
@@ -1,3 +1,7 @@
+#
+# Check signatures for validity using gpg on the server
+#
+
 class CheckSignature < React
   def initialize
     @signature = nil
@@ -15,9 +19,7 @@ def componentDidMount()
   end
 
   def componentWillReceiveProps()
-    @signature = @@attachments.find {|attachment|
-      attachment == @@selected + '.asc' or attachment == @@selected + '.sig'
-    }
+    @signature = CheckSignature.find(@@selected, @@attachments)
 
     if @signature and @signature != @checked
       @flag = 'alert-info'
@@ -46,4 +48,27 @@ def componentWillReceiveProps()
       }
     end
   end
+
+  # find signature file that matches the selected attachment from the list
+  # of attachments
+  def self.find(selected, attachments)
+    return unless selected
+
+    # first look for a signature that matches this selected file
+    signature = attachments.find {|attachment|
+      attachment == selected + '.asc' or attachment == selected + '.sig'
+    }
+
+    # if no exact match, look closer at the other attachment if there
+    # are exactly two attachments
+    if not signature and attachments.length == 2
+      signature = attachments.find {|attachment| attachment != selected}
+
+      unless signature.end_with? '.asc' or signature.end_with? '.sig'
+        signature = nil
+      end
+    end
+
+    return signature
+  end
 end
diff --git a/www/secmail/views/parts.js.rb b/www/secmail/views/parts.js.rb
index ff370d4..86eac31 100644
--- a/www/secmail/views/parts.js.rb
+++ b/www/secmail/views/parts.js.rb
@@ -31,13 +31,16 @@ def render
       onClick: self.select
     }
 
+    # locate corresponding signature file (if any)
+    signature = CheckSignature.find(@selected, @attachments)
+
     # list of attachments
     _ul @attachments, ref: 'attachments' do |attachment|
       if attachment == @drag
         options[:className] = 'dragging'
       elsif attachment == @selected
         options[:className] = 'selected'
-      elsif attachment == @selected + '.asc' or attachment == @selected + '.sig'
+      elsif attachment == signature
         options[:className] = 'signature'
       else
         options[:className] = nil
@@ -256,11 +259,7 @@ def submit(event)
     end
 
     # add signature (if present)
-    @attachments.each do |attachment|
-      if attachment == @selected + '.asc' or attachment == @selected + '.sig'
-        data.signature = attach
-      end
-    end
+    data.signature = CheckSignature.find(@selected, @attachments)
 
     # submit HTTP post request
     @busy = true