All files / app/service conversations.ts

20.61% Statements 20/97
0% Branches 0/14
0% Functions 0/16
21.73% Lines 20/92

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183      95x 95x                                 95x   95x 95x 95x   94x                                 94x             94x                             94x                         94x             94x           94x                   94x                   94x                   94x                         94x                   94x                                     94x                 94x                  
import {
  BoolValue,
  StringValue,
} from "google-protobuf/google/protobuf/wrappers_pb";
import { StatusCode } from "grpc-web";
import { LiteUser, User } from "proto/api_pb";
import {
  CreateGroupChatReq,
  EditGroupChatReq,
  GetDirectMessageReq,
  GetGroupChatMessagesReq,
  GetGroupChatReq,
  InviteToGroupChatReq,
  LeaveGroupChatReq,
  ListGroupChatsReq,
  MakeGroupChatAdminReq,
  MarkLastSeenGroupChatReq,
  MuteGroupChatReq,
  RemoveGroupChatAdminReq,
  SendMessageReq,
  SetGroupChatArchiveStatusReq,
} from "proto/conversations_pb";
 
import client from "./client";
import { Duration, duration2pb } from "./utils/date";
import isGrpcError from "./utils/isGrpcError";
 
export async function listGroupChats(
  lastMessageId = 0,
  count = 10,
  onlyArchived?: boolean,
) {
  const req = new ListGroupChatsReq();
  req.setLastMessageId(lastMessageId);
  req.setNumber(count);
  Iif (onlyArchived !== undefined) {
    req.setOnlyArchived(onlyArchived);
  }
 
  const response = await client.conversations.listGroupChats(req);
 
  return response.toObject();
}
 
export async function getGroupChat(id: number) {
  const req = new GetGroupChatReq();
  req.setGroupChatId(id);
  const response = await client.conversations.getGroupChat(req);
  return response.toObject();
}
 
export async function getGroupChatMessages(
  groupChatId: number,
  lastMessageId = 0,
  count = 20,
) {
  const req = new GetGroupChatMessagesReq();
  req.setGroupChatId(groupChatId);
  req.setLastMessageId(lastMessageId);
  req.setNumber(count);
 
  const response = await client.conversations.getGroupChatMessages(req);
 
  return response.toObject();
}
 
export async function createGroupChat(
  title: string,
  users: User.AsObject[],
): Promise<number> {
  const req = new CreateGroupChatReq();
  req.setRecipientUserIdsList(users.map((user) => user.userId));
  req.setTitle(new StringValue().setValue(title));
  const response = await client.conversations.createGroupChat(req);
  const groupChatId = response.getGroupChatId();
 
  return groupChatId;
}
 
export async function sendMessage(groupChatId: number, text: string) {
  const req = new SendMessageReq();
  req.setGroupChatId(groupChatId);
  req.setText(text);
  return await client.conversations.sendMessage(req);
}
 
export function leaveGroupChat(groupChatId: number) {
  const req = new LeaveGroupChatReq();
  req.setGroupChatId(groupChatId);
  return client.conversations.leaveGroupChat(req);
}
 
export function inviteToGroupChat(groupChatId: number, users: User.AsObject[]) {
  const promises = users.map((user) => {
    const req = new InviteToGroupChatReq();
    req.setGroupChatId(groupChatId);
    req.setUserId(user.userId);
    return client.conversations.inviteToGroupChat(req);
  });
  return Promise.all(promises);
}
 
export function makeGroupChatAdmin(
  groupChatId: number,
  user: LiteUser.AsObject,
) {
  const req = new MakeGroupChatAdminReq();
  req.setGroupChatId(groupChatId);
  req.setUserId(user.userId);
  return client.conversations.makeGroupChatAdmin(req);
}
 
export function removeGroupChatAdmin(
  groupChatId: number,
  user: LiteUser.AsObject,
) {
  const req = new RemoveGroupChatAdminReq();
  req.setGroupChatId(groupChatId);
  req.setUserId(user.userId);
  return client.conversations.removeGroupChatAdmin(req);
}
 
export function editGroupChat(
  groupChatId: number,
  title?: string,
  onlyAdminsInvite?: boolean,
) {
  const req = new EditGroupChatReq();
  req.setGroupChatId(groupChatId);
  Iif (title !== undefined) req.setTitle(new StringValue().setValue(title));
  Iif (onlyAdminsInvite !== undefined)
    req.setOnlyAdminsInvite(new BoolValue().setValue(onlyAdminsInvite));
  return client.conversations.editGroupChat(req);
}
 
export function markLastSeenGroupChat(
  groupChatId: number,
  lastSeenMessageId: number,
) {
  const req = new MarkLastSeenGroupChatReq();
  req.setGroupChatId(groupChatId);
  req.setLastSeenMessageId(lastSeenMessageId);
  return client.conversations.markLastSeenGroupChat(req);
}
 
export async function getDirectMessage(userId: number) {
  const req = new GetDirectMessageReq();
  req.setUserId(userId);
  try {
    const res = await client.conversations.getDirectMessage(req);
    return res.getGroupChatId();
  } catch (e) {
    if (isGrpcError(e) && e.code === StatusCode.NOT_FOUND) {
      return false;
    } else {
      throw e;
    }
  }
}
 
export type MuteChatOptions = Pick<MuteGroupChatReq.AsObject, "groupChatId"> &
  Partial<Omit<MuteGroupChatReq.AsObject, "groupChatId" | "forDuration">> & {
    forDuration?: Duration;
  };
export async function muteChat(options: MuteChatOptions) {
  const req = new MuteGroupChatReq();
  req.setGroupChatId(options.groupChatId);
  Iif (options.unmute) req.setUnmute(true);
  Iif (options.forever) req.setForever(true);
  Iif (options.forDuration) req.setForDuration(duration2pb(options.forDuration));
  return client.conversations.muteGroupChat(req);
}
 
export async function setGroupChatArchiveStatus(
  groupChatId: number,
  isArchived: boolean,
) {
  const req = new SetGroupChatArchiveStatusReq();
  req.setGroupChatId(groupChatId);
  req.setIsArchived(isArchived);
  return client.conversations.setGroupChatArchiveStatus(req);
}