Skip to content

Commit 6ba60c8

Browse files
committed
move mysql serialization tools to shared pkgs
1 parent 0b7be24 commit 6ba60c8

File tree

4 files changed

+30
-29
lines changed

4 files changed

+30
-29
lines changed

irc/history/serialization.go

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
// Copyright (c) 2020 Shivaram Lingamneni
2+
// released under the MIT license
3+
4+
package history
5+
6+
import (
7+
"encoding/json"
8+
)
9+
10+
// 123 / '{' is the magic number that means JSON;
11+
// if we want to do a binary encoding later, we just have to add different magic version numbers
12+
13+
func MarshalItem(item *Item) (result []byte, err error) {
14+
return json.Marshal(item)
15+
}
16+
17+
func UnmarshalItem(data []byte, result *Item) (err error) {
18+
return json.Unmarshal(data, result)
19+
}

irc/mysql/history.go

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -652,12 +652,12 @@ func (mysql *MySQL) insertCorrespondentsEntry(ctx context.Context, target, corre
652652
}
653653

654654
func (mysql *MySQL) insertBase(ctx context.Context, item history.Item) (id int64, err error) {
655-
value, err := marshalItem(&item)
655+
value, err := history.MarshalItem(&item)
656656
if err != nil {
657657
return 0, fmt.Errorf("could not marshal item: %w", err)
658658
}
659659

660-
msgidBytes, err := decodeMsgid(item.Message.Msgid)
660+
msgidBytes, err := utils.DecodeSecretToken(item.Message.Msgid)
661661
if err != nil {
662662
return 0, fmt.Errorf("could not decode msgid: %w", err)
663663
}
@@ -754,7 +754,7 @@ func (mysql *MySQL) DeleteMsgid(msgid, accountName string) (err error) {
754754

755755
if accountName != "*" {
756756
var item history.Item
757-
err = unmarshalItem(data, &item)
757+
err = history.UnmarshalItem(data, &item)
758758
// delete if the entry is corrupt
759759
if err == nil && item.AccountName != accountName {
760760
return ErrDisallowed
@@ -800,7 +800,7 @@ func (mysql *MySQL) Export(account string, writer io.Writer) {
800800
if err != nil {
801801
return
802802
}
803-
err = unmarshalItem(blob, &item)
803+
err = history.UnmarshalItem(blob, &item)
804804
if err != nil {
805805
return
806806
}
@@ -828,7 +828,7 @@ func (mysql *MySQL) Export(account string, writer io.Writer) {
828828
}
829829

830830
func (mysql *MySQL) lookupMsgid(ctx context.Context, msgid string, includeData bool) (result time.Time, id uint64, data []byte, err error) {
831-
decoded, err := decodeMsgid(msgid)
831+
decoded, err := utils.DecodeSecretToken(msgid)
832832
if err != nil {
833833
return
834834
}
@@ -886,7 +886,7 @@ func (mysql *MySQL) selectItems(ctx context.Context, query string, args ...inter
886886
if err != nil {
887887
return nil, fmt.Errorf("could not scan history item: %w", err)
888888
}
889-
err = unmarshalItem(blob, &item)
889+
err = history.UnmarshalItem(blob, &item)
890890
if err != nil {
891891
return nil, fmt.Errorf("could not unmarshal history item: %w", err)
892892
}

irc/mysql/serialization.go

Lines changed: 0 additions & 23 deletions
This file was deleted.

irc/utils/crypto.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,11 @@ func GenerateSecretToken() string {
4242
return B32Encoder.EncodeToString(buf[:])
4343
}
4444

45+
// return a compact representation of a token generated by GenerateSecretToken()
46+
func DecodeSecretToken(t string) ([]byte, error) {
47+
return B32Encoder.DecodeString(t)
48+
}
49+
4550
// securely check if a supplied token matches a stored token
4651
func SecretTokensMatch(storedToken string, suppliedToken string) bool {
4752
// XXX fix a potential gotcha: if the stored token is uninitialized,

0 commit comments

Comments
 (0)