From 07d2cae5b277a89fbf771e4eeca0ebcdc42b2de2 Mon Sep 17 00:00:00 2001 From: Sumner Evans Date: Fri, 21 Oct 2022 09:35:03 -0500 Subject: [PATCH] pre-commit: add configuration and did some cleanup Signed-off-by: Sumner Evans --- .pre-commit-config.yaml | 20 ++++++++++++++++++++ commands.go | 15 +++++---------- config/config.go | 4 ++-- crypto.go | 1 + database/cryptostore.go | 1 + database/migrate.go | 3 ++- database/portal.go | 2 +- formatting.go | 3 +-- go.mod | 2 +- groupmeExt/message.go | 14 +++++++++++--- main.go | 2 +- matrix.go | 7 +++---- no-crypto.go | 1 + portal.go | 12 ++++++------ provisioning.go | 24 ++++++++++++------------ puppet.go | 5 ++--- types/contentURI.go | 28 ---------------------------- user.go | 6 +++--- 18 files changed, 73 insertions(+), 77 deletions(-) create mode 100644 .pre-commit-config.yaml delete mode 100644 types/contentURI.go diff --git a/.pre-commit-config.yaml b/.pre-commit-config.yaml new file mode 100644 index 0000000..b72f46c --- /dev/null +++ b/.pre-commit-config.yaml @@ -0,0 +1,20 @@ +repos: + - repo: https://github.com/pre-commit/pre-commit-hooks + rev: v4.1.0 + hooks: + - id: trailing-whitespace + exclude_types: [markdown] + - id: end-of-file-fixer + - id: check-yaml + - id: check-added-large-files + + - repo: https://github.com/tekwizely/pre-commit-golang + rev: v1.0.0-beta.5 + hooks: + - id: go-imports-repo + args: + - "-local" + - "github.com/beeper/hungryserv" + - "-w" + - id: go-vet-repo-mod + # - id: go-staticcheck-repo-mod diff --git a/commands.go b/commands.go index f59a67b..2a6787c 100644 --- a/commands.go +++ b/commands.go @@ -17,14 +17,10 @@ package main import ( - // "errors" - "fmt" "math" "sort" - // "math" - "strconv" "strings" @@ -879,12 +875,11 @@ func (handler *CommandHandler) CommandOpen(ce *CommandEvent) { const cmdPMHelp = `pm - To direct message someone already in a shared group start a direct chat with them in Matrix` func (handler *CommandHandler) CommandPM(ce *CommandEvent) { - ce.Reply(fmt.Sprintf("**DEPRECATED COMMAND:** `%s`", cmdPMHelp)) - return - if len(ce.Args) == 0 { - ce.Reply(fmt.Sprintf("**DEPRECATED COMMAND:** `%s`", cmdPMHelp)) - return - } + // ce.Reply(fmt.Sprintf("**DEPRECATED COMMAND:** `%s`", cmdPMHelp)) + // if len(ce.Args) == 0 { + // ce.Reply(fmt.Sprintf("**DEPRECATED COMMAND:** `%s`", cmdPMHelp)) + // return + // } // force := ce.Args[0] == "--force" // if force { diff --git a/config/config.go b/config/config.go index 755ecd9..c51e462 100644 --- a/config/config.go +++ b/config/config.go @@ -68,8 +68,8 @@ type Config struct { } `yaml:"metrics"` GroupMe struct { - OSName string `yaml:"os_name"` - BrowserName string `yaml:"browser_name"` + OSName string `yaml:"os_name"` + BrowserName string `yaml:"browser_name"` } `yaml:"groupme"` Bridge BridgeConfig `yaml:"bridge"` diff --git a/crypto.go b/crypto.go index 3b49e93..a3b6eff 100644 --- a/crypto.go +++ b/crypto.go @@ -14,6 +14,7 @@ // You should have received a copy of the GNU Affero General Public License // along with this program. If not, see . +//go:build cgo && !nocrypto // +build cgo,!nocrypto package main diff --git a/database/cryptostore.go b/database/cryptostore.go index 9e2a876..bfd02f8 100644 --- a/database/cryptostore.go +++ b/database/cryptostore.go @@ -14,6 +14,7 @@ // You should have received a copy of the GNU Affero General Public License // along with this program. If not, see . +//go:build cgo && !nocrypto // +build cgo,!nocrypto package database diff --git a/database/migrate.go b/database/migrate.go index 810bdd8..4c3c31d 100644 --- a/database/migrate.go +++ b/database/migrate.go @@ -92,8 +92,9 @@ package database // } func Migrate(old *Database, new *Database) { -print("skipping migration because test") + print("skipping migration because test") } + // err := migrateTable(old, new, "portal", "jid", "receiver", "mxid", "name", "topic", "avatar", "avatar_url", "encrypted") // if err != nil { // panic(err) diff --git a/database/portal.go b/database/portal.go index 8f5b4ce..a8e7b40 100644 --- a/database/portal.go +++ b/database/portal.go @@ -157,7 +157,7 @@ type Portal struct { Name string Topic string Avatar string - AvatarURL types.ContentURI + AvatarURL id.ContentURI Encrypted bool `gorm:"notNull;default:false"` } diff --git a/formatting.go b/formatting.go index f878fb7..2874ee2 100644 --- a/formatting.go +++ b/formatting.go @@ -97,8 +97,7 @@ func NewFormatter(bridge *Bridge) *Formatter { return fmt.Sprintf("%s", str) }, } - formatter.waReplFuncText = map[*regexp.Regexp]func(string) string{ - } + formatter.waReplFuncText = map[*regexp.Regexp]func(string) string{} return formatter } diff --git a/go.mod b/go.mod index 513175b..d173ea3 100644 --- a/go.mod +++ b/go.mod @@ -25,4 +25,4 @@ require ( maunium.net/go/mautrix v0.9.24 ) -replace github.com/karmanyaahm/groupme => ./groupme +replace github.com/karmanyaahm/groupme => ../groupme-lib diff --git a/groupmeExt/message.go b/groupmeExt/message.go index 0d416c8..5a421fe 100644 --- a/groupmeExt/message.go +++ b/groupmeExt/message.go @@ -36,7 +36,7 @@ func (m *Message) Value() (driver.Value, error) { return e, nil } -//DownloadImage helper function to download image from groupme; +// DownloadImage helper function to download image from groupme; // append .large/.preview/.avatar to get various sizes func DownloadImage(URL string) (bytes *[]byte, mime string, err error) { //TODO check its actually groupme? @@ -70,7 +70,11 @@ func DownloadFile(RoomJID types.GroupMeID, FileID string, token string) (content req, _ := http.NewRequest("POST", fmt.Sprintf("https://file.groupme.com/v1/%s/fileData", RoomJID), bytes.NewReader(b)) req.Header.Add("X-Access-Token", token) req.Header.Add("Content-Type", "application/json") - resp, _ := client.Do(req) + resp, err := client.Do(req) + if err != nil { + // TODO: FIX + panic(err) + } defer resp.Body.Close() data := []ImgData{} @@ -83,7 +87,11 @@ func DownloadFile(RoomJID types.GroupMeID, FileID string, token string) (content req, _ = http.NewRequest("POST", fmt.Sprintf("https://file.groupme.com/v1/%s/files/%s", RoomJID, FileID), nil) req.URL.Query().Add("token", token) req.Header.Add("X-Access-Token", token) - resp, _ = client.Do(req) + resp, err = client.Do(req) + if err != nil { + // TODO: FIX + panic(err) + } defer resp.Body.Close() bytes, _ := ioutil.ReadAll(resp.Body) diff --git a/main.go b/main.go index 2f03d30..614f22f 100644 --- a/main.go +++ b/main.go @@ -426,7 +426,7 @@ func (bridge *Bridge) Main() { defer pprof.StopCPUProfile() } - c := make(chan os.Signal) + c := make(chan os.Signal, 1) signal.Notify(c, os.Interrupt, syscall.SIGTERM) <-c diff --git a/matrix.go b/matrix.go index d0fa160..b5b3d61 100644 --- a/matrix.go +++ b/matrix.go @@ -31,7 +31,6 @@ import ( "maunium.net/go/mautrix/id" "github.com/karmanyaahm/matrix-groupme-go/database" - "github.com/karmanyaahm/matrix-groupme-go/types" ) type MatrixHandler struct { @@ -165,17 +164,17 @@ func (mx *MatrixHandler) handlePrivatePortal(roomID id.RoomID, inviter *User, pu func (mx *MatrixHandler) createPrivatePortalFromInvite(roomID id.RoomID, inviter *User, puppet *Puppet, portal *Portal) { portal.MXID = roomID portal.Topic = "WhatsApp private chat" - portal.Key = database.PortalKey{puppet.JID, inviter.JID} + portal.Key = database.PortalKey{JID: puppet.JID, Receiver: inviter.JID} _, _ = portal.MainIntent().SetRoomTopic(portal.MXID, portal.Topic) if portal.bridge.Config.Bridge.PrivateChatPortalMeta { m, _ := mx.bridge.StateStore.TryGetMemberRaw(portal.MXID, puppet.MXID) portal.Name = m.DisplayName - portal.AvatarURL = types.ContentURI{id.MustParseContentURI(m.AvatarURL)} + portal.AvatarURL = id.MustParseContentURI(m.AvatarURL) print("possible bug with pointer above") portal.Avatar = m.Avatar _, _ = portal.MainIntent().SetRoomName(portal.MXID, portal.Name) - _, _ = portal.MainIntent().SetRoomAvatar(portal.MXID, portal.AvatarURL.ContentURI) + _, _ = portal.MainIntent().SetRoomAvatar(portal.MXID, portal.AvatarURL) } else { portal.Name = "" } diff --git a/no-crypto.go b/no-crypto.go index 75a2c68..14e49c0 100644 --- a/no-crypto.go +++ b/no-crypto.go @@ -1,3 +1,4 @@ +//go:build !cgo || nocrypto // +build !cgo nocrypto package main diff --git a/portal.go b/portal.go index 323469d..a7b030f 100644 --- a/portal.go +++ b/portal.go @@ -196,7 +196,7 @@ func (portal *Portal) handleMessageLoop() { if msg.timestamp+MaxMessageAgeToCreatePortal < uint64(time.Now().Unix()) { portal.log.Debugln("Not creating portal room for incoming message: message is too old") continue - } + } portal.log.Debugln("Creating Matrix room from incoming message") err := portal.CreateMatrixRoom(msg.source) if err != nil { @@ -392,7 +392,7 @@ func (portal *Portal) UpdateAvatar(user *User, avatar string, updateInfo bool) b if err != nil { portal.log.Warnln("Failed to remove avatar:", err) } - portal.AvatarURL = types.ContentURI{} + portal.AvatarURL = id.ContentURI{} portal.Avatar = avatar return true } @@ -425,7 +425,7 @@ func (portal *Portal) UpdateAvatar(user *User, avatar string, updateInfo bool) b return false } - portal.AvatarURL = types.ContentURI{resp.ContentURI} + portal.AvatarURL = resp.ContentURI if len(portal.MXID) > 0 { _, err = portal.MainIntent().SetRoomAvatar(portal.MXID, resp.ContentURI) if err != nil { @@ -951,7 +951,7 @@ func (portal *Portal) CreateMatrixRoom(user *User) error { //m, _ := portal.bridge.StateStore.TryGetMemberRaw(portal.MXID, puppet.MXID) if portal.bridge.Config.Bridge.PrivateChatPortalMeta { portal.Name = meta.DisplayName - portal.AvatarURL = types.ContentURI{id.MustParseContentURI(meta.AvatarURL)} + portal.AvatarURL = id.MustParseContentURI(meta.AvatarURL) portal.Avatar = meta.Avatar } else { portal.Name = "" @@ -992,7 +992,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.ContentURI}, + Parsed: event.RoomAvatarEventContent{URL: portal.AvatarURL}, }, }) } @@ -1414,7 +1414,7 @@ func (portal *Portal) handleAttachment(intent *appservice.IntentAPI, attachment portal.log.Warnln("Unable to handle groupme attachment type", attachment.Type) return nil, true, fmt.Errorf("Unable to handle groupme attachment type %s", attachment.Type) } - return nil, true, errors.New("Unknown type") + // return nil, true, errors.New("Unknown type") } func (portal *Portal) HandleMediaMessage(source *User, msg mediaMessage) { // intent := portal.startHandling(source, msg.info) diff --git a/provisioning.go b/provisioning.go index a86c482..13d4f4f 100644 --- a/provisioning.go +++ b/provisioning.go @@ -336,20 +336,20 @@ var upgrader = websocket.Upgrader{ } func (prov *ProvisioningAPI) Login(w http.ResponseWriter, r *http.Request) { - userID := r.URL.Query().Get("user_id") - user := prov.bridge.GetUserByMXID(id.UserID(userID)) + // userID := r.URL.Query().Get("user_id") + // user := prov.bridge.GetUserByMXID(id.UserID(userID)) - if len(ce.Args) < 1 { - // Return error that the token needs to be longer than 0 length - // ce.Reply(`Get your access token from https://dev.groupme.com/ which should be the first argument to login`) - return - } - user.Token = ce.Args[0] + // if len(ce.Args) < 1 { + // // Return error that the token needs to be longer than 0 length + // // ce.Reply(`Get your access token from https://dev.groupme.com/ which should be the first argument to login`) + // return + // } + // user.Token = ce.Args[0] - user.addToJIDMap() - // ce.Reply("Successfully logged in, synchronizing chats...") - user.PostLogin() - user.Connect() + // user.addToJIDMap() + // // ce.Reply("Successfully logged in, synchronizing chats...") + // user.PostLogin() + // user.Connect() // c, err := upgrader.Upgrade(w, r, nil) // if err != nil { diff --git a/puppet.go b/puppet.go index 5055108..d2fae6f 100644 --- a/puppet.go +++ b/puppet.go @@ -237,10 +237,9 @@ func (puppet *Puppet) UpdateAvatar(source *User, portalMXID id.RoomID, avatar st } func (puppet *Puppet) UpdateName(source *User, portalMXID id.RoomID, contact groupme.Member) bool { - newName, quality := puppet.bridge.Config.Bridge.FormatDisplayname(contact) + newName, _ := puppet.bridge.Config.Bridge.FormatDisplayname(contact) memberRaw, _ := puppet.bridge.StateStore.TryGetMemberRaw(portalMXID, puppet.MXID) //TODO Handle - quality = quality //quality not used if memberRaw.DisplayName != newName { //&& quality >= puppet.NameQuality[portalMXID] { var err error @@ -278,7 +277,7 @@ func (puppet *Puppet) updatePortalAvatar() { portal.log.Warnln("Failed to set avatar:", err) } } - portal.AvatarURL = types.ContentURI{id.MustParseContentURI(m.AvatarURL)} + portal.AvatarURL = id.MustParseContentURI(m.AvatarURL) portal.Avatar = m.Avatar portal.Update() }) diff --git a/types/contentURI.go b/types/contentURI.go deleted file mode 100644 index c0d6430..0000000 --- a/types/contentURI.go +++ /dev/null @@ -1,28 +0,0 @@ -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 { - //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 -} diff --git a/user.go b/user.go index cb54b0d..ae52ecc 100644 --- a/user.go +++ b/user.go @@ -363,7 +363,7 @@ func (user *User) Login(ce *CommandEvent) { // // Also between the two logout methods (commands.go and provisioning.go) // user.ConnectionErrors = 0 // user.JID = strings.Replace(user.Conn.Info.Wid, whatsappExt.OldUserSuffix, whatsappExt.NewUserSuffix, 1) - + if len(ce.Args) < 1 { ce.Reply(`Get your access token from https://dev.groupme.com/ which should be the first argument to login`) return @@ -643,7 +643,7 @@ func (user *User) syncPortals(createAll bool) { break } wg.Add(1) - go func(chat Chat) { + go func(chat Chat, i int) { create := (chat.LastMessageTime >= user.LastConnection && user.LastConnection > 0) || i < limit if len(chat.Portal.MXID) > 0 || create || createAll { chat.Portal.Sync(user, chat.Group) @@ -654,7 +654,7 @@ func (user *User) syncPortals(createAll bool) { } wg.Done() - }(chat) + }(chat, i) } wg.Wait()