Skip to content

Commit 04eae1e

Browse files
milhnlguludo
authored andcommitted
Add option to remove obsolete drafts
(cherry picked from commit 391f02c) This is a cherry-pick from pazz#1627.
1 parent 4d6b24c commit 04eae1e

File tree

5 files changed

+67
-9
lines changed

5 files changed

+67
-9
lines changed

alot/commands/envelope.py

Lines changed: 53 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -112,6 +112,48 @@ async def apply(self, ui):
112112
await ui.apply_command(globals.PromptCommand(cmdstring))
113113

114114

115+
async def delete_previous_draft(envelope, ui):
116+
try:
117+
if envelope.previous_draft is None:
118+
return
119+
except AttributeError:
120+
return
121+
if settings.get('envelope_always_delete_old_drafts'):
122+
del_old_draft = True
123+
else:
124+
msg = 'Do you want to delete the old draft?'
125+
del_old_draft = (await ui.choice(msg, cancel='no',
126+
msg_position='left'))
127+
del_old_draft = (del_old_draft == 'yes')
128+
if del_old_draft:
129+
try:
130+
message_path = envelope.previous_draft.get_filename()
131+
except Exception as e:
132+
logging.error(e)
133+
ui.notify('could not get draft path:\n%s' % e,
134+
priority='error', block=True)
135+
return
136+
137+
try:
138+
ui.dbman.remove_message(envelope.previous_draft)
139+
await ui.apply_command(globals.FlushCommand())
140+
except DatabaseError as e:
141+
logging.error(e)
142+
ui.notify('could not remove draft from db:\n%s' % e,
143+
priority='error', block=True)
144+
return
145+
146+
try:
147+
os.unlink(message_path)
148+
except OSError as e:
149+
logging.error(e)
150+
ui.notify('could not delete draft file:\n%s' % e,
151+
priority='error', block=True)
152+
return
153+
154+
envelope.previous_draft = None
155+
156+
115157
@registerCommand(MODE, 'save')
116158
class SaveCommand(Command):
117159
"""save draft"""
@@ -138,23 +180,26 @@ async def apply(self, ui):
138180
path = account.store_draft_mail(
139181
mail.as_string(policy=email.policy.SMTP, maxheaderlen=sys.maxsize))
140182

141-
msg = 'draft saved successfully'
142-
143183
# add mail to index if maildir path available
144184
if path is not None:
145-
ui.notify(msg + ' to %s' % path)
185+
ui.notify('draft saved successfully to %s' % path)
146186
logging.debug('adding new mail to index')
147187
try:
148188
ui.dbman.add_message(path, account.draft_tags + envelope.tags)
149189
await ui.apply_command(globals.FlushCommand())
150-
await ui.apply_command(commands.globals.BufferCloseCommand())
151190
except DatabaseError as e:
152191
logging.error(str(e))
153192
ui.notify('could not index message:\n%s' % str(e),
154193
priority='error',
155194
block=True)
156-
else:
157-
await ui.apply_command(commands.globals.BufferCloseCommand())
195+
return
196+
197+
await delete_previous_draft(envelope, ui)
198+
199+
# strip the outside '<' and '>' characters from the id
200+
mid = mail['Message-ID'][1:-1]
201+
envelope.previous_draft = ui.dbman.get_message(mid)
202+
await ui.apply_command(commands.globals.BufferCloseCommand())
158203

159204

160205
@registerCommand(MODE, 'send')
@@ -315,6 +360,8 @@ async def apply(self, ui):
315360
ui.dbman.add_message(path, account.sent_tags + initial_tags)
316361
await ui.apply_command(globals.FlushCommand())
317362

363+
await delete_previous_draft(self.envelope, ui)
364+
318365

319366
@registerCommand(MODE, 'edit', arguments=[
320367
(['--spawn'], {'action': cargparse.BooleanAction, 'default': None,

alot/commands/thread.py

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -469,9 +469,15 @@ async def apply(self, ui):
469469
tags.difference_update({'inbox', 'sent', 'draft', 'killed', 'replied',
470470
'signed', 'encrypted', 'unread', 'attachment'})
471471
tags = list(tags)
472+
473+
draft = None
474+
if 'draft' in self.message.get_tags():
475+
draft = self.message
476+
472477
# set body text
473478
mailcontent = self.message.get_body_text()
474-
envelope = Envelope(bodytext=mailcontent, tags=tags)
479+
envelope = Envelope(bodytext=mailcontent, tags=tags,
480+
previous_draft=draft)
475481

476482
# copy selected headers
477483
to_copy = ['Subject', 'From', 'To', 'Cc', 'Bcc', 'In-Reply-To',

alot/db/envelope.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ class Envelope:
5454
def __init__(
5555
self, template=None, bodytext=None, headers=None, attachments=None,
5656
sign=False, sign_key=None, encrypt=False, tags=None, replied=None,
57-
passed=None, account=None):
57+
passed=None, account=None, previous_draft=None):
5858
"""
5959
:param template: if not None, the envelope will be initialised by
6060
:meth:`parsing <parse_template>` this string before
@@ -92,6 +92,7 @@ def __init__(
9292
self.tags = tags or [] # tags to add after successful sendout
9393
self.replied = replied # message being replied to
9494
self.passed = passed # message being passed on
95+
self.previous_draft = previous_draft
9596
self.sent_time = None
9697
self.modified_since_sent = False
9798
self.sending = False # semaphore to avoid accidental double sendout

alot/db/manager.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -298,7 +298,7 @@ def _with_notmuch_message(self, mid):
298298
with Database(path=self.path, mode=Database.MODE.READ_ONLY,
299299
config=self.config) as db:
300300
try:
301-
yield db.find_message(mid)
301+
yield db.find(mid)
302302
except:
303303
errmsg = 'no message with id %s exists!' % mid
304304
raise NonexistantObjectError(errmsg)

alot/defaults/alot.rc.spec

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -268,6 +268,10 @@ auto_replyto_mailinglist = boolean(default=False)
268268
# prefer plaintext alternatives over html content in multipart/alternative
269269
prefer_plaintext = boolean(default=False)
270270

271+
# When sending an email or saving a new draft, always delete the old draft.
272+
# If set to False, the user will be prompted every time.
273+
envelope_always_delete_old_drafts = boolean(default=False)
274+
271275
# always edit the given body text alternative when editing outgoing messages in envelope mode.
272276
# alternative, and not the html source, even if that is currently displayed.
273277
# If unset, html content will be edited unless the current envelope shows the plaintext alternative.

0 commit comments

Comments
 (0)