2018-03-13 20:58:43 +01:00
|
|
|
// gomuks - A terminal Matrix client written in Go.
|
|
|
|
// Copyright (C) 2018 Tulir Asokan
|
|
|
|
//
|
|
|
|
// This program is free software: you can redistribute it and/or modify
|
|
|
|
// it under the terms of the GNU General Public License as published by
|
|
|
|
// the Free Software Foundation, either version 3 of the License, or
|
|
|
|
// (at your option) any later version.
|
|
|
|
//
|
|
|
|
// This program is distributed in the hope that it will be useful,
|
|
|
|
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
// GNU General Public License for more details.
|
|
|
|
//
|
|
|
|
// You should have received a copy of the GNU General Public License
|
|
|
|
// along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
|
2018-03-18 20:24:03 +01:00
|
|
|
package ui
|
2018-03-13 20:58:43 +01:00
|
|
|
|
|
|
|
import (
|
2018-03-17 13:32:01 +01:00
|
|
|
"fmt"
|
2018-03-17 15:09:53 +01:00
|
|
|
"sort"
|
2018-03-13 20:58:43 +01:00
|
|
|
"strings"
|
2018-03-15 20:38:43 +01:00
|
|
|
"time"
|
2018-03-17 13:32:01 +01:00
|
|
|
"unicode"
|
2018-03-13 20:58:43 +01:00
|
|
|
|
|
|
|
"github.com/gdamore/tcell"
|
2018-03-17 13:32:01 +01:00
|
|
|
"github.com/mattn/go-runewidth"
|
2018-03-15 17:25:16 +01:00
|
|
|
"maunium.net/go/gomatrix"
|
2018-03-18 20:24:03 +01:00
|
|
|
"maunium.net/go/gomuks/config"
|
|
|
|
"maunium.net/go/gomuks/interface"
|
2018-03-26 16:22:47 +02:00
|
|
|
"maunium.net/go/gomuks/matrix/pushrules"
|
|
|
|
"maunium.net/go/gomuks/matrix/rooms"
|
|
|
|
"maunium.net/go/gomuks/notification"
|
2018-03-18 20:24:03 +01:00
|
|
|
"maunium.net/go/gomuks/ui/debug"
|
|
|
|
"maunium.net/go/gomuks/ui/types"
|
|
|
|
"maunium.net/go/gomuks/ui/widget"
|
2018-03-14 21:19:26 +01:00
|
|
|
"maunium.net/go/tview"
|
2018-03-13 20:58:43 +01:00
|
|
|
)
|
|
|
|
|
2018-03-15 19:53:04 +01:00
|
|
|
type MainView struct {
|
2018-03-22 16:00:56 +01:00
|
|
|
*tview.Flex
|
2018-03-15 19:53:04 +01:00
|
|
|
|
2018-03-25 13:21:59 +02:00
|
|
|
roomList *widget.RoomList
|
2018-03-15 19:53:04 +01:00
|
|
|
roomView *tview.Pages
|
2018-03-18 20:24:03 +01:00
|
|
|
rooms map[string]*widget.RoomView
|
2018-03-15 19:53:04 +01:00
|
|
|
currentRoomIndex int
|
|
|
|
roomIDs []string
|
|
|
|
|
2018-03-24 21:14:17 +01:00
|
|
|
lastFocusTime time.Time
|
|
|
|
|
2018-03-18 20:24:03 +01:00
|
|
|
matrix ifc.MatrixContainer
|
|
|
|
gmx ifc.Gomuks
|
|
|
|
config *config.Config
|
2018-03-15 19:53:04 +01:00
|
|
|
parent *GomuksUI
|
|
|
|
}
|
|
|
|
|
2018-03-15 20:28:21 +01:00
|
|
|
func (ui *GomuksUI) NewMainView() tview.Primitive {
|
|
|
|
mainView := &MainView{
|
2018-03-22 16:00:56 +01:00
|
|
|
Flex: tview.NewFlex(),
|
2018-03-25 13:21:59 +02:00
|
|
|
roomList: widget.NewRoomList(),
|
2018-03-15 19:53:04 +01:00
|
|
|
roomView: tview.NewPages(),
|
2018-03-18 20:24:03 +01:00
|
|
|
rooms: make(map[string]*widget.RoomView),
|
2018-03-15 19:53:04 +01:00
|
|
|
|
2018-03-23 13:44:36 +01:00
|
|
|
matrix: ui.gmx.Matrix(),
|
2018-03-15 19:53:04 +01:00
|
|
|
gmx: ui.gmx,
|
2018-03-18 20:24:03 +01:00
|
|
|
config: ui.gmx.Config(),
|
2018-03-15 19:53:04 +01:00
|
|
|
parent: ui,
|
|
|
|
}
|
|
|
|
|
2018-03-22 16:00:56 +01:00
|
|
|
mainView.SetDirection(tview.FlexColumn)
|
2018-03-22 17:14:08 +01:00
|
|
|
mainView.AddItem(mainView.roomList, 25, 0, false)
|
2018-03-22 16:00:56 +01:00
|
|
|
mainView.AddItem(widget.NewBorder(), 1, 0, false)
|
|
|
|
mainView.AddItem(mainView.roomView, 0, 1, true)
|
2018-03-13 20:58:43 +01:00
|
|
|
|
2018-03-15 20:28:21 +01:00
|
|
|
ui.mainView = mainView
|
|
|
|
|
|
|
|
return mainView
|
2018-03-13 20:58:43 +01:00
|
|
|
}
|
|
|
|
|
2018-03-24 21:14:17 +01:00
|
|
|
func (view *MainView) BumpFocus() {
|
|
|
|
view.lastFocusTime = time.Now()
|
|
|
|
}
|
|
|
|
|
2018-03-22 15:44:24 +01:00
|
|
|
func (view *MainView) InputChanged(roomView *widget.RoomView, text string) {
|
2018-03-15 20:28:21 +01:00
|
|
|
if len(text) == 0 {
|
2018-03-22 15:44:24 +01:00
|
|
|
go view.matrix.SendTyping(roomView.Room.ID, false)
|
2018-03-15 20:28:21 +01:00
|
|
|
} else if text[0] != '/' {
|
2018-03-22 15:44:24 +01:00
|
|
|
go view.matrix.SendTyping(roomView.Room.ID, true)
|
2018-03-15 20:28:21 +01:00
|
|
|
}
|
2018-03-15 19:53:04 +01:00
|
|
|
}
|
|
|
|
|
2018-03-17 13:32:01 +01:00
|
|
|
func findWordToTabComplete(text string) string {
|
|
|
|
output := ""
|
|
|
|
runes := []rune(text)
|
|
|
|
for i := len(runes) - 1; i >= 0; i-- {
|
|
|
|
if unicode.IsSpace(runes[i]) {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
output = string(runes[i]) + output
|
|
|
|
}
|
|
|
|
return output
|
|
|
|
}
|
|
|
|
|
2018-03-22 15:44:24 +01:00
|
|
|
func (view *MainView) InputTabComplete(roomView *widget.RoomView, text string, cursorOffset int) string {
|
|
|
|
str := runewidth.Truncate(text, cursorOffset, "")
|
|
|
|
word := findWordToTabComplete(str)
|
|
|
|
userCompletions := roomView.AutocompleteUser(word)
|
|
|
|
if len(userCompletions) == 1 {
|
|
|
|
startIndex := len(str) - len(word)
|
|
|
|
completion := userCompletions[0]
|
|
|
|
if startIndex == 0 {
|
|
|
|
completion = completion + ": "
|
2018-03-17 13:32:01 +01:00
|
|
|
}
|
2018-03-22 15:44:24 +01:00
|
|
|
text = str[0:startIndex] + completion + text[len(str):]
|
|
|
|
} else if len(userCompletions) > 1 && len(userCompletions) < 6 {
|
|
|
|
roomView.SetStatus(fmt.Sprintf("Completions: %s", strings.Join(userCompletions, ", ")))
|
2018-03-17 13:00:02 +01:00
|
|
|
}
|
2018-03-17 13:32:01 +01:00
|
|
|
return text
|
2018-03-17 13:00:02 +01:00
|
|
|
}
|
|
|
|
|
2018-03-22 15:44:24 +01:00
|
|
|
func (view *MainView) InputSubmit(roomView *widget.RoomView, text string) {
|
2018-03-20 18:14:39 +01:00
|
|
|
if len(text) == 0 {
|
|
|
|
return
|
|
|
|
} else if text[0] == '/' {
|
|
|
|
args := strings.SplitN(text, " ", 2)
|
|
|
|
command := strings.ToLower(args[0])
|
|
|
|
args = args[1:]
|
2018-03-22 20:44:46 +01:00
|
|
|
go view.HandleCommand(roomView, command, args)
|
2018-03-20 18:14:39 +01:00
|
|
|
} else {
|
2018-03-22 20:44:46 +01:00
|
|
|
view.SendMessage(roomView, text)
|
2018-03-20 18:14:39 +01:00
|
|
|
}
|
2018-03-22 15:44:24 +01:00
|
|
|
roomView.SetInputText("")
|
2018-03-20 18:14:39 +01:00
|
|
|
}
|
|
|
|
|
2018-03-22 20:44:46 +01:00
|
|
|
func (view *MainView) SendMessage(roomView *widget.RoomView, text string) {
|
|
|
|
tempMessage := roomView.NewTempMessage("m.text", text)
|
|
|
|
go view.sendTempMessage(roomView, tempMessage)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (view *MainView) sendTempMessage(roomView *widget.RoomView, tempMessage *types.Message) {
|
|
|
|
defer view.gmx.Recover()
|
|
|
|
eventID, err := view.matrix.SendMessage(roomView.Room.ID, tempMessage.Type, tempMessage.Text)
|
|
|
|
if err != nil {
|
2018-03-22 22:47:59 +01:00
|
|
|
tempMessage.State = types.MessageStateFailed
|
2018-03-22 20:44:46 +01:00
|
|
|
roomView.SetStatus(fmt.Sprintf("Failed to send message: %s", err))
|
|
|
|
} else {
|
|
|
|
roomView.MessageView().UpdateMessageID(tempMessage, eventID)
|
|
|
|
}
|
2018-03-15 19:53:04 +01:00
|
|
|
}
|
|
|
|
|
2018-03-22 20:44:46 +01:00
|
|
|
func (view *MainView) HandleCommand(roomView *widget.RoomView, command string, args []string) {
|
2018-03-19 00:18:36 +01:00
|
|
|
defer view.gmx.Recover()
|
2018-03-18 20:24:03 +01:00
|
|
|
debug.Print("Handling command", command, args)
|
2018-03-13 20:58:43 +01:00
|
|
|
switch command {
|
2018-03-22 20:44:46 +01:00
|
|
|
case "/me":
|
|
|
|
tempMessage := roomView.NewTempMessage("m.emote", strings.Join(args, " "))
|
|
|
|
go view.sendTempMessage(roomView, tempMessage)
|
|
|
|
view.parent.Render()
|
2018-03-14 23:14:39 +01:00
|
|
|
case "/quit":
|
2018-03-15 19:53:04 +01:00
|
|
|
view.gmx.Stop()
|
2018-03-14 23:14:39 +01:00
|
|
|
case "/clearcache":
|
2018-03-22 18:51:20 +01:00
|
|
|
view.config.Clear()
|
2018-03-15 19:53:04 +01:00
|
|
|
view.gmx.Stop()
|
2018-03-19 00:18:36 +01:00
|
|
|
case "/panic":
|
|
|
|
panic("This is a test panic.")
|
2018-03-14 23:14:39 +01:00
|
|
|
case "/part":
|
2018-03-16 15:24:11 +01:00
|
|
|
fallthrough
|
2018-03-14 23:14:39 +01:00
|
|
|
case "/leave":
|
2018-03-22 20:44:46 +01:00
|
|
|
debug.Print("Leave room result:", view.matrix.LeaveRoom(roomView.Room.ID))
|
2018-03-14 23:14:39 +01:00
|
|
|
case "/join":
|
2018-03-13 20:58:43 +01:00
|
|
|
if len(args) == 0 {
|
2018-03-22 20:44:46 +01:00
|
|
|
view.AddServiceMessage(roomView, "Usage: /join <room>")
|
2018-03-15 19:53:04 +01:00
|
|
|
break
|
2018-03-13 20:58:43 +01:00
|
|
|
}
|
2018-03-22 20:44:46 +01:00
|
|
|
debug.Print("Join room result:", view.matrix.JoinRoom(args[0]))
|
2018-03-16 15:24:11 +01:00
|
|
|
default:
|
2018-03-22 20:44:46 +01:00
|
|
|
view.AddServiceMessage(roomView, "Unknown command.")
|
2018-03-13 20:58:43 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-03-24 21:14:17 +01:00
|
|
|
func (view *MainView) KeyEventHandler(roomView *widget.RoomView, key *tcell.EventKey) *tcell.EventKey {
|
|
|
|
view.BumpFocus()
|
|
|
|
|
2018-03-17 00:27:30 +01:00
|
|
|
k := key.Key()
|
2018-03-20 11:16:32 +01:00
|
|
|
if key.Modifiers() == tcell.ModCtrl || key.Modifiers() == tcell.ModAlt {
|
2018-03-25 13:21:59 +02:00
|
|
|
switch k {
|
|
|
|
case tcell.KeyDown:
|
2018-03-15 19:53:04 +01:00
|
|
|
view.SwitchRoom(view.currentRoomIndex + 1)
|
2018-03-25 13:21:59 +02:00
|
|
|
case tcell.KeyUp:
|
2018-03-15 19:53:04 +01:00
|
|
|
view.SwitchRoom(view.currentRoomIndex - 1)
|
2018-03-25 13:21:59 +02:00
|
|
|
default:
|
2018-03-14 21:19:26 +01:00
|
|
|
return key
|
2018-03-13 20:58:43 +01:00
|
|
|
}
|
2018-03-25 11:35:50 +02:00
|
|
|
} else if k == tcell.KeyPgUp || k == tcell.KeyPgDn || k == tcell.KeyUp || k == tcell.KeyDown || k == tcell.KeyEnd || k == tcell.KeyHome {
|
2018-03-22 15:44:24 +01:00
|
|
|
msgView := roomView.MessageView()
|
2018-03-25 11:35:50 +02:00
|
|
|
|
|
|
|
if msgView.IsAtTop() && (k == tcell.KeyPgUp || k == tcell.KeyUp) {
|
|
|
|
go view.LoadHistory(roomView.Room.ID, false)
|
|
|
|
}
|
|
|
|
|
|
|
|
switch k {
|
|
|
|
case tcell.KeyPgUp:
|
|
|
|
msgView.AddScrollOffset(msgView.Height() / 2)
|
|
|
|
case tcell.KeyPgDn:
|
|
|
|
msgView.AddScrollOffset(-msgView.Height() / 2)
|
|
|
|
case tcell.KeyUp:
|
|
|
|
msgView.AddScrollOffset(1)
|
|
|
|
case tcell.KeyDown:
|
|
|
|
msgView.AddScrollOffset(-1)
|
|
|
|
case tcell.KeyHome:
|
|
|
|
msgView.AddScrollOffset(msgView.TotalHeight())
|
|
|
|
case tcell.KeyEnd:
|
|
|
|
msgView.AddScrollOffset(-msgView.TotalHeight())
|
2018-03-17 00:27:30 +01:00
|
|
|
}
|
2018-03-13 20:58:43 +01:00
|
|
|
} else {
|
|
|
|
return key
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2018-03-25 11:35:50 +02:00
|
|
|
const WheelScrollOffsetDiff = 3
|
|
|
|
|
2018-03-24 21:14:17 +01:00
|
|
|
func (view *MainView) MouseEventHandler(roomView *widget.RoomView, event *tcell.EventMouse) *tcell.EventMouse {
|
2018-03-25 11:35:50 +02:00
|
|
|
if event.Buttons() == tcell.ButtonNone {
|
|
|
|
return event
|
|
|
|
}
|
|
|
|
view.BumpFocus()
|
|
|
|
|
|
|
|
msgView := roomView.MessageView()
|
|
|
|
x, y := event.Position()
|
|
|
|
|
|
|
|
switch event.Buttons() {
|
|
|
|
case tcell.WheelUp:
|
|
|
|
if msgView.IsAtTop() {
|
|
|
|
go view.LoadHistory(roomView.Room.ID, false)
|
|
|
|
} else {
|
|
|
|
msgView.AddScrollOffset(WheelScrollOffsetDiff)
|
2018-03-26 13:31:44 +02:00
|
|
|
|
|
|
|
view.parent.Render()
|
2018-03-25 11:35:50 +02:00
|
|
|
}
|
|
|
|
case tcell.WheelDown:
|
|
|
|
msgView.AddScrollOffset(-WheelScrollOffsetDiff)
|
2018-03-26 13:31:44 +02:00
|
|
|
|
|
|
|
view.parent.Render()
|
2018-03-26 16:22:47 +02:00
|
|
|
|
|
|
|
if msgView.ScrollOffset == 0 {
|
|
|
|
roomView.Room.MarkRead()
|
|
|
|
}
|
2018-03-25 11:35:50 +02:00
|
|
|
default:
|
|
|
|
debug.Print("Mouse event received:", event.Buttons(), event.Modifiers(), x, y)
|
2018-03-26 13:31:44 +02:00
|
|
|
return event
|
2018-03-24 21:14:17 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
return event
|
|
|
|
}
|
|
|
|
|
2018-03-15 19:53:04 +01:00
|
|
|
func (view *MainView) CurrentRoomID() string {
|
|
|
|
if len(view.roomIDs) == 0 {
|
|
|
|
return ""
|
|
|
|
}
|
|
|
|
return view.roomIDs[view.currentRoomIndex]
|
|
|
|
}
|
|
|
|
|
|
|
|
func (view *MainView) SwitchRoom(roomIndex int) {
|
|
|
|
if roomIndex < 0 {
|
|
|
|
roomIndex = len(view.roomIDs) - 1
|
|
|
|
}
|
2018-03-19 14:19:44 +01:00
|
|
|
if len(view.roomIDs) == 0 {
|
|
|
|
return
|
|
|
|
}
|
2018-03-15 19:53:04 +01:00
|
|
|
view.currentRoomIndex = roomIndex % len(view.roomIDs)
|
2018-03-22 16:36:06 +01:00
|
|
|
view.roomView.SwitchToPage(view.CurrentRoomID())
|
2018-03-26 16:22:47 +02:00
|
|
|
roomView := view.rooms[view.CurrentRoomID()]
|
|
|
|
if roomView.MessageView().ScrollOffset == 0 {
|
|
|
|
roomView.Room.MarkRead()
|
|
|
|
}
|
|
|
|
view.roomList.SetSelected(roomView.Room)
|
2018-03-22 15:44:24 +01:00
|
|
|
view.gmx.App().SetFocus(view)
|
2018-03-15 19:53:04 +01:00
|
|
|
view.parent.Render()
|
|
|
|
}
|
|
|
|
|
2018-03-22 15:44:24 +01:00
|
|
|
func (view *MainView) Focus(delegate func(p tview.Primitive)) {
|
|
|
|
roomView, ok := view.rooms[view.CurrentRoomID()]
|
|
|
|
if ok {
|
|
|
|
delegate(roomView)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-03-22 18:51:20 +01:00
|
|
|
func (view *MainView) SaveAllHistory() {
|
|
|
|
for _, room := range view.rooms {
|
|
|
|
err := room.SaveHistory(view.config.HistoryDir)
|
|
|
|
if err != nil {
|
|
|
|
debug.Printf("Failed to save history of %s: %v", room.Room.GetTitle(), err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-03-16 15:24:11 +01:00
|
|
|
func (view *MainView) addRoom(index int, room string) {
|
|
|
|
roomStore := view.matrix.GetRoom(room)
|
|
|
|
|
2018-03-25 13:21:59 +02:00
|
|
|
view.roomList.Add(roomStore)
|
2018-03-16 15:24:11 +01:00
|
|
|
if !view.roomView.HasPage(room) {
|
2018-03-22 15:44:24 +01:00
|
|
|
roomView := widget.NewRoomView(roomStore).
|
|
|
|
SetInputSubmitFunc(view.InputSubmit).
|
|
|
|
SetInputChangedFunc(view.InputChanged).
|
|
|
|
SetTabCompleteFunc(view.InputTabComplete).
|
2018-03-24 21:14:17 +01:00
|
|
|
SetInputCapture(view.KeyEventHandler).
|
|
|
|
SetMouseCapture(view.MouseEventHandler)
|
2018-03-16 15:24:11 +01:00
|
|
|
view.rooms[room] = roomView
|
|
|
|
view.roomView.AddPage(room, roomView, true, false)
|
|
|
|
roomView.UpdateUserList()
|
2018-03-22 18:51:20 +01:00
|
|
|
|
|
|
|
count, err := roomView.LoadHistory(view.config.HistoryDir)
|
|
|
|
if err != nil {
|
|
|
|
debug.Printf("Failed to load history of %s: %v", roomView.Room.GetTitle(), err)
|
|
|
|
} else if count <= 0 {
|
2018-03-24 12:27:13 +01:00
|
|
|
go view.LoadHistory(room, true)
|
2018-03-22 18:51:20 +01:00
|
|
|
}
|
2018-03-16 15:24:11 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-03-18 20:24:03 +01:00
|
|
|
func (view *MainView) GetRoom(id string) *widget.RoomView {
|
2018-03-18 16:34:42 +01:00
|
|
|
return view.rooms[id]
|
|
|
|
}
|
|
|
|
|
2018-03-16 15:24:11 +01:00
|
|
|
func (view *MainView) HasRoom(room string) bool {
|
|
|
|
for _, existingRoom := range view.roomIDs {
|
|
|
|
if existingRoom == room {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
func (view *MainView) AddRoom(room string) {
|
|
|
|
if view.HasRoom(room) {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
view.roomIDs = append(view.roomIDs, room)
|
2018-03-17 13:00:02 +01:00
|
|
|
view.addRoom(len(view.roomIDs)-1, room)
|
2018-03-16 15:24:11 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
func (view *MainView) RemoveRoom(room string) {
|
2018-03-25 13:21:59 +02:00
|
|
|
roomView := view.GetRoom(room)
|
|
|
|
if roomView == nil {
|
2018-03-16 15:24:11 +01:00
|
|
|
return
|
|
|
|
}
|
2018-03-17 15:09:53 +01:00
|
|
|
removeIndex := 0
|
2018-03-16 15:24:11 +01:00
|
|
|
if view.CurrentRoomID() == room {
|
2018-03-17 15:09:53 +01:00
|
|
|
removeIndex = view.currentRoomIndex
|
2018-03-16 15:24:11 +01:00
|
|
|
view.SwitchRoom(view.currentRoomIndex - 1)
|
2018-03-17 15:09:53 +01:00
|
|
|
} else {
|
|
|
|
removeIndex = sort.StringSlice(view.roomIDs).Search(room)
|
2018-03-16 15:24:11 +01:00
|
|
|
}
|
2018-03-25 13:21:59 +02:00
|
|
|
view.roomList.Remove(roomView.Room)
|
2018-03-17 15:09:53 +01:00
|
|
|
view.roomIDs = append(view.roomIDs[:removeIndex], view.roomIDs[removeIndex+1:]...)
|
2018-03-16 15:24:11 +01:00
|
|
|
view.roomView.RemovePage(room)
|
2018-03-18 16:34:42 +01:00
|
|
|
delete(view.rooms, room)
|
2018-03-20 11:16:32 +01:00
|
|
|
view.parent.Render()
|
2018-03-16 15:24:11 +01:00
|
|
|
}
|
|
|
|
|
2018-03-18 20:24:03 +01:00
|
|
|
func (view *MainView) SetRooms(rooms []string) {
|
2018-03-15 19:53:04 +01:00
|
|
|
view.roomIDs = rooms
|
|
|
|
view.roomList.Clear()
|
2018-03-16 15:24:11 +01:00
|
|
|
view.roomView.Clear()
|
2018-03-18 20:24:03 +01:00
|
|
|
view.rooms = make(map[string]*widget.RoomView)
|
2018-03-13 20:58:43 +01:00
|
|
|
for index, room := range rooms {
|
2018-03-16 15:24:11 +01:00
|
|
|
view.addRoom(index, room)
|
2018-03-13 20:58:43 +01:00
|
|
|
}
|
2018-03-15 19:53:04 +01:00
|
|
|
view.SwitchRoom(0)
|
2018-03-13 20:58:43 +01:00
|
|
|
}
|
|
|
|
|
2018-03-15 19:53:04 +01:00
|
|
|
func (view *MainView) SetTyping(room string, users []string) {
|
|
|
|
roomView, ok := view.rooms[room]
|
2018-03-14 23:14:39 +01:00
|
|
|
if ok {
|
2018-03-15 18:45:52 +01:00
|
|
|
roomView.SetTyping(users)
|
2018-03-15 19:53:04 +01:00
|
|
|
view.parent.Render()
|
2018-03-14 23:14:39 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-03-26 17:04:10 +02:00
|
|
|
func sendNotification(room *rooms.Room, sender, text string, critical, sound bool) {
|
2018-03-26 16:22:47 +02:00
|
|
|
if room.GetTitle() != sender {
|
|
|
|
sender = fmt.Sprintf("%s (%s)", sender, room.GetTitle())
|
|
|
|
}
|
2018-03-26 17:04:10 +02:00
|
|
|
notification.Send(sender, text, critical, sound)
|
2018-03-26 16:22:47 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func (view *MainView) NotifyMessage(room *rooms.Room, message *types.Message, should pushrules.PushActionArrayShould) {
|
2018-03-26 19:55:52 +02:00
|
|
|
// Whether or not the room where the message came is the currently shown room.
|
2018-03-26 16:22:47 +02:00
|
|
|
isCurrent := room.ID == view.CurrentRoomID()
|
2018-03-26 19:55:52 +02:00
|
|
|
// Whether or not the terminal window is focused.
|
|
|
|
isFocused := view.lastFocusTime.Add(30 * time.Second).Before(time.Now())
|
|
|
|
|
|
|
|
// Whether or not the push rules say this message should be notified about.
|
|
|
|
shouldNotify := (should.Notify || !should.NotifySpecified) && message.Sender != view.config.Session.UserID
|
|
|
|
|
2018-03-26 16:22:47 +02:00
|
|
|
if !isCurrent {
|
2018-03-26 19:55:52 +02:00
|
|
|
// The message is not in the current room, show new message status in room list.
|
2018-03-26 16:22:47 +02:00
|
|
|
room.HasNewMessages = true
|
2018-03-26 19:55:52 +02:00
|
|
|
room.Highlighted = should.Highlight || room.Highlighted
|
|
|
|
if shouldNotify {
|
|
|
|
room.UnreadMessages++
|
|
|
|
}
|
2018-03-26 16:22:47 +02:00
|
|
|
}
|
2018-03-26 19:55:52 +02:00
|
|
|
|
|
|
|
if shouldNotify && !isFocused {
|
|
|
|
// Push rules say notify and the terminal is not focused, send desktop notification.
|
2018-03-26 17:04:10 +02:00
|
|
|
shouldPlaySound := should.PlaySound && should.SoundName == "default"
|
|
|
|
sendNotification(room, message.Sender, message.Text, should.Highlight, shouldPlaySound)
|
2018-03-26 16:22:47 +02:00
|
|
|
}
|
2018-03-26 19:55:52 +02:00
|
|
|
|
2018-03-26 16:22:47 +02:00
|
|
|
if should.Highlight {
|
2018-03-26 19:55:52 +02:00
|
|
|
// Message is highlight, set color.
|
2018-03-26 16:22:47 +02:00
|
|
|
message.TextColor = tcell.ColorYellow
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-03-22 20:44:46 +01:00
|
|
|
func (view *MainView) AddServiceMessage(roomView *widget.RoomView, text string) {
|
|
|
|
message := roomView.NewMessage("", "*", "gomuks.service", text, time.Now())
|
|
|
|
message.TextColor = tcell.ColorGray
|
|
|
|
message.SenderColor = tcell.ColorGray
|
|
|
|
roomView.AddMessage(message, widget.AppendMessage)
|
|
|
|
view.parent.Render()
|
2018-03-13 20:58:43 +01:00
|
|
|
}
|
2018-03-17 14:48:31 +01:00
|
|
|
|
2018-03-20 22:31:04 +01:00
|
|
|
func (view *MainView) LoadHistory(room string, initial bool) {
|
2018-03-20 11:16:32 +01:00
|
|
|
defer view.gmx.Recover()
|
2018-03-18 16:34:42 +01:00
|
|
|
roomView := view.rooms[room]
|
2018-03-20 11:16:32 +01:00
|
|
|
|
|
|
|
batch := roomView.Room.PrevBatch
|
|
|
|
lockTime := time.Now().Unix() + 1
|
|
|
|
|
2018-03-22 16:36:06 +01:00
|
|
|
roomView.Room.LockHistory()
|
2018-03-20 11:16:32 +01:00
|
|
|
roomView.MessageView().LoadingMessages = true
|
|
|
|
defer func() {
|
2018-03-22 16:36:06 +01:00
|
|
|
roomView.Room.UnlockHistory()
|
2018-03-20 11:16:32 +01:00
|
|
|
roomView.MessageView().LoadingMessages = false
|
|
|
|
}()
|
|
|
|
|
|
|
|
// There's no clean way to try to lock a mutex, so we just check if we still
|
|
|
|
// want to continue after we get the lock. This function should always be ran
|
|
|
|
// in a goroutine, so the blocking doesn't matter.
|
|
|
|
if time.Now().Unix() >= lockTime || batch != roomView.Room.PrevBatch {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
if initial {
|
|
|
|
batch = view.config.Session.NextBatch
|
2018-03-24 12:27:13 +01:00
|
|
|
debug.Print("Loading initial history for", room)
|
|
|
|
} else {
|
|
|
|
debug.Print("Loading more history for", room, "starting from", batch)
|
2018-03-20 11:16:32 +01:00
|
|
|
}
|
|
|
|
history, prevBatch, err := view.matrix.GetHistory(roomView.Room.ID, batch, 50)
|
2018-03-18 16:34:42 +01:00
|
|
|
if err != nil {
|
2018-03-22 20:44:46 +01:00
|
|
|
view.AddServiceMessage(roomView, "Failed to fetch history")
|
2018-03-18 20:24:03 +01:00
|
|
|
debug.Print("Failed to fetch history for", roomView.Room.ID, err)
|
2018-03-18 16:34:42 +01:00
|
|
|
return
|
|
|
|
}
|
2018-03-20 11:16:32 +01:00
|
|
|
roomView.Room.PrevBatch = prevBatch
|
2018-03-18 16:34:42 +01:00
|
|
|
for _, evt := range history {
|
2018-03-18 20:24:03 +01:00
|
|
|
var message *types.Message
|
2018-03-18 16:34:42 +01:00
|
|
|
if evt.Type == "m.room.message" {
|
2018-03-23 00:00:13 +01:00
|
|
|
message = view.ProcessMessageEvent(roomView, &evt)
|
2018-03-18 16:34:42 +01:00
|
|
|
} else if evt.Type == "m.room.member" {
|
2018-03-23 00:00:13 +01:00
|
|
|
message = view.ProcessMembershipEvent(roomView, &evt)
|
2018-03-18 16:34:42 +01:00
|
|
|
}
|
2018-03-23 00:00:13 +01:00
|
|
|
if message != nil {
|
|
|
|
roomView.AddMessage(message, widget.PrependMessage)
|
2018-03-18 16:34:42 +01:00
|
|
|
}
|
|
|
|
}
|
2018-03-22 18:51:20 +01:00
|
|
|
err = roomView.SaveHistory(view.config.HistoryDir)
|
|
|
|
if err != nil {
|
2018-03-23 00:07:44 +01:00
|
|
|
debug.Printf("Failed to save history of %s: %v", roomView.Room.GetTitle(), err)
|
2018-03-22 18:51:20 +01:00
|
|
|
}
|
|
|
|
view.config.Session.Save()
|
2018-03-20 11:16:32 +01:00
|
|
|
view.parent.Render()
|
2018-03-18 16:34:42 +01:00
|
|
|
}
|
|
|
|
|
2018-03-23 00:00:13 +01:00
|
|
|
func (view *MainView) ProcessMessageEvent(room *widget.RoomView, evt *gomatrix.Event) (message *types.Message) {
|
|
|
|
text, _ := evt.Content["body"].(string)
|
|
|
|
msgtype, _ := evt.Content["msgtype"].(string)
|
|
|
|
return room.NewMessage(evt.ID, evt.Sender, msgtype, text, unixToTime(evt.Timestamp))
|
2018-03-18 16:34:42 +01:00
|
|
|
}
|
|
|
|
|
2018-03-23 00:00:13 +01:00
|
|
|
func (view *MainView) getMembershipEventContent(evt *gomatrix.Event) (sender, text string) {
|
2018-03-18 16:34:42 +01:00
|
|
|
membership, _ := evt.Content["membership"].(string)
|
2018-03-23 00:00:13 +01:00
|
|
|
displayname, _ := evt.Content["displayname"].(string)
|
|
|
|
if len(displayname) == 0 {
|
|
|
|
displayname = *evt.StateKey
|
2018-03-18 16:34:42 +01:00
|
|
|
}
|
2018-03-23 13:20:46 +01:00
|
|
|
prevMembership := "leave"
|
|
|
|
prevDisplayname := ""
|
|
|
|
if evt.Unsigned.PrevContent != nil {
|
|
|
|
prevMembership, _ = evt.Unsigned.PrevContent["membership"].(string)
|
|
|
|
prevDisplayname, _ = evt.Unsigned.PrevContent["displayname"].(string)
|
|
|
|
}
|
2018-03-23 00:00:13 +01:00
|
|
|
|
2018-03-23 13:20:46 +01:00
|
|
|
if membership != prevMembership {
|
|
|
|
switch membership {
|
|
|
|
case "invite":
|
|
|
|
sender = "---"
|
|
|
|
text = fmt.Sprintf("%s invited %s.", evt.Sender, displayname)
|
|
|
|
case "join":
|
|
|
|
sender = "-->"
|
|
|
|
text = fmt.Sprintf("%s joined the room.", displayname)
|
|
|
|
case "leave":
|
|
|
|
sender = "<--"
|
|
|
|
if evt.Sender != *evt.StateKey {
|
|
|
|
reason, _ := evt.Content["reason"].(string)
|
|
|
|
text = fmt.Sprintf("%s kicked %s: %s", evt.Sender, displayname, reason)
|
|
|
|
} else {
|
|
|
|
text = fmt.Sprintf("%s left the room.", displayname)
|
|
|
|
}
|
2018-03-23 00:00:13 +01:00
|
|
|
}
|
2018-03-23 13:20:46 +01:00
|
|
|
} else if displayname != prevDisplayname {
|
|
|
|
sender = "---"
|
|
|
|
text = fmt.Sprintf("%s changed their display name to %s.", prevDisplayname, displayname)
|
2018-03-18 16:34:42 +01:00
|
|
|
}
|
2018-03-23 00:00:13 +01:00
|
|
|
return
|
2018-03-18 16:34:42 +01:00
|
|
|
}
|
|
|
|
|
2018-03-23 00:00:13 +01:00
|
|
|
func (view *MainView) ProcessMembershipEvent(room *widget.RoomView, evt *gomatrix.Event) (message *types.Message) {
|
|
|
|
sender, text := view.getMembershipEventContent(evt)
|
|
|
|
if len(text) == 0 {
|
|
|
|
return
|
2018-03-18 16:34:42 +01:00
|
|
|
}
|
2018-03-23 00:00:13 +01:00
|
|
|
message = room.NewMessage(evt.ID, sender, "m.room.member", text, unixToTime(evt.Timestamp))
|
|
|
|
message.TextColor = tcell.ColorGreen
|
2018-03-18 16:34:42 +01:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
func unixToTime(unix int64) time.Time {
|
|
|
|
timestamp := time.Now()
|
|
|
|
if unix != 0 {
|
|
|
|
timestamp = time.Unix(unix/1000, unix%1000*1000)
|
|
|
|
}
|
|
|
|
return timestamp
|
|
|
|
}
|