-
Notifications
You must be signed in to change notification settings - Fork 1
/
get-history.js
109 lines (86 loc) · 2.67 KB
/
get-history.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
// SPDX-License-Identifier: CC0-1.0
// This is meant to be run in Google Apps Script: see README.
const root = "items/YOUR_FOLDER_ID";
function exportActivity() {
let ret = DriveActivity.Activity.query({
pageSize: 100,
ancestorName: root,
});
let activities = ret.activities;
activities.reverse();
let out = "";
while (ret.nextPageToken && ret.nextPageToken.length > 0) {
ret = DriveActivity.Activity.query({
pageSize: 100,
ancestorName: root,
pageToken: ret.nextPageToken,
});
ret.activities.reverse();
activities = ret.activities.concat(activities);
console.log(`Fetched ${activities.length} activities...`);
}
let users = {};
let seenFiles = {};
console.log(
"Activities fetched. Fetching additional data (usernames and file parents)..."
);
// at this stage we do a progress report every 10 seconds
let lastProgressReport = Date.now();
activities.forEach((act, i) => {
if (
act.targets[0].driveItem &&
!seenFiles[act.targets[0].driveItem.name] &&
!act.actions.some((x) => x.detail.move)
) {
// first time we're seeing a file, and the activity doesn't have a move
// component; grab its current parents in case we need them as a fallback
const id = act.targets[0].driveItem.name;
const shortId = id.split("/")[1];
try
{
const parentIter = DriveApp.getFileById(shortId).getParents();
let parents = [];
while (parentIter.hasNext()) {
parents.push("items/" + parentIter.next().getId());
}
act.targets[0]._d2g_parents = parents;
seenFiles[id] = true;
}
catch(e)
{
console.log(e);
console.log("Could not parse file by Id!");
}
}
if (lastProgressReport + 1000 * 10 <= Date.now()) {
console.log(`Fetched additional data for ${i + 1} activities...`);
lastProgressReport = Date.now();
}
if (!act.actors[0].user || act.actors[0].user.deletedUser)
{
return;
}
const userId = act.actors[0].user.knownUser.personName;
if (!users[userId]) {
try{
users[userId] = People.People.get(userId, { personFields: "names" });
}
catch(e)
{
console.log(e);
console.log("Could not get user by ID!");
return;
}
}
act.actors[0].user._d2g_info = users[userId];
});
const file = DriveApp.createFile(
Utilities.newBlob(
activities.map(JSON.stringify).join("\n"),
"application/json",
`${root.split("/")[1]}-${new Date().toISOString()}.ndjson`
)
);
console.log("Log generated and saved to Google Drive:");
console.log(file.getDownloadUrl());
}