تنظيم صفحاتك في مجموعات
يمكنك حفظ المحتوى وتصنيفه حسب إعداداتك المفضّلة.
يشرح هذا الدليل كيفية استخدام طريقة
update()
في مورد SpaceReadState ضمن Google Chat API لوضع علامة "مقروء" أو "غير مقروء" على المساحات.
SpaceReadState المورد هو مورد فردي يمثّل تفاصيل حول آخر رسالة قرأها مستخدم محدّد في مساحة Google Chat.
أنشئ بيانات اعتماد معرِّف عميل OAuth لتطبيق على جهاز كمبيوتر. لتشغيل النموذج في هذا الدليل، احفظ بيانات الاعتماد كملف JSON باسم credentials.json في دليلك المحلي.
للحصول على إرشادات، أكمل خطوات إعداد بيئتك في هذا
البدء السريع.
يتم ضبط حقل name على حالة القراءة للمساحة المطلوب تعديلها، والتي تتضمّن معرّف مستخدم أو اسمًا مستعارًا ومعرّف مساحة. لا يتيح تعديل حالة القراءة للمساحة سوى تعديل حالة القراءة للمستخدم الذي يجري المكالمة، ويمكن تحديد ذلك من خلال ضبط أحد الخيارات التالية:
الاسم المستعار me على سبيل المثال،
users/me/spaces/SPACE/spaceReadState.
عنوان البريد الإلكتروني في Workspace للمستخدم الذي يجري المكالمة على سبيل المثال،
users/user@example.com/spaces/SPACE/spaceReadState.
رقم تعريف المستخدم الذي يجري المكالمة على سبيل المثال،
users/USER/spaces/SPACE/spaceReadState.
تم ضبط الحقل lastReadTime على القيمة المعدَّلة للوقت الذي تم فيه تعديل حالة قراءة مساحة المستخدم. عادةً ما يتطابق هذا الطابع الزمني مع الطابع الزمني لآخر رسالة تمت قراءتها، أو مع طابع زمني يحدّده المستخدم لتحديد موضع آخر رسالة تمت قراءتها في مساحة. عندما يكون lastReadTime قبل
وقت إنشاء أحدث رسالة، تظهر المساحة كرسالة غير مقروءة في واجهة المستخدم. لوضع علامة "مقروءة" على المساحة، اضبط قيمة lastReadTime على أي قيمة أكبر من وقت إنشاء آخر رسالة. يتم فرض قيمة lastReadTime لتتطابق مع آخر وقت تم فيه إنشاء الرسالة. يُرجى العِلم أنّ حالة القراءة في المساحة تؤثر فقط في حالة قراءة الرسائل المرئية في المحادثة ذات المستوى الأعلى في المساحة.
لا يتأثر الردود في سلاسل المحادثات بهذا الطابع الزمني، بل تعتمد بدلاً من ذلك على حالة قراءة سلسلة المحادثات.
يعدّل المثال التالي حالة القراءة للمساحة الخاصة بالمستخدم الذي يجري الاتصال:
import{createClientWithUserCredentials}from'./authentication-utils.js';constUSER_AUTH_OAUTH_SCOPES=['https://www.googleapis.com/auth/chat.users.readstate'];// This sample shows how to update a space read state for the calling userasyncfunctionmain(){// Create a clientconstchatClient=awaitcreateClientWithUserCredentials(USER_AUTH_OAUTH_SCOPES);// Initialize request argument(s)consttimestamp=newDate('2000-01-01').getTime();constrequest={spaceReadState:{// Replace SPACE_NAME herename:'users/me/spaces/SPACE_NAME/spaceReadState',lastReadTime:{seconds:Math.floor(timestamp/1000),nanos:(timestamp%1000)*1000000}},updateMask:{// The field paths to update.paths:['last_read_time']}};// Make the requestconstresponse=awaitchatClient.updateSpaceReadState(request);// Handle the responseconsole.log(response);}main().catch(console.error);
لتشغيل هذا النموذج، استبدِل SPACE_NAME برقم التعريف من name الخاص بالمساحة.
يمكنك الحصول على المعرّف من خلال استدعاء الطريقة
ListSpaces()
أو من عنوان URL الخاص بالمساحة.
تعدّل Google Chat API حالة القراءة للمساحة المحدّدة وتعرض مثيلاً من SpaceReadState.
تاريخ التعديل الأخير: 2025-08-04 (حسب التوقيت العالمي المتفَّق عليه)
[null,null,["تاريخ التعديل الأخير: 2025-08-04 (حسب التوقيت العالمي المتفَّق عليه)"],[[["\u003cp\u003eThis guide explains how to use the \u003ccode\u003eupdate()\u003c/code\u003e method to mark Google Chat spaces as read or unread.\u003c/p\u003e\n"],["\u003cp\u003eThe \u003ccode\u003eSpaceReadState\u003c/code\u003e resource stores a user's last read message details in a space.\u003c/p\u003e\n"],["\u003cp\u003eTo update read state, use the \u003ccode\u003eUpdateSpaceReadState()\u003c/code\u003e method with the \u003ccode\u003elastReadTime\u003c/code\u003e field.\u003c/p\u003e\n"],["\u003cp\u003eYou need a Google Workspace account and a configured Google Chat API to use this functionality.\u003c/p\u003e\n"],["\u003cp\u003eNode.js code samples and related topics are provided for practical implementation.\u003c/p\u003e\n"]]],["To update a user's read state in a Google Chat space, use the `UpdateSpaceReadState()` method with the `chat.users.readstate` authorization scope. Set the `name` field in `SpaceReadState` to the user's ID or alias and the space ID. Adjust the `lastReadTime` to reflect when the user last read messages; setting it before the latest message time marks the space as unread. Use `updateMask` with `lastReadTime`. The API returns the updated `SpaceReadState`.\n"],null,["# Update a user's space read state\n\nThis guide explains how to use the\n[`update()`](/workspace/chat/api/reference/rpc/google.chat.v1#google.chat.v1.ChatService.UpdateSpaceReadState)\nmethod on the `SpaceReadState` resource of the Google Chat API to mark spaces as\nread or unread.\n\nThe\n[`SpaceReadState` resource](/workspace/chat/api/reference/rpc/google.chat.v1#google.chat.v1.SpaceReadState)\nis a singleton resource that represents details about a\nspecified user's last read message in a Google Chat space.\n\nPrerequisites\n-------------\n\n\n### Node.js\n\n- A Business or Enterprise [Google Workspace](https://support.google.com/a/answer/6043576) account with access to [Google Chat](https://workspace.google.com/products/chat/).\n\n\u003c!-- --\u003e\n\n- Set up your environment:\n - [Create a Google Cloud project](/workspace/guides/create-project).\n - [Configure the OAuth consent screen](/workspace/guides/configure-oauth-consent).\n - [Enable and configure the Google Chat API](/workspace/chat/configure-chat-api) with a name, icon, and description for your Chat app.\n - Install the Node.js [Cloud Client Library](/workspace/chat/libraries?tab=nodejs#cloud-client-libraries).\n - [Create OAuth client ID credentials](/workspace/chat/authenticate-authorize-chat-user#step-2:) for a desktop application. To run the sample in this guide, save the credentials as a JSON file named `credentials.json` to your local directory.\n\n For guidance, complete the steps for setting up your environment in this [quickstart](/workspace/chat/api/guides/quickstart/nodejs\n #set-up-environment).\n- [Choose an authorization scope](/workspace/chat/authenticate-authorize#asynchronous-chat-calls) that supports user authentication.\n\n\n| The code samples in this page use the gRPC API interface with the Google Cloud client libraries. Alternatively, you can use the REST API interface. For more information about the gRPC and REST interfaces, see [Google Chat API overview](/workspace/chat/api/reference).\n\n\u003cbr /\u003e\n\nUpdate the calling user's space read state\n------------------------------------------\n\nTo update a user's read state within a space, include the following in\nyour request:\n\n- Specify the `chat.users.readstate` authorization scope.\n- Call the [`UpdateSpaceReadState()`](/workspace/chat/api/reference/rpc/google.chat.v1#google.chat.v1.ChatService.UpdateSpaceReadState) method.\n- Pass `updateMask` with the value `lastReadTime`.\n- Pass `spaceReadState` as an instance of [`SpaceReadState`](/workspace/chat/api/reference/rpc/google.chat.v1#google.chat.v1.SpaceReadState) with the following:\n - The `name` field set to the space read state to update, which includes a user ID or alias and a space ID. Updating space read state only supports updating the read state of the calling user, which can be specified by setting one of the following:\n - The `me` alias. For example, `users/me/spaces/`\u003cvar translate=\"no\"\u003eSPACE\u003c/var\u003e`/spaceReadState`.\n - The calling user's Workspace email address. For example, `users/user@example.com/spaces/`\u003cvar translate=\"no\"\u003eSPACE\u003c/var\u003e`/spaceReadState`.\n - The calling user's user ID. For example, `users/`\u003cvar translate=\"no\"\u003eUSER\u003c/var\u003e`/spaces/`\u003cvar translate=\"no\"\u003eSPACE\u003c/var\u003e`/spaceReadState`.\n - The `lastReadTime` field set to the updated value of the time when the user's space read state was updated. Usually this corresponds with either the timestamp of the last read message, or a timestamp specified by the user to mark the last read position in a space. When the `lastReadTime` is before the latest message create time, the space appears as unread in the UI. To mark the space as read, set `lastReadTime` to any value later (larger) than the latest message create time. The `lastReadTime` is coerced to match the latest message create time. Note that the space read state only affects the read state of messages that are visible in the space's top-level conversation. Replies in threads are unaffected by this timestamp, and instead rely on the thread read state.\n\nThe following example updates the calling user's space read state: \n\n### Node.js\n\nchat/client-libraries/cloud/update-space-read-state-user-cred.js \n[View on GitHub](https://github.com/googleworkspace/node-samples/blob/main/chat/client-libraries/cloud/update-space-read-state-user-cred.js) \n\n```javascript\nimport {createClientWithUserCredentials} from './authentication-utils.js';\n\nconst USER_AUTH_OAUTH_SCOPES = ['https://www.googleapis.com/auth/chat.users.readstate'];\n\n// This sample shows how to update a space read state for the calling user\nasync function main() {\n // Create a client\n const chatClient = await createClientWithUserCredentials(USER_AUTH_OAUTH_SCOPES);\n\n // Initialize request argument(s)\n const timestamp = new Date('2000-01-01').getTime();\n const request = {\n spaceReadState: {\n // Replace SPACE_NAME here\n name: 'users/me/spaces/SPACE_NAME/spaceReadState',\n lastReadTime: {\n seconds: Math.floor(timestamp / 1000),\n nanos: (timestamp % 1000) * 1000000\n }\n },\n updateMask: {\n // The field paths to update.\n paths: ['last_read_time']\n }\n };\n\n // Make the request\n const response = await chatClient.updateSpaceReadState(request);\n\n // Handle the response\n console.log(response);\n}\n\nmain().catch(console.error);\n```\n\nTo run this sample, replace \u003cvar translate=\"no\"\u003eSPACE_NAME\u003c/var\u003e with the ID from\nthe space's\n[`name`](/workspace/chat/api/reference/rpc/google.chat.v1#google.chat.v1.Space.FIELDS.string.google.chat.v1.Space.name).\nYou can obtain the ID by calling the\n[`ListSpaces()`](/workspace/chat/api/reference/rpc/google.chat.v1#google.chat.v1.ChatService.ListSpaces)\nmethod or from the space's URL.\n\nThe Google Chat API updates the specified space read state and returns\nan instance of\n[`SpaceReadState`](/workspace/chat/api/reference/rpc/google.chat.v1#google.chat.v1.SpaceReadState).\n\nRelated topics\n--------------\n\n- [Get the calling user's space read state](/workspace/chat/get-space-read-state).\n- [Get the calling user's thread read state](/workspace/chat/get-thread-read-state)."]]