Internalize id.ContentURI Valuer

Removes dependency on custom mautrix-go
This commit is contained in:
Karmanyaah Malhotra 2021-03-03 15:51:08 -05:00
parent 2fe729bee0
commit d8bff50005
9 changed files with 41 additions and 11 deletions

3
.gitignore vendored
View File

@ -8,4 +8,5 @@
*.db
*.log
/mautrix-whatsapp
/matrix-groupme-go
.profile

View File

@ -122,7 +122,7 @@ type Portal struct {
Name string
Topic string
Avatar string
AvatarURL id.ContentURI
AvatarURL types.ContentURI
Encrypted bool `gorm:"notNull;default:false"`
}

View File

@ -101,7 +101,7 @@ type Puppet struct {
JID types.GroupMeID `gorm:"primaryKey"`
Avatar string
AvatarURL id.ContentURI
AvatarURL types.ContentURI
Displayname string
NameQuality int8

2
go.mod
View File

@ -29,5 +29,3 @@ require (
)
replace github.com/karmanyaahm/groupme => ../groupme
replace maunium.net/go/mautrix => ../mautrix

2
go.sum
View File

@ -644,5 +644,7 @@ maunium.net/go/mauflag v1.0.0 h1:YiaRc0tEI3toYtJMRIfjP+jklH45uDHtT80nUamyD4M=
maunium.net/go/mauflag v1.0.0/go.mod h1:nLivPOpTpHnpzEh8jEdSL9UqO9+/KBJFmNRlwKfkPeA=
maunium.net/go/maulogger/v2 v2.1.1 h1:NAZNc6XUFJzgzfewCzVoGkxNAsblLCSSEdtDuIjP0XA=
maunium.net/go/maulogger/v2 v2.1.1/go.mod h1:TYWy7wKwz/tIXTpsx8G3mZseIRiC5DoMxSZazOHy68A=
maunium.net/go/mautrix v0.8.2 h1:E3NudQ/QolmE/yhHau8iCkbmcq6gCLvoEvukdqPFJu4=
maunium.net/go/mautrix v0.8.2/go.mod h1:KiViCshKBUZwrVRvTOXsJBFfstvR/btxckHUbOPdu54=
sigs.k8s.io/yaml v1.1.0/go.mod h1:UJmg0vDUVViEyp3mgSv9WPwZCDxu4rQW1olrI1uml+o=
sourcegraph.com/sourcegraph/appdash v0.0.0-20190731080439-ebfcffb1b5c0/go.mod h1:hI742Nqp5OhwiqlzhgfbWU4mW4yO10fP+LoT9WOswdU=

View File

@ -171,7 +171,7 @@ func (mx *MatrixHandler) createPrivatePortalFromInvite(roomID id.RoomID, inviter
print("possible bug with pointer above")
portal.Avatar = puppet.Avatar
_, _ = portal.MainIntent().SetRoomName(portal.MXID, portal.Name)
_, _ = portal.MainIntent().SetRoomAvatar(portal.MXID, portal.AvatarURL)
_, _ = portal.MainIntent().SetRoomAvatar(portal.MXID, portal.AvatarURL.ContentURI)
} else {
portal.Name = ""
}

View File

@ -1001,7 +1001,7 @@ func (portal *Portal) CreateMatrixRoom(user *User) error {
initialState = append(initialState, &event.Event{
Type: event.StateRoomAvatar,
Content: event.Content{
Parsed: event.RoomAvatarEventContent{URL: portal.AvatarURL},
Parsed: event.RoomAvatarEventContent{URL: portal.AvatarURL.ContentURI},
},
})
}

View File

@ -188,7 +188,7 @@ func (puppet *Puppet) UpdateAvatar(source *User, avatar string) bool {
if err != nil {
puppet.log.Warnln("Failed to remove avatar:", err)
}
puppet.AvatarURL = id.ContentURI{}
puppet.AvatarURL = types.ContentURI{}
puppet.Avatar = avatar
go puppet.updatePortalAvatar()
return true
@ -222,8 +222,8 @@ func (puppet *Puppet) UpdateAvatar(source *User, avatar string) bool {
return false
}
puppet.AvatarURL = resp.ContentURI
err = puppet.DefaultIntent().SetAvatarURL(puppet.AvatarURL)
puppet.AvatarURL = types.ContentURI{resp.ContentURI}
err = puppet.DefaultIntent().SetAvatarURL(resp.ContentURI)
if err != nil {
puppet.log.Warnln("Failed to set avatar:", err)
}
@ -260,7 +260,7 @@ func (puppet *Puppet) updatePortalMeta(meta func(portal *Portal)) {
func (puppet *Puppet) updatePortalAvatar() {
puppet.updatePortalMeta(func(portal *Portal) {
if len(portal.MXID) > 0 {
_, err := portal.MainIntent().SetRoomAvatar(portal.MXID, puppet.AvatarURL)
_, err := portal.MainIntent().SetRoomAvatar(portal.MXID, puppet.AvatarURL.ContentURI)
if err != nil {
portal.log.Warnln("Failed to set avatar:", err)
}

29
types/contentURI.go Normal file
View File

@ -0,0 +1,29 @@
package types
import (
"database/sql/driver"
"maunium.net/go/mautrix/id"
)
type ContentURI struct {
id.ContentURI
}
func (m *ContentURI) Scan(value interface{}) error {
bytes, ok := value.([]byte)
if !ok {
println(len(bytes))
//return errors.New(fmt.Sprint("Failed to unmarshal value:", value))
}
if len(bytes) == 0 {
uri, _ := id.ParseContentURI("")
*m = ContentURI{uri}
return nil
}
return m.UnmarshalText(bytes)
}
func (m ContentURI) Value() (driver.Value, error) {
return m.String(), nil
}