forked from abrahamjuliot/creepjs
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.gs
143 lines (124 loc) · 3.73 KB
/
server.gs
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
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
// !important: must redeploy
// required: doPost trigger in spreadsheet on form submit
function setJSONFile() {
const filename = 'db.json.txt'
const timestamp = new Date()
// create some dummy data
const obj = {}
obj['init'] = {
firstVisit: timestamp,
latestVisit: timestamp,
visits: 1,
subIds: { ['init']: 1 }
}
const json = JSON.stringify(obj)
// get the file
const files = DriveApp.getFilesByName(filename)
if (files.hasNext()) { return files.next().setContent(json) } // overwrite and set the contents of the file
else { return DriveApp.createFile(filename, json) } // create the file if it does not exist
}
function setTimeBasedTrigger() {
return ScriptApp.newTrigger('setJSONFile')
.timeBased()
.atHour(1)
.everyWeeks(1)
.onWeekDay(ScriptApp.WeekDay.MONDAY)
.inTimezone('America/Los_Angeles')
.create()
}
function log(message) {
const filename = 'log.txt'
const timestamp = new Date()
const log = `${timestamp}: ${message}`
const files = DriveApp.getFilesByName(filename)
if (files.hasNext()) { return files.next().setContent(log) }
else { return DriveApp.createFile(filename, log) }
}
function getData() {
const files = DriveApp.getFilesByName('db.json.txt')
if (files.hasNext()) {
const file = files.next()
const json = file.getBlob().getDataAsString()
const data = JSON.parse(json)
return data
}
}
function setData(obj) {
const json = JSON.stringify(obj)
const filename = 'db.json.txt'
const files = DriveApp.getFilesByName(filename)
if (files.hasNext()) { return files.next().setContent(json) }
else { return DriveApp.createFile(filename, json) }
}
// https://github.com/jamiewilson/form-to-google-sheets
function doGet(e) {
const lock = LockService.getScriptLock()
lock.tryLock(1000)
try {
// get params
const { parameter: { id: fingerprintId, subId } } = e
let hasTrash = false
let hasLied = false
let hasErrors = false
//log(JSON.stringify(e.parameter))
// try server feature
try {
hasTrash = e.parameter.hasTrash
hasLied = e.parameter.hasLied
hasErrors = e.parameter.hasErrors
}
catch (error) { }
// get database
const db = getData()
// update or create data
const timestamp = new Date()
if (db[fingerprintId]) {
// update
const dbId = db[fingerprintId]
const { subIds } = dbId
if (subIds[subId]) { subIds[subId]++ }
else { subIds[subId] = 1 }
dbId.latestVisit = timestamp
dbId.visits++
dbId.subIds = subIds
db[fingerprintId] = dbId
if (('hasTrash' in dbId) && (''+dbId.hasTrash) == 'false' && (''+hasTrash) == 'true') {
dbId.hasTrash = true
}
if (('hasLied' in dbId) && (''+dbId.hasLied) == 'false' && (''+hasLied) == 'true') {
dbId.hasLied = true
}
if (('hasErrors' in dbId) && (''+dbId.hasErrors) == 'false' && (''+hasErrors) == 'true') {
dbId.hasErrors = true
}
}
else {
// create
db[fingerprintId] = {
firstVisit: timestamp,
latestVisit: timestamp,
visits: 1,
subIds: { [subId]: 1 },
hasTrash,
hasLied,
hasErrors
}
}
// set database
setData(db)
// get id data
const idData = db[fingerprintId]
//log('test')
// return json
const json = JSON.stringify(idData)
return ContentService.createTextOutput(json).setMimeType(ContentService.MimeType.JSON)
}
catch (err) {
return ContentService
.createTextOutput(JSON.stringify({ 'result': 'error', 'error': JSON.stringify(err) }))
.setMimeType(ContentService.MimeType.JSON)
}
finally {
lock.releaseLock()
}
}