Beispiel #1
0
        private void UpdateRelevance(List <string> pages, Dictionary <string, string> url2id, Dictionary <string, int> url2idx, double[] values, double min, double max)
        {
            CCIndex      index  = CC.Current.Indexes[indexname];
            EngineClient client = null;

            try
            {
                client = EngineClientsPool.FromPool(index);

                foreach (string page in pages)
                {
                    int    idx   = url2idx[page];
                    string id    = url2id[page];
                    double value = 0.50 * Math.Sqrt((values[idx] - min) / (max - min));  // Value will be between 0 and 50%

                    String sqlquery = String.Format("UPDATE {3} SET {2} = '{0}' WHERE id = '{1}';", value, id.Replace("'", "''"), column_score, indexname);

                    //Sys.Log("processing query for ", sqlquery);

                    client.Exec(sqlquery);
                }
            }
            finally
            {
                EngineClientsPool.ToPool(client);
            }
        }
Beispiel #2
0
        /// <summary>
        /// Create a new comment
        /// </summary>
        /// <param name="client"></param>
        /// <param name="docid"></param>
        private void Create(EngineClient client, string docid)
        {
            // Get required inputs
            string message = ensureStrInput("message");

            if (message == null)
            {
                return;
            }
            string replyto = Method.JsonRequest.ValueStr("replyto", "");

            // Create a Comment object
            var comment = new Comment(docid, message, Method.Session.User, replyto);

            // Insert into index
            string sql = comment.ToInsertSQL(indexname);

            client.Exec(sql);
            if (client.HasError())
            {
                SetError(500, client.GetError());
                Sys.LogError($"Error engine: {client.GetError()} for SQL: {sql}");
                return;
            }

            // Return the new comment
            JsonResponse.Set("comment", comment.ToJson());
        }
Beispiel #3
0
        /// <summary>
        /// Like or Unlike a comment
        /// </summary>
        /// <param name="client"></param>
        private void Like(EngineClient client, string docid)
        {
            // Get required inputs
            string commentid = ensureStrInput("commentid");

            if (commentid == null)
            {
                return;
            }

            // Read the comment from the engine
            var comment = ReadComment(client, commentid, docid);

            if (comment == null)
            {
                return;
            }

            // Prevent liking a deleted comment
            if (comment.deleted)
            {
                SetError(400, $"Cannot like/unlike a deleted comment");
                return;
            }

            // Update the comment
            string sql;

            if (comment.likes.Contains(Method.Session.UserId))
            {
                comment.likes.Remove(Method.Session.UserId);
                comment.likedByUser = false;
                sql = comment.ToUpdateDeleteLikeSQL(indexname, Method.Session.UserId);
            }
            else
            {
                comment.likes.Add(Method.Session.UserId);
                comment.likedByUser = true;
                sql = comment.ToUpdateAppendLikeSQL(indexname, Method.Session.UserId);
            }

            int res = client.Exec(sql);

            if (client.HasError())
            {
                SetError(500, client.GetError());
                Sys.LogError($"Error engine: {client.GetError()} for SQL: {sql}");
                return;
            }

            if (res != 1)
            {
                SetError(404, $"This comment could not be found");
                return;
            }

            // Return the updated comment
            JsonResponse.Set("comment", comment.ToJson());
        }
Beispiel #4
0
        /// <summary>
        /// Delete a comment (hard or soft delete supported)
        /// </summary>
        /// <param name="client"></param>
        private void Delete(EngineClient client, string docid)
        {
            // Get required inputs
            string commentid = ensureStrInput("commentid");

            if (commentid == null)
            {
                return;
            }
            bool markAsDeleted = Method.JsonRequest.ValueBoo("markAsDeleted", true);

            // Hard vs soft delete mode
            string sql;

            if (markAsDeleted)
            {
                // Only mark the comment as deleted (can still have replies)
                sql = $"UPDATE {indexname} SET message='',userid='',username='',likes='',deleted='true' WHERE id={Str.SqlValue(commentid)} AND docid={Str.SqlValue(docid)}";
            }
            else
            {
                // Hard delete the comment
                sql = $"DELETE FROM {indexname} WHERE id={Str.SqlValue(commentid)}";
            }

            // Admin can delete any comment
            if (!Method.Session.User.IsAdministrator)
            {
                string userid = Method.Session.UserId;
                sql = $"{sql} AND userid={Str.SqlValue(userid)}";
            }

            // Delete from index
            int res = client.Exec(sql);

            if (client.HasError())
            {
                SetError(500, client.GetError());
                Sys.LogError($"Error engine: {client.GetError()} for SQL: {sql}");
                return;
            }

            // Check that the comment was correctly deleted
            if (res != 1)
            {
                SetError(404, $"This comment could not be found");
                return;
            }
        }
Beispiel #5
0
        /// <summary>
        /// Update the content of a comment
        /// </summary>
        /// <param name="client"></param>
        /// <param name="docid"></param>
        private void Update(EngineClient client, string docid)
        {
            // Get required inputs
            string commentid = ensureStrInput("commentid");

            if (commentid == null)
            {
                return;
            }

            string message = ensureStrInput("message");

            if (message == null)
            {
                return;
            }

            // Read the comment from the engine
            var comment = ReadComment(client, commentid, docid);

            if (comment == null)
            {
                return;
            }

            // Prevent updating a deleted comment
            if (comment.deleted)
            {
                SetError(400, $"Cannot like/unlike a deleted comment");
                return;
            }

            // Update the message content
            comment.message  = message;
            comment.modified = Dat.ToUtc(DateTime.Now);

            string sql = comment.ToUpdateSQL(indexname);

            // Admin can update any comment
            if (!Method.Session.User.IsAdministrator)
            {
                string userid = Method.Session.UserId;
                sql = $"{sql} AND userid={Str.SqlValue(userid)}";
            }

            // Update the index;
            int res = client.Exec(sql);

            if (client.HasError())
            {
                SetError(500, client.GetError());
                Sys.LogError($"Error engine: {client.GetError()} for SQL: {sql}");
                return;
            }

            // Check that the comment was correctly updated
            if (res != 1)
            {
                SetError(404, $"This comment could not be found");
                return;
            }

            // Return the updated comment
            JsonResponse.Set("comment", comment.ToJson());
        }