Skip to content

Commit

Permalink
[fix] #95 #96
Browse files Browse the repository at this point in the history
  • Loading branch information
yoneyan committed Jul 6, 2021
1 parent 5e0be70 commit 23ac43c
Show file tree
Hide file tree
Showing 6 changed files with 95 additions and 83 deletions.
1 change: 1 addition & 0 deletions pkg/api/core/controller/interface.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ type Chat struct {
ID uint `json:"id"`
Err string `json:"error"`
CreatedAt time.Time `json:"created_at"`
TicketID uint `json:"ticket_id"`
UserID uint `json:"user_id"`
UserName string `json:"user_name"`
GroupID uint `json:"group_id"`
Expand Down
13 changes: 4 additions & 9 deletions pkg/api/core/controller/v0/admin.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,15 +20,7 @@ func SendChatByAdmin(data controller.Chat) {
client := &http.Client{}
client.Timeout = time.Second * 5

body, _ := json.Marshal(controller.Chat{
Err: data.Err,
CreatedAt: data.CreatedAt,
UserID: data.UserID,
UserName: data.UserName,
GroupID: data.GroupID,
Admin: data.Admin,
Message: data.Message,
})
body, _ := json.Marshal(data)

//Header部分
header := http.Header{}
Expand Down Expand Up @@ -66,14 +58,17 @@ func ReceiveChatByAdmin(c *gin.Context) {

if err := auth.ControllerAuthentication(controller.Controller{Token1: token1, Token2: token2}); err != nil {
log.Println(err)
return
}

var input controller.Chat
log.Println(c.BindJSON(&input))

support.Broadcast <- support.WebSocketResult{
CreatedAt: time.Now(),
TicketID: input.TicketID,
UserID: input.UserID,
UserName: input.UserName,
GroupID: input.GroupID,
Admin: input.Admin,
Message: input.Message,
Expand Down
4 changes: 2 additions & 2 deletions pkg/api/core/controller/v0/user.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,7 @@ func SendChatUser(data controller.Chat) {
client := &http.Client{}
client.Timeout = time.Second * 5

body, _ := json.Marshal(controller.Chat{Err: data.Err, CreatedAt: data.CreatedAt, UserID: data.UserID,
GroupID: data.GroupID, Admin: data.Admin, Message: data.Message})
body, _ := json.Marshal(data)

//Header部分
header := http.Header{}
Expand Down Expand Up @@ -72,6 +71,7 @@ func ReceiveChatUser(c *gin.Context) {

support.Broadcast <- support.WebSocketResult{
CreatedAt: time.Now(),
TicketID: input.TicketID,
UserID: input.UserID,
GroupID: input.GroupID,
Admin: input.Admin,
Expand Down
1 change: 1 addition & 0 deletions pkg/api/core/support/interface.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ type WebSocketResult struct {
ID uint `json:"id"`
Err string `json:"error"`
CreatedAt time.Time `json:"created_at"`
TicketID uint `json:"ticket_id"`
UserToken string `json:"user_token"`
AccessToken string `json:"access_token"`
UserID uint `json:"user_id"`
Expand Down
67 changes: 37 additions & 30 deletions pkg/api/core/support/ticket/v0/admin.go
Original file line number Diff line number Diff line change
Expand Up @@ -251,13 +251,14 @@ func GetAdminWebSocket(c *gin.Context) {

_, err = dbChat.Create(&core.Chat{
TicketID: ticketResult.Tickets[0].ID,
UserID: &resultAdmin.AdminID,
UserID: nil,
Admin: true,
Data: msg.Message,
})
if err != nil {
conn.WriteJSON(&support.WebSocketResult{Err: "db write error"})
} else {
msg.TicketID = uint(id)
msg.UserID = resultAdmin.AdminID
msg.GroupID = groupID
msg.UserName = "HomeNOC(運営)"
Expand All @@ -268,6 +269,7 @@ func GetAdminWebSocket(c *gin.Context) {

//Admin側に送信
controller.SendChatByAdmin(controllerInterface.Chat{
TicketID: uint(id),
CreatedAt: msg.CreatedAt,
Admin: msg.Admin,
UserID: resultAdmin.AdminID,
Expand Down Expand Up @@ -309,40 +311,45 @@ func GetAdminWebSocket(c *gin.Context) {
func HandleMessagesByAdmin() {
for {
msg := <-support.Broadcast
log.Println(msg)
log.Println(msg.TicketID)

//登録されているクライアント宛にデータ送信する
for client := range support.Clients {
// ユーザのみの場合
log.Println(msg)
if msg.GroupID == 0 {
err := client.Socket.WriteJSON(support.WebSocketChatResponse{
Time: time.Now().UTC().Add(9 * time.Hour).Format(timeLayout),
UserID: msg.UserID,
UserName: msg.UserName,
GroupID: 0,
Admin: msg.Admin,
Message: msg.Message,
})
if err != nil {
log.Printf("error: %v", err)
client.Socket.Close()
delete(support.Clients, client)
}
} else if client.GroupID == msg.GroupID {
err := client.Socket.WriteJSON(support.WebSocketChatResponse{
Time: time.Now().UTC().Add(9 * time.Hour).Format(timeLayout),
UserID: msg.UserID,
UserName: msg.UserName,
GroupID: msg.GroupID,
Admin: msg.Admin,
Message: msg.Message,
})
if err != nil {
log.Printf("error: %v", err)
client.Socket.Close()
delete(support.Clients, client)
if client.TicketID == msg.TicketID {
if msg.GroupID == 0 {
err := client.Socket.WriteJSON(support.WebSocketChatResponse{
Time: time.Now().UTC().Add(9 * time.Hour).Format(timeLayout),
UserID: msg.UserID,
UserName: msg.UserName,
GroupID: 0,
Admin: msg.Admin,
Message: msg.Message,
})
if err != nil {
log.Printf("error: %v", err)
client.Socket.Close()
delete(support.Clients, client)
}
} else if client.GroupID == msg.GroupID {
err := client.Socket.WriteJSON(support.WebSocketChatResponse{
Time: time.Now().UTC().Add(9 * time.Hour).Format(timeLayout),
UserID: msg.UserID,
UserName: msg.UserName,
GroupID: msg.GroupID,
Admin: msg.Admin,
Message: msg.Message,
})
if err != nil {
log.Printf("error: %v", err)
client.Socket.Close()
delete(support.Clients, client)
}
} else {
// 認証失敗時の処理
}
} else {
// 認証失敗時の処理
}
}
}
Expand Down
92 changes: 50 additions & 42 deletions pkg/api/core/support/ticket/v0/ticket.go
Original file line number Diff line number Diff line change
Expand Up @@ -263,17 +263,19 @@ func GetWebSocket(c *gin.Context) {
return
}

// check groupID
if ticketResult.Tickets[0].GroupID == nil && ticketResult.Tickets[0].GroupID != result.User.GroupID {
//if ticketResult.Tickets[0].GroupID != result.User.GroupID {

log.Println("groupID not match.")
return
}
// check userID
if ticketResult.Tickets[0].GroupID == nil && ticketResult.Tickets[0].UserID != &result.User.ID {
log.Println("userID not match.")
return
// [group ticket] check groupID
if ticketResult.Tickets[0].GroupID != nil {
// [group ticket] check groupID
if ticketResult.Tickets[0].GroupID != result.User.GroupID {
log.Println("groupID not match.")
return
}
} else {
// [user ticket] check userID
if ticketResult.Tickets[0].UserID != nil && *ticketResult.Tickets[0].UserID != result.User.ID {
log.Println("userID not match.")
return
}
}

var groupID uint = 0
Expand Down Expand Up @@ -333,7 +335,7 @@ func GetWebSocket(c *gin.Context) {
if err != nil {
conn.WriteJSON(&support.WebSocketResult{Err: "db write error"})
} else {

msg.TicketID = ticketResult.Tickets[0].ID
msg.UserID = result.User.ID
msg.GroupID = groupID
msg.Admin = false
Expand All @@ -342,8 +344,9 @@ func GetWebSocket(c *gin.Context) {
msg.AccessToken = ""
msg.UserToken = ""

//ユーザ側に送信
//管理側に送信
controller.SendChatUser(controllerInterface.Chat{
TicketID: ticketResult.Tickets[0].ID,
CreatedAt: msg.CreatedAt,
UserID: result.User.ID,
UserName: result.User.Name,
Expand Down Expand Up @@ -371,39 +374,44 @@ func HandleMessages() {
for {
msg := <-support.Broadcast

log.Println(msg)
log.Println(msg.TicketID)

//登録されているクライアント宛にデータ送信する
for client := range support.Clients {
// ユーザのみの場合
if msg.GroupID == 0 && client.GroupID == 0 && client.UserID == msg.UserID {
err := client.Socket.WriteJSON(support.WebSocketChatResponse{
Time: time.Now().UTC().Add(9 * time.Hour).Format(timeLayout),
UserID: msg.UserID,
UserName: msg.UserName,
GroupID: 0,
Admin: msg.Admin,
Message: msg.Message,
})
if err != nil {
log.Printf("error: %v", err)
client.Socket.Close()
delete(support.Clients, client)
}
} else if client.GroupID == msg.GroupID {
err := client.Socket.WriteJSON(support.WebSocketChatResponse{
Time: time.Now().UTC().Add(9 * time.Hour).Format(timeLayout),
UserID: msg.UserID,
UserName: msg.UserName,
GroupID: msg.GroupID,
Admin: msg.Admin,
Message: msg.Message,
})
if err != nil {
log.Printf("error: %v", err)
client.Socket.Close()
delete(support.Clients, client)
if client.TicketID == msg.TicketID {
if msg.GroupID == 0 && client.GroupID == 0 && client.UserID == msg.UserID {
err := client.Socket.WriteJSON(support.WebSocketChatResponse{
Time: time.Now().UTC().Add(9 * time.Hour).Format(timeLayout),
UserID: msg.UserID,
UserName: msg.UserName,
GroupID: 0,
Admin: msg.Admin,
Message: msg.Message,
})
if err != nil {
log.Printf("error: %v", err)
client.Socket.Close()
delete(support.Clients, client)
}
} else if client.GroupID == msg.GroupID {
err := client.Socket.WriteJSON(support.WebSocketChatResponse{
Time: time.Now().UTC().Add(9 * time.Hour).Format(timeLayout),
UserID: msg.UserID,
UserName: msg.UserName,
GroupID: msg.GroupID,
Admin: msg.Admin,
Message: msg.Message,
})
if err != nil {
log.Printf("error: %v", err)
client.Socket.Close()
delete(support.Clients, client)
}
} else {
// 認証失敗時の処理
}
} else {
// 認証失敗時の処理
}
}
}
Expand Down

0 comments on commit 23ac43c

Please sign in to comment.