Пример #1
0
func UserFollow(user *models.User, theUser *models.User) {
	if err := models.GetById(theUser.Id, theUser); err != nil {
		var mutual bool
		tFollow := models.Follow{UserId: theUser.Id, FollowUserId: user.Id}
		if err := models.GetByExample(&tFollow); err == nil {
			mutual = true
		}

		follow := models.Follow{UserId: user.Id, FollowUserId: theUser.Id, Mutual: mutual}
		if err := models.Insert(&follow); err == nil && mutual {
			tFollow.Mutual = mutual
			models.UpdateById(tFollow.Id, &tFollow, "mutual")
		}

		if nums, err := models.Count(&models.Follow{UserId: user.Id}); err == nil {
			user.Following = int(nums)
			models.UpdateById(user.Id, user, "following")
		}

		if nums, err := models.Count(&models.Follow{FollowUserId: theUser.Id}); err == nil {
			theUser.Followers = int(nums)
			models.UpdateById(theUser.Id, theUser, "followers")
		}
	}
}
Пример #2
0
func (this *BulletinAdminEdit) Post() {
	form := bulletin.BulletinAdminForm{Id: int(this.object.Id)}
	if this.ValidFormSets(&form) == false {
		return
	}

	// get changed field names
	changes := utils.FormChanges(&this.object, &form)

	url := fmt.Sprintf("/admin/bulletin/%d", this.object.Id)

	// update changed fields only
	if len(changes) > 0 {
		form.SetToBulletin(&this.object)
		if err := models.UpdateById(this.object.Id, this.object, models.Obj2Table(changes)...); err == nil {
			this.FlashRedirect(url, 302, "UpdateSuccess")
			return
		} else {
			log.Error(err)
			this.Data["Error"] = err
		}
	} else {
		this.Redirect(url, 302)
	}
}
Пример #3
0
// view for update object
func (this *PostAdminEdit) Post() {
	form := this.GetForm(false)
	if this.ValidFormSets(&form) == false {
		return
	}

	// get changed field names
	changes := utils.FormChanges(&this.object, &form)

	url := fmt.Sprintf("/admin/post/%d", this.object.Id)

	// update changed fields only
	if len(changes) > 0 {
		//fix the bug of category not updated
		changes = append(changes, "Category")
		form.SetToPost(&this.object)
		if err := models.UpdateById(this.object.Id, this.object, models.Obj2Table(changes)...); err == nil {
			this.FlashRedirect(url, 302, "UpdateSuccess")
			return
		} else {
			log.Error(err)
			this.Data["Error"] = err
		}
	} else {
		this.Redirect(url, 302)
	}
}
Пример #4
0
func (form *CommentForm) SaveComment(comment *models.Comment, user *models.User, post *models.Post) error {
	comment.Message = form.Message
	comment.MessageCache = utils.RenderMarkdown(form.Message)
	comment.UserId = user.Id
	comment.PostId = post.Id
	if err := models.InsertComment(comment); err == nil {
		post.LastReplyId = user.Id
		models.UpdateById(post.Id, post, "last_reply_id", "last_replied")

		cnt, _ := models.CountCommentsLTEId(comment.Id)
		comment.Floor = int(cnt)
		return models.UpdateById(comment.Id, comment, "floor")
	} else {
		return err
	}
}
Пример #5
0
func (form *PostForm) UpdatePost(post *models.Post, user *models.User) error {
	changes := utils.FormChanges(post, form)
	if len(changes) == 0 {
		return nil
	}
	utils.SetFormValues(form, post)
	post.CategoryId = form.Category
	post.TopicId = form.Topic
	for _, c := range changes {
		if c == "Content" {
			post.ContentCache = utils.RenderMarkdown(form.Content)
			changes = append(changes, "ContentCache")
		}
	}

	// update last edit author
	if post.LastAuthorId != user.Id {
		post.LastAuthorId = user.Id
		changes = append(changes, "LastAuthor")
	}

	changes = append(changes, "Updated")

	return models.UpdateById(post.Id, post, models.Obj2Table(changes)...)
}
Пример #6
0
// Active implemented check Email actice code.
func (this *RegisterActive) Get() {
	// no need active
	if this.CheckActiveRedirect(false) {
		return
	}

	code := this.Params().Get(":code")

	var user models.User

	if auth.VerifyUserActiveCode(&user, code) {
		user.IsActive = true
		user.Rands = models.GetUserSalt()
		if err := models.UpdateById(user.Id, user, models.Obj2Table([]string{"IsActive", "Rands", "Updated"})...); err != nil {
			log.Error("Active: user Update ", err)
		}
		if this.IsLogin {
			this.User = user
		}

		this.Redirect("/active/success", 302)

	} else {
		this.Data["Success"] = false
	}

	this.Render("auth/active.html", this.Data)
}
Пример #7
0
func UserUnFollow(user *models.User, theUser *models.User) {
	follow := &models.Follow{UserId: user.Id, FollowUserId: theUser.Id}
	num, _ := models.ORM().Delete(follow)
	if num > 0 {
		models.ORM().UseBool().Update(&models.Follow{}, follow)

		if nums, err := models.Count(&models.Follow{UserId: user.Id}); err == nil {
			user.Following = int(nums)
			models.UpdateById(user.Id, user, "following")
		}

		if nums, err := models.Count(&models.Follow{FollowUserId: theUser.Id}); err == nil {
			theUser.Followers = int(nums)
			models.UpdateById(theUser.Id, theUser, "followers")
		}
	}
}
Пример #8
0
func PostReplysCount(post *models.Post) {
	cnt, err := models.CountCommentsByPostId(post.Id)
	if err == nil {
		post.Replys = int(cnt)
		//disable post editable
		post.CanEdit = false
		err = models.UpdateById(post.Id, post, "replys", "can_edit")
	}
	if err != nil {
		log.Error("PostReplysCount ", err)
	}
}
Пример #9
0
func (form *ProfileForm) SaveUserProfile(user *models.User) error {
	// set md5 value if the value is an email
	if strings.IndexRune(form.GrEmail, '@') != -1 {
		form.GrEmail = utils.EncodeMd5(form.GrEmail)
	}

	changes := utils.FormChanges(user, form)
	if len(changes) > 0 {
		// if email changed then need re-active
		if user.Email != form.Email {
			user.IsActive = false
			changes = append(changes, "IsActive")
		}

		utils.SetFormValues(form, user)
		return models.UpdateById(user.Id, user, changes...)
	}
	return nil
}
Пример #10
0
func SaveImage(m *models.Image, r io.ReadSeeker, mime string, filename string, created time.Time) error {
	var ext string

	// test image mime type
	switch mime {
	case "image/jpeg":
		ext = ".jpg"

	case "image/png":
		ext = ".png"

	case "image/gif":
		ext = ".gif"

	default:
		ext = filepath.Ext(filename)
		switch ext {
		case ".jpg", ".png", ".gif":
		default:
			return fmt.Errorf("unsupport image format `%s`", filename)
		}
	}

	// decode image
	var img image.Image
	var err error
	switch ext {
	case ".jpg":
		m.Ext = 1
		img, err = jpeg.Decode(r)
	case ".png":
		m.Ext = 2
		img, err = png.Decode(r)
	case ".gif":
		m.Ext = 3
		img, err = gif.Decode(r)
	}

	if err != nil {
		return err
	}

	m.Width = img.Bounds().Dx()
	m.Height = img.Bounds().Dy()
	m.Created = created

	if err := models.Insert(m); err != nil || m.Id <= 0 {
		return err
	}

	m.Token = m.GetToken()
	if err := models.UpdateById(m.Id, m); err != nil {
		return err
	}

	path := GenImagePath(m)
	os.MkdirAll(path, 0755)

	fullPath := GenImageFilePath(m, 0)
	if _, err := r.Seek(0, 0); err != nil {
		return err
	}

	var file *os.File
	if f, err := os.OpenFile(fullPath, os.O_CREATE|os.O_TRUNC|os.O_WRONLY, 0644); err != nil {
		return err
	} else {
		file = f
	}

	if _, err := io.Copy(file, r); err != nil {
		os.RemoveAll(fullPath)
		return err
	}

	if ext != ".gif" {

		if m.Width > setting.ImageSizeSmall {
			if err := ImageResize(m, img, setting.ImageSizeSmall); err != nil {
				os.RemoveAll(fullPath)
				return err
			}
		}

		if m.Width > setting.ImageSizeMiddle {
			if err := ImageResize(m, img, setting.ImageSizeMiddle); err != nil {
				os.RemoveAll(fullPath)
				return err
			}
		}

	}

	return nil
}
Пример #11
0
func SaveImageToQiniu(m *models.Image, r goio.ReadSeeker, mime string, filename string, created time.Time, bucketName string) error {
	var ext string

	// check image mime type
	switch mime {
	case "image/jpeg":
		ext = ".jpg"

	case "image/png":
		ext = ".png"

	case "image/gif":
		ext = ".gif"

	default:
		ext = filepath.Ext(filename)
		switch ext {
		case ".jpg", ".png", ".gif":
		default:
			return fmt.Errorf("unsupport image format `%s`", filename)
		}
	}

	// decode image
	var img image.Image
	var err error
	switch ext {
	case ".jpg":
		m.Ext = 1
		img, err = jpeg.Decode(r)
	case ".png":
		m.Ext = 2
		img, err = png.Decode(r)
	case ".gif":
		m.Ext = 3
		img, err = gif.Decode(r)
	}

	if err != nil {
		return err
	}

	m.Width = img.Bounds().Dx()
	m.Height = img.Bounds().Dy()
	m.Created = created

	//save to database
	if err := models.Insert(m); err != nil || m.Id <= 0 {
		return err
	}

	m.Token = m.GetToken()
	if err := models.UpdateById(m.Id, m); err != nil {
		return err
	}

	var key = m.Token

	//reset reader pointer
	if _, err := r.Seek(0, 0); err != nil {
		return err
	}

	//save to qiniu
	var uptoken = utils.GetQiniuUptoken(bucketName)
	var putRet io.PutRet
	var putExtra = &io.PutExtra{
		MimeType: mime,
	}

	err = io.Put(nil, &putRet, uptoken, key, r, putExtra)
	if err != nil {
		return err
	}

	return nil
}
Пример #12
0
func (this *Post) Post() {
	if this.CheckActiveRedirect() {
		return
	}

	if !this.IsAjax() {
		return
	}

	result := map[string]interface{}{
		"success": false,
	}
	action := this.GetString("action")
	switch action {
	case "toggle-best":
		if this.User.IsAdmin {
			if postId, err := this.GetInt("post"); err == nil {
				//set post best
				var post models.Post
				if err := models.GetById(postId, &post); err == nil {
					post.IsBest = !post.IsBest
					if models.UpdateById(post.Id, post, "is_best") == nil {
						result["success"] = true
					}
				}
			} else {
				this.Logger.Error("post value is not int:", this.GetString("post"))
			}
		}
	case "toggle-fav":
		if postId, err := this.GetInt("post"); err == nil {
			var post models.Post
			if err := models.GetById(postId, &post); err == nil {
				var favoritePost = models.FavoritePost{
					PostId: post.Id,
					UserId: this.User.Id,
				}

				if err := models.GetByExample(&favoritePost); err == nil {
					//toogle IsFav
					favoritePost.IsFav = !favoritePost.IsFav
					if models.UpdateById(favoritePost.Id, favoritePost, "is_fav") == nil {
						//update user fav post count
						if favoritePost.IsFav {
							this.User.FavPosts += 1
						} else {
							this.User.FavPosts -= 1

						}
						if models.UpdateById(this.User.Id, this.User, "fav_posts") == nil {
							result["success"] = true
						}
					}
				} else if err == models.ErrNotExist {
					favoritePost = models.FavoritePost{
						UserId: this.User.Id,
						PostId: post.Id,
						IsFav:  true,
					}
					if models.Insert(favoritePost) == nil {
						//update user fav post count
						this.User.FavPosts += 1
						if models.UpdateById(this.User.Id, this.User, "fav_posts") == nil {
							result["success"] = true
						}
					}
				} else {
					this.Logger.Error("Get favorite post err:", err)
				}
			}
		}
	}
	this.Data["json"] = result
	this.ServeJson(this.Data)
}
Пример #13
0
//upload user avatar
func UploadUserAvatarToQiniu(r io.ReadSeeker, filename string, mime string, bucketName string, user *models.User) error {
	var ext string

	// test image mime type
	switch mime {
	case "image/jpeg":
		ext = ".jpg"

	case "image/png":
		ext = ".png"

	case "image/gif":
		ext = ".gif"

	default:
		ext = filepath.Ext(filename)
		switch ext {
		case ".jpg", ".png", ".gif":
		default:
			return fmt.Errorf("unsupport image format `%s`", filename)
		}
	}

	// decode image
	var err error
	switch ext {
	case ".jpg":
		_, err = jpeg.Decode(r)
	case ".png":
		_, err = png.Decode(r)
	case ".gif":
		_, err = gif.Decode(r)
	}

	if err != nil {
		return err
	}

	//reset reader pointer
	if _, err := r.Seek(0, 0); err != nil {
		return err
	}
	var data []byte
	if data, err = ioutil.ReadAll(r); err != nil {
		return err
	}

	if len(data) > setting.AvatarImageMaxLength {
		return fmt.Errorf("avatar image size too large", filename)
	}

	//reset reader pointer again
	if _, err := r.Seek(0, 0); err != nil {
		return err
	}

	//save to qiniu
	var uptoken = utils.GetQiniuUptoken(bucketName)
	var putRet qio.PutRet
	var putExtra = &qio.PutExtra{
		MimeType: mime,
	}

	err = qio.PutWithoutKey(nil, &putRet, uptoken, r, putExtra)
	if err != nil {
		return err
	}

	//update user
	user.AvatarKey = putRet.Key
	if err := models.UpdateById(user.Id, user, "avatar_key", "updated"); err != nil {
		return err
	}
	return nil
}
Пример #14
0
//set a new avatar type to user
func SaveAvatarType(user *models.User, avatarType int) error {
	user.AvatarType = avatarType
	return models.UpdateById(user.Id, user, "avatar_type", "updated")
}
Пример #15
0
// set a new password to user
func SaveNewPassword(user *models.User, password string) error {
	salt := models.GetUserSalt()
	user.Password = fmt.Sprintf("%s$%s", salt, utils.EncodePassword(password, salt))
	return models.UpdateById(user.Id, user, "password", "rands", "updated")
}