-
Notifications
You must be signed in to change notification settings - Fork 0
/
shortener.go
113 lines (89 loc) · 2.51 KB
/
shortener.go
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
package shortener
import (
"context"
"fmt"
"strings"
"cloud.google.com/go/datastore"
"github.com/majordomusio/commons/pkg/util"
"github.com/lnkk-ai/lnkk/pkg/platform"
"github.com/lnkk-ai/lnkk/pkg/types"
)
// CreateAsset stores an asset in the Datastore
func CreateAsset(ctx context.Context, as *types.Asset) (string, error) {
uri, _ := util.ShortUUID()
secret, _ := util.ShortUUID()
asset := AssetDS{
URI: uri,
URL: as.URL,
Owner: as.Owner,
SecretID: secret,
Source: as.Source,
Client: as.Client,
Affiliate: as.Affiliate,
Tags: as.Tags,
Created: util.Timestamp(),
}
k := assetKey(uri)
if _, err := platform.DataStore().Put(ctx, k, &asset); err != nil {
platform.Report(err)
return "", err
}
return uri, nil
}
// GetAsset retrieves the asset
func GetAsset(ctx context.Context, uri string) (*types.Asset, error) {
var as AssetDS
k := assetKey(uri)
if err := platform.DataStore().Get(ctx, k, &as); err != nil {
return nil, err
}
return as.AsExternal(), nil
}
// CreateMeasurement records a link activation
func CreateMeasurement(ctx context.Context, m *MeasurementDS) error {
// anonimize the IP to be GDPR compliant
m.IP = anonimizeIP(m.IP)
// TODO: use a queue here, go routine will not work !
CreateGeoLocation(ctx, m.IP)
k := datastore.IncompleteKey(DatastoreMeasurement, nil)
if _, err := platform.DataStore().Put(ctx, k, m); err != nil {
platform.Report(err)
return err
}
return nil
}
// CreateGeoLocation looks up the IP's geolocation if it is unknown
func CreateGeoLocation(ctx context.Context, ip string) error {
var loc GeoLocationDS
k := geoLocationKey(ip)
if err := platform.DataStore().Get(ctx, k, &loc); err != nil {
// assuming the location is unknown
l, err := lookupGeoLocation(ip)
if err != nil {
platform.Report(err)
return err
}
if _, err := platform.DataStore().Put(ctx, k, l.AsInternal()); err != nil {
platform.Report(err)
return err
}
}
return nil
}
// Anonimize the IP to be GDPR compliant
func anonimizeIP(ip string) string {
if strings.ContainsRune(ip, 58) {
// IPv6
parts := strings.Split(ip, ":")
return fmt.Sprintf("%s:%s:%s:0000:0000:0000:0000:0000", parts[0], parts[1], parts[2])
}
// IPv4
parts := strings.Split(ip, ".")
return fmt.Sprintf("%s.%s.%s.0", parts[0], parts[1], parts[2])
}
func assetKey(uri string) *datastore.Key {
return datastore.NameKey(DatastoreAssets, uri, nil)
}
func geoLocationKey(ip string) *datastore.Key {
return datastore.NameKey(DatastoreGeoLocation, ip, nil)
}