Skip to content

Commit

Permalink
feat(pastes): set custom expiration and set paste to unlisted (#52)
Browse files Browse the repository at this point in the history
  • Loading branch information
neurosnap authored Nov 12, 2023
1 parent 36b9540 commit f13ddd4
Show file tree
Hide file tree
Showing 5 changed files with 82 additions and 45 deletions.
7 changes: 7 additions & 0 deletions pastes/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ type PostPageData struct {
Contents template.HTML
PublishAtISO string
PublishAt string
ExpiresAt string
}

type TransparencyPageData struct {
Expand Down Expand Up @@ -185,6 +186,10 @@ func postHandler(w http.ResponseWriter, r *http.Request) {
if err != nil {
logger.Error(err)
}
expiresAt := "never"
if post.ExpiresAt != nil {
expiresAt = post.ExpiresAt.Format("02 Jan, 2006")
}

data = PostPageData{
Site: *cfg.GetSiteData(),
Expand All @@ -199,6 +204,7 @@ func postHandler(w http.ResponseWriter, r *http.Request) {
Username: username,
BlogName: blogName,
Contents: template.HTML(parsedText),
ExpiresAt: expiresAt,
}
} else {
logger.Infof("post not found %s/%s", username, slug)
Expand All @@ -213,6 +219,7 @@ func postHandler(w http.ResponseWriter, r *http.Request) {
Username: username,
BlogName: blogName,
Contents: "oops! we can't seem to find this post.",
ExpiresAt: "",
}
}

Expand Down
33 changes: 31 additions & 2 deletions pastes/html/help.page.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -109,8 +109,8 @@ scp ./delete.txt {{.Site.Domain}}:/</pre>

<section id="pipe">
<h2 class="text-xl">
<a href="#pipe" rel="nofollow noopener">#</a>
Can I pipe my paste?
<a href="#pipe" rel="nofollow noopener">#</a>
Can I pipe my paste?
</h2>
<p>
Yes!
Expand All @@ -120,6 +120,35 @@ scp ./delete.txt {{.Site.Domain}}:/</pre>
<pre># if the tty warning annoys you
echo "foobar" | ssh -T pastes.sh</pre>
</section>

<section id="expires">
<h2 class="text-xl">
<a href="#expires" rel="nofollow noopener">#</a>
Can I set the expiration date to a paste?
</h2>
<p>
Yes. The default expiration date for a paste is 90 days.
We do allow the user to set the paste to never expire.
We also allow custom <a href="https://pkg.go.dev/time#ParseDuration">duration</a>
or <a href="https://pkg.go.dev/github.com/araddon/dateparse#ParseStrict">timestamp</a>.
</p>
<pre>echo "foobar" | ssh pastes.sh FILENAME expires=false</pre>
<pre>echo "foobar" | ssh pastes.sh FILENAME expires=2023-12-12</pre>
<pre>echo "foobar" | ssh pastes.sh FILENAME expires=1h</pre>
</section>

<section id="unlisted">
<h2 class="text-xl">
<a href="#unlisted" rel="nofollow noopener">#</a>
Can I hide pastes from my landing page?
</h2>
<p>
Yes. Unlisted in this context means it does not show up on
your user landing page where we show all of your pastes.
In this case, yes, you can "hide" it using a pipe command.
</p>
<pre>echo "foobar" | ssh pastes.sh FILENAME hidden=true</pre>
</section>
</main>
{{template "marketing-footer" .}}
{{end}}
6 changes: 4 additions & 2 deletions pastes/html/marketing.page.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,9 @@ echo "foobar" | ssh -T pastes.sh</pre>
<section>
<h2 class="text-lg font-bold">Features</h2>
<ul>
<li>Pastes last 90 days</li>
<li>Pastes last <strong>90 days</strong> by default</li>
<li><a href="/help#expires">Ability to set custom expiration</a></li>
<li><a href="/help#unlisted">Ability to "hide" pastes</a></li>
<li>Bring your own editor</li>
<li>Terminal workflow with no installation</li>
<li>Public-key based authentication</li>
Expand All @@ -106,7 +108,7 @@ echo "foobar" | ssh -T pastes.sh</pre>
<section>
<h2 class="text-lg font-bold">Roadmap</h2>
<ol>
<li>Ability to customize expiration</li>
<li>Mobile support?</li>
</ol>
</section>
</main>
Expand Down
1 change: 1 addition & 0 deletions pastes/html/post.page.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
<span> | </span>
<a href="{{.RawURL}}">raw</a>
</p>
<p class="font-bold m-0">expires: {{.ExpiresAt}}</p>
</header>
<main>
<article>
Expand Down
80 changes: 39 additions & 41 deletions pastes/scp_hooks.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,57 +43,55 @@ func (p *FileHooks) FileMeta(s ssh.Session, data *filehandlers.PostMetaData) err
data.ExpiresAt = &expiresAt
}

if p.Db.HasFeatureForUser(data.User.ID, "pastes-advanced") {
var hidden bool
var expiresFound bool
var expires *time.Time
var hidden bool
var expiresFound bool
var expires *time.Time

cmd := s.Command()
cmd := s.Command()

for _, arg := range cmd {
if strings.Contains(arg, "=") {
splitArg := strings.Split(arg, "=")
if len(splitArg) != 2 {
for _, arg := range cmd {
if strings.Contains(arg, "=") {
splitArg := strings.Split(arg, "=")
if len(splitArg) != 2 {
continue
}

switch splitArg[0] {
case "hidden":
val, err := strconv.ParseBool(splitArg[1])
if err != nil {
continue
}

hidden = val
case "expires":
val, err := strconv.ParseBool(splitArg[1])
if err == nil {
expiresFound = !val
continue
}

switch splitArg[0] {
case "hidden":
val, err := strconv.ParseBool(splitArg[1])
if err != nil {
continue
}

hidden = val
case "expires":
val, err := strconv.ParseBool(splitArg[1])
if err == nil {
expiresFound = !val
continue
}

duration, err := time.ParseDuration(splitArg[1])
if err == nil {
expiresFound = true
expireTime := time.Now().Add(duration)
expires = &expireTime
continue
}

expireTime, err := dateparse.ParseStrict(splitArg[1])
if err == nil {
expiresFound = true
expires = &expireTime
}
duration, err := time.ParseDuration(splitArg[1])
if err == nil {
expiresFound = true
expireTime := time.Now().Add(duration)
expires = &expireTime
continue
}

expireTime, err := dateparse.ParseStrict(splitArg[1])
if err == nil {
expiresFound = true
expires = &expireTime
}
}
}
}

data.Hidden = hidden
data.Hidden = hidden

if expiresFound {
data.ExpiresAt = expires
}
if expiresFound {
data.ExpiresAt = expires
}

return nil
Expand Down

0 comments on commit f13ddd4

Please sign in to comment.