2020-08-17 15:33:53 -04:00
|
|
|
package display
|
|
|
|
|
|
|
|
import (
|
2021-05-14 21:00:44 -04:00
|
|
|
"bytes"
|
2020-08-17 15:33:53 -04:00
|
|
|
"fmt"
|
2020-11-17 20:56:15 -05:00
|
|
|
"net/url"
|
|
|
|
"path"
|
2020-12-06 20:57:57 -05:00
|
|
|
"sort"
|
2020-11-17 20:56:15 -05:00
|
|
|
"strconv"
|
2020-08-17 17:36:50 -04:00
|
|
|
"time"
|
2020-08-17 15:33:53 -04:00
|
|
|
|
2021-02-17 14:17:13 -05:00
|
|
|
"github.com/gdamore/tcell/v2"
|
2020-08-28 19:33:37 -04:00
|
|
|
"github.com/makeworld-the-better-one/amfora/cache"
|
2020-11-17 20:56:15 -05:00
|
|
|
"github.com/makeworld-the-better-one/amfora/config"
|
2020-08-17 15:33:53 -04:00
|
|
|
"github.com/makeworld-the-better-one/amfora/structs"
|
2020-11-27 17:01:29 -05:00
|
|
|
"github.com/makeworld-the-better-one/amfora/subscriptions"
|
2020-12-05 20:35:15 -05:00
|
|
|
"github.com/makeworld-the-better-one/go-gemini"
|
2020-11-17 20:56:15 -05:00
|
|
|
"github.com/mmcdole/gofeed"
|
|
|
|
"github.com/spf13/viper"
|
2020-08-17 15:33:53 -04:00
|
|
|
)
|
|
|
|
|
2020-12-06 20:57:57 -05:00
|
|
|
// Map page number (zero-indexed) to the time it was made at.
|
|
|
|
// This allows for caching the pages until there's an update.
|
|
|
|
var subscriptionPageUpdated = make(map[int]time.Time)
|
2020-08-28 19:33:37 -04:00
|
|
|
|
2020-08-29 20:51:51 -04:00
|
|
|
// toLocalDay truncates the provided time to a date only,
|
|
|
|
// but converts to the local time first.
|
|
|
|
func toLocalDay(t time.Time) time.Time {
|
|
|
|
t = t.Local()
|
|
|
|
return time.Date(t.Year(), t.Month(), t.Day(), 0, 0, 0, 0, t.Location())
|
|
|
|
}
|
|
|
|
|
2020-12-05 20:35:15 -05:00
|
|
|
// Subscriptions displays the subscriptions page on the current tab.
|
2020-12-06 20:57:57 -05:00
|
|
|
func Subscriptions(t *tab, u string) string {
|
|
|
|
pageN := 0 // Pages are zero-indexed internally
|
|
|
|
|
|
|
|
// Correct URL if query string exists
|
|
|
|
// The only valid query string is an int above 1.
|
|
|
|
// Anything "redirects" to the first page, with no query string.
|
|
|
|
// This is done over just serving the first page content for
|
|
|
|
// invalid query strings so that there won't be duplicate caches.
|
|
|
|
correctURL := func(u2 string) string {
|
|
|
|
if len(u2) > 20 && u2[:20] == "about:subscriptions?" {
|
|
|
|
query, err := gemini.QueryUnescape(u2[20:])
|
|
|
|
if err != nil {
|
|
|
|
return "about:subscriptions"
|
|
|
|
}
|
|
|
|
// Valid query string
|
|
|
|
i, err := strconv.Atoi(query)
|
|
|
|
if err != nil {
|
|
|
|
// Not an int
|
|
|
|
return "about:subscriptions"
|
|
|
|
}
|
|
|
|
if i < 2 {
|
|
|
|
return "about:subscriptions"
|
|
|
|
}
|
|
|
|
// Valid int above 1
|
|
|
|
pageN = i - 1 // Pages are zero-indexed internally
|
|
|
|
return u2
|
|
|
|
}
|
|
|
|
return u2
|
|
|
|
}
|
|
|
|
u = correctURL(u)
|
|
|
|
|
2020-11-17 11:59:06 -05:00
|
|
|
// Retrieve cached version if there hasn't been any updates
|
2020-12-06 20:57:57 -05:00
|
|
|
p, ok := cache.GetPage(u)
|
|
|
|
if subscriptionPageUpdated[pageN].After(subscriptions.LastUpdated) && ok {
|
2021-06-25 19:03:41 -04:00
|
|
|
t.setPage(p)
|
2020-08-28 19:33:37 -04:00
|
|
|
t.applyBottomBar()
|
2020-12-06 20:57:57 -05:00
|
|
|
return u
|
2020-08-28 19:33:37 -04:00
|
|
|
}
|
2020-08-17 15:33:53 -04:00
|
|
|
|
2020-11-27 17:01:29 -05:00
|
|
|
pe := subscriptions.GetPageEntries()
|
2020-08-28 19:33:37 -04:00
|
|
|
|
2020-12-06 20:57:57 -05:00
|
|
|
// Figure out where the entries for this page start, if at all.
|
|
|
|
epp := viper.GetInt("subscriptions.entries_per_page")
|
|
|
|
if epp <= 0 {
|
|
|
|
epp = 1
|
|
|
|
}
|
|
|
|
start := pageN * epp // Index of the first page entry to be displayed
|
|
|
|
end := start + epp
|
|
|
|
if end > len(pe.Entries) {
|
|
|
|
end = len(pe.Entries)
|
|
|
|
}
|
|
|
|
|
|
|
|
var rawPage string
|
|
|
|
if pageN == 0 {
|
|
|
|
rawPage = "# Subscriptions\n\n" + rawPage
|
|
|
|
} else {
|
|
|
|
rawPage = fmt.Sprintf("# Subscriptions (page %d)\n\n", pageN+1) + rawPage
|
|
|
|
}
|
2020-08-17 15:33:53 -04:00
|
|
|
|
2020-12-06 20:57:57 -05:00
|
|
|
if start > len(pe.Entries)-1 && len(pe.Entries) != 0 {
|
|
|
|
// The page is out of range, doesn't exist
|
|
|
|
rawPage += "This page does not exist.\n\n=> about:subscriptions Subscriptions\n"
|
|
|
|
} else {
|
|
|
|
// Render page
|
|
|
|
|
|
|
|
rawPage += "You can use Ctrl-X to subscribe to a page, or to an Atom/RSS/JSON feed. See the online wiki for more.\n" +
|
|
|
|
"If you just opened Amfora then updates may appear incrementally. Reload the page to see them.\n\n" +
|
|
|
|
"=> about:manage-subscriptions Manage subscriptions\n\n"
|
|
|
|
|
|
|
|
// curDay represents what day of posts the loop is on.
|
|
|
|
// It only goes backwards in time.
|
|
|
|
// Its initial setting means:
|
|
|
|
// Only display posts older than 26 hours in the future, nothing further in the future.
|
|
|
|
//
|
|
|
|
// 26 hours was chosen because it is the largest timezone difference
|
|
|
|
// currently in the world. Posts may be dated in the future
|
|
|
|
// due to software bugs, where the local user's date is used, but
|
|
|
|
// the UTC timezone is specified. Gemfeed does this at the time of
|
|
|
|
// writing, but will not after #3 gets merged on its repo. Still,
|
|
|
|
// the older version will be used for a while.
|
|
|
|
curDay := toLocalDay(time.Now()).Add(26 * time.Hour)
|
|
|
|
|
|
|
|
for _, entry := range pe.Entries[start:end] { // From new to old
|
|
|
|
// Convert to local time, remove sub-day info
|
|
|
|
pub := toLocalDay(entry.Published)
|
|
|
|
|
|
|
|
if pub.Before(curDay) {
|
|
|
|
// This post is on a new day, add a day header
|
|
|
|
curDay = pub
|
|
|
|
rawPage += fmt.Sprintf("\n## %s\n\n", curDay.Format("Jan 02, 2006"))
|
|
|
|
}
|
|
|
|
if entry.Title == "" || entry.Title == "/" {
|
|
|
|
// Just put author/title
|
|
|
|
// Mainly used for when you're tracking the root domain of a site
|
|
|
|
rawPage += fmt.Sprintf("=>%s %s\n", entry.URL, entry.Prefix)
|
|
|
|
} else {
|
|
|
|
// Include title and dash
|
|
|
|
rawPage += fmt.Sprintf("=>%s %s - %s\n", entry.URL, entry.Prefix, entry.Title)
|
|
|
|
}
|
2020-08-17 15:33:53 -04:00
|
|
|
}
|
2020-12-06 20:57:57 -05:00
|
|
|
|
|
|
|
if pageN == 0 && len(pe.Entries) > epp {
|
|
|
|
// First page, and there's more than can fit
|
|
|
|
rawPage += "\n\n=> about:subscriptions?2 Next Page\n"
|
|
|
|
} else if pageN > 0 {
|
|
|
|
// A later page
|
|
|
|
rawPage += fmt.Sprintf(
|
|
|
|
"\n\n=> about:subscriptions?%d Previous Page\n",
|
|
|
|
pageN, // pageN is zero-indexed but the query string is one-indexed
|
|
|
|
)
|
2020-12-07 18:09:10 -05:00
|
|
|
if end != len(pe.Entries) {
|
2020-12-06 20:57:57 -05:00
|
|
|
// There's more
|
|
|
|
rawPage += fmt.Sprintf("=> about:subscriptions?%d Next Page\n", pageN+2)
|
|
|
|
}
|
2020-11-19 11:34:10 -05:00
|
|
|
}
|
2020-08-17 15:33:53 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
page := structs.Page{
|
2021-05-14 21:00:44 -04:00
|
|
|
Raw: stringToBytes(rawPage),
|
2021-02-27 21:18:03 -05:00
|
|
|
URL: u,
|
|
|
|
TermWidth: termW,
|
|
|
|
Mediatype: structs.TextGemini,
|
2020-08-17 15:33:53 -04:00
|
|
|
}
|
2020-08-29 20:51:51 -04:00
|
|
|
go cache.AddPage(&page)
|
2021-06-25 19:03:41 -04:00
|
|
|
t.setPage(&page)
|
2020-08-17 15:33:53 -04:00
|
|
|
t.applyBottomBar()
|
2020-08-28 19:33:37 -04:00
|
|
|
|
2020-12-06 20:57:57 -05:00
|
|
|
subscriptionPageUpdated[pageN] = time.Now()
|
2020-11-18 16:10:22 -05:00
|
|
|
|
2020-12-06 20:57:57 -05:00
|
|
|
return u
|
2020-08-28 19:33:37 -04:00
|
|
|
}
|
|
|
|
|
2020-12-05 20:35:15 -05:00
|
|
|
// ManageSubscriptions displays the subscription managing page in
|
|
|
|
// the current tab. `u` is the URL entered by the user.
|
|
|
|
func ManageSubscriptions(t *tab, u string) {
|
|
|
|
if len(u) > 27 && u[:27] == "about:manage-subscriptions?" {
|
|
|
|
// There's a query string, aka a URL to unsubscribe from
|
|
|
|
manageSubscriptionQuery(t, u)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
rawPage := "# Manage Subscriptions\n\n" +
|
2020-12-06 20:57:57 -05:00
|
|
|
"Below is list of URLs you are subscribed to, both feeds and pages. " +
|
|
|
|
"Navigate to the link to unsubscribe from that feed or page.\n\n"
|
|
|
|
|
|
|
|
urls := subscriptions.AllURLS()
|
|
|
|
sort.Strings(urls)
|
2020-12-05 20:35:15 -05:00
|
|
|
|
2020-12-06 20:57:57 -05:00
|
|
|
for _, u2 := range urls {
|
2020-12-05 20:35:15 -05:00
|
|
|
rawPage += fmt.Sprintf(
|
|
|
|
"=>%s %s\n",
|
|
|
|
"about:manage-subscriptions?"+gemini.QueryEscape(u2),
|
|
|
|
u2,
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
page := structs.Page{
|
2021-05-14 21:00:44 -04:00
|
|
|
Raw: stringToBytes(rawPage),
|
2021-02-27 21:18:03 -05:00
|
|
|
URL: "about:manage-subscriptions",
|
|
|
|
TermWidth: termW,
|
|
|
|
Mediatype: structs.TextGemini,
|
2020-12-05 20:35:15 -05:00
|
|
|
}
|
|
|
|
go cache.AddPage(&page)
|
2021-06-25 19:03:41 -04:00
|
|
|
t.setPage(&page)
|
2020-12-05 20:35:15 -05:00
|
|
|
t.applyBottomBar()
|
|
|
|
}
|
|
|
|
|
|
|
|
func manageSubscriptionQuery(t *tab, u string) {
|
|
|
|
sub, err := gemini.QueryUnescape(u[27:])
|
|
|
|
if err != nil {
|
|
|
|
Error("URL Error", "Invalid query string: "+err.Error())
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
err = subscriptions.Remove(sub)
|
|
|
|
if err != nil {
|
|
|
|
ManageSubscriptions(t, "about:manage-subscriptions") // Reload
|
|
|
|
Error("Save Error", "Error saving the unsubscription to disk: "+err.Error())
|
|
|
|
return
|
|
|
|
}
|
|
|
|
ManageSubscriptions(t, "about:manage-subscriptions") // Reload
|
|
|
|
Info("Unsubscribed from " + sub)
|
|
|
|
}
|
|
|
|
|
2020-11-27 17:01:29 -05:00
|
|
|
// openSubscriptionModal displays the "Add subscription" modal
|
|
|
|
// It returns whether the user wanted to subscribe to feed/page.
|
|
|
|
// The subscribed arg specifies whether this feed/page is already
|
|
|
|
// subscribed to.
|
|
|
|
func openSubscriptionModal(validFeed, subscribed bool) bool {
|
2020-11-17 20:56:15 -05:00
|
|
|
// Reuses yesNoModal
|
|
|
|
|
|
|
|
if viper.GetBool("a-general.color") {
|
2021-02-17 14:17:13 -05:00
|
|
|
m := yesNoModal
|
|
|
|
m.SetBackgroundColor(config.GetColor("subscription_modal_bg"))
|
|
|
|
m.SetTextColor(config.GetColor("subscription_modal_text"))
|
|
|
|
frame := yesNoModal.GetFrame()
|
|
|
|
frame.SetBorderColor(config.GetColor("subscription_modal_text"))
|
|
|
|
frame.SetTitleColor(config.GetColor("subscription_modal_text"))
|
2020-11-17 20:56:15 -05:00
|
|
|
} else {
|
2021-02-17 14:17:13 -05:00
|
|
|
m := yesNoModal
|
|
|
|
m.SetBackgroundColor(tcell.ColorBlack)
|
|
|
|
m.SetTextColor(tcell.ColorWhite)
|
|
|
|
frame := yesNoModal.GetFrame()
|
|
|
|
frame.SetBorderColor(tcell.ColorWhite)
|
|
|
|
frame.SetTitleColor(tcell.ColorWhite)
|
2020-11-17 20:56:15 -05:00
|
|
|
}
|
|
|
|
if validFeed {
|
2020-11-27 17:01:29 -05:00
|
|
|
yesNoModal.GetFrame().SetTitle("Feed Subscription")
|
|
|
|
if subscribed {
|
|
|
|
yesNoModal.SetText("You are already subscribed to this feed. Would you like to manually update it?")
|
2020-11-17 20:56:15 -05:00
|
|
|
} else {
|
2020-11-27 17:01:29 -05:00
|
|
|
yesNoModal.SetText("Would you like to subscribe to this feed?")
|
2020-11-17 20:56:15 -05:00
|
|
|
}
|
|
|
|
} else {
|
2020-11-27 17:01:29 -05:00
|
|
|
yesNoModal.GetFrame().SetTitle("Page Subscription")
|
|
|
|
if subscribed {
|
|
|
|
yesNoModal.SetText("You are already subscribed to this page. Would you like to manually update it?")
|
2020-11-17 20:56:15 -05:00
|
|
|
} else {
|
2020-11-27 17:01:29 -05:00
|
|
|
yesNoModal.SetText("Would you like to subscribe to this page?")
|
2020-11-17 20:56:15 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-02-17 14:17:13 -05:00
|
|
|
panels.ShowPanel("yesno")
|
|
|
|
panels.SendToFront("yesno")
|
2020-11-17 20:56:15 -05:00
|
|
|
App.SetFocus(yesNoModal)
|
|
|
|
App.Draw()
|
|
|
|
|
|
|
|
resp := <-yesNoCh
|
2021-02-17 14:17:13 -05:00
|
|
|
panels.HidePanel("yesno")
|
2020-11-17 20:56:15 -05:00
|
|
|
App.SetFocus(tabs[curTab].view)
|
|
|
|
App.Draw()
|
|
|
|
return resp
|
|
|
|
}
|
|
|
|
|
2020-11-27 17:01:29 -05:00
|
|
|
// getFeedFromPage is like subscriptions.GetFeed but takes a structs.Page as input.
|
2020-11-17 20:56:15 -05:00
|
|
|
func getFeedFromPage(p *structs.Page) (*gofeed.Feed, bool) {
|
|
|
|
parsed, _ := url.Parse(p.URL)
|
|
|
|
filename := path.Base(parsed.Path)
|
2021-05-14 21:00:44 -04:00
|
|
|
r := bytes.NewReader(p.Raw)
|
2020-11-27 17:01:29 -05:00
|
|
|
return subscriptions.GetFeed(p.RawMediatype, filename, r)
|
2020-11-17 20:56:15 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
// addFeedDirect is only for adding feeds, not pages.
|
|
|
|
// It's for when you already have a feed and know if it's tracked.
|
2020-11-19 21:14:19 -05:00
|
|
|
// Used mainly by handleURL because it already did a lot of the work.
|
|
|
|
// It returns a bool indicating whether the user actually wanted to
|
|
|
|
// add the feed or not.
|
2020-11-17 20:56:15 -05:00
|
|
|
//
|
|
|
|
// Like addFeed, it should be called in a goroutine.
|
2020-11-19 21:14:19 -05:00
|
|
|
func addFeedDirect(u string, feed *gofeed.Feed, tracked bool) bool {
|
2020-11-27 17:01:29 -05:00
|
|
|
if openSubscriptionModal(true, tracked) {
|
|
|
|
err := subscriptions.AddFeed(u, feed)
|
2020-11-17 20:56:15 -05:00
|
|
|
if err != nil {
|
|
|
|
Error("Feed Error", err.Error())
|
|
|
|
}
|
2020-11-19 21:14:19 -05:00
|
|
|
return true
|
2020-11-17 20:56:15 -05:00
|
|
|
}
|
2020-11-19 21:14:19 -05:00
|
|
|
return false
|
2020-11-17 20:56:15 -05:00
|
|
|
}
|
|
|
|
|
2020-11-27 17:01:29 -05:00
|
|
|
// addFeed goes through the process of subscribing to the current page/feed.
|
2020-11-17 20:56:15 -05:00
|
|
|
// It is the high-level way of doing it. It should be called in a goroutine.
|
2020-11-27 17:01:29 -05:00
|
|
|
func addSubscription() {
|
2020-11-17 20:56:15 -05:00
|
|
|
t := tabs[curTab]
|
|
|
|
p := t.page
|
|
|
|
|
2021-05-13 16:38:53 -04:00
|
|
|
if !t.hasContent() || t.isAnAboutPage() {
|
2020-11-17 20:56:15 -05:00
|
|
|
// It's an about: page, or a malformed one
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
feed, isFeed := getFeedFromPage(p)
|
2020-11-27 17:01:29 -05:00
|
|
|
tracked := subscriptions.IsSubscribed(p.URL)
|
2020-11-17 20:56:15 -05:00
|
|
|
|
2020-11-27 17:01:29 -05:00
|
|
|
if openSubscriptionModal(isFeed, tracked) {
|
2020-11-17 20:56:15 -05:00
|
|
|
var err error
|
|
|
|
|
|
|
|
if isFeed {
|
2020-11-27 17:01:29 -05:00
|
|
|
err = subscriptions.AddFeed(p.URL, feed)
|
2020-11-17 20:56:15 -05:00
|
|
|
} else {
|
2021-05-14 21:00:44 -04:00
|
|
|
err = subscriptions.AddPage(p.URL, bytes.NewReader(p.Raw))
|
2020-11-17 20:56:15 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
Error("Feed/Page Error", err.Error())
|
|
|
|
}
|
|
|
|
}
|
2020-08-17 15:33:53 -04:00
|
|
|
}
|