dkforest

A forum and chat platform (onion)
git clone https://git.dasho.dev/n0tr1v/dkforest.git
Log | Files | Refs | LICENSE

commit 7e823a19734cc569fe5d5d67381624b9e5cb4dde
parent 9888c68c43e01cf40adf9c7e68c06de114b58c3c
Author: n0tr1v <n0tr1v@protonmail.com>
Date:   Fri,  2 Jun 2023 21:01:40 -0700

shorten archive links

Diffstat:
Mpkg/web/handlers/api/v1/msgInterceptor.go | 2+-
Mpkg/web/handlers/api/v1/topBarHandler.go | 9++++++++-
Mpkg/web/handlers/api/v1/topBarHandler_test.go | 13++++++++-----
3 files changed, 17 insertions(+), 7 deletions(-)

diff --git a/pkg/web/handlers/api/v1/msgInterceptor.go b/pkg/web/handlers/api/v1/msgInterceptor.go @@ -97,7 +97,7 @@ func ProcessRawMessage(db *database.DkfDB, in, roomKey string, authUserID databa html = convertMarkdown(html) html = convertBangShortcuts(html) html = convertArchiveLinks(db, html, roomID, authUserID) - html = convertLinks(html, db.GetUserByUsername, db.GetLinkByShorthand) + html = convertLinks(html, db.GetUserByUsername, db.GetLinkByShorthand, db.GetChatMessageByUUID) html = linkDefaultRooms(html) html, taggedUsersIDsMap := colorifyTaggedUsers(html, db.GetUsersByUsername) html = linkRoomTags(db, html) diff --git a/pkg/web/handlers/api/v1/topBarHandler.go b/pkg/web/handlers/api/v1/topBarHandler.go @@ -815,6 +815,7 @@ func convertLinksWithoutScheme(in string) string { var linkRgxStr = `(http|ftp|https):\/\/([\w\-_]+(?:(?:\.[\w\-_]+)+))([\w\-\.,@?^=%&amp;:/~\+#\(\)]*[\w\-\@?^=%&amp;/~\+#\(\)])?` var profileRgxStr = `/u/\w{3,20}` var linkShorthandRgxStr = `/l/\w{3,20}` +var dkfArchiveRgx = regexp.MustCompile(`/chat/[^/]+/archive\?uuid=([\w-]{36})#[\w-]{36}`) var linkOrProfileRgx = regexp.MustCompile(`(` + linkRgxStr + `|` + profileRgxStr + `|` + linkShorthandRgxStr + `)`) var userProfileLinkRgx = regexp.MustCompile(`^` + profileRgxStr + `$`) var linkShorthandPageLinkRgx = regexp.MustCompile(`^` + linkShorthandRgxStr + `$`) @@ -844,7 +845,8 @@ func splitQuote(in string) (string, string) { func convertLinks(in string, getUserByUsername func(database.Username) (database.User, error), - getLinkByShorthand func(string) (database.Link, error)) string { + getLinkByShorthand func(string) (database.Link, error), + getChatMessageByUUID func(string) (database.ChatMessage, error)) string { quote, rest := splitQuote(in) libredditURLs := []string{ @@ -1040,6 +1042,11 @@ func convertLinks(in string, label = link href = trimmed } + if m := dkfArchiveRgx.FindStringSubmatch(label); len(m) == 2 { + if msg, err := getChatMessageByUUID(m[1]); err == nil { + label = msg.CreatedAt.Format("[Jan 02 03:04:05]") + } + } // Allows to have messages such as: "my profile is /u/username :)" if userProfileLinkRgx.MatchString(trimmed) { if user, err := getUserByUsername(database.Username(strings.TrimPrefix(trimmed, "/u/"))); err == nil { diff --git a/pkg/web/handlers/api/v1/topBarHandler_test.go b/pkg/web/handlers/api/v1/topBarHandler_test.go @@ -130,29 +130,32 @@ func TestConvertLinks(t *testing.T) { getLinkByShorthand := func(shorthand string) (database.Link, error) { return database.Link{}, nil } + getChatMessageByUUID := func(uuid string) (database.ChatMessage, error) { + return database.ChatMessage{}, nil + } // Replace /u/username to link when user exists - actual := convertLinks("this is /u/username a test", getUserByUsername, getLinkByShorthand) + actual := convertLinks("this is /u/username a test", getUserByUsername, getLinkByShorthand, getChatMessageByUUID) expected := `this is <a href="/u/username" rel="noopener noreferrer" target="_blank">/u/username</a> a test` assert.Equal(t, expected, actual) // Does not replace /u/notExist to link when user does not exist - actual = convertLinks("this is /u/notExist a test", getUserByUsername, getLinkByShorthand) + actual = convertLinks("this is /u/notExist a test", getUserByUsername, getLinkByShorthand, getChatMessageByUUID) expected = `this is /u/notExist a test` assert.Equal(t, expected, actual) // Fix case errors - actual = convertLinks("this is /u/uSerNaMe a test", getUserByUsername, getLinkByShorthand) + actual = convertLinks("this is /u/uSerNaMe a test", getUserByUsername, getLinkByShorthand, getChatMessageByUUID) expected = `this is <a href="/u/username" rel="noopener noreferrer" target="_blank">/u/username</a> a test` assert.Equal(t, expected, actual) // Convert long dkf url - actual = convertLinks("this is http://dkforestseeaaq2dqz2uflmlsybvnq2irzn4ygyvu53oazyorednviid.onion/u/username a test", getUserByUsername, getLinkByShorthand) + actual = convertLinks("this is http://dkforestseeaaq2dqz2uflmlsybvnq2irzn4ygyvu53oazyorednviid.onion/u/username a test", getUserByUsername, getLinkByShorthand, getChatMessageByUUID) expected = `this is <a href="/u/username" rel="noopener noreferrer" target="_blank">/u/username</a> a test` assert.Equal(t, expected, actual) // Shorten dkf url but keep the short form since the user does not exist - actual = convertLinks("this is http://dkforestseeaaq2dqz2uflmlsybvnq2irzn4ygyvu53oazyorednviid.onion/u/notExist a test", getUserByUsername, getLinkByShorthand) + actual = convertLinks("this is http://dkforestseeaaq2dqz2uflmlsybvnq2irzn4ygyvu53oazyorednviid.onion/u/notExist a test", getUserByUsername, getLinkByShorthand, getChatMessageByUUID) expected = `this is <a href="/u/notExist" rel="noopener noreferrer" target="_blank">http://dkf.onion/u/notExist</a> a test` assert.Equal(t, expected, actual) }