| 1 |
// gomuks - A terminal Matrix client written in Go. |
| 2 |
// Copyright (C) 2020 Tulir Asokan |
| 3 |
// |
| 4 |
// This program is free software: you can redistribute it and/or modify |
| 5 |
// it under the terms of the GNU Affero General Public License as published by |
| 6 |
// the Free Software Foundation, either version 3 of the License, or |
| 7 |
// (at your option) any later version. |
| 8 |
// |
| 9 |
// This program is distributed in the hope that it will be useful, |
| 10 |
// but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 |
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 12 |
// GNU Affero General Public License for more details. |
| 13 |
// |
| 14 |
// You should have received a copy of the GNU Affero General Public License |
| 15 |
// along with this program. If not, see <https://www.gnu.org/licenses/>. |
| 16 |
|
| 17 |
package ui |
| 18 |
|
| 19 |
import ( |
| 20 |
"fmt" |
| 21 |
"sort" |
| 22 |
"strings" |
| 23 |
"time" |
| 24 |
"unicode" |
| 25 |
|
| 26 |
"github.com/kyokomi/emoji/v2" |
| 27 |
"github.com/mattn/go-runewidth" |
| 28 |
"github.com/zyedidia/clipboard" |
| 29 |
|
| 30 |
"go.mau.fi/mauview" |
| •31 |
"github.com/gdamore/tcell/v2" |
| 32 |
|
| 33 |
"maunium.net/go/mautrix" |
| 34 |
"maunium.net/go/mautrix/crypto/attachment" |
| 35 |
"maunium.net/go/mautrix/event" |
| 36 |
"maunium.net/go/mautrix/format" |
| 37 |
"maunium.net/go/mautrix/id" |
| 38 |
"maunium.net/go/mautrix/util/variationselector" |
| 39 |
|
| 40 |
"maunium.net/go/gomuks/config" |
| 41 |
"maunium.net/go/gomuks/debug" |
| 42 |
ifc "maunium.net/go/gomuks/interface" |
| 43 |
"maunium.net/go/gomuks/lib/open" |
| 44 |
"maunium.net/go/gomuks/lib/util" |
| 45 |
"maunium.net/go/gomuks/matrix/muksevt" |
| 46 |
"maunium.net/go/gomuks/matrix/rooms" |
| 47 |
"maunium.net/go/gomuks/ui/messages" |
| 48 |
"maunium.net/go/gomuks/ui/widget" |
| 49 |
) |
| 50 |
|
| 51 |
type RoomView struct { |
| 52 |
topic *mauview.TextView |
| 53 |
content *MessageView |
| 54 |
status *mauview.TextField |
| 55 |
userList *MemberList |
| 56 |
ulBorder *widget.Border |
| 57 |
input *mauview.InputArea |
| 58 |
Room *rooms.Room |
| 59 |
|
| 60 |
topicScreen *mauview.ProxyScreen |
| 61 |
contentScreen *mauview.ProxyScreen |
| 62 |
statusScreen *mauview.ProxyScreen |
| 63 |
inputScreen *mauview.ProxyScreen |
| 64 |
ulBorderScreen *mauview.ProxyScreen |
| 65 |
ulScreen *mauview.ProxyScreen |
| 66 |
|
| 67 |
userListLoaded bool |
| 68 |
|
| 69 |
prevScreen mauview.Screen |
| 70 |
|
| 71 |
parent *MainView |
| 72 |
config *config.Config |
| 73 |
|
| 74 |
typing []string |
| 75 |
|
| 76 |
selecting bool |
| 77 |
selectReason SelectReason |
| 78 |
selectContent string |
| 79 |
|
| 80 |
replying *muksevt.Event |
| 81 |
|
| 82 |
editing *muksevt.Event |
| 83 |
editMoveText string |
| 84 |
|
| 85 |
completions struct { |
| 86 |
list []string |
| 87 |
textCache string |
| 88 |
time time.Time |
| 89 |
} |
| 90 |
} |
| 91 |
|
| 92 |
func NewRoomView(parent *MainView, room *rooms.Room) *RoomView { |
| 93 |
view := &RoomView{ |
| 94 |
topic: mauview.NewTextView(), |
| 95 |
status: mauview.NewTextField(), |
| 96 |
userList: NewMemberList(), |
| 97 |
ulBorder: widget.NewBorder(), |
| 98 |
input: mauview.NewInputArea(), |
| 99 |
Room: room, |
| 100 |
|
| 101 |
topicScreen: &mauview.ProxyScreen{OffsetX: 0, OffsetY: 0, Height: TopicBarHeight}, |
| 102 |
contentScreen: &mauview.ProxyScreen{OffsetX: 0, OffsetY: StatusBarHeight}, |
| 103 |
statusScreen: &mauview.ProxyScreen{OffsetX: 0, Height: StatusBarHeight}, |
| 104 |
inputScreen: &mauview.ProxyScreen{OffsetX: 0}, |
| 105 |
ulBorderScreen: &mauview.ProxyScreen{OffsetY: StatusBarHeight, Width: UserListBorderWidth}, |
| 106 |
ulScreen: &mauview.ProxyScreen{OffsetY: StatusBarHeight, Width: UserListWidth}, |
| 107 |
|
| 108 |
parent: parent, |
| 109 |
config: parent.config, |
| 110 |
} |
| 111 |
view.content = NewMessageView(view) |
| 112 |
view.Room.SetPreUnload(func() bool { |
| 113 |
if view.parent.currentRoom == view { |
| 114 |
return false |
| 115 |
} |
| 116 |
view.content.Unload() |
| 117 |
return true |
| 118 |
}) |
| 119 |
view.Room.SetPostLoad(view.loadTyping) |
| 120 |
|
| 121 |
view.input. |
| 122 |
SetTextColor(tcell.ColorDefault). |
| 123 |
SetBackgroundColor(tcell.ColorDefault). |
| 124 |
SetPlaceholder("Send a message..."). |
| 125 |
SetPlaceholderTextColor(tcell.ColorGray). |
| 126 |
SetTabCompleteFunc(view.InputTabComplete). |
| 127 |
SetPressKeyUpAtStartFunc(view.EditPrevious). |
| 128 |
SetPressKeyDownAtEndFunc(view.EditNext) |
| 129 |
|
| 130 |
if room.Encrypted { |
| 131 |
view.input.SetPlaceholder("Send an encrypted message...") |
| 132 |
} |
| 133 |
|
| 134 |
view.topic. |
| 135 |
SetTextColor(tcell.ColorWhite). |
| 136 |
SetBackgroundColor(tcell.ColorDarkGreen) |
| 137 |
|
| 138 |
view.status.SetBackgroundColor(tcell.ColorDimGray) |
| 139 |
|
| 140 |
return view |
| 141 |
} |
| 142 |
|
| 143 |
func (view *RoomView) SetInputChangedFunc(fn func(room *RoomView, text string)) *RoomView { |
| 144 |
view.input.SetChangedFunc(func(text string) { |
| 145 |
fn(view, text) |
| 146 |
}) |
| 147 |
return view |
| 148 |
} |
| 149 |
|
| 150 |
func (view *RoomView) SetInputText(newText string) *RoomView { |
| 151 |
view.input.SetTextAndMoveCursor(newText) |
| 152 |
return view |
| 153 |
} |
| 154 |
|
| 155 |
func (view *RoomView) GetInputText() string { |
| 156 |
return view.input.GetText() |
| 157 |
} |
| 158 |
|
| 159 |
func (view *RoomView) Focus() { |
| 160 |
view.input.Focus() |
| 161 |
} |
| 162 |
|
| 163 |
func (view *RoomView) Blur() { |
| 164 |
view.StopSelecting() |
| 165 |
view.input.Blur() |
| 166 |
} |
| 167 |
|
| 168 |
func (view *RoomView) StartSelecting(reason SelectReason, content string) { |
| 169 |
view.selecting = true |
| 170 |
view.selectReason = reason |
| 171 |
view.selectContent = content |
| 172 |
msgView := view.MessageView() |
| 173 |
if msgView.selected != nil { |
| 174 |
view.OnSelect(msgView.selected) |
| 175 |
} else { |
| 176 |
view.input.Blur() |
| 177 |
view.SelectPrevious() |
| 178 |
} |
| 179 |
} |
| 180 |
|
| 181 |
func (view *RoomView) StopSelecting() { |
| 182 |
view.selecting = false |
| 183 |
view.selectContent = "" |
| 184 |
view.MessageView().SetSelected(nil) |
| 185 |
} |
| 186 |
|
| 187 |
func (view *RoomView) OnSelect(message *messages.UIMessage) { |
| 188 |
if !view.selecting || message == nil { |
| 189 |
return |
| 190 |
} |
| 191 |
switch view.selectReason { |
| 192 |
case SelectReply: |
| 193 |
view.replying = message.Event |
| 194 |
if len(view.selectContent) > 0 { |
| 195 |
go view.SendMessage(event.MsgText, view.selectContent) |
| 196 |
} |
| 197 |
case SelectEdit: |
| 198 |
view.SetEditing(message.Event) |
| 199 |
case SelectReact: |
| 200 |
go view.SendReaction(message.EventID, view.selectContent) |
| 201 |
case SelectRedact: |
| 202 |
go view.Redact(message.EventID, view.selectContent) |
| 203 |
case SelectDownload, SelectOpen: |
| 204 |
msg, ok := message.Renderer.(*messages.FileMessage) |
| 205 |
if ok { |
| 206 |
path := "" |
| 207 |
if len(view.selectContent) > 0 { |
| 208 |
path = view.selectContent |
| 209 |
} else if view.selectReason == SelectDownload { |
| 210 |
path = msg.Body |
| 211 |
} |
| 212 |
go view.Download(msg.URL, msg.File, path, view.selectReason == SelectOpen) |
| 213 |
} |
| 214 |
case SelectCopy: |
| 215 |
go view.CopyToClipboard(message.Renderer.PlainText(), view.selectContent) |
| 216 |
} |
| 217 |
view.selecting = false |
| 218 |
view.selectContent = "" |
| 219 |
view.MessageView().SetSelected(nil) |
| 220 |
view.input.Focus() |
| 221 |
} |
| 222 |
|
| 223 |
func (view *RoomView) GetStatus() string { |
| 224 |
var buf strings.Builder |
| 225 |
|
| 226 |
if view.editing != nil { |
| 227 |
buf.WriteString("Editing message - ") |
| 228 |
} else if view.replying != nil { |
| 229 |
buf.WriteString("Replying to ") |
| 230 |
buf.WriteString(string(view.replying.Sender)) |
| 231 |
buf.WriteString(" - ") |
| 232 |
} else if view.selecting { |
| 233 |
buf.WriteString("Selecting message to ") |
| 234 |
buf.WriteString(string(view.selectReason)) |
| 235 |
buf.WriteString(" - ") |
| 236 |
} |
| 237 |
|
| 238 |
if len(view.completions.list) > 0 { |
| 239 |
if view.completions.textCache != view.input.GetText() || view.completions.time.Add(10*time.Second).Before(time.Now()) { |
| 240 |
view.completions.list = []string{} |
| 241 |
} else { |
| 242 |
buf.WriteString(strings.Join(view.completions.list, ", ")) |
| 243 |
buf.WriteString(" - ") |
| 244 |
} |
| 245 |
} |
| 246 |
|
| 247 |
if len(view.typing) == 1 { |
| 248 |
buf.WriteString("Typing: " + string(view.typing[0])) |
| 249 |
buf.WriteString(" - ") |
| 250 |
} else if len(view.typing) > 1 { |
| 251 |
buf.WriteString("Typing: ") |
| 252 |
for i, userID := range view.typing { |
| 253 |
if i == len(view.typing)-1 { |
| 254 |
buf.WriteString(" and ") |
| 255 |
} else if i > 0 { |
| 256 |
buf.WriteString(", ") |
| 257 |
} |
| 258 |
buf.WriteString(string(userID)) |
| 259 |
} |
| 260 |
buf.WriteString(" - ") |
| 261 |
} |
| 262 |
|
| 263 |
return strings.TrimSuffix(buf.String(), " - ") |
| 264 |
} |
| 265 |
|
| 266 |
// Constants defining the size of the room view grid. |
| 267 |
const ( |
| 268 |
UserListBorderWidth = 1 |
| 269 |
UserListWidth = 20 |
| 270 |
StaticHorizontalSpace = UserListBorderWidth + UserListWidth |
| 271 |
|
| 272 |
TopicBarHeight = 1 |
| 273 |
StatusBarHeight = 1 |
| 274 |
|
| 275 |
MaxInputHeight = 5 |
| 276 |
) |
| 277 |
|
| 278 |
func (view *RoomView) Draw(screen mauview.Screen) { |
| 279 |
width, height := screen.Size() |
| 280 |
if width <= 0 || height <= 0 { |
| 281 |
return |
| 282 |
} |
| 283 |
|
| 284 |
if view.prevScreen != screen { |
| 285 |
view.topicScreen.Parent = screen |
| 286 |
view.contentScreen.Parent = screen |
| 287 |
view.statusScreen.Parent = screen |
| 288 |
view.inputScreen.Parent = screen |
| 289 |
view.ulBorderScreen.Parent = screen |
| 290 |
view.ulScreen.Parent = screen |
| 291 |
view.prevScreen = screen |
| 292 |
} |
| 293 |
|
| 294 |
view.input.PrepareDraw(width) |
| 295 |
inputHeight := view.input.GetTextHeight() |
| 296 |
if inputHeight > MaxInputHeight { |
| 297 |
inputHeight = MaxInputHeight |
| 298 |
} else if inputHeight < 1 { |
| 299 |
inputHeight = 1 |
| 300 |
} |
| 301 |
contentHeight := height - inputHeight - TopicBarHeight - StatusBarHeight |
| 302 |
contentWidth := width - StaticHorizontalSpace |
| 303 |
if view.config.Preferences.HideUserList { |
| 304 |
contentWidth = width |
| 305 |
} |
| 306 |
|
| 307 |
view.topicScreen.Width = width |
| 308 |
view.contentScreen.Width = contentWidth |
| 309 |
view.contentScreen.Height = contentHeight |
| 310 |
view.statusScreen.OffsetY = view.contentScreen.YEnd() |
| 311 |
view.statusScreen.Width = width |
| 312 |
view.inputScreen.Width = width |
| 313 |
view.inputScreen.OffsetY = view.statusScreen.YEnd() |
| 314 |
view.inputScreen.Height = inputHeight |
| 315 |
view.ulBorderScreen.OffsetX = view.contentScreen.XEnd() |
| 316 |
view.ulBorderScreen.Height = contentHeight |
| 317 |
view.ulScreen.OffsetX = view.ulBorderScreen.XEnd() |
| 318 |
view.ulScreen.Height = contentHeight |
| 319 |
|
| 320 |
// Draw everything |
| 321 |
view.topic.Draw(view.topicScreen) |
| 322 |
view.content.Draw(view.contentScreen) |
| 323 |
view.status.SetText(view.GetStatus()) |
| 324 |
view.status.Draw(view.statusScreen) |
| 325 |
view.input.Draw(view.inputScreen) |
| 326 |
if !view.config.Preferences.HideUserList { |
| 327 |
view.ulBorder.Draw(view.ulBorderScreen) |
| 328 |
view.userList.Draw(view.ulScreen) |
| 329 |
} |
| 330 |
} |
| 331 |
|
| 332 |
func (view *RoomView) ClearAllContext() { |
| 333 |
view.SetEditing(nil) |
| 334 |
view.StopSelecting() |
| 335 |
view.replying = nil |
| 336 |
view.input.Focus() |
| 337 |
} |
| 338 |
|
| 339 |
func (view *RoomView) OnKeyEvent(event mauview.KeyEvent) bool { |
| 340 |
msgView := view.MessageView() |
| 341 |
kb := config.Keybind{ |
| 342 |
Key: event.Key(), |
| 343 |
Ch: event.Rune(), |
| 344 |
Mod: event.Modifiers(), |
| 345 |
} |
| 346 |
|
| 347 |
if view.selecting { |
| 348 |
switch view.config.Keybindings.Visual[kb] { |
| 349 |
case "clear": |
| 350 |
view.ClearAllContext() |
| 351 |
case "select_prev": |
| 352 |
view.SelectPrevious() |
| 353 |
case "select_next": |
| 354 |
view.SelectNext() |
| 355 |
case "confirm": |
| 356 |
view.OnSelect(msgView.selected) |
| 357 |
default: |
| 358 |
return false |
| 359 |
} |
| 360 |
return true |
| 361 |
} |
| 362 |
|
| 363 |
switch view.config.Keybindings.Room[kb] { |
| 364 |
case "clear": |
| 365 |
view.ClearAllContext() |
| 366 |
return true |
| 367 |
case "scroll_up": |
| 368 |
if msgView.IsAtTop() { |
| 369 |
go view.parent.LoadHistory(view.Room.ID) |
| 370 |
} |
| 371 |
msgView.AddScrollOffset(+msgView.Height() / 2) |
| 372 |
return true |
| 373 |
case "scroll_down": |
| 374 |
msgView.AddScrollOffset(-msgView.Height() / 2) |
| 375 |
return true |
| 376 |
case "send": |
| 377 |
view.InputSubmit(view.input.GetText()) |
| 378 |
return true |
| 379 |
} |
| 380 |
return view.input.OnKeyEvent(event) |
| 381 |
} |
| 382 |
|
| 383 |
func (view *RoomView) OnPasteEvent(event mauview.PasteEvent) bool { |
| 384 |
return view.input.OnPasteEvent(event) |
| 385 |
} |
| 386 |
|
| 387 |
func (view *RoomView) OnMouseEvent(event mauview.MouseEvent) bool { |
| 388 |
switch { |
| 389 |
case view.contentScreen.IsInArea(event.Position()): |
| 390 |
return view.content.OnMouseEvent(view.contentScreen.OffsetMouseEvent(event)) |
| 391 |
case view.topicScreen.IsInArea(event.Position()): |
| 392 |
return view.topic.OnMouseEvent(view.topicScreen.OffsetMouseEvent(event)) |
| 393 |
case view.inputScreen.IsInArea(event.Position()): |
| 394 |
return view.input.OnMouseEvent(view.inputScreen.OffsetMouseEvent(event)) |
| 395 |
} |
| 396 |
return false |
| 397 |
} |
| 398 |
|
| 399 |
func (view *RoomView) SetCompletions(completions []string) { |
| 400 |
view.completions.list = completions |
| 401 |
view.completions.textCache = view.input.GetText() |
| 402 |
view.completions.time = time.Now() |
| 403 |
} |
| 404 |
|
| 405 |
func (view *RoomView) loadTyping() { |
| 406 |
for index, user := range view.typing { |
| 407 |
member := view.Room.GetMember(id.UserID(user)) |
| 408 |
if member != nil { |
| 409 |
view.typing[index] = member.Displayname |
| 410 |
} |
| 411 |
} |
| 412 |
} |
| 413 |
|
| 414 |
func (view *RoomView) SetTyping(users []id.UserID) { |
| 415 |
view.typing = make([]string, len(users)) |
| 416 |
for i, user := range users { |
| 417 |
view.typing[i] = string(user) |
| 418 |
} |
| 419 |
if view.Room.Loaded() { |
| 420 |
view.loadTyping() |
| 421 |
} |
| 422 |
} |
| 423 |
|
| 424 |
var editHTMLParser = &format.HTMLParser{ |
| 425 |
PillConverter: func(displayname, mxid, eventID string, ctx format.Context) string { |
| 426 |
if len(eventID) > 0 { |
| 427 |
return fmt.Sprintf(`[%s](https://matrix.to/#/%s/%s)`, displayname, mxid, eventID) |
| 428 |
} else { |
| 429 |
return fmt.Sprintf(`[%s](https://matrix.to/#/%s)`, displayname, mxid) |
| 430 |
} |
| 431 |
}, |
| 432 |
Newline: "\n", |
| 433 |
HorizontalLine: "\n---\n", |
| 434 |
} |
| 435 |
|
| 436 |
func (view *RoomView) SetEditing(evt *muksevt.Event) { |
| 437 |
if evt == nil { |
| 438 |
view.editing = nil |
| 439 |
view.SetInputText(view.editMoveText) |
| 440 |
view.editMoveText = "" |
| 441 |
} else { |
| 442 |
if view.editing == nil { |
| 443 |
view.editMoveText = view.GetInputText() |
| 444 |
} |
| 445 |
view.editing = evt |
| 446 |
// replying should never be non-nil when SetEditing, but do this just to be safe |
| 447 |
view.replying = nil |
| 448 |
msgContent := view.editing.Content.AsMessage() |
| 449 |
if len(view.editing.Gomuks.Edits) > 0 { |
| 450 |
// This feels kind of dangerous, but I think it works |
| 451 |
msgContent = view.editing.Gomuks.Edits[len(view.editing.Gomuks.Edits)-1].Content.AsMessage().NewContent |
| 452 |
} |
| 453 |
text := msgContent.Body |
| 454 |
if len(msgContent.FormattedBody) > 0 && (!view.config.Preferences.DisableMarkdown || !view.config.Preferences.DisableHTML) { |
| 455 |
if view.config.Preferences.DisableMarkdown { |
| 456 |
text = msgContent.FormattedBody |
| 457 |
} else { |
| 458 |
text = editHTMLParser.Parse(msgContent.FormattedBody, make(format.Context)) |
| 459 |
} |
| 460 |
} |
| 461 |
if msgContent.MsgType == event.MsgEmote { |
| 462 |
text = "/me " + text |
| 463 |
} |
| 464 |
view.input.SetText(text) |
| 465 |
} |
| 466 |
view.status.SetText(view.GetStatus()) |
| 467 |
view.input.SetCursorOffset(-1) |
| 468 |
} |
| 469 |
|
| 470 |
type findFilter func(evt *muksevt.Event) bool |
| 471 |
|
| 472 |
func (view *RoomView) filterOwnOnly(evt *muksevt.Event) bool { |
| 473 |
return evt.Sender == view.parent.matrix.Client().UserID && evt.Type == event.EventMessage |
| 474 |
} |
| 475 |
|
| 476 |
func (view *RoomView) filterMediaOnly(evt *muksevt.Event) bool { |
| 477 |
content, ok := evt.Content.Parsed.(*event.MessageEventContent) |
| 478 |
return ok && (content.MsgType == event.MsgFile || |
| 479 |
content.MsgType == event.MsgImage || |
| 480 |
content.MsgType == event.MsgAudio || |
| 481 |
content.MsgType == event.MsgVideo) |
| 482 |
} |
| 483 |
|
| 484 |
func (view *RoomView) findMessage(current *muksevt.Event, forward bool, allow findFilter) *messages.UIMessage { |
| 485 |
currentFound := current == nil |
| 486 |
msgs := view.MessageView().messages |
| 487 |
for i := 0; i < len(msgs); i++ { |
| 488 |
index := i |
| 489 |
if !forward { |
| 490 |
index = len(msgs) - i - 1 |
| 491 |
} |
| 492 |
evt := msgs[index] |
| 493 |
if evt.EventID == "" || string(evt.EventID) == evt.TxnID || evt.IsService { |
| 494 |
continue |
| 495 |
} else if currentFound { |
| 496 |
if allow == nil || allow(evt.Event) { |
| 497 |
return evt |
| 498 |
} |
| 499 |
} else if evt.EventID == current.ID { |
| 500 |
currentFound = true |
| 501 |
} |
| 502 |
} |
| 503 |
return nil |
| 504 |
} |
| 505 |
|
| 506 |
func (view *RoomView) EditNext() { |
| 507 |
if view.editing == nil { |
| 508 |
return |
| 509 |
} |
| 510 |
foundMsg := view.findMessage(view.editing, true, view.filterOwnOnly) |
| 511 |
view.SetEditing(foundMsg.GetEvent()) |
| 512 |
} |
| 513 |
|
| 514 |
func (view *RoomView) EditPrevious() { |
| 515 |
if view.replying != nil { |
| 516 |
return |
| 517 |
} |
| 518 |
foundMsg := view.findMessage(view.editing, false, view.filterOwnOnly) |
| 519 |
if foundMsg != nil { |
| 520 |
view.SetEditing(foundMsg.GetEvent()) |
| 521 |
} |
| 522 |
} |
| 523 |
|
| 524 |
func (view *RoomView) SelectNext() { |
| 525 |
msgView := view.MessageView() |
| 526 |
if msgView.selected == nil { |
| 527 |
return |
| 528 |
} |
| 529 |
var filter findFilter |
| 530 |
if view.selectReason == SelectDownload || view.selectReason == SelectOpen { |
| 531 |
filter = view.filterMediaOnly |
| 532 |
} |
| 533 |
foundMsg := view.findMessage(msgView.selected.GetEvent(), true, filter) |
| 534 |
if foundMsg != nil { |
| 535 |
msgView.SetSelected(foundMsg) |
| 536 |
// TODO scroll selected message into view |
| 537 |
} |
| 538 |
} |
| 539 |
|
| 540 |
func (view *RoomView) SelectPrevious() { |
| 541 |
msgView := view.MessageView() |
| 542 |
var filter findFilter |
| 543 |
if view.selectReason == SelectDownload || view.selectReason == SelectOpen { |
| 544 |
filter = view.filterMediaOnly |
| 545 |
} |
| 546 |
foundMsg := view.findMessage(msgView.selected.GetEvent(), false, filter) |
| 547 |
if foundMsg != nil { |
| 548 |
msgView.SetSelected(foundMsg) |
| 549 |
// TODO scroll selected message into view |
| 550 |
} |
| 551 |
} |
| 552 |
|
| 553 |
type completion struct { |
| 554 |
displayName string |
| 555 |
id string |
| 556 |
} |
| 557 |
|
| 558 |
func (view *RoomView) AutocompleteUser(existingText string) (completions []completion) { |
| 559 |
textWithoutPrefix := strings.TrimPrefix(existingText, "@") |
| 560 |
for userID, user := range view.Room.GetMembers() { |
| 561 |
if user.Displayname == textWithoutPrefix || string(userID) == existingText { |
| 562 |
// Exact match, return that. |
| 563 |
return []completion{{user.Displayname, string(userID)}} |
| 564 |
} |
| 565 |
|
| 566 |
if strings.HasPrefix(user.Displayname, textWithoutPrefix) || strings.HasPrefix(string(userID), existingText) { |
| 567 |
completions = append(completions, completion{user.Displayname, string(userID)}) |
| 568 |
} |
| 569 |
} |
| 570 |
return |
| 571 |
} |
| 572 |
|
| 573 |
func (view *RoomView) AutocompleteRoom(existingText string) (completions []completion) { |
| 574 |
for _, room := range view.parent.rooms { |
| 575 |
alias := string(room.Room.GetCanonicalAlias()) |
| 576 |
if alias == existingText { |
| 577 |
// Exact match, return that. |
| 578 |
return []completion{{alias, string(room.Room.ID)}} |
| 579 |
} |
| 580 |
if strings.HasPrefix(alias, existingText) { |
| 581 |
completions = append(completions, completion{alias, string(room.Room.ID)}) |
| 582 |
continue |
| 583 |
} |
| 584 |
} |
| 585 |
return |
| 586 |
} |
| 587 |
|
| 588 |
func (view *RoomView) AutocompleteEmoji(word string) (completions []string) { |
| 589 |
if word[0] != ':' { |
| 590 |
return |
| 591 |
} |
| 592 |
var valueCompletion1 string |
| 593 |
var manyValues bool |
| 594 |
for name, value := range emoji.CodeMap() { |
| 595 |
if name == word { |
| 596 |
return []string{value} |
| 597 |
} else if strings.HasPrefix(name, word) { |
| 598 |
completions = append(completions, name) |
| 599 |
if valueCompletion1 == "" { |
| 600 |
valueCompletion1 = value |
| 601 |
} else if valueCompletion1 != value { |
| 602 |
manyValues = true |
| 603 |
} |
| 604 |
} |
| 605 |
} |
| 606 |
if !manyValues && len(completions) > 0 { |
| 607 |
return []string{emoji.CodeMap()[completions[0]]} |
| 608 |
} |
| 609 |
return |
| 610 |
} |
| 611 |
|
| 612 |
func findWordToTabComplete(text string) string { |
| 613 |
output := "" |
| 614 |
runes := []rune(text) |
| 615 |
for i := len(runes) - 1; i >= 0; i-- { |
| 616 |
if unicode.IsSpace(runes[i]) { |
| 617 |
break |
| 618 |
} |
| 619 |
output = string(runes[i]) + output |
| 620 |
} |
| 621 |
return output |
| 622 |
} |
| 623 |
|
| 624 |
var ( |
| 625 |
mentionMarkdown = "[%[1]s](https://matrix.to/#/%[2]s)" |
| 626 |
mentionHTML = `<a href="https://matrix.to/#/%[2]s">%[1]s</a>` |
| 627 |
mentionPlaintext = "%[1]s" |
| 628 |
) |
| 629 |
|
| 630 |
func (view *RoomView) defaultAutocomplete(word string, startIndex int) (strCompletions []string, strCompletion string) { |
| 631 |
if len(word) == 0 { |
| 632 |
return []string{}, "" |
| 633 |
} |
| 634 |
|
| 635 |
completions := view.AutocompleteUser(word) |
| 636 |
completions = append(completions, view.AutocompleteRoom(word)...) |
| 637 |
|
| 638 |
if len(completions) == 1 { |
| 639 |
completion := completions[0] |
| 640 |
template := mentionMarkdown |
| 641 |
if view.config.Preferences.DisableMarkdown { |
| 642 |
if view.config.Preferences.DisableHTML { |
| 643 |
template = mentionPlaintext |
| 644 |
} else { |
| 645 |
template = mentionHTML |
| 646 |
} |
| 647 |
} |
| 648 |
strCompletion = fmt.Sprintf(template, completion.displayName, completion.id) |
| 649 |
if startIndex == 0 && completion.id[0] == '@' { |
| 650 |
strCompletion = strCompletion + ":" |
| 651 |
} |
| 652 |
} else if len(completions) > 1 { |
| 653 |
for _, completion := range completions { |
| 654 |
strCompletions = append(strCompletions, completion.displayName) |
| 655 |
} |
| 656 |
} |
| 657 |
|
| 658 |
strCompletions = append(strCompletions, view.parent.cmdProcessor.AutocompleteCommand(word)...) |
| 659 |
strCompletions = append(strCompletions, view.AutocompleteEmoji(word)...) |
| 660 |
|
| 661 |
return |
| 662 |
} |
| 663 |
|
| 664 |
func (view *RoomView) InputTabComplete(text string, cursorOffset int) { |
| 665 |
if len(text) == 0 { |
| 666 |
return |
| 667 |
} |
| 668 |
|
| 669 |
str := runewidth.Truncate(text, cursorOffset, "") |
| 670 |
word := findWordToTabComplete(str) |
| 671 |
startIndex := len(str) - len(word) |
| 672 |
|
| 673 |
var strCompletion string |
| 674 |
|
| 675 |
strCompletions, newText, ok := view.parent.cmdProcessor.Autocomplete(view, text, cursorOffset) |
| 676 |
if !ok { |
| 677 |
strCompletions, strCompletion = view.defaultAutocomplete(word, startIndex) |
| 678 |
} |
| 679 |
|
| 680 |
if len(strCompletions) > 0 { |
| 681 |
strCompletion = util.LongestCommonPrefix(strCompletions) |
| 682 |
sort.Sort(sort.StringSlice(strCompletions)) |
| 683 |
} |
| 684 |
if len(strCompletion) > 0 && len(strCompletions) < 2 { |
| 685 |
strCompletion += " " |
| 686 |
strCompletions = []string{} |
| 687 |
} |
| 688 |
|
| 689 |
if len(strCompletion) > 0 && newText == text { |
| 690 |
newText = str[0:startIndex] + strCompletion + text[len(str):] |
| 691 |
} |
| 692 |
|
| 693 |
view.input.SetTextAndMoveCursor(newText) |
| 694 |
view.SetCompletions(strCompletions) |
| 695 |
} |
| 696 |
|
| 697 |
func (view *RoomView) InputSubmit(text string) { |
| 698 |
if len(text) == 0 { |
| 699 |
return |
| 700 |
} else if cmd := view.parent.cmdProcessor.ParseCommand(view, text); cmd != nil { |
| 701 |
go view.parent.cmdProcessor.HandleCommand(cmd) |
| 702 |
} else { |
| 703 |
go view.SendMessage(event.MsgText, text) |
| 704 |
} |
| 705 |
view.editMoveText = "" |
| 706 |
view.SetInputText("") |
| 707 |
} |
| 708 |
|
| 709 |
func (view *RoomView) CopyToClipboard(text string, register string) { |
| 710 |
if register == "clipboard" || register == "primary" { |
| 711 |
err := clipboard.WriteAll(text, register) |
| 712 |
if err != nil { |
| 713 |
view.AddServiceMessage(fmt.Sprintf("Clipboard unsupported: %v", err)) |
| 714 |
view.parent.parent.Render() |
| 715 |
} |
| 716 |
} else { |
| 717 |
view.AddServiceMessage(fmt.Sprintf("Clipboard register %v unsupported", register)) |
| 718 |
view.parent.parent.Render() |
| 719 |
} |
| 720 |
} |
| 721 |
|
| 722 |
func (view *RoomView) Download(url id.ContentURI, file *attachment.EncryptedFile, filename string, openFile bool) { |
| 723 |
path, err := view.parent.matrix.DownloadToDisk(url, file, filename) |
| 724 |
if err != nil { |
| 725 |
view.AddServiceMessage(fmt.Sprintf("Failed to download media: %v", err)) |
| 726 |
view.parent.parent.Render() |
| 727 |
return |
| 728 |
} |
| 729 |
view.AddServiceMessage(fmt.Sprintf("File downloaded to %s", path)) |
| 730 |
view.parent.parent.Render() |
| 731 |
if openFile { |
| 732 |
debug.Print("Opening file", path) |
| 733 |
open.Open(path) |
| 734 |
} |
| 735 |
} |
| 736 |
|
| 737 |
func (view *RoomView) Redact(eventID id.EventID, reason string) { |
| 738 |
defer debug.Recover() |
| 739 |
err := view.parent.matrix.Redact(view.Room.ID, eventID, reason) |
| 740 |
if err != nil { |
| 741 |
if httpErr, ok := err.(mautrix.HTTPError); ok { |
| 742 |
err = httpErr |
| 743 |
if respErr := httpErr.RespError; respErr != nil { |
| 744 |
err = respErr |
| 745 |
} |
| 746 |
} |
| 747 |
view.AddServiceMessage(fmt.Sprintf("Failed to redact message: %v", err)) |
| 748 |
view.parent.parent.Render() |
| 749 |
} |
| 750 |
} |
| 751 |
|
| 752 |
func (view *RoomView) SendReaction(eventID id.EventID, reaction string) { |
| 753 |
defer debug.Recover() |
| 754 |
if !view.config.Preferences.DisableEmojis { |
| 755 |
reaction = emoji.Sprint(reaction) |
| 756 |
} |
| 757 |
reaction = variationselector.Add(strings.TrimSpace(reaction)) |
| 758 |
debug.Print("Reacting to", eventID, "in", view.Room.ID, "with", reaction) |
| 759 |
eventID, err := view.parent.matrix.SendEvent(&muksevt.Event{ |
| 760 |
Event: &event.Event{ |
| 761 |
Type: event.EventReaction, |
| 762 |
RoomID: view.Room.ID, |
| 763 |
Content: event.Content{Parsed: &event.ReactionEventContent{RelatesTo: event.RelatesTo{ |
| 764 |
Type: event.RelAnnotation, |
| 765 |
EventID: eventID, |
| 766 |
Key: reaction, |
| 767 |
}}}, |
| 768 |
}, |
| 769 |
}) |
| 770 |
if err != nil { |
| 771 |
if httpErr, ok := err.(mautrix.HTTPError); ok { |
| 772 |
err = httpErr |
| 773 |
if respErr := httpErr.RespError; respErr != nil { |
| 774 |
err = respErr |
| 775 |
} |
| 776 |
} |
| 777 |
view.AddServiceMessage(fmt.Sprintf("Failed to send reaction: %v", err)) |
| 778 |
view.parent.parent.Render() |
| 779 |
} |
| 780 |
} |
| 781 |
|
| 782 |
func (view *RoomView) SendMessage(msgtype event.MessageType, text string) { |
| 783 |
view.SendMessageHTML(msgtype, text, "") |
| 784 |
} |
| 785 |
|
| 786 |
func (view *RoomView) getRelationForNewEvent() *ifc.Relation { |
| 787 |
if view.editing != nil { |
| 788 |
return &ifc.Relation{ |
| 789 |
Type: event.RelReplace, |
| 790 |
Event: view.editing, |
| 791 |
} |
| 792 |
} else if view.replying != nil { |
| 793 |
return &ifc.Relation{ |
| 794 |
Type: event.RelReply, |
| 795 |
Event: view.replying, |
| 796 |
} |
| 797 |
} |
| 798 |
return nil |
| 799 |
} |
| 800 |
|
| 801 |
func (view *RoomView) SendMessageHTML(msgtype event.MessageType, text, html string) { |
| 802 |
defer debug.Recover() |
| 803 |
debug.Print("Sending message", msgtype, text, "to", view.Room.ID) |
| 804 |
if !view.config.Preferences.DisableEmojis { |
| 805 |
text = emoji.Sprint(text) |
| 806 |
} |
| 807 |
rel := view.getRelationForNewEvent() |
| 808 |
evt := view.parent.matrix.PrepareMarkdownMessage(view.Room.ID, msgtype, text, html, rel) |
| 809 |
view.addLocalEcho(evt) |
| 810 |
} |
| 811 |
|
| 812 |
func (view *RoomView) SendMessageMedia(path string) { |
| 813 |
defer debug.Recover() |
| 814 |
debug.Print("Sending media at", path, "to", view.Room.ID) |
| 815 |
rel := view.getRelationForNewEvent() |
| 816 |
evt, err := view.parent.matrix.PrepareMediaMessage(view.Room, path, rel) |
| 817 |
if err != nil { |
| 818 |
view.AddServiceMessage(fmt.Sprintf("Failed to upload media: %v", err)) |
| 819 |
view.parent.parent.Render() |
| 820 |
return |
| 821 |
} |
| 822 |
view.addLocalEcho(evt) |
| 823 |
} |
| 824 |
|
| 825 |
func (view *RoomView) addLocalEcho(evt *muksevt.Event) { |
| 826 |
msg := view.parseEvent(evt.SomewhatDangerousCopy()) |
| 827 |
view.content.AddMessage(msg, AppendMessage) |
| 828 |
view.ClearAllContext() |
| 829 |
view.status.SetText(view.GetStatus()) |
| 830 |
eventID, err := view.parent.matrix.SendEvent(evt) |
| 831 |
if err != nil { |
| 832 |
msg.State = muksevt.StateSendFail |
| 833 |
// Show shorter version if available |
| 834 |
if httpErr, ok := err.(mautrix.HTTPError); ok { |
| 835 |
err = httpErr |
| 836 |
if respErr := httpErr.RespError; respErr != nil { |
| 837 |
err = respErr |
| 838 |
} |
| 839 |
} |
| 840 |
view.AddServiceMessage(fmt.Sprintf("Failed to send message: %v", err)) |
| 841 |
view.parent.parent.Render() |
| 842 |
} else { |
| 843 |
debug.Print("Event ID received:", eventID) |
| 844 |
msg.EventID = eventID |
| 845 |
msg.State = muksevt.StateDefault |
| 846 |
view.MessageView().setMessageID(msg) |
| 847 |
view.parent.parent.Render() |
| 848 |
} |
| 849 |
} |
| 850 |
|
| 851 |
func (view *RoomView) MessageView() *MessageView { |
| 852 |
return view.content |
| 853 |
} |
| 854 |
|
| 855 |
func (view *RoomView) MxRoom() *rooms.Room { |
| 856 |
return view.Room |
| 857 |
} |
| 858 |
|
| 859 |
func (view *RoomView) Update() { |
| 860 |
topicStr := strings.TrimSpace(strings.ReplaceAll(view.Room.GetTopic(), "\n", " ")) |
| 861 |
if view.config.Preferences.HideRoomList { |
| 862 |
if len(topicStr) > 0 { |
| 863 |
topicStr = fmt.Sprintf("%s - %s", view.Room.GetTitle(), topicStr) |
| 864 |
} else { |
| 865 |
topicStr = view.Room.GetTitle() |
| 866 |
} |
| 867 |
topicStr = strings.TrimSpace(topicStr) |
| 868 |
} |
| 869 |
view.topic.SetText(topicStr) |
| 870 |
if !view.userListLoaded { |
| 871 |
view.UpdateUserList() |
| 872 |
} |
| 873 |
} |
| 874 |
|
| 875 |
func (view *RoomView) UpdateUserList() { |
| 876 |
pls := &event.PowerLevelsEventContent{} |
| 877 |
if plEvent := view.Room.GetStateEvent(event.StatePowerLevels, ""); plEvent != nil { |
| 878 |
pls = plEvent.Content.AsPowerLevels() |
| 879 |
} |
| 880 |
view.userList.Update(view.Room.GetMembers(), pls) |
| 881 |
view.userListLoaded = true |
| 882 |
} |
| 883 |
|
| 884 |
func (view *RoomView) AddServiceMessage(text string) { |
| 885 |
view.content.AddMessage(messages.NewServiceMessage(text), AppendMessage) |
| 886 |
} |
| 887 |
|
| 888 |
func (view *RoomView) parseEvent(evt *muksevt.Event) *messages.UIMessage { |
| 889 |
return messages.ParseEvent(view.parent.matrix, view.parent, view.Room, evt) |
| 890 |
} |
| 891 |
|
| 892 |
func (view *RoomView) AddHistoryEvent(evt *muksevt.Event) { |
| 893 |
if msg := view.parseEvent(evt); msg != nil { |
| 894 |
view.content.AddMessage(msg, PrependMessage) |
| 895 |
} |
| 896 |
} |
| 897 |
|
| 898 |
func (view *RoomView) AddEvent(evt *muksevt.Event) ifc.Message { |
| 899 |
if msg := view.parseEvent(evt); msg != nil { |
| 900 |
view.content.AddMessage(msg, AppendMessage) |
| 901 |
return msg |
| 902 |
} |
| 903 |
return nil |
| 904 |
} |
| 905 |
|
| 906 |
func (view *RoomView) AddRedaction(redactedEvt *muksevt.Event) { |
| 907 |
view.AddEvent(redactedEvt) |
| 908 |
} |
| 909 |
|
| 910 |
func (view *RoomView) AddEdit(evt *muksevt.Event) { |
| 911 |
if msg := view.parseEvent(evt); msg != nil { |
| 912 |
view.content.AddMessage(msg, IgnoreMessage) |
| 913 |
} |
| 914 |
} |
| 915 |
|
| 916 |
func (view *RoomView) AddReaction(evt *muksevt.Event, key string) { |
| 917 |
msgView := view.MessageView() |
| 918 |
msg := msgView.getMessageByID(evt.ID) |
| 919 |
if msg == nil { |
| 920 |
// Message not in view, nothing to do |
| 921 |
return |
| 922 |
} |
| 923 |
heightChanged := len(msg.Reactions) == 0 |
| 924 |
msg.AddReaction(key) |
| 925 |
if heightChanged { |
| 926 |
// Replace buffer to update height of message |
| 927 |
msgView.replaceBuffer(msg, msg) |
| 928 |
} |
| 929 |
} |
| 930 |
|
| 931 |
func (view *RoomView) GetEvent(eventID id.EventID) ifc.Message { |
| 932 |
message, ok := view.content.messageIDs[eventID] |
| 933 |
if !ok { |
| 934 |
return nil |
| 935 |
} |
| 936 |
return message |
| 937 |
} |