forked from red-gold/telar-social-go
-
Notifications
You must be signed in to change notification settings - Fork 0
/
common.go
243 lines (212 loc) · 8.05 KB
/
common.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
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
package handlers
import (
"bytes"
"encoding/hex"
"encoding/json"
"fmt"
"io/ioutil"
"net/http"
models "github.com/GMcD/ts-serverless/micros/vang/models"
"github.com/alexellis/hmac"
"github.com/gofiber/fiber/v2"
"github.com/gofrs/uuid"
coreConfig "github.com/red-gold/telar-core/config"
log "github.com/red-gold/telar-core/pkg/log"
"github.com/red-gold/telar-core/types"
"github.com/red-gold/telar-core/utils"
)
type Action struct {
Type string `json:"type"`
Payload interface{} `json:"payload"`
}
type UserInfoInReq struct {
UserId uuid.UUID `json:"userId"`
Username string `json:"username"`
Avatar string `json:"avatar"`
DisplayName string `json:"displayName"`
SystemRole string `json:"systemRole"`
}
// getHeadersFromUserInfoReq
func getHeadersFromUserInfoReq(info *UserInfoInReq) map[string][]string {
userHeaders := make(map[string][]string)
userHeaders["uid"] = []string{info.UserId.String()}
userHeaders["email"] = []string{info.Username}
userHeaders["avatar"] = []string{info.Avatar}
userHeaders["displayName"] = []string{info.DisplayName}
userHeaders["role"] = []string{info.SystemRole}
return userHeaders
}
// getUserInfoReq
func getUserInfoReqFromCurrentUser(currentUser types.UserContext) *UserInfoInReq {
userInfoInReq := &UserInfoInReq{
UserId: currentUser.UserID,
Username: currentUser.Username,
Avatar: currentUser.Avatar,
DisplayName: currentUser.DisplayName,
SystemRole: currentUser.SystemRole,
}
return userInfoInReq
}
// getUserInfoReq
func getUserInfoReq(c *fiber.Ctx) *UserInfoInReq {
currentUser, ok := c.Locals("user").(types.UserContext)
if !ok {
return &UserInfoInReq{}
}
return getUserInfoReqFromCurrentUser(currentUser)
}
// functionCall send request to another function/microservice using HMAC validation
func functionCall(method string, bytesReq []byte, url string, header map[string][]string) ([]byte, error) {
prettyURL := utils.GetPrettyURLf(url)
bodyReader := bytes.NewBuffer(bytesReq)
httpReq, httpErr := http.NewRequest(method, *coreConfig.AppConfig.InternalGateway+prettyURL, bodyReader)
if httpErr != nil {
return nil, httpErr
}
digest := hmac.Sign(bytesReq, []byte(*coreConfig.AppConfig.PayloadSecret))
httpReq.Header.Set("Content-type", "application/json")
fmt.Printf("\ndigest: %s, header: %v \n", "sha1="+hex.EncodeToString(digest), types.HeaderHMACAuthenticate)
httpReq.Header.Add(types.HeaderHMACAuthenticate, "sha1="+hex.EncodeToString(digest))
if header != nil {
for k, v := range header {
httpReq.Header[k] = v
}
}
utils.AddPolicies(httpReq)
c := http.Client{}
res, reqErr := c.Do(httpReq)
fmt.Printf("\nUrl : %s, Result : %v\n", url, *res)
if reqErr != nil {
return nil, fmt.Errorf("Error while sending admin check request!: %s", reqErr.Error())
}
if res.Body != nil {
defer res.Body.Close()
}
resData, readErr := ioutil.ReadAll(res.Body)
if resData == nil || readErr != nil {
return nil, fmt.Errorf("failed to read response from admin check request.")
}
if res.StatusCode != http.StatusAccepted && res.StatusCode != http.StatusOK {
if res.StatusCode == http.StatusNotFound {
return nil, NotFoundHTTPStatusError
}
return nil, fmt.Errorf("failed to call %s api, invalid status: %s", prettyURL, res.Status)
}
return resData, nil
}
// Dispatch action
func dispatchAction(action Action, roomId uuid.UUID, userInfoInReq *UserInfoInReq) {
actionURL := fmt.Sprintf("/actions/dispatch/%s", roomId.String())
actionBytes, marshalErr := json.Marshal(action)
if marshalErr != nil {
errorMessage := fmt.Sprintf("Marshal notification Error %s", marshalErr.Error())
fmt.Println(errorMessage)
}
// Create user headers for http request
userHeaders := make(map[string][]string)
userHeaders["uid"] = []string{userInfoInReq.UserId.String()}
userHeaders["email"] = []string{userInfoInReq.Username}
userHeaders["avatar"] = []string{userInfoInReq.Avatar}
userHeaders["displayName"] = []string{userInfoInReq.DisplayName}
userHeaders["role"] = []string{userInfoInReq.SystemRole}
_, actionErr := functionCall(http.MethodPost, actionBytes, actionURL, userHeaders)
if actionErr != nil {
errorMessage := fmt.Sprintf("Cannot send action request! error: %s", actionErr.Error())
fmt.Println(errorMessage)
}
}
// getUserProfileByID Get user profile by user ID
func getUserProfileByID(userID uuid.UUID) (*models.UserProfileModel, error) {
profileURL := fmt.Sprintf("/profile/dto/id/%s", userID.String())
foundProfileData, err := functionCall(http.MethodGet, []byte(""), profileURL, nil)
if err != nil {
if err == NotFoundHTTPStatusError {
return nil, nil
}
log.Error("functionCall (%s) - %s", profileURL, err.Error())
return nil, fmt.Errorf("getUserProfileByID/functionCall")
}
var foundProfile models.UserProfileModel
err = json.Unmarshal(foundProfileData, &foundProfile)
if err != nil {
log.Error("Unmarshal foundProfile - %s", err.Error())
return nil, fmt.Errorf("getUserProfileByID/unmarshal")
}
return &foundProfile, nil
}
// getProfileBySocialName Get user profile by social name
func getProfileBySocialName(socialName string) (*models.UserProfileModel, error) {
profileURL := fmt.Sprintf("/profile/social/%s", socialName)
foundProfileData, err := functionCall(http.MethodGet, []byte(""), profileURL, nil)
if err != nil {
if err == NotFoundHTTPStatusError {
return nil, nil
}
log.Error("functionCall (%s) - %s", profileURL, err.Error())
return nil, fmt.Errorf("getProfileBySocialName/functionCall")
}
var foundProfile models.UserProfileModel
err = json.Unmarshal(foundProfileData, &foundProfile)
if err != nil {
log.Error("Unmarshal foundProfile - %s", err.Error())
return nil, fmt.Errorf("getProfileBySocialName/unmarshal")
}
return &foundProfile, nil
}
// getProfilesByUserIds Get user profiles by user IDs
func getProfilesByUserIds(model models.GetProfilesModel, userInfoInReq *UserInfoInReq) ([]models.UserProfileModel, error) {
profileURL := "/profile/dto/ids"
body, marshalErr := json.Marshal(model)
if marshalErr != nil {
errorMessage := fmt.Sprintf("Marshal models.GetProfilesModel Error %s", marshalErr.Error())
fmt.Println(errorMessage)
return nil, marshalErr
}
// Create user headers for http request
userHeaders := make(map[string][]string)
userHeaders["uid"] = []string{userInfoInReq.UserId.String()}
userHeaders["email"] = []string{userInfoInReq.Username}
userHeaders["avatar"] = []string{userInfoInReq.Avatar}
userHeaders["displayName"] = []string{userInfoInReq.DisplayName}
userHeaders["role"] = []string{userInfoInReq.SystemRole}
foundProfilesData, err := functionCall(http.MethodPost, body, profileURL, nil)
if err != nil {
if err == NotFoundHTTPStatusError {
return nil, nil
}
log.Error("functionCall (%s) - %s", profileURL, err.Error())
return nil, fmt.Errorf("getProfilesByUserIds/functionCall")
}
var foundProfile []models.UserProfileModel
err = json.Unmarshal(foundProfilesData, &foundProfile)
if err != nil {
log.Error("Unmarshal foundProfiles - %s", err.Error())
return nil, fmt.Errorf("getProfilesByUserIds/unmarshal")
}
return foundProfile, nil
}
// dispatchProfileByUserIds Dispatch profile by user Ids
func dispatchProfileByUserIds(model models.DispatchProfilesModel, userInfoInReq *UserInfoInReq) error {
profileURL := "/profile/dispatch"
body, marshalErr := json.Marshal(model)
if marshalErr != nil {
errorMessage := fmt.Sprintf("Marshal models.DispatchProfilesModel Error %s", marshalErr.Error())
fmt.Println(errorMessage)
}
// Create user headers for http request
userHeaders := make(map[string][]string)
userHeaders["uid"] = []string{userInfoInReq.UserId.String()}
userHeaders["email"] = []string{userInfoInReq.Username}
userHeaders["avatar"] = []string{userInfoInReq.Avatar}
userHeaders["displayName"] = []string{userInfoInReq.DisplayName}
userHeaders["role"] = []string{userInfoInReq.SystemRole}
_, err := functionCall(http.MethodPost, body, profileURL, userHeaders)
if err != nil {
if err == NotFoundHTTPStatusError {
return nil
}
log.Error("functionCall (%s) - %s", profileURL, err.Error())
return fmt.Errorf("dispatchProfileByUserIds/functionCall")
}
return nil
}