Remove unnecessary utility functions

This commit is contained in:
Tulir Asokan 2023-07-15 16:39:11 +03:00
parent c46e47aba8
commit ffd84f5039
5 changed files with 7 additions and 35 deletions

View file

@ -293,7 +293,7 @@ func (c *Client) updateJWK(jwk *crypto.JWK) {
} }
func (c *Client) updateTachyonAuthToken(t []byte) { func (c *Client) updateTachyonAuthToken(t []byte) {
authenticatedAt := util.TimestampNow() authenticatedAt := time.Now().UTC()
c.authData.TachyonAuthToken = t c.authData.TachyonAuthToken = t
c.authData.AuthenticatedAt = &authenticatedAt c.authData.AuthenticatedAt = &authenticatedAt
c.Logger.Debug().Any("authenticatedAt", authenticatedAt).Any("tachyonAuthToken", t).Msg("Updated TachyonAuthToken") c.Logger.Debug().Any("authenticatedAt", authenticatedAt).Any("tachyonAuthToken", t).Msg("Updated TachyonAuthToken")

View file

@ -130,7 +130,7 @@ func (mb *MessageBuilder) AddImage(imgBytes []byte, mime string) *MessageBuilder
func (mb *MessageBuilder) newImageData(imgBytes []byte, mime string) (*Image, error) { func (mb *MessageBuilder) newImageData(imgBytes []byte, mime string) (*Image, error) {
// TODO explode on unsupported types // TODO explode on unsupported types
imgType := ImageTypes[mime] imgType := ImageTypes[mime]
imageId := util.GenerateImageId() imageId := util.GenerateImageID()
imageName := util.RandStr(8) + "." + imgType.Extension imageName := util.RandStr(8) + "." + imgType.Extension
decryptionKey, err := crypto.GenerateKey(32) decryptionKey, err := crypto.GenerateKey(32)
if err != nil { if err != nil {

View file

@ -95,7 +95,7 @@ func (mb *MessageBuilder) Build() (*binary.SendMessagePayload, error) {
} }
if mb.tmpID == "" { if mb.tmpID == "" {
mb.tmpID = util.GenerateTmpId() mb.tmpID = util.GenerateTmpID()
} }
return mb.newSendConversationMessage(), nil return mb.newSendConversationMessage(), nil
@ -106,7 +106,7 @@ func (c *Client) NewMessageBuilder() *MessageBuilder {
client: c, client: c,
} }
tmpId := util.GenerateTmpId() tmpId := util.GenerateTmpID()
mb.SetTmpID(tmpId) mb.SetTmpID(tmpId)
return mb return mb

View file

@ -1,8 +1,6 @@
package util package util
import ( import (
crand "crypto/rand"
"encoding/hex"
"encoding/json" "encoding/json"
"fmt" "fmt"
"math/rand" "math/rand"
@ -17,10 +15,6 @@ import (
var Charset = []rune("abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890") var Charset = []rune("abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890")
func TimestampNow() time.Time {
return time.Now().UTC()
}
func RandStr(length int) string { func RandStr(length int) string {
b := make([]rune, length) b := make([]rune, length)
for i := range b { for i := range b {
@ -29,45 +23,23 @@ func RandStr(length int) string {
return string(b) return string(b)
} }
func GenerateImageId() string { func GenerateImageID() string {
part1 := RandomUUIDv4() part1 := RandomUUIDv4()
part2 := RandStr(25) part2 := RandStr(25)
return part1 + "/" + part2 return part1 + "/" + part2
} }
func GenerateTmpId() string { func GenerateTmpID() string {
src := rand.NewSource(time.Now().UnixNano()) src := rand.NewSource(time.Now().UnixNano())
r := rand.New(src) r := rand.New(src)
randNum := r.Int63n(1e12) randNum := r.Int63n(1e12)
return fmt.Sprintf("tmp_%012d", randNum) return fmt.Sprintf("tmp_%012d", randNum)
} }
func ParseTimestamp(unixTs int64) time.Time {
seconds := unixTs / int64(time.Second/time.Microsecond)
nanoseconds := (unixTs % int64(time.Second/time.Microsecond)) * int64(time.Microsecond/time.Nanosecond)
return time.Unix(seconds, nanoseconds).UTC()
}
func RandomHex(n int) string {
bytes := make([]byte, n)
crand.Read(bytes)
return hex.EncodeToString(bytes)
}
func RandomUUIDv4() string { func RandomUUIDv4() string {
return uuid.New().String() return uuid.New().String()
} }
func RemoveFromSlice(s []string, v string) []string {
newS := []string{}
for _, i := range s {
if i != v {
newS = append(newS, i)
}
}
return newS
}
func BuildRelayHeaders(req *http.Request, contentType string, accept string) { func BuildRelayHeaders(req *http.Request, contentType string, accept string) {
req.Header.Add("host", "instantmessaging-pa.googleapis.com") req.Header.Add("host", "instantmessaging-pa.googleapis.com")
req.Header.Add("connection", "keep-alive") req.Header.Add("connection", "keep-alive")

View file

@ -1220,7 +1220,7 @@ func (portal *Portal) HandleMatrixMessage(sender *User, evt *event.Event, timing
} }
} }
txnID := util.GenerateTmpId() txnID := util.GenerateTmpID()
portal.outgoingMessagesLock.Lock() portal.outgoingMessagesLock.Lock()
portal.outgoingMessages[txnID] = &outgoingMessage{Event: evt} portal.outgoingMessages[txnID] = &outgoingMessage{Event: evt}
portal.outgoingMessagesLock.Unlock() portal.outgoingMessagesLock.Unlock()