From 165e85189fad078b40387ed5d86066521a5d023e Mon Sep 17 00:00:00 2001 From: Max Magorsch Date: Wed, 24 Jun 2020 21:54:41 +0000 Subject: Fix the computation of replies to a message Signed-off-by: Max Magorsch --- pkg/app/message/show.go | 51 +++++++++++++++++++++++++++++++++++++++---------- 1 file changed, 41 insertions(+), 10 deletions(-) (limited to 'pkg/app') diff --git a/pkg/app/message/show.go b/pkg/app/message/show.go index e8eb456..baed3f1 100644 --- a/pkg/app/message/show.go +++ b/pkg/app/message/show.go @@ -6,6 +6,7 @@ import ( "archives/pkg/database" "archives/pkg/models" "net/http" + "sort" "strings" ) @@ -29,10 +30,15 @@ func Show(w http.ResponseWriter, r *http.Request) { return } + // + // Compute Replies + // + var replies []*models.Message var queryParts []string for _, reference := range message.References { queryParts = append(queryParts, "reference_id = '" + reference.Id + "'") } + queryParts = append(queryParts, "reference_id = '" + message.Id + "'") query := strings.Join(queryParts, " OR ") var refs []*models.MessageToReferences @@ -41,18 +47,43 @@ func Show(w http.ResponseWriter, r *http.Request) { Select() // part 2 - // TODO only if len(refs) >= 1 - var nextQueryParts []string - for _, reference := range refs { - nextQueryParts = append(nextQueryParts, "id = '" + reference.MessageId + "'") + if len(refs) > 0 { + var nextQueryParts []string + for _, reference := range refs { + nextQueryParts = append(nextQueryParts, "id = '" + reference.MessageId + "'") + } + nextQuery := strings.Join(nextQueryParts, " OR ") + + + err = database.DBCon.Model(&replies). + Where(nextQuery). + Where("date >= '" + message.Date.Format("2006-01-02 15:04:05") + "'"). + Where("NOT id = ?", message.Id). + Select() + + // + // If In-Reply is null, but there are references we will use the last message + // in the thread as In-Reply-To message + // + var inReplyTo []*models.Message + if message.InReplyToId == "" || message.InReplyTo == nil { + err = database.DBCon.Model(&inReplyTo). + Where(nextQuery). + Where("date <= '" + message.Date.Format("2006-01-02 15:04:05") + "'"). + Where("NOT id = ?", message.Id). + Order("date DESC"). + Limit(1). + Select() + + if err == nil && len(inReplyTo) > 0 { + message.InReplyTo = inReplyTo[0] + } + } } - nextQuery := strings.Join(nextQueryParts, " OR ") - var replies []*models.Message - err = database.DBCon.Model(&replies). - Where(nextQuery). - // Where date is newer than message - Select() + sort.Slice(replies, func(i,j int) bool { + return replies[i].Date.Before(replies[j].Date) + }) renderMessageTemplate(w, listName, message, replies) } -- cgit v1.2.3-18-g5258