From 38f01c1910c57af62ea362c37ad3ac2de5b24253 Mon Sep 17 00:00:00 2001 From: FChannel <=> Date: Tue, 16 Feb 2021 01:11:55 -0800 Subject: cut down catalog query to only grab op --- CacheDatabase.go | 69 +++++++++++-------- Database.go | 47 ++++++++++++- client.go | 53 +++++++++++---- main.go | 28 +++++++- outboxGet.go | 22 +++++- static/catalog.html | 189 --------------------------------------------------- static/ncatalog.html | 4 +- 7 files changed, 174 insertions(+), 238 deletions(-) delete mode 100644 static/catalog.html diff --git a/CacheDatabase.go b/CacheDatabase.go index 26d7d8d..010ad67 100644 --- a/CacheDatabase.go +++ b/CacheDatabase.go @@ -252,6 +252,47 @@ func GetObjectFromCache(db *sql.DB, id string) Collection { return nColl } +func GetObjectFromCacheCatalog(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 actor=$1 and id in (select id from replies where inreplyto='') and type='Note' order by updated asc` + + rows, err := db.Query(query, id) + + 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.Updated, &post.AttributedTo, &attachID, &previewID, &actor.Id) + + CheckError(err, "error scan object into post struct cache") + + post.Actor = &actor + + var replies CollectionBase + + post.Replies = &replies + + post.Replies.TotalItems, post.Replies.TotalImgs = GetObjectRepliesCount(db, post) + + post.Attachment = GetObjectAttachmentCache(db, attachID) + + post.Preview = GetObjectPreviewCache(db, previewID) + + result = append(result, post) + } + + nColl.OrderedItems = result + + return nColl +} + func GetObjectByIDFromCache(db *sql.DB, postID string) Collection { var nColl Collection var result []ObjectBase @@ -479,34 +520,6 @@ func GetObjectRepliesRepliesCache(db *sql.DB, parent ObjectBase) (*CollectionBas return &nColl, 0, 0 } -func GetObjectRepliesCacheCount(db *sql.DB, parent ObjectBase) (int, int) { - - var countId int - var countImg int - - query := `select count(id) from replies where inreplyto=$1 and id in (select id from activitystream where type='Note')` - - rows, err := db.Query(query, parent.Id) - - CheckError(err, "error with replies count db query") - - defer rows.Close() - rows.Next() - rows.Scan(&countId) - - query = `select count(attachment) from activitystream where id in (select id from replies where inreplyto=$1) and attachment != ''` - - rows, err = db.Query(query, parent.Id) - - CheckError(err, "error with select attachment count db query") - - defer rows.Close() - rows.Next() - rows.Scan(&countImg) - - return countId, countImg -} - func GetObjectAttachmentCache(db *sql.DB, id string) []ObjectBase { var attachments []ObjectBase diff --git a/Database.go b/Database.go index d027b43..3cd9f44 100644 --- a/Database.go +++ b/Database.go @@ -445,6 +445,47 @@ func GetObjectFromDB(db *sql.DB, id string) Collection { return nColl } +func GetObjectFromDBCatalog(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 activitystream where actor=$1 and id in (select id from replies where inreplyto='') and type='Note' 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(&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 replies CollectionBase + + post.Replies = &replies + + post.Replies.TotalItems, post.Replies.TotalImgs = GetObjectRepliesCount(db, post) + + post.Attachment = GetObjectAttachment(db, attachID) + + post.Preview = GetObjectPreview(db, previewID) + + result = append(result, post) + } + + nColl.OrderedItems = result + + return nColl +} + func GetObjectByIDFromDB(db *sql.DB, postID string) Collection { var nColl Collection var result []ObjectBase @@ -717,12 +758,12 @@ func CheckIfObjectOP(db *sql.DB, id string) bool { return false } -func GetObjectRepliesDBCount(db *sql.DB, parent ObjectBase) (int, int) { +func GetObjectRepliesCount(db *sql.DB, parent ObjectBase) (int, int) { var countId int var countImg int - query := `select count(id) from replies where inreplyto=$1 and id in (select id from activitystream where type='Note')` + query := `select count(id) from replies where inreplyto=$1 and id in (select id from activitystream where type='Note' union select id from cacheactivitystream where type='Note')` rows, err := db.Query(query, parent.Id) @@ -732,7 +773,7 @@ func GetObjectRepliesDBCount(db *sql.DB, parent ObjectBase) (int, int) { rows.Next() rows.Scan(&countId) - query = `select count(attachment) from activitystream where id in (select id from replies where inreplyto=$1) and attachment != ''` + query = `select count(attach) from (select attachment from activitystream where id in (select id from replies where inreplyto=$1) and attachment != '' union select attachment from cacheactivitystream where id in (select id from replies where inreplyto=$1) and attachment != '') as attach` rows, err = db.Query(query, parent.Id) diff --git a/client.go b/client.go index f82c8f7..01dc894 100644 --- a/client.go +++ b/client.go @@ -212,9 +212,9 @@ func CatalogGet(w http.ResponseWriter, r *http.Request, db *sql.DB, collection C var mergeCollection Collection - for _, e := range collection.OrderedItems { - mergeCollection.OrderedItems = append(mergeCollection.OrderedItems, e) - } + + + mergeCollection.OrderedItems = collection.OrderedItems domainURL := GetDomainURL(*actor) @@ -229,7 +229,7 @@ func CatalogGet(w http.ResponseWriter, r *http.Request, db *sql.DB, collection C DeleteRemovedPosts(db, &mergeCollection) DeleteTombstonePosts(&mergeCollection) - + sort.Sort(ObjectBaseSortDesc(mergeCollection.OrderedItems)) var returnData PageData @@ -255,15 +255,6 @@ func CatalogGet(w http.ResponseWriter, r *http.Request, db *sql.DB, collection C returnData.Posts = mergeCollection.OrderedItems - for i, _ := range returnData.Posts { - for _, e := range returnData.Posts[i].Replies.OrderedItems { - if len(e.Attachment) > 0 { - returnData.Posts[i].Replies.TotalImgs = returnData.Posts[i].Replies.TotalImgs + 1 - } - } - returnData.Posts[i].Replies.TotalItems = len(returnData.Posts[i].Replies.OrderedItems) - } - t.ExecuteTemplate(w, "layout", returnData) } @@ -450,6 +441,40 @@ func WantToServe(db *sql.DB, actorName string) (Collection, bool) { return collection, serve } +func WantToServeCatalog(db *sql.DB, actorName string) (Collection, bool) { + + var collection Collection + serve := false + + boardActor := GetActorByNameFromDB(db, actorName) + + if boardActor.Id != "" { + collection = GetActorCollectionDBCatalog(db, boardActor) + return collection, true + } + + for _, e := range FollowingBoards { + boardActor := GetActorFromDB(db, e.Id) + + if boardActor.Id == "" { + boardActor = GetActor(e.Id) + } + + if boardActor.Name == actorName { + serve = true + if IsActorLocal(db, boardActor.Id) { + collection = GetActorCollectionDBCatalog(db, boardActor) + } else { + collection = GetActorCollectionCacheCatalog(db, boardActor) + } + collection.Actor = &boardActor + return collection, serve + } + } + + return collection, serve +} + func StripTransferProtocol(value string) string { re := regexp.MustCompile("(http://|https://)?(www.)?") @@ -538,7 +563,7 @@ func DeleteRemovedPosts(db *sql.DB, collection *Collection) { } } } - + for i, r := range e.Replies.OrderedItems { for _, k := range removed { if r.Id == k.ID { diff --git a/main.go b/main.go index 3dfad82..bfc2846 100644 --- a/main.go +++ b/main.go @@ -204,7 +204,7 @@ func main() { } if actorCatalog { - collection, valid := WantToServe(db, actor.Name) + collection, valid := WantToServeCatalog(db, actor.Name) if valid { CatalogGet(w, r, db, collection) } @@ -1311,6 +1311,19 @@ func GetActorCollectionCache(db *sql.DB, actor Actor) Collection { return collection } +func GetActorCollectionCacheCatalog(db *sql.DB, actor Actor) Collection { + var collection Collection + + collection.OrderedItems = GetObjectFromCacheCatalog(db, actor.Id).OrderedItems + + collection.Actor = &actor + + collection.TotalItems = GetObjectPostsTotalCache(db, actor) + collection.TotalImgs = GetObjectImgsTotalCache(db, actor) + + return collection +} + func GetActorCollectionDB(db *sql.DB, actor Actor) Collection { var collection Collection @@ -1324,6 +1337,19 @@ func GetActorCollectionDB(db *sql.DB, actor Actor) Collection { return collection } +func GetActorCollectionDBCatalog(db *sql.DB, actor Actor) Collection { + var collection Collection + + collection.OrderedItems = GetObjectFromDBCatalog(db, actor.Id).OrderedItems + + collection.Actor = &actor + + collection.TotalItems = GetObjectPostsTotalDB(db, actor) + collection.TotalImgs = GetObjectImgsTotalDB(db, actor) + + return collection +} + func GetActorCollection(collection string) Collection { var nCollection Collection diff --git a/outboxGet.go b/outboxGet.go index a42a3d7..8ee6587 100644 --- a/outboxGet.go +++ b/outboxGet.go @@ -40,6 +40,24 @@ func GetObjectsFromFollow(db *sql.DB, actor Actor) []ObjectBase { return followObj } +func GetObjectsFromFollowCatalog(db *sql.DB, actor Actor) []ObjectBase { + var followingCol Collection + var followObj []ObjectBase + followingCol = GetActorCollection(actor.Following) + for _, e := range followingCol.Items { + var followOutbox Collection + if !IsActorLocal(db, e.Id) { + followOutbox = GetObjectFromCacheCatalog(db, e.Id) + } else { + followOutbox = GetObjectFromDBCatalog(db, e.Id) + } + for _, e := range followOutbox.OrderedItems { + followObj = append(followObj, e) + } + } + return followObj +} + func GetCollectionFromPath(db *sql.DB, path string) Collection { var nColl Collection @@ -71,7 +89,7 @@ func GetCollectionFromPath(db *sql.DB, path string) Collection { var imgCnt int post.Replies, postCnt, imgCnt = GetObjectRepliesDB(db, post) - post.Replies.TotalItems, post.Replies.TotalImgs = GetObjectRepliesDBCount(db, post) + post.Replies.TotalItems, post.Replies.TotalImgs = GetObjectRepliesCount(db, post) post.Replies.TotalItems = post.Replies.TotalItems + postCnt post.Replies.TotalImgs = post.Replies.TotalImgs + imgCnt @@ -117,7 +135,7 @@ func GetObjectFromPath(db *sql.DB, path string) ObjectBase{ nObj.Replies, postCnt, imgCnt = GetObjectRepliesDB(db, nObj) - nObj.Replies.TotalItems, nObj.Replies.TotalImgs = GetObjectRepliesDBCount(db, nObj) + nObj.Replies.TotalItems, nObj.Replies.TotalImgs = GetObjectRepliesCount(db, nObj) nObj.Replies.TotalItems = nObj.Replies.TotalItems + postCnt nObj.Replies.TotalImgs = nObj.Replies.TotalImgs + imgCnt diff --git a/static/catalog.html b/static/catalog.html deleted file mode 100644 index b5b361e..0000000 --- a/static/catalog.html +++ /dev/null @@ -1,189 +0,0 @@ - - -
-