Skip to content

Commit d88d526

Browse files
author
Eric Koleda
authored
Merge pull request googleworkspace#94 from jsmeredith/master
Adding Apps Script quickstart example for Drive Activity v2 API.
2 parents bae9205 + b43fc9f commit d88d526

File tree

1 file changed

+108
-0
lines changed

1 file changed

+108
-0
lines changed

drive/activity-v2/quickstart.gs

+108
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,108 @@
1+
/**
2+
* Copyright Google LLC
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
// [START drive_activity_v2_quickstart]
17+
/**
18+
* Lists activity for a Drive user.
19+
*/
20+
function listDriveActivity() {
21+
var request = {pageSize: 10};
22+
var response = DriveActivity.Activity.query(request);
23+
var activities = response.activities;
24+
if (activities && activities.length > 0) {
25+
Logger.log('Recent activity:');
26+
for (var i = 0; i < activities.length; i++) {
27+
var activity = activities[i];
28+
var time = getTimeInfo(activity);
29+
var action = getActionInfo(activity.primaryActionDetail);
30+
var actors = activity.actors.map(getActorInfo);
31+
var targets = activity.targets.map(getTargetInfo);
32+
Logger.log(
33+
'%s: %s, %s, %s', time, truncated(actors), action,
34+
truncated(targets));
35+
}
36+
} else {
37+
Logger.log('No activity.');
38+
}
39+
}
40+
41+
/** Returns a string representation of the first elements in a list. */
42+
function truncated(array, opt_limit) {
43+
var limit = opt_limit || 2;
44+
var contents = array.slice(0, limit).join(', ');
45+
var more = array.length > limit ? ', ...' : '';
46+
return '[' + contents + more + ']';
47+
}
48+
49+
/** Returns the name of a set property in an object, or else "unknown". */
50+
function getOneOf(object) {
51+
for (var key in object) {
52+
return key;
53+
}
54+
return 'unknown';
55+
}
56+
57+
/** Returns a time associated with an activity. */
58+
function getTimeInfo(activity) {
59+
if ('timestamp' in activity) {
60+
return activity.timestamp;
61+
}
62+
if ('timeRange' in activity) {
63+
return activity.timeRange.endTime;
64+
}
65+
return 'unknown';
66+
}
67+
68+
/** Returns the type of action. */
69+
function getActionInfo(actionDetail) {
70+
return getOneOf(actionDetail);
71+
}
72+
73+
/** Returns user information, or the type of user if not a known user. */
74+
function getUserInfo(user) {
75+
if ('knownUser' in user) {
76+
var knownUser = user.knownUser;
77+
var isMe = knownUser.isCurrentUser || false;
78+
return isMe ? 'people/me' : knownUser.personName;
79+
}
80+
return getOneOf(user);
81+
}
82+
83+
/** Returns actor information, or the type of actor if not a user. */
84+
function getActorInfo(actor) {
85+
if ('user' in actor) {
86+
return getUserInfo(actor.user)
87+
}
88+
return getOneOf(actor);
89+
}
90+
91+
/** Returns the type of a target and an associated title. */
92+
function getTargetInfo(target) {
93+
if ('driveItem' in target) {
94+
var title = target.driveItem.title || 'unknown';
95+
return 'driveItem:"' + title + '"';
96+
}
97+
if ('teamDrive' in target) {
98+
var title = target.teamDrive.title || 'unknown';
99+
return 'teamDrive:"' + title + '"';
100+
}
101+
if ('fileComment' in target) {
102+
var parent = target.fileComment.parent || {};
103+
var title = parent.title || 'unknown';
104+
return 'fileComment:"' + title + '"';
105+
}
106+
return getOneOf(target) + ':unknown';
107+
}
108+
// [END drive_activity_v2_quickstart]

0 commit comments

Comments
 (0)