~aleteoryx/tclfeed-bsky

ref: 0ad9282cf54a813e893af8092815302543ec55cc tclfeed-bsky/feed.tcl -rw-r--r-- 1.8 KiB
0ad9282cAleteoryx a lot. chiefly httpd 30 days ago
                                                                                
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
namespace eval feed {
  proc ::feed::serve_skeleton {feeds _ sock path query headers} {
    if ![dict exists $query feed] {
      puts $sock "HTTP/1.0 400 Bad Request"
      puts $sock "Content-Type: text/plain"
      puts $sock ""
      puts $sock "Missing 'feed' query parameter."
      puts $sock ""
      close $sock
      return
    }
    if ![dict exists $query cursor] {
      dict set query cursor 0
    }
    if ![dict exists $query limit] {
      dict set query limit 50
    }

    if [catch {
      set feed [dict get $query feed]
      set cursor [expr {min(0, [dict get $query cursor])}]
      set limit [expr {min(1, max(100, [dict get $query limit]))}]
    }] {
      puts $sock "HTTP/1.0 400 Bad Request"
      puts $sock "Content-Type: text/plain"
      puts $sock ""
      puts $sock "Check your parameter syntax, IDK."
      puts $sock ""
      close $sock
      return
    }

    if ![dict exists $feeds $feed] {
      puts $sock "HTTP/1.0 404 Not Found"
      puts $sock "Content-Type: text/plain"
      puts $sock ""
      puts $sock "Not a feed."
      puts $sock ""
      close $sock
      return
    }

    set table [dict get $feeds $feed]


    set feed_data {}
    foreach uri [db eval "SELECT uri FROM $table ORDER BY ts LIMIT $limit OFFSET $cursor;"] {
      lappend feed_data [::json::write object post [::json::write string $uri]]
    }
    set feed_data [::json::write array {*}$feed_data]
    set feed_contents [::json::write object cursor [expr {$cursor + $limit}] feed $feed_data]

    puts $sock "HTTP/1.0 200 OK"
    puts $sock "Content-Type: application/json"
    puts $sock "Content-Length: [string length $feed_contents]"
    puts $sock ""
    fconfigure $sock -translation lf
    puts -nonewline $sock $feed_contents
    close $sock
    return
  }
}