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)
jsonBuffer := new(bytes.Buffer)
encoder := json.NewEncoder(jsonBuffer)
encoder.SetEscapeHTML(false)
encoder.SetIndent("", " ")
outBuffer := new(bytes.Buffer)
zw := zip.NewWriter(outBuffer)
defer zw.Close()
@ -136,19 +141,28 @@ func (s *server) doExport(u db.User) {
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)
ub, err := json.Marshal(dbUserToExport(u, fields))
err = encoder.Encode(dbUserToExport(u, fields, warnings))
if err != nil {
log.Errorf("[%v] marshaling user: %v", u.ID, err)
return
}
_, err = w.Write(ub)
_, err = io.Copy(w, jsonBuffer)
if err != nil {
log.Errorf("[%v] writing user: %v", u.ID, err)
return
}
jsonBuffer.Reset()
if u.Avatar != nil {
log.Debugf("[%v] getting user avatar", u.ID)
@ -196,17 +210,18 @@ func (s *server) doExport(u db.User) {
return
}
mb, err := json.Marshal(dbMemberToExport(m, fields))
err = encoder.Encode(dbMemberToExport(m, fields))
if err != nil {
log.Errorf("[%v] marshaling member %v: %v", u.ID, m.ID, err)
return
}
_, err = w.Write(mb)
_, err = io.Copy(w, jsonBuffer)
if err != nil {
log.Errorf("[%v] writing member %v json: %v", u.ID, m.ID, err)
return
}
jsonBuffer.Reset()
if m.Avatar != nil {
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"`
Fields []db.Field `json:"fields"`
Fediverse *string `json:"fediverse"`
FediverseUsername *string `json:"fediverse_username"`
FediverseInstance *string `json:"fediverse_instance"`
Discord *string `json:"discord"`
DiscordUsername *string `json:"discord_username"`
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{
ID: u.ID,
Username: u.Username,
DisplayName: u.DisplayName,
Bio: u.Bio,
Links: u.Links,
Names: u.Names,
Pronouns: u.Pronouns,
Fields: fields,
Links: db.NotNull(u.Links),
Names: db.NotNull(u.Names),
Pronouns: db.NotNull(u.Pronouns),
Fields: db.NotNull(fields),
Discord: u.Discord,
DiscordUsername: u.DiscordUsername,
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"`
Pronouns []db.PronounEntry `json:"pronouns"`
Fields []db.Field `json:"fields"`
Unlisted bool `json:"unlisted"`
}
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,
DisplayName: m.DisplayName,
Bio: m.Bio,
Links: m.Links,
Names: m.Names,
Pronouns: m.Pronouns,
Fields: fields,
Links: db.NotNull(m.Links),
Names: db.NotNull(m.Names),
Pronouns: db.NotNull(m.Pronouns),
Fields: db.NotNull(fields),
Unlisted: m.Unlisted,
}
}