support LÖVE v12

In LÖVE v12 you no longer need to run it through the unfurl.lua shim,
and you can now open new threads by pasting their links into the app
using `ctrl+v`.
This commit is contained in:
Kartik K. Agaram 2023-10-15 12:52:12 -07:00
parent d1c0eb3afd
commit 9ac993eb58
12 changed files with 125 additions and 22 deletions

View File

@ -1,15 +1,28 @@
on.initialize = function(arg)
if #arg == 0 then
error('Please pass in a file containing a Mastodon thread, and optionally an id within it to start out focused on.\n\nThis app currently needs to be invoked from a terminal.')
end
Input_filename = arg[1]
A()
Cursor_node = Root
if #arg >= 2 then
local initial_id = arg[2]
if Nodes[initial_id] then
Cursor_node = Nodes[initial_id]
if Major_version >= 12 then
-- LÖVE has https
-- commandline arg is a url
if #arg > 0 then
Url = arg[1]
A()
end
else
-- LÖVE pre-v12 doesn't have https
-- we depend on an external shim to download
-- commandline arg is a filename
if #arg == 0 then
-- no ability to load new URL
error('When running with LÖVE v11, please pass in a file containing a Mastodon thread, and optionally an id within it to start out focused on.\n\nThis app currently needs to be invoked from a terminal.')
end
Input_filename = arg[1]
A()
Cursor_node = Root
if #arg >= 2 then
local initial_id = arg[2]
if Nodes[initial_id] then
Cursor_node = Nodes[initial_id]
end
end
ensure_cursor_node_within_viewport()
end
ensure_cursor_node_within_viewport()
end

View File

@ -11,6 +11,22 @@ on.keychord_press = function(chord, key)
-- reset zoom
Viewport.zoom = 1.0
B()
elseif chord == 'C-v' then
-- load new URL
if Major_version >= 12 then -- requires LÖVE with https
-- ignore stuff in the clipboard that doesn't look like a url
local cb = App.get_clipboard()
if #cb > 300 then
print('clipboard: '..cb:sub(1,300)..'...')
else
print('clipboard: '..cb)
end
if cb:match('^%s*https://[^%s]*%s*$') then
print('clipboard contains a URL')
Url = trim(cb)
A()
end
end
elseif chord == 'C-up' then
if Cursor_node.in_reply_to_id then
Cursor_node = Nodes[Cursor_node.in_reply_to_id]
@ -46,4 +62,4 @@ on.keychord_press = function(chord, key)
end
end
end
end
end

33
0028-A
View File

@ -1,11 +1,28 @@
A = function()
if Input_filename then
love.graphics.setFont(love.graphics.newFont(scale(20))) -- editor objects implicitly depend on current font
local f = io.open(Input_filename)
assert(f)
local thread_data = json.decode(f:read('*a'))
f:close()
render_thread_to_surface(thread_data)
if Major_version >= 12 then
-- reload Url from network
if Url then
local initial_ml = ml_from_url(Url)
local thread_data = try_load_nodes_from_url(initial_ml)
render_thread_to_surface(thread_data)
Cursor_node = Root
if Nodes[initial_ml.id] then
Cursor_node = Nodes[initial_ml.id]
end
ensure_cursor_node_within_viewport()
end
B()
else
-- LÖVE pre-v12 has no https
-- reload same thread from file
if Input_filename then
love.graphics.setFont(love.graphics.newFont(scale(20))) -- editor objects implicitly depend on current font
local f = io.open(Input_filename)
assert(f)
local thread_data = json.decode(f:read('*a'))
f:close()
render_thread_to_surface(thread_data)
end
B()
end
B()
end

View File

@ -1 +1,4 @@
Input_filename = nil
-- file containing the thread to render
-- used only in LÖVE pre-v12 which didn't have https
-- only ever initialized once; pre-v12 LÖVE can't load new threads
Input_filename = nil

13
0048-get_toot Normal file
View File

@ -0,0 +1,13 @@
get_toot = function(ml)
local url = url_from_ml(ml)
local code, response, response_headers = https.request(url)
local result = json.decode(response)
code, response, response_headers = https.request(url..'/context')
local rels = json.decode(response)
result.host = ml.host
result.user = ml.user
result.id = ml.id
result.ancestors = rels.ancestors
result.descendants = rels.descendants
return result
end

View File

@ -0,0 +1,8 @@
try_load_nodes_from_url = function(initial_ml)
local result = get_toot(initial_ml)
if result.ancestors and #result.ancestors > 0 then
-- redo with oldest ancestor
result = get_toot(ml_from_url(result.ancestors[1].url))
end
return result
end

1
0050-Url Normal file
View File

@ -0,0 +1 @@
Url = nil

6
0051-ml_from_url Normal file
View File

@ -0,0 +1,6 @@
ml_from_url = function(url)
local host, user, id = url:match('^([^/]*://[^/]*)/@([^/]*)/(%d*)$')
if host then return {host=host, user=user, id=id} end
local host, user, id = url:match('^([^/]*://[^/]*)/users/([^/]*)/statuses/(%d*)$')
if host then return {host=host, user=user, id=id} end
end

3
0052-url_from_ml Normal file
View File

@ -0,0 +1,3 @@
url_from_ml = function(ml)
return ('%s/api/v1/statuses/%s'):format(ml.host, ml.id)
end

View File

@ -22,7 +22,7 @@ Pros:
This repo is an example of a [Freewheeling App](http://akkartik.name/freewheeling),
designed above all to be easy to run, easy to modify and easy to share.
## Getting started
## Getting started (LÖVE v11)
Install the [Lua](https://www.lua.org) programming language. Just a 200KB
download, open source and with a stellar reputation. I'll assume below that
@ -52,6 +52,23 @@ LÖVE.
Click on a toot to copy its URL to the clipboard.
## Getting started (LÖVE v12 pre-release)
Install LÖVE from a nightly build.
* Go to https://github.com/love2d/love/actions
* Click on the most recent successful build (with a green check mark before it)
* Scroll down to the "Artifacts" section
* Download the artifact for your OS
* If you don't have a Github account, these won't be clickable.
Copy the URL for the build and paste it into https://nightly.link to get
clickable links to artifacts.
Run LÖVE from the commandline and pass in this directory. Optionally also pass
in a link to a Mastodon toot.
Copy a link to any mastodon toot and paste it into the app window using
`ctrl+v` to view it in the context of its thread.
## Hacking
To modify the code for the app without restarting the app each time, download

View File

@ -1,5 +1,8 @@
utf8 = require 'utf8'
json = require 'json'
if love.getVersion() >= 12 then
https = require 'https'
end
require 'app'
require 'test'

View File

@ -1,3 +1,6 @@
-- Helper for LÖVE pre-v12.
-- Download a thread before running the LÖVE app on the results.
json = require 'json'
https = require 'ssl.https'