From 8456c5d0f5433afa091f1df908075c1c5b2cba4a Mon Sep 17 00:00:00 2001 From: Noah Petherbridge Date: Sun, 24 Sep 2023 14:29:54 -0700 Subject: [PATCH] Messages: if thread already open, go directly there instead of dedicated Compose page --- pkg/controller/inbox/compose.go | 6 ++++++ pkg/models/message.go | 15 +++++++++++++++ web/static/js/bulma.js | 2 +- 3 files changed, 22 insertions(+), 1 deletion(-) diff --git a/pkg/controller/inbox/compose.go b/pkg/controller/inbox/compose.go index aed3627..490047b 100644 --- a/pkg/controller/inbox/compose.go +++ b/pkg/controller/inbox/compose.go @@ -41,6 +41,12 @@ func Compose() http.HandlerFunc { return } + // If we already have a thread open with them, go to that instead of the stand-alone compose page. + if msgID, ok := models.HasMessageThread(currentUser, user); ok { + templates.Redirect(w, fmt.Sprintf("/messages/read/%d", msgID)) + return + } + // POSTing? if r.Method == http.MethodPost { var ( diff --git a/pkg/models/message.go b/pkg/models/message.go index 25e7507..fd0f884 100644 --- a/pkg/models/message.go +++ b/pkg/models/message.go @@ -75,6 +75,21 @@ func GetMessageThread(sourceUserID, targetUserID uint64, pager *Pagination) ([]* return m, result.Error } +// HasMessageThread returns if a message thread exists between two users (either direction). +// Returns the ID of the thread and a boolean OK that it existed. +func HasMessageThread(a, b *User) (uint64, bool) { + var pager = &Pagination{ + Page: 1, + PerPage: 1, + Sort: "updated_at desc", + } + messages, err := GetMessageThread(a.ID, b.ID, pager) + if err == nil && len(messages) > 0 { + return messages[0].ID, true + } + return 0, false +} + // DeleteMessageThread removes all message history between two people. func DeleteMessageThread(message *Message) error { return DB.Where( diff --git a/web/static/js/bulma.js b/web/static/js/bulma.js index 26b9865..a3e41f5 100644 --- a/web/static/js/bulma.js +++ b/web/static/js/bulma.js @@ -133,7 +133,7 @@ document.addEventListener('DOMContentLoaded', () => { node.addEventListener("click", e => { if (node.classList.contains("blurred-explicit")) { node.classList.remove("blurred-explicit"); - if (node.tagName.toLowerCase() !== "video") { + if (node.tagName !== "VIDEO") { e.preventDefault(); e.stopPropagation(); }