Commit e292ecd1 authored by zauberstuhl's avatar zauberstuhl

Implement ApiLike.Create

related to ganggo#3
parent 4accee34
......@@ -59,7 +59,7 @@ func (c ApiComment) Create() revel.Result {
return c.RenderError(err)
}
err = post.FindByID(postID)
err = post.FindByID(postID, false)
if err != nil {
revel.ERROR.Println(err)
return c.NotFound("Post not found")
......
......@@ -20,6 +20,9 @@ package controllers
import (
"github.com/revel/revel"
"gopkg.in/ganggo/ganggo.v0/app/models"
"gopkg.in/ganggo/ganggo.v0/app/jobs"
"gopkg.in/ganggo/api.v0/app/helpers"
federation "gopkg.in/ganggo/federation.v0"
)
type ApiLike struct {
......@@ -43,7 +46,65 @@ func (l ApiLike) Index() revel.Result {
}
func (l ApiLike) Create() revel.Result {
return l.NotFound("Not implemented yet")
var (
postID uint
post models.Post
like models.Like
positive bool
)
l.Params.Bind(&postID, "id")
l.Params.Bind(&positive, "positive")
err := post.FindByID(postID, true)
if err != nil {
revel.ERROR.Println(err)
return l.NotFound("Post not found")
}
user, err := models.GetCurrentUser(l.Session["TOKEN"])
if err != nil {
revel.ERROR.Println(err)
return l.RenderError(err)
}
guid, err := helpers.Uuid()
if err != nil {
revel.ERROR.Println(err)
return l.RenderError(err)
}
entityLike := federation.EntityLike{
Positive: positive,
Guid: guid,
DiasporaHandle: user.Person.DiasporaHandle,
TargetType: models.ShareablePost,
ParentGuid: post.Guid,
}
err = like.Create(&entityLike)
if err != nil {
revel.ERROR.Println(err)
return l.RenderError(err)
}
// deliver to the network
dispatcher := jobs.Dispatcher{
User: user,
Message: entityLike,
}
if user.Person.UserID > 0 {
var parentUser models.User
err = parentUser.FindByID(user.Person.UserID)
if err != nil {
revel.ERROR.Println(err)
return l.RenderError(err)
}
dispatcher.ParentUser = &parentUser
}
go dispatcher.Run()
return l.RenderJson(like)
}
func (l ApiLike) Show() revel.Result {
......
......@@ -72,7 +72,7 @@ func (p ApiPost) Show() revel.Result {
)
p.Params.Bind(&postID, "id")
err := post.FindByID(postID)
err := post.FindByID(postID, false)
if err != nil {
revel.ERROR.Println(err)
return p.NotFound("Post not found")
......
package helpers
//
// GangGo API Library
// Copyright (C) 2017 Lukas Matt <lukas@zauberstuhl.de>
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <http://www.gnu.org/licenses/>.
//
import (
"fmt"
"crypto/rand"
)
func Uuid() (uuid string, err error) {
b := make([]byte, 16)
_, err = rand.Read(b)
if err != nil {
return
}
uuid = fmt.Sprintf("%x%x%x%x%x", b[0:4], b[4:6], b[6:8], b[8:10], b[10:])
return
}
......@@ -6,7 +6,7 @@ GET /api/v0/posts/:id ApiPost.Show
DELETE /api/v0/posts/:id ApiPost.Delete
GET /api/v0/posts/:id/likes ApiLike.Index
POST /api/v0/posts/:id/likes ApiLike.Create
POST /api/v0/posts/:id/likes/:positive ApiLike.Create
GET /api/v0/posts/:id/likes/:like_id ApiLike.Show
DELETE /api/v0/posts/:id/likes/:like_id ApiLike.Delete
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment