Self-hosted project management for devs and their clients: tickets, time tracking, docs and meetings, with an AI assistant that cites its sources. AGPL-3.0.
Ruby agplai-assistantdockerdsgvogdprhotwireissue-trackerjira-alternativeknowledge-basellmmcp-servermeeting-notesopen-sourceproject-managementredmine-alternativeruby-on-railsself-hostedticketingtime-tracking
feat(people): read the meetings and the last contact of people
Guido Boykea9159d42b249
diff --git a/app/services/people/contacts.rb b/app/services/people/contacts.rbnew file mode 100644index 0000000..ce944c1--- /dev/null+++ b/app/services/people/contacts.rb@@ -0,0 +1,126 @@+# frozen_string_literal: true++# Read object for the people directory (S2a): the meetings of a person and the+# last contact with people, both only as far as the viewer may read them.+#+# Entry point: meetings_for / last_contacts+module People+ # * A person's meetings are the meetings with a participant that points to+ # the person row (person_id) OR, for a member, to the account (user_id).+ # A member without a row of their own has meetings through user_id alone.+ # * Readable means MeetingPolicy::Scope and TicketPolicy::Scope, the rules of+ # "Alle Meetings" and the ticket list; no rule of its own next to them.+ # * The last contact (E3) is the newest readable meeting; for an active+ # member also their own work on a readable ticket (created, changed,+ # commented), whichever is newer. A comment the viewer may not read+ # (Post.readable_by) does not count, nor does a bulk run (Activity.feed).+ # * #last_contacts answers a whole list in a fixed number of queries: one+ # per kind (meetings by person, meetings by user, ticket work), one for the+ # ticket numbers, plus the policy scopes.+ class Contacts+ # Ticket activity keys that count as own work, by kind (the words of the+ # lane: angelegt, geändert, kommentiert). Subscribing, assigning and+ # moving are not counted.+ TICKET_ACTIONS = {+ 'created' => :created,+ 'post_added' => :commented,+ 'updated' => :changed,+ 'title_changed' => :changed,+ 'description_changed' => :changed,+ 'status_changed' => :changed,+ 'priority_changed' => :changed,+ 'custom_attributes_changed' => :changed+ }.freeze++ # What a list row or the lane needs to know about the one contact shown.+ LastContact = Struct.new(:at, :kind, :meeting_title, :ticket_number, :action, keyword_init: true)++ # One entry of the directory: its person row, its account, and whether the+ # account is an active member (only then does ticket work count).+ Subject = Struct.new(:person_id, :user_id, :member, keyword_init: true)++ # The participant column a meeting lookup reads, by name; never interpolated+ # from a caller.+ PARTICIPANT_COLUMNS = {+ person: 'meeting_participants.person_id',+ user: 'meeting_participants.user_id'+ }.freeze++ def initialize(viewer:, organisation:)+ @viewer = viewer+ @organisation = organisation+ end++ # The readable meetings of a person row and/or an account, newest first.+ def meetings_for(person: nil, user: nil)+ user_id = user&.id || person&.user_id+ return Meeting.none if person.nil? && user_id.nil?++ participants = MeetingParticipant.none+ participants = participants.or(MeetingParticipant.where(person_id: person.id)) if person+ participants = participants.or(MeetingParticipant.where(user_id: user_id)) if user_id+ readable_meetings.where(id: participants.select(:meeting_id)).order(occurred_at: :desc, id: :desc)+ end++ # The last contact of each subject, in the order given (nil: none).+ def last_contacts(subjects)+ by_person = latest_meetings(:person, subjects.filter_map(&:person_id).uniq)+ by_user = latest_meetings(:user, subjects.filter_map(&:user_id).uniq)+ by_actor = latest_ticket_work(subjects.select(&:member).filter_map(&:user_id).uniq)+ subjects.map do |subject|+ candidates = [by_person[subject.person_id], by_user[subject.user_id]]+ candidates << by_actor[subject.user_id] if subject.member+ candidates.compact.max_by(&:at)+ end+ end++ private++ def readable_meetings+ @readable_meetings ||= MeetingPolicy::Scope.new(@viewer, Meeting).resolve+ end++ def readable_tickets+ @readable_tickets ||= TicketPolicy::Scope.new(@viewer, Ticket).resolve+ end++ # The newest readable meeting per person row or per account.+ def latest_meetings(key, ids)+ return {} if ids.empty?++ column = PARTICIPANT_COLUMNS.fetch(key)+ rows = MeetingParticipant.where(meeting_id: readable_meetings.select(:id))+ .where(key == :person ? { person_id: ids } : { user_id: ids })+ .joins(:meeting)+ .select(Arel.sql("DISTINCT ON (#{column}) #{column} AS subject_id, " \+ 'meetings.title AS meeting_title, meetings.occurred_at AS met_at'))+ .order(Arel.sql("#{column}, meetings.occurred_at DESC, meetings.id DESC"))+ rows.to_h do |row|+ [row.subject_id, LastContact.new(at: row.met_at, kind: :meeting, meeting_title: row.meeting_title)]+ end+ end++ # The newest own work of each account on a readable ticket.+ def latest_ticket_work(user_ids)+ return {} if user_ids.empty?++ rows = own_ticket_work(user_ids)+ .select('DISTINCT ON (activities.actor_id) activities.actor_id, activities.trackable_id, ' \+ 'activities.action, activities.created_at')+ .order('activities.actor_id, activities.created_at DESC, activities.id DESC').to_a+ numbers = readable_tickets.where(id: rows.map(&:trackable_id)).pluck(:id, :number).to_h+ rows.to_h do |row|+ [row.actor_id, LastContact.new(at: row.created_at, kind: :ticket, action: TICKET_ACTIONS.fetch(row.action),+ ticket_number: numbers[row.trackable_id])]+ end+ end++ def own_ticket_work(user_ids)+ base = Activity.feed.where(actor_id: user_ids, trackable_type: 'Ticket', action: TICKET_ACTIONS.keys,+ trackable_id: readable_tickets.select(:id))+ readable_posts = Post.readable_by(@viewer, @organisation).select(:id)+ base.where.not(secondary_type: 'Post').or(base.where(secondary_type: nil))+ .or(base.where(secondary_type: 'Post', secondary_id: readable_posts))+ end+ end+enddiff --git a/spec/services/people/contacts_spec.rb b/spec/services/people/contacts_spec.rbnew file mode 100644index 0000000..168e1e4--- /dev/null+++ b/spec/services/people/contacts_spec.rb@@ -0,0 +1,191 @@+# frozen_string_literal: true++require 'rails_helper'++# What the people directory may show about meetings and the last contact of a+# person (S2a). Only what the viewer may read counts: a meeting in a project+# without read access, an activity on an unreadable ticket and a private+# comment the viewer may not read never count, not in the list, not in the+# last contact. No hint at anything hidden.+RSpec.describe People::Contacts, :ci_guard do+ include ActiveSupport::Testing::TimeHelpers++ let(:organisation) { create(:organisation) }++ before { ActsAsTenant.current_tenant = organisation }+ after { ActsAsTenant.current_tenant = nil }++ def member(first_name, role: create(:role, organisation: organisation))+ user = create(:user, first_name: first_name, last_name: 'Beispiel')+ create(:membership, user: user, organisation: organisation, role: role, active: true)+ User.find(user.id)+ end++ let(:readable) { create(:project, organisation: organisation, name: 'Offen') }+ let(:hidden) { create(:project, organisation: organisation, name: 'Verborgen') }+ let(:viewer) { member('Vera') }+ let(:admin) { member('Anna', role: create(:role, organisation: organisation, title: Role::ACCOUNT_ADMIN_TITLE)) }+ let(:colleague) { member('Carl') }+ let(:contact) { create(:person, organisation: organisation, name: 'Doris Dienstleister') }++ before do+ create(:project_permission, user: viewer, project: readable, tickets_access: :read_only, meetings_access: :read_only)+ end++ def meeting_with(project, at:, title:, person: nil, user: nil)+ meeting = create(:meeting, organisation: organisation, project: project, occurred_at: at, title: title)+ create(:meeting_participant, meeting: meeting, person: person, user: user,+ display_name: person&.name || user&.display_name || 'Gast')+ meeting+ end++ def ticket_activity(user, ticket, action:, at:, secondary: nil)+ Activity.create!(actor: user, trackable: ticket, organisation: organisation, action: action,+ secondary: secondary, parameters: { 'skip_notifications' => true }, created_at: at)+ end++ def contacts(user = viewer)+ described_class.new(viewer: User.find(user.id), organisation: organisation)+ end++ def subject_for(person: nil, user: nil, member: false)+ described_class::Subject.new(person_id: person&.id, user_id: user&.id, member: member)+ end++ describe '#meetings_for' do+ it 'lists the readable meetings of a contact through person_id, newest first' do+ older = meeting_with(readable, at: 10.days.ago, title: 'Kick-off', person: contact)+ newer = meeting_with(readable, at: 2.days.ago, title: 'Status', person: contact)+ meeting_with(hidden, at: 1.day.ago, title: 'Geheim', person: contact)++ expect(contacts.meetings_for(person: contact).to_a).to eq([newer, older])+ end++ it 'shows an admin the meeting of every project' do+ meeting_with(readable, at: 2.days.ago, title: 'Status', person: contact)+ secret = meeting_with(hidden, at: 1.day.ago, title: 'Geheim', person: contact)++ expect(contacts(admin).meetings_for(person: contact).first).to eq(secret)+ end++ it 'finds the meetings of a member without a person row through user_id' do+ attended = meeting_with(readable, at: 3.days.ago, title: 'Planung', user: colleague)++ expect(contacts.meetings_for(user: colleague).to_a).to eq([attended])+ end++ it 'joins person_id and user_id for a member with a row, each meeting once' do+ row = create(:person, organisation: organisation, user: colleague)+ by_user = meeting_with(readable, at: 3.days.ago, title: 'Planung', user: colleague)+ by_person = meeting_with(readable, at: 1.day.ago, title: 'Abnahme', person: row)++ expect(contacts.meetings_for(person: row, user: colleague).to_a).to eq([by_person, by_user])+ end++ it 'finds nothing for nobody' do+ expect(contacts.meetings_for.to_a).to be_empty+ end+ end++ describe '#last_contacts' do+ it 'takes the newest readable meeting of a contact, never one it may not read' do+ meeting_with(readable, at: 5.days.ago, title: 'Status', person: contact)+ meeting_with(hidden, at: 1.day.ago, title: 'Geheim', person: contact)++ last = contacts.last_contacts([subject_for(person: contact)]).first++ expect([last.kind, last.meeting_title]).to eq([:meeting, 'Status'])+ expect(last.at).to be_within(1.second).of(5.days.ago)+ end++ it 'is nil without anything readable' do+ meeting_with(hidden, at: 1.day.ago, title: 'Geheim', person: contact)++ expect(contacts.last_contacts([subject_for(person: contact)])).to eq([nil])+ end++ describe 'a member (E3: the newer of meeting and own ticket activity)' do+ let(:ticket) { create(:ticket, organisation: organisation, project: readable, number: 142) }+ let(:hidden_ticket) { create(:ticket, organisation: organisation, project: hidden) }++ before { meeting_with(readable, at: 4.days.ago, title: 'Status', user: colleague) }++ it 'takes a newer comment on a readable ticket' do+ post = create(:post, ticket: ticket, user: colleague, organisation: organisation)+ ticket_activity(colleague, ticket, action: 'post_added', secondary: post, at: 1.day.ago)++ last = contacts.last_contacts([subject_for(user: colleague, member: true)]).first++ expect([last.kind, last.action, last.ticket_number]).to eq([:ticket, :commented, 142])+ end++ it 'keeps the meeting when the activity is older' do+ ticket_activity(colleague, ticket, action: 'status_changed', at: 9.days.ago)++ expect(contacts.last_contacts([subject_for(user: colleague, member: true)]).first.kind).to eq(:meeting)+ end++ it 'ignores an activity on a ticket the viewer may not read' do+ ticket_activity(colleague, hidden_ticket, action: 'created', at: 1.hour.ago)++ expect(contacts.last_contacts([subject_for(user: colleague, member: true)]).first.kind).to eq(:meeting)+ expect(contacts(admin).last_contacts([subject_for(user: colleague, member: true)]).first.kind).to eq(:ticket)+ end++ it 'ignores a private comment the viewer may not read' do+ addressee = member('Paula')+ post = create(:post, ticket: ticket, user: colleague, organisation: organisation,+ is_private: true, visible_to_user: addressee)+ ticket_activity(colleague, ticket, action: 'post_added', secondary: post, at: 1.hour.ago)++ expect(contacts.last_contacts([subject_for(user: colleague, member: true)]).first.kind).to eq(:meeting)+ expect(contacts(admin).last_contacts([subject_for(user: colleague, member: true)]).first.kind).to eq(:ticket)+ end++ it 'ignores ticket activities that are no own work (subscribing) and bulk runs' do+ ticket_activity(colleague, ticket, action: 'subscriber_added', at: 1.hour.ago)+ Activity.create!(actor: colleague, trackable: ticket, organisation: organisation, action: 'status_changed',+ parameters: { 'skip_notifications' => true, 'bulk_run_member' => true }, created_at: 1.hour.ago)++ expect(contacts.last_contacts([subject_for(user: colleague, member: true)]).first.kind).to eq(:meeting)+ end++ it 'counts no activity for a contact, also a former member' do+ former_row = create(:person, organisation: organisation, user: colleague)+ ticket_activity(colleague, ticket, action: 'created', at: 1.hour.ago)++ # The same account once as a member and once as a contact row in one+ # call: only the member entry may count the ticket work.+ member_last, contact_last = contacts.last_contacts([subject_for(user: colleague, member: true),+ subject_for(person: former_row, user: colleague, member: false)])++ expect([member_last.kind, contact_last.kind]).to eq(%i[ticket meeting])+ end+ end++ # Counts the queries itself, so Prosopite (which would flag the setup and+ # the second call of the same shape) stays out of this one.+ it 'asks the database the same number of times for two people and for six' do+ Prosopite.pause+ people = Array.new(6) { |index| create(:person, organisation: organisation, name: "Kontakt #{index}") }+ members = Array.new(6) { |index| member("Mitglied#{index}") }+ people.each_with_index { |person, index| meeting_with(readable, at: index.days.ago, title: "M#{index}", person: person) }+ members.each_with_index { |user, index| meeting_with(readable, at: index.hours.ago, title: "U#{index}", user: user) }++ count = lambda do |size|+ subjects = people.first(size).map { |person| subject_for(person: person) } ++ members.first(size).map { |user| subject_for(user: user, member: true) }+ queries = 0+ counter = ->(*, payload) { queries += 1 unless payload[:name] == 'SCHEMA' || payload[:cached] }+ ActiveSupport::Notifications.subscribed(counter, 'sql.active_record') do+ contacts.last_contacts(subjects)+ end+ queries+ end++ expect(count.call(6)).to eq(count.call(2))+ ensure+ Prosopite.resume+ end+ end+end