From 1892327cee2c3fa1d3bea729bd08eb63c2189a96 Mon Sep 17 00:00:00 2001 From: FChannel <> Date: Sat, 30 Apr 2022 11:00:55 -0700 Subject: restructured code base to prevent circular dependicies --- util/accept.go | 21 --------------- util/util.go | 80 ++++++++++++++++++++++------------------------------------ 2 files changed, 30 insertions(+), 71 deletions(-) delete mode 100644 util/accept.go (limited to 'util') diff --git a/util/accept.go b/util/accept.go deleted file mode 100644 index 2765c32..0000000 --- a/util/accept.go +++ /dev/null @@ -1,21 +0,0 @@ -package util - -import ( - "regexp" - "strings" -) - -// False positive for application/ld+ld, application/activity+ld, application/json+json -var activityRegexp = regexp.MustCompile("application\\/(ld|json|activity)((\\+(ld|json))|$)") - -func AcceptActivity(header string) bool { - accept := false - if strings.Contains(header, ";") { - split := strings.Split(header, ";") - accept = accept || activityRegexp.MatchString(split[0]) - accept = accept || strings.Contains(split[len(split)-1], "profile=\"https://www.w3.org/ns/activitystreams\"") - } else { - accept = accept || activityRegexp.MatchString(header) - } - return accept -} diff --git a/util/util.go b/util/util.go index 1feb53a..381db94 100644 --- a/util/util.go +++ b/util/util.go @@ -7,6 +7,8 @@ import ( "os" "regexp" "strings" + + "github.com/FChannel0/FChannel-Server/config" ) func IsOnion(url string) bool { @@ -18,56 +20,6 @@ func IsOnion(url string) bool { return false } -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 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 StripTransferProtocol(value string) string { re := regexp.MustCompile("(http://|https://)?(www.)?") @@ -245,3 +197,31 @@ func EscapeString(text string) string { text = strings.Replace(text, "<", "<", -1) return text } + +func CreateUniqueID(actor string) (string, error) { + var newID string + isUnique := false + for !isUnique { + newID = RandomID(8) + + query := "select id from activitystream where id=$1" + args := fmt.Sprintf("%s/%s/%s", config.Domain, actor, newID) + rows, err := config.DB.Query(query, args) + if err != nil { + return "", err + } + + defer rows.Close() + + // reusing a variable here + // if we encounter a match, it'll get set to false causing the outer for loop to loop and to go through this all over again + // however if nothing is there, it'll remain true and exit the loop + isUnique = true + for rows.Next() { + isUnique = false + break + } + } + + return newID, nil +} -- cgit v1.2.3