Beispiel #1
0
func (bs *BootstrapService) Install(v interface{}) (*Result, error) {
	opt, ok := v.(BootstrapInstallOption)
	if !ok {
		return nil, ErrServiceFuncNeedType(bs.Install, opt)
	}

	// create tables
	if err := core.Db.Sync2(new(model.User),
		new(model.UserToken),
		new(model.Theme),
		new(model.Article),
		new(model.ArticleTag),
		new(model.Setting),
		new(model.Media),
		new(model.Page),
		new(model.Comment),
		new(model.Message)); err != nil {
		return nil, err
	}

	// insert default user
	user := &model.User{
		Name:      opt.AdminUser,
		Email:     opt.AdminEmail,
		Nick:      opt.AdminUser,
		Profile:   "this is administrator",
		Role:      model.USER_ROLE_ADMIN,
		Status:    model.USER_STATUS_ACTIVE,
		AvatarUrl: utils.Gravatar("*****@*****.**"),
	}
	user.SetPassword(opt.AdminPassword)
	if _, err := core.Db.Insert(user); err != nil {
		return nil, err
	}

	// insert default themes
	themes := []interface{}{
		&model.Theme{
			Name:      "admin",
			Author:    core.PUGO_AUTHOR,
			Version:   "1.0",
			Directory: path.Join(core.ThemeDirectory, "admin"),
			Status:    model.THEME_STATUS_LOCKED,
		},
		&model.Theme{
			Name:      "default",
			Author:    core.PUGO_AUTHOR,
			Version:   "1.0",
			Directory: path.Join(core.ThemeDirectory, "default"),
			Status:    model.THEME_STATUS_CURRENT,
		},
	}
	if _, err := core.Db.Insert(themes...); err != nil {
		return nil, err
	}

	// insert settings
	generalSetting := &model.SettingGeneral{
		Title:          "PUGO",
		SubTitle:       "Simple Blog Engine",
		Keyword:        "pugo,blog,go,golang",
		Description:    "PUGO is a simple blog engine by golang",
		HostName:       fmt.Sprintf("http://%s", opt.Domain),
		HeroImage:      "/img/bg.png",
		TopAvatarImage: "/img/logo.png",
	}
	setting := &model.Setting{
		Name:   "general",
		UserId: 0,
		Type:   model.SETTING_TYPE_GENERAL,
	}
	setting.Encode(generalSetting)
	if _, err := core.Db.Insert(setting); err != nil {
		return nil, err
	}
	Setting.General = generalSetting

	mediaSetting := &model.SettingMedia{
		MaxFileSize: 10 * 1024,
		ImageFile:   []string{"jpg", "jpeg", "png", "gif", "bmp", "vbmp"},
		DocFile:     []string{"txt", "doc", "docx", "xls", "xlsx", "ppt", "pptx", "pdf"},
		CommonFile:  []string{"zip", "rar"},
		DynamicLink: false,
	}
	setting = &model.Setting{
		Name:   "media",
		UserId: 0,
		Type:   model.SETTING_TYPE_MEDIA,
	}
	setting.Encode(mediaSetting)
	if _, err := core.Db.Insert(setting); err != nil {
		return nil, err
	}
	Setting.Media = mediaSetting

	contentSetting := &model.SettingContent{
		PageSize:         5,
		RSSFullText:      true,
		RSSNumberLimit:   0,
		TopPage:          0,
		PageDisallowLink: []string{"article", "archive", "feed", "comment", "admin", "sitemap"},
	}
	setting = &model.Setting{
		Name:   "content",
		UserId: 0,
		Type:   model.SETTING_TYPE_CONTENT,
	}
	setting.Encode(contentSetting)
	if _, err := core.Db.Insert(setting); err != nil {
		return nil, err
	}
	Setting.Content = contentSetting

	commentSetting := &model.SettingComment{
		IsPager:        false,
		PageSize:       10,
		Order:          "create_time DESC",
		CheckAll:       false,
		CheckNoPass:    true,
		CheckRefer:     true,
		AutoCloseDay:   30,
		SubmitDuration: 60,
		MaxLength:      512,
		MinLength:      2,
	}
	setting = &model.Setting{
		Name:   "comment",
		UserId: 0,
		Type:   model.SETTING_TYPE_COMMENT,
	}
	setting.Encode(commentSetting)
	if _, err := core.Db.Insert(setting); err != nil {
		return nil, err
	}
	Setting.Comment = commentSetting

	menuSettings := []*model.SettingMenu{
		&model.SettingMenu{
			"Home", "/", "Home",
			false, "home",
		},
		&model.SettingMenu{
			"Archive", "/archive", "Archive",
			false, "archive",
		},
		&model.SettingMenu{
			"About", "/about.html", "About",
			false, "about",
		},
	}
	setting = &model.Setting{
		Name:   "menu",
		UserId: 0,
		Type:   model.SETTING_TYPE_MENU,
	}
	setting.Encode(menuSettings)
	if _, err := core.Db.Insert(setting); err != nil {
		return nil, err
	}
	Setting.Menu = menuSettings

	// first article
	article := &model.Article{
		UserId:        user.Id,
		Title:         firstArticleTitle,
		Link:          firstArticleLink,
		Body:          firstArticleContent,
		TagString:     firstArticleTag,
		Status:        model.ARTICLE_STATUS_PUBLISH,
		CommentStatus: model.ARTICLE_COMMENT_OPEN,
		Hits:          1,
		Preview:       firstArticleContent,
		BodyType:      model.ARTICLE_BODY_MARKDOWN,
	}
	if _, err := Article.Write(article); err != nil {
		return nil, err
	}

	// first comment
	cmt := &model.Comment{
		Name:      user.Name,
		UserId:    user.Id,
		Email:     user.Email,
		Url:       user.Url,
		AvatarUrl: user.AvatarUrl,
		Body:      firstCommentContent,
		Status:    model.COMMENT_STATUS_APPROVED,
		From:      model.COMMENT_FROM_ARTICLE,
		FromId:    article.Id,
		ParentId:  0,
	}
	if _, err := Comment.Save(cmt); err != nil {
		return nil, err
	}

	// first page
	page := &model.Page{
		UserId:        user.Id,
		Title:         firstPageTitle,
		Link:          firstPageLink,
		Body:          firstPageContent,
		Status:        model.PAGE_STATUS_PUBLISH,
		CommentStatus: model.PAGE_COMMENT_OPEN,
		Hits:          1,
		Template:      "page.tmpl",
		BodyType:      model.PAGE_BODY_MARKDOWN,
		TopLink:       true,
	}
	if _, err := Page.Write(page); err != nil {
		return nil, err
	}

	// assign install time to config
	core.Cfg.Http.Port = opt.Port
	core.Cfg.Http.Domain = opt.Domain
	core.Cfg.Install = fmt.Sprint(time.Now().Unix())
	if err := core.Cfg.WriteToFile(core.ConfigFile); err != nil {
		return nil, err
	}
	return nil, nil
}
Beispiel #2
0
func (cs *CommentService) Create(v interface{}) (*Result, error) {
	opt, ok := v.(CommentCreateOption)
	if !ok {
		return nil, ErrServiceFuncNeedType(cs.Create, opt)
	}
	c := &model.Comment{
		Name:      opt.Name,
		UserId:    opt.UserId,
		Email:     opt.Email,
		Url:       opt.Url,
		AvatarUrl: utils.Gravatar(opt.Email),
		Body:      opt.Content,
		Status:    model.COMMENT_STATUS_WAIT,
		//From     int   `xorm:"INT(8) index(from)" json:"-"`
		FromId:   opt.Id,
		ParentId: opt.ParentId,
	}

	// filter content
	if len(c.Body) < Setting.Comment.MinLength {
		return nil, ErrCommentContentTooShort
	}
	if len(c.Body) > Setting.Comment.MaxLength {
		return nil, ErrCommentContentTooLong
	}
	if strings.Contains(c.Body, "href=") {
		return nil, ErrCommentContentHref
	}

	// set origin
	if opt.Type == "article" {
		c.From = model.COMMENT_FROM_ARTICLE
	}
	if opt.Type == "page" {
		c.From = model.COMMENT_FROM_PAGE
	}
	if c.From == 0 {
		return nil, ErrCommentOriginMissing
	}

	// check refer
	if Setting.Comment.CheckRefer {
		// todo : check refer
	}

	// try to read user
	if opt.UserId == 0 && opt.Email != "" {
		if user, _ := getUserBy("email", opt.Email); user != nil && user.Id > 0 {
			c.UserId = user.Id
		}
	}

	// update status
	if Setting.Comment.CheckAll {
		// check all comment
		return newResult(cs.Create, c), nil
	}
	if Setting.Comment.CheckNoPass {
		count, err := cs.countEmailComment(c.Email)
		if err != nil {
			return nil, err
		}
		if count > 0 {
			c.Status = model.COMMENT_STATUS_APPROVED
		}
	}
	if c.UserId != 0 { // known user should approve
		c.Status = model.COMMENT_STATUS_APPROVED
	}

	return newResult(cs.Create, c), nil
}