feat(exporter): use indentation, export some new fields

This commit is contained in:
Sam 2023-04-08 01:00:37 +02:00
parent 97ffb2eab5
commit 62b9c1dbd6
No known key found for this signature in database
GPG key ID: B4EF20DDE721CAA1
2 changed files with 47 additions and 20 deletions

View file

@ -118,6 +118,11 @@ func (s *server) doExport(u db.User) {
log.Debugf("[%v] starting export of user", u.ID) log.Debugf("[%v] starting export of user", u.ID)
jsonBuffer := new(bytes.Buffer)
encoder := json.NewEncoder(jsonBuffer)
encoder.SetEscapeHTML(false)
encoder.SetIndent("", " ")
outBuffer := new(bytes.Buffer) outBuffer := new(bytes.Buffer)
zw := zip.NewWriter(outBuffer) zw := zip.NewWriter(outBuffer)
defer zw.Close() defer zw.Close()
@ -136,19 +141,28 @@ func (s *server) doExport(u db.User) {
return return
} }
log.Debugf("[%v] getting user warnings", u.ID)
warnings, err := s.DB.Warnings(ctx, u.ID, false)
if err != nil {
log.Errorf("[%v] getting warnings: %v", u.ID, err)
return
}
log.Debugf("[%v] writing user json", u.ID) log.Debugf("[%v] writing user json", u.ID)
ub, err := json.Marshal(dbUserToExport(u, fields)) err = encoder.Encode(dbUserToExport(u, fields, warnings))
if err != nil { if err != nil {
log.Errorf("[%v] marshaling user: %v", u.ID, err) log.Errorf("[%v] marshaling user: %v", u.ID, err)
return return
} }
_, err = w.Write(ub) _, err = io.Copy(w, jsonBuffer)
if err != nil { if err != nil {
log.Errorf("[%v] writing user: %v", u.ID, err) log.Errorf("[%v] writing user: %v", u.ID, err)
return return
} }
jsonBuffer.Reset()
if u.Avatar != nil { if u.Avatar != nil {
log.Debugf("[%v] getting user avatar", u.ID) log.Debugf("[%v] getting user avatar", u.ID)
@ -196,17 +210,18 @@ func (s *server) doExport(u db.User) {
return return
} }
mb, err := json.Marshal(dbMemberToExport(m, fields)) err = encoder.Encode(dbMemberToExport(m, fields))
if err != nil { if err != nil {
log.Errorf("[%v] marshaling member %v: %v", u.ID, m.ID, err) log.Errorf("[%v] marshaling member %v: %v", u.ID, m.ID, err)
return return
} }
_, err = w.Write(mb) _, err = io.Copy(w, jsonBuffer)
if err != nil { if err != nil {
log.Errorf("[%v] writing member %v json: %v", u.ID, m.ID, err) log.Errorf("[%v] writing member %v json: %v", u.ID, m.ID, err)
return return
} }
jsonBuffer.Reset()
if m.Avatar != nil { if m.Avatar != nil {
log.Debugf("[%v] getting member %v avatar", u.ID, m.ID) log.Debugf("[%v] getting member %v avatar", u.ID, m.ID)

View file

@ -17,25 +17,35 @@ type userExport struct {
Pronouns []db.PronounEntry `json:"pronouns"` Pronouns []db.PronounEntry `json:"pronouns"`
Fields []db.Field `json:"fields"` Fields []db.Field `json:"fields"`
Fediverse *string `json:"fediverse"`
FediverseUsername *string `json:"fediverse_username"`
FediverseInstance *string `json:"fediverse_instance"`
Discord *string `json:"discord"` Discord *string `json:"discord"`
DiscordUsername *string `json:"discord_username"` DiscordUsername *string `json:"discord_username"`
MaxInvites int `json:"max_invites"` MaxInvites int `json:"max_invites"`
Warnings []db.Warning `json:"warnings"`
} }
func dbUserToExport(u db.User, fields []db.Field) userExport { func dbUserToExport(u db.User, fields []db.Field, warnings []db.Warning) userExport {
return userExport{ return userExport{
ID: u.ID, ID: u.ID,
Username: u.Username, Username: u.Username,
DisplayName: u.DisplayName, DisplayName: u.DisplayName,
Bio: u.Bio, Bio: u.Bio,
Links: u.Links, Links: db.NotNull(u.Links),
Names: u.Names, Names: db.NotNull(u.Names),
Pronouns: u.Pronouns, Pronouns: db.NotNull(u.Pronouns),
Fields: fields, Fields: db.NotNull(fields),
Discord: u.Discord, Discord: u.Discord,
DiscordUsername: u.DiscordUsername, DiscordUsername: u.DiscordUsername,
MaxInvites: u.MaxInvites, MaxInvites: u.MaxInvites,
Fediverse: u.Fediverse,
FediverseUsername: u.FediverseUsername,
FediverseInstance: u.FediverseInstance,
Warnings: db.NotNull(warnings),
} }
} }
@ -48,6 +58,7 @@ type memberExport struct {
Names []db.FieldEntry `json:"names"` Names []db.FieldEntry `json:"names"`
Pronouns []db.PronounEntry `json:"pronouns"` Pronouns []db.PronounEntry `json:"pronouns"`
Fields []db.Field `json:"fields"` Fields []db.Field `json:"fields"`
Unlisted bool `json:"unlisted"`
} }
func dbMemberToExport(m db.Member, fields []db.Field) memberExport { func dbMemberToExport(m db.Member, fields []db.Field) memberExport {
@ -56,9 +67,10 @@ func dbMemberToExport(m db.Member, fields []db.Field) memberExport {
Name: m.Name, Name: m.Name,
DisplayName: m.DisplayName, DisplayName: m.DisplayName,
Bio: m.Bio, Bio: m.Bio,
Links: m.Links, Links: db.NotNull(m.Links),
Names: m.Names, Names: db.NotNull(m.Names),
Pronouns: m.Pronouns, Pronouns: db.NotNull(m.Pronouns),
Fields: fields, Fields: db.NotNull(fields),
Unlisted: m.Unlisted,
} }
} }