Skip to content

Fix messages #1

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 7 commits into from
Oct 5, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 17 additions & 5 deletions apis/message/apis.go
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
package message

import (
"github.com/gofiber/fiber/v2"
. "notification/models"
"notification/push"
. "notification/utils"

"github.com/gofiber/fiber/v2"
)

// ListMessages
Expand All @@ -14,14 +15,15 @@ import (
// @Router /messages [get]
// @Success 200 {array} Message
func ListMessages(c *fiber.Ctx) error {
var messages []Message
var messages Messages
DB.Raw(`
SELECT * FROM message
INNER JOIN message_user ON message.id = message_user.message_id
WHERE message_user.user_id = ?`,
WHERE message_user.user_id = ?
ORDER BY updated_at DESC`,
c.Locals("userID").(int),
).Scan(&messages)
return c.JSON(messages)
return Serialize(c, &messages)
}

// SendMessage
Expand Down Expand Up @@ -61,7 +63,7 @@ func SendMessage(c *fiber.Ctx) error {

go push.Send(message)

return c.Status(201).JSON(message)
return Serialize(c.Status(201), &message)
}

// ClearMessages
Expand All @@ -81,6 +83,16 @@ func ClearMessages(c *fiber.Ctx) error {
return c.Status(204).JSON(nil)
}

// ClearMessagesDeprecated
// @Summary Clear Messages Deprecated
// @Tags Message
// @Produce application/json
// @Router /messages [put]
// @Success 204
func ClearMessagesDeprecated(c *fiber.Ctx) error {
return ClearMessages(c)
}

// DeleteMessage
// @Summary Delete a message of a user
// @Tags Message
Expand Down
1 change: 1 addition & 0 deletions apis/message/routes.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,6 @@ func RegisterRoutes(app fiber.Router) {
app.Get("/messages", ListMessages)
app.Post("/messages", SendMessage)
app.Post("/messages/clear", ClearMessages)
app.Put("/messages", ClearMessagesDeprecated)
app.Delete("/messages/:id", DeleteMessage)
}
4 changes: 1 addition & 3 deletions app/middlewares.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package app

import (
"notification/config"
"notification/utils"
"strconv"

"github.com/gofiber/fiber/v2"
Expand Down Expand Up @@ -30,9 +29,8 @@ func getUserID(c *fiber.Ctx) error {
if err != nil {
if config.Config.Debug {
userID = 1
} else {
return utils.Unauthorized("Unauthorized")
}
// do not return error if user is not logged in
}

c.Locals("userID", userID)
Expand Down
20 changes: 20 additions & 0 deletions models/message.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
package models

import (
"github.com/gofiber/fiber/v2"
"gorm.io/gorm"
)

type Messages []Message

type Message struct {
BaseModel
Title string `json:"message" gorm:"size:32;not null"`
Expand All @@ -12,6 +15,8 @@ type Message struct {
Type MessageType `json:"code" gorm:"size:16;not null"`
URL string `json:"url" gorm:"size:64;default:'';not null"`
Recipients []int `json:"-" gorm:"-:all" `
MessageID int `json:"message_id" gorm:"-:all"` // 兼容旧版 id
HasRead bool `json:"has_read" gorm:"default:false"` // 兼容旧版
}

type MessageUser struct {
Expand All @@ -31,6 +36,21 @@ const (
MessageTypeReportDealt MessageType = "report_dealt"
)

func (messages Messages) Preprocess(c *fiber.Ctx) error {
for i := 0; i < len(messages); i++ {
err := messages[i].Preprocess(c)
if err != nil {
return err
}
}
return nil
}

func (message *Message) Preprocess(c *fiber.Ctx) error {
message.MessageID = message.ID
return nil
}

func (m *Message) AfterCreate(tx *gorm.DB) (err error) {
mapping := make([]MessageUser, len(m.Recipients))
for i, userID := range m.Recipients {
Expand Down
12 changes: 11 additions & 1 deletion push/apns/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"fmt"
"github.com/sideshow/apns2"
"notification/config"
. "notification/models"
"notification/push/base"
"notification/utils"
"strings"
Expand All @@ -20,7 +21,7 @@ func (s *Sender) Send() bool {
res, err := client.Push(&apns2.Notification{
DeviceToken: token,
Topic: config.Config.IOSPackageName,
Payload: s.Message.Data,
Payload: constructPayload(s.Message),
})
if err != nil {
utils.Logger.Error("APNS push error: " + err.Error())
Expand All @@ -42,3 +43,12 @@ func (s *Sender) Send() bool {

return success
}

func constructPayload(message *Message) any {
return Map{"aps": Map{"alert": Map{
"title": message.Title,
"subtitle": message.Description,
"body": message.Data,
}}}

}
17 changes: 17 additions & 0 deletions push/manual.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package push

import (
. "notification/models"
)

func manualSend(service PushService, title string, description string, data Map, tokens []string) {
m := Message{
Title: title,
Description: description,
Data: data,
}
sender := factory.CreateSender(service)
sender.New(&m, tokens)
sender.Send()
sender.Clear()
}
6 changes: 6 additions & 0 deletions push/push_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,3 +49,9 @@ func TestPushNotification(t *testing.T) {
})
assert.True(t, success)
}

//func TestManualSend(t *testing.T) {
// tokens := []string{}
// message := ""
// manualSend(ServiceAPNS, message, tokens)
//}
11 changes: 10 additions & 1 deletion utils/utils.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
package utils

import (
"github.com/gofiber/fiber/v2"
"os"
"path"

"github.com/gofiber/fiber/v2"
)

type CanPreprocess interface {
Expand Down Expand Up @@ -42,3 +43,11 @@ func ToAbsolutePath(relativePath string) string {
}
return path.Join(basePath, relativePath)
}

func Serialize(c *fiber.Ctx, obj CanPreprocess) error {
err := obj.Preprocess(c)
if err != nil {
return err
}
return c.JSON(obj)
}