Skip to content

Commit

Permalink
legacymigrate: move to mautrix-go
Browse files Browse the repository at this point in the history
  • Loading branch information
tulir committed Jul 19, 2024
1 parent cbd0e9e commit 661ee44
Show file tree
Hide file tree
Showing 6 changed files with 3 additions and 85 deletions.
72 changes: 0 additions & 72 deletions cmd/mautrix-slack/legacymigrate.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,16 +17,7 @@
package main

import (
"context"
_ "embed"
"fmt"

"github.com/rs/zerolog"
"maunium.net/go/mautrix/appservice"
"maunium.net/go/mautrix/bridgev2/database"
"maunium.net/go/mautrix/bridgev2/matrix"
"maunium.net/go/mautrix/event"
"maunium.net/go/mautrix/id"
)

const legacyMigrateRenameTables = `
Expand All @@ -45,66 +36,3 @@ ALTER TABLE emoji RENAME TO emoji_old;

//go:embed legacymigrate.sql
var legacyMigrateCopyData string

func postMigrate(ctx context.Context) error {
wasMigrated, err := m.DB.TableExists(ctx, "database_was_migrated")
if err != nil {
return fmt.Errorf("failed to check if database_was_migrated table exists: %w", err)
} else if !wasMigrated {
return nil
}
zerolog.Ctx(ctx).Info().Msg("Doing post-migration updates to Matrix rooms")

portals, err := m.Bridge.GetAllPortalsWithMXID(ctx)
if err != nil {
return fmt.Errorf("failed to get all portals: %w", err)
}
for _, portal := range portals {
switch portal.RoomType {
case database.RoomTypeDM:
otherUserID := portal.OtherUserID
if otherUserID == "" {
zerolog.Ctx(ctx).Warn().Msg("DM portal has no other user ID")
} else {
ghost, err := m.Bridge.GetGhostByID(ctx, otherUserID)
if err != nil {
return fmt.Errorf("failed to get ghost for %s: %w", otherUserID, err)
}
mx := ghost.Intent.(*matrix.ASIntent).Matrix
err = m.Matrix.Bot.EnsureJoined(ctx, portal.MXID, appservice.EnsureJoinedParams{
BotOverride: mx.Client,
})
if err != nil {
zerolog.Ctx(ctx).Warn().Err(err).Stringer("room_id", portal.MXID).Msg("Failed to ensure bot is joined to DM")
}
pls, err := mx.PowerLevels(ctx, portal.MXID)
if err != nil {
zerolog.Ctx(ctx).Warn().Err(err).Stringer("room_id", portal.MXID).Msg("Failed to get power levels in room")
} else {
userLevel := pls.GetUserLevel(mx.UserID)
pls.EnsureUserLevel(m.Matrix.Bot.UserID, userLevel)
if userLevel > 50 {
pls.SetUserLevel(mx.UserID, 50)
}
_, err = mx.SetPowerLevels(ctx, portal.MXID, pls)
if err != nil {
zerolog.Ctx(ctx).Warn().Err(err).Stringer("room_id", portal.MXID).Msg("Failed to set power levels")
}
}
}
}
_, err = m.Matrix.Bot.SendStateEvent(ctx, portal.MXID, event.StateElementFunctionalMembers, "", &event.ElementFunctionalMembersContent{
ServiceMembers: []id.UserID{m.Matrix.Bot.UserID},
})
if err != nil {
zerolog.Ctx(ctx).Warn().Err(err).Stringer("room_id", portal.MXID).Msg("Failed to set service members")
}
}

_, err = m.DB.Exec(ctx, "DROP TABLE database_was_migrated")
if err != nil {
return fmt.Errorf("failed to drop database_was_migrated table: %w", err)
}
zerolog.Ctx(ctx).Info().Msg("Post-migration updates complete")
return nil
}
2 changes: 0 additions & 2 deletions cmd/mautrix-slack/legacymigrate.sql
Original file line number Diff line number Diff line change
Expand Up @@ -227,5 +227,3 @@ DROP TABLE user_team_old;
DROP TABLE puppet_old;
DROP TABLE user_old;
DROP TABLE emoji_old;

CREATE TABLE database_was_migrated();
1 change: 0 additions & 1 deletion cmd/mautrix-slack/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,6 @@ func main() {
true,
)
}
c.HackyStartupHook = postMigrate
m.InitVersion(Tag, Commit, BuildTime)
m.Run()
}
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ require (
github.com/yuin/goldmark v1.7.4
go.mau.fi/util v0.6.0
gopkg.in/yaml.v3 v3.0.1
maunium.net/go/mautrix v0.19.1-0.20240718165923-a4b0b55db29d
maunium.net/go/mautrix v0.19.1-0.20240719110719-b881a7d45511
)

require (
Expand Down
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -73,5 +73,5 @@ gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
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/mautrix v0.19.1-0.20240718165923-a4b0b55db29d h1:/bZZbGY5XcWdt0HjBSdcjIRcHX8uK+v+DZJyQ8q0Mn4=
maunium.net/go/mautrix v0.19.1-0.20240718165923-a4b0b55db29d/go.mod h1:UE+mSQ4sDUuJMbjN0aB9EjQSGgXd48AzMvZ6+QJV1k8=
maunium.net/go/mautrix v0.19.1-0.20240719110719-b881a7d45511 h1:Qbpe6EdaZn4YzNsJTrowWv34afbc1c8OYij58e7Vl70=
maunium.net/go/mautrix v0.19.1-0.20240719110719-b881a7d45511/go.mod h1:UE+mSQ4sDUuJMbjN0aB9EjQSGgXd48AzMvZ6+QJV1k8=
7 changes: 0 additions & 7 deletions pkg/connector/connector.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,6 @@ type SlackConnector struct {
Config Config
DB *slackdb.SlackDB
MsgConv *msgconv.MessageConverter

HackyStartupHook func(context.Context) error
}

var (
Expand All @@ -51,11 +49,6 @@ func (s *SlackConnector) SetMaxFileSize(maxSize int64) {
}

func (s *SlackConnector) Start(ctx context.Context) error {
if s.HackyStartupHook != nil {
if err := s.HackyStartupHook(ctx); err != nil {
return err
}
}
return s.DB.Upgrade(ctx)
}

Expand Down

0 comments on commit 661ee44

Please sign in to comment.