aboutsummaryrefslogtreecommitdiff
path: root/db
diff options
context:
space:
mode:
Diffstat (limited to 'db')
-rw-r--r--db/boards.go64
-rw-r--r--db/redis.go68
-rw-r--r--db/report.go138
3 files changed, 270 insertions, 0 deletions
diff --git a/db/boards.go b/db/boards.go
new file mode 100644
index 0000000..b54ab60
--- /dev/null
+++ b/db/boards.go
@@ -0,0 +1,64 @@
+package db
+
+import (
+ "sort"
+
+ "github.com/FChannel0/FChannel-Server/activitypub"
+ "github.com/FChannel0/FChannel-Server/webfinger"
+)
+
+var Boards []Board
+var FollowingBoards []activitypub.ObjectBase
+
+type Board struct {
+ Name string
+ Actor activitypub.Actor
+ Summary string
+ PrefName string
+ InReplyTo string
+ Location string
+ To string
+ RedirectTo string
+ Captcha string
+ CaptchaCode string
+ ModCred string
+ Domain string
+ TP string
+ Restricted bool
+ Post activitypub.ObjectBase
+}
+
+type BoardSortAsc []Board
+
+func (a BoardSortAsc) Len() int { return len(a) }
+func (a BoardSortAsc) Less(i, j int) bool { return a[i].Name < a[j].Name }
+func (a BoardSortAsc) Swap(i, j int) { a[i], a[j] = a[j], a[i] }
+
+func GetBoardCollection() ([]Board, error) {
+ var collection []Board
+ for _, e := range FollowingBoards {
+ var board Board
+ boardActor, err := GetActorFromDB(e.Id)
+ if err != nil {
+ return collection, err
+ }
+
+ if boardActor.Id == "" {
+ boardActor, err = webfinger.FingerActor(e.Id)
+ if err != nil {
+ return collection, err
+ }
+ }
+
+ board.Name = boardActor.Name
+ board.PrefName = boardActor.PreferredUsername
+ board.Location = "/" + boardActor.Name
+ board.Actor = boardActor
+ board.Restricted = boardActor.Restricted
+ collection = append(collection, board)
+ }
+
+ sort.Sort(BoardSortAsc(collection))
+
+ return collection, nil
+}
diff --git a/db/redis.go b/db/redis.go
new file mode 100644
index 0000000..873ca27
--- /dev/null
+++ b/db/redis.go
@@ -0,0 +1,68 @@
+package db
+
+import (
+ "bufio"
+ "fmt"
+ "net/http"
+ "os"
+
+ "github.com/FChannel0/FChannel-Server/config"
+ "github.com/gomodule/redigo/redis"
+)
+
+var Cache redis.Conn
+
+func InitCache() error {
+ conn, err := redis.DialURL(config.Redis)
+ Cache = conn
+ return err
+}
+
+func CloseCache() error {
+ return Cache.Close()
+}
+
+func CheckSession(w http.ResponseWriter, r *http.Request) (interface{}, error) {
+ c, err := r.Cookie("session_token")
+
+ if err != nil {
+ if err == http.ErrNoCookie {
+ w.WriteHeader(http.StatusUnauthorized)
+ return nil, err
+ }
+
+ w.WriteHeader(http.StatusBadRequest)
+ return nil, err
+ }
+
+ sessionToken := c.Value
+
+ response, err := Cache.Do("GET", sessionToken)
+
+ if err != nil {
+ w.WriteHeader(http.StatusInternalServerError)
+ return nil, err
+ }
+ if response == nil {
+ w.WriteHeader(http.StatusUnauthorized)
+ return nil, err
+ }
+
+ return response, nil
+}
+
+func GetClientKey() (string, error) {
+ file, err := os.Open("clientkey")
+ if err != nil {
+ return "", err
+ }
+ defer file.Close()
+
+ scanner := bufio.NewScanner(file)
+ var line string
+ for scanner.Scan() {
+ line = fmt.Sprintf("%s", scanner.Text())
+ }
+
+ return line, nil
+}
diff --git a/db/report.go b/db/report.go
index 4fcb806..a64c94f 100644
--- a/db/report.go
+++ b/db/report.go
@@ -1,5 +1,17 @@
package db
+type Report struct {
+ ID string
+ Count int
+ Reason string
+}
+
+type Removed struct {
+ ID string
+ Type string
+ Board string
+}
+
func DeleteReportActivity(id string) error {
query := `delete from reported where id=$1`
@@ -54,3 +66,129 @@ func ReportActivity(id string, reason string) (bool, error) {
return true, nil
}
+
+func CreateLocalDeleteDB(id string, _type string) error {
+ query := `select id from removed where id=$1`
+
+ rows, err := db.Query(query, id)
+ if err != nil {
+ return err
+ }
+ defer rows.Close()
+
+ if rows.Next() {
+ var i string
+
+ if err := rows.Scan(&i); err != nil {
+ return err
+ }
+
+ if i != "" {
+ query := `update removed set type=$1 where id=$2`
+
+ if _, err := db.Exec(query, _type, id); err != nil {
+ return err
+ }
+ }
+ } else {
+ query := `insert into removed (id, type) values ($1, $2)`
+
+ if _, err := db.Exec(query, id, _type); err != nil {
+ return err
+ }
+ }
+
+ return nil
+}
+
+func GetLocalDeleteDB() ([]Removed, error) {
+ var deleted []Removed
+
+ query := `select id, type from removed`
+
+ rows, err := db.Query(query)
+ if err != nil {
+ return deleted, err
+ }
+
+ defer rows.Close()
+
+ for rows.Next() {
+ var r Removed
+
+ if err := rows.Scan(&r.ID, &r.Type); err != nil {
+ return deleted, err
+ }
+
+ deleted = append(deleted, r)
+ }
+
+ return deleted, nil
+}
+
+func CreateLocalReportDB(id string, board string, reason string) error {
+ query := `select id, count from reported where id=$1 and board=$2`
+
+ rows, err := db.Query(query, id, board)
+ if err != nil {
+ return err
+ }
+ defer rows.Close()
+
+ if rows.Next() {
+ var i string
+ var count int
+
+ if err := rows.Scan(&i, &count); err != nil {
+ return err
+ }
+
+ if i != "" {
+ count = count + 1
+ query := `update reported set count=$1 where id=$2`
+
+ if _, err := db.Exec(query, count, id); err != nil {
+ return err
+ }
+ }
+ } else {
+ query := `insert into reported (id, count, board, reason) values ($1, $2, $3, $4)`
+
+ if _, err := db.Exec(query, id, 1, board, reason); err != nil {
+ return err
+ }
+ }
+
+ return nil
+}
+
+func GetLocalReportDB(board string) ([]Report, error) {
+ var reported []Report
+
+ query := `select id, count, reason from reported where board=$1`
+
+ rows, err := db.Query(query, board)
+ if err != nil {
+ return reported, err
+ }
+ defer rows.Close()
+
+ for rows.Next() {
+ var r Report
+
+ if err := rows.Scan(&r.ID, &r.Count, &r.Reason); err != nil {
+ return reported, err
+ }
+
+ reported = append(reported, r)
+ }
+
+ return reported, nil
+}
+
+func CloseLocalReportDB(id string, board string) error {
+ query := `delete from reported where id=$1 and board=$2`
+
+ _, err := db.Exec(query, id, board)
+ return err
+}