2023-01-06 00:39:21 +01:00
|
|
|
package primary
|
2022-11-19 23:16:54 +01:00
|
|
|
|
|
|
|
import (
|
2023-07-27 17:44:06 +02:00
|
|
|
"blackforestbytes.com/simplecloudnotifier/db"
|
2023-02-03 22:51:03 +01:00
|
|
|
ct "blackforestbytes.com/simplecloudnotifier/db/cursortoken"
|
2022-11-19 23:16:54 +01:00
|
|
|
"blackforestbytes.com/simplecloudnotifier/models"
|
|
|
|
"database/sql"
|
2023-06-18 02:09:05 +02:00
|
|
|
"errors"
|
2022-12-07 23:32:58 +01:00
|
|
|
"gogs.mikescher.com/BlackForestBytes/goext/sq"
|
2022-11-19 23:16:54 +01:00
|
|
|
"time"
|
|
|
|
)
|
|
|
|
|
2023-07-27 17:44:06 +02:00
|
|
|
func (db *Database) GetMessageByUserMessageID(ctx db.TxContext, usrMsgId string) (*models.Message, error) {
|
2022-11-19 23:16:54 +01:00
|
|
|
tx, err := ctx.GetOrCreateTransaction(db)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2022-12-07 22:11:44 +01:00
|
|
|
rows, err := tx.Query(ctx, "SELECT * FROM messages WHERE usr_message_id = :umid LIMIT 1", sq.PP{"umid": usrMsgId})
|
2022-11-19 23:16:54 +01:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
msg, err := models.DecodeMessage(rows)
|
2023-07-30 15:58:37 +02:00
|
|
|
if errors.Is(err, sql.ErrNoRows) {
|
2022-11-19 23:16:54 +01:00
|
|
|
return nil, nil
|
|
|
|
}
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return &msg, nil
|
|
|
|
}
|
|
|
|
|
2023-07-27 17:44:06 +02:00
|
|
|
func (db *Database) GetMessage(ctx db.TxContext, scnMessageID models.MessageID, allowDeleted bool) (models.Message, error) {
|
2022-11-19 23:16:54 +01:00
|
|
|
tx, err := ctx.GetOrCreateTransaction(db)
|
|
|
|
if err != nil {
|
|
|
|
return models.Message{}, err
|
|
|
|
}
|
|
|
|
|
2022-12-14 12:29:55 +01:00
|
|
|
var sqlcmd string
|
|
|
|
if allowDeleted {
|
2023-01-14 00:48:51 +01:00
|
|
|
sqlcmd = "SELECT * FROM messages WHERE message_id = :mid LIMIT 1"
|
2022-12-14 12:29:55 +01:00
|
|
|
} else {
|
2023-01-14 00:48:51 +01:00
|
|
|
sqlcmd = "SELECT * FROM messages WHERE message_id = :mid AND deleted=0 LIMIT 1"
|
2022-12-14 12:29:55 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
rows, err := tx.Query(ctx, sqlcmd, sq.PP{"mid": scnMessageID})
|
2022-11-19 23:16:54 +01:00
|
|
|
if err != nil {
|
|
|
|
return models.Message{}, err
|
|
|
|
}
|
|
|
|
|
|
|
|
msg, err := models.DecodeMessage(rows)
|
|
|
|
if err != nil {
|
|
|
|
return models.Message{}, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return msg, nil
|
|
|
|
}
|
|
|
|
|
2023-07-27 17:44:06 +02:00
|
|
|
func (db *Database) CreateMessage(ctx db.TxContext, senderUserID models.UserID, channel models.Channel, timestampSend *time.Time, title string, content *string, priority int, userMsgId *string, senderIP string, senderName *string, usedKeyID models.KeyTokenID) (models.Message, error) {
|
2022-11-19 23:16:54 +01:00
|
|
|
tx, err := ctx.GetOrCreateTransaction(db)
|
|
|
|
if err != nil {
|
|
|
|
return models.Message{}, err
|
|
|
|
}
|
|
|
|
|
2023-05-28 22:27:38 +02:00
|
|
|
entity := models.MessageDB{
|
|
|
|
MessageID: models.NewMessageID(),
|
2022-12-22 11:22:36 +01:00
|
|
|
SenderUserID: senderUserID,
|
|
|
|
ChannelInternalName: channel.InternalName,
|
|
|
|
ChannelID: channel.ChannelID,
|
|
|
|
SenderIP: senderIP,
|
|
|
|
SenderName: senderName,
|
2023-05-28 22:27:38 +02:00
|
|
|
TimestampReal: time2DB(time.Now()),
|
|
|
|
TimestampClient: time2DBOpt(timestampSend),
|
2022-12-22 11:22:36 +01:00
|
|
|
Title: title,
|
|
|
|
Content: content,
|
|
|
|
Priority: priority,
|
|
|
|
UserMessageID: userMsgId,
|
2023-05-27 18:51:20 +02:00
|
|
|
UsedKeyID: usedKeyID,
|
2023-05-28 22:27:38 +02:00
|
|
|
Deleted: bool2DB(false),
|
|
|
|
}
|
|
|
|
|
|
|
|
_, err = sq.InsertSingle(ctx, tx, "messages", entity)
|
|
|
|
if err != nil {
|
|
|
|
return models.Message{}, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return entity.Model(), nil
|
2022-11-19 23:16:54 +01:00
|
|
|
}
|
|
|
|
|
2023-07-27 17:44:06 +02:00
|
|
|
func (db *Database) DeleteMessage(ctx db.TxContext, messageID models.MessageID) error {
|
2022-11-19 23:16:54 +01:00
|
|
|
tx, err := ctx.GetOrCreateTransaction(db)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2023-01-14 00:48:51 +01:00
|
|
|
_, err = tx.Exec(ctx, "UPDATE messages SET deleted=1 WHERE message_id = :mid AND deleted=0", sq.PP{"mid": messageID})
|
2022-11-19 23:16:54 +01:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
2022-11-20 00:19:41 +01:00
|
|
|
|
2023-07-27 17:44:06 +02:00
|
|
|
func (db *Database) ListMessages(ctx db.TxContext, filter models.MessageFilter, pageSize *int, inTok ct.CursorToken) ([]models.Message, ct.CursorToken, error) {
|
2022-11-20 00:19:41 +01:00
|
|
|
tx, err := ctx.GetOrCreateTransaction(db)
|
|
|
|
if err != nil {
|
2023-02-03 22:51:03 +01:00
|
|
|
return nil, ct.CursorToken{}, err
|
2022-11-20 00:19:41 +01:00
|
|
|
}
|
|
|
|
|
2023-02-03 22:51:03 +01:00
|
|
|
if inTok.Mode == ct.CTMEnd {
|
|
|
|
return make([]models.Message, 0), ct.End(), nil
|
2022-11-20 00:19:41 +01:00
|
|
|
}
|
|
|
|
|
2022-12-11 02:47:23 +01:00
|
|
|
pageCond := "1=1"
|
2023-02-03 22:51:03 +01:00
|
|
|
if inTok.Mode == ct.CTMNormal {
|
2023-01-14 00:48:51 +01:00
|
|
|
pageCond = "timestamp_real < :tokts OR (timestamp_real = :tokts AND message_id < :tokid )"
|
2022-11-20 00:19:41 +01:00
|
|
|
}
|
|
|
|
|
2022-12-10 03:38:48 +01:00
|
|
|
filterCond, filterJoin, prepParams, err := filter.SQL()
|
2022-11-20 00:19:41 +01:00
|
|
|
|
2023-05-27 23:54:14 +02:00
|
|
|
orderClause := ""
|
|
|
|
if pageSize != nil {
|
|
|
|
orderClause = "ORDER BY COALESCE(timestamp_client, timestamp_real) DESC, message_id DESC LIMIT :lim"
|
|
|
|
prepParams["lim"] = *pageSize + 1
|
|
|
|
} else {
|
|
|
|
orderClause = "ORDER BY COALESCE(timestamp_client, timestamp_real) DESC, message_id DESC"
|
|
|
|
}
|
2022-11-20 00:19:41 +01:00
|
|
|
|
2022-12-11 02:47:23 +01:00
|
|
|
sqlQuery := "SELECT " + "messages.*" + " FROM messages " + filterJoin + " WHERE ( " + pageCond + " ) AND ( " + filterCond + " ) " + orderClause
|
2022-11-20 00:30:30 +01:00
|
|
|
|
2022-12-10 03:38:48 +01:00
|
|
|
prepParams["tokts"] = inTok.Timestamp
|
|
|
|
prepParams["tokid"] = inTok.Id
|
2022-11-20 00:30:30 +01:00
|
|
|
|
2022-12-10 03:38:48 +01:00
|
|
|
rows, err := tx.Query(ctx, sqlQuery, prepParams)
|
2022-11-20 00:30:30 +01:00
|
|
|
if err != nil {
|
2023-02-03 22:51:03 +01:00
|
|
|
return nil, ct.CursorToken{}, err
|
2022-11-20 00:30:30 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
data, err := models.DecodeMessages(rows)
|
|
|
|
if err != nil {
|
2023-02-03 22:51:03 +01:00
|
|
|
return nil, ct.CursorToken{}, err
|
2022-11-20 00:30:30 +01:00
|
|
|
}
|
|
|
|
|
2023-05-27 23:54:14 +02:00
|
|
|
if pageSize == nil || len(data) <= *pageSize {
|
2023-02-03 22:51:03 +01:00
|
|
|
return data, ct.End(), nil
|
2022-11-20 00:30:30 +01:00
|
|
|
} else {
|
2023-05-27 23:54:14 +02:00
|
|
|
outToken := ct.Normal(data[*pageSize-1].Timestamp(), data[*pageSize-1].MessageID.String(), "DESC", filter.Hash())
|
|
|
|
return data[0:*pageSize], outToken, nil
|
2022-11-20 00:30:30 +01:00
|
|
|
}
|
|
|
|
}
|
2023-06-18 02:09:05 +02:00
|
|
|
|
2023-07-27 17:44:06 +02:00
|
|
|
func (db *Database) CountMessages(ctx db.TxContext, filter models.MessageFilter) (int64, error) {
|
2023-06-18 02:09:05 +02:00
|
|
|
tx, err := ctx.GetOrCreateTransaction(db)
|
|
|
|
if err != nil {
|
|
|
|
return 0, err
|
|
|
|
}
|
|
|
|
|
|
|
|
filterCond, filterJoin, prepParams, err := filter.SQL()
|
|
|
|
|
|
|
|
sqlQuery := "SELECT " + "COUNT(*)" + " FROM messages " + filterJoin + " WHERE ( " + filterCond + " ) "
|
|
|
|
|
|
|
|
rows, err := tx.Query(ctx, sqlQuery, prepParams)
|
|
|
|
if err != nil {
|
|
|
|
return 0, err
|
|
|
|
}
|
|
|
|
|
|
|
|
if !rows.Next() {
|
|
|
|
return 0, errors.New("COUNT query returned no results")
|
|
|
|
}
|
|
|
|
|
|
|
|
var countRes int64
|
|
|
|
err = rows.Scan(&countRes)
|
|
|
|
if err != nil {
|
|
|
|
return 0, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return countRes, nil
|
|
|
|
}
|