aboutsummaryrefslogtreecommitdiff
path: root/go-src
diff options
context:
space:
mode:
authorSyndamia <kamen.d.mladenov@protonmail.com>2021-09-11 18:10:55 +0300
committerSyndamia <kamen.d.mladenov@protonmail.com>2021-09-11 18:10:55 +0300
commit8400b2398d09221953697b1e036f504b59575e69 (patch)
treef30edbb44015af0cda39334d3e7a1df36c331946 /go-src
parent9ba9248ebea1209db2c0f005135fa7bfaf8dedf6 (diff)
downloadctfc-8400b2398d09221953697b1e036f504b59575e69.tar
ctfc-8400b2398d09221953697b1e036f504b59575e69.tar.gz
ctfc-8400b2398d09221953697b1e036f504b59575e69.zip
Implemented chat property updating
Diffstat (limited to 'go-src')
-rw-r--r--go-src/chat.go57
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")