Browse Source

update sync user info

pull/18/head
Sun 4 years ago
parent
commit
a5bdba268f
  1. 74
      src/account.rs
  2. 69
      src/apps/chat/layer.rs
  3. 46
      src/apps/chat/models/friend.rs
  4. 6
      src/apps/chat/models/request.rs
  5. 15
      src/apps/chat/rpc.rs
  6. 16
      src/apps/wallet/rpc.rs
  7. 22
      src/event.rs
  8. 24
      src/group.rs
  9. 4
      src/migrate/account.rs
  10. 1
      src/migrate/chat.rs

74
src/account.rs

@ -55,7 +55,9 @@ pub(crate) struct Account { @@ -55,7 +55,9 @@ pub(crate) struct Account {
pub lock: Vec<u8>, // hashed-lock.
pub secret: Vec<u8>, // encrypted value.
pub encrypt: Vec<u8>, // encrypted encrypt key.
pub height: u64,
pub wallet: String, // main wallet info.
pub pub_height: i64, // public information height.
pub own_height: u64, // own data consensus height.
pub event: EventId,
pub datetime: i64,
}
@ -81,7 +83,9 @@ impl Account { @@ -81,7 +83,9 @@ impl Account {
Account {
id: 0,
height: 0,
pub_height: 0,
own_height: 0,
wallet: String::new(),
event: EventId::default(),
gid,
index,
@ -178,7 +182,9 @@ impl Account { @@ -178,7 +182,9 @@ impl Account {
Account {
datetime: v.pop().unwrap().as_i64(),
event: EventId::from_hex(v.pop().unwrap().as_str()).unwrap_or(EventId::default()),
height: v.pop().unwrap().as_i64() as u64,
own_height: v.pop().unwrap().as_i64() as u64,
pub_height: v.pop().unwrap().as_i64(),
wallet: v.pop().unwrap().as_string(),
avatar: base64::decode(v.pop().unwrap().as_str()).unwrap_or(vec![]),
encrypt: base64::decode(v.pop().unwrap().as_str()).unwrap_or(vec![]),
secret: base64::decode(v.pop().unwrap().as_str()).unwrap_or(vec![]),
@ -193,22 +199,23 @@ impl Account { @@ -193,22 +199,23 @@ impl Account {
}
}
pub fn _get(db: &DStorage, gid: &GroupId) -> Result<Option<Account>> {
pub fn get(db: &DStorage, gid: &GroupId) -> Result<Account> {
let sql = format!(
"SELECT id, gid, indx, lang, pass, name, lock, mnemonic, secret, encrypt, avatar, height, event, datetime FROM accounts WHERE gid = '{}'",
"SELECT id, gid, indx, lang, pass, name, lock, mnemonic, secret, encrypt, avatar, wallet, pub_height, own_height, event, datetime FROM accounts WHERE gid = '{}'",
gid.to_hex()
);
let mut matrix = db.query(&sql)?;
if matrix.len() > 0 {
let values = matrix.pop().unwrap(); // safe unwrap()
return Ok(Some(Account::from_values(values)));
Ok(Account::from_values(values))
} else {
Err(anyhow!("account is missing."))
}
Ok(None)
}
pub fn all(db: &DStorage) -> Result<Vec<Account>> {
let matrix = db.query(
"SELECT id, gid, indx, lang, pass, name, lock, mnemonic, secret, encrypt, avatar, height, event, datetime FROM accounts ORDER BY datetime DESC",
"SELECT id, gid, indx, lang, pass, name, lock, mnemonic, secret, encrypt, avatar, wallet, pub_height, own_height, event, datetime FROM accounts ORDER BY datetime DESC",
)?;
let mut accounts = vec![];
for values in matrix {
@ -227,7 +234,7 @@ impl Account { @@ -227,7 +234,7 @@ impl Account {
self.id = id;
self.update(db)?;
} else {
let sql = format!("INSERT INTO accounts (gid, indx, lang, pass, name, lock, mnemonic, secret, encrypt, avatar, height, event, datetime) VALUES ('{}', {}, {}, '{}', '{}', '{}', '{}', '{}', '{}', '{}', {}, '{}', {})",
let sql = format!("INSERT INTO accounts (gid, indx, lang, pass, name, lock, mnemonic, secret, encrypt, avatar, wallet, pub_height, own_height, event, datetime) VALUES ('{}', {}, {}, '{}', '{}', '{}', '{}', '{}', '{}', '{}', '{}', {}, {}, '{}', {})",
self.gid.to_hex(),
self.index,
self.lang,
@ -238,7 +245,9 @@ impl Account { @@ -238,7 +245,9 @@ impl Account {
base64::encode(&self.secret),
base64::encode(&self.encrypt),
base64::encode(&self.avatar),
self.height,
self.wallet,
self.pub_height,
self.own_height,
self.event.to_hex(),
self.datetime,
);
@ -249,12 +258,14 @@ impl Account { @@ -249,12 +258,14 @@ impl Account {
}
pub fn update(&self, db: &DStorage) -> Result<usize> {
let sql = format!("UPDATE accounts SET name='{}', lock='{}', encrypt='{}', avatar='{}', height={}, event='{}', datetime={} WHERE id = {}",
let sql = format!("UPDATE accounts SET name='{}', lock='{}', encrypt='{}', avatar='{}', wallet='{}', pub_height={}, own_height={}, event='{}', datetime={} WHERE id = {}",
self.name,
base64::encode(&self.lock),
base64::encode(&self.encrypt),
base64::encode(&self.avatar),
self.height,
self.wallet,
self.pub_height,
self.own_height,
self.datetime,
self.event.to_hex(),
self.id,
@ -264,9 +275,11 @@ impl Account { @@ -264,9 +275,11 @@ impl Account {
pub fn update_info(&self, db: &DStorage) -> Result<usize> {
let sql = format!(
"UPDATE accounts SET name='{}', avatar='{}' WHERE id = {}",
"UPDATE accounts SET name='{}', avatar='{}', wallet='{}', height={} WHERE id = {}",
self.name,
base64::encode(&self.avatar),
self.wallet,
self.pub_height + 1,
self.id,
);
db.update(&sql)
@ -278,11 +291,11 @@ impl Account { @@ -278,11 +291,11 @@ impl Account {
}
pub fn update_consensus(&mut self, db: &DStorage, height: u64, eid: EventId) -> Result<usize> {
self.height = height;
self.own_height = height;
self.event = eid;
let sql = format!(
"UPDATE accounts SET height={}, event='{}' WHERE id = {}",
self.height,
"UPDATE accounts SET own_height={}, event='{}' WHERE id = {}",
self.own_height,
self.event.to_hex(),
self.id,
);
@ -296,33 +309,36 @@ pub(crate) struct User { @@ -296,33 +309,36 @@ pub(crate) struct User {
pub addr: PeerId,
pub name: String,
pub wallet: String,
pub height: i64,
pub avatar: Vec<u8>,
}
impl User {
pub fn simple(id: GroupId, addr: PeerId, name: String, avatar: Vec<u8>) -> Self {
pub fn simple(
id: GroupId,
addr: PeerId,
name: String,
avatar: Vec<u8>,
wallet: String,
) -> Self {
Self {
id,
addr,
name,
avatar,
wallet: String::new(),
wallet,
height: 0,
}
}
pub fn full(
id: GroupId,
addr: PeerId,
name: String,
wallet: String,
avatar: Vec<u8>,
) -> Result<Self> {
Ok(Self {
id,
addr,
pub fn info(name: String, wallet: String, height: i64, avatar: Vec<u8>) -> Self {
Self {
id: GroupId::default(),
addr: PeerId::default(),
name,
wallet,
height,
avatar,
})
}
}
}

69
src/apps/chat/layer.rs

@ -11,7 +11,7 @@ use tokio::sync::RwLock; @@ -11,7 +11,7 @@ use tokio::sync::RwLock;
use chat_types::{MessageType, NetworkMessage};
use crate::account::User;
use crate::account::{Account, User};
use crate::event::InnerEvent;
use crate::layer::{Layer, Online};
use crate::migrate::consensus::{FRIEND_TABLE_PATH, MESSAGE_TABLE_PATH, REQUEST_TABLE_PATH};
@ -19,11 +19,16 @@ use crate::rpc::{ @@ -19,11 +19,16 @@ use crate::rpc::{
notice_menu, session_connect, session_create, session_last, session_lost, session_suspend,
};
use crate::session::{connect_session, Session, SessionType};
use crate::storage::{chat_db, session_db, write_avatar_sync};
use crate::storage::{account_db, chat_db, session_db, write_avatar_sync};
use super::models::{handle_nmsg, raw_to_network_message, Friend, Message, Request};
use super::rpc;
/// Chat connect data structure.
/// params: Friend about me height, connect_proof.
#[derive(Serialize, Deserialize)]
pub struct LayerConnect(pub i64, pub Proof);
/// ESSE chat layer Event.
#[derive(Serialize, Deserialize)]
pub(crate) enum LayerEvent {
@ -41,8 +46,10 @@ pub(crate) enum LayerEvent { @@ -41,8 +46,10 @@ pub(crate) enum LayerEvent {
Reject,
/// receiver gid, sender gid, message.
Message(EventId, NetworkMessage),
/// request user info.
InfoReq(i64),
/// user full info.
Info(User),
InfoRes(User),
/// close friendship.
Close,
}
@ -67,11 +74,18 @@ pub(crate) async fn handle( @@ -67,11 +74,18 @@ pub(crate) async fn handle(
}
RecvType::Connect(addr, data) | RecvType::ResultConnect(addr, data) => {
// ESSE chat layer connect date structure.
if handle_connect(&mgid, &fgid, &addr, data, &mut layer, &mut results)? {
if let Ok(height) = handle_connect(&mgid, &fgid, &addr, data, &mut layer, &mut results)
{
let peer_id = addr.id;
let proof = layer.group.read().await.prove_addr(&mgid, &addr.id)?;
let data = bincode::serialize(&proof).unwrap_or(vec![]);
let msg = SendType::Result(0, addr, true, false, data);
results.layers.push((mgid, fgid, msg));
let info = LayerEvent::InfoReq(height);
let data = bincode::serialize(&info).unwrap_or(vec![]);
let msg = SendType::Event(0, peer_id, data);
results.layers.push((mgid, fgid, msg));
} else {
let msg = SendType::Result(0, addr, false, false, vec![]);
results.layers.push((mgid, fgid, msg));
@ -80,7 +94,14 @@ pub(crate) async fn handle( @@ -80,7 +94,14 @@ pub(crate) async fn handle(
RecvType::Result(addr, is_ok, data) => {
// ESSE chat layer result date structure.
if is_ok {
if !handle_connect(&mgid, &fgid, &addr, data, &mut layer, &mut results)? {
if let Ok(height) =
handle_connect(&mgid, &fgid, &addr, data, &mut layer, &mut results)
{
let info = LayerEvent::InfoReq(height);
let data = bincode::serialize(&info).unwrap_or(vec![]);
let msg = SendType::Event(0, addr.id, data);
results.layers.push((mgid, fgid, msg));
} else {
let msg = SendType::Result(0, addr, false, false, vec![]);
results.layers.push((mgid, fgid, msg));
}
@ -138,7 +159,7 @@ fn handle_connect( @@ -138,7 +159,7 @@ fn handle_connect(
data: Vec<u8>,
layer: &mut Layer,
results: &mut HandleResult,
) -> Result<bool> {
) -> Result<i64> {
// 0. deserialize connect data.
let proof: Proof = bincode::deserialize(&data)?;
@ -148,25 +169,25 @@ fn handle_connect( @@ -148,25 +169,25 @@ fn handle_connect(
// 2. check friendship.
let friend = update_friend(&layer.base, mgid, fgid, &addr.id);
if friend.is_err() {
return Ok(false);
return Err(anyhow!("not friend"));
}
let fid = friend.unwrap().id; // safe.
let f = friend.unwrap(); // safe.
// 3. get session.
let session_some = connect_session(&layer.base, mgid, &SessionType::Chat, &fid, &addr.id)?;
let session_some = connect_session(&layer.base, mgid, &SessionType::Chat, &f.id, &addr.id)?;
if session_some.is_none() {
return Ok(false);
return Err(anyhow!("not friend"));
}
let sid = session_some.unwrap().id;
// 4. active this session.
layer
.running_mut(mgid)?
.check_add_online(*fgid, Online::Direct(addr.id), sid, fid)?;
.check_add_online(*fgid, Online::Direct(addr.id), sid, f.id)?;
// 5. session online to UI.
results.rpcs.push(session_connect(*mgid, &sid, &addr.id));
Ok(true)
Ok(f.height)
}
impl LayerEvent {
@ -208,7 +229,7 @@ impl LayerEvent { @@ -208,7 +229,7 @@ impl LayerEvent {
}
let mut request = Request::new(
remote.id,
remote.addr,
addr,
remote.name.clone(),
remark.clone(),
false,
@ -263,7 +284,8 @@ impl LayerEvent { @@ -263,7 +284,8 @@ impl LayerEvent {
request.is_ok = true;
request.update(&db)?;
let request_id = request.id;
let friend = Friend::from_request(&db, request)?;
let friend =
Friend::from_remote(&db, remote.id, remote.name, addr, remote.wallet)?;
write_avatar_sync(&layer.base, &mgid, &remote.id, remote.avatar)?;
results
.rpcs
@ -322,7 +344,23 @@ impl LayerEvent { @@ -322,7 +344,23 @@ impl LayerEvent {
update_session(&layer.base, &mgid, &fid, &msg, &mut results);
}
}
LayerEvent::Info(remote) => {
LayerEvent::InfoReq(height) => {
// check sync remote height.
if let Ok(account) = Account::get(&account_db(layer.base())?, &mgid) {
if account.pub_height > height {
let info = LayerEvent::InfoRes(User::info(
account.name,
account.wallet,
account.pub_height,
account.avatar,
));
let data = bincode::serialize(&info).unwrap_or(vec![]);
let msg = SendType::Event(0, addr, data);
results.layers.push((mgid, fgid, msg));
}
}
}
LayerEvent::InfoRes(remote) => {
let (_sid, fid) = layer.get_running_remote_id(&mgid, &fgid)?;
let avatar = remote.avatar.clone();
let db = chat_db(&layer.base, &mgid)?;
@ -330,6 +368,7 @@ impl LayerEvent { @@ -330,6 +368,7 @@ impl LayerEvent {
f.name = remote.name;
f.addr = remote.addr;
f.wallet = remote.wallet;
f.height = remote.height;
f.remote_update(&db)?;
drop(db);
write_avatar_sync(&layer.base, &mgid, &remote.id, remote.avatar)?;

46
src/apps/chat/models/friend.rs

@ -8,7 +8,7 @@ use tdn_storage::local::{DStorage, DsValue}; @@ -8,7 +8,7 @@ use tdn_storage::local::{DStorage, DsValue};
use crate::session::{Session, SessionType};
use super::{Message, Request};
use super::Message;
pub(crate) struct Friend {
pub id: i64,
@ -16,13 +16,21 @@ pub(crate) struct Friend { @@ -16,13 +16,21 @@ pub(crate) struct Friend {
pub addr: PeerId,
pub name: String,
pub wallet: String,
pub height: i64,
pub remark: String,
pub is_closed: bool,
pub datetime: i64,
}
impl Friend {
pub fn new(gid: GroupId, addr: PeerId, name: String, wallet: String, remark: String) -> Friend {
pub fn new(
gid: GroupId,
addr: PeerId,
name: String,
wallet: String,
remark: String,
height: i64,
) -> Friend {
let start = SystemTime::now();
let datetime = start
.duration_since(UNIX_EPOCH)
@ -35,6 +43,7 @@ impl Friend { @@ -35,6 +43,7 @@ impl Friend {
addr,
name,
wallet,
height,
remark,
datetime,
is_closed: false,
@ -51,6 +60,7 @@ impl Friend { @@ -51,6 +60,7 @@ impl Friend {
datetime: v.pop().unwrap().as_i64(),
is_closed: v.pop().unwrap().as_bool(),
remark: v.pop().unwrap().as_string(),
height: v.pop().unwrap().as_i64(),
wallet: v.pop().unwrap().as_string(),
name: v.pop().unwrap().as_string(),
addr: PeerId::from_hex(v.pop().unwrap().as_str()).unwrap_or(PeerId::default()),
@ -59,15 +69,22 @@ impl Friend { @@ -59,15 +69,22 @@ impl Friend {
}
}
pub fn from_request(db: &DStorage, request: Request) -> Result<Friend> {
if let Ok(mut friend) = Friend::get_id(&db, &request.gid) {
friend.name = request.name;
friend.addr = request.addr;
pub fn from_remote(
db: &DStorage,
gid: GroupId,
name: String,
addr: PeerId,
wallet: String,
) -> Result<Friend> {
if let Ok(mut friend) = Friend::get_id(&db, &gid) {
friend.name = name;
friend.addr = addr;
friend.wallet = wallet;
friend.is_closed = false;
friend.remote_update(&db)?;
Ok(friend)
} else {
let mut friend = request.to_friend();
let mut friend = Friend::new(gid, addr, name, wallet, "".to_owned(), 0);
friend.insert(&db)?;
Ok(friend)
}
@ -112,7 +129,7 @@ impl Friend { @@ -112,7 +129,7 @@ impl Friend {
}
pub fn get_id(db: &DStorage, gid: &GroupId) -> Result<Friend> {
let sql = format!("SELECT id, gid, addr, name, wallet, remark, is_closed, datetime FROM friends WHERE gid = '{}'", gid.to_hex());
let sql = format!("SELECT id, gid, addr, name, wallet, height, remark, is_closed, datetime FROM friends WHERE gid = '{}'", gid.to_hex());
let mut matrix = db.query(&sql)?;
if matrix.len() > 0 {
Ok(Friend::from_values(matrix.pop().unwrap())) // safe unwrap()
@ -122,7 +139,7 @@ impl Friend { @@ -122,7 +139,7 @@ impl Friend {
}
pub fn get(db: &DStorage, id: &i64) -> Result<Friend> {
let sql = format!("SELECT id, gid, addr, name, wallet, remark, is_closed, datetime FROM friends WHERE id = {}", id);
let sql = format!("SELECT id, gid, addr, name, wallet, height, remark, is_closed, datetime FROM friends WHERE id = {}", id);
let mut matrix = db.query(&sql)?;
if matrix.len() > 0 {
Ok(Friend::from_values(matrix.pop().unwrap())) // safe unwrap()
@ -134,7 +151,7 @@ impl Friend { @@ -134,7 +151,7 @@ impl Friend {
/// use in rpc when load account friends.
pub fn list(db: &DStorage) -> Result<Vec<Friend>> {
let matrix = db.query(
"SELECT id, gid, addr, name, wallet, remark, is_closed, datetime FROM friends",
"SELECT id, gid, addr, name, wallet, height, remark, is_closed, datetime FROM friends",
)?;
let mut friends = vec![];
for values in matrix {
@ -144,11 +161,12 @@ impl Friend { @@ -144,11 +161,12 @@ impl Friend {
}
pub fn insert(&mut self, db: &DStorage) -> Result<()> {
let sql = format!("INSERT INTO friends (gid, addr, name, wallet, remark, is_closed, datetime) VALUES ('{}', '{}', '{}', '{}', '{}', {}, {})",
let sql = format!("INSERT INTO friends (gid, addr, name, wallet, height, remark, is_closed, datetime) VALUES ('{}', '{}', '{}', '{}', {}, '{}', {}, {})",
self.gid.to_hex(),
self.addr.to_hex(),
self.name,
self.wallet,
self.height,
self.remark,
self.is_closed,
self.datetime,
@ -159,10 +177,11 @@ impl Friend { @@ -159,10 +177,11 @@ impl Friend {
}
pub fn update(&self, db: &DStorage) -> Result<usize> {
let sql = format!("UPDATE friends SET addr = '{}', name = '{}', wallet = '{}', remark = '{}', is_closed = {} WHERE id = {}",
let sql = format!("UPDATE friends SET addr = '{}', name = '{}', wallet = '{}', height={}, remark = '{}', is_closed = {} WHERE id = {}",
self.addr.to_hex(),
self.name,
self.wallet,
self.height,
self.remark,
self.is_closed,
self.id
@ -189,10 +208,11 @@ impl Friend { @@ -189,10 +208,11 @@ impl Friend {
pub fn remote_update(&self, db: &DStorage) -> Result<usize> {
let sql = format!(
"UPDATE friends SET addr='{}', name='{}', wallet='{}', is_closed = false, is_deleted = false WHERE id = {}",
"UPDATE friends SET addr='{}', name='{}', wallet='{}', height={}, is_closed = false, is_deleted = false WHERE id = {}",
self.addr.to_hex(),
self.name,
self.wallet,
self.height,
self.id,
);
db.update(&sql)

6
src/apps/chat/models/request.rs

@ -6,8 +6,6 @@ use tdn::types::{ @@ -6,8 +6,6 @@ use tdn::types::{
};
use tdn_storage::local::{DStorage, DsValue};
use super::Friend;
#[derive(Clone)]
pub(crate) struct Request {
pub id: i64,
@ -51,10 +49,6 @@ impl Request { @@ -51,10 +49,6 @@ impl Request {
}
}
pub fn to_friend(self) -> Friend {
Friend::new(self.gid, self.addr, self.name, self.remark, "".to_owned())
}
/// here is zero-copy and unwrap is safe. checked.
fn from_values(mut v: Vec<DsValue>) -> Request {
Request {

15
src/apps/chat/rpc.rs

@ -78,16 +78,6 @@ pub(crate) fn message_delete(mgid: GroupId, id: i64) -> RpcParam { @@ -78,16 +78,6 @@ pub(crate) fn message_delete(mgid: GroupId, id: i64) -> RpcParam {
rpc_response(0, "chat-message-delete", json!([id]), mgid)
}
#[inline]
fn friend_list(friends: Vec<Friend>) -> RpcParam {
let mut results = vec![];
for friend in friends {
results.push(friend.to_rpc());
}
json!(results)
}
#[inline]
fn request_list(requests: Vec<Request>) -> RpcParam {
let mut results = vec![];
@ -300,7 +290,7 @@ pub(crate) fn new_rpc_handler(handler: &mut RpcHandler<RpcState>) { @@ -300,7 +290,7 @@ pub(crate) fn new_rpc_handler(handler: &mut RpcHandler<RpcState>) {
&gid,
InnerEvent::SessionRequestCreate(
true,
User::simple(remote_gid, remote_addr, remote_name, vec![]),
User::simple(remote_gid, remote_addr, remote_name, vec![], "".to_owned()),
remark,
),
REQUEST_TABLE_PATH,
@ -342,7 +332,8 @@ pub(crate) fn new_rpc_handler(handler: &mut RpcHandler<RpcState>) { @@ -342,7 +332,8 @@ pub(crate) fn new_rpc_handler(handler: &mut RpcHandler<RpcState>) {
request.is_over = true;
request.update(&db)?;
let friend = Friend::from_request(&db, request)?;
let friend =
Friend::from_remote(&db, request.gid, request.name, request.addr, "".to_owned())?;
results.rpcs.push(json!([id, friend.to_rpc()]));
// ADD NEW SESSION.

16
src/apps/wallet/rpc.rs

@ -16,7 +16,10 @@ use web3::{ @@ -16,7 +16,10 @@ use web3::{
Web3,
};
use crate::{rpc::RpcState, storage::wallet_db};
use crate::{
rpc::RpcState,
storage::{account_db, wallet_db},
};
use super::{
models::{Address, Balance, ChainToken, Network, Token},
@ -584,8 +587,17 @@ pub(crate) fn new_rpc_handler(handler: &mut RpcHandler<RpcState>) { @@ -584,8 +587,17 @@ pub(crate) fn new_rpc_handler(handler: &mut RpcHandler<RpcState>) {
"wallet-main",
|gid: GroupId, params: Vec<RpcParam>, state: Arc<RpcState>| async move {
let id = params[0].as_i64().ok_or(RpcError::ParseError)?;
let db = wallet_db(state.layer.read().await.base(), &gid)?;
let base = state.layer.read().await.base().clone();
let db = wallet_db(&base, &gid)?;
let a_db = account_db(&base)?;
let address = Address::get(&db, &id)?;
Address::main(&db, &id)?;
let mut group_lock = state.group.write().await;
let account = group_lock.account_mut(&gid)?;
account.wallet = address.address;
account.update_info(&a_db)?;
drop(group_lock);
Ok(HandleResult::new())
},
);

22
src/event.rs

@ -216,14 +216,14 @@ impl InnerEvent { @@ -216,14 +216,14 @@ impl InnerEvent {
}
let (merge_height, next_height, next_eid) =
if account.height + 1 == eheight && account.event == pre_event {
if account.own_height + 1 == eheight && account.event == pre_event {
(eheight, eheight, eid)
} else {
Self::merge_event(
&db,
&addr,
results,
account.height,
account.own_height,
account.event,
eheight,
eid,
@ -274,7 +274,13 @@ impl InnerEvent { @@ -274,7 +274,13 @@ impl InnerEvent {
if avatar.len() > 0 {
write_avatar_sync(group.base(), &gid, &request.gid, avatar)?;
}
let friend = Friend::from_request(&db, request)?;
let friend = Friend::from_remote(
&db,
request.gid,
request.name,
request.addr,
"".to_owned(),
)?;
results
.rpcs
.push(chat_rpc::request_agree(gid, rid, &friend));
@ -718,7 +724,13 @@ impl SyncEvent { @@ -718,7 +724,13 @@ impl SyncEvent {
if avatar.len() > 0 {
write_avatar_sync(&base, &gid, &request.gid, avatar)?;
}
let friend = Friend::from_request(&chat_db, request)?;
let friend = Friend::from_remote(
&chat_db,
request.gid,
request.name,
request.addr,
"".to_owned(),
)?;
results
.rpcs
.push(chat_rpc::request_agree(gid, rid, &friend));
@ -813,7 +825,7 @@ impl SyncEvent { @@ -813,7 +825,7 @@ impl SyncEvent {
&consensus_db,
&addr,
results,
account.height,
account.own_height,
account.event,
height,
eid,

24
src/group.rs

@ -228,10 +228,11 @@ impl Group { @@ -228,10 +228,11 @@ impl Group {
};
let account = self.account(gid)?;
if account.height != remote_height || account.event != remote_event {
results
.groups
.push((*gid, self.sync_message(gid, peer_id, 1, account.height)?));
if account.own_height != remote_height || account.event != remote_event {
results.groups.push((
*gid,
self.sync_message(gid, peer_id, 1, account.own_height)?,
));
}
// connect to others.
@ -432,6 +433,7 @@ impl Group { @@ -432,6 +433,7 @@ impl Group {
self.addr,
u.name.clone(),
u.avatar.clone(),
u.wallet.clone(),
))
} else {
Err(anyhow!("user missing."))
@ -535,7 +537,7 @@ impl Group { @@ -535,7 +537,7 @@ impl Group {
pub fn create_message(&self, gid: &GroupId, addr: Peer) -> Result<SendType> {
let user = self.clone_user(gid)?;
let account = self.account(gid)?;
let height = account.height;
let height = account.own_height;
let event = account.event;
let proof = self.prove_addr(gid, &addr.id)?;
let running = self.running(gid)?;
@ -558,7 +560,7 @@ impl Group { @@ -558,7 +560,7 @@ impl Group {
pub fn connect_message(&self, gid: &GroupId, addr: Peer) -> Result<SendType> {
let account = self.account(gid)?;
let height = account.height;
let height = account.own_height;
let event = account.event;
let data = bincode::serialize(&GroupConnect::Connect(height, event)).unwrap_or(vec![]);
Ok(SendType::Connect(0, addr, data))
@ -566,7 +568,7 @@ impl Group { @@ -566,7 +568,7 @@ impl Group {
pub fn connect_result(&self, gid: &GroupId, addr: Peer) -> Result<SendType> {
let account = self.account(gid)?;
let height = account.height;
let height = account.own_height;
let event = account.event;
let data = bincode::serialize(&GroupConnect::Connect(height, event)).unwrap_or(vec![]);
Ok(SendType::Result(0, addr, true, false, data))
@ -574,7 +576,7 @@ impl Group { @@ -574,7 +576,7 @@ impl Group {
pub fn agree_message(&self, gid: &GroupId, addr: Peer) -> Result<SendType> {
let account = self.account(gid)?;
let height = account.height;
let height = account.own_height;
let event = account.event;
let me = self.clone_user(gid)?;
let proof = self.prove_addr(gid, &addr.id)?;
@ -649,7 +651,7 @@ impl Group { @@ -649,7 +651,7 @@ impl Group {
let account = self.account_mut(gid)?;
let pre_event = account.event;
let eheight = account.height + 1;
let eheight = account.own_height + 1;
let eid = event.generate_event_id();
let db = consensus_db(&base, gid)?;
@ -768,7 +770,7 @@ impl GroupEvent { @@ -768,7 +770,7 @@ impl GroupEvent {
let remote_height = ancestors.last().map(|v| *v).unwrap_or(0);
let remote_event = hashes.last().map(|v| *v).unwrap_or(EventId::default());
if account.height != remote_height || account.event != remote_event {
if account.own_height != remote_height || account.event != remote_event {
// check ancestor and merge.
let db = consensus_db(&group.base, &gid)?;
let ours = crate::consensus::Event::get_assign_hash(&db, &ancestors)?;
@ -819,7 +821,7 @@ impl GroupEvent { @@ -819,7 +821,7 @@ impl GroupEvent {
} else {
results.groups.push((
gid,
group.sync_message(&gid, addr, remote_height, account.height)?,
group.sync_message(&gid, addr, remote_height, account.own_height)?,
));
}
}

4
src/migrate/account.rs

@ -12,7 +12,9 @@ pub(super) const ACCOUNT_VERSIONS: [&str; 13] = [ @@ -12,7 +12,9 @@ pub(super) const ACCOUNT_VERSIONS: [&str; 13] = [
encrypt TEXT NOT NULL,
mnemonic TEXT NOT NULL,
avatar TEXT NOT NULL,
height INTEGER NOT NULL,
wallet TEXT NOT NULL,
pub_height INTEGER NOT NULL,
own_height INTEGER NOT NULL,
event TEXT NOT NULL,
datetime INTEGER NOT NULL);",
"CREATE TABLE IF NOT EXISTS migrates(

1
src/migrate/chat.rs

@ -6,6 +6,7 @@ pub(super) const CHAT_VERSIONS: [&str; 3] = [ @@ -6,6 +6,7 @@ pub(super) const CHAT_VERSIONS: [&str; 3] = [
addr TEXT NOT NULL,
name TEXT NOT NULL,
wallet TEXT,
height INTEGER NOT NULL,
remark TEXT,
is_closed INTEGER NOT NULL,
datetime INTEGER NOT NULL);",

Loading…
Cancel
Save