Skip to content

Commit 033e7aa

Browse files
committed
allow creating and managing groups
1 parent 53e404d commit 033e7aa

File tree

2 files changed

+105
-4
lines changed

2 files changed

+105
-4
lines changed

main.go

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ import (
1616
"gitlab.com/tslocum/cbind"
1717
)
1818

19-
var VERSION string = "v0.9.9"
19+
var VERSION string = "v1.0.0"
2020

2121
var sndTxt string = ""
2222
var currentReceiver messages.Chat = messages.Chat{}
@@ -385,7 +385,6 @@ func PrintHelp() {
385385
fmt.Fprintln(textView, "[::b] Up/Down[::-] = Scroll history/chats")
386386
fmt.Fprintln(textView, "[::b]", config.Config.Keymap.SwitchPanels, "[::-] = Switch input/chats")
387387
fmt.Fprintln(textView, "[::b]", config.Config.Keymap.FocusMessages, "[::-] = Focus message panel")
388-
fmt.Fprintln(textView, "[::b]", config.Config.Keymap.CommandCopyuser, "[::-] = Copy selected user id")
389388
fmt.Fprintln(textView, "")
390389
fmt.Fprintln(textView, "[-::-]Message panel[-::-]")
391390
fmt.Fprintln(textView, "[::b] Up/Down[::-] = select message")
@@ -411,7 +410,14 @@ func PrintHelp() {
411410
fmt.Fprintln(textView, "[::b] "+cmdPrefix+"sendimage[::-] /path/to/file = Send image message")
412411
fmt.Fprintln(textView, "[::b] "+cmdPrefix+"sendvideo[::-] /path/to/file = Send video message")
413412
fmt.Fprintln(textView, "[::b] "+cmdPrefix+"sendaudio[::-] /path/to/file = Send audio message")
413+
fmt.Fprintln(textView, "")
414+
fmt.Fprintln(textView, "[-::-]Groups[-::-]")
414415
fmt.Fprintln(textView, "[::b] "+cmdPrefix+"leave[::-] = Leave group")
416+
fmt.Fprintln(textView, "[::b] "+cmdPrefix+"create[::-] [user-id[] [user-id[] Group Subject = Create group with users")
417+
fmt.Fprintln(textView, "[::b] "+cmdPrefix+"add[::-] [user-id[] = Add user to group")
418+
fmt.Fprintln(textView, "[::b] "+cmdPrefix+"admin[::-] [user-id[] = Set admin role for user in group")
419+
fmt.Fprintln(textView, "[::b] "+cmdPrefix+"subject[::-] New Subject = Change subject of group")
420+
fmt.Fprintln(textView, "Use[::b]", config.Config.Keymap.CommandCopyuser, "[::-]to copy a selected user id)")
415421
fmt.Fprintln(textView, "")
416422
fmt.Fprintln(textView, "Configuration:")
417423
fmt.Fprintln(textView, " ->", config.GetConfigFilePath())

messages/session_manager.go

Lines changed: 97 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package messages
22

33
import (
44
"encoding/gob"
5+
"encoding/json"
56
"errors"
67
"fmt"
78
"io/ioutil"
@@ -544,8 +545,9 @@ func (sm *SessionManager) execCommand(command Command) {
544545
}
545546
case "leave":
546547
groupId := sm.currentReceiver
547-
if checkParam(command.Params, 1) {
548-
groupId = command.Params[0]
548+
if strings.Index(groupId, GROUPSUFFIX) < 0 {
549+
sm.uiHandler.PrintText("not a group")
550+
return
549551
}
550552
wac := sm.getConnection()
551553
var err error
@@ -554,6 +556,99 @@ func (sm *SessionManager) execCommand(command Command) {
554556
sm.uiHandler.PrintText("left group " + groupId)
555557
}
556558
sm.uiHandler.PrintError(err)
559+
case "create":
560+
if !checkParam(command.Params, 1) {
561+
sm.printCommandUsage("create", "[user-id[] [user-id[] New Group Subject")
562+
sm.printCommandUsage("create", "New Group Subject")
563+
return
564+
}
565+
// first params are users if ending in CONTACTSUFFIX, rest is name
566+
users := []string{}
567+
idx := 0
568+
size := len(command.Params)
569+
for idx = 0; idx < size && strings.Index(command.Params[idx], CONTACTSUFFIX) > 0; idx++ {
570+
users = append(users, command.Params[idx])
571+
}
572+
name := ""
573+
if len(command.Params) > idx {
574+
name = strings.Join(command.Params[idx:], " ")
575+
}
576+
wac := sm.getConnection()
577+
var err error
578+
var groupId <-chan string
579+
groupId, err = wac.CreateGroup(name, users)
580+
if err == nil {
581+
sm.uiHandler.PrintText("creating new group " + name)
582+
resultInfo := <-groupId
583+
//{"status":200,"gid":"[email protected]","participants":[{"[email protected]":{"code":"200"}},{"[email protected]":{"code": "200"}}]}
584+
var result map[string]interface{}
585+
json.Unmarshal([]byte(resultInfo), &result)
586+
newChatId := result["gid"].(string)
587+
sm.uiHandler.PrintText("got new Id " + newChatId)
588+
newChat := Chat{}
589+
newChat.Id = newChatId
590+
newChat.Name = name
591+
newChat.IsGroup = true
592+
sm.db.chats[newChatId] = newChat
593+
sm.uiHandler.SetChats(sm.db.GetChatIds())
594+
}
595+
sm.uiHandler.PrintError(err)
596+
case "add":
597+
groupId := sm.currentReceiver
598+
if strings.Index(groupId, GROUPSUFFIX) < 0 {
599+
sm.uiHandler.PrintText("not a group")
600+
return
601+
}
602+
if !checkParam(command.Params, 1) {
603+
sm.printCommandUsage("add", "[user-id[]")
604+
return
605+
}
606+
wac := sm.getConnection()
607+
var err error
608+
_, err = wac.AddMember(groupId, command.Params)
609+
if err == nil {
610+
sm.uiHandler.PrintText("added new members for " + groupId)
611+
}
612+
sm.uiHandler.PrintError(err)
613+
case "admin":
614+
groupId := sm.currentReceiver
615+
if strings.Index(groupId, GROUPSUFFIX) < 0 {
616+
sm.uiHandler.PrintText("not a group")
617+
return
618+
}
619+
if !checkParam(command.Params, 1) {
620+
sm.printCommandUsage("admin", "[user-id[]")
621+
return
622+
}
623+
wac := sm.getConnection()
624+
var err error
625+
_, err = wac.SetAdmin(groupId, command.Params)
626+
if err == nil {
627+
sm.uiHandler.PrintText("added admin for " + groupId)
628+
}
629+
sm.uiHandler.PrintError(err)
630+
case "subject":
631+
groupId := sm.currentReceiver
632+
if strings.Index(groupId, GROUPSUFFIX) < 0 {
633+
sm.uiHandler.PrintText("not a group")
634+
return
635+
}
636+
if !checkParam(command.Params, 1) || groupId == "" {
637+
sm.printCommandUsage("subject", "new-subject -> in group chat")
638+
return
639+
}
640+
name := strings.Join(command.Params, " ")
641+
wac := sm.getConnection()
642+
var err error
643+
_, err = wac.UpdateGroupSubject(name, groupId)
644+
if err == nil {
645+
sm.uiHandler.PrintText("updated subject for " + groupId)
646+
}
647+
newChat := sm.db.chats[groupId]
648+
newChat.Name = name
649+
sm.db.chats[groupId] = newChat
650+
sm.uiHandler.SetChats(sm.db.GetChatIds())
651+
sm.uiHandler.PrintError(err)
557652
case "colorlist":
558653
out := ""
559654
for idx, _ := range tcell.ColorNames {

0 commit comments

Comments
 (0)