From 4e3848cadbb3fd89b94a7ef24838173939d198db Mon Sep 17 00:00:00 2001 From: knotteye Date: Thu, 1 Jul 2021 17:56:04 -0500 Subject: Add a list of local boards and server news --- database.go | 42 ++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 40 insertions(+), 2 deletions(-) (limited to 'database.go') diff --git a/database.go b/database.go index 9d5d721..5fcebf0 100644 --- a/database.go +++ b/database.go @@ -7,11 +7,12 @@ import "time" import "os" import "strings" import "sort" +import "container/list" func GetActorFromDB(db *sql.DB, id string) Actor { - var nActor Actor + var nActor Actor - query :=`select type, id, name, preferedusername, inbox, outbox, following, followers, restricted, summary, publickeypem from actor where id=$1` + query :=`select type, id, name, preferedusername, inbox, outbox, following, followers, restricted, summary, publickeypem from actor where id=$1` rows, err := db.Query(query, id) @@ -1488,3 +1489,40 @@ func MarkObjectSensitive(db *sql.DB, id string, sensitive bool) { CheckError(err, "error updating sensitive object in cacheactivitystream") } + +func getNewsFromDB(db *sql.DB) []NewsItem { + news := list.New() + query :=`select title, content, time from newsItem order by time desc` + + rows, err := db.Query(query) + + if CheckError(err, "could not get news from db query") != nil { + return make([]NewsItem, 0) + } + + defer rows.Close() + for rows.Next() { + n := NewsItem{} + err = rows.Scan(&n.Title, &n.Content, &n.Time) + news.PushBack(n) + } + + anews := make([]NewsItem, news.Len()) + + i:=0 + for e := news.Front(); e != nil; e = e.Next() { + assert := e.Value.(NewsItem) + anews[i] = assert + i++ + } + + return anews +} + +func WriteNewsToDB(db *sql.DB, news NewsItem) { + query := `insert into newsItem (title, content, time) values ($1, $2, $3)` + + _, err := db.Exec(query, news.Title, news.Content, time.Now().Unix()) + + CheckError(err, "error writing news item") +} -- cgit v1.2.3 From 168225daa21fe494bcd74ab6c90d6498ecf9f1e3 Mon Sep 17 00:00:00 2001 From: knotteye Date: Thu, 1 Jul 2021 20:26:49 -0500 Subject: Make news list links to full page news articles --- database.go | 24 ++++++++++++++++++++++++ 1 file changed, 24 insertions(+) (limited to 'database.go') diff --git a/database.go b/database.go index 5fcebf0..037fde2 100644 --- a/database.go +++ b/database.go @@ -1504,6 +1504,9 @@ func getNewsFromDB(db *sql.DB) []NewsItem { for rows.Next() { n := NewsItem{} err = rows.Scan(&n.Title, &n.Content, &n.Time) + if CheckError(err, "error scanning news from db") != nil { + return make([]NewsItem, 0) + } news.PushBack(n) } @@ -1519,6 +1522,27 @@ func getNewsFromDB(db *sql.DB) []NewsItem { return anews } +func getNewsItemFromDB(db *sql.DB, timestamp int) (NewsItem, error) { + var news NewsItem + query := `select title, content, time from newsItem where time=$1 limit 1` + + rows, err := db.Query(query, timestamp) + + if err != nil { + return news, err + } + + defer rows.Close() + rows.Next() + err = rows.Scan(&news.Title, &news.Content, &news.Time) + + if err != nil { + return news, err + } + + return news, nil +} + func WriteNewsToDB(db *sql.DB, news NewsItem) { query := `insert into newsItem (title, content, time) values ($1, $2, $3)` -- cgit v1.2.3 From ad5f4e9ff7e7ab0abab4a964cbf1626d0cdc3365 Mon Sep 17 00:00:00 2001 From: knotteye Date: Fri, 2 Jul 2021 13:56:38 -0500 Subject: select a specific number of news items from the database --- database.go | 32 ++++++++++++++------------------ 1 file changed, 14 insertions(+), 18 deletions(-) (limited to 'database.go') diff --git a/database.go b/database.go index 037fde2..7dc7f5c 100644 --- a/database.go +++ b/database.go @@ -7,7 +7,6 @@ import "time" import "os" import "strings" import "sort" -import "container/list" func GetActorFromDB(db *sql.DB, id string) Actor { var nActor Actor @@ -1490,14 +1489,20 @@ func MarkObjectSensitive(db *sql.DB, id string, sensitive bool) { CheckError(err, "error updating sensitive object in cacheactivitystream") } -func getNewsFromDB(db *sql.DB) []NewsItem { - news := list.New() - query :=`select title, content, time from newsItem order by time desc` +//if limit less than 1 return all news items +func getNewsFromDB(db *sql.DB, limit int) []NewsItem { + news := []NewsItem + + if(limit > 0) { + query :=`select title, content, time from newsItem order by time desc limit $1` + } else { + query :=`select title, content, time from newsItem order by time desc` + } - rows, err := db.Query(query) + rows, err := db.Query(query, limit) if CheckError(err, "could not get news from db query") != nil { - return make([]NewsItem, 0) + return news } defer rows.Close() @@ -1505,21 +1510,12 @@ func getNewsFromDB(db *sql.DB) []NewsItem { n := NewsItem{} err = rows.Scan(&n.Title, &n.Content, &n.Time) if CheckError(err, "error scanning news from db") != nil { - return make([]NewsItem, 0) + return news } - news.PushBack(n) + append(news, n) } - anews := make([]NewsItem, news.Len()) - - i:=0 - for e := news.Front(); e != nil; e = e.Next() { - assert := e.Value.(NewsItem) - anews[i] = assert - i++ - } - - return anews + return news } func getNewsItemFromDB(db *sql.DB, timestamp int) (NewsItem, error) { -- cgit v1.2.3 From faae38726dd804e3246514d88f93794c23a0cdb2 Mon Sep 17 00:00:00 2001 From: knotteye Date: Fri, 2 Jul 2021 14:20:16 -0500 Subject: show only the last 3 news items on index, link to a page with all news items --- database.go | 18 +++++++++++++----- 1 file changed, 13 insertions(+), 5 deletions(-) (limited to 'database.go') diff --git a/database.go b/database.go index 7dc7f5c..731f160 100644 --- a/database.go +++ b/database.go @@ -1491,15 +1491,23 @@ func MarkObjectSensitive(db *sql.DB, id string, sensitive bool) { //if limit less than 1 return all news items func getNewsFromDB(db *sql.DB, limit int) []NewsItem { - news := []NewsItem + var news []NewsItem + var query string if(limit > 0) { - query :=`select title, content, time from newsItem order by time desc limit $1` + query =`select title, content, time from newsItem order by time desc limit $1` } else { - query :=`select title, content, time from newsItem order by time desc` + query =`select title, content, time from newsItem order by time desc` } - rows, err := db.Query(query, limit) + var rows *sql.Rows + var err error + if(limit > 0) { + rows, err = db.Query(query, limit) + } else { + rows, err = db.Query(query) + } + if CheckError(err, "could not get news from db query") != nil { return news @@ -1512,7 +1520,7 @@ func getNewsFromDB(db *sql.DB, limit int) []NewsItem { if CheckError(err, "error scanning news from db") != nil { return news } - append(news, n) + news = append(news, n) } return news -- cgit v1.2.3 From fab8de7187571a4f3f8a30966057d661a858b645 Mon Sep 17 00:00:00 2001 From: knotteye Date: Fri, 2 Jul 2021 15:10:49 -0500 Subject: add deletion of news items --- database.go | 5 +++++ 1 file changed, 5 insertions(+) (limited to 'database.go') diff --git a/database.go b/database.go index 731f160..6a871ba 100644 --- a/database.go +++ b/database.go @@ -1547,6 +1547,11 @@ func getNewsItemFromDB(db *sql.DB, timestamp int) (NewsItem, error) { return news, nil } +func deleteNewsItemFromDB(db *sql.DB, timestamp int) { + query := `delete from newsItem where time=$1` + db.Exec(query, timestamp) +} + func WriteNewsToDB(db *sql.DB, news NewsItem) { query := `insert into newsItem (title, content, time) values ($1, $2, $3)` -- cgit v1.2.3 From d9cce000fdc8d804e73ff2fa4bfd2f1a151d90d9 Mon Sep 17 00:00:00 2001 From: sageman_ Date: Sat, 3 Jul 2021 20:43:41 +0200 Subject: In case the PEM key is missing, do not fail too hard. Tell the administrator in a friendly matter that they are an idiot and they should seek assistance in generating a new keypair and telling everyone about it. --- database.go | 46 ++++++++++++++++++++++++++++++++++++++-------- 1 file changed, 38 insertions(+), 8 deletions(-) (limited to 'database.go') diff --git a/database.go b/database.go index 9d5d721..70b7572 100644 --- a/database.go +++ b/database.go @@ -1,12 +1,15 @@ package main -import "fmt" -import "database/sql" -import _ "github.com/lib/pq" -import "time" -import "os" -import "strings" -import "sort" +import ( + "database/sql" + "fmt" + "os" + "sort" + "strings" + "time" + + _ "github.com/lib/pq" +) func GetActorFromDB(db *sql.DB, id string) Actor { var nActor Actor @@ -27,6 +30,10 @@ func GetActorFromDB(db *sql.DB, id string) Actor { } nActor.PublicKey = GetActorPemFromDB(db, publicKeyPem) + if nActor.PublicKey.PublicKeyPem == ""{ + err = CreatePublicKeyFromPrivate(db, &nActor, publicKeyPem) + CheckError(err, "error creating public key from private") + } return nActor } @@ -50,6 +57,10 @@ func GetActorByNameFromDB(db *sql.DB, name string) Actor { } nActor.PublicKey = GetActorPemFromDB(db, publicKeyPem) + if nActor.PublicKey.PublicKeyPem == ""{ + err = CreatePublicKeyFromPrivate(db, &nActor, publicKeyPem) + CheckError(err, "error creating public key from private") + } return nActor } @@ -1470,13 +1481,32 @@ func GetActorPemFromDB(db *sql.DB, pemID string) PublicKeyPem { defer rows.Close() rows.Next() rows.Scan(&pem.Id, &pem.Owner, &pem.PublicKeyPem) - f, _ := os.ReadFile(pem.PublicKeyPem) + f, err := os.ReadFile(pem.PublicKeyPem) + if err != nil{ + pem.PublicKeyPem = "" + return pem + } pem.PublicKeyPem = strings.ReplaceAll(string(f), "\r\n", `\n`) return pem } +func GetActorPemFileFromDB(db *sql.DB, pemID string) string{ + query := `select file from publickeypem where id=$1` + rows, err := db.Query(query, pemID) + + CheckError(err, "could not get public key filename from database") + + var file string + + defer rows.Close() + rows.Next() + rows.Scan(&file) + + return file +} + func MarkObjectSensitive(db *sql.DB, id string, sensitive bool) { var query = `update activitystream set sensitive=$1 where id=$2` _, err := db.Exec(query, sensitive, id) -- cgit v1.2.3 From cf86acee94652ac7cd8e8f313e6abf6c9901c398 Mon Sep 17 00:00:00 2001 From: FChannel <> Date: Sat, 3 Jul 2021 18:39:34 -0700 Subject: add some styling to news pull request --- database.go | 14 ++++++++++++-- 1 file changed, 12 insertions(+), 2 deletions(-) (limited to 'database.go') diff --git a/database.go b/database.go index baae16a..edab4f6 100644 --- a/database.go +++ b/database.go @@ -7,6 +7,7 @@ import ( "sort" "strings" "time" + "html/template" _ "github.com/lib/pq" ) @@ -1546,10 +1547,15 @@ func getNewsFromDB(db *sql.DB, limit int) []NewsItem { defer rows.Close() for rows.Next() { n := NewsItem{} - err = rows.Scan(&n.Title, &n.Content, &n.Time) + var content string + err = rows.Scan(&n.Title, &content, &n.Time) if CheckError(err, "error scanning news from db") != nil { return news } + + content = strings.ReplaceAll(content, "\n", "
") + n.Content = template.HTML(content) + news = append(news, n) } @@ -1558,6 +1564,7 @@ func getNewsFromDB(db *sql.DB, limit int) []NewsItem { func getNewsItemFromDB(db *sql.DB, timestamp int) (NewsItem, error) { var news NewsItem + var content string query := `select title, content, time from newsItem where time=$1 limit 1` rows, err := db.Query(query, timestamp) @@ -1568,11 +1575,14 @@ func getNewsItemFromDB(db *sql.DB, timestamp int) (NewsItem, error) { defer rows.Close() rows.Next() - err = rows.Scan(&news.Title, &news.Content, &news.Time) + err = rows.Scan(&news.Title, &content, &news.Time) if err != nil { return news, err } + + content = strings.ReplaceAll(content, "\n", "
") + news.Content = template.HTML(content) return news, nil } -- cgit v1.2.3 From 02d55188670a599048486c36e896416d917e831c Mon Sep 17 00:00:00 2001 From: FChannel <> Date: Sat, 3 Jul 2021 19:18:29 -0700 Subject: fixed error with CreatePublicKeyFromPrivate with empty actor --- database.go | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) (limited to 'database.go') diff --git a/database.go b/database.go index edab4f6..954ec0f 100644 --- a/database.go +++ b/database.go @@ -31,7 +31,7 @@ func GetActorFromDB(db *sql.DB, id string) Actor { } nActor.PublicKey = GetActorPemFromDB(db, publicKeyPem) - if nActor.PublicKey.PublicKeyPem == ""{ + if nActor.Id != "" && nActor.PublicKey.PublicKeyPem == ""{ err = CreatePublicKeyFromPrivate(db, &nActor, publicKeyPem) CheckError(err, "error creating public key from private") } @@ -57,8 +57,7 @@ func GetActorByNameFromDB(db *sql.DB, name string) Actor { CheckError(err, "error with actor from db scan ") } - nActor.PublicKey = GetActorPemFromDB(db, publicKeyPem) - if nActor.PublicKey.PublicKeyPem == ""{ + if nActor.Id != "" && nActor.PublicKey.PublicKeyPem == ""{ err = CreatePublicKeyFromPrivate(db, &nActor, publicKeyPem) CheckError(err, "error creating public key from private") } -- cgit v1.2.3 From 68f3c361656ae343fd809239452a954424a0bcb2 Mon Sep 17 00:00:00 2001 From: FChannel <> Date: Sat, 3 Jul 2021 19:58:22 -0700 Subject: added report reason to admin manage page --- database.go | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) (limited to 'database.go') diff --git a/database.go b/database.go index 954ec0f..5668a4d 100644 --- a/database.go +++ b/database.go @@ -1451,7 +1451,7 @@ func GetActorReportedTotal(db *sql.DB, id string) int { func GetActorReportedDB(db *sql.DB, id string) []ObjectBase { var nObj []ObjectBase - query := `select id, count from reported where board=$1` + query := `select id, count, reason from reported where board=$1` rows, err := db.Query(query, id) @@ -1462,7 +1462,7 @@ func GetActorReportedDB(db *sql.DB, id string) []ObjectBase { for rows.Next() { var obj ObjectBase - rows.Scan(&obj.Id, &obj.Size) + rows.Scan(&obj.Id, &obj.Size, &obj.Content) nObj = append(nObj, obj) } -- cgit v1.2.3