diff options
author | FChannel <> | 2022-05-06 22:45:27 -0700 |
---|---|---|
committer | FChannel <> | 2022-06-19 12:53:29 -0700 |
commit | 39012c6b17073f6933a5ead8beed64df555f7348 (patch) | |
tree | 7784d092fe196a9051c81cf6833997607ad2d268 /activitypub/actor.go | |
parent | af542e339e5a611d2a1b5876450bee841b577640 (diff) |
converting functions to activitypub object functions
Diffstat (limited to 'activitypub/actor.go')
-rw-r--r-- | activitypub/actor.go | 669 |
1 files changed, 315 insertions, 354 deletions
diff --git a/activitypub/actor.go b/activitypub/actor.go index 757f6bf..4351a9b 100644 --- a/activitypub/actor.go +++ b/activitypub/actor.go @@ -39,10 +39,10 @@ func CreateNewActor(board string, prefName string, summary string, authReq []str return actor } -func DeleteActorCache(actorID string) error { +func (actor Actor) DeleteCache() error { query := `select id from cacheactivitystream where id in (select id from cacheactivitystream where actor=$1)` - rows, err := config.DB.Query(query, actorID) + rows, err := config.DB.Query(query, actor.Id) if err != nil { return err @@ -64,78 +64,24 @@ func DeleteActorCache(actorID string) error { return nil } -func GetActorAuth(actor string) ([]string, error) { - var auth []string - - query := `select type from actorauth where board=$1` - - rows, err := config.DB.Query(query, actor) - if err != nil { - return auth, err - } - defer rows.Close() - - for rows.Next() { - var e string - if err := rows.Scan(&e); err != nil { - return auth, err - } - - auth = append(auth, e) - } - - return auth, nil -} +func (actor Actor) GetAutoSubscribe() (bool, error) { + var subscribed bool -func GetActorAutoSubscribeDB(id string) (bool, error) { query := `select autosubscribe from actor where id=$1` - - rows, err := config.DB.Query(query, id) - if err != nil { + if err := config.DB.QueryRow(query, actor.Id).Scan(&subscribed); err != nil { return false, err } - var subscribed bool - defer rows.Close() - rows.Next() - err = rows.Scan(&subscribed) - return subscribed, err + return subscribed, nil } -func GetActorByNameFromDB(name string) (Actor, error) { - var nActor Actor - - query := `select type, id, name, preferedusername, inbox, outbox, following, followers, restricted, summary, publickeypem from actor where name=$1` - - rows, err := config.DB.Query(query, name) - if err != nil { - return nActor, err - } - - var publicKeyPem string - defer rows.Close() - for rows.Next() { - if err := rows.Scan(&nActor.Type, &nActor.Id, &nActor.Name, &nActor.PreferredUsername, &nActor.Inbox, &nActor.Outbox, &nActor.Following, &nActor.Followers, &nActor.Restricted, &nActor.Summary, &publicKeyPem); err != nil { - return nActor, err - } - } - - if nActor.Id != "" && nActor.PublicKey.PublicKeyPem == "" { - if err := CreatePublicKeyFromPrivate(&nActor, publicKeyPem); err != nil { - return nActor, err - } - } - - return nActor, nil -} - -func GetActorCollectionDBType(actorId string, nType string) (Collection, error) { +func (actor Actor) GetCollectionType(nType string) (Collection, error) { 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, x.sensitive from (select id, name, content, type, published, updated, attributedto, attachment, preview, actor, tripcode, sensitive from activitystream where actor=$1 and id in (select id from replies where inreplyto='') and type=$2 union select id, name, content, type, published, updated, attributedto, attachment, preview, actor, tripcode, sensitive from activitystream where actor in (select following from following where id=$1) and id in (select id from replies where inreplyto='') and type=$2 union select id, name, content, type, published, updated, attributedto, attachment, preview, actor, tripcode, sensitive from cacheactivitystream where actor in (select following from following where id=$1) and id in (select id from replies where inreplyto='') and type=$2) as x order by x.updated desc` - rows, err := config.DB.Query(query, actorId, nType) + rows, err := config.DB.Query(query, actor.Id, nType) if err != nil { return nColl, err } @@ -158,6 +104,7 @@ func GetActorCollectionDBType(actorId string, nType string) (Collection, error) post.Replies = &replies var err error + post.Replies.TotalItems, post.Replies.TotalImgs, err = GetObjectRepliesCount(post) if err != nil { return nColl, err @@ -181,13 +128,13 @@ func GetActorCollectionDBType(actorId string, nType string) (Collection, error) return nColl, nil } -func GetActorCollectionDBTypeLimit(actorId string, nType string, limit int) (Collection, error) { +func (actor Actor) GetCollectionTypeLimit(nType string, limit int) (Collection, error) { 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, x.sensitive from (select id, name, content, type, published, updated, attributedto, attachment, preview, actor, tripcode, sensitive from activitystream where actor=$1 and id in (select id from replies where inreplyto='') and type=$2 union select id, name, content, type, published, updated, attributedto, attachment, preview, actor, tripcode, sensitive from activitystream where actor in (select following from following where id=$1) and id in (select id from replies where inreplyto='') and type=$2 union select id, name, content, type, published, updated, attributedto, attachment, preview, actor, tripcode, sensitive from cacheactivitystream where actor in (select following from following where id=$1) and id in (select id from replies where inreplyto='') and type=$2) as x order by x.updated desc limit $3` - rows, err := config.DB.Query(query, actorId, nType, limit) + rows, err := config.DB.Query(query, actor.Id, nType, limit) if err != nil { return nColl, err } @@ -233,45 +180,12 @@ func GetActorCollectionDBTypeLimit(actorId string, nType string, limit int) (Col return nColl, nil } -func GetActorCollectionReq(collection string) (Collection, error) { - var nCollection Collection - - req, err := http.NewRequest("GET", collection, nil) - if err != nil { - return nCollection, err - } - - // TODO: rewrite this for fiber - pass := "FIXME" - //_, pass := GetPasswordFromSession(r) - - req.Header.Set("Accept", config.ActivityStreams) - - req.Header.Set("Authorization", "Basic "+pass) - - resp, err := util.RouteProxy(req) - if err != nil { - return nCollection, err - } - defer resp.Body.Close() - - if resp.StatusCode == 200 { - body, _ := ioutil.ReadAll(resp.Body) - - if err := json.Unmarshal(body, &nCollection); err != nil { - return nCollection, err - } - } - - return nCollection, nil -} - -func GetActorFollowDB(id string) ([]ObjectBase, error) { +func (actor Actor) GetFollow() ([]ObjectBase, error) { var followerCollection []ObjectBase query := `select follower from follower where id=$1` - rows, err := config.DB.Query(query, id) + rows, err := config.DB.Query(query, actor.Id) if err != nil { return followerCollection, err } @@ -290,67 +204,40 @@ func GetActorFollowDB(id string) ([]ObjectBase, error) { return followerCollection, nil } -func GetActorFollowNameFromPath(path string) string { - var actor string - - re := regexp.MustCompile("f\\w+-") - - actor = re.FindString(path) - - actor = strings.Replace(actor, "f", "", 1) - actor = strings.Replace(actor, "-", "", 1) - - return actor -} - -func GetActorFollowTotal(id string) (int, int, error) { +func (actor Actor) GetFollowingTotal() (int, error) { var following int - var followers int query := `select count(following) from following where id=$1` - - rows, err := config.DB.Query(query, id) - if err != nil { - return 0, 0, err + if err := config.DB.QueryRow(query, actor.Id).Scan(&following); err != nil { + return following, err } - defer rows.Close() - for rows.Next() { - if err := rows.Scan(&following); err != nil { - return following, 0, err - } - } - - query = `select count(follower) from follower where id=$1` - - rows, err = config.DB.Query(query, id) - if err != nil { - return 0, 0, err - } - defer rows.Close() + return following, nil +} - for rows.Next() { - if err := rows.Scan(&followers); err != nil { - return following, followers, err - } +func (actor Actor) GetFollowersTotal() (int, error) { + var followers int + query := `select count(follower) from follower where id=$1` + if err := config.DB.QueryRow(query, actor.Id).Scan(&followers); err != nil { + return followers, err } - return following, followers, nil + return followers, nil } -func GetActorFollowers(ctx *fiber.Ctx, id string) error { +func (actor Actor) GetFollowersResp(ctx *fiber.Ctx) error { var following Collection var err error following.AtContext.Context = "https://www.w3.org/ns/activitystreams" following.Type = "Collection" - _, following.TotalItems, err = GetActorFollowTotal(id) + following.TotalItems, err = actor.GetFollowingTotal() if err != nil { return err } - following.Items, err = GetActorFollowDB(id) + following.Items, err = actor.GetFollow() if err != nil { return err } @@ -361,18 +248,18 @@ func GetActorFollowers(ctx *fiber.Ctx, id string) error { return err } -func GetActorFollowing(ctx *fiber.Ctx, id string) error { +func (actor Actor) GetFollowingResp(ctx *fiber.Ctx) error { var following Collection var err error following.AtContext.Context = "https://www.w3.org/ns/activitystreams" following.Type = "Collection" - following.TotalItems, _, err = GetActorFollowTotal(id) + following.TotalItems, err = actor.GetFollowingTotal() if err != nil { return err } - following.Items, err = GetActorFollowingDB(id) + following.Items, err = actor.GetFollowing() if err != nil { return err } @@ -384,11 +271,11 @@ func GetActorFollowing(ctx *fiber.Ctx, id string) error { return err } -func GetActorFollowingDB(id string) ([]ObjectBase, error) { +func (actor Actor) GetFollowing() ([]ObjectBase, error) { var followingCollection []ObjectBase query := `select following from following where id=$1` - rows, err := config.DB.Query(query, id) + rows, err := config.DB.Query(query, actor.Id) if err != nil { return followingCollection, err } @@ -407,116 +294,22 @@ func GetActorFollowingDB(id string) ([]ObjectBase, error) { return followingCollection, nil } -func GetActorFromDB(id string) (Actor, error) { - var nActor Actor - - query := `select type, id, name, preferedusername, inbox, outbox, following, followers, restricted, summary, publickeypem from actor where id=$1` - - var publicKeyPem string - err := config.DB.QueryRow(query, id).Scan(&nActor.Type, &nActor.Id, &nActor.Name, &nActor.PreferredUsername, &nActor.Inbox, &nActor.Outbox, &nActor.Following, &nActor.Followers, &nActor.Restricted, &nActor.Summary, &publicKeyPem) - if err != nil { - return nActor, err - } - - nActor.PublicKey, err = GetActorPemFromDB(publicKeyPem) - if err != nil { - return nActor, err - } - - if nActor.Id != "" && nActor.PublicKey.PublicKeyPem == "" { - if err := CreatePublicKeyFromPrivate(&nActor, publicKeyPem); err != nil { - return nActor, err - } - } - - return nActor, nil -} - -func GetActorFromJson(actor []byte) (Actor, error) { - var generic interface{} - var nActor Actor - err := json.Unmarshal(actor, &generic) - if err != nil { - return nActor, err - } - - if generic != nil { - switch generic.(type) { - case map[string]interface{}: - err = json.Unmarshal(actor, &nActor) - break - - case string: - var str string - err = json.Unmarshal(actor, &str) - nActor.Id = str - break - } - - return nActor, err - } - - return nActor, nil -} - -func GetActorInfo(ctx *fiber.Ctx, id string) error { - actor, err := GetActorFromDB(id) - if err != nil { - return err - } - +func (actor Actor) GetInfoResp(ctx *fiber.Ctx) error { enc, _ := json.MarshalIndent(actor, "", "\t") ctx.Response().Header.Set("Content-Type", "application/ld+json; profile=\"https://www.w3.org/ns/activitystreams\"") - _, err = ctx.Write(enc) + _, err := ctx.Write(enc) return err } -func GetActorInstance(path string) (string, string) { - re := regexp.MustCompile(`([@]?([\w\d.-_]+)[@](.+))`) - atFormat := re.MatchString(path) - - if atFormat { - match := re.FindStringSubmatch(path) - if len(match) > 2 { - return match[2], match[3] - } - } - - re = regexp.MustCompile(`(https?://)(www)?([\w\d-_.:]+)(/|\s+|\r|\r\n)?$`) - mainActor := re.MatchString(path) - if mainActor { - match := re.FindStringSubmatch(path) - if len(match) > 2 { - return "main", match[3] - } - } - - re = regexp.MustCompile(`(https?://)?(www)?([\w\d-_.:]+)\/([\w\d-_.]+)(\/([\w\d-_.]+))?`) - httpFormat := re.MatchString(path) - - if httpFormat { - match := re.FindStringSubmatch(path) - if len(match) > 3 { - if match[4] == "users" { - return match[6], match[3] - } - - return match[4], match[3] - } - } - - return "", "" -} - -func GetActorObjectCollectionFromDB(actorId string) (Collection, error) { +func (actor Actor) GetCollection() (Collection, error) { var nColl Collection var result []ObjectBase query := `select id, name, content, type, published, updated, attributedto, attachment, preview, actor, tripcode, sensitive from activitystream where actor=$1 and id in (select id from replies where inreplyto='') and type='Note' order by updated desc` - rows, err := config.DB.Query(query, actorId) + rows, err := config.DB.Query(query, actor.Id) if err != nil { return nColl, err } @@ -562,12 +355,12 @@ func GetActorObjectCollectionFromDB(actorId string) (Collection, error) { return nColl, nil } -func GetActorReportedDB(id string) ([]ObjectBase, error) { +func (actor Actor) GetReported() ([]ObjectBase, error) { var nObj []ObjectBase query := `select id, count, reason from reported where board=$1` - rows, err := config.DB.Query(query, id) + rows, err := config.DB.Query(query, actor.Id) if err != nil { return nObj, err } @@ -585,165 +378,363 @@ func GetActorReportedDB(id string) ([]ObjectBase, error) { return nObj, nil } -func GetActorReportedTotal(id string) (int, error) { - query := `select count(id) from reported where board=$1` +func (actor Actor) GetReportedTotal() (int, error) { + var count int - rows, err := config.DB.Query(query, id) - if err != nil { + query := `select count(id) from reported where board=$1` + if err := config.DB.QueryRow(query, actor.Id).Scan(&count); err != nil { return 0, err } + return count, nil +} + +func (actor Actor) GetAllArchive(offset int) (Collection, error) { + var nColl Collection + var result []ObjectBase + + query := `select x.id, x.updated from (select id, updated from activitystream where actor=$1 and id in (select id from replies where inreplyto='') and type='Note' union select id, updated 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, updated 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 offset $2` + + rows, err := config.DB.Query(query, actor.Id, offset) + if err != nil { + return nColl, err + } defer rows.Close() - var count int for rows.Next() { - rows.Scan(&count) + var post ObjectBase + + if err := rows.Scan(&post.Id, &post.Updated); err != nil { + return nColl, err + } + + post.Replies, _, _, err = GetObjectRepliesDB(post) + + result = append(result, post) } - return count, nil + nColl.OrderedItems = result + + return nColl, nil } -func GetActorsFollowPostFromId(actors []string, id string) (Collection, error) { - var collection Collection +func (actor Actor) IsAlreadyFollowing(follow string) (bool, error) { + followers, err := actor.GetFollowing() + if err != nil { + return false, err + } - for _, e := range actors { - tempCol, err := GetObjectByIDFromDB(e + "/" + id) - if err != nil { - return collection, err + for _, e := range followers { + if e.Id == follow { + return true, nil } + } - if len(tempCol.OrderedItems) > 0 { - collection = tempCol - return collection, nil + return false, nil +} + +func (actor Actor) IsAlreadyFollower(follow string) (bool, error) { + followers, err := actor.GetFollow() + if err != nil { + return false, err + } + + for _, e := range followers { + if e.Id == follow { + return true, nil } } - return collection, nil + return false, nil } -func GetActorPost(ctx *fiber.Ctx, path string) error { - collection, err := GetCollectionFromPath(config.Domain + "" + path) +func (actor Actor) SetActorAutoSubscribeDB() error { + current, err := actor.GetAutoSubscribe() if err != nil { return err } - if len(collection.OrderedItems) > 0 { - enc, err := json.MarshalIndent(collection, "", "\t") - if err != nil { - return err - } + query := `update actor set autosubscribe=$1 where id=$2` - ctx.Response().Header.Set("Content-Type", "application/ld+json; profile=\"https://www.w3.org/ns/activitystreams\"") - _, err = ctx.Write(enc) + _, err = config.DB.Exec(query, !current, actor.Id) + return err +} + +func (actor Actor) GetOutbox(ctx *fiber.Ctx) error { + + var collection Collection + + c, err := actor.GetCollection() + if err != nil { + return err + } + collection.OrderedItems = c.OrderedItems + + collection.AtContext.Context = "https://www.w3.org/ns/activitystreams" + collection.Actor = &actor + + collection.TotalItems, err = GetObjectPostsTotalDB(actor) + if err != nil { return err } - return nil -} + collection.TotalImgs, err = GetObjectImgsTotalDB(actor) + if err != nil { + return err + } -func GetAllActorArchiveDB(id string, offset int) (Collection, error) { - var nColl Collection - var result []ObjectBase + enc, _ := json.Marshal(collection) - query := `select x.id, x.updated from (select id, updated from activitystream where actor=$1 and id in (select id from replies where inreplyto='') and type='Note' union select id, updated 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, updated 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 offset $2` + ctx.Response().Header.Set("Content-Type", config.ActivityStreams) + _, err = ctx.Write(enc) + return err +} - rows, err := config.DB.Query(query, id, offset) +func (actor Actor) UnArchiveLast() error { + col, err := actor.GetCollectionTypeLimit("Archive", 1) if err != nil { - return nColl, err + return err } - defer rows.Close() - for rows.Next() { - var post ObjectBase + for _, e := range col.OrderedItems { + for _, k := range e.Replies.OrderedItems { + if err := UpdateObjectTypeDB(k.Id, "Note"); err != nil { + return err + } + } - if err := rows.Scan(&post.Id, &post.Updated); err != nil { - return nColl, err + if err := UpdateObjectTypeDB(e.Id, "Note"); err != nil { + return err } + } - post.Replies, _, _, err = GetObjectRepliesDB(post) + return nil +} - result = append(result, post) +func GetActorByNameFromDB(name string) (Actor, error) { + var nActor Actor + var publicKeyPem string + + query := `select type, id, name, preferedusername, inbox, outbox, following, followers, restricted, summary, publickeypem from actor where name=$1` + err := config.DB.QueryRow(query, name).Scan(&nActor.Type, &nActor.Id, &nActor.Name, &nActor.PreferredUsername, &nActor.Inbox, &nActor.Outbox, &nActor.Following, &nActor.Followers, &nActor.Restricted, &nActor.Summary, &publicKeyPem) + if err != nil { + return nActor, err } - nColl.OrderedItems = result + if nActor.Id != "" && nActor.PublicKey.PublicKeyPem == "" { + if err := CreatePublicKeyFromPrivate(&nActor, publicKeyPem); err != nil { + return nActor, err + } + } - return nColl, nil + return nActor, nil } -func GetBoards() ([]Actor, error) { - var boards []Actor +func GetActorCollectionReq(collection string) (Collection, error) { + var nCollection Collection - query := `select type, id, name, preferedusername, inbox, outbox, following, followers FROM actor` + req, err := http.NewRequest("GET", collection, nil) + if err != nil { + return nCollection, err + } - rows, err := config.DB.Query(query) + // TODO: rewrite this for fiber + pass := "FIXME" + //_, pass := GetPasswordFromSession(r) + + req.Header.Set("Accept", config.ActivityStreams) + + req.Header.Set("Authorization", "Basic "+pass) + + resp, err := util.RouteProxy(req) if err != nil { - return boards, err + return nCollection, err } + defer resp.Body.Close() - defer rows.Close() - for rows.Next() { - var actor = new(Actor) + if resp.StatusCode == 200 { + body, _ := ioutil.ReadAll(resp.Body) - if err := rows.Scan(&actor.Type, &actor.Id, &actor.Name, &actor.PreferredUsername, &actor.Inbox, &actor.Outbox, &actor.Following, &actor.Followers); err != nil { - return boards, err + if err := json.Unmarshal(body, &nCollection); err != nil { + return nCollection, err } - - boards = append(boards, *actor) } - return boards, nil + return nCollection, nil } -func IsActorLocal(id string) (bool, error) { - actor, err := GetActorFromDB(id) - return actor.Id != "", err +func GetActorFollowNameFromPath(path string) string { + var actor string + + re := regexp.MustCompile("f\\w+-") + + actor = re.FindString(path) + + actor = strings.Replace(actor, "f", "", 1) + actor = strings.Replace(actor, "-", "", 1) + + return actor } -func IsAlreadyFollowing(actor string, follow string) (bool, error) { - followers, err := GetActorFollowingDB(actor) +func GetActorFromDB(id string) (Actor, error) { + var nActor Actor + + query := `select type, id, name, preferedusername, inbox, outbox, following, followers, restricted, summary, publickeypem from actor where id=$1` + + var publicKeyPem string + err := config.DB.QueryRow(query, id).Scan(&nActor.Type, &nActor.Id, &nActor.Name, &nActor.PreferredUsername, &nActor.Inbox, &nActor.Outbox, &nActor.Following, &nActor.Followers, &nActor.Restricted, &nActor.Summary, &publicKeyPem) if err != nil { - return false, err + return nActor, err } - for _, e := range followers { - if e.Id == follow { - return true, nil + nActor.PublicKey, err = GetActorPemFromDB(publicKeyPem) + if err != nil { + return nActor, err + } + + if nActor.Id != "" && nActor.PublicKey.PublicKeyPem == "" { + if err := CreatePublicKeyFromPrivate(&nActor, publicKeyPem); err != nil { + return nActor, err } } - return false, nil + return nActor, nil } -func IsAlreadyFollower(actor string, follow string) (bool, error) { - followers, err := GetActorFollowDB(actor) +func GetActorFromJson(actor []byte) (Actor, error) { + var generic interface{} + var nActor Actor + err := json.Unmarshal(actor, &generic) if err != nil { - return false, err + return nActor, err } - for _, e := range followers { - if e.Id == follow { - return true, nil + if generic != nil { + switch generic.(type) { + case map[string]interface{}: + err = json.Unmarshal(actor, &nActor) + break + + case string: + var str string + err = json.Unmarshal(actor, &str) + nActor.Id = str + break } + + return nActor, err } - return false, nil + return nActor, nil } -func SetActorAutoSubscribeDB(id string) error { - current, err := GetActorAutoSubscribeDB(id) +func GetActorInstance(path string) (string, string) { + re := regexp.MustCompile(`([@]?([\w\d.-_]+)[@](.+))`) + atFormat := re.MatchString(path) + + if atFormat { + match := re.FindStringSubmatch(path) + if len(match) > 2 { + return match[2], match[3] + } + } + + re = regexp.MustCompile(`(https?://)(www)?([\w\d-_.:]+)(/|\s+|\r|\r\n)?$`) + mainActor := re.MatchString(path) + if mainActor { + match := re.FindStringSubmatch(path) + if len(match) > 2 { + return "main", match[3] + } + } + + re = regexp.MustCompile(`(https?://)?(www)?([\w\d-_.:]+)\/([\w\d-_.]+)(\/([\w\d-_.]+))?`) + httpFormat := re.MatchString(path) + + if httpFormat { + match := re.FindStringSubmatch(path) + if len(match) > 3 { + if match[4] == "users" { + return match[6], match[3] + } + + return match[4], match[3] + } + } + + return "", "" +} + +func GetActorsFollowPostFromId(actors []string, id string) (Collection, error) { + var collection Collection + + for _, e := range actors { + tempCol, err := GetObjectByIDFromDB(e + "/" + id) + if err != nil { + return collection, err + } + + if len(tempCol.OrderedItems) > 0 { + collection = tempCol + return collection, nil + } + } + + return collection, nil +} + +func GetActorPost(ctx *fiber.Ctx, path string) error { + collection, err := GetCollectionFromPath(config.Domain + "" + path) if err != nil { return err } - query := `update actor set autosubscribe=$1 where id=$2` + if len(collection.OrderedItems) > 0 { + enc, err := json.MarshalIndent(collection, "", "\t") + if err != nil { + return err + } - _, err = config.DB.Exec(query, !current, id) - return err + ctx.Response().Header.Set("Content-Type", "application/ld+json; profile=\"https://www.w3.org/ns/activitystreams\"") + _, err = ctx.Write(enc) + return err + } + + return nil +} + +func GetBoards() ([]Actor, error) { + var boards []Actor + + query := `select type, id, name, preferedusername, inbox, outbox, following, followers FROM actor` + + rows, err := config.DB.Query(query) + if err != nil { + return boards, err + } + + defer rows.Close() + for rows.Next() { + var actor = new(Actor) + + if err := rows.Scan(&actor.Type, &actor.Id, &actor.Name, &actor.PreferredUsername, &actor.Inbox, &actor.Outbox, &actor.Following, &actor.Followers); err != nil { + return boards, err + } + + boards = append(boards, *actor) + } + + return boards, nil +} + +func IsActorLocal(id string) (bool, error) { + actor, err := GetActorFromDB(id) + return actor.Id != "", err } func SetActorFollowerDB(activity Activity) (Activity, error) { var query string - alreadyFollow, err := IsAlreadyFollower(activity.Actor.Id, activity.Object.Actor) + alreadyFollow, err := activity.Actor.IsAlreadyFollower(activity.Object.Actor) if err != nil { return activity, err } @@ -863,33 +854,3 @@ func WriteActorObjectToCache(obj ObjectBase) (ObjectBase, error) { return obj, nil } - -func GetActorOutbox(ctx *fiber.Ctx, actor Actor) error { - - var collection Collection - - c, err := GetActorObjectCollectionFromDB(actor.Id) - if err != nil { - return err - } - collection.OrderedItems = c.OrderedItems - - collection.AtContext.Context = "https://www.w3.org/ns/activitystreams" - collection.Actor = &actor - - collection.TotalItems, err = GetObjectPostsTotalDB(actor) - if err != nil { - return err - } - - collection.TotalImgs, err = GetObjectImgsTotalDB(actor) - if err != nil { - return err - } - - enc, _ := json.Marshal(collection) - - ctx.Response().Header.Set("Content-Type", config.ActivityStreams) - _, err = ctx.Write(enc) - return err -} |