--- og/og.module	2005-04-15 11:56:46.000000000 -0700
+++ og-mail-factored/og.module	2005-08-04 17:01:15.624158385 -0700
@@ -229,10 +229,43 @@
   print theme('page', $output);
 }
 
-function og_approve($gid, $uid) {
+/**
+ * Low level functions for managing subscriptions
+ *
+ *
+ * @author
+ * @param
+ * @return
+ * @exeption
+ * @see
+ */
+
+//TO DO: factor the code
+
+function og_create_subscription($gid, $uid){
+ if (node_access('update', array('nid' => $gid, 'status' => 1))) {
+    $sql = 
+      "REPLACE INTO {node_access} (nid, gid, realm, grant_view) " .
+      "VALUES (%d, %d, 'og_uid', 1)";
+    db_query($sql, $gid, $uid);
+    watchdog('user', "User $uid subscribed to organic group $gid");
+ }
+
+}
+
+function og_delete_subscription($gid, $uid){
   if (node_access('update', array('nid' => $gid, 'status' => 1))) {
-    $sql = "REPLACE INTO {node_access} (nid, gid, realm, grant_view) VALUES (%d, %d, 'og_uid', 1)";
+    $sql = 
+      "DELETE FROM {node_access} WHERE nid=%d " .
+      "AND gid=%d AND realm='og_uid'";
     db_query($sql, $gid, $uid);
+    watchdog('user', "User $uid unsubscribed from organic group $gid");
+  }
+}
+
+function og_approve($gid, $uid) {
+  if (node_access('update', array('nid' => $gid, 'status' => 1))) {
+    og_create_subscription($gid, $uid);
     drupal_set_message(t('Subscription request approved.'));
     $node = node_load(array('nid' => $gid));
     $subj = t("Subscription request approved for '%title'", array('%title' => $node->title));
@@ -250,8 +283,7 @@
 
 function og_deny($gid, $uid) {
   if (node_access('update', array('nid' => $gid, 'status' => 1))) {
-    $sql = "DELETE FROM {node_access} WHERE nid=%d AND gid=%d AND realm='og_uid'";
-    db_query($sql, $gid, $uid);
+    og_delete_subscription($gid, $uid);
     drupal_set_message(t('Subscription request denied.'));
     $subj = t('Subscription request denied for %title', array('title' => $node->title));
     $body = t('Sorry, your subscription request was denied.');
@@ -437,9 +469,7 @@
     // only admins can unsubscribe another person
     drupal_access_denied();
   }
-
-  $sql = "DELETE FROM {node_access}  WHERE gid = %d AND nid = %d AND realm = 'og_uid'";
-  db_query($sql, $uid, $gid);
+  og_delete_subscription($gid, $uid);
   drupal_set_message(t('User unsubscribed from group.'));
   drupal_goto("node/$gid");
 }
