File tree 2 files changed +20
-7
lines changed
2 files changed +20
-7
lines changed Original file line number Diff line number Diff line change @@ -142,8 +142,8 @@ func ListChatRecords(c *fiber.Ctx) (err error) {
142
142
143
143
// ListMyChatRecords @ListMyChatRecords
144
144
// @Router /api/chats/{id}/records/me [get]
145
- // @Summary list my records
146
- // @Description list my records
145
+ // @Summary list my records in a chat
146
+ // @Description list my records in a chat
147
147
// @Tags ChatRecord
148
148
// @Accept json
149
149
// @Produce json
@@ -259,6 +259,11 @@ func ListMyRecords(c *fiber.Ctx) (err error) {
259
259
// @param Authorization header string true "Bearer和token空格拼接"
260
260
func MossChat (c * websocket.Conn ) {
261
261
var err error
262
+ userID , err := LoadUserFromWs (c )
263
+ if err != nil {
264
+ _ = c .WriteJSON (common .Unauthorized ("Unauthorized" ))
265
+ return
266
+ }
262
267
defer func () {
263
268
if err != nil {
264
269
utils .Logger .Error (
@@ -289,7 +294,7 @@ func MossChat(c *websocket.Conn) {
289
294
log .Println ("Received message from client:" , requestMessage )
290
295
go addRecord (AddRecordsRequest {
291
296
CreatedAt : time .Now (),
292
- UserID : 0 ,
297
+ UserID : userID ,
293
298
RoomID : "moss" ,
294
299
Type : "toMoss" ,
295
300
ToID : 0 ,
@@ -339,7 +344,7 @@ func MossChat(c *websocket.Conn) {
339
344
UserID : 0 ,
340
345
RoomID : "moss" ,
341
346
Type : "fromMoss" ,
342
- ToID : 0 ,
347
+ ToID : userID ,
343
348
Message : mossResponse .Output ,
344
349
})
345
350
return
Original file line number Diff line number Diff line change @@ -160,15 +160,23 @@ func GetUserIDFromWs(c *websocket.Conn) (int, error) {
160
160
if ! ok {
161
161
return 0 , common .Unauthorized ()
162
162
}
163
+ userID := int (id .(float64 ))
164
+ var userJwtSecret UserJwtSecret
165
+ err = DB .Take (& userJwtSecret , userID ).Error
166
+ if err != nil {
167
+ return 0 , common .Unauthorized ("Unauthorized" )
168
+ }
169
+ CheckJWTToken (token , userJwtSecret .Secret )
163
170
return int (id .(float64 )), nil
164
171
}
165
172
166
- func LoadUserFromWs (c * websocket.Conn ) (* User , error ) {
173
+ func LoadUserFromWs (c * websocket.Conn ) (int , error ) {
167
174
userID , err := GetUserIDFromWs (c )
168
175
if err != nil {
169
- return nil , err
176
+ return 0 , err
170
177
}
171
- return LoadUserByID (userID )
178
+ return userID , nil
179
+ //LoadUserByID(userID)
172
180
}
173
181
174
182
// parseJWT extracts and parse token
You can’t perform that action at this time.
0 commit comments