Skip to content

Commit

Permalink
feat: message add expire
Browse files Browse the repository at this point in the history
  • Loading branch information
tangtaoit committed Sep 20, 2023
1 parent d5bbc79 commit 92c2139
Show file tree
Hide file tree
Showing 11 changed files with 55 additions and 16 deletions.
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ module github.com/TangSengDaoDao/TangSengDaoDaoServer
go 1.20

require (
github.com/TangSengDaoDao/TangSengDaoDaoServerLib v0.0.0-20230919085605-00e17e35a35f
github.com/TangSengDaoDao/TangSengDaoDaoServerLib v1.0.6-0.20230920103932-5011d17cb544
github.com/alibabacloud-go/darabonba-openapi v0.2.1
github.com/alibabacloud-go/sms-intl-20180501 v1.0.1
github.com/alibabacloud-go/tea v1.2.1
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,8 @@ github.com/TangSengDaoDao/TangSengDaoDaoServerLib v1.0.6-0.20230825092801-044e34
github.com/TangSengDaoDao/TangSengDaoDaoServerLib v1.0.6-0.20230825092801-044e34e2f62e/go.mod h1:47wmXX6Ny3ir4GNofoZ9zrEryOKve0/wgl4KMZeMnik=
github.com/TangSengDaoDao/TangSengDaoDaoServerLib v1.0.6-0.20230918073947-ba6d7303825e h1:OZHUgLslymN22qNRsIY9hHN2N2fdWwo4yT5DVECWBLY=
github.com/TangSengDaoDao/TangSengDaoDaoServerLib v1.0.6-0.20230918073947-ba6d7303825e/go.mod h1:47wmXX6Ny3ir4GNofoZ9zrEryOKve0/wgl4KMZeMnik=
github.com/TangSengDaoDao/TangSengDaoDaoServerLib v1.0.6-0.20230920103932-5011d17cb544 h1:tlbfSNMFGNfAiB/3ZXJ80HJZjlVGQ/ag7Mo0aqnF6p8=
github.com/TangSengDaoDao/TangSengDaoDaoServerLib v1.0.6-0.20230920103932-5011d17cb544/go.mod h1:47wmXX6Ny3ir4GNofoZ9zrEryOKve0/wgl4KMZeMnik=
github.com/alecthomas/template v0.0.0-20190718012654-fb15b899a751/go.mod h1:LOuyumcjzFXgccqObfd/Ljyb9UuFJ6TxHnclSeseNhc=
github.com/alecthomas/units v0.0.0-20201120081800-1786d5ef83d4/go.mod h1:OMCwj8VM1Kc9e19TLln2VL61YJF0x1XFtfdL4JdbSyE=
github.com/alibabacloud-go/alibabacloud-gateway-spi v0.0.4 h1:iC9YFYKDGEy3n/FtqJnOkZsene9olVspKmkX5A2YBEo=
Expand Down
16 changes: 8 additions & 8 deletions modules/channel/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -95,8 +95,8 @@ func (ch *Channel) channelGet(c *wkhttp.Context) {
ChannelType: channelSettingM.ParentChannelType,
}
}
if channelSettingM.MsgAutoDeleteAt > 0 {
channelResp.Extra["msg_auto_delete_at"] = channelSettingM.MsgAutoDeleteAt
if channelSettingM.MsgAutoDelete > 0 {
channelResp.Extra["msg_auto_delete"] = channelSettingM.MsgAutoDelete
}
}

Expand Down Expand Up @@ -154,14 +154,14 @@ func (ch *Channel) setAutoDeleteForMessage(c *wkhttp.Context) {
channelType := uint8(channelTypeI64)

var req struct {
MsgAutoDeleteAt int64 `json:"msg_auto_delete_at"` // 单位秒
MsgAutoDelete int64 `json:"msg_auto_delete"` // 单位秒
}
if err := c.ShouldBindJSON(&req); err != nil {
c.ResponseError(errors.New("参数错误"))
ch.Error("参数错误", zap.Error(err))
return
}
if req.MsgAutoDeleteAt <= 0 {
if req.MsgAutoDelete <= 0 {
c.ResponseError(errors.New("时间不能小于或等于0"))
return
}
Expand All @@ -183,17 +183,17 @@ func (ch *Channel) setAutoDeleteForMessage(c *wkhttp.Context) {
}
}

if err := ch.channelSettingDB.insertOrAddMsgAutoDeleteAt(fakeChannelID, channelType, req.MsgAutoDeleteAt); err != nil {
if err := ch.channelSettingDB.insertOrAddMsgAutoDelete(fakeChannelID, channelType, req.MsgAutoDelete); err != nil {
c.ResponseError(errors.New("设置失败"))
ch.Error("设置失败", zap.Error(err))
return
}
payload := []byte(util.ToJson(map[string]interface{}{
"content": fmt.Sprintf("{0} 设置消息在 %s 内自动删除", formatSecondToDisplayTime(req.MsgAutoDeleteAt)),
"content": fmt.Sprintf("{0} 设置消息在 %s 内自动删除", formatSecondToDisplayTime(req.MsgAutoDelete)),
"type": common.Tip,
"data": map[string]interface{}{
"msg_auto_delete_at": req.MsgAutoDeleteAt,
"data_type": "autoDeleteForMessage",
"msg_auto_delete": req.MsgAutoDelete,
"data_type": "autoDeleteForMessage",
},
}))

Expand Down
6 changes: 3 additions & 3 deletions modules/channel/db_channel_setting.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,8 @@ func (c *channelSettingDB) queryWithChannelIDs(channelIDs []string) ([]*channelS
return models, err
}

func (c *channelSettingDB) insertOrAddMsgAutoDeleteAt(channelID string, channelType uint8, msgAutoDeleteAt int64) error {
_, err := c.session.InsertBySql("insert into channel_setting (channel_id, channel_type, msg_auto_delete_at) values (?, ?, ?) ON DUPLICATE KEY UPDATE msg_auto_delete_at=VALUES(msg_auto_delete_at)", channelID, channelType, msgAutoDeleteAt).Exec()
func (c *channelSettingDB) insertOrAddMsgAutoDelete(channelID string, channelType uint8, msgAutoDelete int64) error {
_, err := c.session.InsertBySql("insert into channel_setting (channel_id, channel_type, msg_auto_delete) values (?, ?, ?) ON DUPLICATE KEY UPDATE msg_auto_delete=VALUES(msg_auto_delete)", channelID, channelType, msgAutoDelete).Exec()
return err
}

Expand All @@ -40,6 +40,6 @@ type channelSettingModel struct {
ChannelType uint8
ParentChannelID string
ParentChannelType uint8
MsgAutoDeleteAt int64
MsgAutoDelete int64
db.BaseModel
}
4 changes: 3 additions & 1 deletion modules/channel/service.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package channel

import "github.com/TangSengDaoDao/TangSengDaoDaoServerLib/config"
import (
"github.com/TangSengDaoDao/TangSengDaoDaoServerLib/config"
)

type IService interface {
// 获取频道设置集合
Expand Down
2 changes: 1 addition & 1 deletion modules/channel/sql/channel-20230920-01.sql
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
-- +migrate Up

ALTER TABLE `channel_setting` ADD COLUMN msg_auto_delete_at integer not null DEFAULT 0 COMMENT '消息定时删除时间';
ALTER TABLE `channel_setting` ADD COLUMN msg_auto_delete integer not null DEFAULT 0 COMMENT '消息定时删除时间';
2 changes: 1 addition & 1 deletion modules/channel/swagger/api.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ paths:
schema:
type: object
properties:
msg_auto_delete_at:
msg_auto_delete:
type: integer
description: "定时删除时间(单位秒)"
responses:
Expand Down
9 changes: 9 additions & 0 deletions modules/message/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -1464,6 +1464,7 @@ type MsgSyncResp struct {
ToUID string `json:"to_uid,omitempty"` // 接受者uid
ChannelID string `json:"channel_id"` // 频道ID
ChannelType uint8 `json:"channel_type"` // 频道类型
Expire uint32 `json:"expire,omitempty"` // expire
Timestamp int32 `json:"timestamp"` // 服务器消息时间戳(10位,到秒)
Payload map[string]interface{} `json:"payload"` // 消息内容
SignalPayload string `json:"signal_payload"` // signal 加密后的payload base64编码,TODO: 这里为了兼容没加密的版本,所以新用SignalPayload字段
Expand Down Expand Up @@ -1501,6 +1502,7 @@ func (m *MsgSyncResp) from(msgResp *config.MessageResp, loginUID string, message
m.ToUID = msgResp.ToUID
m.ChannelID = msgResp.ChannelID
m.ChannelType = msgResp.ChannelType
m.Expire = msgResp.Expire
m.Timestamp = msgResp.Timestamp
if messageExtraM != nil {

Expand Down Expand Up @@ -1550,6 +1552,13 @@ func (m *MsgSyncResp) from(msgResp *config.MessageResp, loginUID string, message
m.IsDeleted = messageUserExtraM.MessageIsDeleted
m.VoiceStatus = messageUserExtraM.VoiceReaded
}

if msgResp.Expire > 0 {
if time.Now().Unix()-int64(msgResp.Expire) >= int64(msgResp.Timestamp) {
m.IsDeleted = 1
}
}

m.Payload = payloadMap

msgReactionList := make([]*reactionSimpleResp, 0, len(reactionModels))
Expand Down
8 changes: 8 additions & 0 deletions modules/webhook/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -565,6 +565,7 @@ type MsgResp struct {
ToUID string `json:"to_uid"` // 接受者uid
ChannelID string `json:"channel_id"` // 频道ID
ChannelType uint8 `json:"channel_type"` // 频道类型
Expire uint32 `json:"expire"` // 消息过期时间(单位秒)
Timestamp int32 `json:"timestamp"` // 服务器消息时间戳(10位,到秒)
Payload []byte `json:"payload"` // 消息内容
ContentType int // 消息正文类型
Expand All @@ -579,6 +580,10 @@ func (m *MsgResp) toModel() *messageModel {
if setting.Signal {
signal = 1
}
var expireAt uint32 = 0
if m.Expire > 0 {
expireAt = uint32(m.Timestamp) + m.Expire
}
return &messageModel{
MessageID: fmt.Sprintf("%d", m.MessageID),
MessageSeq: int64(m.MessageSeq),
Expand All @@ -589,6 +594,8 @@ func (m *MsgResp) toModel() *messageModel {
FromUID: m.FromUID,
ChannelID: m.ChannelID,
ChannelType: m.ChannelType,
Expire: m.Expire,
ExpireAt: expireAt,
Timestamp: m.Timestamp,
Payload: string(m.Payload),
IsDeleted: 0,
Expand All @@ -609,6 +616,7 @@ func (m *MsgResp) toConfigMessageResp() *config.MessageResp {
ToUID: m.ToUID,
ChannelID: m.ChannelID,
ChannelType: m.ChannelType,
Expire: m.Expire,
Timestamp: m.Timestamp,
Payload: m.Payload,
}
Expand Down
4 changes: 3 additions & 1 deletion modules/webhook/db_message.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ func newMessageDB(ctx *config.Context) *messageDB {

func (m *messageDB) insertOrUpdateTx(model *messageModel, tx *dbr.Tx) error {
tbl := m.getTable(model.ChannelID)
_, err := tx.InsertBySql(fmt.Sprintf("insert into %s(message_id,message_seq,client_msg_no,header,setting,`signal`,from_uid,channel_id,channel_type,timestamp,payload,is_deleted) values(?,?,?,?,?,?,?,?,?,?,?,?) ON DUPLICATE KEY UPDATE payload=payload", tbl), model.MessageID, model.MessageSeq, model.ClientMsgNo, model.Header, model.Setting, model.Signal, model.FromUID, model.ChannelID, model.ChannelType, model.Timestamp, model.Payload, model.IsDeleted).Exec()
_, err := tx.InsertBySql(fmt.Sprintf("insert into %s(message_id,message_seq,client_msg_no,header,setting,`signal`,from_uid,channel_id,channel_type,expire,expire_at,timestamp,payload,is_deleted) values(?,?,?,?,?,?,?,?,?,?,?,?,?,?) ON DUPLICATE KEY UPDATE payload=payload", tbl), model.MessageID, model.MessageSeq, model.ClientMsgNo, model.Header, model.Setting, model.Signal, model.FromUID, model.ChannelID, model.ChannelType, model.Expire, model.ExpireAt, model.Timestamp, model.Payload, model.IsDeleted).Exec()
return err
}

Expand All @@ -47,6 +47,8 @@ type messageModel struct {
FromUID string
ChannelID string
ChannelType uint8
Expire uint32
ExpireAt uint32
Timestamp int32
Payload string
IsDeleted int
Expand Down
16 changes: 16 additions & 0 deletions modules/webhook/sql/webhook-20210226-01.sql
Original file line number Diff line number Diff line change
Expand Up @@ -119,3 +119,19 @@ create table `message4`
);

CREATE UNIQUE INDEX message_id on `message4` (message_id);


ALTER TABLE `message` ADD COLUMN expire integer not null DEFAULT 0 COMMENT '消息过期时长';
ALTER TABLE `message` ADD COLUMN expire_at BIGINT not null DEFAULT 0 COMMENT '消息过期时间';

ALTER TABLE `message1` ADD COLUMN expire integer not null DEFAULT 0 COMMENT '消息过期时长';
ALTER TABLE `message1` ADD COLUMN expire_at BIGINT not null DEFAULT 0 COMMENT '消息过期时间';

ALTER TABLE `message2` ADD COLUMN expire integer not null DEFAULT 0 COMMENT '消息过期时长';
ALTER TABLE `message2` ADD COLUMN expire_at BIGINT not null DEFAULT 0 COMMENT '消息过期时间';

ALTER TABLE `message3` ADD COLUMN expire integer not null DEFAULT 0 COMMENT '消息过期时长';
ALTER TABLE `message3` ADD COLUMN expire_at BIGINT not null DEFAULT 0 COMMENT '消息过期时间';

ALTER TABLE `message4` ADD COLUMN expire integer not null DEFAULT 0 COMMENT '消息过期时长';
ALTER TABLE `message4` ADD COLUMN expire_at BIGINT not null DEFAULT 0 COMMENT '消息过期时间';

0 comments on commit 92c2139

Please sign in to comment.