@@ -11,6 +11,29 @@ import (
11
11
"strings"
12
12
)
13
13
14
+ // Messages is an array of `*Message`.
15
+ type Messages []* Message
16
+
17
+ // Sort sorts messages by `Message.Ts` ascendant order.
18
+ func (msgs Messages ) Sort () {
19
+ sort .SliceStable (msgs , func (i , j int ) bool {
20
+ // must be the same digits, so no need to convert the timestamp to a number
21
+ return msgs [i ].Ts < msgs [j ].Ts
22
+ })
23
+ }
24
+
25
+ // MessagesMap is a map, maps MessageMonthKey to Messages.
26
+ type MessagesMap map [MessageMonthKey ]Messages
27
+
28
+ // Keys returns all keys in the map.
29
+ func (mm MessagesMap ) Keys () []MessageMonthKey {
30
+ keys := make ([]MessageMonthKey , 0 , len (mm ))
31
+ for key := range mm {
32
+ keys = append (keys , key )
33
+ }
34
+ return keys
35
+ }
36
+
14
37
// MessageTable : メッセージデータを保持する
15
38
// スレッドは投稿時刻からどのスレッドへの返信かが判断できるためThreadMapのキー
16
39
// はtsである。
@@ -20,7 +43,7 @@ import (
20
43
type MessageTable struct {
21
44
// key: thread timestamp
22
45
ThreadMap map [string ]* Thread
23
- MsgsMap map [ MessageMonthKey ][] Message
46
+ MsgsMap MessagesMap
24
47
// key: file path
25
48
loadedFiles map [string ]struct {}
26
49
}
@@ -32,7 +55,7 @@ type MessageTable struct {
32
55
func NewMessageTable () * MessageTable {
33
56
return & MessageTable {
34
57
ThreadMap : map [string ]* Thread {},
35
- MsgsMap : map [ MessageMonthKey ][] Message {},
58
+ MsgsMap : MessagesMap {},
36
59
loadedFiles : map [string ]struct {}{},
37
60
}
38
61
}
@@ -79,13 +102,15 @@ func (m *MessageTable) ReadLogFile(path string) error {
79
102
return nil
80
103
}
81
104
82
- var msgs []Message
83
- var visibleMsgs []Message
105
+ var msgs Messages
84
106
err = ReadFileAsJSON (path , & msgs )
85
107
if err != nil {
86
108
return fmt .Errorf ("failed to unmarshal %s: %w" , path , err )
87
109
}
88
- for i , msg := range msgs {
110
+
111
+ // assort messages, visible and threaded.
112
+ var visibleMsgs Messages
113
+ for _ , msg := range msgs {
89
114
if ! msg .IsVisible () {
90
115
continue
91
116
}
@@ -97,18 +122,12 @@ func (m *MessageTable) ReadLogFile(path string) error {
97
122
visibleMsgs = append (visibleMsgs , msg )
98
123
}
99
124
if threadTs != "" {
100
- if m .ThreadMap [threadTs ] == nil {
101
- m .ThreadMap [threadTs ] = & Thread {}
102
- }
103
- if msg .IsRootOfThread () {
104
- m .ThreadMap [threadTs ].rootMsg = & msgs [i ]
105
- } else {
106
- if m .ThreadMap [threadTs ].replies == nil {
107
- m .ThreadMap [threadTs ].replies = []Message {msg }
108
- } else {
109
- m .ThreadMap [threadTs ].replies = append (m .ThreadMap [threadTs ].replies , msg )
110
- }
125
+ thread , ok := m .ThreadMap [threadTs ]
126
+ if ! ok {
127
+ thread = & Thread {}
128
+ m .ThreadMap [threadTs ] = thread
111
129
}
130
+ thread .Put (msg )
112
131
}
113
132
}
114
133
@@ -117,22 +136,15 @@ func (m *MessageTable) ReadLogFile(path string) error {
117
136
return err
118
137
}
119
138
if len (visibleMsgs ) != 0 {
120
- if _ , ok := m .MsgsMap [key ]; ! ok {
121
- m .MsgsMap [key ] = visibleMsgs
122
- } else {
123
- m .MsgsMap [key ] = append (m .MsgsMap [key ], visibleMsgs ... )
124
- }
139
+ m .MsgsMap [key ] = append (m .MsgsMap [key ], visibleMsgs ... )
125
140
}
126
141
127
- for key , msgs := range m .MsgsMap {
128
- sort .SliceStable (m .MsgsMap [key ], func (i , j int ) bool {
129
- // must be the same digits, so no need to convert the timestamp to a number
130
- return m.MsgsMap [key ][i ].Ts < m.MsgsMap [key ][j ].Ts
131
- })
142
+ for _ , msgs := range m .MsgsMap {
143
+ msgs .Sort ()
132
144
var lastUser string
133
- for i , msg := range msgs {
145
+ for _ , msg := range msgs {
134
146
if lastUser == msg .User {
135
- ( & msgs [ i ]) .Trail = true
147
+ msg .Trail = true
136
148
} else {
137
149
lastUser = msg .User
138
150
}
0 commit comments