-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmodels.go
More file actions
303 lines (266 loc) · 7.81 KB
/
models.go
File metadata and controls
303 lines (266 loc) · 7.81 KB
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
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
// Copyright 2017-2026 Allow2 Pty Ltd. All rights reserved.
// Use of this source code is governed by the Allow2 API and SDK Licence.
package allow2service
import (
"fmt"
"time"
)
// Activity ID constants for common Allow2 activities.
const (
// ActivityInternet is the Internet activity ID.
ActivityInternet = 1
// ActivityGaming is the Gaming activity ID.
ActivityGaming = 3
// ActivitySocial is the Social Media activity ID.
ActivitySocial = 6
// ActivityScreenTime is the Screen Time activity ID (master device-level switch).
ActivityScreenTime = 8
)
// Activity represents the permission state of a single activity from a check result.
type Activity struct {
ID int `json:"id"`
Name string `json:"name"`
Allowed bool `json:"allowed"`
Remaining int `json:"remaining"`
Banned bool `json:"banned"`
TimeBlockAllowed bool `json:"timeBlockAllowed"`
}
// ActivityFromMap creates an Activity from a map (API response data).
func ActivityFromMap(data map[string]interface{}) *Activity {
a := &Activity{
TimeBlockAllowed: true, // default
}
if v, ok := data["id"]; ok {
a.ID = toInt(v)
}
if v, ok := data["activity"]; ok {
a.Name = fmt.Sprintf("%v", v)
} else if v, ok := data["name"]; ok {
a.Name = fmt.Sprintf("%v", v)
}
if v, ok := data["allowed"]; ok {
a.Allowed = toBool(v)
}
if v, ok := data["remaining"]; ok {
a.Remaining = toInt(v)
}
if v, ok := data["banned"]; ok {
a.Banned = toBool(v)
}
if v, ok := data["timeblock"]; ok {
a.TimeBlockAllowed = toBool(v)
} else if v, ok := data["timeBlockAllowed"]; ok {
a.TimeBlockAllowed = toBool(v)
}
return a
}
// DayType represents a day type in the Allow2 system (e.g., School Day, Weekend, Holiday).
type DayType struct {
ID int `json:"id"`
Name string `json:"name"`
}
// DayTypeFromMap creates a DayType from a map.
func DayTypeFromMap(data map[string]interface{}) *DayType {
d := &DayType{}
if v, ok := data["id"]; ok {
d.ID = toInt(v)
}
if v, ok := data["name"]; ok {
d.Name = fmt.Sprintf("%v", v)
}
return d
}
// CheckResult is the result of a permission check from the Allow2 Service API.
type CheckResult struct {
Allowed bool `json:"allowed"`
Activities []*Activity `json:"activities"`
TodayDayType *DayType `json:"todayDayType,omitempty"`
TomorrowDayType *DayType `json:"tomorrowDayType,omitempty"`
Raw map[string]interface{}
}
// GetActivity returns a specific activity by ID, or nil if not present.
func (r *CheckResult) GetActivity(activityID int) *Activity {
for _, a := range r.Activities {
if a.ID == activityID {
return a
}
}
return nil
}
// IsActivityAllowed checks whether a specific activity is allowed.
func (r *CheckResult) IsActivityAllowed(activityID int) bool {
a := r.GetActivity(activityID)
return a != nil && a.Allowed
}
// GetRemainingSeconds returns the remaining seconds for a specific activity.
func (r *CheckResult) GetRemainingSeconds(activityID int) int {
a := r.GetActivity(activityID)
if a != nil {
return a.Remaining
}
return 0
}
// CheckResultFromAPIResponse builds a CheckResult from the raw API response.
func CheckResultFromAPIResponse(response map[string]interface{}) *CheckResult {
var activities []*Activity
if rawActs, ok := response["activities"]; ok {
if actSlice, ok := rawActs.([]interface{}); ok {
for _, item := range actSlice {
if actMap, ok := item.(map[string]interface{}); ok {
activities = append(activities, ActivityFromMap(actMap))
}
}
}
}
var todayDayType *DayType
if dt, ok := response["dayType"].(map[string]interface{}); ok {
todayDayType = DayTypeFromMap(dt)
} else if dt, ok := response["today"].(map[string]interface{}); ok {
todayDayType = DayTypeFromMap(dt)
}
var tomorrowDayType *DayType
if dt, ok := response["tomorrowDayType"].(map[string]interface{}); ok {
tomorrowDayType = DayTypeFromMap(dt)
} else if dt, ok := response["tomorrow"].(map[string]interface{}); ok {
tomorrowDayType = DayTypeFromMap(dt)
}
// Global "allowed" is true only if ALL activities are allowed
allowed := true
for _, act := range activities {
if !act.Allowed {
allowed = false
break
}
}
// Override with explicit server value if present
if v, ok := response["allowed"]; ok {
allowed = toBool(v)
}
return &CheckResult{
Allowed: allowed,
Activities: activities,
TodayDayType: todayDayType,
TomorrowDayType: tomorrowDayType,
Raw: response,
}
}
// OAuthTokens is a value object representing an OAuth2 token set.
type OAuthTokens struct {
AccessToken string `json:"access_token"`
RefreshToken string `json:"refresh_token"`
ExpiresAt int64 `json:"expires_at"`
}
// IsExpired checks whether the access token has expired or is about to expire.
func (t *OAuthTokens) IsExpired(bufferSeconds int) bool {
return time.Now().Unix() >= (t.ExpiresAt - int64(bufferSeconds))
}
// ToMap serializes the tokens to a map for storage.
func (t *OAuthTokens) ToMap() map[string]interface{} {
return map[string]interface{}{
"access_token": t.AccessToken,
"refresh_token": t.RefreshToken,
"expires_at": t.ExpiresAt,
}
}
// OAuthTokensFromAPIResponse creates OAuthTokens from an API token response.
func OAuthTokensFromAPIResponse(response map[string]interface{}) *OAuthTokens {
accessToken := ""
if v, ok := response["access_token"].(string); ok {
accessToken = v
}
refreshToken := ""
if v, ok := response["refresh_token"].(string); ok {
refreshToken = v
}
var expiresIn int64
if v, ok := response["expires_in"]; ok {
expiresIn = toInt64(v)
}
return &OAuthTokens{
AccessToken: accessToken,
RefreshToken: refreshToken,
ExpiresAt: time.Now().Unix() + expiresIn,
}
}
// OAuthTokensFromMap reconstitutes OAuthTokens from a stored map.
func OAuthTokensFromMap(data map[string]interface{}) *OAuthTokens {
t := &OAuthTokens{}
if v, ok := data["access_token"].(string); ok {
t.AccessToken = v
}
if v, ok := data["refresh_token"].(string); ok {
t.RefreshToken = v
}
if v, ok := data["expires_at"]; ok {
t.ExpiresAt = toInt64(v)
}
return t
}
// RequestResult is the result of creating a request (more time, day type change, or ban lift).
type RequestResult struct {
RequestID string `json:"requestId"`
StatusSecret string `json:"statusSecret"`
Status string `json:"status"`
}
// IsPending returns true if the request is still pending.
func (r *RequestResult) IsPending() bool {
return r.Status == "pending"
}
// IsApproved returns true if the request was approved.
func (r *RequestResult) IsApproved() bool {
return r.Status == "approved"
}
// IsDenied returns true if the request was denied.
func (r *RequestResult) IsDenied() bool {
return r.Status == "denied"
}
// RequestResultFromAPIResponse creates a RequestResult from API response data.
func RequestResultFromAPIResponse(response map[string]interface{}) *RequestResult {
r := &RequestResult{Status: "pending"}
if v, ok := response["requestId"]; ok {
r.RequestID = fmt.Sprintf("%v", v)
}
if v, ok := response["statusSecret"]; ok {
r.StatusSecret = fmt.Sprintf("%v", v)
}
if v, ok := response["status"].(string); ok {
r.Status = v
}
return r
}
// VoiceCodePair is a challenge-response pair for offline voice code approval.
type VoiceCodePair struct {
Challenge string `json:"challenge"`
ExpectedResponse string `json:"expectedResponse"`
}
// toInt converts an interface{} to int.
func toInt(v interface{}) int {
switch val := v.(type) {
case int:
return val
case int64:
return int(val)
case float64:
return int(val)
case string:
// ignore error, return 0
return 0
default:
return 0
}
}
// toInt64 converts an interface{} to int64.
func toInt64(v interface{}) int64 {
switch val := v.(type) {
case int:
return int64(val)
case int64:
return val
case float64:
return int64(val)
case string:
return 0
default:
return 0
}
}