GeezerVisonThe World Wide Web, restored to 800 pixels of glory. |
| Home|About|Gaming cabinet|Archives|How it works|Starter sites |
GeezerVison is showing github.com — Chat/src/Chat/Global.asax.cs at master · ServiceStackApps/Chat Expand file tree/ Copy pathGlobal.asax.csMore file actions 374 lines (319 loc) · 13.2 KB / Copy pathGlobal.asax.csFile metadata and controls374 lines (319 loc) · 13.2 KB 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 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 using System; using System.Collections.Generic; using System.IO; using System.Linq; using System.Net; using System.Threading.Tasks; using Funq; using ServiceStack; using ServiceStack.Auth; using ServiceStack.Configuration; using ServiceStack.Redis; using ServiceStack.Script; namespace Chat { public partial class AppHost : AppHostBase { public AppHost() : base("Chat", typeof (ServerEventsServices).Assembly) {} public static void Load() => PreInit(); static partial void PreInit(); static partial void PreConfigure(IAppHost appHost); public override void Configure(Container container) { PreConfigure(this); Plugins.Add(new SharpPagesFeature()); Plugins.Add(new ServerEventsFeature()); SetConfig(new HostConfig { DefaultContentType = MimeTypes.Json, AllowSessionIdsInHttpParams = true, UseCamelCase = true, }); this.CustomErrorHttpHandlers.Remove(HttpStatusCode.Forbidden); //Register all Authentication methods you want to enable for this web app. Plugins.Add(new AuthFeature( () => new AuthUserSession(), new IAuthProvider[] { new TwitterAuthProvider(AppSettings), //Sign-in with Twitter new FacebookAuthProvider(AppSettings), //Sign-in with Facebook new GithubAuthProvider(AppSettings), //Sign-in with GitHub OAuth Provider new GoogleAuthProvider(AppSettings), //Sign-in with Google OAuth Provider })); container.RegisterAutoWiredAs<MemoryChatHistory, IChatHistory>(); var redisHost = AppSettings.GetString("RedisHost"); if (redisHost != null) { container.Register<IRedisClientsManager>(new RedisManagerPool(redisHost)); container.Register<IServerEvents>(c => new RedisServerEvents(c.Resolve<IRedisClientsManager>())); container.Resolve<IServerEvents>().Start(); } // for lte IE 9 support + allow connections from local web dev apps Plugins.Add(new CorsFeature( allowOriginWhitelist: new[] { "http://localhost", "http://127.0.0.1:8080", "http://localhost:8080", "http://localhost:8081", "http://null.jsbin.com" }, allowCredentials: true, allowedHeaders: "Content-Type, Allow, Authorization")); } } public interface IChatHistory { long GetNextMessageId(string channel); void Log(string channel, ChatMessage msg); List<ChatMessage> GetRecentChatHistory(string channel, long? afterId, int? take); void Flush(); } public class MemoryChatHistory : IChatHistory { public int DefaultLimit { get; set; } public IServerEvents ServerEvents { get; set; } public MemoryChatHistory() { DefaultLimit = 100; } Dictionary<string, List<ChatMessage>> MessagesMap = new Dictionary<string, List<ChatMessage>>(); public long GetNextMessageId(string channel) { return ServerEvents.GetNextSequence("chatMsg"); } public void Log(string channel, ChatMessage msg) { if (!MessagesMap.TryGetValue(channel, out var msgs)) MessagesMap[channel] = msgs = new List<ChatMessage>(); msgs.Add(msg); } public List<ChatMessage> GetRecentChatHistory(string channel, long? afterId, int? take) { if (!MessagesMap.TryGetValue(channel, out var msgs)) return new List<ChatMessage>(); var ret = msgs.Where(x => x.Id > afterId.GetValueOrDefault()) .Reverse() //get latest logs .Take(take.GetValueOrDefault(DefaultLimit)) .Reverse(); //reverse back return ret.ToList(); } public void Flush() { MessagesMap = new Dictionary<string, List<ChatMessage>>(); } } [Route("/channels/{Channel}/chat")] public class PostChatToChannel : IReturn<ChatMessage> { public string From { get; set; } public string ToUserId { get; set; } public string Channel { get; set; } public string Message { get; set; } public string Selector { get; set; } } public class ChatMessage { public long Id { get; set; } public string Channel { get; set; } public string FromUserId { get; set; } public string FromName { get; set; } public string DisplayName { get; set; } public string Message { get; set; } public string UserAuthId { get; set; } public bool Private { get; set; } } [Route("/channels/{Channel}/raw")] public class PostRawToChannel : IReturnVoid { public string From { get; set; } public string ToUserId { get; set; } public string Channel { get; set; } public string Message { get; set; } public string Selector { get; set; } } [Route("/chathistory")] public class GetChatHistory : IReturn<GetChatHistoryResponse> { public string[] Channels { get; set; } public long? AfterId { get; set; } public int? Take { get; set; } } public class GetChatHistoryResponse { public List<ChatMessage> Results { get; set; } public ResponseStatus ResponseStatus { get; set; } } [Route("/reset")] public class ClearChatHistory : IReturnVoid { } [Route("/reset-serverevents")] public class ResetServerEvents : IReturnVoid { } [Route("/channels/{Channel}/object")] public class PostObjectToChannel : IReturnVoid { public string ToUserId { get; set; } public string Channel { get; set; } public string Selector { get; set; } public CustomType CustomType { get; set; } public SetterType SetterType { get; set; } } public class CustomType { public int Id { get; set; } public string Name { get; set; } } public class SetterType { public int Id { get; set; } public string Name { get; set; } } public class ServerEventsServices : Service { public IServerEvents ServerEvents { get; set; } public IChatHistory ChatHistory { get; set; } public IAppSettings AppSettings { get; set; } public async Task Any(PostRawToChannel request) { if (!IsAuthenticated && AppSettings.Get("LimitRemoteControlToAuthenticatedUsers", false)) throw new HttpError(HttpStatusCode.Forbidden, "You must be authenticated to use remote control."); // Ensure the subscription sending this notification is still active var sub = ServerEvents.GetSubscriptionInfo(request.From); if (sub == null) throw HttpError.NotFound($"Subscription {request.From} does not exist"); // Check to see if this is a private message to a specific user var msg = request.Message?.HtmlEncode(); if (request.ToUserId != null) { // Only notify that specific user await ServerEvents.NotifyUserIdAsync(request.ToUserId, request.Selector, msg); } else { // Notify everyone in the channel for public messages await ServerEvents.NotifyChannelAsync(request.Channel, request.Selector, msg); } } public async Task<object> Any(PostChatToChannel request) { // Ensure the subscription sending this notification is still active var sub = ServerEvents.GetSubscriptionInfo(request.From); if (sub == null) throw HttpError.NotFound("Subscription {0} does not exist".Fmt(request.From)); var channel = request.Channel; var chatMessage = request.Message.IndexOf("{{", StringComparison.Ordinal) >= 0 ? await HostContext.AppHost.ScriptContext.RenderScriptAsync(request.Message, new Dictionary<string, object> { [nameof(Request)] = Request }) : request.Message; // Create a DTO ChatMessage to hold all required info about this message var msg = new ChatMessage { Id = ChatHistory.GetNextMessageId(channel), Channel = request.Channel, FromUserId = sub.UserId, FromName = sub.DisplayName, Message = chatMessage.HtmlEncode(), }; // Check to see if this is a private message to a specific user if (request.ToUserId != null) { // Mark the message as private so it can be displayed differently in Chat msg.Private = true; // Send the message to the specific user Id await ServerEvents.NotifyUserIdAsync(request.ToUserId, request.Selector, msg); // Also provide UI feedback to the user sending the private message so they // can see what was sent. Relay it to all senders active subscriptions var toSubs = ServerEvents.GetSubscriptionInfosByUserId(request.ToUserId); foreach (var toSub in toSubs) { // Change the message format to contain who the private message was sent to msg.Message = $"@{toSub.DisplayName}: {msg.Message}"; await ServerEvents.NotifySubscriptionAsync(request.From, request.Selector, msg); } } else { // Notify everyone in the channel for public messages await ServerEvents.NotifyChannelAsync(request.Channel, request.Selector, msg); } if (!msg.Private) ChatHistory.Log(channel, msg); return msg; } public object Any(GetChatHistory request) { var msgs = request.Channels.Map(x => ChatHistory.GetRecentChatHistory(x, request.AfterId, request.Take)) .SelectMany(x => x) .OrderBy(x => x.Id) .ToList(); return new GetChatHistoryResponse { Results = msgs }; } public object Any(ClearChatHistory request) { ChatHistory.Flush(); return HttpResult.Redirect("/"); } public void Any(ResetServerEvents request) { ServerEvents.Reset(); } public async Task Any(PostObjectToChannel request) { if (request.ToUserId != null) { if (request.CustomType != null) await ServerEvents.NotifyUserIdAsync(request.ToUserId, request.Selector ?? Selector.Id<CustomType>(), request.CustomType); if (request.SetterType != null) await ServerEvents.NotifyUserIdAsync(request.ToUserId, request.Selector ?? Selector.Id<SetterType>(), request.SetterType); } else { if (request.CustomType != null) await ServerEvents.NotifyChannelAsync(request.Channel, request.Selector ?? Selector.Id<CustomType>(), request.CustomType); if (request.SetterType != null) await ServerEvents.NotifyChannelAsync(request.Channel, request.Selector ?? Selector.Id<SetterType>(), request.SetterType); } } } public class Global : System.Web.HttpApplication { protected void Application_Start(object sender, EventArgs e) { new AppHost().Init(); } } [Route("/account")] public class GetUserDetails {} public class GetUserDetailsResponse { public string Provider { get; set; } public string UserId { get; set; } public string UserName { get; set; } public string FullName { get; set; } public string DisplayName { get; set; } public string FirstName { get; set; } public string LastName { get; set; } public string Company { get; set; } public string Email { get; set; } public string PhoneNumber { get; set; } public DateTime? BirthDate { get; set; } public string BirthDateRaw { get; set; } public string Address { get; set; } public string Address2 { get; set; } public string City { get; set; } public string State { get; set; } public string Country { get; set; } public string Culture { get; set; } public string Gender { get; set; } public string Language { get; set; } public string MailAddress { get; set; } public string Nickname { get; set; } public string PostalCode { get; set; } public string TimeZone { get; set; } } [Authenticate] public class UserDetailsService : Service { public object Get(GetUserDetails request) { var session = GetSession(); return session.ConvertTo<GetUserDetailsResponse>(); } } } |
| You are visitor 01402792 — GeezerVison does not store the pages you look up. Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required. |