Example #1
0
func (_ *CommentApi) Save(v interface{}) *Res {
	form, ok := v.(*CommentForm)
	if !ok {
		return Fail(paramTypeError(form))
	}

	c := &model.Comment{
		Name:      form.Name,
		UserId:    form.UserId,
		Email:     form.Email,
		Url:       form.Url,
		AvatarUrl: utils.GravatarLink(form.Email),
		Body:      utils.Nl2BrString(form.Body),
		From:      form.For,
		FromId:    form.ForId,
		ParentId:  form.Parent,
		Status:    model.COMMENT_STATUS_WAIT,
	}
	if model.CountApprovedCommentsByEmail(form.Email) > 0 {
		c.Status = model.COMMENT_STATUS_APPROVED
	}
	if err := model.SaveComment(c); err != nil {
		return Fail(err)
	}
	return Success(map[string]interface{}{
		"comment": c,
	})
}
Example #2
0
// new site init data
func NewSiteInitData(engine *xorm.Engine) {
	// default user
	user := &model.User{
		Name:      "admin",
		Nick:      "admin",
		Email:     "*****@*****.**",
		Url:       "#",
		AvatarUrl: utils.GravatarLink("*****@*****.**"),
		Profile:   "this is an administrator",
		Role:      model.USER_ROLE_ADMIN,
		Status:    model.USER_STATUS_ACTIVE,
	}
	user.Salt = utils.Md5String("123456789")[8:24]
	user.Password = utils.Sha256String("123456789" + user.Salt)
	if _, err := engine.Insert(user); err != nil {
		log.Error("NewSite | %s", err.Error())
		return
	}

	// default article
	article := &model.Article{
		Title:         "Welcome to Purine",
		Link:          "welcome-to-purine",
		Preview:       blogPreview,
		Body:          blogContent,
		TagString:     "blog",
		Hits:          1,
		Comments:      0,
		Status:        model.ARTICLE_STATUS_PUBLISH,
		CommentStatus: model.ARTICLE_COMMENT_OPEN,
		AuthorId:      user.Id,
	}
	if _, err := engine.Insert(article); err != nil {
		log.Error("NewSite | %s", err.Error())
		return
	}

	// default settings
	settings := make([]interface{}, 0)
	settings = append(settings, &model.Setting{"title", "Purine", 0})
	settings = append(settings, &model.Setting{"subtitle", "a simple blog engine", 0})
	settings = append(settings, &model.Setting{"desc", "a simple blog engine by golang", 0})
	settings = append(settings, &model.Setting{"keyword", "purine,blog,golang", 0})
	settings = append(settings, &model.Setting{"theme", "default", 0})
	settings = append(settings, &model.Setting{"baseurl", "http://localhost:9999/", 0})
	settings = append(settings, &model.Setting{"media_imageext", ".jpg,.jpeg,.png,.gif", 0})
	settings = append(settings, &model.Setting{"media_fileext", ".txt,.zip,.doc,.xls,.ppt,.pdf", 0})
	settings = append(settings, &model.Setting{"media_nameformat", ":hash", 0})
	settings = append(settings, &model.Setting{"media_maxsize", strconv.Itoa(2 * 1024 * 1024), 0})
	if _, err := engine.Insert(settings...); err != nil {
		log.Error("NewSite | %s", err.Error())
		return
	}
}
Example #3
0
// update user profile
//
//  in  : *UserProfileForm
//  out : {
//          "user":*User,
//        }
//
func (_ *UserApi) UpdateProfile(v interface{}) *Res {
	form, ok := v.(*UserProfileForm)
	if !ok {
		return Fail(paramTypeError(new(UserProfileForm)))
	}
	u := &model.User{
		Name:    form.User,
		Nick:    form.Nick,
		Email:   form.Email,
		Url:     form.Url,
		Profile: form.Profile,
		Id:      form.Id,
	}
	u.AvatarUrl = utils.GravatarLink(u.Email)
	if u.Url == "" {
		u.Url = "#"
	}

	u2, err := model.GetUserBy("email", u.Email)
	if err != nil {
		return Fail(err)
	}
	if u2 != nil && u2.Id != u.Id {
		return Fail(ERR_USER_EMAIL)
	}

	if err := model.UpdateUser(u); err != nil {
		return Fail(err)
	}
	u, err = model.GetUserBy("id", u.Id)
	if err != nil {
		return Fail(err)
	}
	return Success(map[string]interface{}{
		"user": u,
	})
}