Skip to content

Commit

Permalink
chore: Give each task notification an unique id
Browse files Browse the repository at this point in the history
close #191
  • Loading branch information
alexhua committed Mar 28, 2024
1 parent 580f17f commit df4b8c5
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 11 deletions.
28 changes: 18 additions & 10 deletions background.js
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,7 @@ async function send2Aria(downloadItem, rpcItem) {
});

if (Configs.allowNotification) {
const data = { method: "aria2.onExportSuccess", source: aria2 };
const data = { method: "aria2.onExportSuccess", source: aria2, gid: response.result };
if (!downloadItem.filename)
downloadItem.filename = Utils.getFileNameFromUrl(downloadItem.url);
data.contextMessage = Utils.formatFilepath(options.dir) + downloadItem.filename;
Expand Down Expand Up @@ -854,14 +854,21 @@ function initRemoteAria2() {
}
}

/**
* @param {object} data received event
* @param {string} data.method event name (required)
* @param {string} data.contextMessage context message to notify
* @param {string} data.gid corresponding task gid
* @param {Aria2} data.source the event source
*/
async function notifyTaskStatus(data) {
const events = ["aria2.onDownloadComplete", "aria2.onBtDownloadComplete",
"aria2.onDownloadError", "aria2.onExportSuccess", "aria2.onExportError"];

if (!data || !events.includes(data.method)) return;

const aria2 = data.source;
const gid = data.params?.length ? data.params[0]["gid"] : '';
const gid = data.params?.length ? data.params[0]["gid"] : data.gid ?? '';
let message = data.method;
let contextMessage = data.contextMessage ?? '';
if (!contextMessage && gid) {
Expand All @@ -887,43 +894,44 @@ async function notifyTaskStatus(data) {
}
}

let title = "taskNotification";
let nid = NID_TASK_STOPPED + gid;
let sign = '';
let title = "taskNotification", sign = '', nid = '';
switch (message) {
case "aria2.onDownloadStart":
message = "DownloadStart";
sign = ' ⬇️';
nid = NID_DEFAULT;
nid = NID_DEFAULT + gid;
break;
case "aria2.onDownloadComplete":
message = "DownloadComplete";
sign = ' ✅';
nid = NID_TASK_STOPPED + gid;
break;
case "aria2.onSeedingComplete":
message = "SeedingOver";
sign = ' ⬆️✅';
nid = NID_DEFAULT;
sign = ' ⬆️ ✅';
nid = NID_DEFAULT + gid;
break;
case "aria2.onBtDownloadComplete":
message = "DownloadComplete";
sign = ' ✅';
nid = NID_TASK_STOPPED + gid;
break;
case "aria2.onDownloadError":
message = "DownloadError";
sign = ' ❌';
nid = NID_TASK_STOPPED + gid;
break;
case "aria2.onExportSuccess":
title = "ExportSucceedStr"
message = "ExportSucceedDes";
sign = ' ⬇️';
nid = NID_DEFAULT;
nid = NID_DEFAULT + gid;
break;
case "aria2.onExportError":
title = "ExportFailedStr";
message = "ExportFailedDes";
sign = ' ❌';
nid = NID_DEFAULT;
nid = NID_DEFAULT + Aria2.RequestId;
break;
}
if (message) {
Expand Down
6 changes: 6 additions & 0 deletions js/aria2.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,12 @@ const DEFAULT_ARIA2 = { name: "Aria2", rpcUrl: "http://localhost:6800/jsonrpc",
class Aria2 {
static RequestId = 0;

/**
* @param {object} Aria2
* @param {string} Aria2.name
* @param {string} Aria2.rpcUrl
* @param {string} Aria2.secretKey
*/
constructor(aria2 = DEFAULT_ARIA2) {
Object.assign(this, aria2);
this._isLocalhost = Utils.isLocalhost(this.rpcUrl);
Expand Down
2 changes: 1 addition & 1 deletion manifest.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "__MSG_appName__",
"short_name": "A2E",
"version": "2.4.1",
"version": "2.4.2",
"manifest_version": 3,
"minimum_chrome_version": "100.0.0",
"default_locale": "en",
Expand Down

0 comments on commit df4b8c5

Please sign in to comment.