-
Notifications
You must be signed in to change notification settings - Fork 1.1k
/
api_friend.go
181 lines (148 loc) · 5.99 KB
/
api_friend.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
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
// 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 (
"github.com/gofrs/uuid"
"github.com/golang/protobuf/ptypes/empty"
"github.com/heroiclabs/nakama/api"
"go.uber.org/zap"
"golang.org/x/net/context"
"google.golang.org/grpc/codes"
"google.golang.org/grpc/status"
)
func (s *ApiServer) ListFriends(ctx context.Context, in *empty.Empty) (*api.Friends, error) {
userID := ctx.Value(ctxUserIDKey{}).(uuid.UUID)
friends, err := GetFriends(s.logger, s.db, s.tracker, userID)
if err != nil {
return nil, status.Error(codes.Internal, "Error while trying to list friends.")
}
return friends, nil
}
func (s *ApiServer) AddFriends(ctx context.Context, in *api.AddFriendsRequest) (*empty.Empty, error) {
if len(in.GetIds()) == 0 && len(in.GetUsernames()) == 0 {
return &empty.Empty{}, nil
}
userID := ctx.Value(ctxUserIDKey{}).(uuid.UUID)
username := ctx.Value(ctxUsernameKey{}).(string)
for _, id := range in.GetIds() {
if userID.String() == id {
return nil, status.Error(codes.InvalidArgument, "Cannot add self as friend.")
}
if _, err := uuid.FromString(id); err != nil {
return nil, status.Error(codes.InvalidArgument, "Invalid user ID '"+id+"'.")
}
}
for _, u := range in.GetUsernames() {
if username == u {
return nil, status.Error(codes.InvalidArgument, "Cannot add self as friend.")
}
}
userIDs, err := fetchUserID(s.db, in.GetUsernames())
if err != nil {
s.logger.Error("Could not fetch user IDs.", zap.Error(err), zap.Strings("usernames", in.GetUsernames()))
return nil, status.Error(codes.Internal, "Error while trying to add friends.")
}
if len(userIDs)+len(in.GetIds()) == 0 {
return nil, status.Error(codes.InvalidArgument, "No valid ID or username was provided.")
}
allIDs := make([]string, 0, len(in.GetIds())+len(userIDs))
allIDs = append(allIDs, in.GetIds()...)
allIDs = append(allIDs, userIDs...)
if err := AddFriends(s.logger, s.db, s.router, userID, username, allIDs); err != nil {
return nil, status.Error(codes.Internal, "Error while trying to add friends.")
}
return &empty.Empty{}, nil
}
func (s *ApiServer) DeleteFriends(ctx context.Context, in *api.DeleteFriendsRequest) (*empty.Empty, error) {
if len(in.GetIds()) == 0 && len(in.GetUsernames()) == 0 {
return &empty.Empty{}, nil
}
userID := ctx.Value(ctxUserIDKey{}).(uuid.UUID)
for _, id := range in.GetIds() {
if userID.String() == id {
return nil, status.Error(codes.InvalidArgument, "Cannot delete self.")
}
if _, err := uuid.FromString(id); err != nil {
return nil, status.Error(codes.InvalidArgument, "Invalid user ID '"+id+"'.")
}
}
username := ctx.Value(ctxUsernameKey{}).(string)
for _, u := range in.GetUsernames() {
if username == u {
return nil, status.Error(codes.InvalidArgument, "Cannot delete self.")
}
}
userIDs, err := fetchUserID(s.db, in.GetUsernames())
if err != nil {
s.logger.Error("Could not fetch user IDs.", zap.Error(err), zap.Strings("usernames", in.GetUsernames()))
return nil, status.Error(codes.Internal, "Error while trying to delete friends.")
}
if len(userIDs)+len(in.GetIds()) == 0 {
s.logger.Info("No valid ID or username was provided.")
return &empty.Empty{}, nil
}
allIDs := make([]string, 0, len(in.GetIds())+len(userIDs))
allIDs = append(allIDs, in.GetIds()...)
allIDs = append(allIDs, userIDs...)
if err := DeleteFriends(s.logger, s.db, userID, allIDs); err != nil {
return nil, status.Error(codes.Internal, "Error while trying to delete friends.")
}
return &empty.Empty{}, nil
}
func (s *ApiServer) BlockFriends(ctx context.Context, in *api.BlockFriendsRequest) (*empty.Empty, error) {
if len(in.GetIds()) == 0 && len(in.GetUsernames()) == 0 {
return &empty.Empty{}, nil
}
userID := ctx.Value(ctxUserIDKey{}).(uuid.UUID)
for _, id := range in.GetIds() {
if userID.String() == id {
return nil, status.Error(codes.InvalidArgument, "Cannot block self.")
}
if _, err := uuid.FromString(id); err != nil {
return nil, status.Error(codes.InvalidArgument, "Invalid user ID '"+id+"'.")
}
}
username := ctx.Value(ctxUsernameKey{}).(string)
for _, u := range in.GetUsernames() {
if username == u {
return nil, status.Error(codes.InvalidArgument, "Cannot block self.")
}
}
userIDs, err := fetchUserID(s.db, in.GetUsernames())
if err != nil {
s.logger.Error("Could not fetch user IDs.", zap.Error(err), zap.Strings("usernames", in.GetUsernames()))
return nil, status.Error(codes.Internal, "Error while trying to block friends.")
}
if len(userIDs)+len(in.GetIds()) == 0 {
return nil, status.Error(codes.InvalidArgument, "No valid ID or username was provided.")
}
allIDs := make([]string, 0, len(in.GetIds())+len(userIDs))
allIDs = append(allIDs, in.GetIds()...)
allIDs = append(allIDs, userIDs...)
if err := BlockFriends(s.logger, s.db, userID, allIDs); err != nil {
return nil, status.Error(codes.Internal, "Error while trying to block friends.")
}
return &empty.Empty{}, nil
}
func (s *ApiServer) ImportFacebookFriends(ctx context.Context, in *api.ImportFacebookFriendsRequest) (*empty.Empty, error) {
if in.Account == nil || in.Account.Token == "" {
return nil, status.Error(codes.InvalidArgument, "Facebook token is required.")
}
err := importFacebookFriends(s.logger, s.db, s.router, s.socialClient, ctx.Value(ctxUserIDKey{}).(uuid.UUID), ctx.Value(ctxUsernameKey{}).(string), in.Account.Token, in.Reset_ != nil && in.Reset_.Value)
if err != nil {
// Already logged inside the core importFacebookFriends function.
return nil, err
}
return &empty.Empty{}, nil
}