diff options
author | KushBlazingJudah <59340248+KushBlazingJudah@users.noreply.github.com> | 2021-11-09 19:37:34 -0400 |
---|---|---|
committer | FChannel <> | 2022-06-19 12:53:29 -0700 |
commit | e15bf8cd1375f24251929ff3e13f883f692ee03a (patch) | |
tree | 661fec8cc005630a67df1fc4c933a1e8a19f686b /db | |
parent | 7116705959bad9244893434039a0da9b500f5043 (diff) |
slacking off is great; here's part 6
Diffstat (limited to 'db')
-rw-r--r-- | db/database.go | 79 | ||||
-rw-r--r-- | db/outbox.go | 116 |
2 files changed, 186 insertions, 9 deletions
diff --git a/db/database.go b/db/database.go index bd302aa..dfa66aa 100644 --- a/db/database.go +++ b/db/database.go @@ -18,7 +18,9 @@ import ( _ "github.com/lib/pq" ) +// TODO: merge these var db *sql.DB +var DB *sql.DB type NewsItem struct { Title string @@ -48,6 +50,7 @@ func ConnectDB() error { fmt.Println("Successfully connected DB") db = _db + DB = _db return nil } @@ -289,10 +292,12 @@ func WriteObjectToDB(obj activitypub.ObjectBase) (activitypub.ObjectBase, error) obj.Preview.Published = time.Now().UTC() obj.Preview.Updated = time.Now().UTC() obj.Preview.AttributedTo = obj.Id - WritePreviewToDB(*obj.Preview) + if err := WritePreviewToDB(*obj.Preview); err != nil { + return obj, err + } } - for i, _ := range obj.Attachment { + for i := range obj.Attachment { id, err := CreateUniqueID(obj.Actor) if err != nil { return obj, err @@ -563,15 +568,11 @@ func WriteAttachmentToDB(obj activitypub.ObjectBase) { } } -func WritePreviewToDB(obj activitypub.NestedObjectBase) { +func WritePreviewToDB(obj activitypub.NestedObjectBase) error { query := `insert into activitystream (id, type, name, href, published, updated, attributedTo, mediatype, size) values ($1, $2, $3, $4, $5, $6, $7, $8, $9)` - _, e := db.Exec(query, obj.Id, obj.Type, obj.Name, obj.Href, obj.Published, obj.Updated, obj.AttributedTo, obj.MediaType, obj.Size) - - if e != nil { - fmt.Println("error inserting new attachment") - panic(e) - } + _, err := db.Exec(query, obj.Id, obj.Type, obj.Name, obj.Href, obj.Published, obj.Updated, obj.AttributedTo, obj.MediaType, obj.Size) + return err } func GetActivityFromDB(id string) (activitypub.Collection, error) { @@ -2477,3 +2478,63 @@ func CheckInactiveInstances() (map[string]string, error) { return instances, nil } + +func GetAdminAuth() (string, string, error) { + query := fmt.Sprintf("select identifier, code from boardaccess where board='%s' and type='admin'", config.Domain) + + rows, err := db.Query(query) + if err != nil { + return "", "", err + } + + var code string + var identifier string + + rows.Next() + err = rows.Scan(&identifier, &code) + + return code, identifier, err +} + +func UpdateObjectWithPreview(id string, preview string) error { + query := `update activitystream set preview=$1 where attachment=$2` + + _, err := db.Exec(query, preview, id) + return err +} + +func GetObjectsWithoutPreviewsCallback(callback func(id string, href string, mediatype string, name string, size int, published time.Time) error) error { + query := `select id, href, mediatype, name, size, published from activitystream where id in (select attachment from activitystream where attachment!='' and preview='')` + + rows, err := db.Query(query) + if err != nil { + return err + } + defer rows.Close() + + for rows.Next() { + var id string + var href string + var mediatype string + var name string + var size int + var published time.Time + + if err := rows.Scan(&id, &href, &mediatype, &name, &size, &published); err != nil { + return err + } + + if err := callback(id, href, mediatype, name, size, published); err != nil { + return err + } + } + + return nil +} + +func AddFollower(id string, follower string) error { + query := `insert into follower (id, follower) values ($1, $2)` + + _, err := db.Exec(query, id, follower) + return err +} diff --git a/db/outbox.go b/db/outbox.go new file mode 100644 index 0000000..e8189d9 --- /dev/null +++ b/db/outbox.go @@ -0,0 +1,116 @@ +package db + +import ( + "github.com/FChannel0/FChannel-Server/activitypub" +) + +func GetCollectionFromPath(path string) (activitypub.Collection, error) { + var nColl activitypub.Collection + var result []activitypub.ObjectBase + + query := `select id, name, content, type, published, attributedto, attachment, preview, actor from activitystream where id=$1 order by published desc` + + rows, err := db.Query(query, path) + if err != nil { + return nColl, err + } + defer rows.Close() + + for rows.Next() { + var actor activitypub.Actor + var post activitypub.ObjectBase + var attachID string + var previewID string + + if err := rows.Scan(&post.Id, &post.Name, &post.Content, &post.Type, &post.Published, &post.AttributedTo, &attachID, &previewID, &actor.Id); err != nil { + return nColl, err + } + + post.Actor = actor.Id + + post.InReplyTo, err = GetInReplyToDB(post) + if err != nil { + return nColl, err + } + + var postCnt int + var imgCnt int + post.Replies, postCnt, imgCnt, err = GetObjectRepliesDB(post) + if err != nil { + return nColl, err + } + + post.Replies.TotalItems, post.Replies.TotalImgs, err = GetObjectRepliesCount(post) + if err != nil { + return nColl, err + } + + post.Replies.TotalItems = post.Replies.TotalItems + postCnt + post.Replies.TotalImgs = post.Replies.TotalImgs + imgCnt + + post.Attachment, err = GetObjectAttachment(attachID) + if err != nil { + return nColl, err + } + + post.Preview, err = GetObjectPreview(previewID) + if err != nil { + return nColl, err + } + + result = append(result, post) + } + + nColl.AtContext.Context = "https://www.w3.org/ns/activitystreams" + + nColl.OrderedItems = result + + return nColl, nil +} + +func GetObjectFromPath(path string) (activitypub.ObjectBase, error) { + var nObj activitypub.ObjectBase + + query := `select id, name, content, type, published, attributedto, attachment, preview, actor from activitystream where id=$1 order by published desc` + + rows, err := db.Query(query, path) + if err != nil { + return nObj, err + } + + defer rows.Close() + rows.Next() + var attachID string + var previewID string + + var nActor activitypub.Actor + nObj.Actor = nActor.Id + + if err := rows.Scan(&nObj.Id, &nObj.Name, &nObj.Content, &nObj.Type, &nObj.Published, &nObj.AttributedTo, &attachID, &previewID, &nObj.Actor); err != nil { + return nObj, err + } + + var postCnt int + var imgCnt int + + nObj.Replies, postCnt, imgCnt, err = GetObjectRepliesDB(nObj) + if err != nil { + return nObj, err + } + + nObj.Replies.TotalItems, nObj.Replies.TotalImgs, err = GetObjectRepliesCount(nObj) + if err != nil { + return nObj, err + } + + nObj.Replies.TotalItems = nObj.Replies.TotalItems + postCnt + nObj.Replies.TotalImgs = nObj.Replies.TotalImgs + imgCnt + + nObj.Attachment, err = GetObjectAttachment(attachID) + if err != nil { + return nObj, err + } + + nObj.Preview, err = GetObjectPreview(previewID) + return nObj, err +} |