mirror of
https://github.com/ergochat/ergo.git
synced 2026-01-02 08:17:57 +01:00
move mysql serialization tools to shared pkgs
This commit is contained in:
parent
0b7be24f80
commit
6ba60c89c4
19
irc/history/serialization.go
Normal file
19
irc/history/serialization.go
Normal file
@ -0,0 +1,19 @@
|
||||
// Copyright (c) 2020 Shivaram Lingamneni
|
||||
// released under the MIT license
|
||||
|
||||
package history
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
)
|
||||
|
||||
// 123 / '{' is the magic number that means JSON;
|
||||
// if we want to do a binary encoding later, we just have to add different magic version numbers
|
||||
|
||||
func MarshalItem(item *Item) (result []byte, err error) {
|
||||
return json.Marshal(item)
|
||||
}
|
||||
|
||||
func UnmarshalItem(data []byte, result *Item) (err error) {
|
||||
return json.Unmarshal(data, result)
|
||||
}
|
||||
@ -652,12 +652,12 @@ func (mysql *MySQL) insertCorrespondentsEntry(ctx context.Context, target, corre
|
||||
}
|
||||
|
||||
func (mysql *MySQL) insertBase(ctx context.Context, item history.Item) (id int64, err error) {
|
||||
value, err := marshalItem(&item)
|
||||
value, err := history.MarshalItem(&item)
|
||||
if err != nil {
|
||||
return 0, fmt.Errorf("could not marshal item: %w", err)
|
||||
}
|
||||
|
||||
msgidBytes, err := decodeMsgid(item.Message.Msgid)
|
||||
msgidBytes, err := utils.DecodeSecretToken(item.Message.Msgid)
|
||||
if err != nil {
|
||||
return 0, fmt.Errorf("could not decode msgid: %w", err)
|
||||
}
|
||||
@ -754,7 +754,7 @@ func (mysql *MySQL) DeleteMsgid(msgid, accountName string) (err error) {
|
||||
|
||||
if accountName != "*" {
|
||||
var item history.Item
|
||||
err = unmarshalItem(data, &item)
|
||||
err = history.UnmarshalItem(data, &item)
|
||||
// delete if the entry is corrupt
|
||||
if err == nil && item.AccountName != accountName {
|
||||
return ErrDisallowed
|
||||
@ -800,7 +800,7 @@ func (mysql *MySQL) Export(account string, writer io.Writer) {
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
err = unmarshalItem(blob, &item)
|
||||
err = history.UnmarshalItem(blob, &item)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
@ -828,7 +828,7 @@ func (mysql *MySQL) Export(account string, writer io.Writer) {
|
||||
}
|
||||
|
||||
func (mysql *MySQL) lookupMsgid(ctx context.Context, msgid string, includeData bool) (result time.Time, id uint64, data []byte, err error) {
|
||||
decoded, err := decodeMsgid(msgid)
|
||||
decoded, err := utils.DecodeSecretToken(msgid)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
@ -886,7 +886,7 @@ func (mysql *MySQL) selectItems(ctx context.Context, query string, args ...inter
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("could not scan history item: %w", err)
|
||||
}
|
||||
err = unmarshalItem(blob, &item)
|
||||
err = history.UnmarshalItem(blob, &item)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("could not unmarshal history item: %w", err)
|
||||
}
|
||||
|
||||
@ -1,23 +0,0 @@
|
||||
package mysql
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
|
||||
"github.com/ergochat/ergo/irc/history"
|
||||
"github.com/ergochat/ergo/irc/utils"
|
||||
)
|
||||
|
||||
// 123 / '{' is the magic number that means JSON;
|
||||
// if we want to do a binary encoding later, we just have to add different magic version numbers
|
||||
|
||||
func marshalItem(item *history.Item) (result []byte, err error) {
|
||||
return json.Marshal(item)
|
||||
}
|
||||
|
||||
func unmarshalItem(data []byte, result *history.Item) (err error) {
|
||||
return json.Unmarshal(data, result)
|
||||
}
|
||||
|
||||
func decodeMsgid(msgid string) ([]byte, error) {
|
||||
return utils.B32Encoder.DecodeString(msgid)
|
||||
}
|
||||
@ -42,6 +42,11 @@ func GenerateSecretToken() string {
|
||||
return B32Encoder.EncodeToString(buf[:])
|
||||
}
|
||||
|
||||
// return a compact representation of a token generated by GenerateSecretToken()
|
||||
func DecodeSecretToken(t string) ([]byte, error) {
|
||||
return B32Encoder.DecodeString(t)
|
||||
}
|
||||
|
||||
// securely check if a supplied token matches a stored token
|
||||
func SecretTokensMatch(storedToken string, suppliedToken string) bool {
|
||||
// XXX fix a potential gotcha: if the stored token is uninitialized,
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user