diff options
author | FChannel <=> | 2021-02-16 01:11:55 -0800 |
---|---|---|
committer | FChannel <=> | 2021-02-16 01:11:55 -0800 |
commit | 38f01c1910c57af62ea362c37ad3ac2de5b24253 (patch) | |
tree | 1797446329cba7fcf046d75c42d4f1f956c213b7 | |
parent | 549e974b9911723b52503e1fde81ce0944e8b76e (diff) |
cut down catalog query to only grab op
-rw-r--r-- | CacheDatabase.go | 69 | ||||
-rw-r--r-- | Database.go | 47 | ||||
-rw-r--r-- | client.go | 53 | ||||
-rw-r--r-- | main.go | 28 | ||||
-rw-r--r-- | outboxGet.go | 22 | ||||
-rw-r--r-- | static/catalog.html | 189 | ||||
-rw-r--r-- | static/ncatalog.html | 4 |
7 files changed, 174 insertions, 238 deletions
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) @@ -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 { @@ -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 @@ -<!DOCTYPE html> -<html> - <head> - <title>{{ .Title }}</title> - </head> - <style> - a, a:link, a:visited, a:hover, a:active { - text-decoration: none - } - - a:link, a:visited, a:active { - color: black; - } - - a:hover { - color: #de0808; - } - </style> - <script> - - function getMIMEType(type) - { - re = /\/.+/g - return type.replace(re, "") - } - - function shortURL(url) - { - var check = url.replace("{{.Board.Actor}}/", "") - re = /.+\//g; - temp = re.exec(url) - if(temp[0] == "{{ .Board.Actor }}/") - { - var short = url.replace("https://", ""); - short = short.replace("http://", ""); - short = short.replace("www.", ""); - - var re = /^.{3}/g; - - var u = re.exec(short); - - re = /\w+$/g; - - u = re.exec(short); - - return u; - }else{ - var short = url.replace("https://", ""); - short = short.replace("http://", ""); - short = short.replace("www.", ""); - - var re = /^.{3}/g; - - var u = re.exec(short); - - re = /\w+$/g; - - u = re.exec(short); - - - replace = short.replace(/\/+/g, " ") - replace = replace.replace(u, " ").trim() - re = /\w+$/; - v = re.exec(replace) - - v = "f" + v[0] + "-" + u - - return v; - } - } - - </script> - <body style="background-color: #eef2fe;"> - <ul id="top" style="padding:0; display: inline;"> - {{range .Boards}} - <li style="display: inline;"><a href="{{.Location}}">{{.Name }}</a></li> - {{end}} - </ul> - {{ $board := .Board }} - {{ if $board.IsMod }} - <span style="float: right;"><a href="/{{ .Key }}/{{ .Board.Name }}">[Manage Board]</a></span> - {{ end }} - <div style="margin: 0 auto; width: 400px; margin-bottom: 100px;"> - <h1 style="color: #af0a0f;">/{{ $board.Name }}/ - {{ $board.PrefName }}</h1> - <form id="new-post" action="/post" method="post" enctype="multipart/form-data"> - <label for="name">Name:</label><br> - <input type="text" id="name" name="name" placeholder="Anonymous"><br> - <label for="options">Options:</label><br> - <input type="text" id="options" name="options"><br> - <label for="subject">Subject:</label><br> - <input type="text" id="subject" name="subject"><input type="submit" value="Post"><br> - <label for="comment">Comment:</label><br> - <textarea rows="10" cols="50" id="comment" name="comment"></textarea><br> - <input type="hidden" id="inReplyTo" name="inReplyTo" value="{{ $board.InReplyTo }}"> - <input type="hidden" id="sendTo" name="sendTo" value="{{ $board.To }}"> - <input type="hidden" id="boardName" name="boardName" value="{{ $board.Name }}"> - <input type="hidden" id="captchaCode" name="captchaCode" value="{{ $board.CaptchaCode }}"> - <input type="file" id="file" name="file"><br><br> - <label stye="display: inline-block;" for="captcha">Captcha:</label><br> - <input style="display: inline-block;" type="text" id="captcha" name="captcha"><br> - <div style="height: 65px;"> - <img src="{{ $board.Captcha }}"> - </div> - </form> - </div> - - <hr> - <ul style="margin: 0; padding: 0; display: inline"> - <li style="display: inline"><a href="/{{ $board.Name }}">[Return]</a></li> - <li style="display: inline"><a href="#bottom">[Bottom]</a></li> - <li style="display: inline"><a href="javascript:location.reload()">[Refresh]</a></li> - </ul> - <hr> - - <div style="padding: 10px; text-align: center;"> - {{ range .Posts }} - <div style="overflow: hidden; vertical-align: top; margin: 0 auto; display: inline-block; width: 180px; max-height: 320px; margin-bottom: 10px;"> - {{ if $board.IsMod }} - <a href="/delete?id={{ .Id }}">[Delete Post]</a> - {{ end }} - {{ if .Attachment }} - {{ if $board.IsMod }} - <a href="/deleteattach?id={{ .Id }}">[Delete Attachment]</a> - {{ end }} - <a id="{{ .Id }}-anchor" href="/{{ $board.Name }}/"> - <div id="media-{{ .Id }}"></div> - <script> - media = document.getElementById("media-{{ .Id }}") - if(getMIMEType({{ (index .Attachment 0).MediaType }}) == "image"){ - var img = document.createElement("img"); - img.style = "float: left; margin-right: 10px; margin-bottom: 10px; max-width: 150px; max-height: 150px; cursor: move;" - img.setAttribute("id", "img") - img.setAttribute("main", "1") - img.setAttribute("src", "{{ (index .Attachment 0).Href }}") - media.appendChild(img) - } - - if(getMIMEType({{ (index .Attachment 0).MediaType }}) == "audio"){ - var audio = document.createElement("audio") - audio.controls = 'controls' - audio.muted = 'muted' - audio.src = '{{ (index .Attachment 0).Href }}' - audio.type = '{{ (index .Attachment 0).MediaType }}' - audio.style = "float: left; margin-right: 10px; margin-bottom: 10px; width: 150px;" - audio.innerText = 'Audio is not supported.' - media.appendChild(audio) - } - - if(getMIMEType({{ (index .Attachment 0).MediaType }}) == "video"){ - var video = document.createElement("video") - video.controls = 'controls' - video.muted = 'muted' - video.src = '{{ (index .Attachment 0).Href }}' - video.type = '{{ (index .Attachment 0).MediaType }}' - video.style = "float: left; margin-right: 10px; margin-bottom: 10px; width: 150px;" - video.innerText = 'Video is not supported.' - media.appendChild(video) - } - </script> - - - {{ end }} - <div> - {{ $replies := .Replies }} - <span style="display: block">R: {{ $replies.TotalItems }}{{ if $replies.TotalImgs }}/ A: {{ $replies.TotalImgs }}{{ end }}</span> - {{ if .Name }} - <span style="display: block; color: #0f0c5d;"><b>{{ .Name }}</b></span> - {{ end }} - {{ if .Content }} - <span style="display: block">{{.Content}}</span> - {{ end }} - </div> - </a> - </div> - <script> - document.getElementById("{{ .Id }}-anchor").href = "/{{ $board.Name }}/" + shortURL("{{ .Id }}") - </script> - {{ end }} - </div> - <hr> - <ul style="margin: 0; padding: 0; display: inline"> - <li style="display: inline"><a href="/{{ $board.Name }}">[Return]</a></li> - <li style="display: inline"><a id="bottom" href="#top">[Top]</a></li> - <li style="display: inline"><a href="javascript:location.reload()">[Refresh]</a></li> - </ul> - <hr> - </body> -</html> - diff --git a/static/ncatalog.html b/static/ncatalog.html index 3638d95..c5b6a23 100644 --- a/static/ncatalog.html +++ b/static/ncatalog.html @@ -61,7 +61,9 @@ {{ end }} <div> {{ $replies := .Replies }} - <span style="display: block;">R: {{ $replies.TotalItems }}{{ if $replies.TotalImgs }}/ A: {{ $replies.TotalImgs }}{{ end }}</span> + {{ if $replies }} + <span style="display: block;">R: {{ $replies.TotalItems }}{{ if $replies.TotalImgs }}/ A: {{ $replies.TotalImgs }}{{ end }}</span> + {{ end }} {{ if .Name }} <span style="display: block; color: #0f0c5d;"><b>{{ .Name }}</b></span> {{ end }} |