No Description

WsApi.ts 4.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  1. import { WebSocketClient } from './../WebSocketClient';
  2. import { MsgBase, GroupOption } from '../protocol/MsgBase';
  3. import { CmdMsg } from '../protocol/CmdMsg';
  4. /** WebSocket Api */
  5. export class WsApi extends WebSocketClient {
  6. // 保存公共的状态信息
  7. token: string;
  8. from: string;
  9. async sendMsg(msg: Partial<MsgBase>) {
  10. return this.sendMessage(msg);
  11. }
  12. /** @start 好友管理 */
  13. // 通用查询
  14. async sendGetCmdMsg(action: string, extra: {} = {}) {
  15. const msg = CmdMsg.createMsg({ from: this.from, action, ...extra });
  16. return this.sendMsg(msg);
  17. }
  18. // 通用操作
  19. async sendPostCmdMsg(action: string, userIds: string[] | null, extra: {} = {}) {
  20. const msg = CmdMsg.createMsg({
  21. from: this.from,
  22. to: (userIds || []).join(','),
  23. action,
  24. ...extra
  25. });
  26. return this.sendMsg(msg);
  27. }
  28. // 查询好友列表
  29. getRoster() {
  30. return this.sendGetCmdMsg('getRoster');
  31. }
  32. // 添加好友
  33. addFriends(userIds: string[]) {
  34. return this.sendPostCmdMsg('addFriends', userIds);
  35. }
  36. // 同意好友请求
  37. agreeFriends(userIds: string[]) {
  38. return this.sendPostCmdMsg('agreeFriends', userIds);
  39. }
  40. // 拒绝好友请求
  41. rejectFriends(userIds: string[]) {
  42. return this.sendPostCmdMsg('rejectFriends', userIds);
  43. }
  44. // 移除
  45. removeFriends(userIds: string[]) {
  46. return this.sendPostCmdMsg('removeFriends', userIds);
  47. }
  48. // 获取黑名单列表
  49. getBlacklist() {
  50. return this.sendGetCmdMsg('getBlacklist');
  51. }
  52. // 加入黑名单
  53. addToBlackList(userIds: string[]) {
  54. return this.sendPostCmdMsg('addToBlackList', userIds);
  55. }
  56. // 移出黑名单
  57. removeBlackList(userIds: string[]) {
  58. return this.sendPostCmdMsg('removeBlackList', userIds);
  59. }
  60. /** @end 好友管理 */
  61. /** @start 会话管理 */
  62. // 删除会话
  63. deleteConversation(contactId: string, deleteMessage: boolean = false) {
  64. return this.request('/v1/delete/conversation', {
  65. contact: contactId,
  66. delete_message: deleteMessage
  67. });
  68. }
  69. // 标记会话消息为已读
  70. markMessageRead(contactId: string) {
  71. return this.request('/v1/mark/messages/read', {
  72. contact: contactId
  73. });
  74. }
  75. /** @end 会话管理 */
  76. /** @start 群组管理 */
  77. // 列出所有的群组
  78. listGroups() {
  79. return this.sendGetCmdMsg('listGroups');
  80. }
  81. // 获取群组信息
  82. queryGroupInfo(roomId: string) {
  83. return this.sendGetCmdMsg('queryGroupInfo', { to: roomId });
  84. }
  85. // 查询群组的成员
  86. queryRoomMember(roomId: string) {
  87. return this.sendGetCmdMsg('queryRoomMember', { to: roomId });
  88. }
  89. // 获取群组黑名单
  90. getGroupBlackList(roomId: string) {
  91. return this.sendGetCmdMsg('getGroupBlackList', { to: roomId });
  92. }
  93. // 建立群组
  94. createGroup(groupOption: GroupOption) {
  95. return this.sendPostCmdMsg('createGroup', null, { ext: groupOption });
  96. }
  97. // 更新群组
  98. changeGroupInfo(groupOption: GroupOption) {
  99. return this.sendPostCmdMsg('changeGroupInfo', null, { ext: groupOption });
  100. }
  101. // 将好友加入群组
  102. addGroupMembers(roomId: string, userIds: string[]) {
  103. return this.sendPostCmdMsg('addGroupMembers', [roomId], {
  104. ext: {
  105. members: userIds
  106. }
  107. });
  108. }
  109. // 将成员踢出群组
  110. addToGroupBlackList(roomId: string, userIds: string[]) {
  111. return this.sendPostCmdMsg('addToGroupBlackList', [roomId], {
  112. ext: {
  113. members: userIds
  114. }
  115. });
  116. }
  117. // 解散群组
  118. destroyGroup(roomId: string) {
  119. return this.sendPostCmdMsg('destroyGroup', [roomId]);
  120. }
  121. // 退出群组
  122. leaveGroup(roomId: string) {
  123. return this.sendPostCmdMsg('leaveGroup', [roomId]);
  124. }
  125. /** @end 群组管理 */
  126. // 创建聊天室群组
  127. createRoom(groupOption: GroupOption) {
  128. return this.sendPostCmdMsg('createGroup', null, { ext: groupOption });
  129. }
  130. // 列出所有的聊天室
  131. listRooms() {
  132. return this.sendGetCmdMsg('listRooms');
  133. }
  134. // 退出聊天室
  135. joinRoom(roomId: string) {
  136. return this.sendPostCmdMsg('joinRoom', [roomId]);
  137. }
  138. // 退出聊天室
  139. quitRoom(roomId: string) {
  140. return this.sendPostCmdMsg('quitRoom', [roomId]);
  141. }
  142. /** @start 聊天室管理 */
  143. /** @end 聊天室管理 */
  144. }