-
Notifications
You must be signed in to change notification settings - Fork 1
/
message_finder.go
162 lines (128 loc) · 3.83 KB
/
message_finder.go
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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
package main
import (
"fmt"
"strings"
"time"
"github.com/bwmarrin/discordgo"
"github.com/kmc-jp/DiscordSlackSynchronizer/discord_webhook"
"github.com/kmc-jp/DiscordSlackSynchronizer/slack_webhook"
"github.com/pkg/errors"
)
type MessageFinder struct {
slackHook *slack_webhook.Handler
discordHook *discord_webhook.Handler
escaper MessageEscaper
}
type MetaData struct {
OriginalMessage string
TS string
SlackUserID string
DiscordUserID string
}
func NewMessageFinder(slackHook *slack_webhook.Handler, discordHook *discord_webhook.Handler) *MessageFinder {
return &MessageFinder{
discordHook: discordHook,
slackHook: slackHook,
}
}
func (h *MessageFinder) SetMessageEscaper(escaper MessageEscaper) {
h.escaper = escaper
}
func (h MessageFinder) ParseMetaData(text string) *MetaData {
var meta MetaData
var sepMessage = strings.Split(text, "<"+SlackMessageDummyURI)
var uriQuery = strings.Split(sepMessage[len(sepMessage)-1], "|")[0]
meta.TS = strings.Split(uriQuery, "&")[0]
if len(sepMessage) == 1 {
return nil
}
meta.OriginalMessage = strings.Join(sepMessage[:len(sepMessage)-1], "<"+SlackMessageDummyURI)
if !strings.Contains(sepMessage[1], "&slack_user_id=") {
return &meta
}
uriQuery = strings.Split(uriQuery, "&slack_user_id=")[1]
meta.SlackUserID = strings.Split(uriQuery, "&")[0]
if !strings.Contains(uriQuery, "&discord_user_id=") {
return &meta
}
meta.DiscordUserID = strings.Split(uriQuery, "&discord_user_id=")[0]
return &meta
}
func (h MessageFinder) CreateURIformat(timestamp, slackUserID, discordUserID string) string {
return fmt.Sprintf("<%s%s&slack_user_id=%s&discord_user_id=%s|%s>", SlackMessageDummyURI, timestamp, slackUserID, discordUserID, "ㅤ")
}
func (h MessageFinder) FindFromSlackMessage(srcContent *slack_webhook.Message, discordChannel string) (*discordgo.Message, *MetaData, error) {
var message discordgo.Message
var meta = h.ParseMetaData(srcContent.Text)
if meta.TS != "" {
messages, err := h.discordHook.GetMessages(discordChannel, "")
if err != nil {
return nil, nil, err
}
srcT, err := time.Parse(time.RFC3339, meta.TS)
if err != nil {
goto next
}
for i, msg := range messages {
if i == 0 {
continue
}
if msg.Timestamp.UnixMilli() < srcT.UnixMilli() {
message = messages[i-1]
break
}
}
}
next:
// if message not found, find by its message text
if message.ID == "" {
messages, err := h.discordHook.GetMessages(discordChannel, "")
if err != nil {
return nil, nil, err
}
content, err := h.escaper.EscapeMessage(srcContent.Text)
if err != nil {
return nil, nil, err
}
for i, msg := range messages {
if content == msg.Content {
message = messages[i-1]
break
}
}
}
// not found
if message.ID == "" {
return nil, nil, fmt.Errorf("MessageNotFound")
}
return &message, meta, nil
}
func (h MessageFinder) FindFromDiscordMessage(message discordgo.Message, slackChannel string) (*slack_webhook.Message, *MetaData, error) {
srcMessages, err := h.slackHook.GetMessages(slackChannel, "", 100)
if err != nil {
return nil, nil, errors.Wrap(err, "GetSlackMessages")
}
var srcMessage slack_webhook.Message
var check bool
dTime := message.Timestamp
var meta *MetaData
for i, msg := range srcMessages {
if strings.Contains(msg.Text, "<"+SlackMessageDummyURI) {
meta = h.ParseMetaData(msg.Text)
var messageTS = meta.TS
srcT, err := time.Parse(time.RFC3339, messageTS)
if err != nil {
continue
}
if dTime.UnixMilli() >= srcT.UnixMilli() {
srcMessage = srcMessages[i]
check = true
break
}
}
}
if !check {
return nil, nil, errors.New("NotFound")
}
return &srcMessage, meta, nil
}