-
Notifications
You must be signed in to change notification settings - Fork 1
/
2019-05-23-protoupgrade.go
61 lines (57 loc) · 1.5 KB
/
2019-05-23-protoupgrade.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
package upgrades
import (
"database/sql"
"encoding/json"
"fmt"
)
func init() {
var keys = []string{"imageMessage", "contactMessage", "locationMessage", "extendedTextMessage", "documentMessage", "audioMessage", "videoMessage"}
upgrades[4] = upgrade{"Update message content to new protocol version. This may take a while.", func(tx *sql.Tx, ctx context) error {
rows, err := ctx.db.Query("SELECT mxid, content FROM message")
if err != nil {
return err
}
for rows.Next() {
var mxid string
var rawContent []byte
err = rows.Scan(&mxid, &rawContent)
if err != nil {
fmt.Println("Error scanning:", err)
continue
}
var content map[string]interface{}
err = json.Unmarshal(rawContent, &content)
if err != nil {
fmt.Printf("Error unmarshaling content of %s: %v\n", mxid, err)
continue
}
for _, key := range keys {
val, ok := content[key].(map[string]interface{})
if !ok {
continue
}
ci, ok := val["contextInfo"].(map[string]interface{})
if !ok {
continue
}
qm, ok := ci["quotedMessage"].([]interface{})
if !ok {
continue
}
ci["quotedMessage"] = qm[0]
goto save
}
continue
save:
rawContent, err = json.Marshal(&content)
if err != nil {
fmt.Printf("Error marshaling updated content of %s: %v\n", mxid, err)
}
_, err = tx.Exec("UPDATE message SET content=$1 WHERE mxid=$2", rawContent, mxid)
if err != nil {
fmt.Printf("Error updating row of %s: %v\n", mxid, err)
}
}
return nil
}}
}