diff --git a/VERSION b/VERSION index f2722b1..2779245 100644 --- a/VERSION +++ b/VERSION @@ -1 +1 @@ -0.2.12 +0.2.13 diff --git a/snooper_calendar.py b/snooper_calendar.py index aac22bc..a49f5f1 100644 --- a/snooper_calendar.py +++ b/snooper_calendar.py @@ -251,6 +251,7 @@ def fetch_calendar_events( event_map = response.get("events") or {} events = [] used_urls: set[str] = set() + event_end_times: list[tuple[int, str]] = [] for instance in response.get("calendarEventInstance") or []: event = event_map.get(str(instance.get("eventRefId") or ""), {}) locations = event.get("locations") or [] @@ -280,9 +281,23 @@ def fetch_calendar_events( "url": ai_notes_url or native_url or manual_url, } ) + event_end_times.append((event_end_ts, summary)) events.sort(key=lambda event: (event["begin"], event["summary"])) # Collect unmatched links for separate display unmatched_links = [ link for link in ai_notes_links if link["url"] not in used_urls ] + # Fall back to the nearest meeting name for untitled links (e.g. docx notices). + for link in unmatched_links: + if link.get("topic"): + continue + best_delta = None + best_summary = "" + for end_ts, summary in event_end_times: + delta = link["create_time"] - end_ts + if abs(delta) <= 21600 and (best_delta is None or abs(delta) < abs(best_delta)): + best_delta = delta + best_summary = summary + if best_summary: + link["topic"] = best_summary return events, unmatched_links diff --git a/tests/test_calendar.py b/tests/test_calendar.py index 0ed880a..f047511 100644 --- a/tests/test_calendar.py +++ b/tests/test_calendar.py @@ -123,3 +123,60 @@ def test_fetch_calendar_events_normalizes_and_sorts_daily_instances(): assert request["calendarIds"] == ["calendar-synthetic"] assert request["needsBriefEvents"] is True assert request["endTime"] - request["startTime"] == 86_400 + + +class FakeMeetingCalendarClient: + def __init__(self, begin_ts, end_ts): + self.begin_ts = begin_ts + self.end_ts = end_ts + + def api(self, name, data): + if name == "calendars.GetUserPrimaryCalendarRequest": + return {"calendar": {"id": "calendar-synthetic"}} + assert name == "calendarevents.QueryInstanceRequest" + return { + "calendarEventInstance": [ + {"eventRefId": "event-a", "beginTime": self.begin_ts, "endTime": self.end_ts} + ], + "events": { + "event-a": { + "summary": "指数-金科定期AI业务讨论会", + "locations": [], + "isDeleted": False, + } + }, + } + + +def test_untitled_ai_notes_link_falls_back_to_nearest_meeting_name(): + tz = ZoneInfo("Asia/Shanghai") + begin = int(datetime(2026, 11, 17, 14, 0, tzinfo=tz).timestamp()) + end = int(datetime(2026, 11, 17, 15, 0, tzinfo=tz).timestamp()) + minutes_ts = int(datetime(2026, 11, 17, 15, 15, tzinfo=tz).timestamp()) + docx_ts = int(datetime(2026, 11, 17, 17, 38, tzinfo=tz).timestamp()) + + storage = SnifferStorage(url="sqlite:///:memory:") + storage.save_message({ + "msg_id": "minutes-1", "chat_id": AI_NOTES_CHAT_ID, "chat_type": 2, + "from_id": "bot", "msg_type": 14, "create_time": minutes_ts, + "content": ( + "[卡片]\nMeeting recording completed" + "Topic: 指数-金科定期AI业务讨论会Owner: 万轶伦\n" + "https://efund.feishu.cn/minutes/m1" + ), + }) + storage.save_message({ + "msg_id": "docx-1", "chat_id": AI_NOTES_CHAT_ID, "chat_type": 2, + "from_id": "bot", "msg_type": 4, "create_time": docx_ts, + "content": "https://efund.feishu.cn/docx/d1", + }) + + client = FakeMeetingCalendarClient(begin, end) + events, unmatched = fetch_calendar_events( + client, date(2026, 11, 17), tz, storage=storage + ) + + assert events[0]["url"].startswith("https://efund.feishu.cn/minutes/m1") + assert any(link["url"].startswith("https://efund.feishu.cn/docx/d1") for link in unmatched) + docx = next(link for link in unmatched if "docx/d1" in link["url"]) + assert docx["topic"] == "指数-金科定期AI业务讨论会"