Skip to content

Commit

Permalink
feat: autofill contributor fields via email / uri lookup
Browse files Browse the repository at this point in the history
  • Loading branch information
saerdnaer committed Jun 6, 2021
1 parent ec60ce0 commit 503feef
Show file tree
Hide file tree
Showing 2 changed files with 117 additions and 4 deletions.
9 changes: 9 additions & 0 deletions includes/scripts_and_styles.php
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,15 @@
'jquery', 'jquery-ui-sortable', 'jquery-ui-datepicker',
], $version);

wp_localize_script(
'podlove_admin',
'podlove_vue',
[
'rest_url' => esc_url_raw(rest_url()),
'nonce' => wp_create_nonce('wp_rest')
]
);

wp_enqueue_style('jquery-ui-style', \Podlove\PLUGIN_URL.'/js/admin/jquery-ui/css/smoothness/jquery-ui.css');
}
});
112 changes: 108 additions & 4 deletions js/src/admin.js
Original file line number Diff line number Diff line change
Expand Up @@ -91,8 +91,79 @@ function convert_to_slug(string) {
return string;
}


function lookup_identifier(service, id) {
try {
return jQuery.getJSON(podlove_vue.rest_url + "podlove/v1/social/lookup/" + service, {'id': id })
// returns 404 when nothing is found,
// TODO: how to I supress the error message in the browser's JavaScript Console?
}
catch { }
}

function auto_fill_form(id, title_id) {
(function ($) {
function find_profile(identifier, type) {
// identier is probably an URI
if ( type !== 'email' && identifier.indexOf("@") == -1 ) {
lookup_identifier('webfinger', identifier).done(function(webfinger) {
console.debug("webfinger lookup response", webfinger);
fill_if_empty('#podlove_contributor_guid', webfinger.subject);
fill_if_empty('#podlove_contributor_privateemail', webfinger.alias);

// TODO: Add social media accounts from webfinger.aliases to datatable
fill_person_from_links(webfinger.links);
});
return null;
}
// identier is probably a string in form user@domain.tld
else {
lookup_identifier('webfinger', 'acct:' + identifier).done((webfinger) => {
console.debug("webfinger lookup response", webfinger);
if (webfinger) {
fill_if_empty('#podlove_contributor_guid', webfinger.subject);
// TODO: Add social media accounts from webfinger.aliases to datatable
fill_person_from_links(webfinger.links);
}
}).fail(() => lookup_identifier('gravatar.com', identifier).done((gravatar) => {
console.debug("gravatar lookup response", gravatar);
fill_if_empty('#podlove_contributor_guid', gravatar.urls?.[0].value || gravatar.accounts?.[0].url || 'https://' + identifier.split('@')[1]);
fill_if_empty('#podlove_contributor_identifier', gravatar.preferredUsername);
fill_if_empty('#podlove_contributor_realname', gravatar.name.formatted
|| [gravatar.name.givenName, gravatar.name.familyName].join(' ')
|| gravatar.preferredUsername);
fill_if_empty('#podlove_contributor_publicname', gravatar.name.formatted || gravatar.preferredUsername);
fill_if_empty('#podlove_contributor_avatar', gravatar.thumbnailUrl, true);
}));
}
}

function fill_person_from_links(links) {
// lookup links[rel=self] for name, avatar, etc.
var self = links.filter(x => x.rel === 'self');
if (self.length > 0) {
lookup_identifier('json', self[0].href).done((mastodon) => {
console.debug("links.self person lookup response", mastodon);
fill_if_empty('#podlove_contributor_identifier', mastodon.preferredUsername);
fill_if_empty('#podlove_contributor_realname', mastodon.name || mastodon.preferredUsername);
fill_if_empty('#podlove_contributor_publicname', mastodon.name || mastodon.preferredUsername);
fill_if_empty('#podlove_contributor_avatar', mastodon.icon.url, true);
});
}
}

function fill_if_empty(field, value, triggerChangeEvent) {
var input = $(field);
if ( input.val() == "" && value ) {
input.attr('value', value);
if (triggerChangeEvent) {
input.change();
}
return true;
}
}


switch (id) {
case 'contributor':
if ($("#podlove_contributor_publicname").val() == "") {
Expand All @@ -102,6 +173,27 @@ function auto_fill_form(id, title_id) {
$("#podlove_contributor_publicname").attr('placeholder', $("#podlove_contributor_realname").val());
}
}
if ($("#podlove_contributor_guid").val() == "") {
if ($("#podlove_contributor_realname").val() != "") {
$("#podlove_contributor_publicname").attr('placeholder', $("#podlove_contributor_nickname").val());
}
}
break;
case 'contributor_email':
if ($("#podlove_contributor_avatar").val() == "") {
var email = $("#podlove_contributor_privateemail").val();
if (email != "") {
find_profile(email, 'email');
}
}
break;
case 'contributor_guid':
if ($("#podlove_contributor_avatar").val() == "") {
var guid = $("#podlove_contributor_guid").val();
if (guid != "") {
find_profile(guid, 'uri');
}
}
break;
case 'contributor_group':
if ($("#podlove_contributor_group_slug").val() == "") {
Expand Down Expand Up @@ -155,6 +247,9 @@ function clean_up_input() {

textfield.addClass("podlove-invalid-input");
$status.addClass("podlove-input-isinvalid");

// abort further change events, hopefully
return false;
}

// trim whitespace
Expand Down Expand Up @@ -182,7 +277,7 @@ function clean_up_input() {
if (!textfield.val().match(valid_url_regexp)) {
// Encode URL only if it is not already encoded
if (!encodeURI(textfield.val()).match(valid_url_regexp)) {
ShowInputError('Please enter a valid URL');
return ShowInputError('Please enter a valid URL');
} else {
textfield.val(encodeURI(textfield.val()));
}
Expand All @@ -193,13 +288,13 @@ function clean_up_input() {
// textfield.val( encodeURI( textfield.val() ) );

if (!textfield.val().match(/^(?:(?:https?|ftp):\/\/)(?:\S+(?::\S*)?@)?(?:(?!(?:10|127)(?:\.\d{1,3}){3})(?!(?:169\.254|192\.168)(?:\.\d{1,3}){2})(?!172\.(?:1[6-9]|2\d|3[0-1])(?:\.\d{1,3}){2})(?:[1-9]\d?|1\d\d|2[01]\d|22[0-3])(?:\.(?:1?\d{1,2}|2[0-4]\d|25[0-5])){2}(?:\.(?:[1-9]\d?|1\d\d|2[0-4]\d|25[0-4]))|(?:(?:[a-z\u00a1-\uffff0-9]+-?)*[a-z\u00a1-\uffff0-9]+)(?:\.(?:[a-z\u00a1-\uffff0-9]+-?)*[a-z\u00a1-\uffff0-9]+)*(?:\.(?:[a-z\u00a1-\uffff]{2,})))(?::\d{2,5})?(?:\/[^\s]*)?$/i)) {
ShowInputError('Please enter a valid email adress or a valid URL');
return ShowInputError('Please enter a valid email adress or a valid URL');
}
}
break;
case "email":
if (!textfield.val().match(/^[a-zA-Z0-9_.+-]+@[a-zA-Z0-9-]+\.[a-zA-Z0-9-.]+$/i))
ShowInputError('Please enter a valid email adress.');
if (!textfield.val().match(/^[a-zA-Z0-9_.+-]+@[a-zA-Z0-9-]+\.[a-zA-Z0-9-.]+$/i))
return ShowInputError('Please enter a valid email adress.');
break;
}
}
Expand Down Expand Up @@ -286,6 +381,14 @@ jQuery(function ($) {
auto_fill_form('contributor', 'realname');
});

$("#podlove_contributor_privateemail").change(function () {
auto_fill_form('contributor_email', 'email');
});

$("#podlove_contributor_guid").change(function () {
auto_fill_form('contributor_guid', 'guid');
});

$("#podlove_contributor_group_title").change(function () {
auto_fill_form('contributor_group', 'group_title');
});
Expand All @@ -296,6 +399,7 @@ jQuery(function ($) {

$(document).ready(function () {
auto_fill_form('contributor', 'realname');
// TODO auto_fill_form('contributor_guid', 'guid'); from social media accounts
clean_up_input();
init_contextual_help_links();
new ClipboardJS('.clipboard-btn');
Expand Down

0 comments on commit 503feef

Please sign in to comment.