diff options
author | KushBlazingJudah <59340248+KushBlazingJudah@users.noreply.github.com> | 2021-11-07 00:32:39 -0300 |
---|---|---|
committer | FChannel <> | 2022-06-19 12:53:29 -0700 |
commit | 972223c992ca5aa5e5d93cff3b2ee4e30182025b (patch) | |
tree | ff61695c734852aeafdc0e872cc6f47085a8e787 /db | |
parent | bc9051fd1a17e793647cf309c973a7feefebd98f (diff) |
restructuring part 5
Diffstat (limited to 'db')
-rw-r--r-- | db/actor.go | 51 | ||||
-rw-r--r-- | db/database.go | 149 |
2 files changed, 199 insertions, 1 deletions
diff --git a/db/actor.go b/db/actor.go new file mode 100644 index 0000000..51c8f41 --- /dev/null +++ b/db/actor.go @@ -0,0 +1,51 @@ +package db + +import ( + "fmt" + "regexp" + "strings" + + "github.com/FChannel0/FChannel-Server/activitypub" +) + +func GetActorFromPath(location string, prefix string) (activitypub.Actor, error) { + pattern := fmt.Sprintf("%s([^/\n]+)(/.+)?", prefix) + re := regexp.MustCompile(pattern) + match := re.FindStringSubmatch(location) + + var actor string + + if len(match) < 1 { + actor = "/" + } else { + actor = strings.Replace(match[1], "/", "", -1) + } + + if actor == "/" || actor == "outbox" || actor == "inbox" || actor == "following" || actor == "followers" { + actor = "main" + } + + var nActor activitypub.Actor + + nActor, err := GetActorByNameFromDB(actor) + if err != nil { + return nActor, err + } + + if nActor.Id == "" { + nActor = GetActorByName(actor) + } + + return nActor, nil +} + +func GetActorByName(name string) activitypub.Actor { + var actor activitypub.Actor + for _, e := range Boards { + if e.Actor.Name == name { + actor = e.Actor + } + } + + return actor +} diff --git a/db/database.go b/db/database.go index 559188b..bd302aa 100644 --- a/db/database.go +++ b/db/database.go @@ -1975,7 +1975,7 @@ func GetNewsFromDB(limit int) ([]NewsItem, error) { return news, nil } -func getNewsItemFromDB(timestamp int) (NewsItem, error) { +func GetNewsItemFromDB(timestamp int) (NewsItem, error) { var news NewsItem var content string query := `select title, content, time from newsItem where time=$1 limit 1` @@ -2330,3 +2330,150 @@ func IsReplyInThread(inReplyTo string, id string) (bool, error) { return false, nil } + +func GetActorsFollowPostFromId(actors []string, id string) (activitypub.Collection, error) { + var collection activitypub.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 IsReplyToOP(op string, link string) (string, bool, error) { + if op == link { + return link, true, nil + } + + re := regexp.MustCompile(`f(\w+)\-`) + match := re.FindStringSubmatch(link) + + if len(match) > 0 { + re := regexp.MustCompile(`(.+)\-`) + link = re.ReplaceAllString(link, "") + link = "%" + match[1] + "/" + link + } + + query := `select id from replies where id like $1 and inreplyto=$2` + + rows, err := db.Query(query, link, op) + if err != nil { + return op, false, err + } + defer rows.Close() + + var id string + rows.Next() + if err := rows.Scan(&id); err != nil { + return id, false, err + } + + if id != "" { + return id, true, nil + } + + return "", false, nil +} + +func GetReplyOP(link string) (string, error) { + query := `select id from replies where id in (select inreplyto from replies where id=$1) and inreplyto=''` + + rows, err := db.Query(query, link) + if err != nil { + return "", err + } + defer rows.Close() + + var id string + + rows.Next() + err = rows.Scan(&id) + return id, err +} + +func StartupArchive() error { + for _, e := range FollowingBoards { + actor, err := GetActorFromDB(e.Id) + if err != nil { + return err + } + + if err := ArchivePosts(actor); err != nil { + return err + } + } + + return nil +} + +func CheckInactive() { + for true { + CheckInactiveInstances() + time.Sleep(24 * time.Hour) + } +} + +func CheckInactiveInstances() (map[string]string, error) { + instances := make(map[string]string) + query := `select following from following` + + rows, err := db.Query(query) + if err != nil { + return instances, err + } + defer rows.Close() + + for rows.Next() { + var instance string + if err := rows.Scan(&instance); err != nil { + return instances, err + } + + instances[instance] = instance + } + + query = `select follower from follower` + rows, err = db.Query(query) + if err != nil { + return instances, err + } + defer rows.Close() + + for rows.Next() { + var instance string + if err := rows.Scan(&instance); err != nil { + return instances, err + } + + instances[instance] = instance + } + + re := regexp.MustCompile(config.Domain + `(.+)?`) + for _, e := range instances { + actor, err := webfinger.GetActor(e) + if err != nil { + return instances, err + } + + if actor.Id == "" && !re.MatchString(e) { + if err := AddInstanceToInactiveDB(e); err != nil { + return instances, err + } + } else { + if err := DeleteInstanceFromInactiveDB(e); err != nil { + return instances, err + } + } + } + + return instances, nil +} |