Add displayname field

This commit is contained in:
kpcyrd
2019-02-14 16:07:39 +01:00
parent 17f4682476
commit c0c2c31b65
7 changed files with 101 additions and 4 deletions

View File

@@ -3,6 +3,7 @@ CREATE TABLE accounts (
value VARCHAR NOT NULL,
service VARCHAR NOT NULL,
username VARCHAR NOT NULL,
displayname VARCHAR,
email VARCHAR,
url VARCHAR,
last_seen DATETIME,

76
modules/dev/github.lua Normal file
View File

@@ -0,0 +1,76 @@
-- Description: Collect data from github profiles
-- Version: 0.1.0
-- Source: accounts:github.com
-- License: GPL-3.0
function api_get(url)
local req = http_request(session, 'GET', url, {})
local resp = http_send(req)
if last_err() then return end
if resp['status'] ~= 200 then return 'invalid status code' end
local data = json_decode(resp['text'])
if last_err() then return end
return data
end
function scan4email(username)
local url = 'https://api.github.com/users/' .. username .. '/repos'
local repos = api_get(url)
if last_err() then return end
i = 1
while i <= #repos do
local repo = repos[i]
debug(repo)
local commits = api_get(repo['url'] .. '/commits')
if last_err() then return end
j = 1
while j <= #commits do
local commit = commits[j]
debug(commit)
if commit['author']['login'] == username then
-- name = commit['commit']['author']['name']
local email = commit['commit']['author']['email']
return email
end
if commit['committer']['login'] == username then
-- name = commit['commit']['committer']['name']
local email = commit['commit']['committer']['email']
return email
end
j = j+1
end
i = i+1
end
end
function run(arg)
session = http_mksession()
local url = 'https://api.github.com/users/' .. arg['username']
local data = api_get(url)
if last_err() then return end
debug(data)
-- company = data['company']
-- location = data['location']
-- homepage = data['blog']
local email = data['email']
if not email and not arg['email'] then
email = scan4email(arg['username'])
if last_err() then return end
end
db_update('account', arg, {
url=data['html_url'],
displayname=data['name'],
email=email,
})
end

View File

@@ -24,9 +24,13 @@ function run(arg)
db_add('email', {
value=ld['email'],
})
db_update('account', arg, {
email=ld['email'],
url=url,
})
end
-- homepage=ld['url']
db_update('account', arg, {
displayname=ld['name'],
email=ld['email'],
url=url,
})
end

View File

@@ -237,6 +237,7 @@ fn add_account(rl: &mut Readline, args: AddAccount) -> Result<()> {
value: &value,
service: &service,
username: &username,
displayname: None,
email: None,
url: None,
last_seen: None,

View File

@@ -189,6 +189,7 @@ impl Database {
value: &object.value,
service: &object.service,
username: &object.username,
displayname: object.displayname.as_ref(),
email: object.email.as_ref(),
url: object.url.as_ref(),
last_seen: object.last_seen,

View File

@@ -13,6 +13,7 @@ pub struct Account {
pub value: String,
pub service: String,
pub username: String,
pub displayname: Option<String>,
pub email: Option<String>,
pub url: Option<String>,
pub last_seen: Option<NaiveDateTime>,
@@ -152,6 +153,7 @@ impl Printable<PrintableAccount> for Account {
pub struct DetailedAccount {
id: i32,
value: String,
displayname: Option<String>,
email: Option<String>,
url: Option<String>,
last_seen: Option<NaiveDateTime>,
@@ -170,6 +172,7 @@ impl DisplayableDetailed for DetailedAccount {
w.debug::<Green, _>(&self.value)?;
w.start_group();
w.opt_debug::<Yellow, _>(&self.displayname)?;
w.opt_debug::<Yellow, _>(&self.email)?;
w.opt_debug::<Yellow, _>(&self.url)?;
w.opt_debug::<Yellow, _>(&self.last_seen)?;
@@ -193,6 +196,7 @@ impl Detailed for Account {
Ok(DetailedAccount {
id: self.id,
value: self.value.to_string(),
displayname: self.displayname.clone(),
email: self.email.clone(),
url: self.url.clone(),
last_seen: self.last_seen.clone(),
@@ -207,6 +211,7 @@ pub struct NewAccount<'a> {
pub value: &'a str,
pub service: &'a str,
pub username: &'a str,
pub displayname: Option<&'a String>,
pub email: Option<&'a String>,
pub url: Option<&'a String>,
pub last_seen: Option<NaiveDateTime>,
@@ -231,6 +236,7 @@ impl<'a> Upsertable<Account> for NewAccount<'a> {
fn upsert(self, existing: &Account) -> Self::Update {
Self::Update {
id: existing.id,
displayname: Self::upsert_str(self.displayname, &existing.displayname),
email: Self::upsert_str(self.email, &existing.email),
url: Self::upsert_str(self.url, &existing.url),
last_seen: Self::upsert_opt(self.last_seen, &existing.last_seen),
@@ -244,6 +250,7 @@ pub struct NewAccountOwned {
pub value: String,
pub service: String,
pub username: String,
pub displayname: Option<String>,
pub email: Option<String>,
pub url: Option<String>,
pub last_seen: Option<NaiveDateTime>,
@@ -261,6 +268,7 @@ impl Printable<PrintableAccount> for NewAccountOwned {
pub struct InsertAccount {
pub service: String,
pub username: String,
pub displayname: Option<String>,
pub email: Option<String>,
pub url: Option<String>,
pub last_seen: Option<NaiveDateTime>,
@@ -278,6 +286,7 @@ impl LuaInsertToNewOwned for InsertAccount {
value,
service: self.service,
username: self.username,
displayname: self.displayname,
email: self.email,
url: self.url,
last_seen: self.last_seen,
@@ -289,6 +298,7 @@ impl LuaInsertToNewOwned for InsertAccount {
#[table_name="accounts"]
pub struct AccountUpdate {
pub id: i32,
pub displayname: Option<String>,
pub email: Option<String>,
pub url: Option<String>,
pub last_seen: Option<NaiveDateTime>,
@@ -296,6 +306,7 @@ pub struct AccountUpdate {
impl Upsert for AccountUpdate {
fn is_dirty(&self) -> bool {
self.displayname.is_some() ||
self.email.is_some() ||
self.url.is_some() ||
self.last_seen.is_some()
@@ -312,12 +323,14 @@ impl Upsert for AccountUpdate {
impl Updateable<Account> for AccountUpdate {
fn changeset(&mut self, existing: &Account) {
Self::clear_if_equal(&mut self.displayname, &existing.displayname);
Self::clear_if_equal(&mut self.email, &existing.email);
Self::clear_if_equal(&mut self.url, &existing.url);
Self::clear_if_equal(&mut self.last_seen, &existing.last_seen);
}
fn fmt(&self, updates: &mut Vec<String>) {
Self::push_value(updates, "displayname", &self.displayname);
Self::push_value(updates, "email", &self.email);
Self::push_value(updates, "url", &self.url);
Self::push_value(updates, "last_seen", &self.last_seen);

View File

@@ -4,6 +4,7 @@ table! {
value -> Text,
service -> Text,
username -> Text,
displayname -> Nullable<Text>,
email -> Nullable<Text>,
url -> Nullable<Text>,
last_seen -> Nullable<Timestamp>,