Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,12 @@ All notable changes to `fopost` are documented here.

### Added

- `inbox.resolve(workspace_id=, type=, account_id=None, before=None)` resolves every
unanswered item of one type, optionally only on some accounts or only items dated before
`before`, and returns how many changed. Nothing is sent to a platform.
- `inbox.list`, `inbox.threads` and `inbox.conversations` take `status` (`unanswered`,
`replied` or `resolved`) and `since` (a `datetime` or ISO 8601 string), and `account_id`
accepts a list of ids.
- X engagement: `inbox.repost`, `inbox.unrepost`, `inbox.bookmark` and `inbox.unbookmark`
on X items (where `can_repost` / `can_bookmark` are true), `accounts.follow_x_user` and
`accounts.unfollow_x_user` taking a numeric X user id and returning an `XFollowResult`,
Expand Down Expand Up @@ -41,6 +47,10 @@ All notable changes to `fopost` are documented here.

### Removed

- **Breaking:** the `state`, `platform` and `q` filters on `inbox.list`, `inbox.threads` and
`inbox.conversations`, and `sort="unanswered"`. Filter with `status` and `account_id`;
`sort` is `newest` or `oldest`. `ChatAdapter.receive` takes `status` (default
`unanswered`) in place of `state` and `platform`.
- **Breaking:** `client.blog` (`/v1/blog`: settings, website secret and test, keywords and
articles), the `Blog*` models and `BLOG_*` constants, `ArticlePublishedPayload`, and
`article.published` from the webhook event names. The API no longer serves them; articles
Expand Down
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -296,8 +296,8 @@ except FopostError as err:
| `labels` | `list`, `create`, `get`, `update`, `delete` |
| `media` | `list`, `upload`, `delete`, `limits`, `presign`, `complete`, `upload_direct` |
| `ai` | `credits`, `generate_caption`, `rewrite`, `repurpose_url` |
| `inbox` | `list`, `threads`, `conversations`, `unread_count`, `accounts`, `platforms`, `mark_thread_read`, `refresh`, `update`, `edit_comment`, `reply`, `hide`, `unhide`, `delete`, `like`, `unlike`, `repost`, `unrepost`, `bookmark`, `unbookmark`, `vote`, `pin`, `unpin`, `react`, `start_conversation`, `set_typing`, `handover`, `list_approvals`, `approve_reply`, `reject_reply` |
| `inbox` | `list`, `threads`, `conversations`, `unread_count`, `accounts`, `platforms`, `mark_thread_read`, `refresh`, `update`, `edit_comment`, `reply`, `hide`, `unhide`, `delete`, `like`, `unlike`, `repost`, `unrepost`, `bookmark`, `unbookmark`, `vote`, `pin`, `unpin`, `react`, `start_conversation`, `set_typing`, `list_approvals`, `approve_reply`, `reject_reply` |
| `inbox` | `list`, `threads`, `conversations`, `unread_count`, `accounts`, `platforms`, `mark_thread_read`, `refresh`, `resolve`, `update`, `edit_comment`, `reply`, `hide`, `unhide`, `delete`, `like`, `unlike`, `repost`, `unrepost`, `bookmark`, `unbookmark`, `vote`, `pin`, `unpin`, `react`, `start_conversation`, `set_typing`, `handover`, `list_approvals`, `approve_reply`, `reject_reply` |
| `inbox` | `list`, `threads`, `conversations`, `unread_count`, `accounts`, `platforms`, `mark_thread_read`, `refresh`, `resolve`, `update`, `edit_comment`, `reply`, `hide`, `unhide`, `delete`, `like`, `unlike`, `repost`, `unrepost`, `bookmark`, `unbookmark`, `vote`, `pin`, `unpin`, `react`, `start_conversation`, `set_typing`, `list_approvals`, `approve_reply`, `reject_reply` |
| `contacts` | `list`, `get`, `create`, `update`, `delete`, `conversations`, `import_csv`, `list_fields`, `create_field`, `update_field`, `delete_field`, `conversation_analytics` |
| `broadcasts` | `list`, `get`, `create`, `update`, `delete`, `send`, `cancel`, `recipients` |
| `sequences` | `list`, `get`, `create`, `update`, `delete`, `enroll`, `unenroll`, `enrollments` |
Expand Down Expand Up @@ -380,7 +380,7 @@ chat.send(account_id="acc_...", handle="samrivera", text="Following up.")
| ------ | ------------ |
| `parse_webhook(body, headers)` | Verifies a delivery and returns the `ChatEvent` |
| `verify_webhook(body, headers)` | Signature check on its own; raises on a forged or stale delivery |
| `receive(...)` | Inbound DMs, unread by default |
| `receive(...)` | Inbound DMs, unanswered by default |
| `receive_one(event_or_id)` | The full message behind an event id, or `None` |
| `send(...)` | Reply, reply into a thread, or open one |
| `typing(conversation_id, account_id, on=True)` | Typing indicator |
Expand Down
12 changes: 5 additions & 7 deletions src/fopost/chat_adapter.py
Original file line number Diff line number Diff line change
Expand Up @@ -194,22 +194,20 @@ def receive(
*,
account_id: str | None = None,
conversation_id: str | None = None,
platform: str | None = None,
state: str | None = "unread",
status: str | None = "unanswered",
limit: int | None = None,
) -> builtins.list[ChatMessage]:
"""Inbound direct messages, newest first. Unread by default.
"""Inbound direct messages, newest first. Unanswered by default.

Pass ``state=None`` to read every state.
Pass ``status=None`` to read every status.
"""
page = self._client.inbox.list(
workspace_id=self._workspace_id,
type="dm",
direction="inbound",
state=state,
status=status,
account_id=account_id,
conversation_id=conversation_id,
platform=platform,
per_page=limit or self._page_size,
)
return [to_chat_message(item) for item in page.items]
Expand Down Expand Up @@ -295,7 +293,7 @@ def typing(self, conversation_id: str, account_id: str, on: bool = True) -> None
self._client.inbox.set_typing(conversation_id, account_id=account_id, on=on)

def mark_read(self, message: ChatMessage | str) -> None:
"""Mark one message read, so ``receive()`` stops returning it."""
"""Mark one message read."""
item_id = message if isinstance(message, str) else message.id
self._client.inbox.update(item_id, state="read")

Expand Down
82 changes: 57 additions & 25 deletions src/fopost/resources/inbox.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
from __future__ import annotations

import builtins
from collections.abc import Sequence
from datetime import datetime
from typing import Any

from .._http import unwrap
Expand Down Expand Up @@ -32,38 +34,50 @@ def _page(model: Any, body: Any) -> Any:
return Page[model](items=items, meta=meta)


def _ids(value: str | Sequence[str] | None) -> str | None:
if value is None or isinstance(value, str):
return value
return ",".join(value) or None


def _iso(value: str | datetime | None) -> str | None:
return value.isoformat() if isinstance(value, datetime) else value


class InboxResource(Resource):
def list(
self,
*,
workspace_id: str | None = None,
type: str | None = None,
state: str | None = None,
platform: str | None = None,
account_id: str | None = None,
status: str | None = None,
account_id: str | Sequence[str] | None = None,
since: str | datetime | None = None,
post_id: str | None = None,
post_external_id: str | None = None,
conversation_id: str | None = None,
direction: str | None = None,
q: str | None = None,
sort: str | None = None,
page: int = 1,
per_page: int = 25,
) -> Page[InboxItem]:
"""One page of items, newest first. ``meta.per_page`` and ``meta.total`` are set."""
"""One page of items, newest first. ``meta.per_page`` and ``meta.total`` are set.

``status`` is ``unanswered``, ``replied`` or ``resolved``; ``account_id`` takes
one id or several; ``sort`` is ``newest`` or ``oldest``.
"""
body = self._http.get(
"/inbox",
{
"workspace_id": workspace_id,
"type": type,
"state": state,
"platform": platform,
"account_id": account_id,
"status": status,
"account_id": _ids(account_id),
"since": _iso(since),
"post_id": post_id,
"post_external_id": post_external_id,
"conversation_id": conversation_id,
"direction": direction,
"q": q,
"sort": sort,
"page": page,
"per_page": per_page,
Expand All @@ -76,10 +90,9 @@ def threads(
*,
workspace_id: str | None = None,
kind: str | None = None,
platform: str | None = None,
account_id: str | None = None,
state: str | None = None,
q: str | None = None,
account_id: str | Sequence[str] | None = None,
status: str | None = None,
since: str | datetime | None = None,
sort: str | None = None,
page: int = 1,
per_page: int = 25,
Expand All @@ -94,10 +107,9 @@ def threads(
{
"workspace_id": workspace_id,
"kind": kind,
"platform": platform,
"account_id": account_id,
"state": state,
"q": q,
"account_id": _ids(account_id),
"status": status,
"since": _iso(since),
"sort": sort,
"page": page,
"per_page": per_page,
Expand All @@ -109,10 +121,9 @@ def conversations(
self,
*,
workspace_id: str | None = None,
platform: str | None = None,
account_id: str | None = None,
state: str | None = None,
q: str | None = None,
account_id: str | Sequence[str] | None = None,
status: str | None = None,
since: str | datetime | None = None,
sort: str | None = None,
page: int = 1,
per_page: int = 25,
Expand All @@ -122,17 +133,38 @@ def conversations(
"/inbox/conversations",
{
"workspace_id": workspace_id,
"platform": platform,
"account_id": account_id,
"state": state,
"q": q,
"account_id": _ids(account_id),
"status": status,
"since": _iso(since),
"sort": sort,
"page": page,
"per_page": per_page,
},
)
return _page(InboxConversation, body) # type: ignore[no-any-return]

def resolve(
self,
*,
workspace_id: str,
type: str,
account_id: str | Sequence[str] | None = None,
before: str | datetime | None = None,
) -> int:
"""Resolve every unanswered item of ``type``; returns how many changed.

Narrow it to some accounts with ``account_id`` and to items dated before
``before``. Nothing is sent to a platform.
"""
body: dict[str, Any] = {"workspace_id": workspace_id, "type": type}
if account_id is not None:
body["account_id"] = [account_id] if isinstance(account_id, str) else list(account_id)
if before is not None:
body["before"] = _iso(before)
result = unwrap(self._http.post("/inbox/resolve", body))
resolved = result.get("resolved") if isinstance(result, dict) else None
return int(resolved) if isinstance(resolved, int) else 0

def unread_count(self, *, workspace_id: str | None = None) -> int:
body = self._http.get("/inbox/unread-count", {"workspace_id": workspace_id})
count = body.get("count") if isinstance(body, dict) else None
Expand Down
4 changes: 2 additions & 2 deletions tests/test_chat_adapter.py
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,7 @@ def test_round_trip_from_delivery_to_reply_to_read(client: Fopost) -> None:


@respx.mock
def test_receive_reads_unread_inbound_dms(client: Fopost) -> None:
def test_receive_reads_unanswered_inbound_dms(client: Fopost) -> None:
route = respx.get(f"{BASE_URL}/inbox").mock(
return_value=httpx.Response(
200, json={"data": [item()], "meta": {"page": 1, "perPage": 25, "total": 1}}
Expand All @@ -140,7 +140,7 @@ def test_receive_reads_unread_inbound_dms(client: Fopost) -> None:
params = dict(route.calls.last.request.url.params)
assert params["type"] == "dm"
assert params["direction"] == "inbound"
assert params["state"] == "unread"
assert params["status"] == "unanswered"


@respx.mock
Expand Down
61 changes: 59 additions & 2 deletions tests/test_inbox.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
from __future__ import annotations

import json
from datetime import datetime, timezone

import httpx
import respx
Expand Down Expand Up @@ -35,7 +36,14 @@ def test_list_sends_snake_case_filters_and_keeps_meta(client: Fopost) -> None:
)
)

page = client.inbox.list(workspace_id="ws_1", state="unread", post_external_id="17895", page=2)
page = client.inbox.list(
workspace_id="ws_1",
status="unanswered",
account_id=["acc_1", "acc_2"],
since=datetime(2026, 9, 1, tzinfo=timezone.utc),
post_external_id="17895",
page=2,
)

assert len(page) == 1
assert isinstance(page[0], InboxItem)
Expand All @@ -46,7 +54,9 @@ def test_list_sends_snake_case_filters_and_keeps_meta(client: Fopost) -> None:
assert page.meta.per_page == 10
assert dict(route.calls.last.request.url.params) == {
"workspace_id": "ws_1",
"state": "unread",
"status": "unanswered",
"account_id": "acc_1,acc_2",
"since": "2026-09-01T00:00:00+00:00",
"post_external_id": "17895",
"page": "2",
"per_page": "25",
Expand Down Expand Up @@ -285,3 +295,50 @@ def test_start_conversation_and_typing(client: Fopost) -> None:

assert client.inbox.accounts()[0].can_start_conversation is True
assert accounts.called


@respx.mock
def test_resolve_posts_the_type_with_account_ids_as_a_list(client: Fopost) -> None:
route = respx.post(f"{BASE_URL}/inbox/resolve").mock(
return_value=httpx.Response(200, json={"data": {"resolved": 7}})
)

resolved = client.inbox.resolve(
workspace_id="ws_1", type="comment", account_id="acc_1", before="2026-09-01T00:00:00Z"
)

assert resolved == 7
assert json.loads(route.calls.last.request.content) == {
"workspace_id": "ws_1",
"type": "comment",
"account_id": ["acc_1"],
"before": "2026-09-01T00:00:00Z",
}


@respx.mock
def test_threads_and_conversations_send_status_and_joined_accounts(client: Fopost) -> None:
threads = respx.get(f"{BASE_URL}/inbox/posts").mock(
return_value=httpx.Response(200, json={"data": [], "meta": {"page": 1, "perPage": 25}})
)
conversations = respx.get(f"{BASE_URL}/inbox/conversations").mock(
return_value=httpx.Response(200, json={"data": [], "meta": {"page": 1, "perPage": 25}})
)

client.inbox.threads(kind="reviews", status="replied", account_id=["a", "b"], sort="oldest")
client.inbox.conversations(status="resolved", since="2026-09-01T00:00:00Z")

assert dict(threads.calls.last.request.url.params) == {
"kind": "reviews",
"account_id": "a,b",
"status": "replied",
"sort": "oldest",
"page": "1",
"per_page": "25",
}
assert dict(conversations.calls.last.request.url.params) == {
"status": "resolved",
"since": "2026-09-01T00:00:00Z",
"page": "1",
"per_page": "25",
}
Loading