Skip to content

Commit

Permalink
Add server sdk v2.0 code
Browse files Browse the repository at this point in the history
  • Loading branch information
FuDongHai committed Jan 25, 2024
1 parent 3f47367 commit f97a7c2
Show file tree
Hide file tree
Showing 703 changed files with 85,149 additions and 35,425 deletions.
10 changes: 8 additions & 2 deletions .github/workflows/build.yml
Original file line number Diff line number Diff line change
@@ -1,12 +1,18 @@
name: Build
on:
push:
branches: [ master ]
branches: [ master_java_2.0 ]
pull_request:
branches: [ master ]
branches: [ master_java_2.0 ]
jobs:
build:
runs-on: ubuntu-latest
env:
IM_REALM: ${{ secrets.IM_REALM }}
IM_BASE_URI: ${{ secrets.IM_BASE_URI }}
IM_APPKEY: ${{ secrets.IM_APPKEY }}
IM_CLIENT_ID: ${{ secrets.IM_CLIENT_ID }}
IM_CLIENT_SECRET: ${{ secrets.IM_CLIENT_SECRET }}
steps:
- name: Checkout
uses: actions/checkout@v2
Expand Down
767 changes: 767 additions & 0 deletions .openapi-generator/FILES

Large diffs are not rendered by default.

1 change: 1 addition & 0 deletions .openapi-generator/VERSION
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
7.1.0
26 changes: 0 additions & 26 deletions CHANGELOG.md

This file was deleted.

160 changes: 0 additions & 160 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,160 +0,0 @@
# Easemob IM Java SDK [![build](https://github.com/easemob/easemob-im-server-sdk/workflows/Build/badge.svg)](https://github.com/easemob/easemob-im-server-sdk/actions) [![codecov](https://codecov.io/gh/easemob/easemob-im-server-sdk/branch/master/graph/badge.svg)](https://codecov.io/gh/easemob/easemob-im-server-sdk)

**Easemob IM Java SDK** 是对环信 IM [服务端 API](http://docs-im.easemob.com/im/server/ready/intro) 的封装。

提供用户、群组等资源的操作管理能力。

## 依赖

- Java 1.8
- [Reactor](https://projectreactor.io)(io.projectreactor:reactor-bom:2020.0.4)

## 安装

如果你的项目使用 Maven 构建,在 pom.xml 中添加下面代码即可:

``` xml
<dependency>
<groupId>com.easemob.im</groupId>
<artifactId>im-sdk-core</artifactId>
<version>0.5.4</version>
</dependency>
```

如果你的项目使用 Gradle 构建,可以在 build.grade 中添加下面代码:

``` gradle
implementation 'com.easemob.im:im-sdk-core:0.5.4'
```

## 准备
使用 Server SDK 的前提需要您准备:
* 1. 环信 appKey
* 2. 两种 appCredentials 中的其中之一:
- 2.1 环信 clientId, clientSecret
- 2.2 声网 appId, appCert

### 获取环信 appKey, clientId, clientSecret

如果您有环信管理后台账号并创建过应用,请进入 [这里](https://console.easemob.com/user/login) 进行登录。
![图片](https://user-images.githubusercontent.com/15087647/114996679-a34cb980-9ed1-11eb-89ae-a22c1af7d69d.png)

如图点击查看后,可以看到自己的 appkey、Client ID、ClientSecret,用于 SDK 的初始化。

如果您没有环信管理后台账号,请进入 [这里](https://console.easemob.com/user/register) 进行注册账号,注册成功后请进行登录。
![图片](https://user-images.githubusercontent.com/15087647/114997381-59180800-9ed2-11eb-968a-a29406c78021.png)

如图先添加应用(也就是创建 appkey,自动生成 Client ID、ClientSecret),添加成功后在应用列表中可以看到应用信息,点击查看可以看到自己的 appkey、Client ID、ClientSecret,用于 SDK 的初始化。

### 获取声网 appId, appCert

如果您有声网 Console 后台账号并创建过项目,请先登录声网 Console 后台,点击[这里](https://sso.agora.io/cn/login/),然后到"项目列表" -> 找到自己的项目点击"编辑"图标后,即可看到 App ID、APP 证书。

如果您没有声网Console后台账号,请先注册账号,点击[这里](https://sso.agora.io/cn/v4/signup),注册成功后按照步骤1操作。


## 使用

[EMService](https://easemob.github.io/easemob-im-server-sdk/com/easemob/im/server/EMService.html) 是所有 API 的入口,可以这样初始化:

### 1. 使用 Easemob App Credentials 的情况
``` java
建议写到配置类中,示例如下:
@Configuration
public class Config {

@Bean
public EMService service() {

EMProperties properties = EMProperties.builder()
.setAppkey("Appkey")
.setClientId("Client ID")
.setClientSecret("ClientSecret")
.build();

return new EMService(properties);
}
}
```

根据业务资源,API 分为:

- [Attachment](https://easemob.github.io/easemob-im-server-sdk/com/easemob/im/server/api/attachment/AttachmentApi.html)
用于上传下载附件
- [Block](https://easemob.github.io/easemob-im-server-sdk/com/easemob/im/server/api/block/BlockApi.html)
用于限制访问(将用户加入黑名单、群组/聊天室禁言等)
- [Contact](https://easemob.github.io/easemob-im-server-sdk/com/easemob/im/server/api/contact/ContactApi.html)
用于管理联系人(添加好友等)
- [Group](https://easemob.github.io/easemob-im-server-sdk/com/easemob/im/server/api/group/GroupApi.html)
用于管理群组
- [Message](https://easemob.github.io/easemob-im-server-sdk/com/easemob/im/server/api/message/MessageApi.html)
用于发送消息
- [User](https://easemob.github.io/easemob-im-server-sdk/com/easemob/im/server/api/user/UserApi.html)
用于管理用户
- [UserMetadata](https://easemob.github.io/easemob-im-server-sdk/com/easemob/im/server/api/metadata/MetadataApi.html)
用于管理用户属性
- [Push](https://easemob.github.io/easemob-im-server-sdk/com/easemob/im/server/api/push/PushApi.html)
用于管理用户推送(设置推送免打扰等)
- [Token](https://easemob.github.io/easemob-im-server-sdk/com/easemob/im/server/api/token/TokenApi.html)
用于获取用户Token
- [Room](https://easemob.github.io/easemob-im-server-sdk/com/easemob/im/server/api/room/RoomApi.html)
用于管理聊天室

每个业务资源对应一个方法,例如,用户相关的 API,都可以在 `.user()` 找到。

举个例子,我们要注册一个用户,就可以这样写:

``` java
@Service
public class UserService {

@Autowired
private EMService service;

private void createUser() {
try {
EMUser user = service.user().create("username", "password").block();
} catch (EMException e) {
e.getErrorCode();
e.getMessage();
}

}
}
```

API 的返回值是响应式的,如果希望阻塞,可以使用上面例子中的 `.block()`

注意:如果你的项目不是响应式的编程,那么请在调用的 server sdk api 的结尾添加 `.block()`

对使用的 API 添加 `try/catch` ,如果使用的 API 没有抛出异常,代表请求成功,反之则请求失败,通过 `EMException` 异常对象的 `getErrorCode()/getMessage()` 拿到错误码以及错误描述。

## 参考

SDK 的 api 文档在[这里](https://easemob.github.io/easemob-im-server-sdk/)

## 常见问题
如果你想看 Server SDK 的请求与响应,可以在配置文件中添加:
``` java
logging.level.com.easemob.im.http=debug
```

## 注意事项

Server SDK 是对环信 IM [服务端 API](http://docs-im.easemob.com/im/server/ready/intro) 的封装,但并没有封装所有的 API,只封装了开发者常用的 API。

对于注册环信id的规则,Server SDK 有自己的限制, 正则为 `^[a-z][0-9a-z-]{1,32}$`,这点与 [官网文档](https://docs-im.easemob.com/im/server/ready/user#%E7%8E%AF%E4%BF%A1_id_%E4%BD%BF%E7%94%A8%E8%A7%84%E5%88%99) 中说明的环信id规则是有区别的,这样做是因为目前环信id注册的限制范围比较广,Server SDK 考虑缩小环信 id 注册的限制范围使其更加规范,在此说明一下。

如果不想使用 Server SDK 注册环信 id 的限制,可以在初始化配置时添加 `turnOffUserNameValidation()` (sdk 需要使用 0.3.5 以上的版本)。

强烈建议不要使用纯数字或者有规律的字符串来注册环信 id,否则自己的用户可能会遭受到攻击、垃圾消息等后果。
``` java
EMProperties properties = EMProperties.builder()
.setAppkey("appkey")
.setClientId("Client ID")
.setClientSecret("ClientSecret")
.turnOffUserNameValidation()
.build();
```


168 changes: 168 additions & 0 deletions build.gradle
Original file line number Diff line number Diff line change
@@ -0,0 +1,168 @@
apply plugin: 'idea'
apply plugin: 'eclipse'
apply plugin: 'java'
apply plugin: 'com.diffplug.spotless'

group = 'com.easemob.im'
version = '1.0.0'

buildscript {
repositories {
mavenCentral()
}
dependencies {
classpath 'com.android.tools.build:gradle:2.3.+'
classpath 'com.github.dcendents:android-maven-gradle-plugin:2.1'
classpath 'com.diffplug.spotless:spotless-plugin-gradle:6.11.0'
}
}

repositories {
mavenCentral()
}
sourceSets {
main.java.srcDirs = ['src/main/java']
}

if(hasProperty('target') && target == 'android') {

apply plugin: 'com.android.library'
apply plugin: 'com.github.dcendents.android-maven'

android {
compileSdkVersion 25
buildToolsVersion '25.0.2'
defaultConfig {
minSdkVersion 14
targetSdkVersion 25
}
compileOptions {
sourceCompatibility JavaVersion.VERSION_1_8
targetCompatibility JavaVersion.VERSION_1_8
}

// Rename the aar correctly
libraryVariants.all { variant ->
variant.outputs.each { output ->
def outputFile = output.outputFile
if (outputFile != null && outputFile.name.endsWith('.aar')) {
def fileName = "${project.name}-${variant.baseName}-${version}.aar"
output.outputFile = new File(outputFile.parent, fileName)
}
}
}

dependencies {
provided "jakarta.annotation:jakarta.annotation-api:$jakarta_annotation_version"
}
}

afterEvaluate {
android.libraryVariants.all { variant ->
def task = project.tasks.create "jar${variant.name.capitalize()}", Jar
task.description = "Create jar artifact for ${variant.name}"
task.dependsOn variant.javaCompile
task.from variant.javaCompile.destinationDirectory
task.destinationDirectory = project.file("${project.buildDir}/outputs/jar")
task.archiveFileName = "${project.name}-${variant.baseName}-${version}.jar"
artifacts.add('archives', task)
}
}

task sourcesJar(type: Jar) {
from android.sourceSets.main.java.srcDirs
classifier = 'sources'
}

artifacts {
archives sourcesJar
}

} else {

apply plugin: 'java'
apply plugin: 'maven-publish'

sourceCompatibility = JavaVersion.VERSION_1_8
targetCompatibility = JavaVersion.VERSION_1_8

publishing {
publications {
maven(MavenPublication) {
artifactId = 'im-sdk-core'
from components.java
}
}
}

task execute(type:JavaExec) {
main = System.getProperty('mainClass')
classpath = sourceSets.main.runtimeClasspath
}
}

ext {
jakarta_annotation_version = "1.3.5"
}

dependencies {
implementation 'io.swagger:swagger-annotations:1.6.8'
implementation "com.google.code.findbugs:jsr305:3.0.2"
implementation 'com.squareup.okhttp3:okhttp:4.10.0'
implementation 'com.squareup.okhttp3:logging-interceptor:4.10.0'
implementation 'com.google.code.gson:gson:2.9.1'
implementation 'io.gsonfire:gson-fire:1.9.0'
implementation 'javax.ws.rs:jsr311-api:1.1.1'
implementation 'javax.ws.rs:javax.ws.rs-api:2.1.1'
implementation 'org.openapitools:jackson-databind-nullable:0.2.6'
implementation group: 'org.apache.commons', name: 'commons-lang3', version: '3.12.0'
implementation "jakarta.annotation:jakarta.annotation-api:$jakarta_annotation_version"
testImplementation 'org.junit.jupiter:junit-jupiter-api:5.9.1'
testImplementation 'org.mockito:mockito-core:3.12.4'
testRuntimeOnly 'org.junit.jupiter:junit-jupiter-engine:5.9.1'
}

javadoc {
options.tags = [ "http.response.details:a:Http Response Details" ]
}

// Use spotless plugin to automatically format code, remove unused import, etc
// To apply changes directly to the file, run `gradlew spotlessApply`
// Ref: https://github.com/diffplug/spotless/tree/main/plugin-gradle
spotless {
// comment out below to run spotless as part of the `check` task
enforceCheck false

format 'misc', {
// define the files (e.g. '*.gradle', '*.md') to apply `misc` to
target '.gitignore'

// define the steps to apply to those files
trimTrailingWhitespace()
indentWithSpaces() // Takes an integer argument if you don't like 4
endWithNewline()
}
java {
// don't need to set target, it is inferred from java

// apply a specific flavor of google-java-format
googleJavaFormat('1.8').aosp().reflowLongStrings()

removeUnusedImports()
importOrder()
}
}

test {
// Enable JUnit 5 (Gradle 4.6+).
useJUnitPlatform()

// Always run tests, even when nothing changed.
dependsOn 'cleanTest'

// Show test results.
testLogging {
events "passed", "skipped", "failed"
}

}
Loading

0 comments on commit f97a7c2

Please sign in to comment.