Skip to content

Commit

Permalink
Merge pull request #939 from nanu-c/fix_session_migartion
Browse files Browse the repository at this point in the history
Fix session migration and assign message history to correct sessions
  • Loading branch information
nanu-c authored Nov 9, 2022
2 parents b427045 + 607e86c commit 7c19ccf
Show file tree
Hide file tree
Showing 10 changed files with 178 additions and 12 deletions.
5 changes: 5 additions & 0 deletions app/store/db.go
Original file line number Diff line number Diff line change
Expand Up @@ -306,5 +306,10 @@ func Migrate() error {
log.Errorln("[axolotl] setupDb: Couldn't migrate db: " + err.Error())
return err
}
err = update_v_1_6_1()
if err != nil {
log.Errorln("[axolotl] Couldn't migrate sessions: " + err.Error())
return err
}
return nil
}
3 changes: 2 additions & 1 deletion app/store/message.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ const getLastMessagesQuery = "SELECT *, max(sentat) FROM messages GROUP BY sid O
type Message struct {
ID int64 `db:"id"`
SID int64
SV1ID *int64
ChatID string
Source string `db:"source"`
SourceUUID string `db:"srcUUID"`
Expand Down Expand Up @@ -162,7 +163,7 @@ func FindOutgoingMessage(timestamp uint64) (*Message, error) {
// GetUnreadMessageCounterForSession returns an int for the unread messages for a session
func GetUnreadMessageCounterForSession(id int64) (int64, error) {
var message = []Message{}
err := DS.Dbx.Select(&message, "SELECT id FROM messages WHERE isread = 0 AND sessionid = ?", id)
err := DS.Dbx.Select(&message, "SELECT id FROM messages WHERE isread = 0 AND sid = ?", id)
if err != nil {
return 0, err
}
Expand Down
154 changes: 154 additions & 0 deletions app/store/migrations.go
Original file line number Diff line number Diff line change
Expand Up @@ -243,3 +243,157 @@ func update_v_1_6_0() error {
}
return nil
}

// update_v_1_6_1 fixes the message histroy and migrates missing chats
func update_v_1_6_1() error {
// check if new column exists and only migrate if it does not
_, err := DS.Dbx.Prepare("SELECT sv1id FROM messages limit 1")
if err == nil {
return nil
}
err = migrateMissingSessions()
if err != nil {
return err
}
return migrateMessageIds()
}

func migrateMissingSessions() error {
var sessions []*Session
err := DS.Dbx.Select(&sessions, sessionsSelect)
if err != nil {
return fmt.Errorf("error loading sessions: %s", err)
}
log.Infoln("[axolotl][update v_1_6_1] migrate missing sessions")
for _, session := range sessions {
err = migrateSession(session)
if err != nil {
log.Errorf("[axolotl][update v_1_6_1] failed to migrate session. Error: %s", err)
}
}
return nil
}

func migrateSession(session *Session) error {
if session.IsGroup && session.Type == SessionTypeGroupV2 {
return migrateGroupV2Session(session)
} else if session.IsGroup && session.Type == SessionTypeGroupV1 {
return migrateGroupV1Session(session)
} else if session.Type == SessionTypePrivateChat {
return migrateDirectChatSession(session)
}
return fmt.Errorf("session type unknown: isGroup:%t, type:%d", session.IsGroup, session.Type)
}

func migrateDirectChatSession(session *Session) error {
var err error
recipient := RecipientsModel.GetRecipientByUUID(session.UUID)
if recipient != nil {
sessionV2, err := SessionsV2Model.GetSessionByDirectMessageRecipientID(recipient.Id)
if err != nil {
return err
}
if sessionV2 != nil {
//allready migrated
return nil
}
}
log.Infoln("[axolotl][update v_1_6_1] migrate direct chat session")
if recipient == nil {
recipient, err = RecipientsModel.CreateRecipientWithoutProfileUpdate(&Recipient{
UUID: session.UUID,
ProfileGivenName: session.Name,
E164: session.Tel,
})
if err != nil {
return err
}
}
_, err = SessionsV2Model.SaveSession(&SessionV2{
ID: session.ID,
DirectMessageRecipientID: recipient.Id,
})
return err
}

func migrateGroupV1Session(session *Session) error {
sessionV2, err := SessionsV2Model.GetSessionByID(session.ID)
if sessionV2 != nil && err == nil {
//allready migrated
return nil
}
log.Infoln("[axolotl][update v_1_6_1] migrate groupv1 session")
_, err = SessionsV2Model.SaveSession(&SessionV2{
ID: session.ID,
GroupV1ID: session.UUID,
DirectMessageRecipientID: int64(GroupRecipientsID),
})
return err
}

func migrateGroupV2Session(session *Session) error {
group, err := GroupV2sModel.GetGroupById(session.UUID)
if group != nil && err == nil {
//allready migrated
return nil
}
log.Infoln("[axolotl][update v_1_6_1] migrate groupv2 session")

group, err = GroupV2sModel.Create(&GroupV2{
Id: session.UUID,
Name: session.Name,
JoinStatus: session.GroupJoinStatus,
})
if err != nil {
return fmt.Errorf("error creating group v2: %s", err)
}
_, err = SessionsV2Model.SaveSession(&SessionV2{
ID: session.ID,
DirectMessageRecipientID: int64(GroupRecipientsID),
GroupV2ID: session.UUID,
})
if err != nil {
return fmt.Errorf("error creating session groupv2: %s", err)
}
log.Infoln("[axolotl][update v_1_6_1] migrate groupv2 session: members")
groupMembers, err := textsecure.GetGroupV2MembersForGroup(session.UUID)
if err != nil {
return fmt.Errorf("error getting group members: %s", err)
}
err = group.AddGroupMembers(groupMembers)
if err != nil {
return fmt.Errorf("error adding group members: %s", err)
}
return nil
}

func migrateMessageIds() error {
log.Infoln("[axolotl][update v_1_6_1] add column sv1id")
_, err := DS.Dbx.Exec("ALTER TABLE messages ADD sv1id integer;")
if err != nil {
return err
}
log.Infoln("[axolotl][update v_1_6_1] copy sid into sv1id")
_, err = DS.Dbx.Exec("UPDATE messages SET sv1id = sid;")
if err != nil {
return err
}
log.Infoln("[axolotl][update v_1_6_1] delete sid of all messages")
_, err = DS.Dbx.Exec("UPDATE messages SET sid = null;")
if err != nil {
return err
}
log.Infoln("[axolotl][update v_1_6_1] set sid for group messages")
_, err = DS.Dbx.Exec("UPDATE messages SET sid = (SELECT v2.ID from sessions v1 JOIN sessionsv2 v2 ON v1.uuid = v2.groupV2Id where v1.ID = messages.sv1id) WHERE sid IS null;")
if err != nil {
return err
}
log.Infoln("[axolotl][update v_1_6_1] set sid for direct messages")
_, err = DS.Dbx.Exec("UPDATE messages SET sid = (SELECT ID from sessionsv2 WHERE directMessageRecipientId = (SELECT r.id from recipients r JOIN sessions v1 ON r.uuid = v1.uuid WHERE v1.id = messages.sv1id)) WHERE sid IS null;")
if err != nil {
return err
}
log.Infoln("[axolotl][update v_1_6_1] set sid for messages of new sessions")
_, err = DS.Dbx.Exec("UPDATE messages SET sid = sv1id WHERE sid IS null;")
return err
}
4 changes: 1 addition & 3 deletions app/store/sessionV2.go
Original file line number Diff line number Diff line change
Expand Up @@ -324,10 +324,8 @@ func (s *SessionV2) NotificationsToggle() error {
func (s *SessionV2) GetName() (string, error) {
if s.IsGroup() {
return s.getGroupName()
} else {
return s.getDirectChatName()
}
return "", fmt.Errorf("GetSessionNames failed")
return s.getDirectChatName()
}

func (s *SessionV2) getDirectChatName() (string, error) {
Expand Down
2 changes: 1 addition & 1 deletion app/worker/textsecureapi.go
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@ func RunBackend(errorChannel chan error) error {
},
GetVerificationCode: func() string {
if !settings.SettingsModel.Registered {
log.Debugf("settings.SettingsModel.Registered = false")
log.Debugf("[axolotl] settings.SettingsModel.Registered = false")
verificationCode := ui.GetVerificationCode()
settings.SettingsModel.Registered = true
return verificationCode
Expand Down
4 changes: 2 additions & 2 deletions axolotl-web/src/components/ErrorModal.vue
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
<h5 v-translate class="modal-title">
Error communicating with Signal servers
</h5>
<button type="button" class="close" @click="close">
<button type="button" class="close btn" @click="close">
<span aria-hidden="true">&times;</span>
</button>
</div>
Expand All @@ -22,7 +22,7 @@
</p>
<p v-translate>
If you think that something is wrong on your side, you can
<a href="#" @click="unregister">unregister</a> and register again.
<a @click="unregister">unregister</a> and register again.
Be careful,
<strong>your encryption key will change and you will lose all your
messages</strong>
Expand Down
2 changes: 0 additions & 2 deletions axolotl-web/src/store/store.js
Original file line number Diff line number Diff line change
Expand Up @@ -725,8 +725,6 @@ export default createStore({
}
socketSend(message);
}
this.commit("SET_REGISTRATION_STATUS", "");
router.push("/")
},
sendPin(state, pin) {
if (this.state.socket.isConnected) {
Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ require (
github.com/satori/go.uuid v1.2.0
github.com/signal-golang/go-vcard v0.1.2
github.com/signal-golang/libphonenumber v1.2.2-0.20220127214340-b765372d3707
github.com/signal-golang/textsecure v1.18.0
github.com/signal-golang/textsecure v1.20.0
github.com/sirupsen/logrus v1.9.0
github.com/ttacon/builder v0.0.0-20170518171403-c099f663e1c2 // indirect
github.com/vincent-petithory/dataurl v1.0.0
Expand Down
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -112,8 +112,8 @@ github.com/signal-golang/libphonenumber v1.2.2-0.20220127214340-b765372d3707 h1:
github.com/signal-golang/libphonenumber v1.2.2-0.20220127214340-b765372d3707/go.mod h1:oW+JXk+4C0R5h8BQzfRA4k/vXuBfBcNSqKWjn7Dgncs=
github.com/signal-golang/mimemagic v0.0.0-20200821045537-3f613cf2cd3f h1:S17lCk5rcobvgcaLf7XSAoLuNkRPgFyhvgRA9JwDzHc=
github.com/signal-golang/mimemagic v0.0.0-20200821045537-3f613cf2cd3f/go.mod h1:tU6SWwv50oGkZNPlvTFvmqZvEBp0vWWCC+LCEVlTE5A=
github.com/signal-golang/textsecure v1.18.0 h1:3aEH+l09RkTO/nWzHaYdEoKkB1MXJIYis/65yFzal1c=
github.com/signal-golang/textsecure v1.18.0/go.mod h1:sna5yQxNwY/F54q/K4xxP90VgWMSBgm3L8zbWOkF2mk=
github.com/signal-golang/textsecure v1.20.0 h1:qwCAeLCRcMxNBHSkehAQyx5V1f4Xa2GWzYjTpe2Q1SI=
github.com/signal-golang/textsecure v1.20.0/go.mod h1:sna5yQxNwY/F54q/K4xxP90VgWMSBgm3L8zbWOkF2mk=
github.com/sirupsen/logrus v1.8.1/go.mod h1:yWOB1SBYBC5VeMP7gHvWumXLIWorT60ONWic61uBYv0=
github.com/sirupsen/logrus v1.9.0 h1:trlNQbNUG3OdDrDil03MCb1H2o9nJ1x4/5LYw7byDE0=
github.com/sirupsen/logrus v1.9.0/go.mod h1:naHLuLoDiP4jHNo9R0sCBMtWGeIprob74mVsIT4qYEQ=
Expand Down
10 changes: 10 additions & 0 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,9 +30,19 @@ func init() {
flag.StringVar(&config.ServerHost, "host", "127.0.0.1", "Host to serve UI from.")
flag.StringVar(&config.ServerPort, "port", "9080", "Port to serve UI from.")
flag.StringVar(&config.ElectronFlag, "electron-flag", "", "Specify electron flag. Use no-ozone to disable Ozone/Wayland platform")
flag.StringVar(&config.LogLevel, "d", "", "Specify debug level. Use either debug, info, warn, error or fatal")
}
func setup() {
config.SetupConfig()
if config.LogLevel != "" {
level, err := log.ParseLevel(config.LogLevel)
if err != nil {
log.Fatal(err)
}
log.SetLevel(level)
} else {
log.SetLevel(log.InfoLevel)
}
log.Infoln("[axolotl] Starting axolotl version", config.AppVersion)
}
func runBackend() {
Expand Down

0 comments on commit 7c19ccf

Please sign in to comment.