diff options
author | FChannel <=> | 2021-02-18 00:58:21 -0800 |
---|---|---|
committer | FChannel <=> | 2021-02-18 00:58:21 -0800 |
commit | aa42546b2786138330ddd921b340a2b0f9898d5c (patch) | |
tree | 35dc1f7d168e8059f3429ebd9d55ee763eeea3d2 | |
parent | 38f01c1910c57af62ea362c37ad3ac2de5b24253 (diff) |
limit number of post and replies queried for page views with pagination
-rw-r--r-- | Database.go | 124 | ||||
-rw-r--r-- | client.go | 88 | ||||
-rw-r--r-- | main.go | 5 |
3 files changed, 130 insertions, 87 deletions
diff --git a/Database.go b/Database.go index 3cd9f44..b9adda3 100644 --- a/Database.go +++ b/Database.go @@ -373,7 +373,6 @@ func GetActivityFromDB(db *sql.DB, id string) Collection { 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") @@ -401,6 +400,54 @@ func GetActivityFromDB(db *sql.DB, id string) Collection { return nColl } + + +func GetObjectFromDBPage(db *sql.DB, id string, page int) Collection { + var nColl Collection + var result []ObjectBase + + query := `select count (x.id) over(), x.id, x.name, x.content, x.type, x.published, x.updated, x.attributedto, x.attachment, x.preview, x.actor from (select * from activitystream where actor=$1 and id in (select id from replies where inreplyto='') and type='Note' union select * from activitystream where actor in (select following from following where id=$1) and id in (select id from replies where inreplyto='') and type='Note' union select * from cacheactivitystream where actor in (select following from following where id=$1) and id in (select id from replies where inreplyto='') and type='Note') as x order by x.updated desc limit 8 offset $2` + + rows, err := db.Query(query, id, page * 8) + + CheckError(err, "error query object from db") + + var count int + defer rows.Close() + for rows.Next(){ + var post ObjectBase + var actor Actor + var attachID string + var previewID string + + err = rows.Scan(&count, &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 = GetObjectRepliesDBLimit(db, post, 5) + + // post.Replies.TotalItems, post.Replies.TotalImgs = GetObjectRepliesDBCount(db, post) + + post.Replies.TotalItems = postCnt + post.Replies.TotalImgs = imgCnt + + post.Attachment = GetObjectAttachment(db, attachID) + + post.Preview = GetObjectPreview(db, previewID) + + result = append(result, post) + } + + nColl.TotalItems = count + nColl.OrderedItems = result + + return nColl +} + func GetObjectFromDB(db *sql.DB, id string) Collection { var nColl Collection var result []ObjectBase @@ -555,6 +602,56 @@ func GetInReplyToDB(db *sql.DB, parent ObjectBase) []ObjectBase { return result } +func GetObjectRepliesDBLimit(db *sql.DB, parent ObjectBase, limit int) (*CollectionBase, int, int) { + + var nColl CollectionBase + var result []ObjectBase + + query := `select count(x.id) over(), sum(case when RTRIM(x.attachment) = '' then 0 else 1 end) over(), x.id, x.name, x.content, x.type, x.published, x.attributedto, x.attachment, x.preview, x.actor from (select * from activitystream where id in (select id from replies where inreplyto=$1) and type='Note' union select * from cacheactivitystream where id in (select id from replies where inreplyto=$1) and type='Note') as x order by x.published desc limit $2` + + rows, err := db.Query(query, parent.Id, limit) + + CheckError(err, "error with replies db query") + + var total int + var attach int + + defer rows.Close() + for rows.Next() { + var post ObjectBase + var actor Actor + var attachID string + var previewID string + + post.InReplyTo = append(post.InReplyTo, parent) + + err = rows.Scan(&total, &attach, &post.Id, &post.Name, &post.Content, &post.Type, &post.Published, &post.AttributedTo, &attachID, &previewID, &actor.Id) + + CheckError(err, "error with replies db scan") + + post.Actor = &actor + + var postCnt int + var imgCnt int + post.Replies, postCnt, imgCnt = GetObjectRepliesRepliesDB(db, post) + + post.Replies.TotalItems = postCnt + post.Replies.TotalImgs = imgCnt + + post.Attachment = GetObjectAttachment(db, attachID) + + post.Preview = GetObjectPreview(db, previewID) + + result = append(result, post) + } + + nColl.OrderedItems = result + + sort.Sort(ObjectBaseSortAsc(nColl.OrderedItems)) + + return &nColl, total, attach +} + func GetObjectRepliesDB(db *sql.DB, parent ObjectBase) (*CollectionBase, int, int) { var nColl CollectionBase @@ -669,7 +766,6 @@ func GetObjectRepliesReplies(db *sql.DB, parent ObjectBase) (*CollectionBase, in err = rows.Scan(&post.Id, &post.Name, &post.Content, &post.Type, &post.Published, &post.AttributedTo, &attachID, &previewID, &actor.Id) - CheckError(err, "error with replies replies db scan") post.Actor = &actor @@ -691,12 +787,14 @@ func GetObjectRepliesRepliesDB(db *sql.DB, parent ObjectBase) (*CollectionBase, var nColl CollectionBase var result []ObjectBase - query := `select id, name, content, type, published, attributedto, attachment, preview, actor from activitystream where id in (select id from replies where inreplyto=$1) and type='Note' order by published asc` + query := `select count(x.id) over(), sum(case when RTRIM(x.attachment) = '' then 0 else 1 end) over(), x.id, x.name, x.content, x.type, x.published, x.attributedto, x.attachment, x.preview, x.actor from (select * from activitystream where id in (select id from replies where inreplyto=$1) and type='Note' union select * from cacheactivitystream where id in (select id from replies where inreplyto=$1) and type='Note') as x order by x.published asc` rows, err := db.Query(query, parent.Id) CheckError(err, "error with replies replies db query") + var total int + var attach int defer rows.Close() for rows.Next() { var post ObjectBase @@ -706,8 +804,7 @@ func GetObjectRepliesRepliesDB(db *sql.DB, parent ObjectBase) (*CollectionBase, post.InReplyTo = append(post.InReplyTo, parent) - err = rows.Scan(&post.Id, &post.Name, &post.Content, &post.Type, &post.Published, &post.AttributedTo, &attachID, &previewID, &actor.Id) - + err = rows.Scan(&total, &attach, &post.Id, &post.Name, &post.Content, &post.Type, &post.Published, &post.AttributedTo, &attachID, &previewID, &actor.Id) CheckError(err, "error with replies replies db scan") @@ -722,20 +819,7 @@ func GetObjectRepliesRepliesDB(db *sql.DB, parent ObjectBase) (*CollectionBase, nColl.OrderedItems = result - remoteCollection, postc, imgc := GetObjectRepliesRepliesCache(db, parent) - - for _, e := range remoteCollection.OrderedItems { - - nColl.OrderedItems = append(nColl.OrderedItems, e) - postc = postc + 1 - if len(e.Attachment) > 0 { - imgc = imgc + 1 - } - } - - sort.Sort(ObjectBaseSortAsc(nColl.OrderedItems)) - - return &nColl, 0, 0 + return &nColl, total, attach } func CheckIfObjectOP(db *sql.DB, id string) bool { @@ -790,7 +874,7 @@ func GetObjectAttachment(db *sql.DB, id string) []ObjectBase { var attachments []ObjectBase - query := `select id, type, name, href, mediatype, size, published from activitystream where id=$1` + query := `select x.id, x.type, x.name, x.href, x.mediatype, x.size, x.published from (select id, type, name, href, mediatype, size, published from activitystream where id=$1 union select id, type, name, href, mediatype, size, published from cacheactivitystream where id=$1) as x` rows, err := db.Query(query, id) @@ -124,76 +124,16 @@ func OutboxGet(w http.ResponseWriter, r *http.Request, db *sql.DB, collection Co returnData.Key = *Key - var mergeCollection Collection - - for _, e := range collection.OrderedItems { - if e.Type != "Tombstone" { - mergeCollection.OrderedItems = append(mergeCollection.OrderedItems, e) - } - } - - domainURL := GetDomainURL(*actor) - - if domainURL == Domain { - followCol := GetObjectsFromFollow(db, *actor) - for _, e := range followCol { - if e.Type != "Tombstone" { - mergeCollection.OrderedItems = append(mergeCollection.OrderedItems, e) - } - } - } - - DeleteRemovedPosts(db, &mergeCollection) - DeleteTombstoneReplies(&mergeCollection) - - for i, _ := range mergeCollection.OrderedItems { - sort.Sort(ObjectBaseSortAsc(mergeCollection.OrderedItems[i].Replies.OrderedItems)) - } - - DeleteTombstonePosts(&mergeCollection) - sort.Sort(ObjectBaseSortDesc(mergeCollection.OrderedItems)) + DeleteRemovedPosts(db, &collection) + DeleteTombstoneReplies(&collection) + DeleteTombstonePosts(&collection) returnData.Boards = Boards + returnData.Posts = collection.OrderedItems - offset := 8 - start := page * offset - for i := 0; i < offset; i++ { - length := len(mergeCollection.OrderedItems) - current := start + i - if(current < length) { - returnData.Posts = append(returnData.Posts, mergeCollection.OrderedItems[current]) - } - } - - for i, e := range returnData.Posts { - var replies []ObjectBase - for i := 0; i < 5; i++ { - cur := len(e.Replies.OrderedItems) - i - 1 - if cur > -1 { - replies = append(replies, e.Replies.OrderedItems[cur]) - } - } - - var orderedReplies []ObjectBase - for i := 0; i < 5; i++ { - cur := len(replies) - i - 1 - if cur > - 1 { - orderedReplies = append(orderedReplies, replies[cur]) - } - } - - 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) - returnData.Posts[i].Replies.OrderedItems = orderedReplies - } - + var offset = 8 var pages []int - pageLimit := (float64(len(mergeCollection.OrderedItems)) / float64(offset)) + pageLimit := (float64(collection.TotalItems) / float64(offset)) for i := 0.0; i < pageLimit; i++ { pages = append(pages, int(i)) } @@ -441,6 +381,22 @@ func WantToServe(db *sql.DB, actorName string) (Collection, bool) { return collection, serve } +func WantToServePage(db *sql.DB, actorName string, page int) (Collection, bool) { + + var collection Collection + serve := false + + actor := GetActorByNameFromDB(db, actorName) + + if actor.Id != "" { + collection = GetObjectFromDBPage(db, actor.Id, page) + collection.Actor = &actor + return collection, true + } + + return collection, serve +} + func WantToServeCatalog(db *sql.DB, actorName string) (Collection, bool) { var collection Collection @@ -175,7 +175,10 @@ func main() { return } - collection, valid := WantToServe(db, actor.Name) + postNum := strings.Replace(r.URL.EscapedPath(), "/" + actor.Name + "/", "", 1) + + page, _ := strconv.Atoi(postNum) + collection, valid := WantToServePage(db, actor.Name, page) if valid { OutboxGet(w, r, db, collection) } |