CasaOS/service/notify.go

108 lines
2.5 KiB
Go
Raw Normal View History

2021-09-26 02:35:02 +00:00
package service
import (
2022-02-17 10:43:25 +00:00
json2 "encoding/json"
"time"
2021-09-27 06:17:36 +00:00
"github.com/IceWhaleTech/CasaOS/service/model"
"github.com/IceWhaleTech/CasaOS/types"
2022-02-17 10:43:25 +00:00
"github.com/gorilla/websocket"
2021-09-26 02:35:02 +00:00
"gorm.io/gorm"
)
type NotifyServer interface {
GetLog(id string) model.AppNotify
AddLog(log model.AppNotify)
UpdateLog(log model.AppNotify)
2022-02-17 10:43:25 +00:00
UpdateLogByCustomId(log model.AppNotify)
2021-09-26 02:35:02 +00:00
DelLog(id string)
2022-02-17 10:43:25 +00:00
GetList(c int) (list []model.AppNotify)
2021-09-26 02:35:02 +00:00
MarkRead(id string, state int)
}
type notifyServer struct {
db *gorm.DB
}
2022-02-17 10:43:25 +00:00
func (i notifyServer) GetList(c int) (list []model.AppNotify) {
i.db.Where("class = ?", c).Where(i.db.Where("state = ?", types.NOTIFY_DYNAMICE).Or("state = ?", types.NOTIFY_UNREAD)).Find(&list)
2021-09-26 02:35:02 +00:00
return
}
func (i *notifyServer) AddLog(log model.AppNotify) {
i.db.Create(&log)
}
func (i *notifyServer) UpdateLog(log model.AppNotify) {
i.db.Save(&log)
}
2022-02-17 10:43:25 +00:00
func (i *notifyServer) UpdateLogByCustomId(log model.AppNotify) {
if len(log.CustomId) == 0 {
return
}
i.db.Model(&model.AppNotify{}).Select("*").Where("custom_id = ? ", log.CustomId).Updates(log)
}
2021-09-26 02:35:02 +00:00
func (i *notifyServer) GetLog(id string) model.AppNotify {
var log model.AppNotify
i.db.Where("custom_id = ? ", id).First(&log)
return log
}
func (i *notifyServer) MarkRead(id string, state int) {
2022-02-17 10:43:25 +00:00
if id == "0" {
i.db.Model(&model.AppNotify{}).Where("1 = ?", 1).Update("state", state)
return
}
i.db.Model(&model.AppNotify{}).Where("id = ? ", id).Update("state", state)
2021-09-26 02:35:02 +00:00
}
func (i *notifyServer) DelLog(id string) {
var log model.AppNotify
i.db.Where("custom_id = ?", id).Delete(&log)
}
2022-02-17 10:43:25 +00:00
func SendMeg() {
// for {
// mt, message, err := ws.ReadMessage()
// if err != nil {
// break
// }
// notify := model.NotifyMssage{}
// json2.Unmarshal(message, &notify)
// if notify.Type == "read" {
// service.MyService.Notify().MarkRead(notify.Data, types.NOTIFY_READ)
// }
// if notify.Type == "app" {
// go func(ws *websocket.Conn) {
for {
list := MyService.Notify().GetList(types.NOTIFY_APP)
json, _ := json2.Marshal(list)
if len(list) > 0 {
var temp []*websocket.Conn
for _, v := range WebSocketConns {
err := v.WriteMessage(1, json)
if err == nil {
temp = append(temp, v)
}
}
WebSocketConns = temp
for _, v := range list {
MyService.Notify().MarkRead(v.Id, types.NOTIFY_READ)
}
}
if len(WebSocketConns) == 0 {
SocketRun = false
}
time.Sleep(time.Second * 2)
}
// }(ws)
// }
// }
}
2021-09-26 02:35:02 +00:00
func NewNotifyService(db *gorm.DB) NotifyServer {
return &notifyServer{db: db}
}