Browse Source

fix random avatars (#1147)

Lunny Xiao 3 years ago
parent
commit
5d40db629c
2 changed files with 11 additions and 1 deletions
  1. 3 1
      models/org.go
  2. 8 0
      models/user.go

+ 3 - 1
models/org.go

@@ -135,7 +135,9 @@ func CreateOrganization(org, owner *User) (err error) {
135 135
 	if _, err = sess.Insert(org); err != nil {
136 136
 		return fmt.Errorf("insert organization: %v", err)
137 137
 	}
138
-	org.GenerateRandomAvatar()
138
+	if err = org.generateRandomAvatar(sess); err != nil {
139
+		return fmt.Errorf("generate random avatar: %v", err)
140
+	}
139 141
 
140 142
 	// Add initial creator to organization and owner team.
141 143
 	if _, err = sess.Insert(&OrgUser{

+ 8 - 0
models/user.go

@@ -287,6 +287,10 @@ func (u *User) CustomAvatarPath() string {
287 287
 
288 288
 // GenerateRandomAvatar generates a random avatar for user.
289 289
 func (u *User) GenerateRandomAvatar() error {
290
+	return u.generateRandomAvatar(x)
291
+}
292
+
293
+func (u *User) generateRandomAvatar(e Engine) error {
290 294
 	seed := u.Email
291 295
 	if len(seed) == 0 {
292 296
 		seed = u.Name
@@ -308,6 +312,10 @@ func (u *User) GenerateRandomAvatar() error {
308 312
 	}
309 313
 	defer fw.Close()
310 314
 
315
+	if _, err := e.Id(u.ID).Cols("avatar").Update(u); err != nil {
316
+		return err
317
+	}
318
+
311 319
 	if err = png.Encode(fw, img); err != nil {
312 320
 		return fmt.Errorf("Encode: %v", err)
313 321
 	}