From 5d40db629cb3a9ffdb7cdaa759f80c253c5ea428 Mon Sep 17 00:00:00 2001
From: Lunny Xiao <xiaolunwen@gmail.com>
Date: Wed, 8 Mar 2017 23:05:15 +0800
Subject: [PATCH] fix random avatars (#1147)

---
 models/org.go  | 4 +++-
 models/user.go | 8 ++++++++
 2 files changed, 11 insertions(+), 1 deletion(-)

diff --git a/models/org.go b/models/org.go
index d6cbcc9658..9d7aaec7ef 100644
--- a/models/org.go
+++ b/models/org.go
@@ -135,7 +135,9 @@ func CreateOrganization(org, owner *User) (err error) {
 	if _, err = sess.Insert(org); err != nil {
 		return fmt.Errorf("insert organization: %v", err)
 	}
-	org.GenerateRandomAvatar()
+	if err = org.generateRandomAvatar(sess); err != nil {
+		return fmt.Errorf("generate random avatar: %v", err)
+	}
 
 	// Add initial creator to organization and owner team.
 	if _, err = sess.Insert(&OrgUser{
diff --git a/models/user.go b/models/user.go
index fc78c5800f..54a60acd84 100644
--- a/models/user.go
+++ b/models/user.go
@@ -287,6 +287,10 @@ func (u *User) CustomAvatarPath() string {
 
 // GenerateRandomAvatar generates a random avatar for user.
 func (u *User) GenerateRandomAvatar() error {
+	return u.generateRandomAvatar(x)
+}
+
+func (u *User) generateRandomAvatar(e Engine) error {
 	seed := u.Email
 	if len(seed) == 0 {
 		seed = u.Name
@@ -308,6 +312,10 @@ func (u *User) GenerateRandomAvatar() error {
 	}
 	defer fw.Close()
 
+	if _, err := e.Id(u.ID).Cols("avatar").Update(u); err != nil {
+		return err
+	}
+
 	if err = png.Encode(fw, img); err != nil {
 		return fmt.Errorf("Encode: %v", err)
 	}