forked from heroiclabs/nakama
-
Notifications
You must be signed in to change notification settings - Fork 0
/
console_gdpr.go
141 lines (125 loc) · 5.37 KB
/
console_gdpr.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
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
// Copyright 2018 The Nakama Authors
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package server
import (
"context"
"database/sql"
"encoding/json"
"github.com/gofrs/uuid"
"github.com/golang/protobuf/ptypes/timestamp"
"github.com/heroiclabs/nakama/api"
"github.com/heroiclabs/nakama/console"
"go.uber.org/zap"
"google.golang.org/grpc/codes"
"google.golang.org/grpc/status"
)
func (s *ConsoleServer) RecordAccountDeletion(tx *sql.Tx, userID uuid.UUID) error {
if _, err := tx.Exec(`INSERT INTO user_tombstone (user_id) VALUES ($1) ON CONFLICT(user_id) DO NOTHING`, userID); err != nil {
s.logger.Debug("Could not insert user ID into tombstone", zap.Error(err), zap.String("user_id", userID.String()))
return err
}
return nil
}
func (s *ConsoleServer) ExportAccount(ctx context.Context, in *console.AccountIdRequest) (*console.AccountExport, error) {
userID := uuid.FromStringOrNil(in.Id)
if userID == uuid.Nil {
return nil, status.Error(codes.InvalidArgument, "Invalid user ID was provided.")
}
// Core user account.
account, err := GetAccount(s.logger, s.db, nil, userID)
if err != nil {
if err == ErrAccountNotFound {
return nil, status.Error(codes.NotFound, "Account not found.")
}
s.logger.Error("Could not export account data", zap.Error(err), zap.String("user_id", in.Id))
return nil, status.Error(codes.Internal, "An error occurred while trying to export user data.")
}
// Friends.
friends, err := GetFriendIDs(s.logger, s.db, userID)
if err != nil {
s.logger.Error("Could not fetch friend IDs", zap.Error(err), zap.String("user_id", in.Id))
return nil, status.Error(codes.Internal, "An error occurred while trying to export user data.")
}
// Messages.
messages, err := GetChannelMessages(s.logger, s.db, userID)
if err != nil {
s.logger.Error("Could not fetch messages", zap.Error(err), zap.String("user_id", in.Id))
return nil, status.Error(codes.Internal, "An error occurred while trying to export user data.")
}
// Leaderboard records.
leaderboardRecords, err := LeaderboardRecordReadAll(s.logger, s.db, userID)
if err != nil {
s.logger.Error("Could not fetch leaderboard records", zap.Error(err), zap.String("user_id", in.Id))
return nil, status.Error(codes.Internal, "An error occurred while trying to export user data.")
}
groups := make([]*api.Group, 0)
groupUsers, err := ListUserGroups(s.logger, s.db, userID)
if err != nil {
s.logger.Error("Could not fetch groups that belong to the user", zap.Error(err), zap.String("user_id", in.Id))
return nil, status.Error(codes.Internal, "An error occurred while trying to export user data.")
}
for _, g := range groupUsers.UserGroups {
groups = append(groups, g.Group)
}
// Notifications.
notifications, err := NotificationList(s.logger, s.db, userID, 0, "", nil)
if err != nil {
s.logger.Error("Could not fetch notifications", zap.Error(err), zap.String("user_id", in.Id))
return nil, status.Error(codes.Internal, "An error occurred while trying to export user data.")
}
// Storage objects where user is the owner.
storageObjects, err := StorageReadAllUserObjects(s.logger, s.db, userID)
if err != nil {
s.logger.Error("Could not fetch notifications", zap.Error(err), zap.String("user_id", in.Id))
return nil, status.Error(codes.Internal, "An error occurred while trying to export user data.")
}
// History of user's wallet.
walletLedgers, err := ListWalletLedger(s.logger, s.db, userID)
if err != nil {
s.logger.Error("Could not fetch wallet ledger items", zap.Error(err), zap.String("user_id", in.Id))
return nil, status.Error(codes.Internal, "An error occurred while trying to export user data.")
}
wl := make([]*console.WalletLedger, len(walletLedgers))
for i, w := range walletLedgers {
changeset, err := json.Marshal(w.Changeset)
if err != nil {
s.logger.Error("Could not fetch wallet ledger items, error encoding changeset", zap.Error(err), zap.String("user_id", in.Id))
return nil, status.Error(codes.Internal, "An error occurred while trying to export user data.")
}
metadata, err := json.Marshal(w.Metadata)
if err != nil {
s.logger.Error("Could not fetch wallet ledger items, error encoding metadata", zap.Error(err), zap.String("user_id", in.Id))
return nil, status.Error(codes.Internal, "An error occurred while trying to export user data.")
}
wl[i] = &console.WalletLedger{
Id: w.ID,
UserId: w.UserID,
Changeset: string(changeset),
Metadata: string(metadata),
CreateTime: ×tamp.Timestamp{Seconds: w.CreateTime},
UpdateTime: ×tamp.Timestamp{Seconds: w.UpdateTime},
}
}
export := &console.AccountExport{
Account: account,
Objects: storageObjects,
Friends: friends.GetFriends(),
Messages: messages,
Groups: groups,
LeaderboardRecords: leaderboardRecords,
Notifications: notifications.GetNotifications(),
WalletLedgers: wl,
}
return export, nil
}