From 000c206417a91d0f910cba35d16d433a1f038530 Mon Sep 17 00:00:00 2001 From: FChannel <=> Date: Sat, 23 Jan 2021 13:53:25 -0800 Subject: foundation for cache retrieval --- CacheDatabase.go | 88 +++++++++++++++++++++++++++++++++++++++++++++- client.go | 12 +++++-- clientkey | 2 +- main.go | 101 ++++++++++++++++++++++++++++++++++++++--------------- outboxGet.go | 1 - static/manage.html | 17 ++++++--- 6 files changed, 181 insertions(+), 40 deletions(-) diff --git a/CacheDatabase.go b/CacheDatabase.go index ebb84df..4add356 100644 --- a/CacheDatabase.go +++ b/CacheDatabase.go @@ -140,11 +140,61 @@ func WritePreviewToCache(db *sql.DB, obj NestedObjectBase) { } } +func GetActivityFromCache(db *sql.DB, id string) Collection { + var nColl Collection + var nActor Actor + var result []ObjectBase + + nColl.Actor = &nActor + + query := `select actor, id, name, content, type, published, updated, attributedto, attachment, preview, actor from activitystream where id=$1 order by updated asc` + + rows, err := db.Query(query, id) + + CheckError(err, "error query object from db") + + defer rows.Close() + for rows.Next(){ + var post ObjectBase + var actor Actor + var attachID string + var previewID string + + + err = rows.Scan(&nColl.Actor.Id, &post.Id, &post.Name, &post.Content, &post.Type, &post.Published, &post.Updated, &post.AttributedTo, &attachID, &previewID, &actor.Id) + + CheckError(err, "error scan object into post struct") + + post.Actor = &actor + + var postCnt int + var imgCnt int + + post.Replies, postCnt, imgCnt = GetObjectRepliesCache(db, post) + + post.Replies.TotalItems, post.Replies.TotalImgs = GetObjectRepliesCacheCount(db, post) + + post.Replies.TotalItems = post.Replies.TotalItems + postCnt + post.Replies.TotalImgs = post.Replies.TotalImgs + imgCnt + + post.Attachment = GetObjectAttachmentCache(db, attachID) + + post.Preview = GetObjectPreviewCache(db, previewID) + + result = append(result, post) + } + + nColl.OrderedItems = result + + return nColl +} + func GetObjectFromCache(db *sql.DB, id string) Collection { var nColl Collection var result []ObjectBase - query := `select id, name, content, type, published, updated, attributedto, attachment, preview, actor from cacheactivitystream where id=$1 and type='Note'` + + query := `select id, name, content, type, published, updated, attributedto, attachment, preview, actor from activitystream where actor=$1 and id in (select id from cachereplies where inreplyto='') and type='Note' order by updated asc` rows, err := db.Query(query, id) @@ -429,3 +479,39 @@ func DeleteObjectFromCache(db *sql.DB, id string) { CheckError(err, "could not delete cache replies activitystream") } +func GetObjectPostsTotalCache(db *sql.DB, actor Actor) int{ + + count := 0 + query := `select count(id) from cacheactivitystream where actor=$1 and id in (select id from cachereplies where inreplyto='' and type='Note')` + + rows, err := db.Query(query, actor.Id) + + CheckError(err, "could not select post total count query") + + defer rows.Close() + for rows.Next() { + err = rows.Scan(&count) + CheckError(err, "error with total post db scan") + } + + return count +} + +func GetObjectImgsTotalCache(db *sql.DB, actor Actor) int{ + + count := 0 + query := `select count(attachment) from cacheactivitystream where actor=$1 and id in (select id from cachereplies where inreplyto='' and type='Note' )` + + rows, err := db.Query(query, actor.Id) + + CheckError(err, "error with posts total db query") + + defer rows.Close() + for rows.Next() { + err = rows.Scan(&count) + + CheckError(err, "error with total post db scan") + } + + return count +} diff --git a/client.go b/client.go index 976eb31..d62c26f 100644 --- a/client.go +++ b/client.go @@ -56,6 +56,7 @@ type AdminPage struct { Followers []string Reported []Report Domain string + IsLocal bool } type Report struct { @@ -396,11 +397,16 @@ func WantToServe(db *sql.DB, actorName string) (Collection, bool) { if boardActor.Id == "" { boardActor = GetActor(e.Id) } - - if boardActor.Id == actorName { + + if boardActor.Name == actorName { serve = true - collection = GetActorCollection(boardActor.Outbox) + if IsActorLocal(db, boardActor.Id) { + collection = GetActorCollectionDB(db, boardActor) + } else { + collection = GetActorCollectionCache(db, boardActor) + } collection.Actor = &boardActor + return collection, serve } } diff --git a/clientkey b/clientkey index 3438bee..d15afae 100644 --- a/clientkey +++ b/clientkey @@ -1 +1 @@ -166070629d7087e34b0cce5dadaf0a2d \ No newline at end of file +f0e6a4255a3cab9679c1dca374632efd \ No newline at end of file diff --git a/main.go b/main.go index 60f5701..e752759 100644 --- a/main.go +++ b/main.go @@ -80,9 +80,7 @@ func main() { path = re.ReplaceAllString(path, "") } - method := r.Method - actor := GetActorFromPath(db, path, "/") var mainActor bool var mainInbox bool @@ -101,27 +99,29 @@ func main() { var actorVerification bool var accept = r.Header.Get("Accept") + + var method = r.Method - if(actor.Id != ""){ - if actor.Name == "main" { - mainActor = (path == "/") - mainInbox = (path == "/inbox") - mainOutbox = (path == "/outbox") - mainFollowing = (path == "/following") - mainFollowers = (path == "/followers") - } else { - actorMain = (path == "/" + actor.Name) - actorInbox = (path == "/" + actor.Name + "/inbox") - actorCatalog = (path == "/" + actor.Name + "/catalog") - actorOutbox = (path == "/" + actor.Name + "/outbox") - actorFollowing = (path == "/" + actor.Name + "/following") - actorFollowers = (path == "/" + actor.Name + "/followers") - actorReported = (path == "/" + actor.Name + "/reported") - actorVerification = (path == "/" + actor.Name + "/verification") - - re := regexp.MustCompile("/" + actor.Name + "/\\w+") - actorPost = re.MatchString(path) - } + var actor = GetActorFromPath(db, path, "/") + + if actor.Name == "" { + mainActor = (path == "/") + mainInbox = (path == "/inbox") + mainOutbox = (path == "/outbox") + mainFollowing = (path == "/following") + mainFollowers = (path == "/followers") + } else { + actorMain = (path == "/" + actor.Name) + actorInbox = (path == "/" + actor.Name + "/inbox") + actorCatalog = (path == "/" + actor.Name + "/catalog") + actorOutbox = (path == "/" + actor.Name + "/outbox") + actorFollowing = (path == "/" + actor.Name + "/following") + actorFollowers = (path == "/" + actor.Name + "/followers") + actorReported = (path == "/" + actor.Name + "/reported") + actorVerification = (path == "/" + actor.Name + "/verification") + + re := regexp.MustCompile("/" + actor.Name + "/\\w+") + actorPost = re.MatchString(path) } if mainActor { @@ -174,7 +174,7 @@ func main() { return } - collection, valid := WantToServe(db, actor.Id) + collection, valid := WantToServe(db, actor.Name) if valid { OutboxGet(w, r, db, collection) } @@ -427,9 +427,11 @@ func main() { req.Header.Set("Content-Type", activitystreams) - _, err = http.DefaultClient.Do(req) + _, err = http.DefaultClient.Do(req) - CheckError(err, "error with add board follow resp") + CheckError(err, "error with add board follow resp") + + http.Redirect(w, r, r.Header.Get("Referer"), http.StatusSeeOther) @@ -473,6 +475,7 @@ func main() { adminData.Followers = followers adminData.Reported = reports adminData.Domain = Domain + adminData.IsLocal = IsActorLocal(db, actor.Id) var boardCollection []Board @@ -577,6 +580,7 @@ func main() { CheckError(err, "error getting actor from body in new board") //update board list with new instances following + fmt.Println(resp.StatusCode) if resp.StatusCode == 200 { var board []ObjectBase var item ObjectBase @@ -910,14 +914,12 @@ func GetActorFromPath(db *sql.DB, location string, prefix string) Actor { } if actor == "/" || actor == "outbox" || actor == "inbox" || actor == "following" || actor == "followers" { - actor = Domain - } else { - actor = Domain + "/" + actor + actor = "main" } var nActor Actor - nActor = GetActorFromDB(db, actor) + nActor = GetActorByName(db, actor) return nActor } @@ -1232,6 +1234,36 @@ func GetActor(id string) Actor { return respActor } +func GetActorCollectionCache(db *sql.DB, actor Actor) Collection { + var collection Collection + + collection.OrderedItems = GetObjectFromCache(db, actor.Id).OrderedItems + + collection.AtContext.Context = "https://www.w3.org/ns/activitystreams" + collection.Actor = &actor + collection.Actor.AtContext.Context = "" + + collection.TotalItems = GetObjectPostsTotalCache(db, actor) + collection.TotalImgs = GetObjectImgsTotalCache(db, actor) + + return collection +} + +func GetActorCollectionDB(db *sql.DB, actor Actor) Collection { + var collection Collection + + collection.OrderedItems = GetObjectFromDB(db, actor).OrderedItems + + collection.AtContext.Context = "https://www.w3.org/ns/activitystreams" + collection.Actor = &actor + collection.Actor.AtContext.Context = "" + + collection.TotalItems = GetObjectPostsTotalDB(db, actor) + collection.TotalImgs = GetObjectImgsTotalDB(db, actor) + + return collection +} + func GetActorCollection(collection string) Collection { var nCollection Collection @@ -1326,6 +1358,16 @@ func IsIDLocal(db *sql.DB, id string) bool { return len(activity.OrderedItems) > 0 } +func IsActorLocal(db *sql.DB, id string) bool { + actor := GetActorFromDB(db, id) + + if actor.Id != "" { + return true + } + + return false +} + func IsObjectLocal(db *sql.DB, id string) bool { query := fmt.Sprintf("select id from activitystream where id='%s'", id) @@ -1795,3 +1837,4 @@ func GetActorCollectionReq(r *http.Request, collection string) Collection { return nCollection } + diff --git a/outboxGet.go b/outboxGet.go index dbd0fa9..8eec74e 100644 --- a/outboxGet.go +++ b/outboxGet.go @@ -6,7 +6,6 @@ import _ "github.com/lib/pq" import "encoding/json" func GetActorOutbox(w http.ResponseWriter, r *http.Request, db *sql.DB) { - actor := GetActorFromPath(db, r.URL.Path, "/") var collection Collection diff --git a/static/manage.html b/static/manage.html index 43ff766..3a86f7a 100644 --- a/static/manage.html +++ b/static/manage.html @@ -8,12 +8,18 @@

Manage /{{ .Board.Name }}/

[Return] +{{ $actor := .Actor }} +{{ $board := .Board }} +{{ $key := .Key }} +{{ if .IsLocal }}

Subscribed

@@ -23,9 +29,7 @@
+{{ end }}