From 4bef26e64d1a3310ded19a2198fb4c79417196a8 Mon Sep 17 00:00:00 2001
From: leo <leopold@lchappuis.fr>
Date: Wed, 12 Feb 2025 11:36:53 -0500
Subject: [PATCH] presence: add signals and API functions

Change-Id: I7e43234ebf1ebe3f831e6b886f333b8a53e5c3a2
---
 server/src/jamid/jami-signal-interfaces.ts | 30 ++++++++++
 server/src/jamid/jami-signal.ts            |  7 +++
 server/src/jamid/jami-swig.ts              |  7 +++
 server/src/jamid/jamid.ts                  | 69 ++++++++++++++++++++++
 4 files changed, 113 insertions(+)

diff --git a/server/src/jamid/jami-signal-interfaces.ts b/server/src/jamid/jami-signal-interfaces.ts
index 36d8fa79..8bd2557a 100644
--- a/server/src/jamid/jami-signal-interfaces.ts
+++ b/server/src/jamid/jami-signal-interfaces.ts
@@ -231,3 +231,33 @@ export interface MessageFound {
   conversationId: string
   messages: Message[]
 }
+
+export interface ISubscriptionStateChanged {
+  accountId: string
+  buddyUri: string
+  state: number
+}
+
+export interface INearbyPeerNotification {
+  accountId: string
+  buddyUri: string
+  state: number
+  displayName: string
+}
+
+export interface INewBuddyNotification {
+  accountId: string
+  buddyUri: string
+  status: number
+  lineStatus: string
+}
+
+export interface INewServerSubscriptionRequest {
+  remote: string
+}
+
+export interface IServerError {
+  accountId: string
+  error: string
+  msg: string
+}
diff --git a/server/src/jamid/jami-signal.ts b/server/src/jamid/jami-signal.ts
index 5a769e20..7e8e18f9 100644
--- a/server/src/jamid/jami-signal.ts
+++ b/server/src/jamid/jami-signal.ts
@@ -67,4 +67,11 @@ export enum JamiSignal {
 
   // libjami::DataTransferSignal
   DataTransferEvent = 'DataTransferEvent',
+
+  // libjami::PresenceSignal
+  SubscriptionStateChanged = 'SubscriptionStateChanged',
+  NearbyPeerNotification = 'NearbyPeerNotification',
+  NewBuddyNotification = 'NewBuddyNotification',
+  NewServerSubscriptionRequest = 'NewServerSubscriptionRequest',
+  ServerError = 'ServerError',
 }
diff --git a/server/src/jamid/jami-swig.ts b/server/src/jamid/jami-swig.ts
index 21205bfb..9e794b8b 100644
--- a/server/src/jamid/jami-swig.ts
+++ b/server/src/jamid/jami-swig.ts
@@ -172,6 +172,13 @@ export interface JamiSwig {
     progress_out: number,
   ): DataTransferError
 
+  // Presence
+  publish(accountId: string, status: boolean, note: string): void
+  answerServerRequest(uri: string, flag: boolean): void
+  subscribeBuddy(accountId: string, uri: string, flag: boolean): void
+  getSubscriptions(accountId: string): VectMap
+  setSubscriptions(accountId: string, uris: string[]): void
+
   // IntVect: Constructable<IntVect>;
   // UintVect: Constructable<UintVect>;
   // FloatVect: Constructable<FloatVect>;
diff --git a/server/src/jamid/jamid.ts b/server/src/jamid/jamid.ts
index 10770a06..6a43148c 100644
--- a/server/src/jamid/jamid.ts
+++ b/server/src/jamid/jamid.ts
@@ -65,6 +65,11 @@ import {
   DataTransferEvent,
   DeviceRevocationEnded,
   IncomingAccountMessage,
+  INearbyPeerNotification,
+  INewBuddyNotification,
+  INewServerSubscriptionRequest,
+  IServerError,
+  ISubscriptionStateChanged,
   KnownDevicesChanged,
   MessageFound,
   NameRegistrationEnded,
@@ -284,6 +289,25 @@ export class Jamid {
     handlers.DeviceRevocationEnded = (accountId: string, device: string, state: DeviceRevocationState) =>
       onDeviceRevocationEnded.next({ accountId, device, state })
 
+    const onSubscriptionStateChanged = new Subject<ISubscriptionStateChanged>()
+    handlers.SubscriptionStateChanged = (accountId: string, buddyUri: string, state: number) =>
+      onSubscriptionStateChanged.next({ accountId, buddyUri, state })
+
+    const onNearbyPeerNotification = new Subject<INearbyPeerNotification>()
+    handlers.NearbyPeerNotification = (accountId: string, buddyUri: string, state: number, displayName: string) =>
+      onNearbyPeerNotification.next({ accountId, buddyUri, state, displayName })
+
+    const onNewBuddyNotification = new Subject<INewBuddyNotification>()
+    handlers.NewBuddyNotification = (accountId: string, buddyUri: string, status: number, lineStatus: string) =>
+      onNewBuddyNotification.next({ accountId, buddyUri, status, lineStatus })
+
+    const onNewServerSubscriptionRequest = new Subject<INewServerSubscriptionRequest>()
+    handlers.NewServerSubscriptionRequest = (remote: string) => onNewServerSubscriptionRequest.next({ remote })
+
+    const onServerError = new Subject<IServerError>()
+    handlers.ServerError = (accountId: string, error: string, msg: string) =>
+      onServerError.next({ accountId, error, msg })
+
     // Expose all signals in an events object to allow other handlers to subscribe after jamiSwig.init()
     this.events = {
       onAccountsChanged: onAccountsChanged.asObservable(),
@@ -315,6 +339,11 @@ export class Jamid {
       onUserSearchEnded: onUserSearchEnded.asObservable(),
       onDeviceRevocationEnded: onDeviceRevocationEnded.asObservable(),
       onMessageFound: onMessageFound.asObservable(),
+      onSubscriptionStateChanged: onSubscriptionStateChanged.asObservable(),
+      onNearbyPeerNotification: onNearbyPeerNotification.asObservable(),
+      onNewBuddyNotification: onNewBuddyNotification.asObservable(),
+      onNewServerSubscriptionRequest: onNewServerSubscriptionRequest.asObservable(),
+      onServerError: onServerError.asObservable(),
     }
 
     this.setupSignalHandlers()
@@ -735,7 +764,47 @@ export class Jamid {
     )
   }
 
+  publish(accountId: string, status: boolean, note: string): void {
+    this.jamiSwig.publish(accountId, status, note)
+  }
+
+  answerServerRequest(uri: string, flag: boolean) {
+    this.jamiSwig.answerServerRequest(uri, flag)
+  }
+
+  subscribeBuddy(accountId: string, uri: string, flag: boolean): void {
+    this.jamiSwig.subscribeBuddy(accountId, uri, flag)
+  }
+
+  getSubscriptions(accountId: string) {
+    return vectMapToRecordArray(this.jamiSwig.getSubscriptions(accountId))
+  }
+
+  setSubscriptions(accountId: string, uris: string[]): void {
+    this.jamiSwig.setSubscriptions(accountId, uris)
+  }
+
   private setupSignalHandlers(): void {
+    this.events.onSubscriptionStateChanged.subscribe((signal) => {
+      log.debug('Received SubscriptionStateChanged:', JSON.stringify(signal))
+    })
+
+    this.events.onNearbyPeerNotification.subscribe((signal) => {
+      log.debug('Received NearbyPeerNotification:', JSON.stringify(signal))
+    })
+
+    this.events.onNewBuddyNotification.subscribe((signal) => {
+      log.debug('Received NewBuddyNotification:', JSON.stringify(signal))
+    })
+
+    this.events.onNewServerSubscriptionRequest.subscribe((signal) => {
+      log.debug('Received NewServerSubscriptionRequest:', JSON.stringify(signal))
+    })
+
+    this.events.onServerError.subscribe((signal) => {
+      log.debug('Received ServerError:', JSON.stringify(signal))
+    })
+
     this.events.onDataTransferEvent.subscribe((signal) => {
       log.debug('Received DataTransferEvent:', JSON.stringify(signal))
 
-- 
GitLab