Skip to content

Commit

Permalink
Merge pull request #82 from traP-jp/add-anonSodan_2
Browse files Browse the repository at this point in the history
Add anon sodan 2
  • Loading branch information
kavos113 authored Sep 2, 2024
2 parents 0d68265 + 72ce2fa commit dc04f0d
Show file tree
Hide file tree
Showing 6 changed files with 138 additions and 3 deletions.
96 changes: 96 additions & 0 deletions src/handler/sodanHandler.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
package handler

import (
"database/sql"
"errors"
"log"
"net/http"
"quickwiki-backend/model"
"strconv"

"github.com/labstack/echo"
)

func (h *Handler) PostMessageToTraQ(c echo.Context) error {
var message model.MessageToTraQ_POST
err := c.Bind(&message)
if err != nil {
return echo.NewHTTPError(http.StatusBadRequest, "bad request body")
}

channelId := "aff37b5f-0911-4255-81c3-b49985c8943f"
err = h.scraper.MessageToTraQ(message.Content, channelId)

if err != nil {
log.Println("post message err : ", err)
return echo.NewHTTPError(http.StatusInternalServerError, "Internal server error.")
}

return c.JSON(http.StatusOK, h.GetSodanHandler(c))
}

func (h *Handler) PatchMessageToTraQ(c echo.Context) error {
wikiId, err := strconv.Atoi(c.QueryParam("wikiId"))
if err != nil {
log.Printf("failed to convert wikiId to int: %v", err)
return c.JSON(http.StatusBadRequest, err)
}

var message model.MessageToTraQ_POST
err = c.Bind(&message)
if err != nil {
return echo.NewHTTPError(http.StatusBadRequest, "bad request body")
}

var messageContents model.SodanContent_fromDB
err = h.db.Select(&messageContents, "select * from messages where wiki_id = ? order by created_at desc limit 1", wikiId)
if err != nil {
if errors.Is(err, sql.ErrNoRows) {
return c.NoContent(http.StatusNotFound)
}
log.Printf("failed to get sodanContent: %s\n", err)
return c.NoContent(http.StatusInternalServerError)
}
messageID := messageContents.MessageID
err = h.scraper.MessageToTraQ(message.Content, messageID)

if err != nil {
log.Println("post message err : ", err)
return echo.NewHTTPError(http.StatusInternalServerError, "Internal server error.")
}

return c.JSON(http.StatusOK, h.GetSodanHandler(c))
}

func (h *Handler) PostRepliesToTraQ(c echo.Context) error {
wikiId, err := strconv.Atoi(c.QueryParam("wikiId"))
if err != nil {
log.Printf("failed to convert wikiId to int: %v", err)
return c.JSON(http.StatusBadRequest, err)
}

var message model.MessageToTraQ_POST
err = c.Bind(&message)
if err != nil {
return echo.NewHTTPError(http.StatusBadRequest, "bad request body")
}

var messageContents model.SodanContent_fromDB
err = h.db.Select(&messageContents, "select * from messages where wiki_id = ? order by created_at desc limit 1", wikiId)
if err != nil {
if errors.Is(err, sql.ErrNoRows) {
return c.NoContent(http.StatusNotFound)
}
log.Printf("failed to get sodanContent: %s\n", err)
return c.NoContent(http.StatusInternalServerError)
}
channelID := messageContents.ChannelID
err = h.scraper.MessageToTraQ(message.Content, channelID)

if err != nil {
log.Println("post message err : ", err)
return echo.NewHTTPError(http.StatusInternalServerError, "Internal server error.")
}

return c.JSON(http.StatusOK, h.GetSodanHandler(c))
}
4 changes: 4 additions & 0 deletions src/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,10 @@ func main() {
e.POST("/wiki/user/favorite", h.PostUserFavoriteWikiHandler)
e.DELETE("/wiki/user/favorite", h.DeleteUserFavoriteWikiHandler)

e.POST("/anon-sodan", h.PostMessageToTraQ)
e.PATCH("/anon-sodan", h.PatchMessageToTraQ)
e.POST("/anon-sodan/replies", h.PostRepliesToTraQ)

e.GET("/setting/index", h.SetIndexingHandler)

e.Logger.Fatal(e.Start(":8080"))
Expand Down
4 changes: 4 additions & 0 deletions src/model/model.go
Original file line number Diff line number Diff line change
Expand Up @@ -187,3 +187,7 @@ type Folder_fromDB struct {
Name string `db:"name"`
ParentID int `db:"parent_id"`
}

type MessageToTraQ_POST struct {
Content string `json:"content"`
}
30 changes: 30 additions & 0 deletions src/scraper/messageBot.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package scraper

import (
"context"

"github.com/traPtitech/go-traq"
)

func (s *Scraper) MessageToTraQ(message string, PostChanellId string) error {
PostChanellId = "01913f8b-8c05-76a2-b51f-bb83e9e93615" //DEV_MODE
_, _, err := s.bot.API().
MessageApi.
PostMessage(context.Background(), PostChanellId).
PostMessageRequest(traq.PostMessageRequest{
Content: message,
}).
Execute()
return err
}

func (s *Scraper) MessageEditOnTraQ(message string, editMessageId string) error {
editMessageId = "0191a3c2-6659-7c4e-a03f-6a37d080cd10" //DEV_MODE
_, err := s.bot.API().MessageApi.
EditMessage(context.Background(), editMessageId).
PostMessageRequest(traq.PostMessageRequest{
Content: message,
}).
Execute()
return err
}
2 changes: 1 addition & 1 deletion src/scraper/messages.go
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ func (s *Scraper) GetSodanMessages() {

s.GetSodanSubMessages("98ea48da-64e8-4f69-9d0d-80690b682670", 40, 52)
log.Println("sodan messages scraped")
s.GetSodanSubMessages("98ea48da-64e8-4f69-9d0d-80690b682670", 40, 52)
s.GetSodanSubMessages("98ea48da-64e8-4f69-9d0d-80690b682670", 48, 52)
log.Println("sodan sub messages 1 scraped")
s.GetSodanSubMessages("30c30aa5-c380-4324-b227-0ca85c34801c", 22, 32)
log.Println("sodan sub messages 2 scraped")
Expand Down
5 changes: 3 additions & 2 deletions src/scraper/scraper.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,13 @@ package scraper

import (
"context"
"log"
"os"

"github.com/jmoiron/sqlx"
"github.com/traPtitech/go-traq"
traqwsbot "github.com/traPtitech/traq-ws-bot"
"github.com/traPtitech/traq-ws-bot/payload"
"log"
"os"
)

type Scraper struct {
Expand Down

0 comments on commit dc04f0d

Please sign in to comment.