Skip to content

Commit

Permalink
fix session migration and assign message history to correct sessions
Browse files Browse the repository at this point in the history
  • Loading branch information
Blackoverflow committed Oct 30, 2022
1 parent e521ab3 commit 04570ce
Show file tree
Hide file tree
Showing 6 changed files with 156 additions and 8 deletions.
7 changes: 6 additions & 1 deletion app/store/db.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ var (

sessionsSelect = "SELECT * FROM sessions ORDER BY timestamp DESC"

messagesSchema = "CREATE TABLE IF NOT EXISTS messages (id INTEGER PRIMARY KEY, sid integer, source text, srcUUID string NOT NULL DEFAULT 0, message text, outgoing boolean, sentat integer, receivedat integer, ctype integer, attachment string, issent boolean, isread boolean, flags integer default 0, sendingError boolean, expireTimer integer default 0, receipt boolean default 0, statusMessage boolean default 0, quoteId integer NOT NULL default -1)"
messagesSchema = "CREATE TABLE IF NOT EXISTS messages (id INTEGER PRIMARY KEY, sv2id integer, source text, srcUUID string NOT NULL DEFAULT 0, message text, outgoing boolean, sentat integer, receivedat integer, ctype integer, attachment string, issent boolean, isread boolean, flags integer default 0, sendingError boolean, expireTimer integer default 0, receipt boolean default 0, statusMessage boolean default 0, quoteId integer NOT NULL default -1)"
messagesInsert = "INSERT INTO messages (sid, source, srcUUID, message, outgoing, sentat, receivedat, ctype, attachment, issent, isread, flags, sendingError, expireTimer, statusMessage, quoteID) VALUES (:sid, :source, :srcUUID, :message, :outgoing, :sentat, :receivedat, :ctype, :attachment, :issent, :isread, :flags, :sendingError, :expireTimer, :statusMessage, :quoteId)"
messagesSelectWhereMore = "SELECT * FROM messages WHERE sid = ? AND sentat < ? ORDER BY sentat DESC LIMIT 20"

Expand Down 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
}
9 changes: 5 additions & 4 deletions app/store/message.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,12 @@ import (
log "github.com/sirupsen/logrus"
)

const getLastMessagesQuery = "SELECT *, max(sentat) FROM messages GROUP BY sid ORDER BY sentat DESC"
const getLastMessagesQuery = "SELECT *, max(sentat) FROM messages GROUP BY sv2id ORDER BY sentat DESC"

type Message struct {
ID int64 `db:"id"`
SID int64
ID int64 `db:"id"`
SID int64 `db:"sv2id"`
SV1ID *int64 `db:"sid"`
ChatID string
Source string `db:"source"`
SourceUUID string `db:"srcUUID"`
Expand Down Expand Up @@ -181,7 +182,7 @@ func GetLastMessagesForAllSessions() ([]Message, error) {

func getMessagesForSession(id int64, limit, offset int) ([]*Message, error) {
var messages = []*Message{}
err := DS.Dbx.Select(&messages, "SELECT * FROM messages WHERE sid = ? ORDER BY sentat DESC LIMIT ? OFFSET ?", id, limit, offset)
err := DS.Dbx.Select(&messages, "SELECT * FROM messages WHERE sv2id = ? ORDER BY sentat DESC LIMIT ? OFFSET ?", id, limit, offset)
if err != nil {
return nil, err
}
Expand Down
138 changes: 138 additions & 0 deletions app/store/migrations.go
Original file line number Diff line number Diff line change
Expand Up @@ -243,3 +243,141 @@ func update_v_1_6_0() error {
}
return nil
}

// update_v_1_6_1 fixes the message histroy by introducing the new column sv2id in messages
func update_v_1_6_1() error {
err := sessionsV1toSessionsV2()
if err != nil {
return err
}
return migrateMessageIds()
}

func migrateMessageIds() error {
// check if new column exists and only migrate if it does not
_, err := DS.Dbx.Prepare("SELECT sv2id FROM messages limit 1")
if err == nil {
return nil
}
log.Infoln("[axolotl][update v_1_6_1] add column sv2id")
_, err = DS.Dbx.Exec("ALTER TABLE messages ADD sv2id integer;")
if err != nil {
return err
}

log.Infoln("[axolotl][update v_1_6_1] set sv2id for group messages")
_, err = DS.Dbx.Exec("UPDATE messages SET sV2id = (SELECT v2.ID from sessions v1 JOIN sessionsv2 v2 ON v1.uuid = v2.groupV2Id where v1.ID = messages.sid) WHERE sv2id IS null;")
if err != nil {
return err
}
log.Infoln("[axolotl][update v_1_6_1] set sv2id for direct messages")
_, err = DS.Dbx.Exec("UPDATE messages SET sv2id = (SELECT ID from sessionsv2 WHERE directMessageRecipientId = (SELECT r.id from recipients r JOIN sessions v1 ON r.uuid = v1.uuid WHERE v1.id = messages.sid)) WHERE sv2id IS null;")
if err != nil {
return err
}
log.Infoln("[axolotl][update v_1_6_1] set sv2id for messages of newly created sessions")
_, err = DS.Dbx.Exec("UPDATE messages SET sV2id = sid WHERE sv2id IS null;")
return err
}

func sessionV1ToGroupV2(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("[axolotl][update v_1_6_1] error getting group members: %s", err)
}
err = group.AddGroupMembers(groupMembers)
if err != nil {
return fmt.Errorf("[axolotl][update v_1_6_1] error adding group members: %s", err)
}
return nil
}

func sessionV1ToSessionV2(session *Session) error {
if session.IsGroup && session.Type == SessionTypeGroupV2 {
err := sessionV1ToGroupV2(session)
if err != nil {
return err
}
} else if session.IsGroup && session.Type == SessionTypeGroupV1 {
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),
})
if err != nil {
return err
}
} else if session.Type == SessionTypePrivateChat {
sessionV2, err := SessionsV2Model.GetSessionByID(session.ID)
if sessionV2 != nil && err == nil {
//allready migrated
return nil
}
recipient := RecipientsModel.GetRecipientByUUID(session.UUID)
log.Infoln("[axolotl][update v_1_6_1] migrate private 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,
})
if err != nil {
return err
}
}
return nil
}

func sessionsV1toSessionsV2() 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 sessionsv1 to sessionsv2")
for _, session := range sessions {
err = sessionV1ToSessionV2(session)
if err != nil {
log.Errorf("failed to migrate session %s. Error: %s", session.UUID, err)
}
}
return nil
}
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: 2 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -31,3 +31,5 @@ require (
gopkg.in/yaml.v3 v3.0.0-20210107192922-496545a6307b // indirect
launchpad.net/gocheck v0.0.0-20140225173054-000000000087 // indirect
)

replace github.com/signal-golang/textsecure v1.18.0 => github.com/Blackoverflow/textsecure v1.4.3-0.20221029211627-ba3c0a915b70
4 changes: 4 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
cloud.google.com/go v0.26.0/go.mod h1:aQUYkXzVsufM+DwF1aE+0xfcU+56JwCaLick0ClmMTw=
github.com/Blackoverflow/gcmsiv v0.0.0-20201031155123-20af2af977ff h1:l3WopnX/gWnDItXZdr2Ygq0+4ZwXCSyozyPNR8jdb8I=
github.com/Blackoverflow/gcmsiv v0.0.0-20201031155123-20af2af977ff/go.mod h1:t1lmwOv+IYxaUkr2qIKXEfn90t4GgocS0qy8jkDonbg=
github.com/Blackoverflow/textsecure v1.4.3-0.20221028183342-5dd575a18460 h1:ZPUbXWULhddzbWI9AnpKdbKGY317FatHsAyTQzJg3Xs=
github.com/Blackoverflow/textsecure v1.4.3-0.20221028183342-5dd575a18460/go.mod h1:sna5yQxNwY/F54q/K4xxP90VgWMSBgm3L8zbWOkF2mk=
github.com/Blackoverflow/textsecure v1.4.3-0.20221029211627-ba3c0a915b70 h1:uAVKek9XGhmBDU9FHs41IlY3GfJRtBaEuFcspouhLts=
github.com/Blackoverflow/textsecure v1.4.3-0.20221029211627-ba3c0a915b70/go.mod h1:sna5yQxNwY/F54q/K4xxP90VgWMSBgm3L8zbWOkF2mk=
github.com/BurntSushi/toml v0.3.1/go.mod h1:xHWCNGjB5oqiDr8zfno3MHue2Ht5sIBksp03qcyfWMU=
github.com/PuerkitoBio/goquery v1.5.1/go.mod h1:GsLWisAFVj4WgDibEWF4pvYnkVQBpKBKeU+7zCJoLcc=
github.com/akavel/rsrc v0.8.0 h1:zjWn7ukO9Kc5Q62DOJCcxGpXC18RawVtYAGdz2aLlfw=
Expand Down

0 comments on commit 04570ce

Please sign in to comment.