Skip to content

Commit

Permalink
logout / remove account
Browse files Browse the repository at this point in the history
  • Loading branch information
singpolyma committed Oct 6, 2024
1 parent be7f78b commit 2738faf
Show file tree
Hide file tree
Showing 9 changed files with 109 additions and 0 deletions.
12 changes: 12 additions & 0 deletions snikket/Client.hx
Original file line number Diff line number Diff line change
Expand Up @@ -522,6 +522,18 @@ class Client extends EventEmitter {
});
}

/**
Destroy local data for this account
@param completely if true chats, messages, etc will be deleted as well
**/
public function logout(completely: Bool) {
// TODO: unregister from all push notifications
stream.disconnect();
// TODO: FAST invalidate https://xmpp.org/extensions/xep-0484.html#invalidation
persistence.removeAccount(accountId(), completely);
}

/**
Sets the password to be used in response to the password needed event
Expand Down
1 change: 1 addition & 0 deletions snikket/GenericStream.hx
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ abstract class GenericStream extends EventEmitter {
/* Connections and streams */

abstract public function connect(jid:String, sm:Null<BytesData>):Void;
abstract public function disconnect():Void;
abstract public function sendStanza(stanza:Stanza):Void;
abstract public function newId():String;
abstract public function onIq(type:IqRequestType, tag:String, xmlns:String, handler:(Stanza)->IqResult):Void;
Expand Down
1 change: 1 addition & 0 deletions snikket/Persistence.hx
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ interface Persistence {
public function getCaps(ver:String, callback: (Null<Caps>)->Void):Void;
public function storeLogin(login:String, clientId:String, displayName:String, token:Null<String>):Void;
public function getLogin(login:String, callback:(clientId:Null<String>, token:Null<String>, fastCount: Int, displayName:Null<String>)->Void):Void;
public function removeAccount(accountId: String, completely:Bool):Void;
public function storeStreamManagement(accountId:String, data:BytesData):Void;
public function getStreamManagement(accountId:String, callback: (Null<BytesData>)->Void):Void;
public function storeService(accountId:String, serviceId:String, name:Null<String>, node:Null<String>, caps:Caps):Void;
Expand Down
5 changes: 5 additions & 0 deletions snikket/persistence/Custom.hx
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,11 @@ class Custom implements Persistence {
backing.storeStreamManagement(accountId, sm);
}

@HaxeCBridge.noemit
public function removeAccount(accountId:String, completely:Bool) {
backing.removeAccount(accountId, completely);
}

@HaxeCBridge.noemit
public function getStreamManagement(accountId:String, callback: (BytesData)->Void) {
backing.getStreamManagement(accountId, callback);
Expand Down
3 changes: 3 additions & 0 deletions snikket/persistence/Dummy.hx
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,9 @@ class Dummy implements Persistence {
callback(null, null, 0, null);
}

@HaxeCBridge.noemit
public function removeAccount(accountId:String, completely:Bool) { }

@HaxeCBridge.noemit
public function storeStreamManagement(accountId:String, sm:BytesData) { }

Expand Down
26 changes: 26 additions & 0 deletions snikket/persistence/Sqlite.hx
Original file line number Diff line number Diff line change
Expand Up @@ -412,6 +412,32 @@ class Sqlite implements Persistence {
callback(null, null, 0, null);
}

@HaxeCBridge.noemit
public function removeAccount(accountId:String, completely:Bool) {
var q = new StringBuf();
q.add("DELETE FROM logins WHERE login=");
db.addValue(q, accountId);
db.request(q.toString());
// TODO stream managemento

if (!completely) return;

var q = new StringBuf();
q.add("DELETE FROM messages WHERE account_id=");
db.addValue(q, accountId);
db.request(q.toString());

var q = new StringBuf();
q.add("DELETE FROM chats WHERE account_id=");
db.addValue(q, accountId);
db.request(q.toString());

var q = new StringBuf();
q.add("DELETE FROM services WHERE account_id=");
db.addValue(q, accountId);
db.request(q.toString());
}

@HaxeCBridge.noemit
public function storeStreamManagement(accountId:String, sm:BytesData) {
// TODO
Expand Down
48 changes: 48 additions & 0 deletions snikket/persistence/browser.js
Original file line number Diff line number Diff line change
Expand Up @@ -592,6 +592,54 @@ const browser = (dbname, tokenize, stemmer) => {
});
},

removeAccount(account, completely) {
const tx = db.transaction(["keyvaluepairs", "services", "messages", "chats", "reactions"], "readwrite");
const store = tx.objectStore("keyvaluepairs");
store.delete("login:clientId:" + account);
store.delete("login:token:" + account);
store.delete("login:fastCount:" + account);
store.delete("fn:" + account);
store.delete("sm:" + account);

if (!completely) return;

const servicesStore = tx.objectStore("services");
const servicesCursor = servicesStore.openCursor(IDBKeyRange.bound([account], [account, []]));
servicesCursor.onsuccess = (event) => {
if (event.target.result) {
event.target.result.delete();
event.target.result.continue();
}
};

const messagesStore = tx.objectStore("messages");
const messagesCursor = messagesStore.openCursor(IDBKeyRange.bound([account], [account, []]));
messagesCursor.onsuccess = (event) => {
if (event.target.result) {
event.target.result.delete();
event.target.result.continue();
}
};

const chatsStore = tx.objectStore("chats");
const chatsCursor = chatsStore.openCursor(IDBKeyRange.bound([account], [account, []]));
chatsCursor.onsuccess = (event) => {
if (event.target.result) {
event.target.result.delete();
event.target.result.continue();
}
};

const reactionsStore = tx.objectStore("reactions");
const reactionsCursor = reactionsStore.openCursor(IDBKeyRange.bound([account], [account, []]));
reactionsCursor.onsuccess = (event) => {
if (event.target.result) {
event.target.result.delete();
event.target.result.continue();
}
};
},

storeService(account, serviceId, name, node, caps) {
this.storeCaps(caps);

Expand Down
6 changes: 6 additions & 0 deletions snikket/streams/XmppJsStream.hx
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ extern class XmppJsScramSha1 {
extern class XmppJsClient {
function new(options:Dynamic);
function start():Promise<Dynamic>;
function stop():Promise<Dynamic>;
function on(eventName:String, callback:(Dynamic)->Void):Void;
function send(stanza:XmppJsXml):Void;
var jid:XmppJsJID;
Expand Down Expand Up @@ -277,6 +278,11 @@ class XmppJsStream extends GenericStream {
resolveConnectionURI(this.jid.domain, this.connectWithURI);
}

public function disconnect() {
if (client == null) return;
client.stop();
}

private function convertFromStanza(el:Stanza):XmppJsXml {
var xml = new XmppJsXml(el.name, el.attr);
if(el.children.length > 0) {
Expand Down
7 changes: 7 additions & 0 deletions snikket/streams/XmppStropheStream.hx
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,9 @@ extern class StropheConn {
userdata:RawPointer<Void>
):cpp.Int32;

@:native("xmpp_disconnect")
static function disconnect(conn:StropheConn):Void;

@:native("xmpp_handler_add")
static function handler_add(
conn:StropheConn,
Expand Down Expand Up @@ -268,6 +271,10 @@ class XmppStropheStream extends GenericStream {
poll();
}

public function disconnect() {
StropheConn.disconnect(conn);
}

private function poll() {
sys.thread.Thread.current().events.run(() -> {
StropheCtx.run_once(ctx, 1);
Expand Down

0 comments on commit 2738faf

Please sign in to comment.