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/13 16:14:54 UTC
[whimsy.git] [24/37] Commit 13f865c: factor our post logic
Commit 13f865c8b1db18a1cb721ff1ca0c196749e093b5:
factor our post logic
Branch: refs/heads/secmail
Author: Sam Ruby <ru...@intertwingly.net>
Committer: Sam Ruby <ru...@intertwingly.net>
Pusher: rubys <ru...@apache.org>
------------------------------------------------------------
views/app.js.rb | ++
views/index.js.rb | +++ ------
views/index.json.rb | +++++++ -------
views/utils.js.rb | ++++++++++++
------------------------------------------------------------
176 changes: 126 additions, 50 deletions.
------------------------------------------------------------
diff --git a/views/app.js.rb b/views/app.js.rb
index 45d3bc1..6a6bd2f 100644
--- a/views/app.js.rb
+++ b/views/app.js.rb
@@ -1,2 +1,4 @@
+require_relative 'utils'
+
require_relative 'index'
require_relative 'parts'
diff --git a/views/index.js.rb b/views/index.js.rb
index cafe71c..a48480d 100644
--- a/views/index.js.rb
+++ b/views/index.js.rb
@@ -32,7 +32,7 @@ def render
end
_input.btn.btn_primary type: 'submit', value: 'fetch previous month',
- onClick: self.fetch
+ onClick: self.fetch_month
end
# initialize latest mailbox (year+month)
@@ -42,7 +42,7 @@ def componentWillMount()
# on initial load, fetch latest mailbox and subscribe to keyboard events
def componentDidMount()
- self.fetch()
+ self.fetch_month()
window.onkeydown = self.keydown
end
@@ -55,30 +55,17 @@ def componentDidUpdate()
end
# fetch a month's worth of messages
- def fetch()
- # build JSON post XMLHttpRequest
- xhr = XMLHttpRequest.new()
- xhr.open 'POST', "", true
- xhr.setRequestHeader 'Content-Type', 'application/json;charset=utf-8'
- xhr.responseType = 'json'
+ def fetch_month()
+ post('', mbox: @latest) do |response|
+ # update latest mbox
+ @latest = response.mbox if response.mbox
- # process response
- def xhr.onreadystatechange()
- if xhr.readyState == 4
- response = xhr.response.json
+ # add messages to list
+ @messages = @messages.concat(*response.messages)
- # update latest mbox
- @latest = response.mbox if response.mbox
-
- # add messages to list
- @messages = @messages.concat(*response.messages)
-
- # select oldest message
- @selected = @messages.last.href
- end
+ # select oldest message
+ @selected = @messages.last.href
end
-
- xhr.send(JSON.stringify mbox: @latest)
end
# handle keyboard events
diff --git a/views/index.json.rb b/views/index.json.rb
index b888473..77a3a9a 100644
--- a/views/index.json.rb
+++ b/views/index.json.rb
@@ -1,34 +1,32 @@
-_json do
- if @mbox =~ /^\d+$/
- # find indicated mailbox in the list of available mailboxes
- available = Dir["#{ARCHIVE}/*.yml"].sort
- index = available.find_index "#{ARCHIVE}/#{@mbox}.yml"
+if @mbox =~ /^\d+$/
+ # find indicated mailbox in the list of available mailboxes
+ available = Dir["#{ARCHIVE}/*.yml"].sort
+ index = available.find_index "#{ARCHIVE}/#{@mbox}.yml"
- # if found and not first, process it
- if index
- # fetch a list of headers for all messages in the maibox with attachments
- headers = Mailbox.new(@mbox).headers.to_a.select do |id, message|
- message[:attachments]
- end
-
- # extract relevant fields from the headers
- headers.map! do |id, message|
- {
- time: message[:time],
- href: "#{message[:source]}/#{id}/",
- from: message[:from],
- subject: message['Subject']
- }
- end
-
- # select previous mailbox
- mbox = available[index-1].untaint
+ # if found and not first, process it
+ if index
+ # fetch a list of headers for all messages in the maibox with attachments
+ headers = Mailbox.new(@mbox).headers.to_a.select do |id, message|
+ message[:attachments]
+ end
- # return mailbox name and messages
+ # extract relevant fields from the headers
+ headers.map! do |id, message|
{
- mbox: File.basename(mbox, '.yml'),
- messages: headers.sort_by {|message| message[:time]}.reverse
+ time: message[:time],
+ href: "#{message[:source]}/#{id}/",
+ from: message[:from],
+ subject: message['Subject']
}
end
+
+ # select previous mailbox
+ mbox = available[index-1].untaint
+
+ # return mailbox name and messages
+ {
+ mbox: File.basename(mbox, '.yml'),
+ messages: headers.sort_by {|message| message[:time]}.reverse
+ }
end
end
diff --git a/views/utils.js.rb b/views/utils.js.rb
new file mode 100644
index 0000000..f8db8eb
--- /dev/null
+++ b/views/utils.js.rb
@@ -0,0 +1,89 @@
+# "AJAX" style post request to the server, with a callback
+def post(target, data, &block)
+ xhr = XMLHttpRequest.new()
+ xhr.open('POST', target, true)
+ xhr.setRequestHeader('Content-Type', 'application/json;charset=utf-8')
+ xhr.responseType = 'text'
+
+ def xhr.onreadystatechange()
+ if xhr.readyState == 4
+ data = nil
+
+ begin
+ if xhr.status == 200
+ data = JSON.parse(xhr.responseText)
+ alert "Exception\n#{data.exception}" if data.exception
+ elsif xhr.status == 404
+ alert "Not Found: json/#{target}"
+ elsif xhr.status >= 400
+ console.log(xhr.response)
+ if not xhr.response
+ alert "Exception - #{xhr.statusText}"
+ elsif xhr.response.exception
+ alert "Exception\n#{xhr.response.exception}"
+ else
+ alert "Exception\n#{JSON.parse(xhr.responseText).exception}"
+ end
+ end
+ rescue => e
+ console.log(e)
+ end
+
+ block(data)
+ Main.refresh()
+ end
+ end
+
+ xhr.send(JSON.stringify(data))
+end
+
+# "AJAX" style get request to the server, with a callback
+def fetch(target, type, &block)
+ xhr = XMLHttpRequest.new()
+
+ def xhr.onreadystatechange()
+ if xhr.readyState == 1
+ clock_counter += 1
+ setTimeout(0) {Main.refresh()}
+ elsif xhr.readyState == 4
+ data = nil
+
+ begin
+ if xhr.status == 200
+ if type == :json
+ data = xhr.response || JSON.parse(xhr.responseText)
+ else
+ data = xhr.responseText
+ end
+ elsif xhr.status == 404
+ alert "Not Found: #{type}/#{target}"
+ elsif xhr.status >= 400
+ console.log(xhr.response)
+ if not xhr.response
+ alert "Exception - #{xhr.statusText}"
+ elsif xhr.response.exception
+ alert "Exception\n#{xhr.response.exception}"
+ else
+ alert "Exception\n#{JSON.parse(xhr.responseText).exception}"
+ end
+ end
+ rescue => e
+ console.log(e)
+ end
+
+ block(data)
+ clock_counter -= 1
+ Main.refresh()
+ end
+ end
+
+ if target =~ /^https?:/
+ xhr.open('GET', target, true)
+ xhr.setRequestHeader("Accept", "application/json") if type == :json
+ else
+ xhr.open('GET', "../#{type}/#{target}", true)
+ end
+ xhr.responseType = type
+ xhr.send()
+end
+