Skip to content

Commit

Permalink
Deleting chat will also delete messages
Browse files Browse the repository at this point in the history
  • Loading branch information
hsluoyz committed Mar 9, 2024
1 parent 4a150c7 commit a9fc0ff
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 0 deletions.
10 changes: 10 additions & 0 deletions controllers/chat.go
Original file line number Diff line number Diff line change
Expand Up @@ -140,5 +140,15 @@ func (c *ApiController) DeleteChat() {
return
}

message := object.Message{
Owner: chat.Owner,
Chat: chat.Name,
}
success, err = object.DeleteMessagesByChat(&message)
if err != nil {
c.ResponseError(err.Error())
return
}

c.ResponseOk(success)
}
15 changes: 15 additions & 0 deletions object/chat_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -157,6 +157,10 @@ func TestPrintChatUsers(t *testing.T) {
userMap := make(map[string]struct{})
var users []string
for _, chat := range chats {
if chat.User == "admin" {
continue
}

if _, exists := userMap[chat.User]; !exists {
userMap[chat.User] = struct{}{}
users = append(users, chat.User)
Expand All @@ -168,4 +172,15 @@ func TestPrintChatUsers(t *testing.T) {
for _, user := range users {
fmt.Printf("%s\n", user)
}

fmt.Printf("\nCount: %d\n\n", len(users))

var concatenatedUsers string
for i, user := range users {
if i > 0 {
concatenatedUsers += " or "
}
concatenatedUsers += fmt.Sprintf(`name = "%s"`, user)
}
fmt.Println(concatenatedUsers)
}
9 changes: 9 additions & 0 deletions object/message.go
Original file line number Diff line number Diff line change
Expand Up @@ -233,6 +233,15 @@ func DeleteMessage(message *Message) (bool, error) {
return affected != 0, nil
}

func DeleteMessagesByChat(message *Message) (bool, error) {
affected, err := adapter.engine.Delete(&Message{Owner: message.Owner, Chat: message.Chat})
if err != nil {
return false, err
}

return affected != 0, nil
}

func (message *Message) GetId() string {
return fmt.Sprintf("%s/%s", message.Owner, message.Name)
}
Expand Down
1 change: 1 addition & 0 deletions web/src/ChatListPage.js
Original file line number Diff line number Diff line change
Expand Up @@ -388,6 +388,7 @@ class ChatListPage extends React.Component {
dataIndex: "action",
key: "action",
width: "110px",
fixed: "right",
render: (text, record, index) => {
return (
<div>
Expand Down

0 comments on commit a9fc0ff

Please sign in to comment.