Skip to content

Commit

Permalink
Add settings to auto save passphrase on publish, add auto try to deco…
Browse files Browse the repository at this point in the history
…de all encoded objects from same account on decode by new passphrase, completed the manual
  • Loading branch information
On1x committed Jul 11, 2023
1 parent aff6e99 commit 01397ee
Show file tree
Hide file tree
Showing 3 changed files with 152 additions and 11 deletions.
131 changes: 125 additions & 6 deletions app.js
Original file line number Diff line number Diff line change
Expand Up @@ -1019,6 +1019,8 @@ var default_settings={
sync_size:100,
nsfw_warning:true,
nsfw_hashtags:['nsfw','sex','porn'],

save_passphrase_on_publish:false,
};
var settings=default_settings;

Expand Down Expand Up @@ -1147,6 +1149,8 @@ function save_settings(view){
settings.energy=parseInt(parseFloat(energy_str)*100);
settings.silent_award=tab.find('input[name="silent_award"]').prop("checked");

settings.save_passphrase_on_publish=tab.find('input[name="save_passphrase_on_publish"]').prop("checked");

let settings_json=JSON.stringify(settings);
localStorage.setItem(storage_prefix+'settings',settings_json);

Expand Down Expand Up @@ -4012,12 +4016,14 @@ function publish(view){
}
//check passphrase and try encode
let passphrase=view.find('.encode-passphrase input[name="encode-passphrase"]').val();
let passphrase_comment=view.find('.encode-passphrase input[name="encode-comment"]').val();
if(''!=passphrase){
try{
new_object['d']['nt']=new_object['t'];//new type
new_object['d']=JSON.stringify(new_object);
new_object['d']=viz.aes.simpleEncoder(new_object['d'],passphrase);
new_object['t']='e';//encoded
new_object['c']=passphrase_comment;//comment
new_object['p']=previous;
}
catch(e){
Expand All @@ -4035,6 +4041,13 @@ function publish(view){
viz.broadcast.custom(users[current_user].regular_key,[],[current_user],publish_protocol,object_json,function(err,result){
if(result){
console.log(result);

if(settings.save_passphrase_on_publish){
if(''!=passphrase){
add_passphrase(current_user,passphrase);
}
}

view.find('.success').html(ltmp_arr.publish_success);

view.find('input').val('');
Expand Down Expand Up @@ -5097,6 +5110,27 @@ function app_mouse(e){
}
else{//not editor buttons
if(!ignore){
if($(target).hasClass('delete-all-passphrases-action')){
e.preventDefault();
if(!$(target).hasClass('disabled')){
$(target).addClass('disabled');
let view=$(target).closest('.view');
let tab=view.find('.content-view[data-tab="main"]');
tab.find('.submit-button-ring').addClass('show');
tab.find('.error').html('');
tab.find('.success').html('');
let t=db.transaction(['passphrases'],'readwrite');
let q=t.objectStore('passphrases');
let req=q.clear();
req.onsuccess=function(){
view.find('.button').removeClass('disabled');
view.find('.submit-button-ring').removeClass('show');
view.find('.error').html('');
view.find('.success').html(ltmp_arr.app_passphrases_deleted);
};
}
return;
}
if($(target).hasClass('passphrase-remove-action')){
e.preventDefault();
let passphrase_id=$(target).closest('.passphrase-item').data('passphrase-id');
Expand Down Expand Up @@ -6597,12 +6631,14 @@ function app_mouse(e){
}
//check passphrase and try encode
let passphrase=$('.article-settings .encode-passphrase input[name="encode-passphrase"]').val();
let passphrase_comment=$('.article-settings .encode-passphrase input[name="encode-comment"]').val();
if(''!=passphrase){
try{
new_object['d']['nt']=new_object['t'];//new type
new_object['d']=JSON.stringify(new_object);
new_object['d']=viz.aes.simpleEncoder(new_object['d'],passphrase);
new_object['t']='e';//encoded
new_object['c']=passphrase_comment;//comment
new_object['p']=previous;
}
catch(e){
Expand All @@ -6619,7 +6655,19 @@ function app_mouse(e){
viz.broadcast.custom(users[current_user].regular_key,[],[current_user],publish_protocol,object_json,function(err,result){
if(result){
console.log(result);

if(settings.save_passphrase_on_publish){
if(''!=passphrase){
add_passphrase(current_user,passphrase);
}
}

if(false!==edit){
if(settings.save_passphrase_on_publish){
if(''!=passphrase){
add_passphrase(current_user,passphrase);
}
}
setTimeout(function(){
update_user_last_event(current_user,function(result){
if(false!==result){
Expand Down Expand Up @@ -8429,7 +8477,6 @@ function parse_object(account,block,feed_load_flag,callback){
update_short_date();
}
else{
let link='viz://@'+account+'/'+block+'/';
set_date_view($('.object[data-link="'+link+'"] .date-view'),true);
}
}
Expand Down Expand Up @@ -8567,7 +8614,6 @@ function get_replies(object_account,object_block,callback){
}

function add_passphrase(account,passphrase){
console.log('!!! add_passphrase',account,passphrase);
let t=db.transaction(['passphrases'],'readwrite');
let q=t.objectStore('passphrases');
req=q.index('account').openCursor(IDBKeyRange.only(account),'next');
Expand All @@ -8576,14 +8622,12 @@ function add_passphrase(account,passphrase){
req.onsuccess=function(event){
let cur=event.target.result;
if(cur){
console.log('!!! add_passphrase check',cur.value);
if(cur.value.passphrase==passphrase){
find=true;
}
cur.continue();
}
else{
console.log('!!! add_passphrase find',find);
if(!find){
/*
passphrases struct:
Expand All @@ -8595,6 +8639,8 @@ function add_passphrase(account,passphrase){
if(trx_need_commit){
t.commit();
}
//try decode all encoded objects from this account with new passphrase
try_decode_all_objects(account,passphrase);
}
}
};
Expand Down Expand Up @@ -8624,6 +8670,64 @@ function get_passphrases(account,callback){
};
}

function try_decode_all_objects(account,passphrase){
let t=db.transaction(['objects'],'readwrite');
let q=t.objectStore('objects');
let req=q.index('account').openCursor(IDBKeyRange.only(account),'next');
req.onsuccess=function(event){
let cur=event.target.result;
if(cur){
let result=cur.value;
if('e'==result.data.t){//encoded
decode_object(account,result.block,passphrase,function(success){
if(success){
let block=result.block;
console.log('try_decode_all_objects success',account,block);
get_user(account,false,function(err,object_user){
if(!err){
get_object(account,block,false,function(err,object_result){
if(!err){
let link='viz://@'+account+'/'+block+'/';
let view=$('.view[data-level="'+level+'"]');
if(-1==path.indexOf('viz://')){//look in services views
let path_parts=path.split('/');
view=$('.view[data-path="'+path_parts[0]+'"]');
}
let find_object=view.find('.objects>.object[data-account="'+account+'"][data-block="'+block+'"]');
if(find_object.length>0){
find_object=view.find('.object[data-link="'+link+'"]');
}
if(find_object.length>0){
let object_preview=false;
if(find_object.hasClass('type-text-preview')){
object_preview=true;
}
let object_type='default';
if(object_preview){
object_type='preview';
}
new_render=render_object(object_user,object_result,object_type);
find_object.before(new_render);
find_object.remove();//remove old view
if(object_preview){
update_short_date();
}
else{
set_date_view($('.object[data-link="'+link+'"] .date-view'),true);
}
}
}
});
}
});
}
});
}
cur.continue();
}
};
}

function decode_object(account,block,passphrase,callback){
if(typeof callback==='undefined'){
callback=function(){};
Expand Down Expand Up @@ -8651,6 +8755,9 @@ function decode_object(account,block,passphrase,callback){
if(false!==new_object_data){
new_object_data=JSON.parse(new_object_data);
delete result.data.t;
if(typeof result.data.c !== 'undefined'){
delete result.data.c;//remove comment
}
if(typeof new_object_data.d !== 'undefined'){
result.data.d=new_object_data.d;
result.data.d.decoded=1;
Expand Down Expand Up @@ -10878,7 +10985,8 @@ function view_app_settings(view,path_parts,query,title){
tab.find('input[name="nsfw_hashtags"]').val(settings.nsfw_hashtags.join(', '));

tab.find('input[name="energy"]').val(settings.energy/100);
$('input[name="silent_award"]').prop("checked",settings.silent_award);
tab.find('input[name="silent_award"]').prop("checked",settings.silent_award);
tab.find('input[name="save_passphrase_on_publish"]').prop("checked",settings.save_passphrase_on_publish);
}
if('feed'==current_tab){
tab.find('.button').removeClass('disabled');
Expand Down Expand Up @@ -13153,7 +13261,11 @@ function render_object(user,object,type,preset_level){
if(user.account==current_user){
more_view=ltmp(ltmp_arr.more_column,{account:user.account,block:object.block});
}

let comment='';
if(typeof object.data.c !== 'undefined'){
comment=object.data.c;
comment=ltmp(ltmp_arr.object_type_encoded_comment,{comment:comment});
}
render=ltmp(ltmp_arr.object_type_encoded,{
author:'@'+user.account,
account:user.account,
Expand All @@ -13171,6 +13283,7 @@ function render_object(user,object,type,preset_level){
icon_share:ltmp_global.icon_share,
}),
more:more_view,
comment:comment,
});
return render;
}
Expand Down Expand Up @@ -13422,6 +13535,11 @@ function render_object(user,object,type,preset_level){
}
else{
if('encoded'==object_type){
let comment='';
if(typeof object.data.c !== 'undefined'){
comment=object.data.c;
comment=ltmp(ltmp_arr.object_type_encoded_comment,{comment:comment});
}
render=ltmp(ltmp_arr.object_type_encoded_preview,{
author:'@'+user.account,
account:user.account,
Expand All @@ -13432,6 +13550,7 @@ function render_object(user,object,type,preset_level){
events:(typeof object.events !== 'undefined')?object.events.join(','):'',
previous:object.data.p,
timestamp:object.data.timestamp,
comment:comment,
});
return render;
}
Expand Down
15 changes: 13 additions & 2 deletions ltmp_en.js
Original file line number Diff line number Diff line change
Expand Up @@ -119,12 +119,18 @@ var ltmp_en_arr = {
<p><input type="text" name="energy" placeholder="1%" value=""></p>
<div class="input-addon">(regenerates 20% per day)</div>
<hr>
<p>By create encrypted object:</p>
<p><label><input type="checkbox" name="save_passphrase_on_publish"> &mdash; save passphrase</label></p>
<div class="input-addon">(turned off by default)</div>
<p class="error save-settings-error"></p>
<p class="success save-settings-success"></p>
<p><a class="button save-settings-action">Save</a><span class="submit-button-ring"></span></p>
<hr>
<p><a class="button neutral-button install-action"></a></p>
<p><a class="button neutral-button reset-settings-action">Reset all settings</a></p>
<p><a class="button neutral-button delete-all-passphrases-action">Remove all saved passphrases</a></p>
<p><a class="button neutral-button reset-database-action">Reset database state</a></p>
</div>
<div class="content-view" data-tab="feed">
Expand Down Expand Up @@ -273,7 +279,7 @@ var ltmp_en_arr = {
encoding_description: `Type the password that will be used to encrypt the object.<br>
Any user will be able to decrypt the object if he knows the password.<br>
If the password is not specified, the object will be available to everyone.`,
encoding_form: `<input type="text" name="encode-passphrase" class="round" placeholder="Key passphrase..." value="">`,
encoding_form: `<input type="text" name="encode-passphrase" class="round" placeholder="Key passphrase..." value=""><br><input type="text" name="encode-comment" class="round" placeholder="Comment (optional, public)..." value="">`,
beneficiaries_list_caption: `Beneficiaries`,
beneficiaries_list_description: `Specify the users who will receive some of the rewards.`,
beneficiaries_list_add: `%%beneficiaries_item%% %%beneficiaries_add_item%%`,
Expand Down Expand Up @@ -413,6 +419,8 @@ var ltmp_en_arr = {
<p>The editor allows you to design a complete publication with a header, subheadings, links, images and quotes. In the settings you need to specify an annotation, a link to a thumbnail and optionally add tags from your profile.</p>
<p>You can also specify the beneficiaries in the advanced settings by specifying the accounts that will share the reward you receive and the percentages to distribute it.</p>
<p>Extended Publications has a separate viewer in the PC version and feels like a popular blogging platform.</p>
<hr><p>When creating new notes or publications, there is a section "%%open_publish_addons%%", where you can set a password to encrypt the note, as well as a list of beneficiaries (who will share the reward from users with you). When encrypting, the AES-256-CBC algorithm is used, you can set a public comment to the published object. Anyone you give the password to (or who guesses it) will be able to view the object.</p>
<hr><p>If the object is published in the blockchain, then it is impossible to delete it. But with the Voice Events extension, it became possible to hide or edit past objects. The list of possible actions you will see on the page of viewing the object in the drop-down menu expanded by the icon %%icon_more%%.</p>
`},
uri: {
title: 'VIZ links', html: `
Expand Down Expand Up @@ -658,6 +666,7 @@ var ltmp_en_arr = {
app_settings_caption: 'Application Settings',
app_settings_saved: 'Settings saved',
app_settings_reset: 'Settings reset',
app_passphrases_deleted: 'All passphrases deleted',
app_settings_main_tab: 'General',
app_settings_feed_tab: 'News Feed',
app_settings_theme_tab: 'Color Theme',
Expand Down Expand Up @@ -813,6 +822,7 @@ var ltmp_en_arr = {
{more}
</div>
<div class="object-column">
{comment}
<div class="decode-form" data-href="{link}">%%decode_form%%</div>
<div class="date-view" data-timestamp="{timestamp}">&hellip;</div>
<div class="actions-view">{actions}</div>
Expand All @@ -825,10 +835,11 @@ var ltmp_en_arr = {
<div class="author-view">
<div class="author-column"><a tabindex="0" data-href="viz://{author}/" class="profile-name">{nickname}</a><a tabindex="0" data-href="viz://{author}/" class="profile-link">{author}</a><a tabindex="0" data-href="{link}" class="short-date-view" data-timestamp="{timestamp}">&hellip;</a></div>
</div>
{comment}
<div class="decode-form" data-href="{link}">%%decode_form%%</div>
<div class="content-view{class_addon}" data-href="{link}">{text}</div>
</div>
</div>`,
object_type_encoded_comment: `<div class="content-view">{comment}</div>`,
decode_form: `
<div class="notice-caption">%%icon_locked%% Object is encoded</div>
<div class="notice-description">If you know the passphrase you can decode it.<br>All keys will be stored to specific account.<br>Press on me.</div>
Expand Down
Loading

0 comments on commit 01397ee

Please sign in to comment.