aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--go-src/directMessageChat.go65
-rw-r--r--go-src/utils/utils.go16
-rw-r--r--go-src/windowMessages.go8
-rw-r--r--go-src/windows.go120
-rw-r--r--go-src/windowsHelpers.go2
5 files changed, 207 insertions, 4 deletions
diff --git a/go-src/directMessageChat.go b/go-src/directMessageChat.go
new file mode 100644
index 0000000..de287ed
--- /dev/null
+++ b/go-src/directMessageChat.go
@@ -0,0 +1,65 @@
+package ctfc
+
+import (
+ "bytes"
+ "os"
+ "path/filepath"
+ "strings"
+
+ "gitlab.com/Syndamia/ctfc/go-src/folderPaths"
+ "gitlab.com/Syndamia/ctfc/go-src/utils"
+)
+
+type DirectMessageChat struct {
+ Author1 User
+ Author2 User
+ Messages []string
+}
+
+func (dmc DirectMessageChat) genFileName() string {
+ if dmc.Author1.Username > dmc.Author2.Username {
+ return dmc.Author1.Username + " " + dmc.Author2.Username
+ }
+ return dmc.Author2.Username + " " + dmc.Author1.Username
+}
+
+func (dmc DirectMessageChat) addMessage(value string, username string) {
+ if dmc.Author1.Username != username && dmc.Author2.Username != username {
+ return
+ }
+
+ value = username + " : " + value
+ dmc.Messages = append(dmc.Messages, value)
+
+ fileName := folderPaths.FileAtDirectMessagesFolder(dmc.genFileName())
+
+ if !utils.PathExists(fileName) {
+ utils.CreateFile(fileName)
+ }
+ utils.AppendToFile(fileName, value+"\n")
+}
+
+func getDirectMessageChat(otherAuthorUsername string) (dmc DirectMessageChat) {
+ dmc = DirectMessageChat{loggedInUser, getUser(otherAuthorUsername), []string{}}
+
+ if utils.PathExists(folderPaths.FileAtDirectMessagesFolder(dmc.genFileName())) {
+ f, _ := os.ReadFile(folderPaths.FileAtDirectMessagesFolder(dmc.genFileName()))
+ values := bytes.Split(f, []byte("\n"))
+ dmc.Messages = utils.TwoDByteArrayToStringArray(values[:len(values)-1])
+ }
+
+ return
+}
+
+func getAllDirectMessageChats() (chats []string) {
+ filepath.Walk(folderPaths.FileAtDirectMessagesFolder(""), func(path string, info os.FileInfo, err error) error {
+ if strings.Contains(path, loggedInUser.Username) {
+ authors := utils.StrMSplit(path, "/", ".", " ")
+ authors = authors[len(authors)-3 : len(authors)-1]
+ otherAuthor := getUser(utils.If(authors[0] == loggedInUser.Username).String(authors[1], authors[0]))
+ chats = append(chats, otherAuthor.Name+" ("+otherAuthor.Username+")")
+ }
+ return nil
+ })
+ return
+}
diff --git a/go-src/utils/utils.go b/go-src/utils/utils.go
index d6fc47c..271dd3e 100644
--- a/go-src/utils/utils.go
+++ b/go-src/utils/utils.go
@@ -3,6 +3,7 @@ package utils
import (
"math"
"os"
+ "strings"
)
// Repeats a rune given amount of times and returns the result as a string
@@ -53,6 +54,21 @@ func StrShortenRight(s *string, amount int) {
*s = (*s)[:len(*s)-amount]
}
+func StrMSplit(s string, seps ...string) (sret []string) {
+ for _, sep := range seps {
+ if len(sret) == 0 {
+ sret = strings.Split(s, sep)
+ } else {
+ var tmp []string
+ for _, sepStr := range sret {
+ tmp = append(tmp, strings.Split(sepStr, sep)...)
+ }
+ sret = tmp
+ }
+ }
+ return
+}
+
func MaxInt(x int, y int) int {
if x > y {
return x
diff --git a/go-src/windowMessages.go b/go-src/windowMessages.go
index 786a3e5..12bbace 100644
--- a/go-src/windowMessages.go
+++ b/go-src/windowMessages.go
@@ -13,9 +13,11 @@ const (
editNameNavTitle = "Editing Name"
// 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]"
- accountWindowHelpMsg = "Account options : [(C)hats/(D)irect Messages/(L)ogout/(number) for edit option by number/(H)elp]"
+ 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]"
+ 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]"
// Input names
usernameInName = "Username"
diff --git a/go-src/windows.go b/go-src/windows.go
index 50bc5d5..c8e7234 100644
--- a/go-src/windows.go
+++ b/go-src/windows.go
@@ -221,6 +221,126 @@ func chatWindow(values ...string) {
lastLine = -2 // Practically stops execution of the paginated subwindow routine
}
+func directMessagesWindow(values ...string) {
+ csi.ClearScreen()
+
+ initPaginatedValues(0, &values)
+
+ ui.NormalBox(true, chatNavTitle, accountNavTitle, logoutNavTitle)
+
+ allDirectMessageChats := getAllDirectMessageChats()
+ 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(&allDirectMessageChats,
+ func(s *[]string) { *s = (getAllDirectMessageChats()) },
+ &lastLine,
+ len(values[0]),
+ customLinesBelow,
+ true,
+ )
+
+ if values[1] == showHelp {
+ ui.TextField(directMessagesWindowHelpMsg)
+ }
+
+ input := ui.InputField(chatsSpec)
+
+ nextWindow := handleInputActions(input, true,
+ inputAction{"H", directMessagesWindow, []string{values[0], showHelp}},
+ )
+
+ if nextWindow == nil {
+ // If user input is number, navigate to chat of given number, else show error
+ if chatI, err := strconv.Atoi(input); chatI >= 0 && chatI <= len(allDirectMessageChats) && err == nil {
+ defer directMessageWindow(strings.Trim(strings.Split(allDirectMessageChats[utils.MaxInt(len(allDirectMessageChats)-pageSize*len(values[0]), 0)+chatI-1], " (")[1], ")"))
+ } else if input == ">" {
+ // If possible, increment to the next page (adds a dot to the end of the string)
+ if len(values[0]) < totalPages(len(allDirectMessageChats)) {
+ values[0] += "."
+ }
+ defer directMessagesWindow(values...)
+ } else if input == "<" {
+ // If possible, decrement to the previous page (removes a dot from the string)
+ if len(values[0]) > 1 {
+ utils.StrShortenRight(&values[0], 1)
+ }
+ defer directMessagesWindow(values...)
+ } else if usernameExists(input) {
+ defer directMessageWindow(input)
+ } else {
+ defer showError(invalidCommand, chatsWindow)
+ }
+ } else {
+ defer nextWindow()
+ }
+ lastLine = -2
+
+}
+
+func directMessageWindow(values ...string) {
+ csi.ClearScreen()
+
+ // We determine page number by length of a string
+ // This method should be faster than having to cast to int all the time
+ initPaginatedValues(1, &values)
+
+ currDirectMessagesChat := getDirectMessageChat(values[0])
+
+ ui.NormalBox(true, chatNavTitle, directMessagesNavTitle, accountNavTitle, logoutNavTitle)
+ ui.TextField(currDirectMessagesChat.Author2.Name + " (" + currDirectMessagesChat.Author2.Username + ")")
+
+ ui.EmptyLines(pageSize + 3) // empty line + messages lines + page number line + empty line
+
+ customLinesBelow := utils.If(values[2] == showHelp).Int(3, 2)
+
+ lastLine := -1
+ go routinePaginatedSubwindow(&currDirectMessagesChat.Messages,
+ func(s *[]string) { *s = getDirectMessageChat(values[0]).Messages },
+ &lastLine,
+ len(values[1]),
+ customLinesBelow,
+ false,
+ )
+
+ if values[2] == showHelp {
+ ui.TextField(directMessageWindowHelpMsg)
+ values[2] = ""
+ }
+
+ input := ui.InputField(chatWindowSpec)
+
+ nextWindow := handleInputActions(input, true,
+ inputAction{"H", directMessageWindow, []string{values[0], values[1], showHelp}},
+ )
+
+ if nextWindow == nil {
+ switch input {
+ case ">":
+ // If possible, increment to the next page (adds a dot to the end of the string)
+ if len(values[1]) < totalPages(len(currDirectMessagesChat.Messages)) {
+ values[1] += "."
+ }
+ case "<":
+ // If possible, decrement to the previous page (removes a dot from the string)
+ if len(values[1]) > 1 {
+ utils.StrShortenRight(&values[1], 1)
+ }
+ default: // Send a message
+ values[1] = "." // Make sure to be at the first page, when sending a message
+ // Since "C" is a command, to just enter the letter C you'll need to input "\C"
+ currDirectMessagesChat.addMessage(strings.TrimPrefix(input, "\\"), loggedInUser.Username)
+ }
+
+ defer directMessageWindow(values...)
+ } else {
+ defer nextWindow()
+ }
+ lastLine = -2 // Practically stops execution of the paginated subwindow routine
+}
+
func accountWindow(values ...string) {
csi.ClearScreen()
diff --git a/go-src/windowsHelpers.go b/go-src/windowsHelpers.go
index f3b7ff0..9b1d2d9 100644
--- a/go-src/windowsHelpers.go
+++ b/go-src/windowsHelpers.go
@@ -76,7 +76,7 @@ func handleInputActions(input string, handleNav bool, ia ...inputAction) func()
if handleNav {
ia = append(ia,
inputAction{"C", chatsWindow, nil},
- inputAction{"D", chatsWindow, nil},
+ inputAction{"D", directMessagesWindow, nil},
inputAction{"A", accountWindow, nil},
inputAction{"L", logoutWindow, nil},
)