aboutsummaryrefslogtreecommitdiff
blob: 7ceb84c155e9d4911442ddd5c1586f4f0c441d37 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
// Used to show the landing page of the application

package message

import (
	"archives/pkg/database"
	"archives/pkg/models"
	"fmt"
	"net/http"
	"strings"
)

// Show renders a template to show the landing page of the application
func Show(w http.ResponseWriter, r *http.Request) {

	urlParts := strings.Split(r.URL.Path, "/")
	listName := urlParts[1]
	messageHash := urlParts[len(urlParts)-1]

	message := &models.Message{Id: messageHash}
	err := database.DBCon.Model(message).
		Column("message.*").
		Relation("InReplyTo").
		Relation("References").
		WherePK().
		Select()

	if err != nil {
		http.NotFound(w, r)
		return
	}

	var queryParts []string
	for _, reference := range message.References {
		fmt.Println("> " + reference.Id)
		queryParts = append(queryParts, "reference_id = '" + reference.Id + "'")
	}
	query := strings.Join(queryParts, " OR ")

	var refs []*models.MessageToReferences
	err = database.DBCon.Model(&refs).
		Where(query).
		Select()

	// part 2
	// TODO only if len(refs) >= 1
	var nextQueryParts []string
	for _, reference := range refs {
		fmt.Println("> " + reference.MessageId)
		nextQueryParts = append(nextQueryParts, "id = '" + reference.MessageId + "'")
	}
	nextQuery := strings.Join(nextQueryParts, " OR ")

	var replies []*models.Message
	err = database.DBCon.Model(&replies).
		Where(nextQuery).
		// Where date is newer than message
		Select()

	fmt.Println("-------------------")
	fmt.Println(err)
	fmt.Println(len(replies))

	renderMessageTemplate(w, listName, message, replies)
}