+
+
+function member_of($c) {
+
+ $r = q("SELECT `group`.`name`, `group`.`id` FROM `group` INNER JOIN `group_member` ON `group_member`.`gid` = `group`.`id` WHERE `group_member`.`contact-id` = %d AND `group`.`deleted` = 0 ORDER BY `group`.`name` ASC ",
+ intval($c)
+ );
+
+ return $r;
+
+}
+
+function groups_containing($uid,$c) {
+
+ $r = q("SELECT `gid` FROM `group_member` WHERE `uid` = %d AND `group_member`.`contact-id` = %d ",
+ intval($uid),
+ intval($c)
+ );
+
+ $ret = array();
+ if (dbm::is_result($r)) {
+ foreach($r as $rr)
+ $ret[] = $rr['gid'];
+ }
+
+ return $ret;
+}
+/**
+ * @brief count unread group items
+ *
+ * Count unread items of each groups
+ *
+ * @return array
+ * 'id' => group id
+ * 'name' => group name
+ * 'count' => counted unseen group items
+ *
+ */
+function groups_count_unseen() {
+
+ $r = q("SELECT `group`.`id`, `group`.`name`,
+ (SELECT COUNT(*) FROM `item` FORCE INDEX (`uid_unseen_contactid`)
+ WHERE `uid` = %d AND `unseen` AND
+ `contact-id` IN (SELECT `contact-id` FROM `group_member`
+ WHERE `group_member`.`gid` = `group`.`id` AND `group_member`.`uid` = %d)) AS `count`
+ FROM `group` WHERE `group`.`uid` = %d;",
+ intval(local_user()),
+ intval(local_user()),
+ intval(local_user())
+ );
+
+ return $r;
+}
+
+/**
+ * @brief Returns the default group for a given user and network
+ *
+ * @param int $uid User id
+ * @param string $network network name
+ *
+ * @return int group id
+ */
+function get_default_group($uid, $network = "") {
+
+ $default_group = 0;
+
+ if ($network == NETWORK_OSTATUS)
+ $default_group = get_pconfig($uid, "ostatus", "default_group");
+
+ if ($default_group != 0)
+ return $default_group;
+
+ $g = q("SELECT `def_gid` FROM `user` WHERE `uid` = %d LIMIT 1", intval($uid));
+ if($g && intval($g[0]["def_gid"]))
+ $default_group = $g[0]["def_gid"];
+
+ return $default_group;
+}