123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404 |
- package controller
- import (
- "encoding/json"
- "errors"
- "fmt"
- "github.com/gin-gonic/gin"
- "message-pusher/channel"
- "message-pusher/common"
- "message-pusher/model"
- "net/http"
- "strconv"
- "time"
- )
- func keepCompatible(message *model.Message) {
- // Keep compatible with ServerChan: https://sct.ftqq.com/sendkey
- if message.Description == "" {
- message.Description = message.Short
- }
- if message.Content == "" {
- message.Content = message.Desp
- }
- if message.To == "" {
- message.To = message.OpenId
- }
- }
- func GetPushMessage(c *gin.Context) {
- message := model.Message{
- Title: c.Query("title"),
- Description: c.Query("description"),
- Content: c.Query("content"),
- URL: c.Query("url"),
- Channel: c.Query("channel"),
- Token: c.Query("token"),
- To: c.Query("to"),
- Desp: c.Query("desp"),
- Short: c.Query("short"),
- OpenId: c.Query("openid"),
- Async: c.Query("async") == "true",
- }
- keepCompatible(&message)
- pushMessageHelper(c, &message)
- }
- func PostPushMessage(c *gin.Context) {
- message := model.Message{
- Title: c.PostForm("title"),
- Description: c.PostForm("description"),
- Content: c.PostForm("content"),
- URL: c.PostForm("url"),
- Channel: c.PostForm("channel"),
- Token: c.PostForm("token"),
- To: c.PostForm("to"),
- Desp: c.PostForm("desp"),
- Short: c.PostForm("short"),
- OpenId: c.PostForm("openid"),
- Async: c.PostForm("async") == "true",
- }
- if message == (model.Message{}) {
- // Looks like the user is using JSON
- err := json.NewDecoder(c.Request.Body).Decode(&message)
- if err != nil {
- c.JSON(http.StatusOK, gin.H{
- "success": false,
- "message": "无法解析请求体,请检查其是否为合法 JSON",
- })
- return
- }
- }
- keepCompatible(&message)
- pushMessageHelper(c, &message)
- }
- func pushMessageHelper(c *gin.Context, message *model.Message) {
- user := model.User{Username: c.Param("username")}
- err := user.FillUserByUsername()
- if err != nil {
- c.JSON(http.StatusOK, gin.H{
- "success": false,
- "message": err.Error(),
- })
- return
- }
- if user.Status == common.UserStatusNonExisted {
- c.JSON(http.StatusOK, gin.H{
- "success": false,
- "message": "用户不存在",
- })
- return
- }
- if user.Status == common.UserStatusDisabled {
- c.JSON(http.StatusOK, gin.H{
- "success": false,
- "message": "用户已被封禁",
- })
- return
- }
- if user.Token != "" && user.Token != " " {
- if message.Token == "" {
- message.Token = c.Request.Header.Get("Authorization")
- if message.Token == "" {
- c.JSON(http.StatusOK, gin.H{
- "success": false,
- "message": "token 为空",
- })
- return
- }
- }
- if user.Token != message.Token {
- c.JSON(http.StatusOK, gin.H{
- "success": false,
- "message": "无效的 token",
- })
- return
- }
- }
- processMessage(c, message, &user)
- }
- func processMessage(c *gin.Context, message *model.Message, user *model.User) {
- if message.Title == "" {
- message.Title = common.SystemName
- }
- if message.Channel == "" {
- message.Channel = user.Channel
- if message.Channel == "" {
- message.Channel = model.TypeEmail
- }
- }
- channel_, err := model.GetChannelByName(message.Channel, user.Id)
- if err != nil {
- c.JSON(http.StatusOK, gin.H{
- "success": false,
- "message": "无效的渠道名称:" + message.Channel,
- })
- return
- }
- err = saveAndSendMessage(user, message, channel_)
- if err != nil {
- c.JSON(http.StatusOK, gin.H{
- "success": false,
- "message": err.Error(),
- })
- return
- }
- c.JSON(http.StatusOK, gin.H{
- "success": true,
- "message": "",
- "uuid": message.Link,
- })
- }
- func saveAndSendMessage(user *model.User, message *model.Message, channel_ *model.Channel) error {
- if channel_.Status != common.ChannelStatusEnabled {
- return errors.New("该渠道已被禁用")
- }
- common.MessageCount += 1 // We don't need to use atomic here because it's not a critical value
- message.Link = common.GetUUID()
- if message.URL == "" {
- message.URL = fmt.Sprintf("%s/message/%s", common.ServerAddress, message.Link)
- }
- success := false
- if common.MessagePersistenceEnabled || user.SaveMessageToDatabase == common.SaveMessageToDatabaseAllowed {
- defer func() {
- // Update the status of the message
- status := common.MessageSendStatusFailed
- if message.Async {
- status = common.MessageSendStatusAsyncPending
- } else {
- if success {
- status = common.MessageSendStatusSent
- }
- }
- err := message.UpdateStatus(status)
- if err != nil {
- common.SysError("failed to update the status of the message: " + err.Error())
- }
- if message.Async {
- channel.AsyncMessageQueue <- message.Id
- }
- }()
- err := message.UpdateAndInsert(user.Id)
- if err != nil {
- return err
- }
- go syncMessageToUser(message, user.Id)
- } else {
- if message.Async {
- return errors.New("异步发送消息需要用户具备消息持久化的权限")
- }
- message.Link = "unsaved" // This is for user to identify whether the message is saved
- go syncMessageToUser(message, user.Id)
- }
- if !message.Async {
- err := channel.SendMessage(message, user, channel_)
- if err != nil {
- return err
- }
- }
- success = true
- return nil // After this line, the message status will be updated
- }
- func GetStaticFile(c *gin.Context) {
- path := c.Param("file")
- c.FileFromFS("public/static/"+path, http.FS(common.FS))
- }
- func RenderMessage(c *gin.Context) {
- if !common.MessageRenderEnabled {
- c.HTML(http.StatusOK, "message.html", gin.H{
- "title": "无法渲染",
- "time": time.Now().Format("2006-01-02 15:04:05"),
- "description": "超级管理员禁用了消息渲染",
- "content": "很抱歉,您所使用的消息推送服务的管理员禁用了消息渲染功能,因此您的消息无法渲染。",
- })
- return
- }
- link := c.Param("link")
- if link == "unsaved" {
- c.HTML(http.StatusOK, "message.html", gin.H{
- "title": "无法渲染",
- "time": time.Now().Format("2006-01-02 15:04:05"),
- "description": "超级管理员禁用了消息持久化",
- "content": "很抱歉,您所使用的消息推送服务的管理员禁用了消息持久化功能,您的消息并没有存储到数据库中,因此无法渲染。",
- })
- return
- }
- message, err := model.GetMessageByLink(link)
- if err != nil {
- c.Status(http.StatusNotFound)
- return
- }
- if message.Description != "" {
- message.Description, err = common.Markdown2HTML(message.Description)
- if err != nil {
- common.SysLog(err.Error())
- }
- }
- if message.Content != "" {
- message.HTMLContent, err = common.Markdown2HTML(message.Content)
- if err != nil {
- common.SysLog(err.Error())
- }
- }
- c.HTML(http.StatusOK, "message.html", gin.H{
- "title": message.Title,
- "time": time.Unix(message.Timestamp, 0).Format("2006-01-02 15:04:05"),
- "description": message.Description,
- "content": message.HTMLContent,
- })
- return
- }
- func GetUserMessages(c *gin.Context) {
- userId := c.GetInt("id")
- p, _ := strconv.Atoi(c.Query("p"))
- if p < 0 {
- p = 0
- }
- messages, err := model.GetMessagesByUserId(userId, p*common.ItemsPerPage, common.ItemsPerPage)
- if err != nil {
- c.JSON(http.StatusOK, gin.H{
- "success": false,
- "message": err.Error(),
- })
- return
- }
- c.JSON(http.StatusOK, gin.H{
- "success": true,
- "message": "",
- "data": messages,
- })
- return
- }
- func GetMessage(c *gin.Context) {
- messageId, _ := strconv.Atoi(c.Param("id"))
- userId := c.GetInt("id")
- message, err := model.GetMessageByIds(messageId, userId)
- if err != nil {
- c.JSON(http.StatusOK, gin.H{
- "success": false,
- "message": err.Error(),
- })
- return
- }
- c.JSON(http.StatusOK, gin.H{
- "success": true,
- "message": "",
- "data": message,
- })
- return
- }
- func GetMessageStatus(c *gin.Context) {
- link := c.Param("link")
- status, err := model.GetMessageStatusByLink(link)
- if err != nil {
- c.JSON(http.StatusOK, gin.H{
- "success": false,
- "message": err.Error(),
- })
- return
- }
- c.JSON(http.StatusOK, gin.H{
- "success": true,
- "message": "",
- "status": status,
- })
- return
- }
- func SearchMessages(c *gin.Context) {
- keyword := c.Query("keyword")
- messages, err := model.SearchMessages(keyword)
- if err != nil {
- c.JSON(http.StatusOK, gin.H{
- "success": false,
- "message": err.Error(),
- })
- return
- }
- c.JSON(http.StatusOK, gin.H{
- "success": true,
- "message": "",
- "data": messages,
- })
- return
- }
- func ResendMessage(c *gin.Context) {
- messageId, _ := strconv.Atoi(c.Param("id"))
- userId := c.GetInt("id")
- helper := func() error {
- message, err := model.GetMessageByIds(messageId, userId)
- message.Id = 0
- if err != nil {
- return err
- }
- user, err := model.GetUserById(userId, true)
- if err != nil {
- return err
- }
- channel_, err := model.GetChannelByName(message.Channel, user.Id)
- if err != nil {
- return err
- }
- err = saveAndSendMessage(user, message, channel_)
- if err != nil {
- return err
- }
- return nil
- }
- err := helper()
- if err != nil {
- c.JSON(http.StatusOK, gin.H{
- "success": false,
- "message": err.Error(),
- })
- return
- }
- c.JSON(http.StatusOK, gin.H{
- "success": true,
- "message": "",
- })
- return
- }
- func DeleteMessage(c *gin.Context) {
- messageId, _ := strconv.Atoi(c.Param("id"))
- userId := c.GetInt("id")
- err := model.DeleteMessageById(messageId, userId)
- if err != nil {
- c.JSON(http.StatusOK, gin.H{
- "success": false,
- "message": err.Error(),
- })
- return
- }
- c.JSON(http.StatusOK, gin.H{
- "success": true,
- "message": "",
- })
- return
- }
- func DeleteAllMessages(c *gin.Context) {
- err := model.DeleteAllMessages()
- if err != nil {
- c.JSON(http.StatusOK, gin.H{
- "success": false,
- "message": err.Error(),
- })
- return
- }
- c.JSON(http.StatusOK, gin.H{
- "success": true,
- "message": "",
- })
- return
- }
|