From 580dec5b89215310ce34341e11ff17fe38bdb63a Mon Sep 17 00:00:00 2001 From: FChannel <> Date: Sun, 8 May 2022 14:57:40 -0700 Subject: more cleanup, logging and error logging everywhere things are mostly in place can work on "features" and polish --- webfinger/comm.go | 88 -------------- webfinger/util.go | 36 +++++- webfinger/webfinger.go | 313 ------------------------------------------------- 3 files changed, 30 insertions(+), 407 deletions(-) delete mode 100644 webfinger/comm.go delete mode 100644 webfinger/webfinger.go (limited to 'webfinger') diff --git a/webfinger/comm.go b/webfinger/comm.go deleted file mode 100644 index ed20779..0000000 --- a/webfinger/comm.go +++ /dev/null @@ -1,88 +0,0 @@ -package webfinger - -import ( - "encoding/json" - "errors" - "io/ioutil" - "net/http" - "regexp" - - "github.com/FChannel0/FChannel-Server/activitypub" - "github.com/FChannel0/FChannel-Server/config" - "github.com/FChannel0/FChannel-Server/util" -) - -// TODO: All of these functions in this file I don't know where to place so they'll remain here until I find a better place for them. - -func GetActorCollection(collection string) (activitypub.Collection, error) { - var nCollection activitypub.Collection - - if collection == "" { - return nCollection, errors.New("invalid collection") - } - - req, err := http.NewRequest("GET", collection, nil) - if err != nil { - return nCollection, err - } - - req.Header.Set("Accept", config.ActivityStreams) - - 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 len(body) > 0 { - if err := json.Unmarshal(body, &nCollection); err != nil { - return nCollection, err - } - } - } - - return nCollection, nil -} - -func GetCollectionFromReq(path string) (activitypub.Collection, error) { - var respCollection activitypub.Collection - - req, err := http.NewRequest("GET", path, nil) - if err != nil { - return respCollection, err - } - - req.Header.Set("Accept", config.ActivityStreams) - - resp, err := util.RouteProxy(req) - if err != nil { - return respCollection, err - } - defer resp.Body.Close() - - body, _ := ioutil.ReadAll(resp.Body) - - err = json.Unmarshal(body, &respCollection) - return respCollection, err -} - -func GetActorsFollowFromName(actor activitypub.Actor, name string) ([]string, error) { - var followingActors []string - follow, err := GetActorCollection(actor.Following) - if err != nil { - return followingActors, err - } - - re := regexp.MustCompile("\\w+?$") - - for _, e := range follow.Items { - if re.FindString(e.Id) == name { - followingActors = append(followingActors, e.Id) - } - } - - return followingActors, nil -} diff --git a/webfinger/util.go b/webfinger/util.go index 0fc4948..fa8a625 100644 --- a/webfinger/util.go +++ b/webfinger/util.go @@ -7,6 +7,7 @@ import ( "strings" "github.com/FChannel0/FChannel-Server/activitypub" + "github.com/FChannel0/FChannel-Server/util" ) var Boards []Board @@ -38,6 +39,7 @@ func (a BoardSortAsc) Swap(i, j int) { a[i], a[j] = a[j], a[i] } func GetActorByNameFromBoardCollection(name string) activitypub.Actor { var actor activitypub.Actor + boards, _ := GetBoardCollection() for _, e := range boards { if e.Actor.Name == name { @@ -50,17 +52,21 @@ func GetActorByNameFromBoardCollection(name string) activitypub.Actor { func GetBoardCollection() ([]Board, error) { var collection []Board + for _, e := range FollowingBoards { var board Board + boardActor, err := activitypub.GetActorFromDB(e.Id) + if err != nil { - return collection, err + return collection, util.MakeError(err, "GetBoardCollection") } if boardActor.Id == "" { - boardActor, err = FingerActor(e.Id) + boardActor, err = activitypub.FingerActor(e.Id) + if err != nil { - return collection, err + return collection, util.MakeError(err, "GetBoardCollection") } } @@ -69,6 +75,7 @@ func GetBoardCollection() ([]Board, error) { board.Location = "/" + boardActor.Name board.Actor = boardActor board.Restricted = boardActor.Restricted + collection = append(collection, board) } @@ -78,12 +85,12 @@ func GetBoardCollection() ([]Board, error) { } func GetActorFromPath(location string, prefix string) (activitypub.Actor, error) { + var actor string + pattern := fmt.Sprintf("%s([^/\n]+)(/.+)?", prefix) re := regexp.MustCompile(pattern) match := re.FindStringSubmatch(location) - var actor string - if len(match) < 1 { actor = "/" } else { @@ -97,8 +104,9 @@ func GetActorFromPath(location string, prefix string) (activitypub.Actor, error) var nActor activitypub.Actor nActor, err := activitypub.GetActorByNameFromDB(actor) + if err != nil { - return nActor, err + return nActor, util.MakeError(err, "GetActorFromPath") } if nActor.Id == "" { @@ -107,3 +115,19 @@ func GetActorFromPath(location string, prefix string) (activitypub.Actor, error) return nActor, nil } + +func StartupArchive() error { + for _, e := range FollowingBoards { + actor, err := activitypub.GetActorFromDB(e.Id) + + if err != nil { + return util.MakeError(err, "StartupArchive") + } + + if err := actor.ArchivePosts(); err != nil { + return util.MakeError(err, "StartupArchive") + } + } + + return nil +} diff --git a/webfinger/webfinger.go b/webfinger/webfinger.go deleted file mode 100644 index 45650ea..0000000 --- a/webfinger/webfinger.go +++ /dev/null @@ -1,313 +0,0 @@ -package webfinger - -import ( - "encoding/json" - "io/ioutil" - "net/http" - "regexp" - "strings" - "time" - - "github.com/FChannel0/FChannel-Server/activitypub" - "github.com/FChannel0/FChannel-Server/config" - "github.com/FChannel0/FChannel-Server/util" -) - -type Webfinger struct { - Subject string `json:"subject,omitempty"` - Links []WebfingerLink `json:"links,omitempty"` -} - -type WebfingerLink struct { - Rel string `json:"rel,omitempty"` - Type string `json:"type,omitempty"` - Href string `json:"href,omitempty"` -} - -var ActorCache = make(map[string]activitypub.Actor) - -func GetActor(id string) (activitypub.Actor, error) { - var respActor activitypub.Actor - - if id == "" { - return respActor, nil - } - - actor, instance := activitypub.GetActorAndInstance(id) - - if ActorCache[actor+"@"+instance].Id != "" { - respActor = ActorCache[actor+"@"+instance] - return respActor, nil - } - - req, err := http.NewRequest("GET", strings.TrimSpace(id), nil) - if err != nil { - return respActor, err - } - - req.Header.Set("Accept", config.ActivityStreams) - - resp, err := util.RouteProxy(req) - - if err != nil { - return respActor, err - } - - defer resp.Body.Close() - - body, _ := ioutil.ReadAll(resp.Body) - - if err := json.Unmarshal(body, &respActor); err != nil { - return respActor, err - } - - ActorCache[actor+"@"+instance] = respActor - - return respActor, nil -} - -//looks for actor with pattern of board@instance -func FingerActor(path string) (activitypub.Actor, error) { - var nActor activitypub.Actor - - actor, instance := activitypub.GetActorAndInstance(path) - - if actor == "" && instance == "" { - return nActor, nil - } - - if ActorCache[actor+"@"+instance].Id != "" { - nActor = ActorCache[actor+"@"+instance] - } else { - r, _ := FingerRequest(actor, instance) - - if r != nil && r.StatusCode == 200 { - defer r.Body.Close() - - body, _ := ioutil.ReadAll(r.Body) - - json.Unmarshal(body, &nActor) - // if err := json.Unmarshal(body, &nActor); err != nil { - // return nActor, err - // } - - ActorCache[actor+"@"+instance] = nActor - } - } - - return nActor, nil -} - -func FingerRequest(actor string, instance string) (*http.Response, error) { - acct := "acct:" + actor + "@" + instance - - // TODO: respect https - req, _ := http.NewRequest("GET", "http://"+instance+"/.well-known/webfinger?resource="+acct, nil) - // if err != nil { - // return nil, err - // } - - resp, err := util.RouteProxy(req) - if err != nil { - return resp, nil - } - - var finger Webfinger - - if resp.StatusCode == 200 { - defer resp.Body.Close() - - body, _ := ioutil.ReadAll(resp.Body) - - json.Unmarshal(body, &finger) - // if err := json.Unmarshal(body, &finger); err != nil { - // return resp, err - // } - } - - if len(finger.Links) > 0 { - for _, e := range finger.Links { - if e.Type == "application/activity+json" { - req, _ := http.NewRequest("GET", e.Href, nil) - // if err != nil { - // return resp, err - // } - - req.Header.Set("Accept", config.ActivityStreams) - - resp, _ := util.RouteProxy(req) - return resp, nil - } - } - } - - return resp, nil -} - -func CheckValidActivity(id string) (activitypub.Collection, bool, error) { - var respCollection activitypub.Collection - - re := regexp.MustCompile(`.+\.onion(.+)?`) - if re.MatchString(id) { - id = strings.Replace(id, "https", "http", 1) - } - - req, err := http.NewRequest("GET", id, nil) - if err != nil { - return respCollection, false, err - } - - req.Header.Set("Accept", config.ActivityStreams) - - resp, err := util.RouteProxy(req) - if err != nil { - return respCollection, false, err - } - defer resp.Body.Close() - - body, _ := ioutil.ReadAll(resp.Body) - - if err := json.Unmarshal(body, &respCollection); err != nil { - return respCollection, false, err - } - - if respCollection.AtContext.Context == "https://www.w3.org/ns/activitystreams" && respCollection.OrderedItems[0].Id != "" { - return respCollection, true, nil - } - - return respCollection, false, nil -} - -func CreateActivity(activityType string, obj activitypub.ObjectBase) (activitypub.Activity, error) { - var newActivity activitypub.Activity - - actor, err := FingerActor(obj.Actor) - if err != nil { - return newActivity, err - } - - newActivity.AtContext.Context = "https://www.w3.org/ns/activitystreams" - newActivity.Type = activityType - newActivity.Published = obj.Published - newActivity.Actor = &actor - newActivity.Object = &obj - - for _, e := range obj.To { - if obj.Actor != e { - newActivity.To = append(newActivity.To, e) - } - } - - for _, e := range obj.Cc { - if obj.Actor != e { - newActivity.Cc = append(newActivity.Cc, e) - } - } - - return newActivity, nil -} - -func AddFollowersToActivity(activity activitypub.Activity) (activitypub.Activity, error) { - activity.To = append(activity.To, activity.Actor.Id) - - for _, e := range activity.To { - aFollowers, err := GetActorCollection(e + "/followers") - if err != nil { - return activity, err - } - - for _, k := range aFollowers.Items { - activity.To = append(activity.To, k.Id) - } - } - - var nActivity activitypub.Activity - - for _, e := range activity.To { - var alreadyTo = false - for _, k := range nActivity.To { - if e == k || e == activity.Actor.Id { - alreadyTo = true - } - } - - if !alreadyTo { - nActivity.To = append(nActivity.To, e) - } - } - - activity.To = nActivity.To - - return activity, nil -} - -func IsValidActor(id string) (activitypub.Actor, bool, error) { - actor, err := FingerActor(id) - return actor, actor.Id != "", err -} - -func AddInstanceToIndexDB(actor string) error { - // TODO: completely disabling this until it is actually reasonable to turn it on - // only actually allow this when it more or less works, i.e. can post, make threads, manage boards, etc - return nil - - //sleep to be sure the webserver is fully initialized - //before making finger request - time.Sleep(15 * time.Second) - - nActor, err := FingerActor(actor) - if err != nil { - return err - } - - if nActor.Id == "" { - return nil - } - - // TODO: maybe allow different indexes? - - obj := activitypub.ObjectBase{Id: "https://fchan.xyz/followers"} - followers, err := obj.GetCollection() - if err != nil { - return err - } - - var alreadyIndex = false - for _, e := range followers.Items { - if e.Id == nActor.Id { - alreadyIndex = true - } - } - - if !alreadyIndex { - actor := activitypub.Actor{Id: "https://fchan.xyz"} - return actor.AddFollower(nActor.Id) - } - - return nil -} - -func MakeActivityFollowingReq(w http.ResponseWriter, r *http.Request, activity activitypub.Activity) (bool, error) { - actor, err := GetActor(activity.Object.Id) - if err != nil { - return false, err - } - - req, err := http.NewRequest("POST", actor.Inbox, nil) - if err != nil { - return false, err - } - - resp, err := util.RouteProxy(req) - if err != nil { - return false, err - } - defer resp.Body.Close() - - body, _ := ioutil.ReadAll(resp.Body) - - var respActivity activitypub.Activity - - err = json.Unmarshal(body, &respActivity) - return respActivity.Type == "Accept", err -} -- cgit v1.2.3