示例#1
0
文件: api.go 项目: pascalj/disgo
// CreateComment validates and creates a new comment. It also saves the client's IP-adress
// to reduce spam.
func CreateComment(w http.ResponseWriter, req *http.Request, app *App) {
	comment := models.NewComment(
		req.FormValue("email"),
		req.FormValue("name"),
		req.FormValue("title"),
		req.FormValue("body"),
		req.FormValue("url"),
		req.RemoteAddr)
	comment.Created = time.Now().Unix()

	ip, err := relevantIpBytes(req.RemoteAddr)
	if err != nil {
		ip = req.RemoteAddr
	}
	comment.ClientIp = ip

	valid, valErrors := comment.Validate()
	if valid {
		err := comment.Save(app.Db)
		if err != nil {
			w.WriteHeader(500)
		} else {
			session, _ := app.SessionStore.Get(req, SessionName)
			session.Values["email"] = comment.Email
			session.Values["name"] = comment.Name
			session.Save(req, w)
			renderComment(w, "partial/comment", comment, app)
		}
	} else {
		renderErrors(w, valErrors, 422)
	}

	go app.Notifier.CommentCreated(&comment)
}
示例#2
0
文件: importer.go 项目: pascalj/disgo
func Import(db *sql.DB, xmlReader io.Reader) error {
	parsed := &disqus{}
	decoder := xml.NewDecoder(xmlReader)
	if err := decoder.Decode(parsed); err != nil {
		return err
	}
	comments := make([]models.Comment, 0)
	for _, post := range parsed.Posts {
		thread, err := parsed.findThread(post.Thread.Id)
		if err != nil {
			fmt.Println("Could not find thread reference", post.Thread.Id)
		}
		if post.IsDeleted == "true" {
			continue
		}
		createdAt, err := time.Parse(time.RFC3339, post.CreatedAt)
		if err != nil {
			createdAt = time.Now()
		}
		body := sanitize.HTML(post.Message)
		comment := models.NewComment(post.Author.Email, post.Author.Name, "", body, thread.Link, post.IpAddress)
		comment.Created = createdAt.Unix()
		comment.Approved = post.IsSpam == "false"
		comments = append(comments, comment)
	}

	total := len(parsed.Posts)
	fmt.Println("Read", total, "from the file.")

	for i, comment := range comments {
		if err := comment.Save(db); err != nil {
			total--
			fmt.Println("Could not import comment", i)
			fmt.Println(err)
		}
	}

	fmt.Println("Wrote", total, "comments to the database.")

	return nil
}