mirror of
https://github.com/moparisthebest/mailcatcher
synced 2024-10-31 15:35:07 -04:00
Send deletes across websocket and handle in poll as well so deletes update in realtime
This commit is contained in:
parent
017e704791
commit
1c049fc071
@ -103,14 +103,8 @@ class MailCatcher
|
|||||||
url: "/messages/" + id
|
url: "/messages/" + id
|
||||||
type: "DELETE"
|
type: "DELETE"
|
||||||
success: =>
|
success: =>
|
||||||
messageRow = $("""#messages tbody tr[data-message-id="#{id}"]""")
|
# callback will delete, this gets us into race conditions...
|
||||||
switchTo = messageRow.next().data("message-id") || messageRow.prev().data("message-id")
|
#@deleteMessage [id]
|
||||||
messageRow.remove()
|
|
||||||
if switchTo
|
|
||||||
@loadMessage switchTo
|
|
||||||
else
|
|
||||||
@unselectMessage()
|
|
||||||
@updateMessagesCount()
|
|
||||||
|
|
||||||
error: ->
|
error: ->
|
||||||
alert "Error while removing message."
|
alert "Error while removing message."
|
||||||
@ -171,9 +165,8 @@ class MailCatcher
|
|||||||
else
|
else
|
||||||
@nextTab(i + 1)
|
@nextTab(i + 1)
|
||||||
|
|
||||||
haveMessage: (message) ->
|
haveMessage: (id) ->
|
||||||
message = message.id if message.id?
|
$("""#messages tbody tr[data-message-id="#{id}"]""").length > 0
|
||||||
$("""#messages tbody tr[data-message-id="#{message}"]""").length > 0
|
|
||||||
|
|
||||||
selectedMessage: ->
|
selectedMessage: ->
|
||||||
$("#messages tr.selected").data "message-id"
|
$("#messages tr.selected").data "message-id"
|
||||||
@ -198,6 +191,21 @@ class MailCatcher
|
|||||||
row.prependTo($("#messages tbody"))
|
row.prependTo($("#messages tbody"))
|
||||||
@updateMessagesCount()
|
@updateMessagesCount()
|
||||||
|
|
||||||
|
deleteMessage: (ids) ->
|
||||||
|
selectedId = @selectedMessage()
|
||||||
|
for id in ids
|
||||||
|
if @haveMessage id
|
||||||
|
messageRow = $("""#messages tbody tr[data-message-id="#{id}"]""")
|
||||||
|
if messageRow
|
||||||
|
if id == selectedId
|
||||||
|
switchTo = messageRow.next().data("message-id") || messageRow.prev().data("message-id")
|
||||||
|
messageRow.remove()
|
||||||
|
if switchTo && @haveMessage switchTo
|
||||||
|
@loadMessage switchTo
|
||||||
|
#else if selectedId
|
||||||
|
# @unselectMessage()
|
||||||
|
@updateMessagesCount()
|
||||||
|
|
||||||
scrollToRow: (row) ->
|
scrollToRow: (row) ->
|
||||||
relativePosition = row.offset().top - $("#messages").offset().top
|
relativePosition = row.offset().top - $("#messages").offset().top
|
||||||
if relativePosition < 0
|
if relativePosition < 0
|
||||||
@ -211,7 +219,7 @@ class MailCatcher
|
|||||||
$("#messages tbody, #message .metadata dd").empty()
|
$("#messages tbody, #message .metadata dd").empty()
|
||||||
$("#message .metadata .attachments").hide()
|
$("#message .metadata .attachments").hide()
|
||||||
$("#message iframe").attr("src", "about:blank")
|
$("#message iframe").attr("src", "about:blank")
|
||||||
null
|
true
|
||||||
|
|
||||||
loadMessage: (id) ->
|
loadMessage: (id) ->
|
||||||
id = id.id if id?.id?
|
id = id.id if id?.id?
|
||||||
@ -283,10 +291,18 @@ class MailCatcher
|
|||||||
|
|
||||||
refresh: ->
|
refresh: ->
|
||||||
$.getJSON "/messages", (messages) =>
|
$.getJSON "/messages", (messages) =>
|
||||||
|
all_msg_ids = []
|
||||||
$.each messages, (i, message) =>
|
$.each messages, (i, message) =>
|
||||||
unless @haveMessage message
|
id = message.id if message.id?
|
||||||
|
all_msg_ids.push(id)
|
||||||
|
unless @haveMessage id
|
||||||
@addMessage message
|
@addMessage message
|
||||||
@updateMessagesCount()
|
ids_to_remove = []
|
||||||
|
$.each $("#messages tbody tr[data-message-id]"), (i, row) =>
|
||||||
|
existing_id = $(row).data("message-id")
|
||||||
|
unless existing_id in all_msg_ids
|
||||||
|
ids_to_remove.push(existing_id)
|
||||||
|
@deleteMessage ids_to_remove
|
||||||
|
|
||||||
subscribe: ->
|
subscribe: ->
|
||||||
if WebSocket?
|
if WebSocket?
|
||||||
@ -299,7 +315,11 @@ class MailCatcher
|
|||||||
protocol = if secure then "wss" else "ws"
|
protocol = if secure then "wss" else "ws"
|
||||||
@websocket = new WebSocket("#{protocol}://#{window.location.host}/messages")
|
@websocket = new WebSocket("#{protocol}://#{window.location.host}/messages")
|
||||||
@websocket.onmessage = (event) =>
|
@websocket.onmessage = (event) =>
|
||||||
@addMessage $.parseJSON event.data
|
message = $.parseJSON event.data
|
||||||
|
if 'id' of message
|
||||||
|
@addMessage message
|
||||||
|
else
|
||||||
|
@deleteMessage message
|
||||||
|
|
||||||
subscribePoll: ->
|
subscribePoll: ->
|
||||||
unless @refreshInterval?
|
unless @refreshInterval?
|
||||||
|
@ -158,32 +158,43 @@ module MailCatcher::Mail extend self
|
|||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
def delete!
|
def delete_ids_query!(query, *bind_vars)
|
||||||
@delete_all_messages_query ||= db.prepare "DELETE FROM message"
|
ids ||= []
|
||||||
@delete_all_message_parts_query ||= db.prepare "DELETE FROM message_part"
|
query.execute(bind_vars).map do |row|
|
||||||
|
ids << row[0]
|
||||||
|
end
|
||||||
|
delete_ids!(ids)
|
||||||
|
end
|
||||||
|
|
||||||
@delete_all_messages_query.execute and
|
def delete_ids!(ids)
|
||||||
@delete_all_message_parts_query.execute
|
unless ids.empty?
|
||||||
|
id_query = ids.join(',')
|
||||||
|
|
||||||
|
db.execute("DELETE FROM message WHERE id IN (#{id_query})")
|
||||||
|
db.execute("DELETE FROM message_part WHERE id IN (#{id_query})")
|
||||||
|
|
||||||
|
EventMachine.next_tick do
|
||||||
|
MailCatcher::Events::MessageAdded.push ids
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
def delete!
|
||||||
|
@delete_all_messages_query ||= db.prepare "SELECT id FROM message"
|
||||||
|
delete_ids_query!(@delete_all_messages_query)
|
||||||
end
|
end
|
||||||
|
|
||||||
def delete_message!(message_id)
|
def delete_message!(message_id)
|
||||||
@delete_messages_query ||= db.prepare "DELETE FROM message WHERE id = ?"
|
delete_ids!([message_id.to_i])
|
||||||
@delete_message_parts_query ||= db.prepare "DELETE FROM message_part WHERE message_id = ?"
|
|
||||||
@delete_messages_query.execute(message_id) and
|
|
||||||
@delete_message_parts_query.execute(message_id)
|
|
||||||
end
|
end
|
||||||
|
|
||||||
def delete_messages_older_than!(modifier)
|
def delete_messages_older_than!(modifier)
|
||||||
@delete_messages_older_than_query ||= db.prepare "DELETE FROM message WHERE created_at < datetime('now', ?)"
|
@delete_messages_older_than_query ||= db.prepare "SELECT id FROM message WHERE created_at < datetime('now', ?)"
|
||||||
@delete_message_parts_older_than_query ||= db.prepare "DELETE FROM message_part WHERE created_at < datetime('now', ?)"
|
delete_ids_query!(@delete_messages_older_than_query, modifier)
|
||||||
@delete_messages_older_than_query.execute(modifier) and
|
|
||||||
@delete_message_parts_older_than_query.execute(modifier)
|
|
||||||
end
|
end
|
||||||
|
|
||||||
def delete_messages_keep!(keep_num_emails)
|
def delete_messages_keep!(keep_num_emails)
|
||||||
@delete_messages_query ||= db.prepare "DELETE FROM message WHERE id IN (SELECT id FROM message ORDER BY id DESC LIMIT -1 OFFSET ?);"
|
@delete_messages_older_than_query ||= db.prepare "SELECT id FROM message ORDER BY id DESC LIMIT -1 OFFSET ?"
|
||||||
@delete_message_parts_query ||= db.prepare "DELETE FROM message_part WHERE id NOT IN (SELECT id FROM message)"
|
delete_ids_query!(@delete_messages_older_than_query, keep_num_emails)
|
||||||
@delete_messages_query.execute(keep_num_emails) and
|
|
||||||
@delete_message_parts_query.execute()
|
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
Loading…
Reference in New Issue
Block a user