import { messageListApi } from "@/api/siteMessage";
import { store } from "@/store/store";

export async function fetchSiteMessage() {
  const res = await messageListApi(store.user.id, store.siteMessagesTimeStamp);
  const messages = res.data.map((item) => {
    const nitem = { ...item };
    if (item.hasRecalled) {
      nitem.title = "发送者已撤回消息：" + nitem.title;
      nitem.content = "该消息已经被发送者撤回。";
    }
    return nitem;
  });
  const prevSiteMessage = Object.assign(
    {},
    {
      messages: [],
      firstUnreadMessage: null,
      ignoreMessageIds: [],
      unreadCount: 0,
    },
    store.siteMessage
  );
  store.siteMessage = Object.assign({}, prevSiteMessage, { messages });
}
