pgxn / pgxn-manager

Interface for managing extensions on PGXN
http://pgxn.org
47 stars 18 forks source link

Add pgxn_consumer with Mastodon & Twitter handlers #74

Closed theory closed 1 year ago

theory commented 1 year ago

Rip out the Twitter notification in the upload controller, which was always a hack; uploads will now be faster without that overhead. Replace it with a trigger on the distributions table that calls pg_notify('pgxn_release', NEW.meta). This will send it to any Postgres clients listening on the pgxn_release channel.

Implement PGXN::Manager::Consumer to be that listener. It loads any number of event handlers from the configuration file, where the old twitter key has been replaced by a consumers key, now an array of objects with configurations for handlers. The "type" key corresponds to a sub-package of PGXN::Manager::Consumer, which must exist for it to work, while the "events" key names the events for the handler to handle (currently just release).

The consumer then connects to the database, listens for all event channels of interest (currently just pgxn_release), and polls on an interval for new messages (5 seconds by default). The connection is resilient: if it loses the connection, it will simply log the error and keep trying every five seconds.

PGXN::Manager::Consumer::twitter is largely the same as what was in the controller, but now with tests.

PGXN::Manager::Consumer::mastodon is new, and needs a server name and access token, as well as an optional post delay, to do its thing It's a dead simple HTTP Post, so just uses LWP::UserAgent to make the call. This class also has new formatting for the message, including the abstract and a random assortment of Emoji.

The trigger calls a new function, pgxn_notify(), which currently just dispatches to pg_notify(), but could be extended in the future to, say, write to a table so that the consumer can later read from the table if, for some reason, it goes down and misses notifications. Maybe that won't ever be much of a problem, since the manager and consumer should always run together, but it was simple enough to create the new function so that triggers wouldn't have to be changed in the future.

While at it, change the UX text so it no longer mentions posting to Twitter. In the future we'll have to add a field for Mastodon and perhaps other likely outlets. Also fix email sending so that Unicode text is properly encoded as UTF-8.