--- chats-fix-negative-unread


UPDATE chats
SET client_unread = (
  SELECT count(*)
  FROM chat_messages m
  WHERE m.chat_id = chats.id
        AND m.read = FALSE
        AND m.author = 'user'
);


UPDATE chats
SET total_members = (
  SELECT count(*)
  FROM chat_members m
  WHERE m.chat_id = chats.id
        AND m.present = TRUE
);
