From 41c63c0688475d5212ce2262b1be248bf438a9ad Mon Sep 17 00:00:00 2001 From: FChannel <> Date: Thu, 3 Jun 2021 02:44:35 -0700 Subject: cleaned up file names --- .gitignore | 1 + CacheDatabase.go | 324 ------------ Database.go | 1352 ----------------------------------------------- Follow.go | 222 -------- OutboxPost.go | 648 ----------------------- cacheDatabase.go | 254 +++++++++ database.go | 1356 ++++++++++++++++++++++++++++++++++++++++++++++++ follow.go | 222 ++++++++ main.go | 4 - outboxPost.go | 647 +++++++++++++++++++++++ static/.#ncatalog.html | 1 - 11 files changed, 2480 insertions(+), 2551 deletions(-) delete mode 100644 CacheDatabase.go delete mode 100644 Database.go delete mode 100644 Follow.go delete mode 100644 OutboxPost.go create mode 100644 cacheDatabase.go create mode 100644 database.go create mode 100644 follow.go create mode 100644 outboxPost.go delete mode 120000 static/.#ncatalog.html diff --git a/.gitignore b/.gitignore index 9789d20..adb71bb 100644 --- a/.gitignore +++ b/.gitignore @@ -1,3 +1,4 @@ +.* *~ #* public/ diff --git a/CacheDatabase.go b/CacheDatabase.go deleted file mode 100644 index 2761237..0000000 --- a/CacheDatabase.go +++ /dev/null @@ -1,324 +0,0 @@ -package main - -import "fmt" -import "time" -import "database/sql" -import _ "github.com/lib/pq" - -func WriteObjectToCache(db *sql.DB, obj ObjectBase) ObjectBase { - if len(obj.Attachment) > 0 { - if obj.Preview.Href != "" { - WritePreviewToCache(db, *obj.Preview) - } - - for i, _ := range obj.Attachment { - WriteAttachmentToCache(db, obj.Attachment[i]) - WriteActivitytoCacheWithAttachment(db, obj, obj.Attachment[i], *obj.Preview) - } - - } else { - WriteActivitytoCache(db, obj) - } - - WriteObjectReplyToDB(db, obj) - - if obj.Replies != nil { - for _, e := range obj.Replies.OrderedItems { - WriteObjectToCache(db, e) - } - } - - return obj -} - -func WriteObjectUpdatesToCache(db *sql.DB, obj ObjectBase) { - query := `update cacheactivitystream set updated=$1 where id=$2` - - _, e := db.Exec(query, time.Now().Format(time.RFC3339), obj.Id) - - if e != nil{ - fmt.Println("error inserting updating inreplyto") - panic(e) - } -} - -func WriteActivitytoCache(db *sql.DB, obj ObjectBase) { - - obj.Name = EscapeString(obj.Name) - obj.Content = EscapeString(obj.Content) - obj.AttributedTo = EscapeString(obj.AttributedTo) - - query := `select id from cacheactivitystream where id=$1` - - rows, err := db.Query(query, obj.Id) - - CheckError(err, "error selecting obj id from cache") - - var id string - defer rows.Close() - rows.Next() - rows.Scan(&id) - - if id != "" { - return - } - - query = `insert into cacheactivitystream (id, type, name, content, published, updated, attributedto, actor, tripcode) values ($1, $2, $3, $4, $5, $6, $7, $8, $9)` - - _, e := db.Exec(query, obj.Id ,obj.Type, obj.Name, obj.Content, obj.Published, obj.Published, obj.AttributedTo, obj.Actor.Id, obj.TripCode) - - if e != nil{ - fmt.Println("error inserting new activity cache") - panic(e) - } -} - -func WriteActivitytoCacheWithAttachment(db *sql.DB, obj ObjectBase, attachment ObjectBase, preview NestedObjectBase) { - - obj.Name = EscapeString(obj.Name) - obj.Content = EscapeString(obj.Content) - obj.AttributedTo = EscapeString(obj.AttributedTo) - - query := `select id from cacheactivitystream where id=$1` - - rows, err := db.Query(query, obj.Id) - - CheckError(err, "error selecting activity with attachment obj id cache") - - var id string - defer rows.Close() - rows.Next() - rows.Scan(&id) - - if id != "" { - return - } - - query = `insert into cacheactivitystream (id, type, name, content, attachment, preview, published, updated, attributedto, actor, tripcode) values ($1, $2, $3, $4, $5, $6, $7, $8, $9, $10, $11)` - - _, e := db.Exec(query, obj.Id ,obj.Type, obj.Name, obj.Content, attachment.Id, preview.Id, obj.Published, obj.Published, obj.AttributedTo, obj.Actor.Id, obj.TripCode) - - if e != nil{ - fmt.Println("error inserting new activity with attachment cache") - panic(e) - } -} - -func WriteAttachmentToCache(db *sql.DB, obj ObjectBase) { - - query := `select id from cacheactivitystream where id=$1` - - rows, err := db.Query(query, obj.Id) - - CheckError(err, "error selecting attachment obj id cache") - - var id string - defer rows.Close() - rows.Next() - rows.Scan(&id) - - if id != "" { - return - } - - query = `insert into cacheactivitystream (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.Published, obj.AttributedTo, obj.MediaType, obj.Size) - - if e != nil{ - fmt.Println("error inserting new attachment cache") - panic(e) - } -} - -func WritePreviewToCache(db *sql.DB, obj NestedObjectBase) { - - query := `select id from cacheactivitystream where id=$1` - - rows, err := db.Query(query, obj.Id) - - CheckError(err, "error selecting preview obj id cache") - - var id string - defer rows.Close() - rows.Next() - rows.Scan(&id) - - if id != "" { - return - } - - query = `insert into cacheactivitystream (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.Published, obj.AttributedTo, obj.MediaType, obj.Size) - - if e != nil{ - fmt.Println("error inserting new preview cache") - panic(e) - } -} - -func WriteObjectReplyToCache(db *sql.DB, obj ObjectBase) { - - for i, e := range obj.InReplyTo { - if(i == 0 || IsReplyInThread(db, obj.InReplyTo[0].Id, e.Id)){ - - query := `select id from replies where id=$1` - - rows, err := db.Query(query, obj.Id) - - CheckError(err, "error selecting obj id cache reply") - - var id string - defer rows.Close() - rows.Next() - rows.Scan(&id) - - if id != "" { - return - } - - query = `insert into cachereplies (id, inreplyto) values ($1, $2)` - - _, err = db.Exec(query, obj.Id, e.Id) - - if err != nil{ - fmt.Println("error inserting replies cache") - panic(err) - } - } - } - - 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) { - - if obj.Replies != nil { - 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) - } - - } - return - } -} - -func WriteActorToCache(db *sql.DB, actorID string) { - actor := GetActor(actorID) - collection := GetActorCollection(actor.Outbox) - - for _, e := range collection.OrderedItems { - WriteObjectToCache(db, e) - } -} - -func DeleteObjectFromCache(db *sql.DB, id string) { - query := `select attachment, preview from cacheactivitystream where id=$1 ` - - rows, err := db.Query(query, id) - CheckError(err, "could not select cache activitystream") - - var attachment string - var preview string - - defer rows.Close() - rows.Next() - rows.Scan(&attachment, &preview) - - query = `delete from cacheactivitystream where id=$1` - _, err = db.Exec(query, attachment) - CheckError(err, "could not delete attachmet cache activitystream") - - query = `delete from cacheactivitystream where id=$1` - _, err = db.Exec(query, preview) - CheckError(err, "could not delete preview cache activitystream") - - query = `delete from cacheactivitystream where id=$1` - _, err = db.Exec(query, id) - CheckError(err, "could not delete object cache activitystream") - - query = `delete from replies where id=$1` - _, err = db.Exec(query, id) - 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) - - query := `update cacheactivitystream set type='Tombstone', name='', content='', attributedto='deleted', tripcode='', updated=$1, deleted=$2 where id=$3` - - _, err := db.Exec(query, datetime, datetime, id) - - CheckError(err, "error with tombstone cache object") - - query = `update cacheactivitystream set type='Tombstone', mediatype='image/png', href=$1, name='', content='', attributedto='deleted', updated=$2, deleted=$3 where id in (select attachment from cacheactivitystream where id=$4)` - - _, err = db.Exec(query, "/public/removed.png", datetime, datetime, id) - - CheckError(err, "error with tombstone attachment cache object") - - query = `update cacheactivitystream set type='Tombstone', mediatype='image/png', href=$1, name='', content='', attributedto='deleted', updated=$2, deleted=$3 where id in (select preview from cacheactivitystream where id=$4)` - - _, err = db.Exec(query, "/public/removed.png", datetime, datetime, id) - - CheckError(err, "error with tombstone preview cache object") - - query = `delete from replies where id=$1` - _, err = db.Exec(query, id) - - CheckError(err, "could not delete cache replies activitystream") -} diff --git a/Database.go b/Database.go deleted file mode 100644 index fb12597..0000000 --- a/Database.go +++ /dev/null @@ -1,1352 +0,0 @@ -package main - -import "fmt" -import "database/sql" -import _ "github.com/lib/pq" -import "time" -import "os" -import "strings" -// import "regexp" -import "sort" - -func GetActorFromDB(db *sql.DB, id string) Actor { - var nActor Actor - - query :=`select type, id, name, preferedusername, inbox, outbox, following, followers, restricted, summary, publickeypem from actor where id=$1` - - rows, err := db.Query(query, id) - - if CheckError(err, "could not get actor from db query") != nil { - return nActor - } - - var publicKeyPem string - defer rows.Close() - for rows.Next() { - err = rows.Scan(&nActor.Type, &nActor.Id, &nActor.Name, &nActor.PreferredUsername, &nActor.Inbox, &nActor.Outbox, &nActor.Following, &nActor.Followers, &nActor.Restricted, &nActor.Summary, &publicKeyPem) - CheckError(err, "error with actor from db scan ") - } - - nActor.PublicKey = GetActorPemFromDB(db, publicKeyPem) - - return nActor -} - -func GetActorByNameFromDB(db *sql.DB, name string) Actor { - var nActor Actor - - query :=`select type, id, name, preferedusername, inbox, outbox, following, followers, restricted, summary, publickeypem from actor where name=$1` - - rows, err := db.Query(query, name) - - if CheckError(err, "could not get actor from db query") != nil { - return nActor - } - - var publicKeyPem string - defer rows.Close() - for rows.Next() { - err = rows.Scan(&nActor.Type, &nActor.Id, &nActor.Name, &nActor.PreferredUsername, &nActor.Inbox, &nActor.Outbox, &nActor.Following, &nActor.Followers, &nActor.Restricted, &nActor.Summary, &publicKeyPem) - CheckError(err, "error with actor from db scan ") - } - - nActor.PublicKey = GetActorPemFromDB(db, publicKeyPem) - - return nActor -} - -func CreateNewBoardDB(db *sql.DB, actor Actor) Actor{ - - query := `insert into actor (type, id, name, preferedusername, inbox, outbox, following, followers, summary, restricted) values ($1, $2, $3, $4, $5, $6, $7, $8, $9, $10)` - - _, err := db.Exec(query, actor.Type, actor.Id, actor.Name, actor.PreferredUsername, actor.Inbox, actor.Outbox, actor.Following, actor.Followers, actor.Summary, actor.Restricted) - - if err != nil { - fmt.Println("board exists") - } else { - fmt.Println("board added") - for _, e := range actor.AuthRequirement { - query = `insert into actorauth (type, board) values ($1, $2)` - _, err := db.Exec(query, e, actor.Name) - CheckError(err, "auth exists") - } - - var verify Verify - - verify.Identifier = actor.Id - verify.Code = CreateKey(50) - verify.Type = "admin" - - CreateVerification(db, verify) - - verify.Identifier = actor.Id - verify.Code = CreateKey(50) - verify.Type = "janitor" - - CreateVerification(db, verify) - - verify.Identifier = actor.Id - verify.Code = CreateKey(50) - verify.Type = "post" - - CreateVerification(db, verify) - - var nverify Verify - nverify.Board = actor.Id - nverify.Identifier = "admin" - nverify.Type = "admin" - CreateBoardMod(db, nverify) - - nverify.Board = actor.Id - nverify.Identifier = "janitor" - nverify.Type = "janitor" - CreateBoardMod(db, nverify) - - nverify.Board = actor.Id - nverify.Identifier = "post" - nverify.Type = "post" - CreateBoardMod(db, nverify) - - if actor.Name != "main" { - var nActor Actor - var nObject ObjectBase - var nActivity Activity - - nActivity.AtContext.Context = "https://www.w3.org/ns/activitystreams" - nActivity.Type = "Follow" - nActivity.Actor = &nActor - nActivity.Object = &nObject - nActivity.Actor.Id = Domain - var mActor Actor - nActivity.Object.Actor = &mActor - nActivity.Object.Actor.Id = actor.Id - nActivity.To = append(nActivity.To, actor.Id) - - response := AcceptFollow(nActivity) - SetActorFollowingDB(db, response) - MakeActivityRequest(db, nActivity) - } - - CreatePem(db, actor) - } - - return actor -} - -func GetBoards(db *sql.DB) []Actor { - - var board []Actor - - query := `select type, id, name, preferedusername, inbox, outbox, following, followers FROM actor` - - rows, err := db.Query(query) - - CheckError(err, "could not get boards from db query") - - defer rows.Close() - for rows.Next(){ - var actor = new(Actor) - - err = rows.Scan(&actor.Type, &actor.Id, &actor.Name, &actor.PreferredUsername, &actor.Inbox, &actor.Outbox, &actor.Following, &actor.Followers) - - if err !=nil{ - panic(err) - } - - board = append(board, *actor) - } - - return board -} - -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 != "" { - obj.Preview.Id = fmt.Sprintf("%s/%s", obj.Actor.Id, CreateUniqueID(db, obj.Actor.Id)) - obj.Preview.Published = time.Now().Format(time.RFC3339) - obj.Preview.Updated = time.Now().Format(time.RFC3339) - obj.Preview.AttributedTo = obj.Id - WritePreviewToDB(db, *obj.Preview) - } - - for i, _ := range obj.Attachment { - obj.Attachment[i].Id = fmt.Sprintf("%s/%s", obj.Actor.Id, CreateUniqueID(db, obj.Actor.Id)) - 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) - } - - } else { - WriteActivitytoDB(db, obj) - } - - WriteObjectReplyToDB(db, obj) - WriteWalletToDB(db, obj) - - return obj -} - -func WriteObjectUpdatesToDB(db *sql.DB, obj ObjectBase) { - query := `update activitystream set updated=$1 where id=$2` - - _, e := db.Exec(query, time.Now().Format(time.RFC3339), obj.Id) - - if e != nil{ - fmt.Println("error inserting updating inreplyto") - panic(e) - } -} - -func WriteObjectReplyToLocalDB(db *sql.DB, id string, replyto string) { - query := `insert into replies (id, inreplyto) values ($1, $2)` - - _, err := db.Exec(query, id, replyto) - - CheckError(err, "Could not insert local reply query") - - query = `select inreplyto from replies where id=$1` - - rows, err := db.Query(query,replyto) - - CheckError(err, "Could not query select inreplyto") - - defer rows.Close() - - for rows.Next() { - var val string - rows.Scan(&val) - if val == "" { - updated := time.Now().Format(time.RFC3339) - query := `update activitystream set updated=$1 where id=$2` - - _, err := db.Exec(query, updated, replyto) - - CheckError(err, "error with updating replyto updated at date") - } - } -} - -func WriteObjectReplyToDB(db *sql.DB, obj ObjectBase) { - for _, e := range obj.InReplyTo { - query := `select id from replies where id=$1 and inreplyto=$2` - - rows, err := db.Query(query, obj.Id, e.Id) - - CheckError(err, "error selecting replies db") - - defer rows.Close() - - var id string - rows.Next() - rows.Scan(&id) - - if id == "" { - query := `insert into replies (id, inreplyto) values ($1, $2)` - - _, err := db.Exec(query, obj.Id, e.Id) - - - CheckError(err, "error inserting replies db") - } - - update := true - for _, e := range obj.Option { - if e == "sage" || e == "nokosage" { - update = false - break - } - } - - if update { - if IsObjectLocal(db, e.Id) { - WriteObjectUpdatesToDB(db, e) - } else { - WriteObjectUpdatesToCache(db, e) - } - } - } - - if len(obj.InReplyTo) < 1 { - query := `select id from replies where id=$1 and inreplyto=$2` - - rows, err := db.Query(query, obj.Id, "") - - CheckError(err, "error selecting replies db") - - defer rows.Close() - - var id string - rows.Next() - rows.Scan(&id) - - if id == "" { - query := `insert into replies (id, inreplyto) values ($1, $2)` - - _, err := db.Exec(query, obj.Id, "") - - CheckError(err, "error inserting replies db") - } - } -} - -func WriteWalletToDB(db *sql.DB, obj ObjectBase) { - for _, e := range obj.Option { - if e == "wallet" { - for _, e := range obj.Wallet { - query := `insert into wallet (id, type, address) values ($1, $2, $3)` - - _, err := db.Exec(query, obj.Id ,e.Type, e.Address) - - CheckError(err, "error with write wallet query") - } - return - } - } -} - -func WriteActivitytoDB(db *sql.DB, obj ObjectBase) { - - obj.Name = EscapeString(obj.Name) - obj.Content = EscapeString(obj.Content) - obj.AttributedTo = EscapeString(obj.AttributedTo) - - query := `insert into activitystream (id, type, name, content, published, updated, attributedto, actor, tripcode) values ($1, $2, $3, $4, $5, $6, $7, $8, $9)` - - _, e := db.Exec(query, obj.Id ,obj.Type, obj.Name, obj.Content, obj.Published, obj.Updated, obj.AttributedTo, obj.Actor.Id, obj.TripCode) - - if e != nil{ - fmt.Println("error inserting new activity") - panic(e) - } -} - -func WriteActivitytoDBWithAttachment(db *sql.DB, obj ObjectBase, attachment ObjectBase, preview NestedObjectBase) { - - obj.Name = EscapeString(obj.Name) - obj.Content = EscapeString(obj.Content) - obj.AttributedTo = EscapeString(obj.AttributedTo) - - query := `insert into activitystream (id, type, name, content, attachment, preview, published, updated, attributedto, actor, tripcode) values ($1, $2, $3, $4, $5, $6, $7, $8, $9, $10, $11)` - - _, e := db.Exec(query, obj.Id ,obj.Type, obj.Name, obj.Content, attachment.Id, preview.Id, obj.Published, obj.Updated, obj.AttributedTo, obj.Actor.Id, obj.TripCode) - - if e != nil{ - fmt.Println("error inserting new activity with attachment") - panic(e) - } -} - -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) - - if e != nil{ - fmt.Println("error inserting new attachment") - panic(e) - } -} - -func WritePreviewToDB(db *sql.DB, obj NestedObjectBase) { - 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) - - if e != nil{ - fmt.Println("error inserting new attachment") - panic(e) - } -} - -func GetActivityFromDB(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, tripcode 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, &post.TripCode) - - CheckError(err, "error scan object into post struct") - - post.Actor = &actor - - var postCnt int - var imgCnt int - post.Replies, postCnt, imgCnt = GetObjectRepliesDB(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 - - 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, x.tripcode from (select id, name, content, type, published, updated, attributedto, attachment, preview, actor, tripcode from activitystream where actor=$1 and id in (select id from replies where inreplyto='') and type='Note' union select id, name, content, type, published, updated, attributedto, attachment, preview, actor, tripcode 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 id, name, content, type, published, updated, attributedto, attachment, preview, actor, tripcode 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, &post.TripCode) - - 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 = 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 - - query := `select id, name, content, type, published, updated, attributedto, attachment, preview, actor, tripcode 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, &post.TripCode) - - CheckError(err, "error scan object into post struct") - - post.Actor = &actor - - var postCnt int - var imgCnt int - post.Replies, postCnt, imgCnt = GetObjectRepliesDB(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 - - return nColl -} - -func GetObjectFromDBCatalog(db *sql.DB, id string) Collection { - var nColl Collection - var result []ObjectBase - - query := `select x.id, x.name, x.content, x.type, x.published, x.updated, x.attributedto, x.attachment, x.preview, x.actor, x.tripcode from (select id, name, content, type, published, updated, attributedto, attachment, preview, actor, tripcode from activitystream where actor=$1 and id in (select id from replies where inreplyto='') and type='Note' union select id, name, content, type, published, updated, attributedto, attachment, preview, actor, tripcode 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 id, name, content, type, published, updated, attributedto, attachment, preview, actor, tripcode 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` - - 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, &post.TripCode) - - 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 - - query := `select x.id, x.name, x.content, x.type, x.published, x.updated, x.attributedto, x.attachment, x.preview, x.actor, x.tripcode from (select id, name, content, type, published, updated, attributedto, attachment, preview, actor, tripcode from activitystream where id=$1 and type='Note' union select id, name, content, type, published, updated, attributedto, attachment, preview, actor, tripcode from cacheactivitystream where id=$1 and type='Note') as x` - - rows, err := db.Query(query, postID) - - 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, &post.TripCode) - - CheckError(err, "error scan object into post struct") - - actor = GetActorFromDB(db, actor.Id) - - post.Actor = &actor - - nColl.Actor = &actor - - var postCnt int - var imgCnt int - post.Replies, postCnt, imgCnt = GetObjectRepliesDB(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 - - return nColl -} - -func GetInReplyToDB(db *sql.DB, parent ObjectBase) []ObjectBase { - var result []ObjectBase - - query := `select inreplyto from replies where id =$1` - - rows, err := db.Query(query, parent.Id) - - CheckError(err, "error with inreplyto db query") - - defer rows.Close() - for rows.Next() { - var post ObjectBase - - rows.Scan(&post.Id) - - result = append(result, post) - } - - 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, x.tripcode 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 postCount int - var attachCount 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(&postCount, &attachCount, &post.Id, &post.Name, &post.Content, &post.Type, &post.Published, &post.AttributedTo, &attachID, &previewID, &actor.Id, &post.TripCode) - - 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, postCount, attachCount -} - -func GetObjectRepliesDB(db *sql.DB, parent ObjectBase) (*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, x.tripcode 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") - - var postCount int - var attachCount 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(&postCount, &attachCount, &post.Id, &post.Name, &post.Content, &post.Type, &post.Published, &post.AttributedTo, &attachID, &previewID, &actor.Id, &post.TripCode) - - 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 - - return &nColl, postCount, attachCount -} - -func GetObjectRepliesReplies(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, tripcode from activitystream where id in (select id from replies where inreplyto=$1) and type='Note' order by updated asc` - - rows, err := db.Query(query, parent.Id) - - CheckError(err, "error with replies 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, &post.TripCode) - - CheckError(err, "error with replies replies db scan") - - post.Actor = &actor - - post.Attachment = GetObjectAttachment(db, attachID) - - post.Preview = GetObjectPreview(db, previewID) - - result = append(result, post) - } - - nColl.OrderedItems = result - - return &nColl, 0, 0 -} - -func GetObjectRepliesRepliesDB(db *sql.DB, parent ObjectBase) (*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, x.tripcode 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 postCount int - var attachCount 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(&postCount, &attachCount, &post.Id, &post.Name, &post.Content, &post.Type, &post.Published, &post.AttributedTo, &attachID, &previewID, &actor.Id, &post.TripCode) - - CheckError(err, "error with replies replies db scan") - - post.Actor = &actor - - post.Attachment = GetObjectAttachment(db, attachID) - - post.Preview = GetObjectPreview(db, previewID) - - result = append(result, post) - } - - nColl.OrderedItems = result - - return &nColl, postCount, attachCount -} - -func CheckIfObjectOP(db *sql.DB, id string) bool { - - var count int - - query := `select count(id) from replies where inreplyto='' and id=$1 ` - - rows, err := db.Query(query, id) - CheckError(err, "error checking if ID is OP") - - defer rows.Close() - rows.Next() - rows.Scan(&count) - - if count > 0 { - return true - } - - return false -} - -func GetObjectRepliesCount(db *sql.DB, parent ObjectBase) (int, int) { - - var countId int - var countImg int - - query := `select count(x.id) over(), sum(case when RTRIM(x.attachment) = '' then 0 else 1 end) over() from (select id, attachment from activitystream where id in (select id from replies where inreplyto=$1) and type='Note' union select id, attachment from cacheactivitystream where id in (select id from replies where inreplyto=$1) and type='Note') as x` - - rows, err := db.Query(query, parent.Id) - - CheckError(err, "error with replies count db query") - - defer rows.Close() - rows.Next() - rows.Scan(&countId, &countImg) - - return countId, countImg -} - -func GetObjectAttachment(db *sql.DB, id string) []ObjectBase { - - var attachments []ObjectBase - - 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 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 GetObjectPreview(db *sql.DB, id string) *NestedObjectBase { - - var preview NestedObjectBase - - 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") - - defer rows.Close() - for rows.Next() { - err = rows.Scan(&preview.Id, &preview.Type, &preview.Name, &preview.Href, &preview.MediaType, &preview.Size, &preview.Published) - } - - return &preview -} - -func GetObjectPostsTotalDB(db *sql.DB, actor Actor) int{ - - count := 0 - query := `select count(id) from activitystream 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 GetObjectImgsTotalDB(db *sql.DB, actor Actor) int{ - - count := 0 - query := `select count(attachment) from activitystream 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 DeletePreviewFromFile(db *sql.DB, id string) { - - var query = `select href from activitystream where id in (select preview from activitystream where id=$1)` - - rows, err := db.Query(query, id) - - CheckError(err, "error query delete attachment") - - defer rows.Close() - for rows.Next() { - var href string - - err := rows.Scan(&href) - href = strings.Replace(href, Domain + "/", "", 1) - CheckError(err, "error scanning delete attachment") - - if(href != "/static/notfound.png") { - _, err = os.Stat(href) - if err == nil { - os.Remove(href) - } - } - } - - DeletePreviewFromDB(db, id) -} - -func DeleteAttachmentFromFile(db *sql.DB, id string) { - - var query = `select href from activitystream where id in (select attachment from activitystream where id=$1)` - - rows, err := db.Query(query, id) - - CheckError(err, "error query delete attachment") - - defer rows.Close() - for rows.Next() { - var href string - - err := rows.Scan(&href) - href = strings.Replace(href, Domain + "/", "", 1) - - CheckError(err, "error scanning delete preview") - - if(href != "/static/notfound.png") { - _, err = os.Stat(href) - if err == nil { - os.Remove(href) - } - } - } - - 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)` - - rows, err := db.Query(query, id) - - CheckError(err, "error query delete preview replies") - - defer rows.Close() - for rows.Next() { - var attachment string - - err := rows.Scan(&attachment) - - CheckError(err, "error scanning delete preview") - - DeletePreviewFromFile(db, attachment) - } -} - -func DeleteAttachmentRepliesFromDB(db *sql.DB, id string) { - var query = `select id from activitystream where id in (select id from replies where inreplyto=$1)` - - rows, err := db.Query(query, id) - - CheckError(err, "error query delete attachment replies") - - defer rows.Close() - for rows.Next() { - var attachment string - - err := rows.Scan(&attachment) - - CheckError(err, "error scanning delete attachment") - - DeleteAttachmentFromFile(db, attachment) - } -} - -func DeleteAttachmentFromDB(db *sql.DB, id string) { - datetime := time.Now().Format(time.RFC3339) - - var query = `update activitystream set type='Tombstone', mediatype='image/png', href=$1, name='', content='', attributedto='deleted', deleted=$2 where id in (select attachment from activitystream where id=$3)` - - _, err := db.Exec(query, Domain + "/static/notfound.png", datetime, id) - - CheckError(err, "error with delete attachment") - - query = `update cacheactivitystream set type='Tombstone', mediatype='image/png', href=$1, name='', content='', attributedto='deleted', deleted=$2 where id in (select attachment from cacheactivitystream where id=$3)` - - _, err = db.Exec(query, Domain + "/static/notfound.png", datetime, id) - - CheckError(err, "error with delete cache attachment") -} - -func DeletePreviewFromDB(db *sql.DB, id string) { - datetime := time.Now().Format(time.RFC3339) - - var query = `update activitystream set type='Tombstone', mediatype='image/png', href=$1, name='', content='', attributedto='deleted', deleted=$2 where id in (select preview from activitystream where id=$3)` - - _, err := db.Exec(query, Domain + "/static/notfound.png", datetime, id) - - CheckError(err, "error with delete preview") - - query = `update cacheactivitystream set type='Tombstone', mediatype='image/png', href=$1, name='', content='', attributedto='deleted', deleted=$2 where id in (select preview from cacheactivitystream where id=$3)` - - _, err = db.Exec(query, Domain + "/static/notfound.png", datetime, id) - - CheckError(err, "error with delete cache preview") -} - -func DeleteObjectRepliedTo(db *sql.DB, id string){ - query := `delete from replies where id=$1` - _, err := db.Exec(query, id) - - 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', tripcode='', deleted=$1 where id=$2` - - _, err := db.Exec(query, datetime, id) - - CheckError(err, "error with delete object") - - query = `update cacheactivitystream set type='Tombstone', name='', content='', attributedto='deleted', tripcode='', deleted=$1 where id=$2` - - _, err = db.Exec(query, datetime, id) - - CheckError(err, "error with delete cache object") -} - -func DeleteObjectsInReplyTo(db *sql.DB, id string) { - query := `delete from replies where id in (select id from replies where inreplyto=$1)` - - _, err := db.Exec(query, id) - - CheckError(err, "error with delete object replies to") -} - -func DeleteObjectRepliesFromDB(db *sql.DB, id string) { - datetime := time.Now().Format(time.RFC3339) - - var query = `update activitystream set type='Tombstone', name='', content='', attributedto='deleted', tripcode='', deleted=$1 where id in (select id from replies where inreplyto=$2)` - - _, err := db.Exec(query, datetime, id) - CheckError(err, "error with delete object replies") - - query = `update cacheactivitystream set type='Tombstone', name='', content='', attributedto='deleted', tripcode='', deleted=$1 where id in (select id from replies where inreplyto=$2)` - - _, err = db.Exec(query, datetime, id) - CheckError(err, "error with delete object cache replies") - -} - -func SetAttachmentFromDB(db *sql.DB, id string, _type string) { - datetime := time.Now().Format(time.RFC3339) - - var query = `update activitystream set type=$1, deleted=$2 where id in (select attachment from activitystream where id=$3)` - - _, err := db.Exec(query, _type, datetime, id) - - CheckError(err, "error with set attachment") - - query = `update cacheactivitystream set type=$1, deleted=$2 where id in (select attachment from cacheactivitystream where id=$3)` - - _, err = db.Exec(query, _type, datetime, id) - - CheckError(err, "error with set cache attachment") -} - -func SetAttachmentRepliesFromDB(db *sql.DB, id string, _type string) { - datetime := time.Now().Format(time.RFC3339) - - var query = `update activitystream set type=$1, deleted=$2 where id in (select attachment from activitystream where id in (select id from replies where inreplyto=$3))` - - _, err := db.Exec(query, _type, datetime, id) - - CheckError(err, "error with set attachment") - - query = `update cacheactivitystream set type=$1, deleted=$2 where id in (select attachment from cacheactivitystream where id in (select id from replies where inreplyto=$3))` - - _, err = db.Exec(query, _type, datetime, id) - - CheckError(err, "error with set cache attachment") -} - -func SetPreviewFromDB(db *sql.DB, id string, _type string) { - datetime := time.Now().Format(time.RFC3339) - - var query = `update activitystream set type=$1, deleted=$2 where id in (select preview from activitystream where id=$3)` - - _, err := db.Exec(query, _type, datetime, id) - - CheckError(err, "error with set preview") - - query = `update cacheactivitystream set type=$1, deleted=$2 where id in (select preview from cacheactivitystream where id=$3)` - - _, err = db.Exec(query, _type, datetime, id) - - CheckError(err, "error with set cache preview") -} - -func SetPreviewRepliesFromDB(db *sql.DB, id string, _type string) { - datetime := time.Now().Format(time.RFC3339) - - var query = `update activitystream set type=$1, deleted=$2 where id in (select preview from activitystream where id in (select id from replies where inreplyto=$3))` - - _, err := db.Exec(query, _type, datetime, id) - - CheckError(err, "error with set preview") - - query = `update cacheactivitystream set type=$1, deleted=$2 where id in (select preview from cacheactivitystream where id in (select id from replies where inreplyto=$3))` - - _, err = db.Exec(query, _type, datetime, id) - - CheckError(err, "error with set cache preview") -} - -func SetObjectFromDB(db *sql.DB, id string, _type string) { - datetime := time.Now().Format(time.RFC3339) - - var query = `update activitystream set type=$1, deleted=$2 where id=$3` - - _, err := db.Exec(query, _type, datetime, id) - - CheckError(err, "error with set object") - - query = `update cacheactivitystream set type=$1, deleted=$2 where id=$3` - - _, err = db.Exec(query, _type, datetime, id) - - CheckError(err, "error with set cache object") -} - -func SetObjectRepliesFromDB(db *sql.DB, id string, _type string) { - datetime := time.Now().Format(time.RFC3339) - - var query = `update activitystream set type=$1, deleted=$2 where id in (select id from replies where inreplyto=$3)` - _, err := db.Exec(query, _type, datetime, id) - CheckError(err, "error with set object replies") - - query = `update cacheactivitystream set type=$1, deleted=$2 where id in (select id from replies where inreplyto=$3)` - _, err = db.Exec(query, _type, datetime, id) - CheckError(err, "error with set cache object replies") -} - -func SetObject(db *sql.DB, id string, _type string) { - SetAttachmentFromDB(db, id, _type); - SetPreviewFromDB(db, id, _type); - SetObjectFromDB(db, id, _type); -} - -func SetObjectAndReplies(db *sql.DB, id string, _type string) { - SetAttachmentFromDB(db, id, _type); - SetPreviewFromDB(db, id, _type); - SetObjectRepliesFromDB(db, id, _type); - SetAttachmentRepliesFromDB(db, id, _type); - SetPreviewRepliesFromDB(db, id, _type); - SetObjectFromDB(db, id, _type); -} - -func DeleteObject(db *sql.DB, id string) { - DeleteReportActivity(db, id) - DeleteAttachmentFromFile(db, id) - DeletePreviewFromFile(db, id) - DeleteObjectFromDB(db, id) - DeleteObjectRepliedTo(db, id) -} - -func DeleteObjectAndReplies(db *sql.DB, id string) { - DeleteReportActivity(db, id) - DeleteAttachmentFromFile(db, id) - DeletePreviewFromFile(db, id) - DeleteObjectRepliedTo(db, id) - DeleteObjectsInReplyTo(db, id) - DeleteObjectRepliesFromDB(db, id) - DeleteAttachmentRepliesFromDB(db, id) - DeletePreviewRepliesFromDB(db, id) - DeleteObjectFromDB(db, id) -} - -func GetRandomCaptcha(db *sql.DB) string{ - query := `select identifier from verification where type='captcha' order by random() limit 1` - - rows, err := db.Query(query) - - CheckError(err, "could not get captcha") - - var verify string - - defer rows.Close() - - rows.Next() - err = rows.Scan(&verify) - - CheckError(err, "Could not get verify captcha") - - return verify -} - -func GetCaptchaTotal(db *sql.DB) int{ - query := `select count(*) from verification where type='captcha'` - - rows, err := db.Query(query) - - CheckError(err, "could not get query captcha total") - - defer rows.Close() - - var count int - for rows.Next(){ - if err := rows.Scan(&count); err != nil{ - CheckError(err, "could not get captcha total") - } - } - - return count -} - -func GetCaptchaCodeDB(db *sql.DB, verify string) string { - - query := `select code from verification where identifier=$1 limit 1` - - rows, err := db.Query(query, verify) - - CheckError(err, "could not get captcha verifciation") - - defer rows.Close() - - var code string - - rows.Next() - err = rows.Scan(&code) - - if err != nil { - fmt.Println("Could not get verification captcha") - } - - return code -} - -func GetActorAuth(db *sql.DB, actor string) []string { - query := `select type from actorauth where board=$1` - - rows, err := db.Query(query, actor) - - CheckError(err, "could not get actor auth") - - defer rows.Close() - - var auth []string - - for rows.Next() { - var e string - err = rows.Scan(&e) - - CheckError(err, "could not get actor auth row scan") - - auth = append(auth, e) - } - - return auth -} - -func DeleteCaptchaCodeDB(db *sql.DB, verify string) { - query := `delete from verification where identifier=$1` - - _, err := db.Exec(query, verify) - - CheckError(err, "could not delete captcah code db") - - os.Remove("./" + verify) -} - -func EscapeString(text string) string { - // re := regexp.MustCompile("(?i)(n)+(\\s+)?(i)+(\\s+)?(g)+(\\s+)?(e)+?(\\s+)?(r)+(\\s+)?") - // text = re.ReplaceAllString(text, "I love black people") - // re = regexp.MustCompile("(?i)(n)+(\\s+)?(i)+(\\s+)?(g)(\\s+)?(g)+(\\s+)?") - // text = re.ReplaceAllString(text, "I love black people") - // text = strings.Replace(text, "<", "<", -1) - return text -} - -func GetActorReportedTotal(db *sql.DB, id string) int { - query := `select count(id) from reported where board=$1` - - rows, err := db.Query(query, id) - - CheckError(err, "error getting actor reported total query") - - defer rows.Close() - - var count int - for rows.Next() { - rows.Scan(&count) - } - - return count -} - -func GetActorReportedDB(db *sql.DB, id string) []ObjectBase { - var nObj []ObjectBase - - query := `select id, count from reported where board=$1` - - rows, err := db.Query(query, id) - - CheckError(err, "error getting actor reported query") - - defer rows.Close() - - for rows.Next() { - var obj ObjectBase - - rows.Scan(&obj.Id, &obj.Size) - - nObj = append(nObj, obj) - } - - return nObj -} - -func GetActorPemFromDB(db *sql.DB, pemID string) PublicKeyPem { - query := `select id, owner, file from publickeypem where id=$1` - rows, err := db.Query(query, pemID) - - CheckError(err, "could not get public key pem from database") - - var pem PublicKeyPem - - defer rows.Close() - rows.Next() - rows.Scan(&pem.Id, &pem.Owner, &pem.PublicKeyPem) - f, _ := os.ReadFile(pem.PublicKeyPem) - - pem.PublicKeyPem = strings.ReplaceAll(string(f), "\r\n", `\n`) - - return pem -} diff --git a/Follow.go b/Follow.go deleted file mode 100644 index 667c6bd..0000000 --- a/Follow.go +++ /dev/null @@ -1,222 +0,0 @@ -package main - -import "net/http" -import "database/sql" -import _ "github.com/lib/pq" -import "encoding/json" - -func GetActorFollowing(w http.ResponseWriter, db *sql.DB, id string) { - var following Collection - - following.AtContext.Context = "https://www.w3.org/ns/activitystreams" - following.Type = "Collection" - following.TotalItems, _ = GetActorFollowTotal(db, id) - following.Items = GetActorFollowingDB(db, id) - - enc, _ := json.MarshalIndent(following, "", "\t") - w.Header().Set("Content-Type", activitystreams) - w.Write(enc) -} - -func GetActorFollowers(w http.ResponseWriter, db *sql.DB, id string) { - var following Collection - - following.AtContext.Context = "https://www.w3.org/ns/activitystreams" - following.Type = "Collection" - _, following.TotalItems = GetActorFollowTotal(db, id) - following.Items = GetActorFollowDB(db, id) - - enc, _ := json.MarshalIndent(following, "", "\t") - w.Header().Set("Content-Type", activitystreams) - w.Write(enc) -} - - -func GetActorFollowingDB(db *sql.DB, id string) []ObjectBase { - var followingCollection []ObjectBase - query := `select following from following where id=$1` - - rows, err := db.Query(query, id) - - CheckError(err, "error with following db query") - - defer rows.Close() - - for rows.Next() { - var obj ObjectBase - - err := rows.Scan(&obj.Id) - - CheckError(err, "error with following db scan") - - followingCollection = append(followingCollection, obj) - } - - return followingCollection -} - -func GetActorFollowDB(db *sql.DB, id string) []ObjectBase { - var followerCollection []ObjectBase - - query := `select follower from follower where id=$1` - - rows, err := db.Query(query, id) - - CheckError(err, "error with follower db query") - - defer rows.Close() - - for rows.Next() { - var obj ObjectBase - - err := rows.Scan(&obj.Id) - - CheckError(err, "error with followers db scan") - - followerCollection = append(followerCollection, obj) - } - - return followerCollection -} - -func GetActorFollowTotal(db *sql.DB, id string) (int, int) { - var following int - var followers int - - query := `select count(following) from following where id=$1` - - rows, err := db.Query(query, id) - - CheckError(err, "error with following total db query") - - defer rows.Close() - - for rows.Next() { - err := rows.Scan(&following) - - CheckError(err, "error with following total db scan") - } - - query = `select count(follower) from follower where id=$1` - - rows, err = db.Query(query, id) - - CheckError(err, "error with followers total db query") - - defer rows.Close() - - for rows.Next() { - err := rows.Scan(&followers) - - CheckError(err, "error with followers total db scan") - } - - return following, followers -} - -func AcceptFollow(activity Activity) Activity { - var accept Activity - accept.AtContext.Context = activity.AtContext.Context - accept.Type = "Accept" - accept.Actor = activity.Object.Actor - var nObj ObjectBase - var nActor Actor - accept.Object = &nObj - accept.Object.Actor = &nActor - accept.Object.Actor = activity.Actor - var nNested NestedObjectBase - var mActor Actor - accept.Object.Object = &nNested - accept.Object.Object.Actor = &mActor - accept.Object.Object.Actor = activity.Object.Actor - accept.Object.Object.Type = "Follow" - accept.To = append(accept.To, activity.Object.Actor.Id) - - return accept -} - -func RejectActivity(activity Activity) Activity { - var accept Activity - accept.AtContext.Context = activity.AtContext.Context - accept.Type = "Reject" - var nObj ObjectBase - var nActor Actor - accept.Object = &nObj - accept.Object.Actor = &nActor - accept.Actor = activity.Object.Actor - accept.Object.Actor = activity.Actor - var nNested NestedObjectBase - var mActor Actor - accept.Object.Object = &nNested - accept.Object.Object.Actor = &mActor - accept.Object.Object.Actor = activity.Object.Actor - accept.Object.Object.Type = "Follow" - accept.To = append(accept.To, activity.Actor.Id) - - return accept -} - -func SetActorFollowerDB(db *sql.DB, activity Activity) Activity { - var query string - alreadyFollow := false - followers := GetActorFollowDB(db, activity.Actor.Id) - - for _, e := range followers { - if e.Id == activity.Object.Actor.Id { - alreadyFollow = true - } - } - if alreadyFollow { - query = `delete from follower where id=$1 and follower=$2` - activity.Summary = activity.Object.Actor.Id + " Unfollow " + activity.Actor.Id - } else { - query = `insert into follower (id, follower) values ($1, $2)` - activity.Summary = activity.Object.Actor.Id + " Follow " + activity.Actor.Id - } - - _, err := db.Exec(query, activity.Actor.Id, activity.Object.Actor.Id) - - if CheckError(err, "error with follower db insert/delete") != nil { - activity.Type = "Reject" - return activity - } - - activity.Type = "Accept" - return activity -} - -func SetActorFollowingDB(db *sql.DB, activity Activity) Activity { - var query string - alreadyFollow := false - following := GetActorFollowingDB(db, activity.Object.Actor.Id) - - for _, e := range following { - if e.Id == activity.Actor.Id { - alreadyFollow = true - } - } - - if alreadyFollow { - query = `delete from following where id=$1 and following=$2` - activity.Summary = activity.Object.Actor.Id + " Unfollowing " + activity.Actor.Id - if !IsActorLocal(db, activity.Actor.Id) { - go 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 - if !IsActorLocal(db, activity.Actor.Id) { - go WriteActorToCache(db, activity.Actor.Id) - } - } - - _, err := db.Exec(query, activity.Object.Actor.Id, activity.Actor.Id) - - if CheckError(err, "error with following db insert/delete") != nil { - activity.Type = "Reject" - return activity - } - - activity.Type = "Accept" - return activity -} diff --git a/OutboxPost.go b/OutboxPost.go deleted file mode 100644 index c9ceb12..0000000 --- a/OutboxPost.go +++ /dev/null @@ -1,648 +0,0 @@ -package main - -import "fmt" -import "net/http" -import "database/sql" -import _ "github.com/lib/pq" -import "encoding/json" -import "reflect" -import "io/ioutil" -import "os" -import "regexp" -import "strings" - -func ParseOutboxRequest(w http.ResponseWriter, r *http.Request, db *sql.DB) { - - var activity Activity - - actor := GetActorFromPath(db, r.URL.Path, "/") - contentType := GetContentType(r.Header.Get("content-type")) - - defer r.Body.Close() - if contentType == "multipart/form-data" || contentType == "application/x-www-form-urlencoded" { - r.ParseMultipartForm(5 << 20) - if(BoardHasAuthType(db, actor.Name, "captcha") && CheckCaptcha(db, r.FormValue("captcha"))) { - f, header, _ := r.FormFile("file") - if(header != nil) { - if(header.Size > (7 << 20)){ - w.WriteHeader(http.StatusRequestEntityTooLarge) - w.Write([]byte("7MB max file size")) - return - } - - contentType, _ := GetFileContentType(f) - - if(!SupportedMIMEType(contentType)) { - w.WriteHeader(http.StatusNotAcceptable) - w.Write([]byte("file type not supported")) - return - } - } - - var nObj = CreateObject("Note") - nObj = ObjectFromForm(r, db, nObj) - - var act Actor - nObj.Actor = &act - nObj.Actor.Id = Domain + "/" + actor.Name - - nObj = WriteObjectToDB(db, nObj) - activity := CreateActivity("Create", nObj) - activity = AddFollowersToActivity(db, activity) - MakeActivityRequest(db, activity) - - var id string - op := len(nObj.InReplyTo) - 1 - if op >= 0 { - if nObj.InReplyTo[op].Id == "" { - id = nObj.Id - } else { - id = nObj.InReplyTo[0].Id + "|" + nObj.Id - } - } - - w.WriteHeader(http.StatusOK) - w.Write([]byte(id)) - return - } - - w.WriteHeader(http.StatusForbidden) - w.Write([]byte("captcha could not auth")) - } else { - activity = GetActivityFromJson(r, db) - - if IsActivityLocal(db, activity) { - switch activity.Type { - case "Create": - w.WriteHeader(http.StatusBadRequest) - w.Write([]byte("")) - break - case "Follow": - - var validActor bool - var validLocalActor bool - - header := r.Header.Get("Authorization") - - auth := strings.Split(header, " ") - - if len(auth) < 2 { - w.WriteHeader(http.StatusBadRequest) - w.Write([]byte("")) - return - } - - _, validActor = IsValidActor(activity.Object.Actor.Id) - validLocalActor = (activity.Actor.Id == actor.Id) - - var verify Verify - verify.Identifier = "admin" - verify.Board = activity.Actor.Id - - verify = GetVerificationCode(db, verify) - - code := verify.Code - code = CreateTripCode(code) - code = CreateTripCode(code) - - if code != auth[1] { - verify.Identifier = "admin" - verify.Board = Domain - - verify = GetVerificationCode(db, verify) - code = verify.Code - code = CreateTripCode(code) - code = CreateTripCode(code) - } - - var rActivity Activity - if validActor && validLocalActor && code == auth[1] || verify.Board == Domain { - rActivity = AcceptFollow(activity) - SetActorFollowingDB(db, rActivity) - MakeActivityRequest(db, activity) - } - - break - case "Delete": - fmt.Println("This is a delete") - w.WriteHeader(http.StatusBadRequest) - w.Write([]byte("could not process activity")) - break - case "Note": - w.WriteHeader(http.StatusBadRequest) - w.Write([]byte("could not process activity")) - break - - case "New": - - header := r.Header.Get("Authorization") - - auth := strings.Split(header, " ") - - if len(auth) < 2 { - w.WriteHeader(http.StatusBadRequest) - w.Write([]byte("")) - return - } - - var verify Verify - verify.Identifier = "admin" - verify.Board = Domain - - verify = GetVerificationCode(db, verify) - - code := verify.Code - code = CreateTripCode(code) - code = CreateTripCode(code) - - if code != auth[1] { - w.WriteHeader(http.StatusBadRequest) - w.Write([]byte("")) - return - } - - name := activity.Object.Actor.Name - prefname := activity.Object.Actor.PreferredUsername - summary := activity.Object.Actor.Summary - restricted := activity.Object.Actor.Restricted - - actor := CreateNewBoardDB(db, *CreateNewActor(name, prefname, summary, authReq, restricted)) - - if actor.Id != "" { - j, _ := json.Marshal(&actor) - w.Write([]byte(j)) - return - } - - w.WriteHeader(http.StatusBadRequest) - w.Write([]byte("")) - break - default: - w.WriteHeader(http.StatusBadRequest) - w.Write([]byte("could not process activity")) - } - } else { - fmt.Println("is NOT activity") - w.WriteHeader(http.StatusBadRequest) - w.Write([]byte("could not process activity")) - } - } -} - -func ObjectFromJson(r *http.Request, obj ObjectBase) ObjectBase { - body, _ := ioutil.ReadAll(r.Body) - - var respActivity ActivityRaw - - err := json.Unmarshal(body, &respActivity) - - CheckError(err, "error with object from json") - - if HasContextFromJson(respActivity.AtContextRaw.Context) { - var jObj ObjectBase - jObj = GetObjectFromJson(respActivity.ObjectRaw) - jObj.To = GetToFromJson(respActivity.ToRaw) - jObj.Cc = GetToFromJson(respActivity.CcRaw) - } - - return obj -} - -func GetObjectFromJson(obj []byte) ObjectBase { - var generic interface{} - - err := json.Unmarshal(obj, &generic) - - CheckError(err, "error with getting obj from json") - - t := reflect.TypeOf(generic) - - var nObj ObjectBase - if t != nil { - switch t.String() { - case "[]interface {}": - var lObj ObjectBase - var arrContext ObjectArray - err = json.Unmarshal(obj, &arrContext.Object) - CheckError(err, "error with []interface{} oject from json") - if len(arrContext.Object) > 0 { - lObj = arrContext.Object[0] - } - nObj = lObj - break - - case "map[string]interface {}": - var arrContext Object - err = json.Unmarshal(obj, &arrContext.Object) - CheckError(err, "error with object from json") - nObj = *arrContext.Object - break - - case "string": - var lObj ObjectBase - var arrContext ObjectString - err = json.Unmarshal(obj, &arrContext.Object) - CheckError(err, "error with string object from json") - lObj.Id = arrContext.Object - nObj = lObj - break - } - } - - return nObj -} - -func GetActorFromJson(actor []byte) Actor{ - var generic interface{} - var nActor Actor - err := json.Unmarshal(actor, &generic) - - if err != nil { - return nActor - } - - t := reflect.TypeOf(generic) - if t != nil { - switch t.String() { - case "map[string]interface {}": - err = json.Unmarshal(actor, &nActor) - CheckError(err, "error with To []interface{}") - - case "string": - var str string - err = json.Unmarshal(actor, &str) - CheckError(err, "error with To string") - nActor.Id = str - } - - return nActor - } - - return nActor -} - -func GetToFromJson(to []byte) []string { - var generic interface{} - - err := json.Unmarshal(to, &generic) - - if err != nil { - return nil - } - - t := reflect.TypeOf(generic) - - if t != nil { - var nStr []string - switch t.String() { - case "[]interface {}": - err = json.Unmarshal(to, &nStr) - CheckError(err, "error with To []interface{}") - return nStr - - case "string": - var str string - err = json.Unmarshal(to, &str) - CheckError(err, "error with To string") - nStr = append(nStr, str) - return nStr - } - } - - return nil -} - -func HasContextFromJson(context []byte) bool { - var generic interface{} - - err := json.Unmarshal(context, &generic) - - CheckError(err, "error with getting context") - - t := reflect.TypeOf(generic) - - hasContext := false - - switch t.String() { - case "[]interface {}": - var arrContext AtContextArray - err = json.Unmarshal(context, &arrContext.Context) - CheckError(err, "error with []interface{}") - if len(arrContext.Context) > 0 { - if arrContext.Context[0] == "https://www.w3.org/ns/activitystreams" { - hasContext = true - } - } - case "string": - var arrContext AtContextString - err = json.Unmarshal(context, &arrContext.Context) - CheckError(err, "error with string") - if arrContext.Context == "https://www.w3.org/ns/activitystreams" { - hasContext = true - } - } - - return hasContext -} - -func ObjectFromForm(r *http.Request, db *sql.DB, obj ObjectBase) ObjectBase { - - file, header, _ := r.FormFile("file") - - if file != nil { - defer file.Close() - - var tempFile = new(os.File) - obj.Attachment, tempFile = CreateAttachmentObject(file, header) - - defer tempFile.Close(); - - fileBytes, _ := ioutil.ReadAll(file) - - tempFile.Write(fileBytes) - - obj.Preview = CreatePreviewObject(obj.Attachment[0]) - } - - obj.AttributedTo = EscapeString(r.FormValue("name")) - obj.TripCode = EscapeString(r.FormValue("tripcode")) - obj.Name = EscapeString(r.FormValue("subject")) - obj.Content = EscapeString(r.FormValue("comment")) - - obj = ParseOptions(r, obj) - - var originalPost ObjectBase - originalPost.Id = EscapeString(r.FormValue("inReplyTo")) - - obj.InReplyTo = append(obj.InReplyTo, originalPost) - - var activity Activity - - if !IsInStringArray(activity.To, originalPost.Id) { - activity.To = append(activity.To, originalPost.Id) - } - - if originalPost.Id != "" { - if !IsActivityLocal(db, activity) { - id := GetActorFromID(originalPost.Id).Id - actor := GetActor(id) - if !IsInStringArray(obj.To, actor.Id) { - obj.To = append(obj.To, actor.Id) - } - } - } - - replyingTo := ParseCommentForReplies(r.FormValue("comment")) - - for _, e := range replyingTo { - - has := false - - for _, f := range obj.InReplyTo { - if e.Id == f.Id { - has = true - break - } - } - - if !has { - obj.InReplyTo = append(obj.InReplyTo, e) - - var activity Activity - - activity.To = append(activity.To, e.Id) - - if !IsActivityLocal(db, activity) { - id := GetActorFromID(e.Id).Id - actor := GetActor(id) - if !IsInStringArray(obj.To, actor.Id) { - obj.To = append(obj.To, actor.Id) - } - } - } - } - - return obj -} - -func ParseOptions(r *http.Request, obj ObjectBase) ObjectBase { - options := EscapeString(r.FormValue("options")) - if options != "" { - option := strings.Split(options, ";") - email := regexp.MustCompile(".+@.+\\..+") - wallet := regexp.MustCompile("wallet:.+") - delete := regexp.MustCompile("delete:.+") - for _, e := range option { - if e == "noko" { - obj.Option = append(obj.Option, "noko") - } else if e == "sage" { - obj.Option = append(obj.Option, "sage") - } else if e == "nokosage" { - obj.Option = append(obj.Option, "nokosage") - } else if email.MatchString(e) { - obj.Option = append(obj.Option, "email:" + e) - } else if wallet.MatchString(e) { - obj.Option = append(obj.Option, "wallet") - var wallet CryptoCur - value := strings.Split(e, ":") - wallet.Type = value[0] - wallet.Address = value[1] - obj.Wallet = append(obj.Wallet, wallet) - } else if delete.MatchString(e) { - obj.Option = append(obj.Option, e) - } - } - } - - return obj -} - -func GetActivityFromJson(r *http.Request, db *sql.DB) Activity { - body, _ := ioutil.ReadAll(r.Body) - - var respActivity ActivityRaw - - var nActivity Activity - - var nType string - - err := json.Unmarshal(body, &respActivity) - - CheckError(err, "error with activity from json") - - if HasContextFromJson(respActivity.AtContextRaw.Context) { - var jObj ObjectBase - - if respActivity.Type == "Note" { - jObj = GetObjectFromJson(body) - nType = "Create" - } else { - jObj = GetObjectFromJson(respActivity.ObjectRaw) - nType = respActivity.Type - } - - actor := GetActorFromJson(respActivity.ActorRaw) - to := GetToFromJson(respActivity.ToRaw) - cc := GetToFromJson(respActivity.CcRaw) - - nActivity.AtContext.Context = "https://www.w3.org/ns/activitystreams" - nActivity.Type = nType - nActivity.Actor = &actor - nActivity.Published = respActivity.Published - nActivity.Auth = respActivity.Auth - - if len(to) > 0 { - nActivity.To = to - } - - if len(cc) > 0 { - nActivity.Cc = cc - } - - nActivity.Name = respActivity.Name - nActivity.Object = &jObj - } - - return nActivity -} - -func CheckCaptcha(db *sql.DB, captcha string) bool { - parts := strings.Split(captcha, ":") - - if strings.Trim(parts[0], " ") == "" || strings.Trim(parts[1], " ") == ""{ - return false - } - - path := "public/" + parts[0] + ".png" - code := GetCaptchaCodeDB(db, path) - - if code != "" { - DeleteCaptchaCodeDB(db, path) - CreateNewCaptcha(db) - } - - if (code == strings.ToUpper(parts[1])) { - return true - } - - return false -} - -func ParseInboxRequest(w http.ResponseWriter, r *http.Request, db *sql.DB) { - activity := GetActivityFromJson(r, db) - - header := r.Header.Get("Authorization") - auth := strings.Split(header, " ") - - - if len(auth) < 2 { - response := RejectActivity(activity) - MakeActivityRequest(db, response) - return - } - - if !RemoteActorHasAuth(activity.Actor.Id, auth[1]) { - if !RemoteActorHasAuth(Domain, auth[1]) { - response := RejectActivity(activity) - MakeActivityRequest(db, response) - return - } - } - - switch(activity.Type) { - case "Create": - for _, e := range activity.To { - if IsActorLocal(db, e) { - if !IsActorLocal(db, activity.Actor.Id) { - WriteObjectToCache(db, *activity.Object) - } - } - } - break - - case "Delete": - for _, e := range activity.To { - actor := GetActorFromDB(db, e) - if actor.Id != "" { - if activity.Object.Replies != nil { - for _, k := range activity.Object.Replies.OrderedItems { - TombstoneObjectFromCache(db, k.Id) - DeleteObject(db, k.Id) - } - } - TombstoneObjectFromCache(db, activity.Object.Id) - break - } - } - break - - - case "Follow": - for _, e := range activity.To { - if GetActorFromDB(db, e).Id != "" { - response := AcceptFollow(activity) - response = SetActorFollowerDB(db, response) - MakeActivityRequest(db, response) - } else { - fmt.Println("follow request for rejected") - response := RejectActivity(activity) - MakeActivityRequest(db, response) - return - } - } - break - - case "Reject": - if activity.Object.Object.Type == "Follow" { - fmt.Println("follow rejected") - SetActorFollowingDB(db, activity) - } - break - } - -} - -func MakeActivityFollowingReq(w http.ResponseWriter, r *http.Request, activity Activity) bool { - actor := GetActor(activity.Object.Id) - - resp, err := http.NewRequest("POST", actor.Inbox, nil) - - CheckError(err, "Cannot make new get request to actor inbox for following req") - - defer resp.Body.Close() - - body, _ := ioutil.ReadAll(resp.Body) - - var respActivity Activity - - err = json.Unmarshal(body, &respActivity) - - if respActivity.Type == "Accept" { - return true - } - - return false -} - -func RemoteActorHasAuth(actor string, code string) bool { - - if actor == "" || code == "" { - return false - } - - req, err := http.NewRequest("GET", actor + "/verification&code=" + code, nil) - - CheckError(err, "could not make remote actor auth req") - - resp, err := http.DefaultClient.Do(req) - - CheckError(err, "could not make remote actor auth resp") - - defer resp.Body.Close() - - if resp.StatusCode == 200 { - return true - } - - return false -} diff --git a/cacheDatabase.go b/cacheDatabase.go new file mode 100644 index 0000000..380ade0 --- /dev/null +++ b/cacheDatabase.go @@ -0,0 +1,254 @@ +package main + +import "fmt" +import "database/sql" +import _ "github.com/lib/pq" + +func WriteObjectToCache(db *sql.DB, obj ObjectBase) ObjectBase { + if len(obj.Attachment) > 0 { + if obj.Preview.Href != "" { + WritePreviewToCache(db, *obj.Preview) + } + + for i, _ := range obj.Attachment { + WriteAttachmentToCache(db, obj.Attachment[i]) + WriteActivitytoCacheWithAttachment(db, obj, obj.Attachment[i], *obj.Preview) + } + + } else { + WriteActivitytoCache(db, obj) + } + + WriteObjectReplyToDB(db, obj) + + if obj.Replies != nil { + for _, e := range obj.Replies.OrderedItems { + WriteObjectToCache(db, e) + } + } + + return obj +} + +func WriteActivitytoCache(db *sql.DB, obj ObjectBase) { + + obj.Name = EscapeString(obj.Name) + obj.Content = EscapeString(obj.Content) + obj.AttributedTo = EscapeString(obj.AttributedTo) + + query := `select id from cacheactivitystream where id=$1` + + rows, err := db.Query(query, obj.Id) + + CheckError(err, "error selecting obj id from cache") + + var id string + defer rows.Close() + rows.Next() + rows.Scan(&id) + + if id != "" { + return + } + + query = `insert into cacheactivitystream (id, type, name, content, published, updated, attributedto, actor, tripcode) values ($1, $2, $3, $4, $5, $6, $7, $8, $9)` + + _, e := db.Exec(query, obj.Id ,obj.Type, obj.Name, obj.Content, obj.Published, obj.Published, obj.AttributedTo, obj.Actor.Id, obj.TripCode) + + if e != nil{ + fmt.Println("error inserting new activity cache") + panic(e) + } +} + +func WriteActivitytoCacheWithAttachment(db *sql.DB, obj ObjectBase, attachment ObjectBase, preview NestedObjectBase) { + + obj.Name = EscapeString(obj.Name) + obj.Content = EscapeString(obj.Content) + obj.AttributedTo = EscapeString(obj.AttributedTo) + + query := `select id from cacheactivitystream where id=$1` + + rows, err := db.Query(query, obj.Id) + + CheckError(err, "error selecting activity with attachment obj id cache") + + var id string + defer rows.Close() + rows.Next() + rows.Scan(&id) + + if id != "" { + return + } + + query = `insert into cacheactivitystream (id, type, name, content, attachment, preview, published, updated, attributedto, actor, tripcode) values ($1, $2, $3, $4, $5, $6, $7, $8, $9, $10, $11)` + + _, e := db.Exec(query, obj.Id ,obj.Type, obj.Name, obj.Content, attachment.Id, preview.Id, obj.Published, obj.Published, obj.AttributedTo, obj.Actor.Id, obj.TripCode) + + if e != nil{ + fmt.Println("error inserting new activity with attachment cache") + panic(e) + } +} + +func WriteAttachmentToCache(db *sql.DB, obj ObjectBase) { + + query := `select id from cacheactivitystream where id=$1` + + rows, err := db.Query(query, obj.Id) + + CheckError(err, "error selecting attachment obj id cache") + + var id string + defer rows.Close() + rows.Next() + rows.Scan(&id) + + if id != "" { + return + } + + query = `insert into cacheactivitystream (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.Published, obj.AttributedTo, obj.MediaType, obj.Size) + + if e != nil{ + fmt.Println("error inserting new attachment cache") + panic(e) + } +} + +func WritePreviewToCache(db *sql.DB, obj NestedObjectBase) { + + query := `select id from cacheactivitystream where id=$1` + + rows, err := db.Query(query, obj.Id) + + CheckError(err, "error selecting preview obj id cache") + + var id string + defer rows.Close() + rows.Next() + rows.Scan(&id) + + if id != "" { + return + } + + query = `insert into cacheactivitystream (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.Published, obj.AttributedTo, obj.MediaType, obj.Size) + + if e != nil{ + fmt.Println("error inserting new preview cache") + panic(e) + } +} + +func WriteObjectReplyToCache(db *sql.DB, obj ObjectBase) { + + for i, e := range obj.InReplyTo { + if(i == 0 || IsReplyInThread(db, obj.InReplyTo[0].Id, e.Id)){ + + query := `select id from replies where id=$1` + + rows, err := db.Query(query, obj.Id) + + CheckError(err, "error selecting obj id cache reply") + + var id string + defer rows.Close() + rows.Next() + rows.Scan(&id) + + if id != "" { + return + } + + query = `insert into cachereplies (id, inreplyto) values ($1, $2)` + + _, err = db.Exec(query, obj.Id, e.Id) + + if err != nil{ + fmt.Println("error inserting replies cache") + panic(err) + } + } + } + + 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) { + + if obj.Replies != nil { + 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) + } + + } + return + } +} + +func WriteActorToCache(db *sql.DB, actorID string) { + actor := GetActor(actorID) + collection := GetActorCollection(actor.Outbox) + + for _, e := range collection.OrderedItems { + WriteObjectToCache(db, e) + } +} + +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) + + DeleteObject(db, id) + } +} diff --git a/database.go b/database.go new file mode 100644 index 0000000..13cd08f --- /dev/null +++ b/database.go @@ -0,0 +1,1356 @@ +package main + +import "fmt" +import "database/sql" +import _ "github.com/lib/pq" +import "time" +import "os" +import "strings" +// import "regexp" +import "sort" + +func GetActorFromDB(db *sql.DB, id string) Actor { + var nActor Actor + + query :=`select type, id, name, preferedusername, inbox, outbox, following, followers, restricted, summary, publickeypem from actor where id=$1` + + rows, err := db.Query(query, id) + + if CheckError(err, "could not get actor from db query") != nil { + return nActor + } + + var publicKeyPem string + defer rows.Close() + for rows.Next() { + err = rows.Scan(&nActor.Type, &nActor.Id, &nActor.Name, &nActor.PreferredUsername, &nActor.Inbox, &nActor.Outbox, &nActor.Following, &nActor.Followers, &nActor.Restricted, &nActor.Summary, &publicKeyPem) + CheckError(err, "error with actor from db scan ") + } + + nActor.PublicKey = GetActorPemFromDB(db, publicKeyPem) + + return nActor +} + +func GetActorByNameFromDB(db *sql.DB, name string) Actor { + var nActor Actor + + query :=`select type, id, name, preferedusername, inbox, outbox, following, followers, restricted, summary, publickeypem from actor where name=$1` + + rows, err := db.Query(query, name) + + if CheckError(err, "could not get actor from db query") != nil { + return nActor + } + + var publicKeyPem string + defer rows.Close() + for rows.Next() { + err = rows.Scan(&nActor.Type, &nActor.Id, &nActor.Name, &nActor.PreferredUsername, &nActor.Inbox, &nActor.Outbox, &nActor.Following, &nActor.Followers, &nActor.Restricted, &nActor.Summary, &publicKeyPem) + CheckError(err, "error with actor from db scan ") + } + + nActor.PublicKey = GetActorPemFromDB(db, publicKeyPem) + + return nActor +} + +func CreateNewBoardDB(db *sql.DB, actor Actor) Actor{ + + query := `insert into actor (type, id, name, preferedusername, inbox, outbox, following, followers, summary, restricted) values ($1, $2, $3, $4, $5, $6, $7, $8, $9, $10)` + + _, err := db.Exec(query, actor.Type, actor.Id, actor.Name, actor.PreferredUsername, actor.Inbox, actor.Outbox, actor.Following, actor.Followers, actor.Summary, actor.Restricted) + + if err != nil { + fmt.Println("board exists") + } else { + fmt.Println("board added") + for _, e := range actor.AuthRequirement { + query = `insert into actorauth (type, board) values ($1, $2)` + _, err := db.Exec(query, e, actor.Name) + CheckError(err, "auth exists") + } + + var verify Verify + + verify.Identifier = actor.Id + verify.Code = CreateKey(50) + verify.Type = "admin" + + CreateVerification(db, verify) + + verify.Identifier = actor.Id + verify.Code = CreateKey(50) + verify.Type = "janitor" + + CreateVerification(db, verify) + + verify.Identifier = actor.Id + verify.Code = CreateKey(50) + verify.Type = "post" + + CreateVerification(db, verify) + + var nverify Verify + nverify.Board = actor.Id + nverify.Identifier = "admin" + nverify.Type = "admin" + CreateBoardMod(db, nverify) + + nverify.Board = actor.Id + nverify.Identifier = "janitor" + nverify.Type = "janitor" + CreateBoardMod(db, nverify) + + nverify.Board = actor.Id + nverify.Identifier = "post" + nverify.Type = "post" + CreateBoardMod(db, nverify) + + if actor.Name != "main" { + var nActor Actor + var nObject ObjectBase + var nActivity Activity + + nActivity.AtContext.Context = "https://www.w3.org/ns/activitystreams" + nActivity.Type = "Follow" + nActivity.Actor = &nActor + nActivity.Object = &nObject + nActivity.Actor.Id = Domain + var mActor Actor + nActivity.Object.Actor = &mActor + nActivity.Object.Actor.Id = actor.Id + nActivity.To = append(nActivity.To, actor.Id) + + response := AcceptFollow(nActivity) + SetActorFollowingDB(db, response) + MakeActivityRequest(db, nActivity) + } + + CreatePem(db, actor) + } + + return actor +} + +func GetBoards(db *sql.DB) []Actor { + + var board []Actor + + query := `select type, id, name, preferedusername, inbox, outbox, following, followers FROM actor` + + rows, err := db.Query(query) + + CheckError(err, "could not get boards from db query") + + defer rows.Close() + for rows.Next(){ + var actor = new(Actor) + + err = rows.Scan(&actor.Type, &actor.Id, &actor.Name, &actor.PreferredUsername, &actor.Inbox, &actor.Outbox, &actor.Following, &actor.Followers) + + if err !=nil{ + panic(err) + } + + board = append(board, *actor) + } + + return board +} + +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 != "" { + obj.Preview.Id = fmt.Sprintf("%s/%s", obj.Actor.Id, CreateUniqueID(db, obj.Actor.Id)) + obj.Preview.Published = time.Now().Format(time.RFC3339) + obj.Preview.Updated = time.Now().Format(time.RFC3339) + obj.Preview.AttributedTo = obj.Id + WritePreviewToDB(db, *obj.Preview) + } + + for i, _ := range obj.Attachment { + obj.Attachment[i].Id = fmt.Sprintf("%s/%s", obj.Actor.Id, CreateUniqueID(db, obj.Actor.Id)) + 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) + } + + } else { + WriteActivitytoDB(db, obj) + } + + WriteObjectReplyToDB(db, obj) + WriteWalletToDB(db, obj) + + return obj +} + +func WriteObjectUpdatesToDB(db *sql.DB, obj ObjectBase) { + query := `update activitystream set updated=$1 where id=$2` + + _, e := db.Exec(query, time.Now().Format(time.RFC3339), obj.Id) + + if e != nil{ + fmt.Println("error inserting updating inreplyto") + panic(e) + } + + query = `update cacheactivitystream set updated=$1 where id=$2` + + _, e = db.Exec(query, time.Now().Format(time.RFC3339), obj.Id) + + if e != nil{ + fmt.Println("error inserting updating cache inreplyto") + panic(e) + } +} + +func WriteObjectReplyToLocalDB(db *sql.DB, id string, replyto string) { + query := `insert into replies (id, inreplyto) values ($1, $2)` + + _, err := db.Exec(query, id, replyto) + + CheckError(err, "Could not insert local reply query") + + query = `select inreplyto from replies where id=$1` + + rows, err := db.Query(query,replyto) + + CheckError(err, "Could not query select inreplyto") + + defer rows.Close() + + for rows.Next() { + var val string + rows.Scan(&val) + if val == "" { + updated := time.Now().Format(time.RFC3339) + query := `update activitystream set updated=$1 where id=$2` + + _, err := db.Exec(query, updated, replyto) + + CheckError(err, "error with updating replyto updated at date") + } + } +} + +func WriteObjectReplyToDB(db *sql.DB, obj ObjectBase) { + for _, e := range obj.InReplyTo { + query := `select id from replies where id=$1 and inreplyto=$2` + + rows, err := db.Query(query, obj.Id, e.Id) + + CheckError(err, "error selecting replies db") + + defer rows.Close() + + var id string + rows.Next() + rows.Scan(&id) + + if id == "" { + query := `insert into replies (id, inreplyto) values ($1, $2)` + + _, err := db.Exec(query, obj.Id, e.Id) + + + CheckError(err, "error inserting replies db") + } + + update := true + for _, e := range obj.Option { + if e == "sage" || e == "nokosage" { + update = false + break + } + } + + if update { + WriteObjectUpdatesToDB(db, e) + } + } + + if len(obj.InReplyTo) < 1 { + query := `select id from replies where id=$1 and inreplyto=$2` + + rows, err := db.Query(query, obj.Id, "") + + CheckError(err, "error selecting replies db") + + defer rows.Close() + + var id string + rows.Next() + rows.Scan(&id) + + if id == "" { + query := `insert into replies (id, inreplyto) values ($1, $2)` + + _, err := db.Exec(query, obj.Id, "") + + CheckError(err, "error inserting replies db") + } + } +} + +func WriteWalletToDB(db *sql.DB, obj ObjectBase) { + for _, e := range obj.Option { + if e == "wallet" { + for _, e := range obj.Wallet { + query := `insert into wallet (id, type, address) values ($1, $2, $3)` + + _, err := db.Exec(query, obj.Id ,e.Type, e.Address) + + CheckError(err, "error with write wallet query") + } + return + } + } +} + +func WriteActivitytoDB(db *sql.DB, obj ObjectBase) { + + obj.Name = EscapeString(obj.Name) + obj.Content = EscapeString(obj.Content) + obj.AttributedTo = EscapeString(obj.AttributedTo) + + query := `insert into activitystream (id, type, name, content, published, updated, attributedto, actor, tripcode) values ($1, $2, $3, $4, $5, $6, $7, $8, $9)` + + _, e := db.Exec(query, obj.Id ,obj.Type, obj.Name, obj.Content, obj.Published, obj.Updated, obj.AttributedTo, obj.Actor.Id, obj.TripCode) + + if e != nil{ + fmt.Println("error inserting new activity") + panic(e) + } +} + +func WriteActivitytoDBWithAttachment(db *sql.DB, obj ObjectBase, attachment ObjectBase, preview NestedObjectBase) { + + obj.Name = EscapeString(obj.Name) + obj.Content = EscapeString(obj.Content) + obj.AttributedTo = EscapeString(obj.AttributedTo) + + query := `insert into activitystream (id, type, name, content, attachment, preview, published, updated, attributedto, actor, tripcode) values ($1, $2, $3, $4, $5, $6, $7, $8, $9, $10, $11)` + + _, e := db.Exec(query, obj.Id ,obj.Type, obj.Name, obj.Content, attachment.Id, preview.Id, obj.Published, obj.Updated, obj.AttributedTo, obj.Actor.Id, obj.TripCode) + + if e != nil{ + fmt.Println("error inserting new activity with attachment") + panic(e) + } +} + +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) + + if e != nil{ + fmt.Println("error inserting new attachment") + panic(e) + } +} + +func WritePreviewToDB(db *sql.DB, obj NestedObjectBase) { + 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) + + if e != nil{ + fmt.Println("error inserting new attachment") + panic(e) + } +} + +func GetActivityFromDB(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, tripcode 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, &post.TripCode) + + CheckError(err, "error scan object into post struct") + + post.Actor = &actor + + var postCnt int + var imgCnt int + post.Replies, postCnt, imgCnt = GetObjectRepliesDB(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 + + 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, x.tripcode from (select id, name, content, type, published, updated, attributedto, attachment, preview, actor, tripcode from activitystream where actor=$1 and id in (select id from replies where inreplyto='') and type='Note' union select id, name, content, type, published, updated, attributedto, attachment, preview, actor, tripcode 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 id, name, content, type, published, updated, attributedto, attachment, preview, actor, tripcode 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, &post.TripCode) + + 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 = 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 + + query := `select id, name, content, type, published, updated, attributedto, attachment, preview, actor, tripcode 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, &post.TripCode) + + CheckError(err, "error scan object into post struct") + + post.Actor = &actor + + var postCnt int + var imgCnt int + post.Replies, postCnt, imgCnt = GetObjectRepliesDB(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 + + return nColl +} + +func GetObjectFromDBCatalog(db *sql.DB, id string) Collection { + var nColl Collection + var result []ObjectBase + + query := `select x.id, x.name, x.content, x.type, x.published, x.updated, x.attributedto, x.attachment, x.preview, x.actor, x.tripcode from (select id, name, content, type, published, updated, attributedto, attachment, preview, actor, tripcode from activitystream where actor=$1 and id in (select id from replies where inreplyto='') and type='Note' union select id, name, content, type, published, updated, attributedto, attachment, preview, actor, tripcode 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 id, name, content, type, published, updated, attributedto, attachment, preview, actor, tripcode 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` + + 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, &post.TripCode) + + 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 + + query := `select x.id, x.name, x.content, x.type, x.published, x.updated, x.attributedto, x.attachment, x.preview, x.actor, x.tripcode from (select id, name, content, type, published, updated, attributedto, attachment, preview, actor, tripcode from activitystream where id=$1 and type='Note' union select id, name, content, type, published, updated, attributedto, attachment, preview, actor, tripcode from cacheactivitystream where id=$1 and type='Note') as x` + + rows, err := db.Query(query, postID) + + 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, &post.TripCode) + + CheckError(err, "error scan object into post struct") + + actor = GetActorFromDB(db, actor.Id) + + post.Actor = &actor + + nColl.Actor = &actor + + var postCnt int + var imgCnt int + post.Replies, postCnt, imgCnt = GetObjectRepliesDB(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 + + return nColl +} + +func GetInReplyToDB(db *sql.DB, parent ObjectBase) []ObjectBase { + var result []ObjectBase + + query := `select inreplyto from replies where id =$1` + + rows, err := db.Query(query, parent.Id) + + CheckError(err, "error with inreplyto db query") + + defer rows.Close() + for rows.Next() { + var post ObjectBase + + rows.Scan(&post.Id) + + result = append(result, post) + } + + 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, x.tripcode 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 postCount int + var attachCount 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(&postCount, &attachCount, &post.Id, &post.Name, &post.Content, &post.Type, &post.Published, &post.AttributedTo, &attachID, &previewID, &actor.Id, &post.TripCode) + + 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, postCount, attachCount +} + +func GetObjectRepliesDB(db *sql.DB, parent ObjectBase) (*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, x.tripcode 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") + + var postCount int + var attachCount 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(&postCount, &attachCount, &post.Id, &post.Name, &post.Content, &post.Type, &post.Published, &post.AttributedTo, &attachID, &previewID, &actor.Id, &post.TripCode) + + 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 + + return &nColl, postCount, attachCount +} + +func GetObjectRepliesReplies(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, tripcode from activitystream where id in (select id from replies where inreplyto=$1) and type='Note' order by updated asc` + + rows, err := db.Query(query, parent.Id) + + CheckError(err, "error with replies 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, &post.TripCode) + + CheckError(err, "error with replies replies db scan") + + post.Actor = &actor + + post.Attachment = GetObjectAttachment(db, attachID) + + post.Preview = GetObjectPreview(db, previewID) + + result = append(result, post) + } + + nColl.OrderedItems = result + + return &nColl, 0, 0 +} + +func GetObjectRepliesRepliesDB(db *sql.DB, parent ObjectBase) (*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, x.tripcode 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 postCount int + var attachCount 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(&postCount, &attachCount, &post.Id, &post.Name, &post.Content, &post.Type, &post.Published, &post.AttributedTo, &attachID, &previewID, &actor.Id, &post.TripCode) + + CheckError(err, "error with replies replies db scan") + + post.Actor = &actor + + post.Attachment = GetObjectAttachment(db, attachID) + + post.Preview = GetObjectPreview(db, previewID) + + result = append(result, post) + } + + nColl.OrderedItems = result + + return &nColl, postCount, attachCount +} + +func CheckIfObjectOP(db *sql.DB, id string) bool { + + var count int + + query := `select count(id) from replies where inreplyto='' and id=$1 ` + + rows, err := db.Query(query, id) + CheckError(err, "error checking if ID is OP") + + defer rows.Close() + rows.Next() + rows.Scan(&count) + + if count > 0 { + return true + } + + return false +} + +func GetObjectRepliesCount(db *sql.DB, parent ObjectBase) (int, int) { + + var countId int + var countImg int + + query := `select count(x.id) over(), sum(case when RTRIM(x.attachment) = '' then 0 else 1 end) over() from (select id, attachment from activitystream where id in (select id from replies where inreplyto=$1) and type='Note' union select id, attachment from cacheactivitystream where id in (select id from replies where inreplyto=$1) and type='Note') as x` + + rows, err := db.Query(query, parent.Id) + + CheckError(err, "error with replies count db query") + + defer rows.Close() + rows.Next() + rows.Scan(&countId, &countImg) + + return countId, countImg +} + +func GetObjectAttachment(db *sql.DB, id string) []ObjectBase { + + var attachments []ObjectBase + + 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 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 GetObjectPreview(db *sql.DB, id string) *NestedObjectBase { + + var preview NestedObjectBase + + 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") + + defer rows.Close() + for rows.Next() { + err = rows.Scan(&preview.Id, &preview.Type, &preview.Name, &preview.Href, &preview.MediaType, &preview.Size, &preview.Published) + } + + return &preview +} + +func GetObjectPostsTotalDB(db *sql.DB, actor Actor) int{ + + count := 0 + query := `select count(id) from activitystream 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 GetObjectImgsTotalDB(db *sql.DB, actor Actor) int{ + + count := 0 + query := `select count(attachment) from activitystream 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 DeletePreviewFromFile(db *sql.DB, id string) { + + var query = `select href from activitystream where id in (select preview from activitystream where id=$1)` + + rows, err := db.Query(query, id) + + CheckError(err, "error query delete attachment") + + defer rows.Close() + for rows.Next() { + var href string + + err := rows.Scan(&href) + href = strings.Replace(href, Domain + "/", "", 1) + CheckError(err, "error scanning delete attachment") + + if(href != "static/notfound.png") { + _, err = os.Stat(href) + if err == nil { + os.Remove(href) + } + } + } + + DeletePreviewFromDB(db, id) +} + +func DeleteAttachmentFromFile(db *sql.DB, id string) { + + var query = `select href from activitystream where id in (select attachment from activitystream where id=$1)` + + rows, err := db.Query(query, id) + + CheckError(err, "error query delete attachment") + + defer rows.Close() + for rows.Next() { + var href string + + err := rows.Scan(&href) + href = strings.Replace(href, Domain + "/", "", 1) + CheckError(err, "error scanning delete preview") + + if(href != "static/notfound.png") { + _, err = os.Stat(href) + if err == nil { + os.Remove(href) + } + } + } + + 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)` + + rows, err := db.Query(query, id) + + CheckError(err, "error query delete preview replies") + + defer rows.Close() + for rows.Next() { + var attachment string + + err := rows.Scan(&attachment) + + CheckError(err, "error scanning delete preview") + + DeletePreviewFromFile(db, attachment) + } +} + +func DeleteAttachmentRepliesFromDB(db *sql.DB, id string) { + var query = `select id from activitystream where id in (select id from replies where inreplyto=$1)` + + rows, err := db.Query(query, id) + + CheckError(err, "error query delete attachment replies") + + defer rows.Close() + for rows.Next() { + var attachment string + + err := rows.Scan(&attachment) + + CheckError(err, "error scanning delete attachment") + + DeleteAttachmentFromFile(db, attachment) + } +} + +func DeleteAttachmentFromDB(db *sql.DB, id string) { + datetime := time.Now().Format(time.RFC3339) + + var query = `update activitystream set type='Tombstone', mediatype='image/png', href=$1, name='', content='', attributedto='deleted', deleted=$2 where id in (select attachment from activitystream where id=$3)` + + _, err := db.Exec(query, Domain + "/static/notfound.png", datetime, id) + + CheckError(err, "error with delete attachment") + + query = `update cacheactivitystream set type='Tombstone', mediatype='image/png', href=$1, name='', content='', attributedto='deleted', deleted=$2 where id in (select attachment from cacheactivitystream where id=$3)` + + _, err = db.Exec(query, Domain + "/static/notfound.png", datetime, id) + + CheckError(err, "error with delete cache attachment") +} + +func DeletePreviewFromDB(db *sql.DB, id string) { + datetime := time.Now().Format(time.RFC3339) + + var query = `update activitystream set type='Tombstone', mediatype='image/png', href=$1, name='', content='', attributedto='deleted', deleted=$2 where id in (select preview from activitystream where id=$3)` + + _, err := db.Exec(query, Domain + "/static/notfound.png", datetime, id) + + CheckError(err, "error with delete preview") + + query = `update cacheactivitystream set type='Tombstone', mediatype='image/png', href=$1, name='', content='', attributedto='deleted', deleted=$2 where id in (select preview from cacheactivitystream where id=$3)` + + _, err = db.Exec(query, Domain + "/static/notfound.png", datetime, id) + + CheckError(err, "error with delete cache preview") +} + +func DeleteObjectRepliedTo(db *sql.DB, id string){ + query := `delete from replies where id=$1` + _, err := db.Exec(query, id) + + 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', tripcode='', deleted=$1 where id=$2` + + _, err := db.Exec(query, datetime, id) + + CheckError(err, "error with delete object") + + query = `update cacheactivitystream set type='Tombstone', name='', content='', attributedto='deleted', tripcode='', deleted=$1 where id=$2` + + _, err = db.Exec(query, datetime, id) + + CheckError(err, "error with delete cache object") +} + +func DeleteObjectsInReplyTo(db *sql.DB, id string) { + query := `delete from replies where id in (select id from replies where inreplyto=$1)` + + _, err := db.Exec(query, id) + + CheckError(err, "error with delete object replies to") +} + +func DeleteObjectRepliesFromDB(db *sql.DB, id string) { + datetime := time.Now().Format(time.RFC3339) + + var query = `update activitystream set type='Tombstone', name='', content='', attributedto='deleted', tripcode='', deleted=$1 where id in (select id from replies where inreplyto=$2)` + + _, err := db.Exec(query, datetime, id) + CheckError(err, "error with delete object replies") + + query = `update cacheactivitystream set type='Tombstone', name='', content='', attributedto='deleted', tripcode='', deleted=$1 where id in (select id from replies where inreplyto=$2)` + + _, err = db.Exec(query, datetime, id) + CheckError(err, "error with delete object cache replies") + +} + +func SetAttachmentFromDB(db *sql.DB, id string, _type string) { + datetime := time.Now().Format(time.RFC3339) + + var query = `update activitystream set type=$1, deleted=$2 where id in (select attachment from activitystream where id=$3)` + + _, err := db.Exec(query, _type, datetime, id) + + CheckError(err, "error with set attachment") + + query = `update cacheactivitystream set type=$1, deleted=$2 where id in (select attachment from cacheactivitystream where id=$3)` + + _, err = db.Exec(query, _type, datetime, id) + + CheckError(err, "error with set cache attachment") +} + +func SetAttachmentRepliesFromDB(db *sql.DB, id string, _type string) { + datetime := time.Now().Format(time.RFC3339) + + var query = `update activitystream set type=$1, deleted=$2 where id in (select attachment from activitystream where id in (select id from replies where inreplyto=$3))` + + _, err := db.Exec(query, _type, datetime, id) + + CheckError(err, "error with set attachment") + + query = `update cacheactivitystream set type=$1, deleted=$2 where id in (select attachment from cacheactivitystream where id in (select id from replies where inreplyto=$3))` + + _, err = db.Exec(query, _type, datetime, id) + + CheckError(err, "error with set cache attachment") +} + +func SetPreviewFromDB(db *sql.DB, id string, _type string) { + datetime := time.Now().Format(time.RFC3339) + + var query = `update activitystream set type=$1, deleted=$2 where id in (select preview from activitystream where id=$3)` + + _, err := db.Exec(query, _type, datetime, id) + + CheckError(err, "error with set preview") + + query = `update cacheactivitystream set type=$1, deleted=$2 where id in (select preview from cacheactivitystream where id=$3)` + + _, err = db.Exec(query, _type, datetime, id) + + CheckError(err, "error with set cache preview") +} + +func SetPreviewRepliesFromDB(db *sql.DB, id string, _type string) { + datetime := time.Now().Format(time.RFC3339) + + var query = `update activitystream set type=$1, deleted=$2 where id in (select preview from activitystream where id in (select id from replies where inreplyto=$3))` + + _, err := db.Exec(query, _type, datetime, id) + + CheckError(err, "error with set preview") + + query = `update cacheactivitystream set type=$1, deleted=$2 where id in (select preview from cacheactivitystream where id in (select id from replies where inreplyto=$3))` + + _, err = db.Exec(query, _type, datetime, id) + + CheckError(err, "error with set cache preview") +} + +func SetObjectFromDB(db *sql.DB, id string, _type string) { + datetime := time.Now().Format(time.RFC3339) + + var query = `update activitystream set type=$1, deleted=$2 where id=$3` + + _, err := db.Exec(query, _type, datetime, id) + + CheckError(err, "error with set object") + + query = `update cacheactivitystream set type=$1, deleted=$2 where id=$3` + + _, err = db.Exec(query, _type, datetime, id) + + CheckError(err, "error with set cache object") +} + +func SetObjectRepliesFromDB(db *sql.DB, id string, _type string) { + datetime := time.Now().Format(time.RFC3339) + + var query = `update activitystream set type=$1, deleted=$2 where id in (select id from replies where inreplyto=$3)` + _, err := db.Exec(query, _type, datetime, id) + CheckError(err, "error with set object replies") + + query = `update cacheactivitystream set type=$1, deleted=$2 where id in (select id from replies where inreplyto=$3)` + _, err = db.Exec(query, _type, datetime, id) + CheckError(err, "error with set cache object replies") +} + +func SetObject(db *sql.DB, id string, _type string) { + SetAttachmentFromDB(db, id, _type); + SetPreviewFromDB(db, id, _type); + SetObjectFromDB(db, id, _type); +} + +func SetObjectAndReplies(db *sql.DB, id string, _type string) { + SetAttachmentFromDB(db, id, _type); + SetPreviewFromDB(db, id, _type); + SetObjectRepliesFromDB(db, id, _type); + SetAttachmentRepliesFromDB(db, id, _type); + SetPreviewRepliesFromDB(db, id, _type); + SetObjectFromDB(db, id, _type); +} + +func DeleteObject(db *sql.DB, id string) { + DeleteReportActivity(db, id) + DeleteAttachmentFromFile(db, id) + DeletePreviewFromFile(db, id) + DeleteObjectFromDB(db, id) + DeleteObjectRepliedTo(db, id) +} + +func DeleteObjectAndReplies(db *sql.DB, id string) { + DeleteReportActivity(db, id) + DeleteAttachmentFromFile(db, id) + DeletePreviewFromFile(db, id) + DeleteObjectRepliedTo(db, id) + DeleteObjectsInReplyTo(db, id) + DeleteObjectRepliesFromDB(db, id) + DeleteAttachmentRepliesFromDB(db, id) + DeletePreviewRepliesFromDB(db, id) + DeleteObjectFromDB(db, id) +} + +func GetRandomCaptcha(db *sql.DB) string{ + query := `select identifier from verification where type='captcha' order by random() limit 1` + + rows, err := db.Query(query) + + CheckError(err, "could not get captcha") + + var verify string + + defer rows.Close() + + rows.Next() + err = rows.Scan(&verify) + + CheckError(err, "Could not get verify captcha") + + return verify +} + +func GetCaptchaTotal(db *sql.DB) int{ + query := `select count(*) from verification where type='captcha'` + + rows, err := db.Query(query) + + CheckError(err, "could not get query captcha total") + + defer rows.Close() + + var count int + for rows.Next(){ + if err := rows.Scan(&count); err != nil{ + CheckError(err, "could not get captcha total") + } + } + + return count +} + +func GetCaptchaCodeDB(db *sql.DB, verify string) string { + + query := `select code from verification where identifier=$1 limit 1` + + rows, err := db.Query(query, verify) + + CheckError(err, "could not get captcha verifciation") + + defer rows.Close() + + var code string + + rows.Next() + err = rows.Scan(&code) + + if err != nil { + fmt.Println("Could not get verification captcha") + } + + return code +} + +func GetActorAuth(db *sql.DB, actor string) []string { + query := `select type from actorauth where board=$1` + + rows, err := db.Query(query, actor) + + CheckError(err, "could not get actor auth") + + defer rows.Close() + + var auth []string + + for rows.Next() { + var e string + err = rows.Scan(&e) + + CheckError(err, "could not get actor auth row scan") + + auth = append(auth, e) + } + + return auth +} + +func DeleteCaptchaCodeDB(db *sql.DB, verify string) { + query := `delete from verification where identifier=$1` + + _, err := db.Exec(query, verify) + + CheckError(err, "could not delete captcah code db") + + os.Remove("./" + verify) +} + +func EscapeString(text string) string { + // re := regexp.MustCompile("(?i)(n)+(\\s+)?(i)+(\\s+)?(g)+(\\s+)?(e)+?(\\s+)?(r)+(\\s+)?") + // text = re.ReplaceAllString(text, "I love black people") + // re = regexp.MustCompile("(?i)(n)+(\\s+)?(i)+(\\s+)?(g)(\\s+)?(g)+(\\s+)?") + // text = re.ReplaceAllString(text, "I love black people") + // text = strings.Replace(text, "<", "<", -1) + return text +} + +func GetActorReportedTotal(db *sql.DB, id string) int { + query := `select count(id) from reported where board=$1` + + rows, err := db.Query(query, id) + + CheckError(err, "error getting actor reported total query") + + defer rows.Close() + + var count int + for rows.Next() { + rows.Scan(&count) + } + + return count +} + +func GetActorReportedDB(db *sql.DB, id string) []ObjectBase { + var nObj []ObjectBase + + query := `select id, count from reported where board=$1` + + rows, err := db.Query(query, id) + + CheckError(err, "error getting actor reported query") + + defer rows.Close() + + for rows.Next() { + var obj ObjectBase + + rows.Scan(&obj.Id, &obj.Size) + + nObj = append(nObj, obj) + } + + return nObj +} + +func GetActorPemFromDB(db *sql.DB, pemID string) PublicKeyPem { + query := `select id, owner, file from publickeypem where id=$1` + rows, err := db.Query(query, pemID) + + CheckError(err, "could not get public key pem from database") + + var pem PublicKeyPem + + defer rows.Close() + rows.Next() + rows.Scan(&pem.Id, &pem.Owner, &pem.PublicKeyPem) + f, _ := os.ReadFile(pem.PublicKeyPem) + + pem.PublicKeyPem = strings.ReplaceAll(string(f), "\r\n", `\n`) + + return pem +} diff --git a/follow.go b/follow.go new file mode 100644 index 0000000..667c6bd --- /dev/null +++ b/follow.go @@ -0,0 +1,222 @@ +package main + +import "net/http" +import "database/sql" +import _ "github.com/lib/pq" +import "encoding/json" + +func GetActorFollowing(w http.ResponseWriter, db *sql.DB, id string) { + var following Collection + + following.AtContext.Context = "https://www.w3.org/ns/activitystreams" + following.Type = "Collection" + following.TotalItems, _ = GetActorFollowTotal(db, id) + following.Items = GetActorFollowingDB(db, id) + + enc, _ := json.MarshalIndent(following, "", "\t") + w.Header().Set("Content-Type", activitystreams) + w.Write(enc) +} + +func GetActorFollowers(w http.ResponseWriter, db *sql.DB, id string) { + var following Collection + + following.AtContext.Context = "https://www.w3.org/ns/activitystreams" + following.Type = "Collection" + _, following.TotalItems = GetActorFollowTotal(db, id) + following.Items = GetActorFollowDB(db, id) + + enc, _ := json.MarshalIndent(following, "", "\t") + w.Header().Set("Content-Type", activitystreams) + w.Write(enc) +} + + +func GetActorFollowingDB(db *sql.DB, id string) []ObjectBase { + var followingCollection []ObjectBase + query := `select following from following where id=$1` + + rows, err := db.Query(query, id) + + CheckError(err, "error with following db query") + + defer rows.Close() + + for rows.Next() { + var obj ObjectBase + + err := rows.Scan(&obj.Id) + + CheckError(err, "error with following db scan") + + followingCollection = append(followingCollection, obj) + } + + return followingCollection +} + +func GetActorFollowDB(db *sql.DB, id string) []ObjectBase { + var followerCollection []ObjectBase + + query := `select follower from follower where id=$1` + + rows, err := db.Query(query, id) + + CheckError(err, "error with follower db query") + + defer rows.Close() + + for rows.Next() { + var obj ObjectBase + + err := rows.Scan(&obj.Id) + + CheckError(err, "error with followers db scan") + + followerCollection = append(followerCollection, obj) + } + + return followerCollection +} + +func GetActorFollowTotal(db *sql.DB, id string) (int, int) { + var following int + var followers int + + query := `select count(following) from following where id=$1` + + rows, err := db.Query(query, id) + + CheckError(err, "error with following total db query") + + defer rows.Close() + + for rows.Next() { + err := rows.Scan(&following) + + CheckError(err, "error with following total db scan") + } + + query = `select count(follower) from follower where id=$1` + + rows, err = db.Query(query, id) + + CheckError(err, "error with followers total db query") + + defer rows.Close() + + for rows.Next() { + err := rows.Scan(&followers) + + CheckError(err, "error with followers total db scan") + } + + return following, followers +} + +func AcceptFollow(activity Activity) Activity { + var accept Activity + accept.AtContext.Context = activity.AtContext.Context + accept.Type = "Accept" + accept.Actor = activity.Object.Actor + var nObj ObjectBase + var nActor Actor + accept.Object = &nObj + accept.Object.Actor = &nActor + accept.Object.Actor = activity.Actor + var nNested NestedObjectBase + var mActor Actor + accept.Object.Object = &nNested + accept.Object.Object.Actor = &mActor + accept.Object.Object.Actor = activity.Object.Actor + accept.Object.Object.Type = "Follow" + accept.To = append(accept.To, activity.Object.Actor.Id) + + return accept +} + +func RejectActivity(activity Activity) Activity { + var accept Activity + accept.AtContext.Context = activity.AtContext.Context + accept.Type = "Reject" + var nObj ObjectBase + var nActor Actor + accept.Object = &nObj + accept.Object.Actor = &nActor + accept.Actor = activity.Object.Actor + accept.Object.Actor = activity.Actor + var nNested NestedObjectBase + var mActor Actor + accept.Object.Object = &nNested + accept.Object.Object.Actor = &mActor + accept.Object.Object.Actor = activity.Object.Actor + accept.Object.Object.Type = "Follow" + accept.To = append(accept.To, activity.Actor.Id) + + return accept +} + +func SetActorFollowerDB(db *sql.DB, activity Activity) Activity { + var query string + alreadyFollow := false + followers := GetActorFollowDB(db, activity.Actor.Id) + + for _, e := range followers { + if e.Id == activity.Object.Actor.Id { + alreadyFollow = true + } + } + if alreadyFollow { + query = `delete from follower where id=$1 and follower=$2` + activity.Summary = activity.Object.Actor.Id + " Unfollow " + activity.Actor.Id + } else { + query = `insert into follower (id, follower) values ($1, $2)` + activity.Summary = activity.Object.Actor.Id + " Follow " + activity.Actor.Id + } + + _, err := db.Exec(query, activity.Actor.Id, activity.Object.Actor.Id) + + if CheckError(err, "error with follower db insert/delete") != nil { + activity.Type = "Reject" + return activity + } + + activity.Type = "Accept" + return activity +} + +func SetActorFollowingDB(db *sql.DB, activity Activity) Activity { + var query string + alreadyFollow := false + following := GetActorFollowingDB(db, activity.Object.Actor.Id) + + for _, e := range following { + if e.Id == activity.Actor.Id { + alreadyFollow = true + } + } + + if alreadyFollow { + query = `delete from following where id=$1 and following=$2` + activity.Summary = activity.Object.Actor.Id + " Unfollowing " + activity.Actor.Id + if !IsActorLocal(db, activity.Actor.Id) { + go 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 + if !IsActorLocal(db, activity.Actor.Id) { + go WriteActorToCache(db, activity.Actor.Id) + } + } + + _, err := db.Exec(query, activity.Object.Actor.Id, activity.Actor.Id) + + if CheckError(err, "error with following db insert/delete") != nil { + activity.Type = "Reject" + return activity + } + + activity.Type = "Accept" + return activity +} diff --git a/main.go b/main.go index da39b5e..aa0764c 100644 --- a/main.go +++ b/main.go @@ -853,10 +853,6 @@ func main() { SetObjectAndReplies(db, id, "Removed") } - if IsIDLocal(db, id){ - DeleteObjectRequest(db, id) - } - if(manage == "t"){ http.Redirect(w, r, "/" + *Key + "/" + board , http.StatusSeeOther) return diff --git a/outboxPost.go b/outboxPost.go new file mode 100644 index 0000000..8bddf42 --- /dev/null +++ b/outboxPost.go @@ -0,0 +1,647 @@ +package main + +import "fmt" +import "net/http" +import "database/sql" +import _ "github.com/lib/pq" +import "encoding/json" +import "reflect" +import "io/ioutil" +import "os" +import "regexp" +import "strings" + +func ParseOutboxRequest(w http.ResponseWriter, r *http.Request, db *sql.DB) { + + var activity Activity + + actor := GetActorFromPath(db, r.URL.Path, "/") + contentType := GetContentType(r.Header.Get("content-type")) + + defer r.Body.Close() + if contentType == "multipart/form-data" || contentType == "application/x-www-form-urlencoded" { + r.ParseMultipartForm(5 << 20) + if(BoardHasAuthType(db, actor.Name, "captcha") && CheckCaptcha(db, r.FormValue("captcha"))) { + f, header, _ := r.FormFile("file") + if(header != nil) { + if(header.Size > (7 << 20)){ + w.WriteHeader(http.StatusRequestEntityTooLarge) + w.Write([]byte("7MB max file size")) + return + } + + contentType, _ := GetFileContentType(f) + + if(!SupportedMIMEType(contentType)) { + w.WriteHeader(http.StatusNotAcceptable) + w.Write([]byte("file type not supported")) + return + } + } + + var nObj = CreateObject("Note") + nObj = ObjectFromForm(r, db, nObj) + + var act Actor + nObj.Actor = &act + nObj.Actor.Id = Domain + "/" + actor.Name + + nObj = WriteObjectToDB(db, nObj) + activity := CreateActivity("Create", nObj) + activity = AddFollowersToActivity(db, activity) + MakeActivityRequest(db, activity) + + var id string + op := len(nObj.InReplyTo) - 1 + if op >= 0 { + if nObj.InReplyTo[op].Id == "" { + id = nObj.Id + } else { + id = nObj.InReplyTo[0].Id + "|" + nObj.Id + } + } + + w.WriteHeader(http.StatusOK) + w.Write([]byte(id)) + return + } + + w.WriteHeader(http.StatusForbidden) + w.Write([]byte("captcha could not auth")) + } else { + activity = GetActivityFromJson(r, db) + + if IsActivityLocal(db, activity) { + switch activity.Type { + case "Create": + w.WriteHeader(http.StatusBadRequest) + w.Write([]byte("")) + break + case "Follow": + + var validActor bool + var validLocalActor bool + + header := r.Header.Get("Authorization") + + auth := strings.Split(header, " ") + + if len(auth) < 2 { + w.WriteHeader(http.StatusBadRequest) + w.Write([]byte("")) + return + } + + _, validActor = IsValidActor(activity.Object.Actor.Id) + validLocalActor = (activity.Actor.Id == actor.Id) + + var verify Verify + verify.Identifier = "admin" + verify.Board = activity.Actor.Id + + verify = GetVerificationCode(db, verify) + + code := verify.Code + code = CreateTripCode(code) + code = CreateTripCode(code) + + if code != auth[1] { + verify.Identifier = "admin" + verify.Board = Domain + + verify = GetVerificationCode(db, verify) + code = verify.Code + code = CreateTripCode(code) + code = CreateTripCode(code) + } + + var rActivity Activity + if validActor && validLocalActor && code == auth[1] || verify.Board == Domain { + rActivity = AcceptFollow(activity) + SetActorFollowingDB(db, rActivity) + MakeActivityRequest(db, activity) + } + + break + case "Delete": + fmt.Println("This is a delete") + w.WriteHeader(http.StatusBadRequest) + w.Write([]byte("could not process activity")) + break + case "Note": + w.WriteHeader(http.StatusBadRequest) + w.Write([]byte("could not process activity")) + break + + case "New": + + header := r.Header.Get("Authorization") + + auth := strings.Split(header, " ") + + if len(auth) < 2 { + w.WriteHeader(http.StatusBadRequest) + w.Write([]byte("")) + return + } + + var verify Verify + verify.Identifier = "admin" + verify.Board = Domain + + verify = GetVerificationCode(db, verify) + + code := verify.Code + code = CreateTripCode(code) + code = CreateTripCode(code) + + if code != auth[1] { + w.WriteHeader(http.StatusBadRequest) + w.Write([]byte("")) + return + } + + name := activity.Object.Actor.Name + prefname := activity.Object.Actor.PreferredUsername + summary := activity.Object.Actor.Summary + restricted := activity.Object.Actor.Restricted + + actor := CreateNewBoardDB(db, *CreateNewActor(name, prefname, summary, authReq, restricted)) + + if actor.Id != "" { + j, _ := json.Marshal(&actor) + w.Write([]byte(j)) + return + } + + w.WriteHeader(http.StatusBadRequest) + w.Write([]byte("")) + break + default: + w.WriteHeader(http.StatusBadRequest) + w.Write([]byte("could not process activity")) + } + } else { + fmt.Println("is NOT activity") + w.WriteHeader(http.StatusBadRequest) + w.Write([]byte("could not process activity")) + } + } +} + +func ObjectFromJson(r *http.Request, obj ObjectBase) ObjectBase { + body, _ := ioutil.ReadAll(r.Body) + + var respActivity ActivityRaw + + err := json.Unmarshal(body, &respActivity) + + CheckError(err, "error with object from json") + + if HasContextFromJson(respActivity.AtContextRaw.Context) { + var jObj ObjectBase + jObj = GetObjectFromJson(respActivity.ObjectRaw) + jObj.To = GetToFromJson(respActivity.ToRaw) + jObj.Cc = GetToFromJson(respActivity.CcRaw) + } + + return obj +} + +func GetObjectFromJson(obj []byte) ObjectBase { + var generic interface{} + + err := json.Unmarshal(obj, &generic) + + CheckError(err, "error with getting obj from json") + + t := reflect.TypeOf(generic) + + var nObj ObjectBase + if t != nil { + switch t.String() { + case "[]interface {}": + var lObj ObjectBase + var arrContext ObjectArray + err = json.Unmarshal(obj, &arrContext.Object) + CheckError(err, "error with []interface{} oject from json") + if len(arrContext.Object) > 0 { + lObj = arrContext.Object[0] + } + nObj = lObj + break + + case "map[string]interface {}": + var arrContext Object + err = json.Unmarshal(obj, &arrContext.Object) + CheckError(err, "error with object from json") + nObj = *arrContext.Object + break + + case "string": + var lObj ObjectBase + var arrContext ObjectString + err = json.Unmarshal(obj, &arrContext.Object) + CheckError(err, "error with string object from json") + lObj.Id = arrContext.Object + nObj = lObj + break + } + } + + return nObj +} + +func GetActorFromJson(actor []byte) Actor{ + var generic interface{} + var nActor Actor + err := json.Unmarshal(actor, &generic) + + if err != nil { + return nActor + } + + t := reflect.TypeOf(generic) + if t != nil { + switch t.String() { + case "map[string]interface {}": + err = json.Unmarshal(actor, &nActor) + CheckError(err, "error with To []interface{}") + + case "string": + var str string + err = json.Unmarshal(actor, &str) + CheckError(err, "error with To string") + nActor.Id = str + } + + return nActor + } + + return nActor +} + +func GetToFromJson(to []byte) []string { + var generic interface{} + + err := json.Unmarshal(to, &generic) + + if err != nil { + return nil + } + + t := reflect.TypeOf(generic) + + if t != nil { + var nStr []string + switch t.String() { + case "[]interface {}": + err = json.Unmarshal(to, &nStr) + CheckError(err, "error with To []interface{}") + return nStr + + case "string": + var str string + err = json.Unmarshal(to, &str) + CheckError(err, "error with To string") + nStr = append(nStr, str) + return nStr + } + } + + return nil +} + +func HasContextFromJson(context []byte) bool { + var generic interface{} + + err := json.Unmarshal(context, &generic) + + CheckError(err, "error with getting context") + + t := reflect.TypeOf(generic) + + hasContext := false + + switch t.String() { + case "[]interface {}": + var arrContext AtContextArray + err = json.Unmarshal(context, &arrContext.Context) + CheckError(err, "error with []interface{}") + if len(arrContext.Context) > 0 { + if arrContext.Context[0] == "https://www.w3.org/ns/activitystreams" { + hasContext = true + } + } + case "string": + var arrContext AtContextString + err = json.Unmarshal(context, &arrContext.Context) + CheckError(err, "error with string") + if arrContext.Context == "https://www.w3.org/ns/activitystreams" { + hasContext = true + } + } + + return hasContext +} + +func ObjectFromForm(r *http.Request, db *sql.DB, obj ObjectBase) ObjectBase { + + file, header, _ := r.FormFile("file") + + if file != nil { + defer file.Close() + + var tempFile = new(os.File) + obj.Attachment, tempFile = CreateAttachmentObject(file, header) + + defer tempFile.Close(); + + fileBytes, _ := ioutil.ReadAll(file) + + tempFile.Write(fileBytes) + + obj.Preview = CreatePreviewObject(obj.Attachment[0]) + } + + obj.AttributedTo = EscapeString(r.FormValue("name")) + obj.TripCode = EscapeString(r.FormValue("tripcode")) + obj.Name = EscapeString(r.FormValue("subject")) + obj.Content = EscapeString(r.FormValue("comment")) + + obj = ParseOptions(r, obj) + + var originalPost ObjectBase + originalPost.Id = EscapeString(r.FormValue("inReplyTo")) + + obj.InReplyTo = append(obj.InReplyTo, originalPost) + + var activity Activity + + if !IsInStringArray(activity.To, originalPost.Id) { + activity.To = append(activity.To, originalPost.Id) + } + + if originalPost.Id != "" { + if !IsActivityLocal(db, activity) { + id := GetActorFromID(originalPost.Id).Id + actor := GetActor(id) + if !IsInStringArray(obj.To, actor.Id) { + obj.To = append(obj.To, actor.Id) + } + } + } + + replyingTo := ParseCommentForReplies(r.FormValue("comment")) + + for _, e := range replyingTo { + + has := false + + for _, f := range obj.InReplyTo { + if e.Id == f.Id { + has = true + break + } + } + + if !has { + obj.InReplyTo = append(obj.InReplyTo, e) + + var activity Activity + + activity.To = append(activity.To, e.Id) + + if !IsActivityLocal(db, activity) { + id := GetActorFromID(e.Id).Id + actor := GetActor(id) + if !IsInStringArray(obj.To, actor.Id) { + obj.To = append(obj.To, actor.Id) + } + } + } + } + + return obj +} + +func ParseOptions(r *http.Request, obj ObjectBase) ObjectBase { + options := EscapeString(r.FormValue("options")) + if options != "" { + option := strings.Split(options, ";") + email := regexp.MustCompile(".+@.+\\..+") + wallet := regexp.MustCompile("wallet:.+") + delete := regexp.MustCompile("delete:.+") + for _, e := range option { + if e == "noko" { + obj.Option = append(obj.Option, "noko") + } else if e == "sage" { + obj.Option = append(obj.Option, "sage") + } else if e == "nokosage" { + obj.Option = append(obj.Option, "nokosage") + } else if email.MatchString(e) { + obj.Option = append(obj.Option, "email:" + e) + } else if wallet.MatchString(e) { + obj.Option = append(obj.Option, "wallet") + var wallet CryptoCur + value := strings.Split(e, ":") + wallet.Type = value[0] + wallet.Address = value[1] + obj.Wallet = append(obj.Wallet, wallet) + } else if delete.MatchString(e) { + obj.Option = append(obj.Option, e) + } + } + } + + return obj +} + +func GetActivityFromJson(r *http.Request, db *sql.DB) Activity { + body, _ := ioutil.ReadAll(r.Body) + + var respActivity ActivityRaw + + var nActivity Activity + + var nType string + + err := json.Unmarshal(body, &respActivity) + + CheckError(err, "error with activity from json") + + if HasContextFromJson(respActivity.AtContextRaw.Context) { + var jObj ObjectBase + + if respActivity.Type == "Note" { + jObj = GetObjectFromJson(body) + nType = "Create" + } else { + jObj = GetObjectFromJson(respActivity.ObjectRaw) + nType = respActivity.Type + } + + actor := GetActorFromJson(respActivity.ActorRaw) + to := GetToFromJson(respActivity.ToRaw) + cc := GetToFromJson(respActivity.CcRaw) + + nActivity.AtContext.Context = "https://www.w3.org/ns/activitystreams" + nActivity.Type = nType + nActivity.Actor = &actor + nActivity.Published = respActivity.Published + nActivity.Auth = respActivity.Auth + + if len(to) > 0 { + nActivity.To = to + } + + if len(cc) > 0 { + nActivity.Cc = cc + } + + nActivity.Name = respActivity.Name + nActivity.Object = &jObj + } + + return nActivity +} + +func CheckCaptcha(db *sql.DB, captcha string) bool { + parts := strings.Split(captcha, ":") + + if strings.Trim(parts[0], " ") == "" || strings.Trim(parts[1], " ") == ""{ + return false + } + + path := "public/" + parts[0] + ".png" + code := GetCaptchaCodeDB(db, path) + + if code != "" { + DeleteCaptchaCodeDB(db, path) + CreateNewCaptcha(db) + } + + if (code == strings.ToUpper(parts[1])) { + return true + } + + return false +} + +func ParseInboxRequest(w http.ResponseWriter, r *http.Request, db *sql.DB) { + activity := GetActivityFromJson(r, db) + + header := r.Header.Get("Authorization") + auth := strings.Split(header, " ") + + + if len(auth) < 2 { + response := RejectActivity(activity) + MakeActivityRequest(db, response) + return + } + + if !RemoteActorHasAuth(activity.Actor.Id, auth[1]) { + if !RemoteActorHasAuth(Domain, auth[1]) { + response := RejectActivity(activity) + MakeActivityRequest(db, response) + return + } + } + + switch(activity.Type) { + case "Create": + for _, e := range activity.To { + if IsActorLocal(db, e) { + if !IsActorLocal(db, activity.Actor.Id) { + WriteObjectToCache(db, *activity.Object) + } + } + } + break + + case "Delete": + for _, e := range activity.To { + actor := GetActorFromDB(db, e) + if actor.Id != "" { + if activity.Object.Replies != nil { + for _, k := range activity.Object.Replies.OrderedItems { + DeleteObject(db, k.Id) + } + } + DeleteObject(db, activity.Object.Id) + break + } + } + break + + + case "Follow": + for _, e := range activity.To { + if GetActorFromDB(db, e).Id != "" { + response := AcceptFollow(activity) + response = SetActorFollowerDB(db, response) + MakeActivityRequest(db, response) + } else { + fmt.Println("follow request for rejected") + response := RejectActivity(activity) + MakeActivityRequest(db, response) + return + } + } + break + + case "Reject": + if activity.Object.Object.Type == "Follow" { + fmt.Println("follow rejected") + SetActorFollowingDB(db, activity) + } + break + } + +} + +func MakeActivityFollowingReq(w http.ResponseWriter, r *http.Request, activity Activity) bool { + actor := GetActor(activity.Object.Id) + + resp, err := http.NewRequest("POST", actor.Inbox, nil) + + CheckError(err, "Cannot make new get request to actor inbox for following req") + + defer resp.Body.Close() + + body, _ := ioutil.ReadAll(resp.Body) + + var respActivity Activity + + err = json.Unmarshal(body, &respActivity) + + if respActivity.Type == "Accept" { + return true + } + + return false +} + +func RemoteActorHasAuth(actor string, code string) bool { + + if actor == "" || code == "" { + return false + } + + req, err := http.NewRequest("GET", actor + "/verification&code=" + code, nil) + + CheckError(err, "could not make remote actor auth req") + + resp, err := http.DefaultClient.Do(req) + + CheckError(err, "could not make remote actor auth resp") + + defer resp.Body.Close() + + if resp.StatusCode == 200 { + return true + } + + return false +} diff --git a/static/.#ncatalog.html b/static/.#ncatalog.html deleted file mode 120000 index 7e43052..0000000 --- a/static/.#ncatalog.html +++ /dev/null @@ -1 +0,0 @@ -namll@parabola.3055 \ No newline at end of file -- cgit v1.2.3