diff options
| -rw-r--r-- | go-src/chat.go | 57 |
1 files changed, 57 insertions, 0 deletions
diff --git a/go-src/chat.go b/go-src/chat.go index 575a88f..e95a094 100644 --- a/go-src/chat.go +++ b/go-src/chat.go @@ -3,11 +3,19 @@ package ctfc import ( "bytes" "os" + "strings" "gitlab.com/Syndamia/ctfc/go-src/folderPaths" "gitlab.com/Syndamia/ctfc/go-src/utils" ) +type ChatProp int + +const ( + chatNameProp ChatProp = iota + 1 + chatDescriptionProp +) + type Chat struct { Name string Description string @@ -22,6 +30,55 @@ func (ch Chat) addMessage(value string, creatorUsername string) { utils.AppendToFile(folderPaths.FileAtChatsFolder(ch.Name), value+"\n") } +func (ch Chat) updateProp(currentPassword string, newValue string, chatProp ChatProp) bool { + if !loggedInUser.ValidatePassword(currentPassword) { + return false + } + + oldName, oldDesc := ch.Name, ch.Description + + chatFile := utils.GetFileContents(folderPaths.FileAtChatsFolder(ch.Name)) + + switch chatProp { + case chatNameProp: + chatFile[0] = newValue + ch.Name = newValue + case chatDescriptionProp: + chatFile[1] = newValue + ch.Description = newValue + default: + return false + } + + os.WriteFile( + folderPaths.FileAtChatsFolder(oldName), + []byte(strings.Join(chatFile, "\n")), + 0644) + + if oldName != ch.Name { + os.Rename(folderPaths.FileAtChatsFolder(oldName), folderPaths.FileAtChatsFolder(ch.Name)) + } + + allChatsFile, _ := os.ReadFile(folderPaths.AllChatsFilePath()) + allChatsUpdatedFile := strings.Replace(string(allChatsFile), oldName+" : "+oldDesc, ch.Name+" : "+ch.Description, 1) + + os.WriteFile( + folderPaths.AllChatsFilePath(), + []byte(allChatsUpdatedFile), + 0644, + ) + + return true +} + +func (ch Chat) UpdateName(password string, newName string) bool { + return ch.updateProp(password, newName, chatNameProp) +} + +func (ch Chat) UpdateDescription(password string, newDesc string) bool { + return ch.updateProp(password, newDesc, chatDescriptionProp) +} + func createChat(data ...string) { chatFile, _ := os.Create(folderPaths.FileAtChatsFolder(data[0])) chatFile.WriteString(data[0] + "\n" + data[1] + "\n" + data[2] + "\n") |
