Skip to content

Commit

Permalink
add all of sodanDraft-function
Browse files Browse the repository at this point in the history
  • Loading branch information
blancnoir256 committed Sep 8, 2024
1 parent 52aa015 commit a1597f9
Show file tree
Hide file tree
Showing 5 changed files with 175 additions and 54 deletions.
21 changes: 20 additions & 1 deletion docs/openapi.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -337,6 +337,9 @@ paths:
schema:
type: object
properties:
isDraft:
type: boolean
example: false
content:
type: string
example: "This is example content."
Expand All @@ -346,7 +349,20 @@ paths:
content:
application/json:
schema:
$ref: '#/components/schemas/sodan'
type: object
properties:
isDraft:
type: boolean
example: false
messageTraqId:
type: string
example: "88888888-2b2b-xxxx-1111-aaaaaaaaaaaa"
userTraqId:
type: string
example: kavos
content:
type: string
example: "This is a example content."
patch:
tags:
- sodan
Expand All @@ -367,6 +383,9 @@ paths:
schema:
type: object
properties:
isDraft:
type: boolean
example: false
content:
type: string
example: "This is edited content."
Expand Down
5 changes: 5 additions & 0 deletions schema.sql
Original file line number Diff line number Diff line change
Expand Up @@ -69,4 +69,9 @@ CREATE TABLE anonSodans (
user_traq_id CHAR(36) NOT NULL,
FOREIGN KEY (wiki_id) REFERENCES wikis(id)
);
CREATE TABLE anonSodanDrafts (
id INT(11) NOT NULL PRIMARY KEY AUTO_INCREMENT,
content TEXT NOT NULL,
user_traq_id CHAR(36) NOT NULL,
);
INSERT INTO folders (name, parent_id) VALUES ('root', 0);
177 changes: 127 additions & 50 deletions src/handler/sodanHandler.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import (
"github.com/traPtitech/go-traq"
)

func (h *Handler) PostMessageToTraQ(c echo.Context) error {
func (h *Handler) PostAnonSodanToTraQ(c echo.Context) error {
var message model.MessageToTraQ_POST
err := c.Bind(&message)
if err != nil {
Expand All @@ -25,30 +25,55 @@ func (h *Handler) PostMessageToTraQ(c echo.Context) error {
return echo.NewHTTPError(http.StatusUnauthorized, "failed to get user traQID")
}

var resp *traq.Message
channelId := "aff37b5f-0911-4255-81c3-b49985c8943f" //random/sodan
resp, err = h.scraper.MessageToTraQ(message.Content, channelId)
if err != nil {
log.Printf("MessageToTraQ err : %s", err)
return echo.NewHTTPError(http.StatusInternalServerError, "For some reason we could not post the message to traQ")
if message.isDraft == nil {
message.isDraft = false
}

messageId := resp.Id
_, err = h.db.Exec("INSERT INTO anonSodans (message_traq_id,user_traq_id) VALUES (?,?)", messageId, owner.TraqID)
if err != nil {
log.Printf("DB Error: %s", err)
return echo.NewHTTPError(http.StatusInternalServerError, "internal server error")
}
if message.IsDraft {
result, err := h.db.Exec("INSERT INTO anonSodanDrafts (content,user_traq_id) VALUES (?,?)", message.Content, owner.Name)
if err != nil {
log.Println("failed to insert anonSodanDrafts : ", err)
return echo.NewHTTPError(http.StatusInternalServerError, "failed to insert anonSodanDrafts")
}
id, err := result.LastInsertId()
if err != nil {
log.Println("failed to get anonSodanDrafts id : ", err)
return echo.NewHTTPError(http.StatusInternalServerError, "failed to get anonSodanDrafts id")
}
var resp model.AnonSodanDraftRespons
resp.ID = id
resp.UserTraqID = owner.TraqID
resp.Content = message.Content
return c.JSON(http.StatusOK, resp)
} else {
var postResp *traq.Message
channelId := "aff37b5f-0911-4255-81c3-b49985c8943f" //random/sodan
postResp, err = h.scraper.MessageToTraQ(message.Content, channelId)
if err != nil {
log.Printf("MessageToTraQ err : %s", err)
return echo.NewHTTPError(http.StatusInternalServerError, "For some reason we could not post the message to traQ")
}

if err != nil {
log.Println("post message err : ", err)
return echo.NewHTTPError(http.StatusInternalServerError, "Internal server error.")
}
messageId := postResp.Id
_, err = h.db.Exec("INSERT INTO anonSodans (message_traq_id,user_traq_id) VALUES (?,?)", messageId, owner.TraqID)
if err != nil {
log.Printf("DB Error: %s", err)
return echo.NewHTTPError(http.StatusInternalServerError, "internal server error")
}

return c.JSON(http.StatusOK, "ok")
if err != nil {
log.Println("post message err : ", err)
return echo.NewHTTPError(http.StatusInternalServerError, "Internal server error.")
}
var resp model.AnonSodanRespons
resp.MessageTraqId = messageId
resp.UserTraqID = owner.TraqID
resp.Content = message.Content
return c.JSON(http.StatusOK, resp)
}
}

func (h *Handler) PatchMessageToTraQ(c echo.Context) error {
func (h *Handler) PatchAnonSodanToTraQ(c echo.Context) error {
wikiId, err := strconv.Atoi(c.QueryParam("wikiId"))
if err != nil {
log.Printf("failed to convert wikiId to int: %v", err)
Expand All @@ -61,45 +86,54 @@ func (h *Handler) PatchMessageToTraQ(c echo.Context) error {
return echo.NewHTTPError(http.StatusBadRequest, "bad request body")
}

var messageContents model.SodanContent_fromDB
err = h.db.Select(&messageContents, "select * from anonSodans where wiki_id = ? order by created_at asc 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
var anonSodan model.AnonSodans_fromDB
err = h.db.Select(&anonSodan, "select * from anonSodans 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 anonSodanContent: %s\n", err)
return c.NoContent(http.StatusInternalServerError)
}

owner, err := h.GetUserInfo(c)
if err != nil {
log.Println("failed to get user info : ", err)
return echo.NewHTTPError(http.StatusUnauthorized, "failed to get user traQID")
}
if owner.TraqID != anonSodan.UserTraqID {
return echo.NewHTTPError(http.StatusForbidden, "This is not your MESSAGE!") //これでいいのか?
}

err = h.scraper.MessageEditOnTraQ(message.Content, messageID)
if err != nil {
log.Println("post message err : ", err)
return echo.NewHTTPError(http.StatusInternalServerError, "Internal server error.")
if message.isDraft == nil {
message.isDraft = false
}

return c.JSON(http.StatusOK, h.GetSodanHandler(c))
if message.isDraft {
_, err := h.db.Exec("UPDATE anonSodanDrafts SET content = ? , user_traq_id = ? WHERE id = ?", message.Content, owner.Name, wikiId)
if err != nil {
log.Println("failed to insert anonSodanDrafts : ", err)
return echo.NewHTTPError(http.StatusInternalServerError, "failed to insert anonSodanDrafts")
}
var resp model.AnonSodanDraftRespons
resp.ID = wikiId
resp.UserTraqID = owner.TraqID
resp.Content = message.Content
return c.JSON(http.StatusOK, resp)
} else {
var anonSodan model.AnonSodans_fromDB
err = h.db.GET(&anonSodan, "select * from anonSodans where wiki_id = ? order by created_at asc limit 1", wikiId)
if err != nil {
if errors.Is(err, sql.ErrNoRows) {
return c.NoContent(http.StatusNotFound)
}
log.Printf("failed to get anonSodanContent: %s\n", err)
return c.NoContent(http.StatusInternalServerError)
}
messageID := anonSodan.messageTraqId

if owner.TraqID != anonSodan.UserTraqID {
return echo.NewHTTPError(http.StatusForbidden, "This is not your MESSAGE!")
}

err = h.scraper.MessageEditOnTraQ(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 {
func (h *Handler) PostAnonSodanRepliesToTraQ(c echo.Context) error {
wikiId, err := strconv.Atoi(c.QueryParam("wikiId"))
if err != nil {
log.Printf("failed to convert wikiId to int: %v", err)
Expand All @@ -123,14 +157,14 @@ func (h *Handler) PostRepliesToTraQ(c echo.Context) error {
}
channelID := messageContents.ChannelID

resp, err := h.scraper.MessageToTraQ(message.Content, channelID)
postResp, err := h.scraper.MessageToTraQ(message.Content, channelID)

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

messageId := resp.Id
messageId := postResp.Id
owner, err := h.GetUserInfo(c)
if err != nil {
log.Println("failed to get user info : ", err)
Expand All @@ -145,3 +179,46 @@ func (h *Handler) PostRepliesToTraQ(c echo.Context) error {

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

func (h *Handler) GetAnonSodanDraftHandler(c echo.Context) error {
id := c.QueryParam("messageDraftId")
owner, err := h.GetUserInfo(c)
if err != nil {
log.Println("failed to get user info : ", err)
return echo.NewHTTPError(http.StatusUnauthorized, "failed to get user traQID")
}
if id == "" {
var anonSodanDrafts []model.AnonSodanDraft_fromDB
err := h.db.Select(&anonSodanDrafts, "select * from anonSodanDrafts where user_traq_id = ? ", owner.Name)
if err != nil {
log.Println("failed to get anonSodanDrafts : err ", err)
return echo.NewHTTPError(http.StatusInternalServerError, "failed to get anonSodan drafts")
}
var resp []model.AnonSodanDraftRespons
for _, anonSodanDraft := range anonSodanDrafts {
var tmp model.anoanonSodanDraftRespons
tmp.ID = anonSodanDraft.ID
tmp.Content = anonSodanDraft.Content
tmp.UserTraqID = anonSodanDraft.UserTraqID
resp = append(resp, tmp)
}
return echo.NewHTTPError(http.StatusOK, resp)
} else {
id, err = strconv.Atoi(id)
if err != nil {
log.Printf("failed to convert wikiId to int: %v", err)
return c.JSON(http.StatusBadRequest, err)
}
var anonSodanDraftById model.AnonSodanDraft_fromDB
err = h.db.Select(&anonSodanDraftById, "select * from anonSodanDrafts where id = ? ", id)
if err != nil {
log.Println("failed to get anonSodanDrafts : err ", err)
return echo.NewHTTPError(http.StatusInternalServerError, "failed to get anonSodan drafts")
}
var resp []model.AnonSodanDraftRespons
resp.ID = anonSodanDraftById.ID
resp.Content = anonSodanDraftById.Content
resp.UserTraqID = anonSodanDraftById.UserTraqID
return echo.NewHTTPError(http.StatusOK, resp)
}
}
7 changes: 4 additions & 3 deletions src/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -69,9 +69,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("/anonn-sodan", h.GetAnonSodanDraftHandler)
e.POST("/anon-sodan", h.PostAnonSodanToTraQ)
e.PATCH("/anon-sodan", h.PatchAnonSodanToTraQ)
e.POST("/anon-sodan/replies", h.PostAnonSodanRepliesToTraQ)

e.GET("/files/:fileId", h.GetFileHandler)
e.GET("/stamps/:stampId", h.GetStampHandler)
Expand Down
19 changes: 19 additions & 0 deletions src/model/model.go
Original file line number Diff line number Diff line change
Expand Up @@ -199,6 +199,7 @@ type Folder_fromDB struct {
}

type MessageToTraQ_POST struct {
IsDraft bool `json:"isDraft"`
Content string `json:"content"`
}

Expand All @@ -207,3 +208,21 @@ type AnonSodans_fromDB struct {
MessageTraqID string `db:"message_traq_id"`
UserTraqID string `db:"user_traq_id"`
}

type AnonSodanRespons struct {
MessageTraqID string `json:"messageTraqId"`
UserTraqID string `json:"userTraqId"`
Content string `json:"content"`
}

type AnonSodanDraftRespons struct {
ID string `json:"messageDraftId"`
UserTraqID string `json:"userTraqId"`
Content string `json:"content"`
}

type AnonSodanDraft_fromDB struct {
ID string `db:"id"`
UserTraqID string `db:"user_traq_id"`
Content string `db:"content"`
}

0 comments on commit a1597f9

Please sign in to comment.