Skip to content

Commit 5e4f861

Browse files
hankypanky666josteink
authored andcommitted
add archive and delete actions for new gmail dataLayer (#475)
Add support for archive and delete-events in new gmail data-layer
1 parent 2dcd978 commit 5e4f861

File tree

4 files changed

+152
-9
lines changed

4 files changed

+152
-9
lines changed

src/gmail.js

Lines changed: 103 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -969,7 +969,7 @@ var Gmail = function(localJQuery) {
969969
api.check.data.is_thread_id = function(id) {
970970
return id
971971
&& typeof id === "string"
972-
&& /^thread-a:/.test(id);
972+
&& /^thread-[a|f]:/.test(id);
973973
};
974974

975975
api.check.data.is_thread = function(obj) {
@@ -982,7 +982,7 @@ var Gmail = function(localJQuery) {
982982
api.check.data.is_email_id = function(id) {
983983
return id
984984
&& typeof id === "string"
985-
&& /^msg-a:/.test(id);
985+
&& /^msg-[a|f]:/.test(id);
986986
};
987987

988988
api.check.data.is_email = function(obj) {
@@ -992,6 +992,14 @@ var Gmail = function(localJQuery) {
992992
&& api.check.data.is_email_id(obj["1"]);
993993
};
994994

995+
api.check.data.is_action = function(obj) {
996+
return obj
997+
&& obj["1"]
998+
&& Array.isArray(obj["1"])
999+
&& obj["1"].length === 1
1000+
&& typeof obj["1"]["0"] === 'string';
1001+
};
1002+
9951003
api.check.data.is_smartlabels_array = function(obj) {
9961004
const isNotArray = !obj || !Array.isArray(obj) ||obj.length === 0;
9971005
if (isNotArray) {
@@ -1021,7 +1029,32 @@ var Gmail = function(localJQuery) {
10211029

10221030
let str = obj.trim();
10231031
return ((str.startsWith("{") && str.endsWith("}"))
1024-
|| (str.startsWith("[") && str.endsWith("]")));
1032+
|| (str.startsWith("[") && str.endsWith("]")));
1033+
};
1034+
1035+
api.tools.get_thread_id = function(obj) {
1036+
return api.check.data.is_thread(obj)
1037+
&& obj["1"];
1038+
};
1039+
1040+
api.tools.get_thread_data = function(obj) {
1041+
return obj
1042+
&& obj["2"]
1043+
&& typeof obj["2"] === "object"
1044+
&& obj["2"]["7"]
1045+
&& typeof obj["2"]["7"] === "object"
1046+
&& obj["2"]["7"];
1047+
};
1048+
1049+
api.tools.get_action_type = function(obj) {
1050+
return obj[1][0];
1051+
};
1052+
1053+
api.tools.get_message_ids = function(obj) {
1054+
return obj
1055+
&& obj["3"]
1056+
&& Array.isArray(obj["3"])
1057+
&& obj["3"];
10251058
};
10261059

10271060
api.tools.extract_from_graph = function(obj, predicate) {
@@ -1067,6 +1100,52 @@ var Gmail = function(localJQuery) {
10671100
return result;
10681101
};
10691102

1103+
api.tools.check_event_type = function(threadObj) {
1104+
const action_map = {
1105+
// "" : "add_to_tasks",
1106+
"^a" : "archive",
1107+
"^k" : "delete",
1108+
// "" : "delete_message_in_thread",
1109+
// "" : "delete_forever",
1110+
// "" : "delete_label",
1111+
// "" : "discard_draft",
1112+
// "" : "expand_categories",
1113+
// "" : "filter_messages_like_these",
1114+
// "" : "label",
1115+
// "" : "mark_as_important",
1116+
// "" : "mark_as_not_important",
1117+
// "" : "mark_as_not_spam",
1118+
// "" : "mark_as_spam",
1119+
// "" : "move_label",
1120+
// "" : "move_to_inbox",
1121+
// "" : "mute",
1122+
// "" : "read",
1123+
// "" : "save_draft",
1124+
// "" : "send_message",
1125+
// "" : "show_newly_arrived_message",
1126+
// "" : "star",
1127+
// "" : "undo_send",
1128+
// "" : "unmute",
1129+
// "" : "unread",
1130+
// "" : "unstar",
1131+
// "" : "new_email",
1132+
// "" : "poll",
1133+
// "" : "refresh",
1134+
// "" : "restore_message_in_thread",
1135+
// "" : "open_email",
1136+
// "" : "toggle_threads"
1137+
};
1138+
const threadData = api.tools.get_thread_data(threadObj);
1139+
1140+
if (threadData && api.check.data.is_action(threadData)) {
1141+
const action = api.tools.get_action_type(threadData);
1142+
1143+
return action_map[action];
1144+
} else {
1145+
return null;
1146+
}
1147+
};
1148+
10701149
api.tools.parse_request_payload = function(params, events) {
10711150
const threads = api.tools.extract_from_graph(params, api.check.data.is_thread);
10721151
// console.log("Threads:");
@@ -1089,6 +1168,23 @@ var Gmail = function(localJQuery) {
10891168
}
10901169
}
10911170
}
1171+
1172+
try {
1173+
if (Array.isArray(threads) && api.check.data.is_thread(threads[0])) {
1174+
const actionType = api.tools.check_event_type(threads[0]);
1175+
1176+
if (actionType) {
1177+
// console.log(threads[0]);
1178+
const threadsData = threads.map(thread => api.tools.get_thread_data(thread));
1179+
1180+
const new_thread_ids = threads.map(thread => api.tools.get_thread_id(thread));
1181+
const new_email_ids = threadsData.map(threadData => api.tools.get_message_ids(threadData)).reduce((a, b) => a.concat(b), []);
1182+
events[actionType] = [null, params.url, params.body, new_email_ids, new_thread_ids];
1183+
}
1184+
}
1185+
} catch (e) {
1186+
console.error('Error: ', e);
1187+
}
10921188
};
10931189

10941190
api.tools.parse_response = function(response) {
@@ -1255,7 +1351,7 @@ var Gmail = function(localJQuery) {
12551351
}
12561352

12571353
// if any matching after events, bind onreadystatechange callback
1258-
if(api.observe.bound(events,"after")) {
1354+
if(api.observe.bound(events, "after")) {
12591355
var curr_onreadystatechange = this.onreadystatechange;
12601356
var xhr = this;
12611357
this.onreadystatechange = function(progress) {
@@ -1943,7 +2039,7 @@ var Gmail = function(localJQuery) {
19432039
if (start) {
19442040
start = parseInt(start - 1);
19452041
url += "&start=" + start +
1946-
"&sstart=" + start;
2042+
"&sstart=" + start;
19472043
} else {
19482044
url += "&start=0";
19492045
}
@@ -2529,8 +2625,8 @@ var Gmail = function(localJQuery) {
25292625
var button = $(document.createElement("div"));
25302626
var buttonClasses = "T-I J-J5-Ji ";
25312627
if(styleClass !== undefined &&
2532-
styleClass !== null &&
2533-
styleClass !== ""){
2628+
styleClass !== null &&
2629+
styleClass !== ""){
25342630
buttonClasses += basicStyle+styleClass;
25352631
}else{
25362632
buttonClasses += basicStyle+defaultStyle;

test/test.parsing.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -398,6 +398,11 @@ describe("New Gmail event-parsing", () => {
398398
assert.ok(events.send_message);
399399
});
400400
});
401+
it("Triggers for archive", () => {
402+
testCase(testData.new_gmail_archive_action_body_params, (events) => {
403+
assert.ok(events.archive);
404+
});
405+
});
401406

402407
// it("Extracts compose-id", () => {
403408

test/test.tools.js

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@ let assert = require('assert');
33
let Gmail = require('../src/gmail').Gmail;
44
let gmail = new Gmail();
55

6+
const testData = require("./testdata-parser.js");
7+
68
describe("Monkeypatching", () => {
79
it("patching functions works", () => {
810
var ns = {};
@@ -45,3 +47,40 @@ describe("Monkeypatching", () => {
4547
assert.equal("nested patched orig", result);
4648
});
4749
});
50+
51+
describe("Test tools for parsing new gmail body_params", () => {
52+
const gmail = new Gmail();
53+
const data = JSON.parse(testData.new_gmail_archive_action_body_params);
54+
55+
it("get thread id", () => {
56+
const thread = gmail.tools.get_thread_id(data);
57+
58+
assert.equal(thread, 'thread-f:1600724307680265309');
59+
});
60+
it("get thread data", () => {
61+
const mockThreadData = data[2][7];
62+
const threadData = gmail.tools.get_thread_data(data);
63+
64+
assert.deepEqual(threadData, mockThreadData);
65+
});
66+
it("get messages ids", () => {
67+
const mockMessageIds = ['msg-f:1600724307680265309', 'msg-f:1600724938213937205', 'msg-f:1600725174437456906', 'msg-f:1600725319255992336', 'msg-f:1600725448529658711'];
68+
const threadData = gmail.tools.get_thread_data(data);
69+
const messagesIds = gmail.tools.get_message_ids(threadData);
70+
71+
assert.equal(messagesIds.length, 5);
72+
assert.deepEqual(messagesIds, mockMessageIds);
73+
});
74+
it("get action type", () => {
75+
const threadData = gmail.tools.get_thread_data(data);
76+
const action = gmail.tools.get_action_type(threadData);
77+
78+
assert.equal(action, "^a");
79+
});
80+
it("is action type isset in thread object", () => {
81+
const threadData = gmail.tools.get_thread_data(data);
82+
const issetAction = gmail.check.data.is_action(threadData);
83+
84+
assert.equal(issetAction, true);
85+
});
86+
});

0 commit comments

Comments
 (0)