Skip to content

Commit 4f6e153

Browse files
authored
Merge pull request #38 from vim-jp/message-by-pointer
treat message by pointer
2 parents 4b47208 + 2dd5a02 commit 4f6e153

File tree

4 files changed

+62
-46
lines changed

4 files changed

+62
-46
lines changed

scripts/lib/generator.go

+3-8
Original file line numberDiff line numberDiff line change
@@ -122,14 +122,9 @@ func (g *HTMLGenerator) generateChannelDir(path string, channel Channel) (bool,
122122
return false, fmt.Errorf("could not create %s directory: %w", path, err)
123123
}
124124

125-
keys := make([]MessageMonthKey, 0, len(msgsMap))
126-
for key := range msgsMap {
127-
keys = append(keys, key)
128-
}
129-
130125
if err := g.generateChannelIndex(
131126
channel,
132-
keys,
127+
msgsMap.Keys(),
133128
filepath.Join(path, "index.html"),
134129
); err != nil {
135130
return true, err
@@ -178,7 +173,7 @@ func (g *HTMLGenerator) generateChannelIndex(channel Channel, keys []MessageMont
178173
return nil
179174
}
180175

181-
func (g *HTMLGenerator) generateMessageDir(channel Channel, key MessageMonthKey, msgs []Message, path string) error {
176+
func (g *HTMLGenerator) generateMessageDir(channel Channel, key MessageMonthKey, msgs Messages, path string) error {
182177
if err := os.MkdirAll(path, 0777); err != nil {
183178
return fmt.Errorf("could not create %s directory: %w", path, err)
184179
}
@@ -233,7 +228,7 @@ func (g *HTMLGenerator) generateMessageDir(channel Channel, key MessageMonthKey,
233228
}
234229
return ""
235230
},
236-
"threads": func(ts string) []Message {
231+
"threads": func(ts string) Messages {
237232
if t, ok := g.s.GetThread(channel.ID, ts); ok {
238233
return t.Replies()
239234
}

scripts/lib/message.go

+40-28
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,29 @@ import (
1111
"strings"
1212
)
1313

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+
1437
// MessageTable : メッセージデータを保持する
1538
// スレッドは投稿時刻からどのスレッドへの返信かが判断できるためThreadMapのキー
1639
// はtsである。
@@ -20,7 +43,7 @@ import (
2043
type MessageTable struct {
2144
// key: thread timestamp
2245
ThreadMap map[string]*Thread
23-
MsgsMap map[MessageMonthKey][]Message
46+
MsgsMap MessagesMap
2447
// key: file path
2548
loadedFiles map[string]struct{}
2649
}
@@ -32,7 +55,7 @@ type MessageTable struct {
3255
func NewMessageTable() *MessageTable {
3356
return &MessageTable{
3457
ThreadMap: map[string]*Thread{},
35-
MsgsMap: map[MessageMonthKey][]Message{},
58+
MsgsMap: MessagesMap{},
3659
loadedFiles: map[string]struct{}{},
3760
}
3861
}
@@ -79,13 +102,15 @@ func (m *MessageTable) ReadLogFile(path string) error {
79102
return nil
80103
}
81104

82-
var msgs []Message
83-
var visibleMsgs []Message
105+
var msgs Messages
84106
err = ReadFileAsJSON(path, &msgs)
85107
if err != nil {
86108
return fmt.Errorf("failed to unmarshal %s: %w", path, err)
87109
}
88-
for i, msg := range msgs {
110+
111+
// assort messages, visible and threaded.
112+
var visibleMsgs Messages
113+
for _, msg := range msgs {
89114
if !msg.IsVisible() {
90115
continue
91116
}
@@ -97,18 +122,12 @@ func (m *MessageTable) ReadLogFile(path string) error {
97122
visibleMsgs = append(visibleMsgs, msg)
98123
}
99124
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
111129
}
130+
thread.Put(msg)
112131
}
113132
}
114133

@@ -117,22 +136,15 @@ func (m *MessageTable) ReadLogFile(path string) error {
117136
return err
118137
}
119138
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...)
125140
}
126141

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()
132144
var lastUser string
133-
for i, msg := range msgs {
145+
for _, msg := range msgs {
134146
if lastUser == msg.User {
135-
(&msgs[i]).Trail = true
147+
msg.Trail = true
136148
} else {
137149
lastUser = msg.User
138150
}

scripts/lib/store.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ func (s *LogStore) HasPrevMonth(channelID string, key MessageMonthKey) bool {
7373
return false
7474
}
7575

76-
func (s *LogStore) GetMessagesPerMonth(channelID string) (map[MessageMonthKey][]Message, error) {
76+
func (s *LogStore) GetMessagesPerMonth(channelID string) (MessagesMap, error) {
7777
mt, ok := s.mts[channelID]
7878
if !ok {
7979
return nil, fmt.Errorf("not found channel: id=%s", channelID)

scripts/lib/thread.go

+18-9
Original file line numberDiff line numberDiff line change
@@ -9,21 +9,30 @@ import (
99
// repliesにはそのスレッドへの返信メッセージが入る。先頭メッセージは含まない。
1010
type Thread struct {
1111
rootMsg *Message
12-
replies []Message
12+
replies Messages
1313
}
1414

15-
func (t Thread) LastReplyTime() time.Time {
16-
return TsToDateTime(t.replies[len(t.replies)-1].Ts)
15+
func (th Thread) LastReplyTime() time.Time {
16+
return TsToDateTime(th.replies[len(th.replies)-1].Ts)
1717
}
1818

19-
func (t Thread) ReplyCount() int {
20-
return len(t.replies)
19+
func (th Thread) ReplyCount() int {
20+
return len(th.replies)
2121
}
2222

23-
func (t Thread) RootText() string {
24-
return t.rootMsg.Text
23+
func (th Thread) RootText() string {
24+
return th.rootMsg.Text
2525
}
2626

27-
func (t Thread) Replies() []Message {
28-
return t.replies
27+
func (th Thread) Replies() Messages {
28+
return th.replies
29+
}
30+
31+
// Put puts a message to the thread as "root" or "reply".
32+
func (th *Thread) Put(m *Message) {
33+
if m.IsRootOfThread() {
34+
th.rootMsg = m
35+
} else {
36+
th.replies = append(th.replies, m)
37+
}
2938
}

0 commit comments

Comments
 (0)