You are reading a single comment by @Velocio and its replies. Click here to read the full conversation.
  • Might be easier to read that SQL as one file rather than as a diff of changes:
    https://github.com/microcosm-cc/microcosm/blob/master/models/updates.go#L329-L497

    --GetUpdates
    WITH m AS (
        SELECT m.microcosm_id
          FROM microcosms m
          LEFT JOIN permissions_cache p ON p.site_id = m.site_id
                                       AND p.item_type_id = 2
                                       AND p.item_id = m.microcosm_id
                                       AND p.profile_id = $2
               LEFT JOIN ignores_expanded i ON i.profile_id = $2
                                           AND i.item_type_id = 2
                                           AND i.item_id = m.microcosm_id
         WHERE m.site_id = $1
           AND m.is_deleted IS NOT TRUE
           AND m.is_moderated IS NOT TRUE
           AND i.profile_id IS NULL
           AND (
                   (p.can_read IS NOT NULL AND p.can_read IS TRUE)
                OR (get_effective_permissions($1,m.microcosm_id,2,m.microcosm_id,$2)).can_read IS TRUE
               )
    )
    SELECT total
          ,update_id
          ,for_profile_id
          ,update_type_id
          ,item_type_id
          ,item_id
          ,created_by
          ,created
          ,site_id
          ,has_unread(COALESCE(parent_item_type_id, item_type_id), COALESCE(parent_item_id, item_id), $2)
      FROM (
              SELECT COUNT(*) OVER() AS total
                    ,update_id
                    ,for_profile_id
                    ,update_type_id
                    ,item_type_id
                    ,item_id
                    ,created_by
                    ,created
                    ,site_id
                    ,parent_item_type_id
                    ,parent_item_id
                FROM (
                              -- 1;'new_comment';'When a comment has been posted in an item you are watching'
                              -- 4;'new_comment_in_huddle';'When you receive a new comment in a private message'
                              SELECT u.update_id
                                    ,u.for_profile_id
                                    ,u.update_type_id
                                    ,u.item_type_id
                                    ,u.item_id
                                    ,u.created_by
                                    ,u.created
                                    ,$1 AS site_id
                                    ,f.parent_item_type_id
                                    ,f.parent_item_id
                                FROM updates u
                                JOIN flags f ON f.item_type_id = u.item_type_id
                                            AND f.item_id = u.item_id
                                     JOIN (
                                              SELECT MAX(u.update_id) AS update_id
                                                    ,f.parent_item_type_id AS item_type_id
                                                    ,f.parent_item_id AS item_id
                                                FROM updates u
                                                     JOIN flags f ON f.item_type_id = u.item_type_id
                                                                 AND f.item_id = u.item_id
                                                LEFT JOIN ignores i ON i.profile_id = $2
                                                                   AND (
                                                                           (i.item_type_id = 3 AND i.item_id = u.created_by)
                                                                        OR (i.item_type_id = f.parent_item_type_id AND i.item_id = f.parent_item_id)
                                                                       )
                                                LEFT JOIN huddle_profiles hp ON hp.huddle_id = f.parent_item_id
                                                                            AND hp.profile_id = u.for_profile_id
                                                                            AND f.parent_item_type_id = 5
                                               WHERE u.for_profile_id = $2
                                                 AND i.profile_id IS NULL
                                                 AND u.update_type_id IN (1, 4)
                                                 AND f.item_is_deleted IS NOT TRUE
                                                 AND f.item_is_moderated IS NOT TRUE
                                                 AND f.parent_is_deleted IS NOT TRUE
                                                 AND f.parent_is_moderated IS NOT TRUE
                                                 AND (
                                                         f.microcosm_id IN (SELECT microcosm_id FROM m)
                                                      OR hp.profile_id = u.for_profile_id
                                                     )
                                               GROUP BY f.parent_item_type_id
                                                       ,f.parent_item_id
                                                       ,f.site_id
                                          ) r ON r.update_id = u.update_id
                                     JOIN watchers w ON w.profile_id = $2
                                                    AND w.item_type_id = r.item_type_id
                                                    AND w.item_id = r.item_id
                               UNION
                              -- 2;'reply_to_comment';'When a comment of yours is replied to'
                              -- 3;'mentioned';'When you are @mentioned in a comment'
                              SELECT u.update_id
                                    ,u.for_profile_id
                                    ,u.update_type_id
                                    ,u.item_type_id
                                    ,u.item_id
                                    ,u.created_by
                                    ,u.created
                                    ,$1 AS site_id
                                    ,u.parent_item_type_id
                                    ,u.parent_item_id
                                FROM updates u
                               WHERE update_id IN (
                                         SELECT MAX(u.update_id)
                                           FROM updates u
                                                JOIN flags f ON f.item_type_id = u.item_type_id
                                                            AND f.item_id = u.item_id
                                                LEFT JOIN huddle_profiles hp ON hp.huddle_id = f.parent_item_id
                                                                            AND hp.profile_id = u.for_profile_id
                                                                            AND f.parent_item_type_id = 5
                                                LEFT JOIN ignores i ON i.profile_id = $2
                                                                   AND (
                                                                           (i.item_type_id = 3 AND i.item_id = u.created_by)
                                                                        OR (i.item_type_id = f.parent_item_type_id AND i.item_id = f.parent_item_id)
                                                                       )
                                          WHERE u.for_profile_id = $2
                                            AND i.profile_id IS NULL
                                            AND (u.update_type_id = 2 OR u.update_type_id = 3) -- replies (2) & mentions (3)
                                            AND f.site_id = $1
                                            AND f.item_is_deleted IS NOT TRUE
                                            AND f.item_is_moderated IS NOT TRUE
                                            AND f.parent_is_deleted IS NOT TRUE
                                            AND f.parent_is_moderated IS NOT TRUE
                                            AND (
                                                    f.microcosm_id IN (SELECT microcosm_id FROM m)
                                                 OR hp.profile_id = u.for_profile_id
                                                )
                                          GROUP BY u.update_type_id
                                                  ,u.item_type_id
                                                  ,u.item_id
                                         )
                               UNION
                              -- 8;'new_item';'When a new item is created in a microcosm you are watching'
                              SELECT u.update_id
                                    ,u.for_profile_id
                                    ,u.update_type_id
                                    ,u.item_type_id
                                    ,u.item_id
                                    ,u.created_by
                                    ,u.created
                                    ,$1 AS site_id
                                    ,u.parent_item_type_id
                                    ,u.parent_item_id
                                FROM updates u
                               WHERE update_id IN (
                                         SELECT MAX(u.update_id)
                                           FROM updates u
                                                JOIN flags f ON f.item_type_id = u.item_type_id
                                                            AND f.item_id = u.item_id
                                                            AND f.microcosm_id IN (SELECT microcosm_id FROM m)
                                                JOIN watchers w ON w.profile_id = $2
                                                               AND w.item_type_id = 2
                                                               AND w.item_id IN (SELECT microcosm_id FROM m)
                                                LEFT JOIN ignores i ON i.profile_id = $2
                                                                   AND i.item_type_id = 3
                                                                   AND i.item_id = u.created_by
                                          WHERE u.for_profile_id = $2
                                            AND i.profile_id IS NULL
                                            AND u.update_type_id = 8
                                          GROUP BY u.item_type_id, u.item_id
                                     )
                     ) AS rollup
               ORDER BY created DESC
               LIMIT $3
              OFFSET $4
              ) final_rollup
    
About

Avatar for Velocio @Velocio started