diff options
author | FChannel <=> | 2021-01-23 22:37:20 -0800 |
---|---|---|
committer | FChannel <=> | 2021-01-23 22:37:20 -0800 |
commit | 2ca021e2c6599b39e6225fdbd449093517a34418 (patch) | |
tree | de65bb297a63765f4e471ddd488975466e438df1 | |
parent | 000c206417a91d0f910cba35d16d433a1f038530 (diff) |
caching progress
-rw-r--r-- | CacheDatabase.go | 83 | ||||
-rw-r--r-- | Database.go | 9 | ||||
-rw-r--r-- | Follow.go | 2 | ||||
-rw-r--r-- | OutboxPost.go | 3 | ||||
-rw-r--r-- | client.go | 5 | ||||
-rw-r--r-- | clientkey | 2 | ||||
-rw-r--r-- | main.go | 32 | ||||
-rw-r--r-- | outboxGet.go | 6 |
8 files changed, 118 insertions, 24 deletions
diff --git a/CacheDatabase.go b/CacheDatabase.go index 4add356..93b3443 100644 --- a/CacheDatabase.go +++ b/CacheDatabase.go @@ -19,7 +19,8 @@ func WriteObjectToCache(db *sql.DB, obj ObjectBase) ObjectBase { WriteActivitytoCache(db, obj) } - WriteObjectReplyToCache(db, obj) + WriteObjectReplyToCache(db, obj) + WriteObjectReplyCache(db, obj) return obj } @@ -193,8 +194,7 @@ 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 activitystream where actor=$1 and id in (select id from cachereplies where inreplyto='') and type='Note' order by updated asc` + query := `select id, name, content, type, published, updated, attributedto, attachment, preview, actor from cacheactivitystream where actor=$1 and id in (select id from cachereplies where inreplyto='') and type='Note' order by updated asc` rows, err := db.Query(query, id) @@ -235,6 +235,7 @@ func GetObjectFromCache(db *sql.DB, id string) Collection { } func WriteObjectReplyToCache(db *sql.DB, obj ObjectBase) { + for i, e := range obj.InReplyTo { if(i == 0 || IsReplyInThread(db, obj.InReplyTo[0].Id, e.Id)){ @@ -263,6 +264,52 @@ func WriteObjectReplyToCache(db *sql.DB, obj ObjectBase) { } } } + + if len(obj.InReplyTo) < 1 { + query := `insert into cachereplies (id, inreplyto) values ($1, $2)` + + _, err := db.Exec(query, obj.Id, "") + + if err != nil{ + fmt.Println("error inserting replies cache") + panic(err) + } + } +} + +func WriteObjectReplyCache(db *sql.DB, obj ObjectBase) { + + for _, e := range obj.Replies.OrderedItems { + + query := `select inreplyto from cachereplies where id=$1` + + rows, err := db.Query(query, obj.Id) + + CheckError(err, "error selecting obj id cache reply") + + var inreplyto string + defer rows.Close() + rows.Next() + rows.Scan(&inreplyto) + + if inreplyto != "" { + return + } + + query = `insert into cachereplies (id, inreplyto) values ($1, $2)` + + _, err = db.Exec(query, e.Id, obj.Id) + + if err != nil{ + fmt.Println("error inserting replies cache") + panic(err) + } + + if !IsObjectLocal(db, e.Id) { + WriteObjectToCache(db, e) + } + + } } func GetObjectRepliesCache(db *sql.DB, parent ObjectBase) (*CollectionBase, int, int) { @@ -515,3 +562,33 @@ func GetObjectImgsTotalCache(db *sql.DB, actor Actor) int{ return count } + +func DeleteActorCache(db *sql.DB, actorID string) { + fmt.Println("delete actor to cache") + 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) { + fmt.Println("writing actor to cache") + actor := GetActor(actorID) + collection := GetActorCollection(actor.Outbox) + + for _, e := range collection.OrderedItems { + WriteObjectToCache(db, e) + } +} diff --git a/Database.go b/Database.go index f79bf50..275bfcd 100644 --- a/Database.go +++ b/Database.go @@ -218,7 +218,6 @@ func writeObjectReplyToDB(db *sql.DB, obj ObjectBase) { } } - func WriteWalletToDB(db *sql.DB, obj ObjectBase) { for _, e := range obj.Option { if e == "wallet" { @@ -401,7 +400,6 @@ func GetInReplyToDB(db *sql.DB, parent ObjectBase) []ObjectBase { return result } - func GetObjectRepliesDB(db *sql.DB, parent ObjectBase) (*CollectionBase, int, int) { var nColl CollectionBase @@ -655,8 +653,6 @@ func GetObjectImgsTotalDB(db *sql.DB, actor Actor) int{ return count } - - func DeletePreviewFromFile(db *sql.DB, id string) { var query = `select href, type from activitystream where id in (select preview from activitystream where id=$1)` @@ -716,7 +712,6 @@ func DeleteAttachmentFromFile(db *sql.DB, id string) { DeleteAttachmentFromDB(db, id) } - func DeletePreviewRepliesFromDB(db *sql.DB, id string) { var query = `select id from activitystream where id in (select id from replies where inreplyto=$1)` // var query = fmt.Sprintf("select id from activitystream where id (select id from replies where inreplyto='%s');", id) @@ -757,7 +752,6 @@ func DeleteAttachmentRepliesFromDB(db *sql.DB, id string) { } } - func DeleteAttachmentFromDB(db *sql.DB, id string) { datetime := time.Now().Format(time.RFC3339) @@ -787,7 +781,6 @@ func DeleteObjectRepliedTo(db *sql.DB, id string){ CheckError(err, "error with delete object replies") } - func DeleteObjectFromDB(db *sql.DB, id string) { datetime := time.Now().Format(time.RFC3339) var query = `update activitystream set type='Tombstone', name='', content='', attributedto='deleted', updated=$1, deleted=$2 where id=$3` @@ -986,5 +979,3 @@ func GetActorReportedDB(db *sql.DB, id string) []ObjectBase { return nObj } - - @@ -200,9 +200,11 @@ func SetActorFollowingDB(db *sql.DB, activity Activity) Activity { if alreadyFollow { query = `delete from following where id=$1 and following=$2` activity.Summary = activity.Object.Actor.Id + " Unfollowing " + activity.Actor.Id + DeleteActorCache(db, activity.Actor.Id) } else { query = `insert into following (id, following) values ($1, $2)` activity.Summary = activity.Object.Actor.Id + " Following " + activity.Actor.Id + WriteActorToCache(db, activity.Actor.Id) } _, err := db.Exec(query, activity.Object.Actor.Id, activity.Actor.Id) diff --git a/OutboxPost.go b/OutboxPost.go index 9fb8710..f66c234 100644 --- a/OutboxPost.go +++ b/OutboxPost.go @@ -49,6 +49,7 @@ func ParseOutboxRequest(w http.ResponseWriter, r *http.Request, db *sql.DB) { nObj = writeObjectToDB(db, nObj) activity := CreateActivity("Create", nObj) + activity = AddFollowersToActivity(db, activity) MakeActivityRequest(activity) var id string @@ -511,6 +512,8 @@ func ParseInboxRequest(w http.ResponseWriter, r *http.Request, db *sql.DB) { for _, e := range activity.Object.InReplyTo { if IsObjectLocal(db, e.Id) { WriteObjectReplyToLocalDB(db, activity.Object.Id, e.Id) + } else if IsObjectCached(db, e.Id) { + WriteObjectToCache(db, *activity.Object) } } break @@ -138,7 +138,7 @@ func OutboxGet(w http.ResponseWriter, r *http.Request, db *sql.DB, collection Co domainURL := GetDomainURL(*actor) if domainURL == Domain { - followCol := GetObjectsFromFollow(*actor) + followCol := GetObjectsFromFollow(db, *actor) for _, e := range followCol { if e.Type != "Tombstone" { mergeCollection.OrderedItems = append(mergeCollection.OrderedItems, e) @@ -217,9 +217,8 @@ func CatalogGet(w http.ResponseWriter, r *http.Request, db *sql.DB, collection C domainURL := GetDomainURL(*actor) if domainURL == Domain { - followCol := GetObjectsFromFollow(*actor) + followCol := GetObjectsFromFollow(db, *actor) for _, e := range followCol { - mergeCollection.OrderedItems = append(mergeCollection.OrderedItems, e) } } @@ -1 +1 @@ -f0e6a4255a3cab9679c1dca374632efd
\ No newline at end of file +fcc28cdb95d2983a10efdc8f3b8a9503
\ No newline at end of file @@ -80,8 +80,6 @@ func main() { path = re.ReplaceAllString(path, "") } - - var mainActor bool var mainInbox bool var mainOutbox bool @@ -1024,6 +1022,16 @@ func CreateObject(objType string) ObjectBase { return nObj } +func AddFollowersToActivity(db *sql.DB, activity Activity) Activity{ + followers := GetActorFollowingDB(db, activity.Actor.Id) + + for _, e := range followers { + activity.To = append(activity.To, e.Id) + } + + return activity +} + func CreateActivity(activityType string, obj ObjectBase) Activity { var newActivity Activity @@ -1370,9 +1378,23 @@ func IsActorLocal(db *sql.DB, id string) bool { func IsObjectLocal(db *sql.DB, id string) bool { - query := fmt.Sprintf("select id from activitystream where id='%s'", id) + query := `select id from activitystream where id=$1` - rows, err := db.Query(query) + rows, err := db.Query(query, id) + + defer rows.Close() + + if err != nil { + return false + } + + return true +} + +func IsObjectCached(db *sql.DB, id string) bool { + + query := `select id from cacheactivitystream where id=$1` + rows, err := db.Query(query, id) defer rows.Close() @@ -1525,6 +1547,8 @@ func MakeActivityRequest(activity Activity) { j, _ := json.MarshalIndent(activity, "", "\t") for _, e := range activity.To { + + fmt.Println(e) actor := GetActor(e) diff --git a/outboxGet.go b/outboxGet.go index 8eec74e..8c178a2 100644 --- a/outboxGet.go +++ b/outboxGet.go @@ -23,15 +23,13 @@ func GetActorOutbox(w http.ResponseWriter, r *http.Request, db *sql.DB) { w.Write(enc) } -func GetObjectsFromFollow(actor Actor) []ObjectBase { +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 - var actor Actor - actor = GetActor(e.Id) - followOutbox = GetActorCollection(actor.Outbox) + followOutbox = GetObjectFromCache(db, e.Id) for _, e := range followOutbox.OrderedItems { followObj = append(followObj, e) } |