diff options
-rw-r--r-- | CacheDatabase.go | 410 | ||||
-rw-r--r-- | Database.go | 148 | ||||
-rw-r--r-- | OutboxPost.go | 2 | ||||
-rw-r--r-- | client.go | 135 | ||||
-rw-r--r-- | main.go | 52 | ||||
-rw-r--r-- | outboxGet.go | 36 |
6 files changed, 85 insertions, 698 deletions
diff --git a/CacheDatabase.go b/CacheDatabase.go index 010ad67..ec3c409 100644 --- a/CacheDatabase.go +++ b/CacheDatabase.go @@ -4,7 +4,6 @@ import "fmt" import "time" import "database/sql" import _ "github.com/lib/pq" -import "sort" func WriteObjectToCache(db *sql.DB, obj ObjectBase) ObjectBase { if len(obj.Attachment) > 0 { @@ -21,7 +20,7 @@ func WriteObjectToCache(db *sql.DB, obj ObjectBase) ObjectBase { WriteActivitytoCache(db, obj) } - writeObjectReplyToDB(db, obj) + WriteObjectReplyToDB(db, obj) if obj.Replies != nil { for _, e := range obj.Replies.OrderedItems { @@ -159,188 +158,6 @@ 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, _, _ = 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 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 postCnt int - // var imgCnt int - post.Replies, _, _ = 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 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 - - 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") - - actor = GetRemoteActor(actor.Id) - - post.Actor = &actor - - nColl.Actor = &actor - - // var postCnt int - // var imgCnt int - post.Replies, _, _ = 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 { @@ -422,146 +239,13 @@ func WriteObjectReplyCache(db *sql.DB, obj ObjectBase) { } } -func GetObjectRepliesCache(db *sql.DB, parent ObjectBase) (*CollectionBase, int, int) { - - var nColl CollectionBase - var result []ObjectBase - - query := `select id, name, content, type, published, attributedto, attachment, preview, actor from cacheactivitystream WHERE id in (select id from replies where inreplyto=$1) and type='Note' order by published asc` - - rows, err := db.Query(query, parent.Id) - - CheckError(err, "error with replies db query") - - 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(&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, _, _ = GetObjectRepliesRepliesCache(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, 0, 0 -} - -func GetObjectRepliesRepliesCache(db *sql.DB, parent ObjectBase) (*CollectionBase, int, int) { - - var nColl CollectionBase - var result []ObjectBase - - query := `select id, name, content, type, published, attributedto, attachment, preview, actor from cacheactivitystream where id in (select id from replies where inreplyto=$1) and type='Note' order by published asc` - - rows, err := db.Query(query, parent.Id) - - CheckError(err, "error with replies replies cache query") - - 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(&post.Id, &post.Name, &post.Content, &post.Type, &post.Published, &post.AttributedTo, &attachID, &previewID, &actor.Id) - - CheckError(err, "error with replies replies cache scan") - - post.Actor = &actor - - post.Attachment = GetObjectAttachmentCache(db, attachID) - - post.Preview = GetObjectPreviewCache(db, previewID) - - result = append(result, post) - } - - nColl.OrderedItems = result - - remoteCollection, postc, imgc := GetObjectRepliesReplies(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 -} - -func GetObjectAttachmentCache(db *sql.DB, id string) []ObjectBase { - - var attachments []ObjectBase - - query := `select id, type, name, href, mediatype, size, published from cacheactivitystream where id=$1` - - rows, err := db.Query(query, id) - - CheckError(err, "could not select object attachment query") - - defer rows.Close() - for rows.Next() { - var attachment = new(ObjectBase) - - err = rows.Scan(&attachment.Id, &attachment.Type, &attachment.Name, &attachment.Href, &attachment.MediaType, &attachment.Size, &attachment.Published) - if err !=nil{ - fmt.Println("error with attachment db query") - panic(err) - } - - attachments = append(attachments, *attachment) - } - - return attachments -} - -func GetObjectPreviewCache(db *sql.DB, id string) *NestedObjectBase { - - var preview NestedObjectBase - - query := `select id, type, name, href, mediatype, size, published from cacheactivitystream where id=$1` - - rows, err := db.Query(query, id) - - CheckError(err, "could not select object preview query") +func WriteActorToCache(db *sql.DB, actorID string) { + actor := GetActor(actorID) + collection := GetActorCollection(actor.Outbox) - defer rows.Close() - for rows.Next() { - err = rows.Scan(&preview.Id, &preview.Type, &preview.Name, &preview.Href, &preview.MediaType, &preview.Size, &preview.Published) + for _, e := range collection.OrderedItems { + WriteObjectToCache(db, e) } - - return &preview } func DeleteObjectFromCache(db *sql.DB, id string) { @@ -594,6 +278,23 @@ func DeleteObjectFromCache(db *sql.DB, id string) { CheckError(err, "could not delete cache replies activitystream") } +func DeleteActorCache(db *sql.DB, actorID string) { + query := `select id from cacheactivitystream where id in (select id from cacheactivitystream where actor=$1)` + + rows, err := db.Query(query, actorID) + + CheckError(err, "error selecting actors activity from cache") + + defer rows.Close() + + for rows.Next() { + var id string + rows.Scan(&id) + + DeleteObjectFromCache(db, id) + } +} + func TombstoneObjectFromCache(db *sql.DB, id string) { datetime := time.Now().Format(time.RFC3339) @@ -621,68 +322,3 @@ func TombstoneObjectFromCache(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 replies 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 replies 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 -} - -func DeleteActorCache(db *sql.DB, actorID string) { - query := `select id from cacheactivitystream where id in (select id from cacheactivitystream where actor=$1)` - - rows, err := db.Query(query, actorID) - - - - CheckError(err, "error selecting actors activity from cache") - - defer rows.Close() - - for rows.Next() { - var id string - rows.Scan(&id) - - DeleteObjectFromCache(db, id) - } -} - -func WriteActorToCache(db *sql.DB, actorID string) { - actor := GetActor(actorID) - collection := GetActorCollection(actor.Outbox) - - for _, e := range collection.OrderedItems { - WriteObjectToCache(db, e) - } -} diff --git a/Database.go b/Database.go index e33e71f..1b71e9d 100644 --- a/Database.go +++ b/Database.go @@ -151,7 +151,7 @@ func GetBoards(db *sql.DB) []Actor { return board } -func writeObjectToDB(db *sql.DB, obj ObjectBase) ObjectBase { +func WriteObjectToDB(db *sql.DB, obj ObjectBase) ObjectBase { obj.Id = fmt.Sprintf("%s/%s", obj.Actor.Id, CreateUniqueID(db, obj.Actor.Id)) if len(obj.Attachment) > 0 { if obj.Preview.Href != "" { @@ -167,15 +167,15 @@ func writeObjectToDB(db *sql.DB, obj ObjectBase) ObjectBase { obj.Attachment[i].Published = time.Now().Format(time.RFC3339) obj.Attachment[i].Updated = time.Now().Format(time.RFC3339) obj.Attachment[i].AttributedTo = obj.Id - writeAttachmentToDB(db, obj.Attachment[i]) - writeActivitytoDBWithAttachment(db, obj, obj.Attachment[i], *obj.Preview) + WriteAttachmentToDB(db, obj.Attachment[i]) + WriteActivitytoDBWithAttachment(db, obj, obj.Attachment[i], *obj.Preview) } } else { - writeActivitytoDB(db, obj) + WriteActivitytoDB(db, obj) } - writeObjectReplyToDB(db, obj) + WriteObjectReplyToDB(db, obj) WriteWalletToDB(db, obj) return obj @@ -221,7 +221,7 @@ func WriteObjectReplyToLocalDB(db *sql.DB, id string, replyto string) { } } -func writeObjectReplyToDB(db *sql.DB, obj ObjectBase) { +func WriteObjectReplyToDB(db *sql.DB, obj ObjectBase) { for _, e := range obj.InReplyTo { query := `select id from replies where id=$1 and inreplyto=$2` @@ -299,7 +299,7 @@ func WriteWalletToDB(db *sql.DB, obj ObjectBase) { } } -func writeActivitytoDB(db *sql.DB, obj ObjectBase) { +func WriteActivitytoDB(db *sql.DB, obj ObjectBase) { obj.Name = EscapeString(obj.Name) obj.Content = EscapeString(obj.Content) @@ -315,7 +315,7 @@ func writeActivitytoDB(db *sql.DB, obj ObjectBase) { } } -func writeActivitytoDBWithAttachment(db *sql.DB, obj ObjectBase, attachment ObjectBase, preview NestedObjectBase) { +func WriteActivitytoDBWithAttachment(db *sql.DB, obj ObjectBase, attachment ObjectBase, preview NestedObjectBase) { obj.Name = EscapeString(obj.Name) obj.Content = EscapeString(obj.Content) @@ -331,7 +331,7 @@ func writeActivitytoDBWithAttachment(db *sql.DB, obj ObjectBase, attachment Obje } } -func writeAttachmentToDB(db *sql.DB, obj ObjectBase) { +func WriteAttachmentToDB(db *sql.DB, obj ObjectBase) { query := `insert into activitystream (id, type, name, href, published, updated, attributedTo, mediatype, size) values ($1, $2, $3, $4, $5, $6, $7, $8, $9)` _, e := db.Exec(query, obj.Id ,obj.Type, obj.Name, obj.Href, obj.Published, obj.Updated, obj.AttributedTo, obj.MediaType, obj.Size) @@ -379,14 +379,12 @@ func GetActivityFromDB(db *sql.DB, id string) Collection { post.Actor = &actor - // var postCnt int - // var imgCnt int - post.Replies, _, _ = GetObjectRepliesDB(db, post) - - // post.Replies.TotalItems, post.Replies.TotalImgs = GetObjectRepliesDBCount(db, post) + var postCnt int + var imgCnt int + post.Replies, postCnt, imgCnt = GetObjectRepliesDB(db, post) - // post.Replies.TotalItems = post.Replies.TotalItems + postCnt - // post.Replies.TotalImgs = post.Replies.TotalImgs + imgCnt + post.Replies.TotalItems = postCnt + post.Replies.TotalImgs = imgCnt post.Attachment = GetObjectAttachment(db, attachID) @@ -400,8 +398,6 @@ 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 @@ -430,8 +426,6 @@ func GetObjectFromDBPage(db *sql.DB, id string, page int) Collection { 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 @@ -471,14 +465,12 @@ func GetObjectFromDB(db *sql.DB, id string) Collection { post.Actor = &actor - // var postCnt int - // var imgCnt int - post.Replies, _, _ = GetObjectRepliesDB(db, post) - - // post.Replies.TotalItems, post.Replies.TotalImgs = GetObjectRepliesDBCount(db, post) + var postCnt int + var imgCnt int + post.Replies, postCnt, imgCnt = GetObjectRepliesDB(db, post) - // post.Replies.TotalItems = post.Replies.TotalItems + postCnt - // post.Replies.TotalImgs = post.Replies.TotalImgs + imgCnt + post.Replies.TotalItems = postCnt + post.Replies.TotalImgs = imgCnt post.Attachment = GetObjectAttachment(db, attachID) @@ -537,7 +529,7 @@ 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 type='Note' order by updated asc` + query := `select x.id, x.name, x.content, x.type, x.published, x.updated, x.attributedto, x.attachment, x.preview, x.actor from (select id, name, content, type, published, updated, attributedto, attachment, preview, actor from activitystream where id=$1 and type='Note' union select id, name, content, type, published, updated, attributedto, attachment, preview, actor from cacheactivitystream where id=$1 and type='Note') as x` rows, err := db.Query(query, postID) @@ -560,14 +552,12 @@ func GetObjectByIDFromDB(db *sql.DB, postID string) Collection { nColl.Actor = &actor - // var postCnt int - // var imgCnt int - post.Replies, _, _ = GetObjectRepliesDB(db, post) - - // post.Replies.TotalItems, post.Replies.TotalImgs = GetObjectRepliesDBCount(db, post) + var postCnt int + var imgCnt int + post.Replies, postCnt, imgCnt = GetObjectRepliesDB(db, post) - // post.Replies.TotalItems = post.Replies.TotalItems + postCnt - // post.Replies.TotalImgs = post.Replies.TotalImgs + imgCnt + post.Replies.TotalItems = postCnt + post.Replies.TotalImgs = imgCnt post.Attachment = GetObjectAttachment(db, attachID) @@ -613,8 +603,8 @@ func GetObjectRepliesDBLimit(db *sql.DB, parent ObjectBase, limit int) (*Collect CheckError(err, "error with replies db query") - var total int - var attach int + var postCount int + var attachCount int defer rows.Close() for rows.Next() { @@ -625,7 +615,7 @@ func GetObjectRepliesDBLimit(db *sql.DB, parent ObjectBase, limit int) (*Collect 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) + err = rows.Scan(&postCount, &attachCount, &post.Id, &post.Name, &post.Content, &post.Type, &post.Published, &post.AttributedTo, &attachID, &previewID, &actor.Id) CheckError(err, "error with replies db scan") @@ -649,7 +639,7 @@ func GetObjectRepliesDBLimit(db *sql.DB, parent ObjectBase, limit int) (*Collect sort.Sort(ObjectBaseSortAsc(nColl.OrderedItems)) - return &nColl, total, attach + return &nColl, postCount, attachCount } func GetObjectRepliesDB(db *sql.DB, parent ObjectBase) (*CollectionBase, int, int) { @@ -657,11 +647,14 @@ func GetObjectRepliesDB(db *sql.DB, parent ObjectBase) (*CollectionBase, int, in 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 db query") + CheckError(err, "error with replies db query") + + var postCount int + var attachCount int defer rows.Close() for rows.Next() { @@ -672,20 +665,18 @@ func GetObjectRepliesDB(db *sql.DB, parent ObjectBase) (*CollectionBase, int, in 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(&postCount, &attachCount, &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, _, _ = GetObjectRepliesRepliesDB(db, post) + var postCnt int + var imgCnt int + post.Replies, postCnt, imgCnt = GetObjectRepliesRepliesDB(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.Replies.TotalItems = postCnt + post.Replies.TotalImgs = imgCnt post.Attachment = GetObjectAttachment(db, attachID) @@ -696,56 +687,11 @@ func GetObjectRepliesDB(db *sql.DB, parent ObjectBase) (*CollectionBase, int, in nColl.OrderedItems = result - - remoteCollection, postc, imgc := GetObjectRepliesCache(db, parent) - - for _, e := range remoteCollection.OrderedItems { - nColl.OrderedItems = append(nColl.OrderedItems, e) - postc = postc + 1 - if len(e.Attachment) > 0 { - imgc = imgc + 1 - } - } - - return &nColl, 0, 0 -} - -func GetObjectRepliesRemote(db *sql.DB, parent ObjectBase) CollectionBase { - var nColl CollectionBase - var result []ObjectBase - query := `select id from replies where id not in (select id from activitystream) and inreplyto=$1` - - rows, err := db.Query(query, parent.Id) - - CheckError(err, "could not get remote id query") - - defer rows.Close() - for rows.Next() { - var id string - rows.Scan(&id) - - cacheColl := GetObjectFromCache(db, id) - - if len(cacheColl.OrderedItems) < 1 { - cacheColl = GetCollectionFromID(id) - for _, e := range cacheColl.OrderedItems { - WriteObjectToCache(db, e) - } - } - - for _, e := range cacheColl.OrderedItems { - result = append(result, e) - } - } - - nColl.OrderedItems = result - - return nColl + return &nColl, postCount, attachCount } func GetObjectRepliesReplies(db *sql.DB, parent ObjectBase) (*CollectionBase, int, int) { - var nColl CollectionBase var result []ObjectBase @@ -793,8 +739,8 @@ func GetObjectRepliesRepliesDB(db *sql.DB, parent ObjectBase) (*CollectionBase, CheckError(err, "error with replies replies db query") - var total int - var attach int + var postCount int + var attachCount int defer rows.Close() for rows.Next() { var post ObjectBase @@ -804,7 +750,7 @@ func GetObjectRepliesRepliesDB(db *sql.DB, parent ObjectBase) (*CollectionBase, 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) + err = rows.Scan(&postCount, &attachCount, &post.Id, &post.Name, &post.Content, &post.Type, &post.Published, &post.AttributedTo, &attachID, &previewID, &actor.Id) CheckError(err, "error with replies replies db scan") @@ -819,7 +765,7 @@ func GetObjectRepliesRepliesDB(db *sql.DB, parent ObjectBase) (*CollectionBase, nColl.OrderedItems = result - return &nColl, total, attach + return &nColl, postCount, attachCount } func CheckIfObjectOP(db *sql.DB, id string) bool { @@ -890,8 +836,8 @@ func GetObjectPreview(db *sql.DB, id string) *NestedObjectBase { var preview NestedObjectBase - 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) CheckError(err, "could not select object preview query") diff --git a/OutboxPost.go b/OutboxPost.go index 6483a27..61175be 100644 --- a/OutboxPost.go +++ b/OutboxPost.go @@ -46,7 +46,7 @@ func ParseOutboxRequest(w http.ResponseWriter, r *http.Request, db *sql.DB) { nObj.Actor = &act nObj.Actor.Id = Domain + "/" + actor.Name - nObj = writeObjectToDB(db, nObj) + nObj = WriteObjectToDB(db, nObj) activity := CreateActivity("Create", nObj) activity = AddFollowersToActivity(db, activity) MakeActivityRequest(db, activity) @@ -149,7 +149,7 @@ func CatalogGet(w http.ResponseWriter, r *http.Request, db *sql.DB, collection C t := template.Must(template.ParseFiles("./static/main.html", "./static/ncatalog.html", "./static/top.html")) actor := collection.Actor - + var returnData PageData returnData.Board.Name = actor.Name returnData.Board.PrefName = actor.PreferredUsername @@ -201,10 +201,8 @@ func PostGet(w http.ResponseWriter, r *http.Request, db *sql.DB){ returnData.Board.Domain = Domain returnData.Board.Restricted = actor.Restricted - if GetDomainURL(actor) != "" { - returnData.Board.Captcha = Domain + "/" + GetRandomCaptcha(db) - returnData.Board.CaptchaCode = GetCaptchaCode(returnData.Board.Captcha) - } + returnData.Board.Captcha = Domain + "/" + GetRandomCaptcha(db) + returnData.Board.CaptchaCode = GetCaptchaCode(returnData.Board.Captcha) returnData.Title = "/" + returnData.Board.Name + "/ - " + returnData.Board.PrefName @@ -214,7 +212,7 @@ func PostGet(w http.ResponseWriter, r *http.Request, db *sql.DB){ re = regexp.MustCompile("f\\w+-\\w+") - if re.MatchString(path) { + if re.MatchString(path) { // if non local actor post name := GetActorFollowNameFromPath(path) followActors := GetActorsFollowFromName(actor, name) followCollection := GetActorsFollowPostFromId(db, followActors, postId) @@ -224,57 +222,24 @@ func PostGet(w http.ResponseWriter, r *http.Request, db *sql.DB){ DeleteRemovedPosts(db, &followCollection) DeleteTombstoneReplies(&followCollection) - for i, _ := range followCollection.OrderedItems { - sort.Sort(ObjectBaseSortAsc(followCollection.OrderedItems[i].Replies.OrderedItems)) - } - if len(followCollection.OrderedItems) > 0 { returnData.Board.InReplyTo = followCollection.OrderedItems[0].Id returnData.Posts = append(returnData.Posts, followCollection.OrderedItems[0]) - sort.Sort(ObjectBaseSortAsc(returnData.Posts[0].Replies.OrderedItems)) } - } else { - returnData.Board.InReplyTo = inReplyTo - collection := GetActorCollectionByID(db, inReplyTo) + collection := GetObjectByIDFromDB(db, inReplyTo) - returnData.Board.Post.Actor = collection.Actor + returnData.Board.Post.Actor = collection.Actor + returnData.Board.InReplyTo = inReplyTo DeleteRemovedPosts(db, &collection) - - for i, e := range collection.OrderedItems { - var replies CollectionBase - for _, k := range e.Replies.OrderedItems { - if k.Type != "Tombstone" { - replies.OrderedItems = append(replies.OrderedItems, k) - } else { - collection.OrderedItems[i].Replies.TotalItems = collection.OrderedItems[i].Replies.TotalItems - 1 - if k.Preview.Id != "" { - collection.OrderedItems[i].Replies.TotalImgs = collection.OrderedItems[i].Replies.TotalImgs - 1 - } - } - } - collection.TotalItems = collection.OrderedItems[i].Replies.TotalItems - collection.TotalImgs = collection.OrderedItems[i].Replies.TotalImgs - collection.OrderedItems[i].Replies = &replies - sort.Sort(ObjectBaseSortAsc(e.Replies.OrderedItems)) - } + DeleteTombstoneReplies(&collection) if len(collection.OrderedItems) > 0 { returnData.Posts = append(returnData.Posts, collection.OrderedItems[0]) - sort.Sort(ObjectBaseSortAsc(returnData.Posts[0].Replies.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) } @@ -328,40 +293,6 @@ func GetBoardCollection(db *sql.DB) []Board { return collection } -func WantToServe(db *sql.DB, actorName string) (Collection, bool) { - - var collection Collection - serve := false - - boardActor := GetActorByNameFromDB(db, actorName) - - if boardActor.Id != "" { - collection = GetActorCollectionDB(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 = GetActorCollectionDB(db, boardActor) - } else { - collection = GetActorCollectionCache(db, boardActor) - } - collection.Actor = &boardActor - return collection, serve - } - } - - return collection, serve -} - func WantToServePage(db *sql.DB, actorName string, page int) (Collection, bool) { var collection Collection @@ -383,32 +314,14 @@ func WantToServeCatalog(db *sql.DB, actorName string) (Collection, bool) { var collection Collection serve := false - boardActor := GetActorByNameFromDB(db, actorName) + actor := GetActorByNameFromDB(db, actorName) - if boardActor.Id != "" { - collection = GetActorCollectionDBCatalog(db, boardActor) + if actor.Id != "" { + collection = GetObjectFromDBCatalog(db, actor.Id) + collection.Actor = &actor 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 } @@ -432,18 +345,6 @@ func GetCaptchaCode(captcha string) string { return code } -func GetDomainURL(actor Actor) string { - re := regexp.MustCompile("(https://|http://)?(www)?.+/") - - domainURL := re.FindString(actor.Id) - - re = regexp.MustCompile("/$") - - domainURL = re.ReplaceAllString(domainURL, "") - - return domainURL -} - func DeleteTombstoneReplies(collection *Collection) { for i, e := range collection.OrderedItems { @@ -681,24 +582,16 @@ func GetActorsFollowPostFromId(db *sql.DB, actors []string, id string) Collectio var collection Collection for _, e := range actors { - tempCol := GetActorCollectionByID(db, e + "/" + id) + tempCol := GetObjectByIDFromDB(db, e + "/" + id) if len(tempCol.OrderedItems) > 0 { collection = tempCol + return 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") @@ -1301,58 +1301,6 @@ 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.Actor = &actor - - collection.TotalItems = GetObjectPostsTotalCache(db, actor) - collection.TotalImgs = GetObjectImgsTotalCache(db, actor) - - 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 - - collection.OrderedItems = GetObjectFromDB(db, actor.Id).OrderedItems - - collection.Actor = &actor - - collection.TotalItems = GetObjectPostsTotalDB(db, actor) - collection.TotalImgs = GetObjectImgsTotalDB(db, actor) - - 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 8ee6587..49b7bd5 100644 --- a/outboxGet.go +++ b/outboxGet.go @@ -22,42 +22,6 @@ func GetActorOutbox(w http.ResponseWriter, r *http.Request, db *sql.DB) { w.Write(enc) } -func GetObjectsFromFollow(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 = GetObjectFromCache(db, e.Id) - } else { - followOutbox = GetObjectFromDB(db, e.Id) - } - for _, e := range followOutbox.OrderedItems { - followObj = append(followObj, e) - } - } - 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 |