Skip to content

Commit

Permalink
Restore schedule as popup defaults when editing a message
Browse files Browse the repository at this point in the history
  • Loading branch information
jph-sendlater committed May 7, 2021
1 parent 472dd63 commit f626041
Show file tree
Hide file tree
Showing 12 changed files with 302 additions and 91 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
Send Later Thunderbird extension
Send Later Thunderbird extension
============================

### Schedule email messages to be sent later
Expand Down
51 changes: 33 additions & 18 deletions background.js
Original file line number Diff line number Diff line change
Expand Up @@ -1125,28 +1125,43 @@ messenger.windows.onCreated.addListener(async (window) => {
// Now, check if this message was an existing scheduled draft.
const originalMsg = await messenger.SL3U.findCurrentDraft();
if (originalMsg) {
let headers = await SendLater.extractCustomHeaders(originalMsg, ["x-send-later-at"]);
const headers = await SendLater.extractCustomHeaders(originalMsg, [
"x-send-later-at", "x-send-later-recur", "x-send-later-args",
"x-send-later-cancel-on-reply", "message-id"
]);
if (headers['x-send-later-at']) {
// Re-save the msg (delete existing schedule headers)
messenger.SL3U.goDoCommand("cmd_saveAsDraft").then(() => {
setTimeout(async () => {
// Alert the user about what just happened
let { preferences } = await browser.storage.local.get({ preferences: {} });
if (preferences.showEditAlert) {
messenger.windows.create({
url: "ui/draftSaveWarning.html",
type: "popup",
titlePreface: browser.i18n.getMessage("extensionName"),
height: 250,
width: 750
});
}
messenger.columnHandler.invalidateMessage(originalMsg.id);
}, 1000);
messenger.SL3U.goDoCommand("cmd_saveAsDraft", window.id).then(async () => {
// Alert the user about what just happened
let { preferences } = await browser.storage.local.get({ preferences: {} });
if (preferences.showEditAlert) {
messenger.windows.create({
url: "ui/draftSaveWarning.html",
type: "popup",
titlePreface: browser.i18n.getMessage("extensionName"),
height: 250,
width: 750
});
}

// Once the draft save operation is definitely complete,
// then invalidate that row with the columnHandler.
setTimeout(() => {
messenger.columnHandler.invalidateRowByMessageId(
headers["message-id"]
).catch(SLStatic.error);
}, 2000);
});

// TODO: Set popup scheduler defaults based on original schedule.

// Set popup scheduler defaults based on original message

browser.storage.local.get({ scheduleCache: {} }).then(
({ scheduleCache }) => {
scheduleCache[window.id] =
SLStatic.parseHeadersForPopupUICache(headers);
console.log(scheduleCache);
browser.storage.local.set({ scheduleCache });
}).catch(SLStatic.error);
} // else: not queued by send later.
} // else: Not editing a saved message.
}
Expand Down
9 changes: 6 additions & 3 deletions experiments/customColumnImplementation.js
Original file line number Diff line number Diff line change
Expand Up @@ -61,12 +61,15 @@ class MessageViewsCustomColumn {

async invalidateMessage(messageId) {
if (this.msgTracker.has(messageId)) {
console.log(`Invalidating message: ${messageId}`);
let treerow = this.msgTracker.get(messageId);
this.msgTracker.delete(messageId);
for (let window of Services.wm.getEnumerator("mail:3pane")) {
if (window.gDBView)
window.gDBView.NoteChange(treerow.rowid, 1, 2);
}
} else {
console.warn(`Tree row cannot be invalidated for message: ${messageId}`);
}
}

Expand Down Expand Up @@ -230,10 +233,10 @@ var columnHandler = class extends ExtensionCommon.ExtensionAPI {
columns.delete(name);
},

async invalidateMessage(id) {
let msgHdr = context.extension.messageManager.get(id);
async invalidateRowByMessageId(msgIdHeader) {
let id = msgIdHeader.replace('<','').replace('>','');
for (let column of columns.values()) {
column.invalidateMessage(msgHdr.messageId);
column.invalidateMessage(id);
}
},

Expand Down
6 changes: 3 additions & 3 deletions experiments/customColumnSchema.json
Original file line number Diff line number Diff line change
Expand Up @@ -60,14 +60,14 @@
"parameters": []
},
{
"name": "invalidateMessage",
"name": "invalidateRowByMessageId",
"type": "function",
"description": "",
"async": true,
"parameters": [
{
"name": "id",
"type": "integer"
"name": "msgIdHeader",
"type": "string"
}
]
},
Expand Down
13 changes: 10 additions & 3 deletions experiments/sl3u.js
Original file line number Diff line number Diff line change
Expand Up @@ -1079,9 +1079,16 @@ var SL3U = class extends ExtensionCommon.ExtensionAPI {
return false;
},

async goDoCommand(command) {
const cw = Services.wm.getMostRecentWindow("msgcompose");
cw.goDoCommand(command);
async goDoCommand(command, windowId) {
let window;
if (windowId) {
let wm = context.extension.windowManager.get(windowId, context);
window = wm.window;
} else {
window = Services.wm.getMostRecentWindow("msgcompose");
}
console.log(window);
window.goDoCommand(command);
},

async builtInSendLater() {
Expand Down
6 changes: 6 additions & 0 deletions experiments/sl3u.json
Original file line number Diff line number Diff line change
Expand Up @@ -186,6 +186,12 @@
"name": "command",
"type": "string",
"description": "Command to execute in current compose window."
},
{
"name": "windowId",
"type": "integer",
"optional": true,
"description": "In which window to execute this command."
}
]
},
Expand Down
6 changes: 3 additions & 3 deletions test/formatrecurtests.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,9 @@ exports.init = function() {

const tests = [
["none", ""],
["none between 730 1930", "betw. 7:30 and 19:30"],
["none on 1 3 5", "Only on Monday, Wednesday, and Friday"],
["none between 100 600 on 1 3 6", "betw. 1:00 and 6:00 Only on Monday, Wednesday, and Saturday"],
["none between 730 1930", ""],
["none on 1 3 5", ""],
["none between 100 600 on 1 3 6", ""],
["minutely", "Recur minutely"],
["daily", "Recur daily"],
["weekly", "Recur weekly"],
Expand Down
71 changes: 71 additions & 0 deletions test/headers_to_dom_element_tests.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
exports.init = function() {
function popupDOMTest(headers, expected) {
const result = SLStatic.parseHeadersForPopupUICache(headers);
return (DeepCompare(result, expected) ||
`expected ${ObjToStr(expected)}", got "${ObjToStr(result)}"`);
}

SLTests.AddTest("popupCacheTest simple-norecur", popupDOMTest, [
{
'x-send-later-at': '2021-01-22T15:31',
'x-send-later-recur': 'none'
},
{
'send-datetime': "1/22/2021, 3:31 PM",
'once': true,
'minutely': false,
'daily': false,
'weekly': false,
'monthly': false,
'yearly': false,
'function': false
}
]);

SLTests.AddTest("popupDOMTest recur every 3 days", popupDOMTest, [
{
'x-send-later-at': 'Fri, 22 Jan 2021 15:31:00 -0800',
'x-send-later-recur': 'daily / 3'
},
{
'send-datetime': "1/22/2021, 3:31 PM",
'once': false,
'minutely': false,
'daily': true,
'weekly': false,
'monthly': false,
'yearly': false,
'function': false,
'recur-cancelonreply': false,
'recur-multiplier': 3,
'recur-function-args': "",
'sendbetween': false,
'sendon': false
}
]);

SLTests.AddTest("popupDOMTest recur every other month on the second Friday", popupDOMTest, [
{
'x-send-later-at': 'Fri, 22 Jan 2021 15:31:00 -0800',
'x-send-later-recur': 'monthly 6 2 / 2'
},
{
'send-datetime': "1/22/2021, 3:31 PM",
'once': false,
'minutely': false,
'daily': false,
'weekly': false,
'monthly': true,
'yearly': false,
'function': false,
'recur-cancelonreply': false,
'recur-multiplier': 2,
'recur-function-args': "",
'recur-monthly-byweek': true,
'recur-monthly-byweek-day': "6",
'recur-monthly-byweek-week': "2",
'sendbetween': false,
'sendon': false
}
]);
}
53 changes: 0 additions & 53 deletions test/nextrecurtests.js
Original file line number Diff line number Diff line change
@@ -1,57 +1,4 @@
exports.init = function() {
function DeepCompare(a, b) {
if (a === b) {
return true;
} else if (a && a.splice) {
if (b && b.splice) {
if (a.length != b.length) {
console.log(a, '!=', b);
return false;
}
for (let i = 0; i < a.length; i++) {
if (!DeepCompare(a[i], b[i])) {
console.log(a[i], '!=', b[i]);
return false;
}
}
return true;
}
console.log(a, '!=', b);
return false;
} else if (b && b.splice) {
console.log(a, '!=', b);
return false;
} else if (a && a.getTime) {
if (b && b.getTime) {
if (a.getTime() == b.getTime()) {
return true;
} else {
console.log(a, '!=', b);
return false;
}
} else {
console.log(a, '!=', b);
return false;
}
}
if (typeof a === "object" && typeof b === "object") {
const aKeys = [...Object.keys(a)];
const bKeys = [...Object.keys(b)];
if (DeepCompare(aKeys, bKeys)) {
for (let key of aKeys) {
if (!DeepCompare(a[key], b[key])) {
console.log(a[key], '!=', b[key]);
return false;
}
}
} else {
console.log(aKeys, '!=',bKeys);
return false;
}
return true;
}
return a == b;
}

async function NextRecurNormalTest(sendat, recur, now, expected) {
let result;
Expand Down
78 changes: 78 additions & 0 deletions test/run_tests.js
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,83 @@ global.SLTests = {
}
}

global.DeepCompare = (a, b) => {
if (a === b) {
return true;
} else if (a && a.splice) {
if (b && b.splice) {
if (a.length != b.length) {
console.log(a, '!=', b);
return false;
}
for (let i = 0; i < a.length; i++) {
if (!DeepCompare(a[i], b[i])) {
console.log(a[i], '!=', b[i]);
return false;
}
}
return true;
}
console.log(a, '!=', b);
return false;
} else if (b && b.splice) {
console.log(a, '!=', b);
return false;
} else if (a && a.getTime) {
if (b && b.getTime) {
if (a.getTime() == b.getTime()) {
return true;
} else {
console.log(a, '!=', b);
return false;
}
} else {
console.log(a, '!=', b);
return false;
}
}
if (typeof a === "object" && typeof b === "object") {
const aKeys = [...Object.keys(a)];
const bKeys = [...Object.keys(b)];
if (DeepCompare(aKeys, bKeys)) {
for (let key of aKeys) {
if (!DeepCompare(a[key], b[key])) {
console.log(a[key], '!=', b[key]);
return false;
}
}
} else {
console.log(aKeys, '!=',bKeys);
return false;
}
return true;
}
return a == b;
};

global.ObjToStr = (obj) => {
if (typeof obj === 'object') {
let contents = [];
for (let [key, value] of Object.entries(obj)) {
contents.push(`${key}: ${ObjToStr(value)}\n`);
}

if (contents.length === 1) {
return `{ ${contents[0].trim()} }`;
} else {
let str = "{";
for (let c of contents) {
str += `\n ${String(c.trim()).replace(/\n/g, '\n ')},`;
}
return str+'\n}';
}
} else if (typeof obj === 'string') {
return `"${obj}"`;
} else {
return ""+obj;
}
};

require('../utils/static.js');

const testPaths = [
Expand All @@ -49,6 +126,7 @@ const testPaths = [
"./nextrecurtests.js",
"./parserecurtests.js",
"./mimetests.js",
"./headers_to_dom_element_tests.js",
"./miscellaneoustests.js"
];

Expand Down
Loading

0 comments on commit f626041

Please sign in to comment.