bing work with gorm

This commit is contained in:
inhosin 2019-09-07 19:10:28 +03:00
parent 8807e5aaab
commit 61b976259a
7 changed files with 203 additions and 13 deletions

43
Gopkg.lock generated
View File

@ -30,6 +30,38 @@
revision = "6c65a5562fc06764971b7c5d05c76c75e84bdbf7" revision = "6c65a5562fc06764971b7c5d05c76c75e84bdbf7"
version = "v1.3.2" version = "v1.3.2"
[[projects]]
digest = "1:cbec35fe4d5a4fba369a656a8cd65e244ea2c743007d8f6c1ccb132acf9d1296"
name = "github.com/gorilla/mux"
packages = ["."]
pruneopts = "UT"
revision = "00bdffe0f3c77e27d2cf6f5c70232a2d3e4d9c15"
version = "v1.7.3"
[[projects]]
digest = "1:17275ec8407e731ae22cc511d52391b2ac31982f23b53b1100265c575dc5cc9f"
name = "github.com/gosimple/slug"
packages = ["."]
pruneopts = "UT"
revision = "984b6d1a0ae5d1ecf6d718f4e990883d281ba3b8"
version = "v1.7.0"
[[projects]]
digest = "1:09424e4abeba1d2fc80c47c26347a3eed24f9cfd83173131dd048479ca6e041a"
name = "github.com/jinzhu/gorm"
packages = ["."]
pruneopts = "UT"
revision = "836fb2c19d84dac7b0272958dfb9af7cf0d0ade4"
version = "v1.9.10"
[[projects]]
digest = "1:01ed62f8f4f574d8aff1d88caee113700a2b44c42351943fa73cc1808f736a50"
name = "github.com/jinzhu/inflection"
packages = ["."]
pruneopts = "UT"
revision = "f5c5f50e6090ae76a29240b61ae2a90dd810112e"
version = "v1.0.0"
[[projects]] [[projects]]
digest = "1:709cd2a2c29cc9b89732f6c24846bbb9d6270f28ef5ef2128cc73bd0d6d7bff9" digest = "1:709cd2a2c29cc9b89732f6c24846bbb9d6270f28ef5ef2128cc73bd0d6d7bff9"
name = "github.com/json-iterator/go" name = "github.com/json-iterator/go"
@ -70,6 +102,14 @@
revision = "4b7aa43c6742a2c18fdef89dd197aaae7dac7ccd" revision = "4b7aa43c6742a2c18fdef89dd197aaae7dac7ccd"
version = "1.0.1" version = "1.0.1"
[[projects]]
branch = "master"
digest = "1:e6a29574542c00bb18adb1bfbe629ff88c468c2af2e2e953d3e58eda07165086"
name = "github.com/rainycape/unidecode"
packages = ["."]
pruneopts = "UT"
revision = "cb7f23ec59bec0d61b19c56cd88cee3d0cc1870c"
[[projects]] [[projects]]
digest = "1:04457f9f6f3ffc5fea48e71d62f2ca256637dee0a04d710288e27e05c8b41976" digest = "1:04457f9f6f3ffc5fea48e71d62f2ca256637dee0a04d710288e27e05c8b41976"
name = "github.com/sirupsen/logrus" name = "github.com/sirupsen/logrus"
@ -115,6 +155,9 @@
analyzer-version = 1 analyzer-version = 1
input-imports = [ input-imports = [
"github.com/gin-gonic/gin", "github.com/gin-gonic/gin",
"github.com/gorilla/mux",
"github.com/gosimple/slug",
"github.com/jinzhu/gorm",
"github.com/sirupsen/logrus", "github.com/sirupsen/logrus",
] ]
solver-name = "gps-cdcl" solver-name = "gps-cdcl"

53
data/models/activity.go Normal file
View File

@ -0,0 +1,53 @@
package models
import (
"database/sql"
"time"
)
type Activity struct {
Id string `json:"id"`
Payload string `json:"note"`
Remote bool `json:"remote"`
Group `json:"group"`
CreatedAt time.Time `json:"created_at"`
UpdatedAt time.Time `json:"updated_at"`
}
// GetActivity gets a Activity at any slug
func GetActivity(db *sql.DB, slug string) (*Activity, error) {
row := db.QueryRow(`
SELECT id, Payload, remote, goupid, created_at, updated_at
FROM activity WHERE id = $1
`, slug)
var note Activity
err := row.Scan(&note.Slug,
&note.Name, &note.Note, &note.CreatedAt, &note.UpdatedAt)
// This is not an error from the user's perspective
if err == sql.ErrNoRows {
return nil, nil
}
if err != nil {
return nil, err
}
return &note, nil
}
/*
// PutNote creates a note with this name and note
func PutActivity(db *sql.DB, name string, note string) (string, error) {
// TODO make guid but not slug
id := slugify.MakeLang(name, "en")
query := `
INSERT INTO activity (id, name, note)
VALUES ($1, $2, $3)
`
_, err := db.Exec(query, slug, name, note)
return slug, err
}
*/

View File

@ -2,21 +2,20 @@ package models
import ( import (
"database/sql" "database/sql"
"time"
slugify "github.com/gosimple/slug" slugify "github.com/gosimple/slug"
"github.com/jinzhu/gorm"
) )
// Group is a collection of Hubs (equivalent to ActivityPub Organizations) // Group is a collection of Hubs (equivalent to ActivityPub Organizations)
// Refers to the https://www.w3.org/TR/activitystreams-vocabulary/#dfn-group // Refers to the https://www.w3.org/TR/activitystreams-vocabulary/#dfn-group
// Also refers to the Groups table in the database // Also refers to the Groups table in the database
type Group struct { type Group struct {
Slug string `json:"slug"` gorm.Model
Id string `json:"id"`
Name string `json:"name"` Name string `json:"name"`
Note string `json:"note"` Note string `json:"note"`
Type string Type string
CreatedAt time.Time `json:"created_at"` UserID uint
UpdatedAt time.Time `json:"updated_at"`
} }
// GetGroup returns a single Group object or nil // GetGroup returns a single Group object or nil
@ -27,7 +26,7 @@ func GetGroup(db *sql.DB, slug string) (*Group, error) {
`, slug) `, slug)
var group Group var group Group
err := row.Scan(&group.Slug, &group.Name, &group.Note, &group.CreatedAt, &group.UpdatedAt) err := row.Scan(&group.id, &group.Name, &group.Note, &group.CreatedAt, &group.UpdatedAt)
if err == sql.ErrNoRows { if err == sql.ErrNoRows {
return nil, nil return nil, nil

19
data/models/user.go Normal file
View File

@ -0,0 +1,19 @@
package models
import (
"github.com/jinzhu/gorm"
)
type User struct {
gorm.Model
Name string
Role string `gorm:"size:255"`
Groups []Group `gorm:"foreignkey:UserID"`
}
type Profile struct {
gorm.Model
UserID int
User User
Email string `gorm:"type:varchar(100);unique_index"`
}

View File

@ -19,7 +19,7 @@ type createGroupResponse struct {
// Create adds an group to the database // Create adds an group to the database
func Create(c *gin.Context) { func Create(c *gin.Context) {
var body createGroupRequest var body createGroupRequest
c.BindJSON(&body) _ = c.BindJSON(&body)
db := data.GetDB() db := data.GetDB()
slug, err := models.PutGroup(db, body.Name, body.Note) slug, err := models.PutGroup(db, body.Name, body.Note)

53
handlers/note/get.go Normal file
View File

@ -0,0 +1,53 @@
package note
import (
"encoding/json"
"github.com/gin-gonic/gin"
"github.com/gorilla/mux"
"net/http"
"net/url"
)
// Get returns a Note
//
// Expects a `{noteid}` url variable
// in the route: `/api/note/{noteid}`
func Get(c *gin.Context) {
noteId := c.Param("noteid")
if noteId == "" {
c.String(http.StatusInternalServerError, "Bad request, no slug in url")
return
}
// Attempt to grab the Note
note, err := models.GetNote(data.GetPool(), noteId)
// 500 because something went wrong with the database
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
// 404 because something we couldn't find the show
if show == nil {
http.Error(w, slug+" does not exist on this server", http.StatusNotFound)
return
}
// Convert to an ActivityPub object
url, err := url.Parse(handlers.GetFullHostname() + "/api/show/" + slug)
actor := activity.NewOrganization(show.Name, url)
// Turn the show into JSON
bytes, err := json.Marshal(actor)
// 500 because something went wrong marshaling the show
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
// Success!
w.Write(bytes)
}

29
main.go
View File

@ -17,15 +17,38 @@ func init() {
func main() { func main() {
r := gin.Default() r := gin.Default()
r.GET("/.well-known/webfinger", webfinder.Get) r.GET("/.well-known/webfinger", webfinder.Get)
// App routes
api := r.Group("/api") api := r.Group("/api")
{ {
api.GET("/api/group/:name", groups.Get) api.GET("/group/:name", groups.Get)
api.POST("/api/group", groups.Create) api.GET("/group/:name/tags", groups.Get)
api.GET("/group/:name/tags", groups.Get)
api.GET("/group/:name/featured", groups.Get)
api.GET("/group/:name/following", groups.Get)
api.GET("/group/:name/outbox", groups.Get)
api.GET("/group/:name/inbox", groups.Create)
api.POST("/group/:name/inbox", groups.Create)
api.POST("/group", groups.Create)
} }
// r.GET("/@:name", groups.GetGroup)
// r.GET("/note/:noteid", note.GetNote)
// Activity pub routes
ap := r.Group("/activity")
{
ap.GET("/group/:name", groups.Get)
ap.GET("/group/:name/followers", groups.Get)
ap.GET("/group/:name/following", groups.Get)
ap.GET("/group/:name/outbox", groups.Get)
ap.GET("/group/:name/inbox", groups.Create)
ap.POST("/group/:name/inbox", groups.Create)
}
r.GET("/health", healthHandler) r.GET("/health", healthHandler)
// Add support for PORT env // Add support for PORT env
port := "8080" port := "8090"
if fromEnv := os.Getenv("PORT"); fromEnv != "" { if fromEnv := os.Getenv("PORT"); fromEnv != "" {
port = fromEnv port = fromEnv
} }