public static void appendSysMsgToMsgBox(frm_group frmGroup, string content, DateTime time)
 {
     if (!frmGroup.IsHandleCreated)
     {
         frmGroup.rtxt_message.AppendRichText("[系统消息]" + "  " + time.ToString("yyyy-MM-dd HH:mm:ss") + "\r\n",
                                              new Font(frmGroup.Font, FontStyle.Regular), Color.Blue);
         frmGroup.rtxt_message.AppendText(content + "\r\n");
         frmGroup.rtxt_message.ForeColor = Color.Black;
         frmGroup.rtxt_message.Select(frmGroup.rtxt_message.Text.Length, 0);
         frmGroup.rtxt_message.ScrollToCaret();
         frmGroup.rtxt_send.Focus();
     }
     else
     {
         frmGroup.Invoke(new Action(() =>
         {
             frmGroup.rtxt_message.AppendRichText("[系统消息]" + "  " + time.ToString("yyyy-MM-dd HH:mm:ss") + "\r\n",
                                                  new Font(frmGroup.Font, FontStyle.Regular), Color.Blue);
             frmGroup.rtxt_message.AppendText(content + "\r\n");
             frmGroup.rtxt_message.ForeColor = Color.Black;
             frmGroup.rtxt_message.Select(frmGroup.rtxt_message.Text.Length, 0);
             frmGroup.rtxt_message.ScrollToCaret();
             frmGroup.rtxt_send.Focus();
         }));
     }
 }
        public static Image appendImageToMsgBox(frm_group frmGroup, string name, DateTime time, byte[] imageData)
        {
            var image = Image.FromStream(new MemoryStream(imageData));

            if (!frmGroup.IsHandleCreated)
            {
                frmGroup.rtxt_message.AppendRichText(name + "  " + time.ToString("yyyy-MM-dd HH:mm:ss") + "\r\n",
                                                     new Font(frmGroup.Font, FontStyle.Regular), Color.Green);
                frmGroup.rtxt_message.InsertImage(image);
                frmGroup.rtxt_message.AppendText("\n");
                frmGroup.rtxt_message.Select(frmGroup.rtxt_message.Text.Length, 0);
                frmGroup.rtxt_message.ScrollToCaret();
                frmGroup.rtxt_send.Focus();
            }
            else
            {
                frmGroup.Invoke(new Action(() =>
                {
                    frmGroup.rtxt_message.AppendRichText(name + "  " + time.ToString("yyyy-MM-dd HH:mm:ss") + "\r\n",
                                                         new Font(frmGroup.Font, FontStyle.Regular), Color.Green);
                    frmGroup.rtxt_message.InsertImage(image);
                    frmGroup.rtxt_message.AppendText("\n");
                    frmGroup.rtxt_message.Select(frmGroup.rtxt_message.Text.Length, 0);
                    frmGroup.rtxt_message.ScrollToCaret();
                    frmGroup.rtxt_send.Focus();
                }));
            }
            return(image);
        }
 public static void appendFileToMsgBox(frm_group frmGroup, string name, DateTime time, string fileName, long objId)
 {
     if (!frmGroup.IsHandleCreated)
     {
         frmGroup.rtxt_message.AppendRichText(name + "  " + time.ToString("yyyy-MM-dd HH:mm:ss") + "\r\n",
                                              new Font(frmGroup.Font, FontStyle.Regular), Color.Green);
         frmGroup.rtxt_message.AppendText(fileName);
         frmGroup.rtxt_message.AppendText("\n");
         frmGroup.rtxt_message.InsertLink("下载文件[" + objId + "]");
         frmGroup.rtxt_message.AppendText("\n");
         frmGroup.rtxt_message.Select(frmGroup.rtxt_message.Text.Length, 0);
         frmGroup.rtxt_message.ScrollToCaret();
         frmGroup.rtxt_send.Focus();
     }
     else
     {
         frmGroup.Invoke(new Action(() =>
         {
             frmGroup.rtxt_message.AppendRichText(name + "  " + time.ToString("yyyy-MM-dd HH:mm:ss") + "\r\n",
                                                  new Font(frmGroup.Font, FontStyle.Regular), Color.Green);
             frmGroup.rtxt_message.AppendText(fileName);
             frmGroup.rtxt_message.AppendText("\n");
             frmGroup.rtxt_message.InsertLink("下载文件[" + objId + "]");
             frmGroup.rtxt_message.AppendText("\n");
             frmGroup.rtxt_message.Select(frmGroup.rtxt_message.Text.Length, 0);
             frmGroup.rtxt_message.ScrollToCaret();
             frmGroup.rtxt_send.Focus();
         }));
     }
 }
        public static void appendCenteralText(frm_group frmGroup, string msg)
        {
            var len = frmGroup.rtxt_message.TextLength;

            frmGroup.rtxt_message.AppendRichText(msg, new Font(frmGroup.Font, FontStyle.Regular), Color.Red);
            frmGroup.rtxt_message.Select(len, 0);
            frmGroup.rtxt_message.SelectionAlignment = HorizontalAlignment.Center;
            frmGroup.rtxt_message.AppendText("\n");
            frmGroup.rtxt_message.SelectionAlignment = HorizontalAlignment.Left;
        }
 public static void appendRtfToMsgBox(frm_group frmGroup, string name, DateTime time, string content)
 {
     if (!frmGroup.IsHandleCreated)
     {
         frmGroup.rtxt_message.AppendRichText(name + "  " + time.ToString("yyyy-MM-dd HH:mm:ss") + "\r\n",
                                              new Font(frmGroup.Font, FontStyle.Regular), Color.Green);
         frmGroup.rtxt_message.AppendRtf(content);
         frmGroup.rtxt_message.Select(frmGroup.rtxt_message.Text.Length, 0);
         frmGroup.rtxt_message.ScrollToCaret();
         frmGroup.rtxt_send.Focus();
     }
     else
     {
         frmGroup.Invoke(new Action(() =>
         {
             frmGroup.rtxt_message.AppendRichText(name + "  " + time.ToString("yyyy-MM-dd HH:mm:ss") + "\r\n",
                                                  new Font(frmGroup.Font, FontStyle.Regular), Color.Green);
             frmGroup.rtxt_message.AppendRtf(content);
             frmGroup.rtxt_message.Select(frmGroup.rtxt_message.Text.Length, 0);
             frmGroup.rtxt_message.ScrollToCaret();
             frmGroup.rtxt_send.Focus();
         }));
     }
 }
        private void clb_unread_msg_ClickSubItem(object sender, CCWin.SkinControl.ChatListClickEventArgs e, MouseEventArgs es)
        {
            if (es.Button != MouseButtons.Left)
            {
                return;
            }
            var dict = (Dictionary <string, object>)e.SelectSubItem.Tag;
            var type = (long)dict["type"];

            switch (type)
            {
            case (long)Class1.UnReadMsgType.Message:
            {
                var msgType = (int)dict["msg_type"];
                switch (msgType)
                {
                case (int)Class1.MsgType.PrivateChat:
                {
                    var      ids        = (List <long>)dict["ids"];
                    var      userIdSend = (long)dict["user_id_send"];
                    frm_chat frm_chat;
                    if (Class1.formChatPool.ContainsKey(userIdSend))
                    {
                        frm_chat = Class1.formChatPool[userIdSend];
                        frm_chat.Activate();
                    }
                    else
                    {
                        frm_chat = new frm_chat(userIdSend);
                        Class1.formChatPool.Add(userIdSend, frm_chat);
                        frm_chat.Show();
                    }
                    var updateFriendLastReadMsgIdReq = new http._User_Relation.UpdateFriendLastReadMsgIdReq()
                    {
                        UserId        = Class1.UserId,
                        LastReadMsgId = new Dictionary <long, long>()
                    };
                    foreach (var id in ids)
                    {
                        if (updateFriendLastReadMsgIdReq.LastReadMsgId.ContainsKey(userIdSend))
                        {
                            updateFriendLastReadMsgIdReq.LastReadMsgId[userIdSend] = Math.Max(updateFriendLastReadMsgIdReq.LastReadMsgId[userIdSend], id);
                        }
                        else
                        {
                            updateFriendLastReadMsgIdReq.LastReadMsgId.Add(userIdSend, id);
                        }
                    }
                    http._User_Relation.UpdateFriendLastReadMsgId(updateFriendLastReadMsgIdReq);


                    var subItem = Class1.unReadPrivateMsgItemPool[userIdSend];
                    clb_unread_msg.Items[0].SubItems.Remove(subItem);
                    Class1.unReadPrivateMsgItemPool.Remove(userIdSend);
                    break;
                }

                case (int)Class1.MsgType.GroupChat:
                {
                    var       maxId   = (long)dict["max_id"];
                    var       groupId = (long)dict["group_id"];
                    frm_group frmGroup;
                    if (Class1.formGroupPool.ContainsKey(groupId))
                    {
                        frmGroup = Class1.formGroupPool[groupId];
                        frmGroup.Activate();
                    }
                    else
                    {
                        frmGroup = new frm_group(Class1.GetGroupInfo(groupId));
                        Class1.formGroupPool.Add(groupId, frmGroup);
                        frmGroup.Show();
                    }
                    var req = new http._Group_User.UpdateGroupLastReadMsgIdReq()
                    {
                        UserId        = Class1.UserId,
                        LastReadMsgId = new Dictionary <long, long>()
                        {
                            { groupId, maxId }
                        }
                    };
                    http._Group_User.UpdateGroupLastReadMsgId(req);
                    var subItem = Class1.unReadGroupMsgItemPool[groupId];
                    clb_unread_msg.Items[2].SubItems.Remove(subItem);
                    Class1.unReadGroupMsgItemPool.Remove(groupId);
                    break;
                }

                default:
                    throw new Exception("unknown msg_type");
                }
                break;
            }

            case (long)Class1.UnReadMsgType.AddFriend:
            {
                var userIdSend = (long)dict["user_id_send"];
                if (!Class1.IsOnline)
                {
                    MessageBox.Show("你目前处于离线状态,暂时无法使用此功能", "", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                    return;
                }
                var id  = (long)dict["id"];
                var ret = MessageBox.Show($"{Class1.GetUserInfo(userIdSend).NickName}({userIdSend})" + "请求添加为好友,是否接受?", "", MessageBoxButtons.YesNoCancel, MessageBoxIcon.Information);
                int status;
                if (ret == DialogResult.Yes)
                {
                    status = (int)Class1.AddFriendStatus.Accepted;
                }
                else if (ret == DialogResult.No)
                {
                    status = (int)Class1.AddFriendStatus.Rejected;
                }
                else
                {
                    return;
                }

                var req = new http._User_Relation.ReplyAddFriendReq()
                {
                    Id     = id,
                    Status = status
                };
                var  resp       = http._User_Relation.ReplyAddFriend(req);
                var  replyId    = resp.Id;
                var  createTime = resp.CreateTime;
                bool ok         = Class1.sql.ExecuteNonQuery($"UPDATE `user_relation_request` SET `status` = {status} WHERE `id` = {id}");
                if (!ok)
                {
                    MessageBox.Show("DB错误,UPDATE user_relation_request", "信息", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                    return;
                }
                ok = Class1.sql.ExecuteNonQuery($"INSERT INTO `user_relation_request`(id, user_id_send, user_id_recv, create_time, status, parent_id) VALUES({replyId}, " +
                                                $"{Class1.UserId}, {userIdSend}, {createTime}, 0, {id})");
                if (!ok)
                {
                    MessageBox.Show("DB错误,INSERT INTO user_relation_request", "信息", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                    return;
                }
                clb_unread_msg.Items[1].SubItems.Remove(e.SelectSubItem);
                break;
            }

            case (long)Class1.UnReadMsgType.ReplyAddFriend:
            {
                var id = (long)dict["id"];
                Class1.UpdateMessageStatus(new Dictionary <long, int> {
                        { id, 1 }
                    }, new Dictionary <long, int>());
                clb_unread_msg.Items[1].SubItems.Remove(e.SelectSubItem);
                break;
            }

            case (long)Class1.UnReadMsgType.AddGroup:
            {
                var userIdSend = (long)dict["user_id_send"];
                if (!Class1.IsOnline)
                {
                    MessageBox.Show("你目前处于离线状态,暂时无法使用此功能", "", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                    return;
                }
                var id      = (long)dict["id"];
                var groupId = (long)dict["group_id"];
                var ret     = MessageBox.Show($"{Class1.GetUserInfo(userIdSend).NickName}({userIdSend})" + "请求加入群聊" + $"{Class1.GetGroupInfo(groupId).Name}({groupId})" + ",是否接受?", "", MessageBoxButtons.YesNoCancel, MessageBoxIcon.Information);
                int status;
                if (ret == DialogResult.Yes)
                {
                    status = (int)Class1.AddFriendStatus.Accepted;
                }
                else if (ret == DialogResult.No)
                {
                    status = (int)Class1.AddFriendStatus.Rejected;
                }
                else
                {
                    return;
                }

                var req = new http._Group_User.ReplyAddGroupReq()
                {
                    Id     = id,
                    Status = status
                };
                var  resp       = http._Group_User.ReplyAddGroup(req);
                var  replyId    = resp.Id;
                var  createTime = resp.CreateTime;
                bool ok         = Class1.sql.ExecuteNonQuery($"UPDATE `group_user_request` SET `status` = {status} WHERE `id` = {id}");
                if (!ok)
                {
                    MessageBox.Show("DB错误,UPDATE group_user_request", "信息", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                    return;
                }
                ok = Class1.sql.ExecuteNonQuery($"INSERT INTO `group_user_request`(id, user_id_send, user_id_recv, group_id, create_time, status, parent_id, type) VALUES({replyId}, " +
                                                $"{Class1.UserId}, {userIdSend}, {groupId}, {createTime}, 0, {id}, {(int)Class1.GroupUserRequestType.AddGroup})");
                if (!ok)
                {
                    MessageBox.Show("DB错误,INSERT INTO group_user_request", "信息", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                    return;
                }
                clb_unread_msg.Items[1].SubItems.Remove(e.SelectSubItem);
                break;
            }

            case (long)Class1.UnReadMsgType.ReplyAddGroup:
            {
                var id = (long)dict["id"];
                Class1.UpdateMessageStatus(new Dictionary <long, int>(), new Dictionary <long, int> {
                        { id, 1 }
                    });
                clb_unread_msg.Items[1].SubItems.Remove(e.SelectSubItem);
                break;
            }

            case (long)Class1.UnReadMsgType.InviteAddGroup:
            {
                break;
            }

            case (long)Class1.UnReadMsgType.ReplyInviteAddGroup:
            {
                break;
            }

            default:
                throw new Exception("unknown UnReadMsgType");
            }
            icon.ChangeIconState();
        }
        public static void UploadImage(string localFileName, long groupId, frm_group frmGroup)
        {
            var eTag = Class1.GenerateMD5WithFilePath(localFileName);
            var req  = new http._Oss.GetSTSInfoReq()
            {
                UserId = Class1.UserId
            };
            var resp = http._Oss.GetSTSInfo(req);

            Thread t = new Thread(new ThreadStart(() =>
            {
                // 拿到STS临时凭证后,通过其中的安全令牌(SecurityToken)和临时访问密钥(AccessKeyId和AccessKeySecret)生成OSSClient。
                var client = new OssClient(Class1.EndPoint, resp.AccessKeyId, resp.AccessKeySecret, resp.SecurityToken);
                if (!client.DoesObjectExist(Class1.BucketName, eTag))
                {
                    try
                    {
                        using (var fs = File.Open(localFileName, FileMode.Open))
                        {
                            var putObjectRequest = new PutObjectRequest(Class1.BucketName, eTag, fs);
                            //putObjectRequest.StreamTransferProgress += UploadProgressCallback;
                            client.PutObject(putObjectRequest);
                        }
                        Console.WriteLine("Put object:{0} succeeded", eTag);
                    }
                    catch (OssException ex)
                    {
                        Console.WriteLine("Failed with error code: {0}; Error info: {1}. \nRequestID: {2}\tHostID: {3}",
                                          ex.ErrorCode, ex.Message, ex.RequestId, ex.HostId);
                    }
                    catch (Exception ex)
                    {
                        Console.WriteLine("Failed with error info: {0}", ex.Message);
                    }
                }

                var name = Path.GetFileName(localFileName);

                var sendMessageReq = new http._Message.SendMessageReq()
                {
                    UserIdSend  = Class1.UserId,
                    IdRecv      = groupId,
                    Content     = eTag,
                    ContentType = (int)Class1.ContentType.Image,
                    MessageType = (int)Class1.MsgType.GroupChat
                };
                var sendMessageResp = http._Message.SendMessage(sendMessageReq);
                var statusCode      = sendMessageResp.StatusCode;

                switch (statusCode)
                {
                case 1:
                    {
                        cls_group.appendSysMsgToMsgBox(frmGroup, "你尚未加入群聊,无法发送消息", DateTime.Now);
                        return;
                    }
                }


                var messageId  = sendMessageResp.Id;
                var createTime = sendMessageResp.CreateTime;
                var param      = Class1.Gzip(System.Text.Encoding.Default.GetBytes(eTag));
                var ret        = Class1.sql.ExecuteNonQueryWithBinary($"INSERT INTO `message`(id, user_id_send, user_id_recv, group_id, content, create_time, content_type, msg_type) VALUES({messageId}, " +
                                                                      $"{Class1.UserId}, 0, {groupId}, @param, {createTime}, {(int)Class1.ContentType.Image}, {(int)Class1.MsgType.GroupChat})", param);
                if (!ret)
                {
                    MessageBox.Show("DB错误,INSERT INTO message失败", "信息", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                    return;
                }
                var imageData  = Class1.LoadFile(localFileName);
                var imageParam = Class1.Gzip(imageData);
                Class1.InsertImageIfNotExists(eTag, imageParam);
                cls_group.appendImageToMsgBox(frmGroup, Class1.UserId.ToString(), Class1.StampToDateTime(createTime), imageData);
            }));

            t.Start();
        }
Example #8
0
        private void ChatListBox_DoubleClickSubItem(object sender, ChatListEventArgs e, MouseEventArgs es)
        {
            if (es.Button != MouseButtons.Left)
            {
                return;
            }
            var itemId = (long)e.SelectSubItem.ID;

            if (Class1.chatListSubItemPool.ContainsKey(itemId) && Class1.chatListSubItemPool[itemId] == e.SelectSubItem)
            {
                var      userId = itemId;
                frm_chat frm_chat;
                if (Class1.formChatPool.ContainsKey(userId))
                {
                    frm_chat = Class1.formChatPool[userId];
                    frm_chat.Activate();
                }
                else
                {
                    frm_chat = new frm_chat(userId);
                    Class1.formChatPool.Add(userId, frm_chat);
                    frm_chat.Show();
                }
                if (Class1.unReadPrivateMsgItemPool.ContainsKey(userId))
                {
                    var updateFriendLastReadMsgIdReq = new http._User_Relation.UpdateFriendLastReadMsgIdReq()
                    {
                        UserId        = Class1.UserId,
                        LastReadMsgId = new Dictionary <long, long>()
                    };
                    var subItem = Class1.unReadPrivateMsgItemPool[userId];
                    var ids     = ((List <long>)(((Dictionary <string, object>)subItem.Tag)["ids"]));
                    foreach (var id in ids)
                    {
                        if (updateFriendLastReadMsgIdReq.LastReadMsgId.ContainsKey(userId))
                        {
                            updateFriendLastReadMsgIdReq.LastReadMsgId[userId] = Math.Max(updateFriendLastReadMsgIdReq.LastReadMsgId[userId], id);
                        }
                        else
                        {
                            updateFriendLastReadMsgIdReq.LastReadMsgId.Add(userId, id);
                        }
                    }
                    http._User_Relation.UpdateFriendLastReadMsgId(updateFriendLastReadMsgIdReq);

                    Class1.frmMsgBox.clb_unread_msg.Items[0].SubItems.Remove(subItem);
                    Class1.unReadPrivateMsgItemPool.Remove(userId);
                    icon.ChangeIconState();
                }
            }
            else if (Class1.groupItemPool.ContainsKey(itemId) && Class1.groupItemPool[itemId] == e.SelectSubItem)
            {
                var       groupId = itemId;
                frm_group frmGroup;
                if (Class1.formGroupPool.ContainsKey(groupId))
                {
                    frmGroup = Class1.formGroupPool[groupId];
                    frmGroup.Activate();
                }
                else
                {
                    frmGroup = new frm_group(Class1.GetGroupInfo(groupId));
                    Class1.formGroupPool.Add(groupId, frmGroup);
                    frmGroup.Show();
                }
                if (Class1.unReadGroupMsgItemPool.ContainsKey(groupId))
                {
                    var subItem = Class1.unReadGroupMsgItemPool[groupId];
                    var maxId   = (long)((Dictionary <string, object>)subItem.Tag)["max_id"];
                    var req     = new http._Group_User.UpdateGroupLastReadMsgIdReq()
                    {
                        UserId        = Class1.UserId,
                        LastReadMsgId = new Dictionary <long, long>()
                        {
                            { groupId, maxId }
                        }
                    };
                    http._Group_User.UpdateGroupLastReadMsgId(req);

                    Class1.frmMsgBox.clb_unread_msg.Items[2].SubItems.Remove(subItem);
                    Class1.unReadGroupMsgItemPool.Remove(groupId);
                    icon.ChangeIconState();
                }
            }
        }