Ejemplo n.º 1
0
// /a/comment
func (s *Server) CommentHandler(c *gin.Context) {
	c.Request.ParseForm()
	id := c.Request.Form.Get("id")
	entryId := c.Request.Form.Get("entry")
	rawBody := c.Request.Form.Get("body")
	if entryId == "" || rawBody == "" {
		c.JSON(http.StatusBadRequest, gin.H{"status": "bad request"})
		return
	}

	body := util.DefaultSanitize(rawBody)
	body = util.EntityToLink(body)

	profile, _ := s.CurrentUser(c)
	from := &pb.Feed{
		Id:   profile.Id,
		Name: profile.Name,
		Type: profile.Type,
	}
	comment := &pb.Comment{
		Body:    body,
		RawBody: rawBody,
		From:    from,
	}

	var err error
	var uuid1 uuid.UUID
	if id != "" {
		uuid1, err = uuid.FromString(id)
		if err != nil {
			c.JSON(http.StatusBadRequest, gin.H{"status": "bad request"})
			return
		}
	} else {
		comment.Date = time.Now().UTC().Format(time.RFC3339)
		name := entryId + profile.Uuid + comment.Date
		uuid1 = uuid.NewV5(uuid.NamespaceURL, name)
	}
	comment.Id = uuid1.String()

	req := &pb.CommentRequest{
		Entry:   entryId,
		Comment: comment,
	}

	ctx, cancel := DefaultTimeoutContext()
	defer cancel()

	_, err = s.client.CommentEntry(ctx, req)
	if RequestError(c, err) {
		return
	}

	comment.Commands = []string{"edit", "delete"}
	c.JSON(200, comment)
}
Ejemplo n.º 2
0
// TODO: allow cross post to multiply feeds
func (s *Server) EntryPostHandler(c *gin.Context) {
	var form struct {
		FeedId string `form:"feedid" binding:"required"`
		Body   string `form:"body" binding:"required"`
	}
	c.BindWith(&form, binding.MultipartForm)

	if !s.feedWritable(c, form.FeedId) {
		c.AbortWithStatus(401)
		return
	}

	body := util.DefaultSanitize(form.Body)
	body = util.EntityToLink(body)

	ctx, cancel := DefaultTimeoutContext()
	defer cancel()

	profile, _ := s.CurrentUser(c)
	dt := time.Now().UTC()
	name := profile.Uuid + "/" + dt.Format(time.RFC3339)
	uuid1 := uuid.NewV5(uuid.NamespaceURL, name)

	from := &pb.Feed{
		Id:   profile.Id,
		Name: profile.Name,
		Type: profile.Type,
	}

	entry := &pb.Entry{
		Id:      uuid1.String(),
		Date:    dt.Format(time.RFC3339),
		Body:    body,
		RawBody: form.Body,
		From:    from,
		// To:      []*pb.Feed{from},
		// Thumbnails: thumbnails,
		ProfileUuid: profile.Uuid,
	}

	entry, err := s.client.PostEntry(ctx, entry)
	if RequestError(c, err) {
		return
	}
	// c.JSON(200, gin.H{"entry": entry})
	c.Redirect(http.StatusFound, "/")
}