-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmsg_records.go
86 lines (75 loc) · 2.56 KB
/
msg_records.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
package main
import (
"crypto/sha256"
"encoding/hex"
"time"
)
func (gateway *Gateway) processMsgRecords() {
for {
msg := <-gateway.MsgRecordChan
if msg.ClientID == 0 {
continue
}
err := gateway.InsertMsgQueueItem(msg.MsgQueueItem, msg.ClientID, msg.Carrier, msg.Internal)
if err != nil {
// todo logging
continue
}
}
}
// MsgRecordDBItem represents the structure for storing messages in the database.
type MsgRecordDBItem struct {
ID uint `gorm:"primaryKey" json:"id"`
ClientID uint `gorm:"index;not null" json:"client_id"`
To string `json:"to_number"`
From string `json:"from_number"`
ReceivedTimestamp time.Time `json:"received_timestamp"`
Type string `json:"type"` // "mms" or "sms"
MsgData string `json:"msg_data"` // Partially redacted message
Carrier string `json:"carrier,omitempty"` // Carrier name (optional)
Internal bool `json:"internal"` // Whether the message is internal
LogID string `json:"log_id"`
ServerID string
}
// PartiallyRedactMessage redacts part of the message for privacy.
func PartiallyRedactMessage(message string) string {
if len(message) <= 10 {
return "**********" // Fully redacted for short messages.
}
return message[:5] + "*****" // Partially redact, keep first 5 characters.
}
// InsertMsgQueueItem inserts a MsgQueueItem into the database.
func (gateway *Gateway) InsertMsgQueueItem(item MsgQueueItem, clientID uint, carrier string, internal bool) error {
// Convert MsgQueueItem to MsgRecordDBItem with redacted message.
var msgData string
if item.Type == MsgQueueItemType.MMS {
hash := sha256.Sum256(item.Files[0].Content) // [32]byte array
msgData = hex.EncodeToString(hash[:]) // convert to hex string
} else if item.Type == MsgQueueItemType.SMS {
msgData = PartiallyRedactMessage(item.Message)
}
dbItem := &MsgRecordDBItem{
To: item.To,
From: item.From,
ClientID: clientID,
ReceivedTimestamp: item.ReceivedTimestamp,
//QueuedTimestamp: item.QueuedTimestamp,
Type: string(item.Type),
MsgData: msgData,
Carrier: carrier,
Internal: internal,
LogID: item.LogID,
ServerID: gateway.ServerID,
}
if err := gateway.DB.Create(dbItem).Error; err != nil {
return err
}
// Insert into the database using InsertStruct.
return nil
}
func swapToAndFrom(item MsgQueueItem) MsgQueueItem {
newMsg := item
newMsg.From = item.To
newMsg.To = item.From
return newMsg
}