Exemplo n.º 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")
		}
	}
}
Exemplo n.º 2
0
// register create user
func RegisterUser(user *models.User, username, email, password string, locale i18n.Locale) error {
	// use random salt encode password
	salt := models.GetUserSalt()
	pwd := utils.EncodePassword(password, salt)

	user.UserName = strings.ToLower(username)
	user.Email = strings.ToLower(email)

	// save salt and encode password, use $ as split char
	user.Password = fmt.Sprintf("%s$%s", salt, pwd)

	// save md5 email value for gravatar
	user.GrEmail = utils.EncodeMd5(user.Email)

	// Use username as default nickname.
	user.NickName = user.UserName

	//set default language
	if locale.Lang == "en-US" {
		user.Lang = setting.LangEnUS
	} else {
		user.Lang = setting.LangZhCN
	}

	//set default avatar
	user.AvatarType = setting.AvatarTypeGravatar
	return models.Insert(user)
}
Exemplo n.º 3
0
func (this *BulletinAdminNew) Post() {
	form := bulletin.BulletinAdminForm{Create: true}
	if this.ValidFormSets(&form) == false {
		return
	}

	var bulletin models.Bulletin
	form.SetToBulletin(&bulletin)
	if err := models.Insert(&bulletin); err == nil {
		this.FlashRedirect(fmt.Sprintf("/admin/bulletin/%d", bulletin.Id), 302, "CreateSuccess")
		return
	} else {
		log.Error(err)
		this.Data["Error"] = err
	}
}
Exemplo n.º 4
0
// view for new object save
func (this *CommentAdminNew) Post() {
	form := post.CommentAdminForm{Create: true}
	if this.ValidFormSets(&form) == false {
		return
	}

	var comment models.Comment
	form.SetToComment(&comment)
	if err := models.Insert(&comment); err == nil {
		this.FlashRedirect(fmt.Sprintf("/admin/comment/%d", comment.Id), 302, "CreateSuccess")
		return
	} else {
		log.Error(err)
		this.Data["Error"] = err
	}
}
Exemplo n.º 5
0
// view for new object save
func (this *PostAdminNew) Post() {
	form := this.GetForm(true)
	if !this.ValidFormSets(&form) {
		return
	}

	var post models.Post
	form.SetToPost(&post)
	if err := models.Insert(&post); err == nil {
		this.FlashRedirect(fmt.Sprintf("/admin/post/%d", post.Id), 302, "CreateSuccess")
		return
	} else {
		log.Error(err)
		this.Data["Error"] = err
	}
}
Exemplo n.º 6
0
// view for new object save
func (this *PageAdminNew) Post() {
	form := page.PageAdminForm{Create: true}
	if !this.ValidFormSets(&form) {
		return
	}

	var a models.Page
	form.SetToPage(&a)
	if err := models.Insert(&a); err == nil {
		this.FlashRedirect(fmt.Sprintf("/admin/page/%d", a.Id), 302, "CreateSuccess")
		return
	} else {
		log.Error(err)
		this.Data["Error"] = err
	}
}
Exemplo n.º 7
0
// view for new object save
func (this *UserAdminNew) Post() {
	form := auth.UserAdminForm{Create: true}
	if this.ValidFormSets(&form) == false {
		return
	}

	var user models.User
	form.SetToUser(&user)
	if err := models.Insert(&user); err == nil {
		this.FlashRedirect(fmt.Sprintf("/admin/user/%d", user.Id), 302, "CreateSuccess")
		return
	} else {
		log.Error(err)
		this.Data["Error"] = err
	}
}
Exemplo n.º 8
0
// view for new object save
func (this *TopicAdminNew) Post() {
	form := post.TopicAdminForm{Create: true}
	if this.ValidFormSets(&form) == false {
		return
	}

	var topic models.Topic
	form.SetToTopic(&topic)
	if err := models.Insert(&topic); err == nil {
		this.FlashRedirect(fmt.Sprintf("/admin/topic/%d", topic.Id), 302, "CreateSuccess")
		return
	} else {
		log.Error(err)
		this.Data["Error"] = err
	}
}
Exemplo n.º 9
0
// Add this topic into favorite list
func (this *Topic) Post() {
	slug := this.Params().Get(":slug")
	result := map[string]interface{}{
		"success": false,
	}

	topic, err := models.GetTopicBySlug(slug)
	if err != nil {
		return
	}

	if this.IsAjax() {
		action := this.GetString("action")
		switch action {
		case "favorite":
			if this.IsLogin {
				has, err := models.HasUserFollowTopic(int64(this.User.Id), topic.Id)
				if err != nil {
					log.Error("get follow user error:", err)
					return
				}

				if has {
					err = models.DeleteFollowTopic(int64(this.User.Id), topic.Id)
				} else {
					fav := models.FollowTopic{UserId: int64(this.User.Id), TopicId: topic.Id}
					err = models.Insert(&fav)
				}

				if err != nil {
					return
				}

				//TODO: add back
				//topic.RefreshFollowers()
				//this.User.RefreshFavTopics()
				result["success"] = true
			}
		}
	}

	this.Data["json"] = result
	this.ServeJson(this.Data)
}
Exemplo n.º 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
}
Exemplo n.º 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
}
Exemplo n.º 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)
}