From 4798308ccda7d300d1b62102d2f6c26e674fb3c9 Mon Sep 17 00:00:00 2001 From: Kevin Froman Date: Sun, 3 Mar 2019 00:26:55 -0600 Subject: [PATCH] work on mail ui deletion of items --- .../default-plugins/pms/mailapi.py | 30 +++++++++-- .../static-data/default-plugins/pms/main.py | 17 ------ .../default-plugins/pms/sentboxdb.py | 16 +++++- onionr/static-data/www/mail/mail.js | 54 ++++++++++++++++--- 4 files changed, 85 insertions(+), 32 deletions(-) diff --git a/onionr/static-data/default-plugins/pms/mailapi.py b/onionr/static-data/default-plugins/pms/mailapi.py index 7ef49bb4..cdc77094 100644 --- a/onionr/static-data/default-plugins/pms/mailapi.py +++ b/onionr/static-data/default-plugins/pms/mailapi.py @@ -17,11 +17,11 @@ You should have received a copy of the GNU General Public License along with this program. If not, see . ''' -import sys, os -from flask import Response, request, redirect, Blueprint +import sys, os, json +from flask import Response, request, redirect, Blueprint, abort import core sys.path.insert(0, os.path.dirname(os.path.realpath(__file__))) -import loadinbox +import loadinbox, sentboxdb flask_blueprint = Blueprint('mail', __name__) c = core.Core() @@ -29,7 +29,8 @@ kv = c.keyStore @flask_blueprint.route('/mail/deletemsg/', methods=['POST']) def mail_delete(block): - assert c._utils.validateHash(block) + if not c._utils.validateHash(block): + abort(504) existing = kv.get('deleted_mail') if existing is None: existing = [] @@ -40,4 +41,23 @@ def mail_delete(block): @flask_blueprint.route('/mail/getinbox') def list_inbox(): - return ','.join(loadinbox.load_inbox(c)) \ No newline at end of file + return ','.join(loadinbox.load_inbox(c)) + +@flask_blueprint.route('/mail/getsentbox') +def list_sentbox(): + sentbox_list = sentboxdb.SentBox(c).listSent() + sentbox_list_copy = list(sentbox_list) + deleted = kv.get('deleted_mail') + if deleted is None: + deleted = [] + for x in range(len(sentbox_list_copy)): + if sentbox_list_copy[x]['hash'] in deleted: + sentbox_list.pop(x) + + ''' + hash_list = [] + for x in sentbox_list: + hash_list.append({x['hash']) + return ','.join(hash_list) + ''' + return json.dumps(sentbox_list) \ No newline at end of file diff --git a/onionr/static-data/default-plugins/pms/main.py b/onionr/static-data/default-plugins/pms/main.py index 5af4a828..5c558a55 100755 --- a/onionr/static-data/default-plugins/pms/main.py +++ b/onionr/static-data/default-plugins/pms/main.py @@ -307,23 +307,6 @@ def on_insertblock(api, data={}): meta = json.loads(data['meta']) sentboxTools.addToSent(data['hash'], data['peer'], data['content'], meta['subject']) -def on_pluginrequest(api, data=None): - resp = '' - subject = '' - recip = '' - message = '' - postData = {} - blockID = '' - sentboxTools = sentboxdb.SentBox(api.get_core()) - keyStore = api.get_core().keyStore - if data['name'] == 'mail': - path = data['path'] - print(cmd) - cmd = path.split('/')[1] - if cmd == 'sentbox': - resp = OnionrMail(api).get_sent_list(display=False) - if resp != '': - api.get_onionr().clientAPIInst.pluginResponses[data['pluginResponse']] = resp def on_init(api, data = None): ''' diff --git a/onionr/static-data/default-plugins/pms/sentboxdb.py b/onionr/static-data/default-plugins/pms/sentboxdb.py index f00accb3..28a5de6f 100755 --- a/onionr/static-data/default-plugins/pms/sentboxdb.py +++ b/onionr/static-data/default-plugins/pms/sentboxdb.py @@ -25,10 +25,15 @@ class SentBox: self.dbLocation = mycore.dataDir + 'sentbox.db' if not os.path.exists(self.dbLocation): self.createDB() - self.conn = sqlite3.connect(self.dbLocation) - self.cursor = self.conn.cursor() self.core = mycore return + + def connect(self): + self.conn = sqlite3.connect(self.dbLocation) + self.cursor = self.conn.cursor() + + def close(self): + self.conn.close() def createDB(self): conn = sqlite3.connect(self.dbLocation) @@ -42,22 +47,29 @@ class SentBox: ); ''') conn.commit() + conn.close() return def listSent(self): + self.connect() retData = [] for entry in self.cursor.execute('SELECT * FROM sent;'): retData.append({'hash': entry[0], 'peer': entry[1], 'message': entry[2], 'subject': entry[3], 'date': entry[4]}) + self.close() return retData def addToSent(self, blockID, peer, message, subject=''): + self.connect() args = (blockID, peer, message, subject, self.core._utils.getEpoch()) self.cursor.execute('INSERT INTO sent VALUES(?, ?, ?, ?, ?)', args) self.conn.commit() + self.close() return def removeSent(self, blockID): + self.connect() args = (blockID,) self.cursor.execute('DELETE FROM sent where hash=?', args) self.conn.commit() + self.close() return diff --git a/onionr/static-data/www/mail/mail.js b/onionr/static-data/www/mail/mail.js index 81350907..1629f3d3 100755 --- a/onionr/static-data/www/mail/mail.js +++ b/onionr/static-data/www/mail/mail.js @@ -71,6 +71,7 @@ function setActiveTab(tabName){ threadPart.innerHTML = "" switch(tabName){ case 'inbox': + refreshPms() getInbox() break case 'sentbox': @@ -82,6 +83,17 @@ function setActiveTab(tabName){ } } +function deleteMessage(bHash){ + fetch('/mail/deletemsg/' + bHash, { + "method": "post", + headers: { + "token": webpass + }}) + .then((resp) => resp.text()) // Transform the data into json + .then(function(resp) { + }) +} + function loadInboxEntries(bHash){ fetch('/getblockheader/' + bHash, { headers: { @@ -96,6 +108,7 @@ function loadInboxEntries(bHash){ var subjectLine = document.createElement('span') var dateStr = document.createElement('span') var validSig = document.createElement('span') + var deleteBtn = document.createElement('button') var humanDate = new Date(0) var metadata = resp['metadata'] humanDate.setUTCSeconds(resp['meta']['time']) @@ -119,6 +132,8 @@ function loadInboxEntries(bHash){ entry.setAttribute('data-pubkey', resp['meta']['signer']) senderInput.readOnly = true dateStr.innerText = humanDate.toString() + deleteBtn.innerText = 'X' + deleteBtn.classList.add('dangerBtn', 'deleteBtn') if (metadata['subject'] === undefined || metadata['subject'] === null) { subjectLine.innerText = '()' } @@ -127,6 +142,7 @@ function loadInboxEntries(bHash){ } //entry.innerHTML = 'sender ' + resp['meta']['signer'] + ' - ' + resp['meta']['time'] threadPart.appendChild(entry) + entry.appendChild(deleteBtn) entry.appendChild(bHashDisplay) entry.appendChild(senderInput) entry.appendChild(validSig) @@ -134,9 +150,17 @@ function loadInboxEntries(bHash){ entry.appendChild(dateStr) entry.classList.add('threadEntry') - entry.onclick = function(){ + entry.onclick = function(event){ + if (event.target.classList.contains('deleteBtn')){ + return + } openThread(entry.getAttribute('data-hash'), senderInput.value, dateStr.innerText, resp['meta']['validSig'], entry.getAttribute('data-pubkey')) } + + deleteBtn.onclick = function(){ + entry.parentNode.removeChild(entry); + deleteMessage(entry.getAttribute('data-hash')) + } }.bind(bHash)) } @@ -160,7 +184,7 @@ function getInbox(){ } function getSentbox(){ - fetch('/apipoints/mail/sentbox', { + fetch('/mail/getsentbox', { headers: { "token": webpass }}) @@ -180,16 +204,27 @@ function getSentbox(){ toLabel.innerText = 'To: ' var toEl = document.createElement('input') var preview = document.createElement('span') + var deleteBtn = document.createElement('button') + deleteBtn.classList.add('deleteBtn', 'dangerBtn') + deleteBtn.innerText = 'X' toEl.readOnly = true - toEl.value = resp[keys[i]][1] - preview.innerText = '(' + resp[keys[i]][2] + ')' + toEl.value = resp[i]['peer'] + preview.innerText = '(' + resp[i]['subject'] + ')' + entry.setAttribute('data-hash', resp[i]['hash']) + entry.appendChild(deleteBtn) entry.appendChild(toLabel) entry.appendChild(toEl) entry.appendChild(preview) - entryUsed = resp[keys[i]] + entryUsed = resp[i]['message'] entry.onclick = function(){ console.log(resp) - showSentboxWindow(toEl.value, entryUsed[0]) + if (! entry.target.classList.contains('deleteBtn')){ + showSentboxWindow(toEl.value, entryUsed) + } + } + deleteBtn.onclick = function(){ + entry.parentNode.removeChild(entry); + deleteMessage(entry.getAttribute('data-hash')) } threadPart.appendChild(entry) } @@ -203,6 +238,7 @@ function showSentboxWindow(to, content){ overlay('sentboxDisplay') } +function refreshPms(){ fetch('/mail/getinbox', { headers: { "token": webpass @@ -210,8 +246,8 @@ fetch('/mail/getinbox', { .then((resp) => resp.text()) // Transform the data into json .then(function(data) { pms = data.split(',') - setActiveTab('inbox') }) +} tabBtns.onclick = function(event){ var children = tabBtns.children @@ -273,4 +309,6 @@ fetch('/friends/list', { //friendSelectParent //alert(resp[keys[i]]['name']) } -}) \ No newline at end of file +}) + +setActiveTab('inbox') \ No newline at end of file