Browse Source

store WA state in database (finally)

master
eta 5 years ago
parent
commit
2437fb924e
  1. 1
      migrations/20180822195900_wabis/down.sql
  2. 2
      migrations/20180822195900_wabis/up.sql
  3. 4
      src/contact.rs
  4. 2
      src/contact_common.rs
  5. 20
      src/insp_s2s.rs
  6. 6
      src/models.rs
  7. 1
      src/schema.rs
  8. 16
      src/store.rs
  9. 2
      src/whatsapp.rs

1
migrations/20180822195900_wabis/down.sql

@ -0,0 +1 @@ @@ -0,0 +1 @@
ALTER TABLE recipients DROP COLUMN whatsapp;

2
migrations/20180822195900_wabis/up.sql

@ -0,0 +1,2 @@ @@ -0,0 +1,2 @@
ALTER TABLE recipients ADD COLUMN whatsapp BOOL NOT NULL DEFAULT false;
ALTER TABLE messages DROP CONSTRAINT IF EXISTS messages_concat_if_pdu;

4
src/contact.rs

@ -177,6 +177,7 @@ impl ContactManager { @@ -177,6 +177,7 @@ impl ContactManager {
"!wa" => {
self.wa_mode = !self.wa_mode;
let state = if self.wa_mode { "ENABLED" } else { "DISABLED" };
self.store.update_recipient_wa(&self.addr, self.wa_mode)?;
self.irc.0.send_notice(&self.admin, &format!("WhatsApp mode: {}", state))?;
},
"!die" => {
@ -297,6 +298,7 @@ impl ContactManager { @@ -297,6 +298,7 @@ impl ContactManager {
}
pub fn new(recip: Recipient, p: InitParameters<IrcClientConfig>) -> impl Future<Item = Self, Error = Error> {
let store = p.store;
let wa_mode = recip.whatsapp;
let addr = match util::un_normalize_address(&recip.phone_number)
.ok_or(format_err!("invalid num {} in db", recip.phone_number)) {
Ok(r) => r,
@ -340,7 +342,7 @@ impl ContactManager { @@ -340,7 +342,7 @@ impl ContactManager {
irc_stream,
id: false,
connected: false,
wa_mode: false,
wa_mode,
// Assume admin is online to start with
admin_is_online: true,
presence: None,

2
src/contact_common.rs

@ -28,7 +28,7 @@ pub trait ContactManagerManager { @@ -28,7 +28,7 @@ pub trait ContactManagerManager {
else {
let nick = util::make_nick_for_address(&addr);
info!("Creating new recipient for {} (nick {})", addr, nick);
let recip = self.store().store_recipient(&addr, &nick)?;
let recip = self.store().store_recipient(&addr, &nick, false)?;
self.setup_recipient(recip)?;
}
Ok(())

20
src/insp_s2s.rs

@ -96,12 +96,13 @@ impl Future for InspLink { @@ -96,12 +96,13 @@ impl Future for InspLink {
impl ContactManagerManager for InspLink {
fn setup_contact_for(&mut self, recip: Recipient, addr: PduAddress) -> Result<()> {
trace!("setting up contact for recip #{}: {}", recip.id, addr);
let user = InspUser::new_from_recipient(addr.clone(), recip.nick, &self.cfg.server_name);
let host = self.host_for_wa(recip.whatsapp);
let user = InspUser::new_from_recipient(addr.clone(), recip.nick, &host);
let uuid = self.new_user(user)?;
self.contacts.insert(addr.clone(), InspContact {
uuid: uuid.clone(),
channels: vec![],
wa_mode: false
wa_mode: recip.whatsapp
});
self.contacts_uuid_pdua.insert(uuid, addr.clone());
if self.state == LinkState::Linked {
@ -223,6 +224,14 @@ impl InspLink { @@ -223,6 +224,14 @@ impl InspLink {
let codec = IrcCodec::new("utf8")?;
Ok((addr, codec))
}
fn host_for_wa(&self, wa: bool) -> String {
if wa {
format!("wa.{}", self.cfg.server_name)
}
else {
format!("s.{}", self.cfg.server_name)
}
}
pub fn new(p: InitParameters<InspConfig>) -> impl Future<Item = Self, Error = Error> {
let store = p.store;
let cfg = p.cfg2.clone();
@ -288,11 +297,14 @@ impl InspLink { @@ -288,11 +297,14 @@ impl InspLink {
self.store.update_recipient_nick(&addr, &msg[1])?;
},
"!wa" => {
let state = {
let (is_wa, state) = {
let mut ct = self.contacts.get_mut(&addr).unwrap();
ct.wa_mode = !ct.wa_mode;
if ct.wa_mode { "ENABLED" } else { "DISABLED" }
self.store.update_recipient_wa(&addr, ct.wa_mode)?;
(ct.wa_mode, if ct.wa_mode { "ENABLED" } else { "DISABLED" })
};
let host = self.host_for_wa(is_wa);
self.outbox.push(Message::new(Some(&uid), "FHOST", vec![&host], None)?);
self.contact_message(&uid, "NOTICE", &auid, &format!("WhatsApp mode: {}", state))?;
},
"!die" => {

6
src/models.rs

@ -5,13 +5,15 @@ use serde_json::Value; @@ -5,13 +5,15 @@ use serde_json::Value;
pub struct Recipient {
pub id: i32,
pub phone_number: String,
pub nick: String
pub nick: String,
pub whatsapp: bool
}
#[derive(Insertable)]
#[table_name="recipients"]
pub struct NewRecipient<'a> {
pub phone_number: &'a str,
pub nick: &'a str
pub nick: &'a str,
pub whatsapp: bool
}
#[derive(Queryable, Debug)]
pub struct Message {

1
src/schema.rs

@ -25,6 +25,7 @@ table! { @@ -25,6 +25,7 @@ table! {
id -> Int4,
phone_number -> Varchar,
nick -> Varchar,
whatsapp -> Bool,
}
}

16
src/store.rs

@ -106,13 +106,14 @@ impl Store { @@ -106,13 +106,14 @@ impl Store {
};
Ok(res)
}
pub fn store_recipient(&mut self, addr: &PduAddress, nick: &str) -> Result<Recipient> {
pub fn store_recipient(&mut self, addr: &PduAddress, nick: &str, wa: bool) -> Result<Recipient> {
use schema::recipients;
let num = util::normalize_address(addr);
let nr = NewRecipient {
phone_number: &num,
nick
nick,
whatsapp: wa
};
let conn = self.inner.get()?;
@ -132,6 +133,17 @@ impl Store { @@ -132,6 +133,17 @@ impl Store {
.execute(&*conn)?;
Ok(())
}
pub fn update_recipient_wa(&mut self, addr: &PduAddress, wa: bool) -> Result<()> {
use schema::recipients::dsl::*;
let conn = self.inner.get()?;
let num = util::normalize_address(addr);
::diesel::update(recipients)
.filter(phone_number.eq(num))
.set(whatsapp.eq(wa))
.execute(&*conn)?;
Ok(())
}
pub fn get_recipient_by_addr_opt(&mut self, addr: &PduAddress) -> Result<Option<Recipient>> {
use schema::recipients::dsl::*;
let conn = self.inner.get()?;

2
src/whatsapp.rs

@ -345,7 +345,7 @@ impl WhatsappManager { @@ -345,7 +345,7 @@ impl WhatsappManager {
}
}
info!("Creating new (WA) recipient for {} (nick {})", addr, nick);
self.store.store_recipient(&addr, &nick)?
self.store.store_recipient(&addr, &nick, true)?
};
self.cf_tx.unbounded_send(ContactFactoryCommand::MakeContact(addr))
.unwrap();

Loading…
Cancel
Save