Esempio n. 1
0
        public async Task <IActionResult> Transpond([Required][FromBody] MessageForTransponder message)
        {
            //需要验证身份
            //返回是否转发成功
            //同样存在与Topic的联动
            int userId;

            if (HttpContext.User.Identity.IsAuthenticated)
            {
                userId = int.Parse(HttpContext.User.Claims.First().Value);
            }
            else
            {
                RestfulResult.RestfulData rr = new RestfulResult.RestfulData();
                rr.Code    = 200;
                rr.Message = "Need Authentication";
                return(new JsonResult(rr));
            }

            return(await Wrapper.wrap(async (OracleConnection conn) =>
            {
                //FUNC_TRANSPOND_MESSAGE(userID in INTEGER, message_content in VARCHAR2, transpondID in INTEGER, messageID out INTEGER)
                //return INTEGER
                string procedureName = "FUNC_TRANSPOND_MESSAGE";
                OracleCommand cmd = new OracleCommand(procedureName, conn);
                cmd.CommandType = CommandType.StoredProcedure;

                //Add return value
                OracleParameter p1 = new OracleParameter();
                p1 = cmd.Parameters.Add("state", OracleDbType.Int32);
                p1.Direction = ParameterDirection.ReturnValue;

                //Add first parameter userID
                OracleParameter p2 = new OracleParameter();
                p2 = cmd.Parameters.Add("userID", OracleDbType.Int32);
                p2.Direction = ParameterDirection.Input;
                p2.Value = userId;

                //Add second parameter message_content
                OracleParameter p3 = new OracleParameter();
                p3 = cmd.Parameters.Add("message_content", OracleDbType.Varchar2);
                p3.Direction = ParameterDirection.Input;
                p3.Value = message.message_content;

                //Add third parameter message_sender_user_id
                OracleParameter p4 = new OracleParameter();
                p4 = cmd.Parameters.Add("transpondID", OracleDbType.Int32);
                p4.Direction = ParameterDirection.Input;
                p4.Value = message.message_transpond_message_id;

                //Add fourth parameter message_id
                OracleParameter p5 = new OracleParameter();
                p5 = cmd.Parameters.Add("messageID", OracleDbType.Int32);
                p5.Direction = ParameterDirection.Output;

                await cmd.ExecuteReaderAsync();
                if (int.Parse(p1.Value.ToString()) == 0)
                {
                    throw new Exception("failed");
                }

                await TopicController.AddTopicsInTwitter(message.message_content, int.Parse(p5.Value.ToString()));
                await AtController.AddAtsInTwitter(message.message_content, int.Parse(p5.Value.ToString()), userId);

                RestfulResult.RestfulData rr = new RestfulResult.RestfulData(200, "success");
                return new JsonResult(rr);
            }));
        }
Esempio n. 2
0
        public async Task <IActionResult> QueryFollowMessage([Required][FromBody] Range range)
        {
            int userId;

            if (HttpContext.User.Identity.IsAuthenticated)
            {
                userId = int.Parse(HttpContext.User.Claims.First().Value);
            }
            else
            {
                RestfulResult.RestfulData rr = new RestfulResult.RestfulData();
                rr.Code    = 200;
                rr.Message = "Need Authentication";
                return(new JsonResult(rr));
            }

            return(await Wrapper.wrap(async (OracleConnection conn) =>
            {
                //function FUNC_SHOW_FOLLOW_MESSAGE(startFrom in INTEGER, limitation in INTEGER, userid in INTEGER, search_result out sys_refcursor)
                //return INTEGER
                string procedurename = "FUNC_SHOW_FOLLOW_MESSAGE";
                OracleCommand cmd = new OracleCommand(procedurename, conn);
                cmd.CommandType = CommandType.StoredProcedure;

                //Add return value
                OracleParameter p1 = new OracleParameter();
                p1 = cmd.Parameters.Add("state", OracleDbType.Int32);
                p1.Direction = ParameterDirection.ReturnValue;

                //Add first parameter startFrom
                OracleParameter p2 = new OracleParameter();
                p2 = cmd.Parameters.Add("startFrom", OracleDbType.Int32);
                p2.Direction = ParameterDirection.Input;
                p2.Value = range.startFrom;

                //Add second parameter limitation
                OracleParameter p3 = new OracleParameter();
                p3 = cmd.Parameters.Add("limitation", OracleDbType.Int32);
                p3.Direction = ParameterDirection.Input;
                p3.Value = range.limitation;

                OracleParameter p4 = new OracleParameter();
                p4 = cmd.Parameters.Add("userid", OracleDbType.Int32);
                p4.Direction = ParameterDirection.Input;
                p4.Value = userId;

                //Add third parameter search_result
                OracleParameter p5 = new OracleParameter();
                p5 = cmd.Parameters.Add("search_result", OracleDbType.RefCursor);
                p5.Direction = ParameterDirection.Output;

                //Get the result table
                OracleDataAdapter DataAdapter = new OracleDataAdapter(cmd);
                DataTable dt = new DataTable();
                await Task.FromResult(DataAdapter.Fill(dt));

                if (int.Parse(p1.Value.ToString()) == 0)
                {
                    throw new Exception("failed");
                }
                MessageForShow[] receivedTwitters = new MessageForShow[dt.Rows.Count];

                for (int i = 0; i < dt.Rows.Count; i++)
                {
                    receivedTwitters[i] = new MessageForShow();
                    receivedTwitters[i].message_id = int.Parse(dt.Rows[i][0].ToString());
                    receivedTwitters[i].message_content = dt.Rows[i][1].ToString();
                    receivedTwitters[i].message_create_time = dt.Rows[i][2].ToString();
                    receivedTwitters[i].message_like_num = int.Parse(dt.Rows[i][3].ToString());
                    receivedTwitters[i].message_transpond_num = int.Parse(dt.Rows[i][4].ToString());
                    receivedTwitters[i].message_comment_num = int.Parse(dt.Rows[i][5].ToString());
                    receivedTwitters[i].message_view_num = int.Parse(dt.Rows[i][6].ToString());
                    receivedTwitters[i].message_has_image = int.Parse(dt.Rows[i][7].ToString());
                    receivedTwitters[i].message_sender_user_id = int.Parse(dt.Rows[i][8].ToString());
                    receivedTwitters[i].message_heat = int.Parse(dt.Rows[i][9].ToString());
                    receivedTwitters[i].message_image_count = int.Parse(dt.Rows[i][10].ToString() == "" ? "0" : dt.Rows[i][10].ToString());
                    receivedTwitters[i].message_transpond_message_id = int.Parse(dt.Rows[i][11].ToString() == "" ? "0" : dt.Rows[i][11].ToString());

                    receivedTwitters[i].message_topics = await TopicController.SearchTopicsInTwitter(receivedTwitters[i].message_content);
                    receivedTwitters[i].message_ats = await AtController.SearchAtsInTwitter(receivedTwitters[i].message_content);
                }
                for (int i = 0; i < dt.Rows.Count; i++)
                {
                    if (receivedTwitters[i].message_has_image == 1)
                    {
                        string path = @"wwwroot\Messages\" + receivedTwitters[i].message_id.ToString() + @"\";
                        receivedTwitters[i].message_image_urls =
                            getMessageImageUrls(receivedTwitters[i].message_id, receivedTwitters[i].message_image_count);
                    }
                }

                RestfulResult.RestfulArray <MessageForShow> rr = new RestfulResult.RestfulArray <MessageForShow>();
                rr.Code = 200;
                rr.Message = "success";
                rr.Data = receivedTwitters;
                return new JsonResult(rr);
            }));
        }
Esempio n. 3
0
        public async Task <IActionResult> Send([Required][FromForm] MessageForSender message)
        {
            //TODO 需要验证身份
            //有很多参数都是有初始化的
            //!!!!!与Topic的联动
            //首先要检查message中是否有两个#号括起来的连续无空格字符串
            //若有,则去数据库中检索该Topic是否存在,若不存在则添加,若存在则将其热度提高

            int           userId;
            List <string> topics = new List <string>();
            List <string> ats    = new List <string>();

            System.Text.RegularExpressions.Regex topicRegex = new System.Text.RegularExpressions.Regex(@"#(\w+)#");
            System.Text.RegularExpressions.Regex atRegex    = new System.Text.RegularExpressions.Regex(@"@(\w+)");

            if (HttpContext.User.Identity.IsAuthenticated)
            {
                userId = int.Parse(HttpContext.User.Claims.First().Value);

                //检查message_content里含有的话题,用两个#包含的内容作为话题。若出现两个连续的#,则忽略之。
                //所有的话题内容会被保存到topics列表内,并在调用第二个函数FUNC_ADD_TOPIC时,逐一对topic的内容进行处理(不存在则创建,存在则热度+1)
                //对艾特内容同理
                System.Text.RegularExpressions.MatchCollection topicCollection = topicRegex.Matches(message.message_content);
                System.Text.RegularExpressions.MatchCollection atCollection    = atRegex.Matches(message.message_content);
                for (int i = 0; i < topicCollection.Count; i++)
                {
                    topics.Add(topicCollection[i].Groups[1].ToString());
                }
                for (int i = 0; i < atCollection.Count; i++)
                {
                    ats.Add(atCollection[i].Groups[1].ToString());
                }
            }
            else
            {
                RestfulResult.RestfulData rr = new RestfulResult.RestfulData();
                rr.Code    = 200;
                rr.Message = "Need Authentication";
                return(new JsonResult(rr));
            }

            using (OracleConnection conn = new OracleConnection(ConnStr.getConnStr()))
            {
                try
                {
                    conn.ConnectionString = ConnStr.getConnStr();
                    conn.Open();
                    //FUNC_SEND_MESSAGE(message_content in VARCHAR2, message_has_image in INTEGER, user_id in INTEGER, message_image_count in INTEGER, message_id out INTEGER)
                    //return INTEGER
                    string        procedureName = "FUNC_SEND_MESSAGE";
                    OracleCommand cmd           = new OracleCommand(procedureName, conn);
                    cmd.CommandType = CommandType.StoredProcedure;

                    //Add return value
                    OracleParameter p1 = new OracleParameter();
                    p1           = cmd.Parameters.Add("state", OracleDbType.Int32);
                    p1.Direction = ParameterDirection.ReturnValue;

                    //Add first parameter message_content
                    OracleParameter p2 = new OracleParameter();
                    p2           = cmd.Parameters.Add("message_content", OracleDbType.Varchar2);
                    p2.Direction = ParameterDirection.Input;
                    p2.Value     = message.message_content;

                    //Add second parameter message_has_image
                    OracleParameter p3 = new OracleParameter();
                    p3           = cmd.Parameters.Add("message_has_image", OracleDbType.Int32);
                    p3.Direction = ParameterDirection.Input;
                    p3.Value     = message.message_has_image;

                    //Add third parameter user_id
                    OracleParameter p4 = new OracleParameter();
                    p4           = cmd.Parameters.Add("user_id", OracleDbType.Int32);
                    p4.Direction = ParameterDirection.Input;
                    p4.Value     = userId;

                    //Add fourth parameter message_image_count
                    OracleParameter p5 = new OracleParameter();
                    p5           = cmd.Parameters.Add("message_image_count", OracleDbType.Int32);
                    p5.Direction = ParameterDirection.Input;
                    p5.Value     = message.message_image_count;

                    //Add fifth parameter message_id
                    OracleParameter p6 = new OracleParameter();
                    p6           = cmd.Parameters.Add("message_id", OracleDbType.Int32);
                    p6.Direction = ParameterDirection.Output;

                    await cmd.ExecuteReaderAsync();

                    if (int.Parse(p1.Value.ToString()) == 0)
                    {
                        throw new Exception("failed");
                    }

                    await TopicController.AddTopicsInTwitter(message.message_content, int.Parse(p6.Value.ToString()));

                    await AtController.AddAtsInTwitter(message.message_content, int.Parse(p6.Value.ToString()), userId);

                    //若推特含图,从POST体内获得图的内容并保存到服务器
                    if (message.message_has_image == 1)
                    {
                        var images  = Request.Form.Files;
                        int img_num = 0;
                        Directory.CreateDirectory(@"wwwroot\Messages\" + p6.Value.ToString());
                        foreach (var imgfile in images)
                        {
                            if (imgfile.Length > 0)
                            {
                                string img_path;
                                if (imgfile.ContentType.Substring(0, 5) == "image")
                                {
                                    img_path = @"wwwroot\Messages\" + p6.Value.ToString() + @"\" + img_num.ToString() + ".jpg";
                                }
                                else
                                {
                                    string videoFormat = imgfile.ContentType.Split("/")[1];
                                    img_path = @"wwwroot\Messages\" + p6.Value.ToString() + @"\" + img_num.ToString() + "." + videoFormat;
                                }
                                using (var stream = new FileStream(img_path, FileMode.Create))
                                {
                                    await imgfile.CopyToAsync(stream);
                                }
                                img_num++;
                            }
                        }
                    }

                    RestfulResult.RestfulData rr = new RestfulResult.RestfulData(200, "success");
                    conn.Close();
                    return(new JsonResult(rr));
                }
                catch (Exception e)
                {
                    RestfulResult.RestfulData rr = new RestfulResult.RestfulData(500, "fail");
                    Console.Write(e.Message);
                    Console.Write(e.StackTrace);
                    conn.Close();
                    return(new JsonResult(rr));
                }
            }
        }
Esempio n. 4
0
        public async Task <IActionResult> Query([Required] int message_id)
        {
            //获得推特的详细信息
            //无需验证登录态
            //除了基本的推特信息以外,我们需要根据这条推特是否含有图,来把MessageForShow的图片url列表填好

            return(await Wrapper.wrap(async (OracleConnection conn) =>
            {
                //function FUNC_SHOW_MESSAGE_BY_ID(message_id in INTEGER, result out sys_refcursor)
                //return INTEGER
                string procedurename = "FUNC_SHOW_MESSAGE_BY_ID";
                OracleCommand cmd = new OracleCommand(procedurename, conn);
                cmd.CommandType = CommandType.StoredProcedure;

                //Add return value
                OracleParameter p1 = new OracleParameter();
                p1 = cmd.Parameters.Add("state", OracleDbType.Int32);
                p1.Direction = ParameterDirection.ReturnValue;

                //Add first parameter message_id
                OracleParameter p2 = new OracleParameter();
                p2 = cmd.Parameters.Add("message_id", OracleDbType.Int32);
                p2.Direction = ParameterDirection.Input;
                p2.Value = message_id;

                //Add second parameter search_result
                OracleParameter p3 = new OracleParameter();
                p3 = cmd.Parameters.Add("result", OracleDbType.RefCursor);
                p3.Direction = ParameterDirection.Output;

                //Get the result table
                OracleDataAdapter DataAdapter = new OracleDataAdapter(cmd);
                DataTable dt = new DataTable();
                await Task.FromResult(DataAdapter.Fill(dt));

                if (int.Parse(p1.Value.ToString()) == 0)
                {
                    throw new Exception("failed");
                }
                MessageForShow infos = new MessageForShow();
                infos.message_id = int.Parse(dt.Rows[0][0].ToString());
                infos.message_content = dt.Rows[0][1].ToString();
                infos.message_create_time = dt.Rows[0][2].ToString();
                infos.message_like_num = int.Parse(dt.Rows[0][3].ToString());
                infos.message_transpond_num = int.Parse(dt.Rows[0][4].ToString());
                infos.message_comment_num = int.Parse(dt.Rows[0][5].ToString());
                infos.message_view_num = int.Parse(dt.Rows[0][6].ToString());
                infos.message_has_image = int.Parse(dt.Rows[0][7].ToString());
                infos.message_sender_user_id = int.Parse(dt.Rows[0][8].ToString());
                infos.message_heat = int.Parse(dt.Rows[0][9].ToString());
                infos.message_image_count = int.Parse(dt.Rows[0][10].ToString() == "" ? "0" : dt.Rows[0][10].ToString());
                infos.message_transpond_message_id = int.Parse(dt.Rows[0][11].ToString() == "" ? "0" : dt.Rows[0][11].ToString());

                infos.message_topics = await TopicController.SearchTopicsInTwitter(infos.message_content);
                infos.message_ats = await AtController.SearchAtsInTwitter(infos.message_content);

                if (infos.message_has_image == 1)
                {
                    string path = @"wwwroot\Messages\" + infos.message_id.ToString() + @"\";
                    infos.message_image_urls = getMessageImageUrls(infos.message_id, infos.message_image_count);
                }

                RestfulResult.RestfulData <MessageForShow> rr = new RestfulResult.RestfulData <MessageForShow>();
                rr.Code = 200;
                rr.Message = "success";
                rr.Data = infos;
                return new JsonResult(rr);
            }));
        }