diff options
| author | Syndamia <kamen.d.mladenov@protonmail.com> | 2021-09-11 18:21:17 +0300 |
|---|---|---|
| committer | Syndamia <kamen.d.mladenov@protonmail.com> | 2021-09-11 18:21:17 +0300 |
| commit | 00361b1099952939bf5bdede7841659b888dd80c (patch) | |
| tree | c16bcdd35adc9c4a3193ab694162205f2c63ed99 | |
| parent | 31918645958b2bf80409a5bcf99d9bb2e775b7b7 (diff) | |
| download | ctfc-00361b1099952939bf5bdede7841659b888dd80c.tar ctfc-00361b1099952939bf5bdede7841659b888dd80c.tar.gz ctfc-00361b1099952939bf5bdede7841659b888dd80c.zip | |
Implemented editing chats
| -rw-r--r-- | go-src/chat.go | 10 | ||||
| -rw-r--r-- | go-src/utils/utils.go | 5 | ||||
| -rw-r--r-- | go-src/windowMessages.go | 32 | ||||
| -rw-r--r-- | go-src/windows.go | 100 |
4 files changed, 133 insertions, 14 deletions
diff --git a/go-src/chat.go b/go-src/chat.go index e95a094..2cc54a6 100644 --- a/go-src/chat.go +++ b/go-src/chat.go @@ -99,3 +99,13 @@ func getAllChats() []string { chats := utils.TwoDByteArrayToStringArray(values) return chats[:len(chats)-1] } + +func getOwnChats() (ownChats []string) { + for _, v := range getAllChats() { + f, _ := os.ReadFile(folderPaths.FileAtChatsFolder(strings.Split(v, " : ")[0])) + if string(bytes.Split(f, []byte("\n"))[2]) == loggedInUser.Username { + ownChats = append(ownChats, v) + } + } + return +} diff --git a/go-src/utils/utils.go b/go-src/utils/utils.go index 271dd3e..537037f 100644 --- a/go-src/utils/utils.go +++ b/go-src/utils/utils.go @@ -50,6 +50,11 @@ func PathExists(path string) bool { return !os.IsNotExist(err) } +func GetFileContents(path string) []string { + input, _ := os.ReadFile(path) + return strings.Split(string(input), "\n") +} + func StrShortenRight(s *string, amount int) { *s = (*s)[:len(*s)-amount] } diff --git a/go-src/windowMessages.go b/go-src/windowMessages.go index 12bbace..88e3b56 100644 --- a/go-src/windowMessages.go +++ b/go-src/windowMessages.go @@ -8,6 +8,10 @@ const ( accountNavTitle = "Account" logoutNavTitle = "Logout" createChatNavTitle = "Creating a new chat" + editChatsNavTitle = "Edit own chat" + editChatNavTitle = "Editing chat" + editChatNameNavTitle = "Editing chat name" + editChatDescNavTitle = "Editing chat description" chatNavTitle = "Chats" editPasswordNavTitle = "Editing Password" editNameNavTitle = "Editing Name" @@ -15,6 +19,8 @@ const ( // Help messages chatsWindowHelpMsg = "Chats page options: [(D)irect messages/(A)ccount/(L)ogout/(<) for previous page/(>) for next page/(C) for create chat/(name) for go to chat room by name/(number) for go to chat room by number/(H)elp]" chatWindowHelpMsg = "Chat options : [(C)hats/(D)irect messages/(A)ccount/(L)ogout/(<) for previous page/(>) for next page/(H)elp]" + editChatsWindowHelpMsg = "Edit own chat options : [(B)ack to chats page/(<) for previous page/(>) for next page/(name) to edit chat room with name/(number) to edit chat room with number/(H)elp]" + editChatWindowHelpMsg = "Editing chat options : [(B)ack to chats page/(number) for edit option by number/(H)elp]" directMessagesWindowHelpMsg = "Direct Messages page options: [(C)hats/(A)ccount/(L)ogout/(<) for previous page/(>) for next page/(name) for start chatting with username/(number) for go to direct messages by number/(H)elp]" directMessageWindowHelpMsg = "Direct Message options : [(C)hats/(D)irect messages/(A)ccount/(L)ogout/(<) for previous page/(>) for next page/(H)elp]" accountWindowHelpMsg = "Account options : [(C)hats/(D)irect Messages/(L)ogout/(number) for edit option by number/(H)elp]" @@ -25,22 +31,26 @@ const ( nameInName = "Name" chatNameInName = "Chat name" chatDescInName = "Description" + newChatNameInName = "New chat name" + newChatDescInName = "New chat description" currentPasswordInName = "Current password" newPasswordInName = "New password" newNameInName = "New name" // Input specifications - startupWindowSpec = "[L/R]" - chatWindowSpec = "Message or [C/D/A/L/</>/H]" - accountWindowSpec = "[C/D/L/number/H]" - logoutWindowSpec = "[Y/N]" - inputBackSpec = " or [B for Back to start page]" - usernameSpec = " [A-z, 0-9, *, ., _, -; at least 1 letter] or [B for Back to start page]" - passwordSpec = " [5-40 characters; at least 1 number]" - nameSpec = " [2-60 characters]" - chatsSpec = "[D/A/L/</>/C/E/name/number/H]" - chatNameSpec = " [2-20 characters; A-z, 0-9, spaces, _, -] or [B for Back to chats page]" - chatDescSpec = " [0-30 characters]" + startupWindowSpec = "[L/R]" + chatWindowSpec = "Message or [C/D/A/L/</>/H]" + editChatsWindowSpec = "[B/</>/name/number/H]" + editChatWindowSpec = "[B/number/H]" + accountWindowSpec = "[C/D/L/number/H]" + logoutWindowSpec = "[Y/N]" + inputBackSpec = " or [B for Back to start page]" + usernameSpec = " [A-z, 0-9, *, ., _, -; at least 1 letter] or [B for Back to start page]" + passwordSpec = " [5-40 characters; at least 1 number]" + nameSpec = " [2-60 characters]" + chatsSpec = "[D/A/L/</>/C/E/name/number/H]" + chatNameSpec = " [2-20 characters; A-z, 0-9, spaces, _, -] or [B for Back to chats page]" + chatDescSpec = " [0-30 characters]" // Other hiddenValue = "[HIDDEN]" diff --git a/go-src/windows.go b/go-src/windows.go index 73f8733..40f56fc 100644 --- a/go-src/windows.go +++ b/go-src/windows.go @@ -103,6 +103,7 @@ func chatsWindow(values ...string) { nextWindow := handleInputActions(input, true, inputAction{"C", createChatWindow, nil}, + inputAction{"E", editChatsWindow, nil}, inputAction{"H", chatsWindow, []string{values[0], showHelp}}, ) @@ -140,6 +141,99 @@ func createChatWindow(values ...string) { defer chatWindow(inputs[0]) } +func editChatsWindow(values ...string) { + csi.ClearScreen() + + initPaginatedValues(0, &values) + + ui.NormalBox(true, editChatsNavTitle) + + ownChats := getOwnChats() + ui.EmptyLines(pageSize + 3) // empty line + message lines + page number line + empty line + + customLinesBelow := utils.If(values[1] == showHelp).Int(3, 2) + + lastLine := -1 + go routinePaginatedSubwindow(&ownChats, + func(s *[]string) { *s = getOwnChats() }, + &lastLine, + len(values[0]), + customLinesBelow, + true, + ) + + if values[1] == showHelp { + ui.TextField(editChatsWindowHelpMsg) + } + + input := ui.InputField(editChatsWindowSpec) + + nextWindow := handleInputActions(input, true, + inputAction{"B", chatsWindow, nil}, + inputAction{"H", editChatsWindow, []string{values[0], showHelp}}, + ) + + if nextWindow == nil { + defer handleComplexInputActions(input, + ownChats, + " : ", + &values[0], + editChatsWindow, + values, + editChatWindow, + chatNameExists, + ) + } else { + defer nextWindow() + } + lastLine = -2 +} + +func editChatWindow(values ...string) { + csi.ClearScreen() + + currChat := getChat(values[0]) + + ui.NormalBox(true, editChatNavTitle) + ui.NumberedFields( + chatNameInName+" : "+currChat.Name, + chatDescInName+" : "+currChat.Description, + ) + + ui.EmptyLine() + if len(values) > 1 { + if values[1] == showHelp { + ui.TextField(editChatWindowHelpMsg) + } + } + input := ui.InputField(editChatWindowSpec) + + nextWindow := handleInputActions(input, false, + inputAction{"B", editChatsWindow, nil}, + inputAction{"H", editChatWindow, []string{values[0], showHelp}}, + ) + + if nextWindow == nil { + forReturn := func(...string) { + defer editChatWindow(values...) + } + defer validatedMultiForm(input, forReturn, + multiFormProp{int(chatNameProp), editChatNameNavTitle, + formInput{newChatNameInName, chatNameSpec, stringValidChatName}, + func(fvalues []string) bool { return currChat.UpdateName(fvalues[0], fvalues[1]) }, + nil, + }, + multiFormProp{int(chatDescriptionProp), editNameNavTitle, + formInput{newChatDescInName, chatDescSpec, stringValidChatDesc}, + func(fvalues []string) bool { return currChat.UpdateDescription(fvalues[0], fvalues[1]) }, + nil, + }, + ) + } else { + defer nextWindow() + } +} + func chatWindow(values ...string) { csi.ClearScreen() @@ -339,13 +433,13 @@ func accountWindow(values ...string) { defer validatedMultiForm(input, accountWindow, multiFormProp{int(userPasswordProp), editPasswordNavTitle, formInput{newPasswordInName, passwordSpec, stringValidPassword}, - func(values []string) bool { return loggedInUser.UpdatePassword(values[0], values[1]) }, + func(fvalues []string) bool { return loggedInUser.UpdatePassword(fvalues[0], fvalues[1]) }, nil, }, multiFormProp{int(userNameProp), editNameNavTitle, formInput{newNameInName, nameSpec, stringValidName}, - func(values []string) bool { return loggedInUser.UpdateName(values[0], values[1]) }, - func(values []string) { loggedInUser.Name = values[1] }, + func(fvalues []string) bool { return loggedInUser.UpdateName(fvalues[0], fvalues[1]) }, + func(fvalues []string) { loggedInUser.Name = fvalues[1] }, }, ) } else { |
