2019-05-23-protoupgrade.go 1.5 KB

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