aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--db/cache.go6
-rw-r--r--db/database.go3
-rw-r--r--db/follow.go5
-rw-r--r--main.go96
-rw-r--r--webfinger/comm.go70
-rw-r--r--webfinger/webfinger.go35
6 files changed, 116 insertions, 99 deletions
diff --git a/db/cache.go b/db/cache.go
index 0831f02..6f1ee8f 100644
--- a/db/cache.go
+++ b/db/cache.go
@@ -290,7 +290,11 @@ func WriteActorToCache(actorID string) error {
if err != nil {
return err
}
- collection := GetActorCollection(actor.Outbox)
+
+ collection, err := webfinger.GetActorCollection(actor.Outbox)
+ if err != nil {
+ return err
+ }
for _, e := range collection.OrderedItems {
if _, err := WriteActorObjectToCache(e); err != nil {
diff --git a/db/database.go b/db/database.go
index 8bb7568..559188b 100644
--- a/db/database.go
+++ b/db/database.go
@@ -14,6 +14,7 @@ import (
"github.com/FChannel0/FChannel-Server/activitypub"
"github.com/FChannel0/FChannel-Server/config"
"github.com/FChannel0/FChannel-Server/util"
+ "github.com/FChannel0/FChannel-Server/webfinger"
_ "github.com/lib/pq"
)
@@ -2316,7 +2317,7 @@ func GetObjectTypeDB(id string) (string, error) {
}
func IsReplyInThread(inReplyTo string, id string) (bool, error) {
- obj, _, err := CheckValidActivity(inReplyTo)
+ obj, _, err := webfinger.CheckValidActivity(inReplyTo)
if err != nil {
return false, err
}
diff --git a/db/follow.go b/db/follow.go
index 386de2b..0919e30 100644
--- a/db/follow.go
+++ b/db/follow.go
@@ -262,7 +262,10 @@ func SetActorFollowingDB(activity activitypub.Activity) (activitypub.Activity, e
return activity, err
}
- remoteActorFollowerCol := GetCollectionFromReq(actor.Followers)
+ remoteActorFollowerCol, err := webfinger.GetCollectionFromReq(actor.Followers)
+ if err != nil {
+ return activity, err
+ }
for _, e := range following {
if e.Id == activity.Actor.Id {
diff --git a/main.go b/main.go
index c056f38..b659fd4 100644
--- a/main.go
+++ b/main.go
@@ -525,81 +525,6 @@ func ParseCommentForReplies(db *sql.DB, comment string, op string) []ObjectBase
return validLinks
}
-func CheckValidActivity(id string) (Collection, bool) {
- var respCollection 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 {
- fmt.Println("error with request")
- }
-
- req.Header.Set("Accept", activitystreams)
-
- resp, err := RouteProxy(req)
-
- if err != nil {
- fmt.Println("error with response")
- return respCollection, false
- }
-
- defer resp.Body.Close()
-
- body, _ := ioutil.ReadAll(resp.Body)
-
- err = json.Unmarshal(body, &respCollection)
-
- if err != nil {
- panic(err)
- }
-
- if respCollection.AtContext.Context == "https://www.w3.org/ns/activitystreams" && respCollection.OrderedItems[0].Id != "" {
- return respCollection, true
- }
-
- return respCollection, false
-}
-
-func GetActorCollection(collection string) activitypub.Collection {
- var nCollection activitypub.Collection
-
- if collection == "" {
- return nCollection
- }
-
- req, err := http.NewRequest("GET", collection, nil)
-
- CheckError(err, "error with getting actor collection req "+collection)
-
- req.Header.Set("Accept", activitystreams)
-
- resp, err := RouteProxy(req)
-
- if err != nil {
- fmt.Println("error with getting actor collection resp " + collection)
- return nCollection
- }
-
- defer resp.Body.Close()
-
- if resp.StatusCode == 200 {
- body, _ := ioutil.ReadAll(resp.Body)
-
- if len(body) > 0 {
- err = json.Unmarshal(body, &nCollection)
-
- CheckError(err, "error getting actor collection from body "+collection)
- }
- }
-
- return nCollection
-}
-
func IsValidActor(id string) (Actor, bool) {
actor := FingerActor(id)
@@ -1100,27 +1025,6 @@ func AddInstanceToIndexDB(db *sql.DB, actor string) {
}
}
-func GetCollectionFromReq(path string) Collection {
- req, err := http.NewRequest("GET", path, nil)
- CheckError(err, "error with getting collection from req")
-
- req.Header.Set("Accept", activitystreams)
-
- resp, err := RouteProxy(req)
-
- CheckError(err, "error getting resp from collection req")
-
- defer resp.Body.Close()
-
- body, _ := ioutil.ReadAll(resp.Body)
-
- var respCollection Collection
-
- _ = json.Unmarshal(body, &respCollection)
-
- return respCollection
-}
-
func HashMedia(media string) string {
h := sha256.New()
h.Write([]byte(media))
diff --git a/webfinger/comm.go b/webfinger/comm.go
new file mode 100644
index 0000000..dd25cb4
--- /dev/null
+++ b/webfinger/comm.go
@@ -0,0 +1,70 @@
+package webfinger
+
+import (
+ "encoding/json"
+ "errors"
+ "io/ioutil"
+ "net/http"
+
+ "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
+}
diff --git a/webfinger/webfinger.go b/webfinger/webfinger.go
index ffe7c6d..9e8d67d 100644
--- a/webfinger/webfinger.go
+++ b/webfinger/webfinger.go
@@ -4,6 +4,7 @@ import (
"encoding/json"
"io/ioutil"
"net/http"
+ "regexp"
"strings"
"github.com/FChannel0/FChannel-Server/activitypub"
@@ -143,3 +144,37 @@ func FingerRequest(actor string, instance string) (*http.Response, error) {
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
+}