> For the complete documentation index, see [llms.txt](https://docs.decentraland.org/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://docs.decentraland.org/contributor/contributor-zh/she-jiao-fu-wu/put-friends.md).

# 更新好友

与社交服务 API 交互，以执行与事件和用户相关的操作。这些操作包括创建新的好友请求、接受或拒绝传入请求，以及删除好友关系。社交服务允许用户在平台内管理他们的关系和互动。

### 好友关系与操作

社交服务中的好友关系是用户之间的双向关系。可以通过一些操作来更新这些好友关系，例如发送好友请求、接受或拒绝请求、取消请求以及移除好友关系。这些操作中的每一种都会对用户之间的关系产生特定影响。

### 实现好友关系更新

要执行这些与好友关系相关的操作，必须使用 `UpdateFriendshipEvent` 消息。该消息可用于执行与好友关系相关的各种操作，例如请求、接受、拒绝、取消和移除。

每种操作所需的确切有效载荷结构和参数在 `UpdateFriendshipEvent` 消息中指定。

**用法示例**

下面是一个示例，展示你可以如何使用 `UpdateFriendshipEvent` 来发送好友请求：

```proto
message UpdateFriendshipEvent {
  string userAddress = 1;
  string targetUserAddress = 2;
  FriendshipAction action = 3;
}
```

在这个示例中， `userAddress` 表示发送者的地址， `targetUserAddress` 表示接收者的地址，而 `action` 表示要执行的操作类型（请求、接受、拒绝、取消或移除）。

#### 发送好友请求

任何用户都可以向其他用户发送好友请求。不过，要建立好友关系，接收方用户必须接受该请求。

```javascript
requestFriendship: async (address: string, message?: string) => {
  const response = await service.updateFriendshipEvent({
    event: { request: { user: { address }, message } },
    authToken: { synapseToken }
  })
  processErrors(response)
  return response.event
}
```

#### 接受或拒绝请求

当用户收到好友请求时，可以选择接受或拒绝。接受请求会在用户之间建立互相好友关系。拒绝请求则不会建立好友关系。

```javascript
acceptFriendshipRequest: async (address: string) => {
  const response = await service.updateFriendshipEvent(
    UpdateFriendshipPayload.create({
      event: { accept: { user: { address } } },
      authToken: { synapseToken }
    })
  )
  processErrors(response)
  return response.event
}
```

```javascript
rejectFriendshipRequest: async (address: string) => {
  const response = await service.updateFriendshipEvent(
    UpdateFriendshipPayload.create({
      authToken: { synapseToken },
      event: { reject: { user: { address } } }
    })
  )
  processErrors(response)
  return response.event
}
```

#### 取消好友请求

如果用户在已发送的好友请求被接受之前决定撤回该请求，可以取消它。这会阻止接收者看到或回应该请求。

```javascript
cancelFriendshipRequest: async (address: string) => {
  const response = await service.updateFriendshipEvent(
    UpdateFriendshipPayload.create({
      event: { cancel: { user: { address } } },
      authToken: { synapseToken }
    })
  )
  processErrors(response)
  return response.event
}
```

#### 移除好友关系

在互为好友的关系中，任一用户都可以选择移除该好友关系。好友关系被移除后，它会从双方用户的记录中删除，从而结束双方关系。

```javascript
removeFriend: async (address: string) => {
  const response = await service.updateFriendshipEvent(
    UpdateFriendshipPayload.create({
      authToken: { synapseToken },
      event: { delete: { user: { address } } }
    })
  )
  processErrors(response)
  return response.event
}
```


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter, and the optional `goal` query parameter:

```
GET https://docs.decentraland.org/contributor/contributor-zh/she-jiao-fu-wu/put-friends.md?ask=<question>&goal=<endgoal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is optional and describes the broader end goal you are ultimately trying to accomplish on behalf of the user. GitBook uses it to tailor the answer towards what is most useful for that goal.

The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
