Skip to content

Instantly share code, notes, and snippets.

@7x7x7eq51
Last active July 21, 2020 07:09
Show Gist options
  • Select an option

  • Save 7x7x7eq51/d7bc8e0902de2595b80c2ecdfcbf04dc to your computer and use it in GitHub Desktop.

Select an option

Save 7x7x7eq51/d7bc8e0902de2595b80c2ecdfcbf04dc to your computer and use it in GitHub Desktop.
Gmail build tree multiple level lables
{
"labels": [
{
"id": "CHAT",
"name": "CHAT",
"messageListVisibility": "hide",
"labelListVisibility": "labelHide",
"type": "system"
},
{
"id": "SENT",
"name": "SENT",
"type": "system"
},
{
"id": "INBOX",
"name": "INBOX",
"type": "system"
},
{
"id": "IMPORTANT",
"name": "IMPORTANT",
"messageListVisibility": "hide",
"labelListVisibility": "labelHide",
"type": "system"
},
{
"id": "TRASH",
"name": "TRASH",
"messageListVisibility": "hide",
"labelListVisibility": "labelHide",
"type": "system"
},
{
"id": "DRAFT",
"name": "DRAFT",
"type": "system"
},
{
"id": "SPAM",
"name": "SPAM",
"messageListVisibility": "hide",
"labelListVisibility": "labelHide",
"type": "system"
},
{
"id": "CATEGORY_FORUMS",
"name": "CATEGORY_FORUMS",
"messageListVisibility": "hide",
"labelListVisibility": "labelHide",
"type": "system"
},
{
"id": "CATEGORY_UPDATES",
"name": "CATEGORY_UPDATES",
"messageListVisibility": "hide",
"labelListVisibility": "labelHide",
"type": "system"
},
{
"id": "CATEGORY_PERSONAL",
"name": "CATEGORY_PERSONAL",
"messageListVisibility": "hide",
"labelListVisibility": "labelHide",
"type": "system"
},
{
"id": "CATEGORY_PROMOTIONS",
"name": "CATEGORY_PROMOTIONS",
"messageListVisibility": "hide",
"labelListVisibility": "labelHide",
"type": "system"
},
{
"id": "CATEGORY_SOCIAL",
"name": "CATEGORY_SOCIAL",
"messageListVisibility": "hide",
"labelListVisibility": "labelHide",
"type": "system"
},
{
"id": "STARRED",
"name": "STARRED",
"type": "system"
},
{
"id": "UNREAD",
"name": "UNREAD",
"type": "system"
},
{
"id": "Label_1132815994075013083",
"name": "My/My 2",
"messageListVisibility": "show",
"labelListVisibility": "labelShow",
"type": "user"
},
{
"id": "Label_1568450317911142934",
"name": "My",
"messageListVisibility": "show",
"labelListVisibility": "labelShow",
"type": "user"
},
{
"id": "Label_2119806523827933884",
"name": "My/My 2/My 123 4/9/0/0/000/333/httml/My 123 4/9/0/0/000/454",
"messageListVisibility": "show",
"labelListVisibility": "labelShow",
"type": "user"
},
{
"id": "Label_3333821800168712846",
"name": "My 2",
"messageListVisibility": "show",
"labelListVisibility": "labelShow",
"type": "user"
},
{
"id": "Label_3924464747358265414",
"name": "My/My 2/My 123 4/9/0/0/000/333/httml",
"messageListVisibility": "show",
"labelListVisibility": "labelShow",
"type": "user"
},
{
"id": "Label_6033357805571224531",
"name": "My 2/My 22",
"type": "user"
}
]
}
/**
** @name labelsParser
** @auth TPT
**/
const _ = require('underscore');
const HasNoChildren = '\HasNoChildren';
const HasChildren = '\HasChildren';
const labelSystem = `system`;
const labelUser = `user`;
function replacePath(str){
try{
if(!_.isString(str)){
new Error('it\' must string');
}
str = str.replace(/\ |\//gi, function(matched){
return '-';
});
return str.toLowerCase();
}catch (e){
throw err;
}
}
/**
* Takes a response from the Gmail API's GET message method and extracts all
* the relevant data.
* @param {object} response
* @return {object}
*/
module.exports = (response) => {
// init result
let result = {};
// filter label as system type
let sysLabels = _.filter(response.labels, lbs => {
return lbs.type === labelSystem;
});
sysLabels = _.map(sysLabels, lbs => {
return {
attribs: [HasNoChildren],
children: null,
delimiter: "/",
displayName: lbs.name,
path: lbs.id
}
});
// progressing category
sysLabels = _.map(sysLabels, lbs => {
if (lbs.path.includes('CATEGORY_')) {
lbs.path = lbs.path.replace('CATEGORY_', '');
lbs.displayName = lbs.displayName.replace('CATEGORY_', '');
}
return lbs;
});
sysLabels = _.indexBy(sysLabels, 'displayName');
// filter all label as type = `user`
let labels = _.filter(response.labels, lbs => {
return lbs.type === labelUser;
});
// sort move root on top
labels.sort((prv, cur) => {
if (prv.name.length < cur.name.length) {
// prv should come before cur in the sorted order
return -1;
} else if (prv.name.length > cur.name.length) {
// prv should come after cur in the sorted order
return 1;
} else {
// prv and cur are the same
return 0;
}
});
// make sure always have system label
if(_.size(sysLabels) > 0){
result = _.extend(result, sysLabels);
}
let labelsdup = _.filter(response.labels, lbs => {
return lbs.type === labelUser;
});
// fill parent for label children
while (labelsdup.length !== 0) {
let label = labelsdup.shift();
labels = labels.map(cur => {
if (cur.id !== label.id && cur.name.length > label.name.length && cur.name.indexOf(label.name + '/') === 0) {
cur['parent'] = label.id;
}
if(!cur['parent']){
cur['parent'] = null;
}
return cur;
});
}
// map structure tree labels imap
labels = _.map(labels, lbs => {
return {
attribs: null,
children: {},
delimiter: '/',
displayName: lbs.name,
id: lbs.id,
path: replacePath(lbs.name),
parent: lbs.parent
}
});
// add properties attribs
labelsdup = _.clone(labels);
while (labelsdup.length !== 0) {
let label = labelsdup.shift();
labels = labels.map(cur => {
if (cur.id === label.parent) {
cur['attribs'] = [HasChildren];
cur.children[label.path] = label;
}
if(cur['attribs']===null){
cur['attribs'] = [HasNoChildren];
}
return cur;
});
}
// remove all lables has parent
labels = _.filter(labels, lbs => {
return lbs.parent === null;
});
labels = _.indexBy(labels, 'path');
result = _.extend(result, labels);
return result;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment