--
-- forums_reading_info__user_add_msg/2
--
create or replace function forums_reading_info__user_add_msg(
  p_root_message_id integer,
  p_user_id integer
) returns int4 as $$

DECLARE
   v_forum_id integer;
BEGIN
   if NOT exists (select 1 from forums_reading_info
                   where user_id = p_user_id
                     and root_message_id = p_root_message_id) then

       insert into forums_reading_info (
                root_message_id,
                user_id,
                forum_id
             ) values (
                p_root_message_id,
                p_user_id,
                (select forum_id from forums_messages
                  where message_id = p_root_message_id)
             );   
    end if;

    return 0;
END;
$$ language plpgsql;