From 2868cbabf7c394c637fc8568c6981da6eb5370ca Mon Sep 17 00:00:00 2001 From: FChannel <=> Date: Tue, 26 Jan 2021 11:54:53 -0800 Subject: individual post view cache --- CacheDatabase.go | 61 ++++++++++++++++++++++++++++++++++++++--------------- Database.go | 44 ++++++++++++++++++++++++++++++++++++++ client.go | 31 +++++++++++++++++---------- databaseschema.psql | 5 ----- main.go | 32 +++++++++++++++------------- static/js/posts.js | 4 ++-- 6 files changed, 127 insertions(+), 50 deletions(-) diff --git a/CacheDatabase.go b/CacheDatabase.go index e533f4f..fe55e84 100644 --- a/CacheDatabase.go +++ b/CacheDatabase.go @@ -251,6 +251,50 @@ func GetObjectFromCache(db *sql.DB, id string) Collection { return nColl } +func GetObjectByIDFromCache(db *sql.DB, postID string) Collection { + var nColl Collection + var result []ObjectBase + + query := `select id, name, content, type, published, attributedto, attachment, preview, actor from cacheactivitystream where id=$1 order by published desc` + + rows, err := db.Query(query, postID) + + CheckError(err, "error query object from db cache") + + defer rows.Close() + for rows.Next(){ + var post ObjectBase + var actor Actor + var attachID string + var previewID string + + err = rows.Scan(&post.Id, &post.Name, &post.Content, &post.Type, &post.Published, &post.AttributedTo, &attachID, &previewID, &actor.Id) + + CheckError(err, "error scan object into post struct cache") + + post.Actor = &actor + + var postCnt int + var imgCnt int + post.Replies, postCnt, imgCnt = GetObjectRepliesDB(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 WriteObjectReplyToCache(db *sql.DB, obj ObjectBase) { for i, e := range obj.InReplyTo { @@ -413,23 +457,6 @@ func GetObjectRepliesRepliesCache(db *sql.DB, parent ObjectBase) (*CollectionBas result = append(result, post) } - /* - remoteCollection := GetObjectRepliesRemote(db, parent) - - var postc int - var imgc int - for _, e := range remoteCollection.OrderedItems { - - nColl.OrderedItems = append(nColl.OrderedItems, e) - postc = postc + 1 - if len(e.Attachment) > 0 { - imgc = imgc + 1 - } - } - - nColl.OrderedItems = result -*/ - return &nColl, 0, 0 } diff --git a/Database.go b/Database.go index cbb035b..c0df3a4 100644 --- a/Database.go +++ b/Database.go @@ -410,6 +410,50 @@ func GetObjectFromDB(db *sql.DB, actor Actor) Collection { return nColl } +func GetObjectByIDFromDB(db *sql.DB, postID string) Collection { + var nColl Collection + var result []ObjectBase + + query := `select id, name, content, type, published, updated, attributedto, attachment, preview, actor from activitystream where id=$1 and id in (select id from replies where inreplyto='') and type='Note' order by updated asc` + + rows, err := db.Query(query, postID) + + 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(&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 = GetObjectRepliesDB(db, post) + + post.Replies.TotalItems, post.Replies.TotalImgs = GetObjectRepliesDBCount(db, post) + + post.Replies.TotalItems = post.Replies.TotalItems + postCnt + post.Replies.TotalImgs = post.Replies.TotalImgs + imgCnt + + post.Attachment = GetObjectAttachment(db, attachID) + + post.Preview = GetObjectPreview(db, previewID) + + result = append(result, post) + } + + nColl.OrderedItems = result + + return nColl +} + func GetInReplyToDB(db *sql.DB, parent ObjectBase) []ObjectBase { var result []ObjectBase diff --git a/client.go b/client.go index b197168..98bb278 100644 --- a/client.go +++ b/client.go @@ -219,7 +219,9 @@ func CatalogGet(w http.ResponseWriter, r *http.Request, db *sql.DB, collection C if domainURL == Domain { followCol := GetObjectsFromFollow(db, *actor) for _, e := range followCol { - mergeCollection.OrderedItems = append(mergeCollection.OrderedItems, e) + if e.Type != "Tombstone" { + mergeCollection.OrderedItems = append(mergeCollection.OrderedItems, e) + } } } @@ -289,8 +291,8 @@ func PostGet(w http.ResponseWriter, r *http.Request, db *sql.DB){ if re.MatchString(path) { name := GetActorFollowNameFromPath(path) followActors := GetActorsFollowFromName(actor, name) - followCollection := GetActorsFollowPostFromId(followActors, postId) - + followCollection := GetActorsFollowPostFromId(db, followActors, postId) + DeleteRemovedPosts(db, &followCollection) DeleteTombstoneReplies(&followCollection) @@ -305,9 +307,8 @@ func PostGet(w http.ResponseWriter, r *http.Request, db *sql.DB){ } } else { - returnData.Board.InReplyTo = inReplyTo - collection := GetActorCollection(inReplyTo) + collection := GetActorCollectionByID(db, inReplyTo) DeleteRemovedPosts(db, &collection) @@ -689,27 +690,26 @@ func GetActorFollowNameFromPath(path string) string{ return actor } -func GetActorsFollowFromName(actor Actor, name string) []Actor { - var followingActors []Actor +func GetActorsFollowFromName(actor Actor, name string) []string { + var followingActors []string follow := GetActorCollection(actor.Following) re := regexp.MustCompile("\\w+?$") for _, e := range follow.Items { if re.FindString(e.Id) == name { - actor := GetActor(e.Id) - followingActors = append(followingActors, actor) + followingActors = append(followingActors, e.Id) } } return followingActors } -func GetActorsFollowPostFromId(actors []Actor, id string) Collection{ +func GetActorsFollowPostFromId(db *sql.DB, actors []string, id string) Collection{ var collection Collection for _, e := range actors { - tempCol := GetActorCollection(e.Id + "/" + id) + tempCol := GetActorCollectionByID(db, e + "/" + id) if len(tempCol.OrderedItems) > 0 { collection = tempCol } @@ -718,6 +718,15 @@ func GetActorsFollowPostFromId(actors []Actor, id string) Collection{ return collection } +func GetActorCollectionByID(db *sql.DB, postID string) Collection { + collection := GetObjectByIDFromDB(db, postID) + if len(collection.OrderedItems) < 1 { + collection = GetObjectByIDFromCache(db, postID) + } + + return collection +} + func CreateClientKey() string{ file, err := os.Create("clientkey") diff --git a/databaseschema.psql b/databaseschema.psql index 84eaf9d..a99f363 100644 --- a/databaseschema.psql +++ b/databaseschema.psql @@ -134,11 +134,6 @@ public boolean default false, CONSTRAINT fk_object FOREIGN KEY (object) REFERENCES activitystream(id) ); -CREATE TABLE IF NOT EXISTS cachereplies( -id varchar(100), -inreplyto varchar(100) -); - CREATE TABLE IF NOT EXISTS cacheactivitystream( actor varchar(100) default '', attachment varchar(100) default '', diff --git a/main.go b/main.go index 90a2521..605314e 100644 --- a/main.go +++ b/main.go @@ -201,7 +201,7 @@ func main() { } if actorCatalog { - collection, valid := WantToServe(db, actor.Id) + collection, valid := WantToServe(db, actor.Name) if valid { CatalogGet(w, r, db, collection) } @@ -1027,17 +1027,18 @@ func CreateObject(objType string) ObjectBase { func AddFollowersToActivity(db *sql.DB, activity Activity) Activity{ followers := GetActorFollowDB(db, activity.Actor.Id) - + var nActivity Activity + var tempActivity Activity for _, e := range followers { - var tempActivity Activity aFollowers := GetActorCollection(e.Id + "/followers") for _, k := range aFollowers.Items { - bFollowers := GetActorCollection(k.Id + "/following") + bFollowing := GetActorCollection(k.Id + "/following") var isFollowingActor = false - for _, n := range bFollowers.Items { + for _, n := range bFollowing.Items { if n.Id == activity.Actor.Id { isFollowingActor = true + break } } @@ -1047,19 +1048,20 @@ func AddFollowersToActivity(db *sql.DB, activity Activity) Activity{ } tempActivity.To = append(tempActivity.To, e.Id) - for _, k := range tempActivity.To { - var alreadyTo = false - for _, n := range nActivity.To { - if k == n || k == activity.Actor.Id { - alreadyTo = true - } - } + } - if !alreadyTo { - nActivity.To = append(nActivity.To, k) + for _, e := range tempActivity.To { + var alreadyTo = false + for _, k := range nActivity.To { + if e == k || e == activity.Actor.Id { + alreadyTo = true } } - } + + if !alreadyTo { + nActivity.To = append(nActivity.To, e) + } + } activity.To = nActivity.To diff --git a/static/js/posts.js b/static/js/posts.js index 7c1a3e8..69c451a 100644 --- a/static/js/posts.js +++ b/static/js/posts.js @@ -125,8 +125,8 @@ function convertContent(actorName, content, opid) { isOP = " (OP)"; } - - newContent = newContent.replace(quote, '>>' + shortURL(actorName, link) + isOP + ''); + + newContent = newContent.replace(quote, '>>' + shortURL(actorName, link) + isOP + ''); }) } -- cgit v1.2.3