Skip to content

Commit

Permalink
Merge pull request #117 from traP-jp/fix/registerwiki
Browse files Browse the repository at this point in the history
fix/registerwiki
  • Loading branch information
kavos113 authored Sep 4, 2024
2 parents aefc31d + b98e5b0 commit fd54457
Show file tree
Hide file tree
Showing 2 changed files with 85 additions and 6 deletions.
44 changes: 44 additions & 0 deletions src/scraper/messages.go
Original file line number Diff line number Diff line change
Expand Up @@ -204,6 +204,50 @@ func (s *Scraper) AddMessageToDB(m traq.Message, wikiId int) {
}
}

func (s *Scraper) UpdateMessageToDB(m traq.Message, wikiId int) {
newMessage := model.SodanContent_fromDB{
WikiID: wikiId,
MessageContent: m.Content,
CreatedAt: m.CreatedAt,
UpdatedAt: m.UpdatedAt,
UserTraqID: s.usersMap[m.UserId].Name,
ChannelID: m.ChannelId,
MessageID: m.Id,
}
_, err := s.db.Exec("UPDATE messages SET content = ?, updated_at = ? WHERE message_traq_id = ?",
newMessage.MessageContent, newMessage.UpdatedAt, newMessage.MessageID)
if err != nil {
log.Println("failed to update message")
log.Printf("%+v\nerr:%+v\n", newMessage.ID, err)
}
err = s.db.Get(&newMessage.ID, "SELECT id FROM messages WHERE message_traq_id = ?", newMessage.MessageID)
if err != nil {
log.Println("failed to get message id")
log.Println(err)
}

stampCount := make(map[string]int)
for _, s := range m.Stamps {
if _, ok := stampCount[s.StampId]; !ok {
stampCount[s.StampId] = 0
}
stampCount[s.StampId] += int(s.Count)
}

for stampId, count := range stampCount {
newStamp := model.Stamp_fromDB{
MessageID: newMessage.ID,
StampTraqID: stampId,
StampCount: count,
}
_, err := s.db.Exec("INSERT INTO messageStamps (message_id, stamp_traq_id, count) VALUES (?, ?, ?)",
newStamp.MessageID, newStamp.StampTraqID, newStamp.StampCount)
if err != nil {
log.Println(err)
}
}
}

func (s *Scraper) MergeWikisContent() {
var wikis []model.WikiContent_fromDB
err := s.db.Select(&wikis, "SELECT * FROM wikis WHERE type = 'sodan'")
Expand Down
47 changes: 41 additions & 6 deletions src/scraper/newMessages.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,6 @@ func (s *Scraper) SodanMessageCreated(p *payload.MessageCreated) {
}

s.AddMessageToDB(newSodan, int(wikiId))
s.addMessageTag(int(wikiId))
s.addMessageIndex(int(wikiId))
}

func (s *Scraper) SodanSubMessageCreated(p *payload.MessageCreated) {
Expand All @@ -52,6 +50,7 @@ func (s *Scraper) SodanSubMessageCreated(p *payload.MessageCreated) {
log.Println(err)
}
if citedMessage.ChannelId == rsodanChannelId {
s.registerWiki(p.Message.ChannelID)
wikiId = s.GetWikiIDByMessageId(citedMessageId)
}
}
Expand All @@ -69,9 +68,6 @@ func (s *Scraper) SodanSubMessageCreated(p *payload.MessageCreated) {
UpdatedAt: p.Message.UpdatedAt,
Stamps: []traq.MessageStamp{},
}, wikiId)
s.addMessageTag(wikiId)
s.removeMentionSingle(wikiId)
s.addMessageIndex(wikiId)
}

func (s *Scraper) getWikiId(channelId string) int {
Expand All @@ -80,7 +76,7 @@ func (s *Scraper) getWikiId(channelId string) int {
rsodanChannelId := "aff37b5f-0911-4255-81c3-b49985c8943f"

var messages []model.SodanContent_fromDB
err := s.db.Select(&messages, "SELECT * FROM messages WHERE channel_id = ? ORDER BY created_at DESC LIMIT 30", channelId)
err := s.db.Select(&messages, "SELECT * FROM messages WHERE channel_id = ? ORDER BY created_at DESC LIMIT 100", channelId)
if err != nil {
log.Println("failed to get messages")
log.Println(err)
Expand Down Expand Up @@ -110,12 +106,35 @@ func (s *Scraper) getWikiId(channelId string) int {
func (s *Scraper) registerWiki(channelId string) {
wikiId := s.getWikiId(channelId)

s.updateMessages(wikiId)
s.mergeWikiContent(wikiId)
s.addMessageTag(wikiId)
s.removeMentionSingle(wikiId)
s.addMessageIndex(wikiId)
}

func (s *Scraper) updateMessages(wikiId int) {
var messages []model.SodanContent_fromDB
err := s.db.Select(&messages, "SELECT * FROM messages WHERE wiki_id = ? ORDER BY created_at DESC", wikiId)
if err != nil {
log.Println("failed to get messages")
log.Println(err)
}

firstCreated := messages[len(messages)-1].CreatedAt
lastCreated := messages[0].CreatedAt

newMessages, r, err := s.bot.API().MessageApi.GetMessages(context.Background(), messages[0].ChannelID).Limit(100).Since(firstCreated).Until(lastCreated).Execute()
if err != nil {
log.Printf("failed to get messages: %+v, %+v", r, err)
log.Println(err)
}

for _, m := range newMessages {
s.UpdateMessageToDB(m, wikiId)
}
}

func (s *Scraper) mergeWikiContent(wikiId int) {
var wiki model.WikiContent_fromDB
err := s.db.Get(&wiki, "SELECT * FROM wikis WHERE id = ?", wikiId)
Expand Down Expand Up @@ -189,4 +208,20 @@ func (s *Scraper) removeMentionSingle(wikiId int) {
log.Println("failed to update wiki")
log.Println(err)
}

var messages []model.SodanContent_fromDB
err = s.db.Select(&messages, "SELECT * FROM messages WHERE wiki_id = ?", wikiId)
if err != nil {
log.Println("failed to get messages")
log.Println(err)
}

for _, m := range messages {
text := ProcessMention(m.MessageContent)
_, err = s.db.Exec("UPDATE messages SET message_content = ? WHERE id = ?", text, m.ID)
if err != nil {
log.Println("failed to update message")
log.Println(err)
}
}
}

0 comments on commit fd54457

Please sign in to comment.