Skip to content

Commit

Permalink
Merge pull request #189 from novaforgood/Max/email-whitelist
Browse files Browse the repository at this point in the history
Add chat stats
  • Loading branch information
legitmaxwu authored Jan 22, 2024
2 parents bcbc6a7 + 51e5e8b commit 996c798
Show file tree
Hide file tree
Showing 28 changed files with 5,399 additions and 1,294 deletions.
2 changes: 2 additions & 0 deletions hasura/docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ services:
restart: always
volumes:
- db_data:/var/lib/postgresql/data
ports:
- "5432:5432"
environment:
POSTGRES_PASSWORD: postgrespassword
graphql-engine:
Expand Down
1 change: 1 addition & 0 deletions hasura/metadata/databases/databases.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,4 @@
isolation_level: read-committed
use_prepared_statements: false
tables: "!include mentorcenter/tables/tables.yaml"
functions: "!include mentorcenter/functions/functions.yaml"
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
- "!include public_get_chat_stats.yaml"
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
function:
name: get_chat_stats
schema: public
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
table:
name: chat_stats
schema: public
object_relationships:
- name: profile
using:
manual_configuration:
column_mapping:
profile_id: id
insertion_order: null
remote_table:
name: profile
schema: public
select_permissions:
- role: user
permission:
columns:
- profile_id
- rooms_messaged
- rooms_read
- total_rooms
filter:
_or:
- _and:
- _exists:
_table:
name: profile_to_profile_role_flattened
schema: public
_where:
_and:
- user_id:
_eq: X-Hasura-User-Id
- space_id:
_eq: X-Hasura-Space-Id
- profile_role:
_eq: Admin
- profile:
space_id:
_eq: X-Hasura-Space-Id
1 change: 1 addition & 0 deletions hasura/metadata/databases/mentorcenter/tables/tables.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
- "!include public_chat_message.yaml"
- "!include public_chat_message_admin_view.yaml"
- "!include public_chat_room.yaml"
- "!include public_chat_stats.yaml"
- "!include public_connection_request.yaml"
- "!include public_connection_request_status.yaml"
- "!include public_event_profile_view.yaml"
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
-- Could not auto-generate a down migration.
-- Please write an appropriate down migration for the SQL below:
-- CREATE TABLE chat_stats (
-- profile_id UUID,
-- rooms_messaged bigint,
-- rooms_read bigint,
-- total_rooms bigint
-- );
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
CREATE TABLE chat_stats (
profile_id UUID,
rooms_messaged bigint,
rooms_read bigint,
total_rooms bigint
);
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
-- Could not auto-generate a down migration.
-- Please write an appropriate down migration for the SQL below:
-- CREATE OR REPLACE FUNCTION get_chat_stats(sp_id UUID DEFAULT NULL, after TIMESTAMP DEFAULT NULL)
-- RETURNS SETOF chat_stats AS $$
--
-- BEGIN
-- -- Return the query using the determined cutoff date
-- RETURN QUERY
-- SELECT
-- p.id AS profile_id,
-- COUNT(DISTINCT cm.chat_room_id) AS rooms_messaged,
-- COUNT(DISTINCT CASE WHEN pc.latest_read_chat_message_id IS NOT NULL THEN pc.chat_room_id END) AS rooms_read,
-- COUNT(DISTINCT pc.chat_room_id) AS total_rooms
-- FROM
-- profile p
-- LEFT JOIN
-- profile_to_chat_room pc ON p.id = pc.profile_id
-- LEFT JOIN
-- chat_message cm ON p.id = cm.sender_profile_id AND pc.chat_room_id = cm.chat_room_id
-- JOIN
-- chat_room ON pc.chat_room_id = chat_room.id
-- JOIN
-- "user" ON "user".id = p.user_id
-- JOIN
-- chat_intro ci ON chat_room.chat_intro_id = ci.id
-- WHERE
-- (ci.space_id = sp_id OR sp_id IS NULL)
-- AND (ci.created_at >= after OR after IS NULL)
-- AND "user"."type" = 'User'
-- GROUP BY
-- p.id;
-- END;
-- $$ LANGUAGE plpgsql STABLE;
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
CREATE OR REPLACE FUNCTION get_chat_stats(sp_id UUID DEFAULT NULL, after TIMESTAMP DEFAULT NULL)
RETURNS SETOF chat_stats AS $$

BEGIN
-- Return the query using the determined cutoff date
RETURN QUERY
SELECT
p.id AS profile_id,
COUNT(DISTINCT cm.chat_room_id) AS rooms_messaged,
COUNT(DISTINCT CASE WHEN pc.latest_read_chat_message_id IS NOT NULL THEN pc.chat_room_id END) AS rooms_read,
COUNT(DISTINCT pc.chat_room_id) AS total_rooms
FROM
profile p
LEFT JOIN
profile_to_chat_room pc ON p.id = pc.profile_id
LEFT JOIN
chat_message cm ON p.id = cm.sender_profile_id AND pc.chat_room_id = cm.chat_room_id
JOIN
chat_room ON pc.chat_room_id = chat_room.id
JOIN
"user" ON "user".id = p.user_id
JOIN
chat_intro ci ON chat_room.chat_intro_id = ci.id
WHERE
(ci.space_id = sp_id OR sp_id IS NULL)
AND (ci.created_at >= after OR after IS NULL)
AND "user"."type" = 'User'
GROUP BY
p.id;
END;
$$ LANGUAGE plpgsql STABLE;
25 changes: 25 additions & 0 deletions mobile/src/lib/gql/from-shared/admin-dashboard/dashboard.graphql
Original file line number Diff line number Diff line change
Expand Up @@ -103,3 +103,28 @@ mutation UpdateSpaceTagCategory(
rigid_select
}
}

query ChatStats(
$space_id: uuid
$after: timestamp
$order_by: [chat_stats_order_by!]
) {
get_chat_stats(
args: { sp_id: $space_id, after: $after }
order_by: $order_by
) {
profile_id
rooms_messaged
rooms_read
total_rooms
profile {
id
attributes
user {
id
full_name
email
}
}
}
}
25 changes: 25 additions & 0 deletions shared/graphql/admin-dashboard/dashboard.graphql
Original file line number Diff line number Diff line change
Expand Up @@ -103,3 +103,28 @@ mutation UpdateSpaceTagCategory(
rigid_select
}
}

query ChatStats(
$space_id: uuid
$after: timestamp
$order_by: [chat_stats_order_by!]
) {
get_chat_stats(
args: { sp_id: $space_id, after: $after }
order_by: $order_by
) {
profile_id
rooms_messaged
rooms_read
total_rooms
profile {
id
attributes
user {
id
full_name
email
}
}
}
}
3 changes: 2 additions & 1 deletion web/components/account-settings/ProfileSettings.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,8 @@ export function ProfileSettings() {
<CheckBox
label={`Opt in to intros (admins can randomly match you with other members in a group chat)`}
checked={settings?.enableChatIntros ?? false}
onChange={async (newVal) => {
onChange={async (e) => {
const newVal = e.target?.checked ?? false;
setMustSave(true);

if (newVal) {
Expand Down
4 changes: 3 additions & 1 deletion web/components/account-settings/UserSettings.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,9 @@ export function UserSettings() {
<CheckBox
label={`Disable email notifications`}
checked={settings?.disableEmailNotifications ?? false}
onChange={(newVal) => {
onChange={(e) => {
const newVal = e.target?.checked ?? false;

setMustSave(true);
setSettings((prev) => ({
...prev,
Expand Down
Loading

0 comments on commit 996c798

Please sign in to comment.